1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00

Moved jid related functions to common

This commit is contained in:
James Booth 2013-01-12 01:44:21 +00:00
parent bfd7362e2a
commit d7b969b135
6 changed files with 117 additions and 115 deletions

View File

@ -167,3 +167,103 @@ prof_getline(FILE *stream)
free(buf);
return s;
}
/*
* Given a full room JID of the form
* room@server/nick
* Will create two new strings and point room and nick to them e.g.
* *room = "room@server", *nick = "nick"
* The strings must be freed by the caller
* Returns TRUE if the JID was parsed successfully, FALSE otherwise
*/
gboolean
parse_room_jid(const char * const full_room_jid, char **room, char **nick)
{
char **tokens = g_strsplit(full_room_jid, "/", 0);
if (tokens == NULL || tokens[0] == NULL || tokens[1] == NULL) {
return FALSE;
} else {
*room = strdup(tokens[0]);
*nick = strdup(tokens[1]);
g_strfreev(tokens);
return TRUE;
}
}
/*
* Given a room name, and a nick name create and return a full JID of the form
* room@server/nick
* Will return a newly created string that must be freed by the caller
*/
char *
create_full_room_jid(const char * const room, const char * const nick)
{
GString *full_jid = g_string_new(room);
g_string_append(full_jid, "/");
g_string_append(full_jid, nick);
char *result = strdup(full_jid->str);
g_string_free(full_jid, TRUE);
return result;
}
/*
* Returns TRUE if the JID is a room JID
* The test is that the passed JID does not contain a "/"
*/
gboolean
jid_is_room(const char * const room_jid)
{
gchar *result = g_strrstr(room_jid, "/");
return (result == NULL);
}
/*
* Get the room name part of the full JID, e.g.
* Full JID = "test@conference.server/person"
* returns "test@conference.server"
*/
char *
get_room_from_full_jid(const char * const full_room_jid)
{
char **tokens = g_strsplit(full_room_jid, "/", 0);
char *room_part;
if (tokens == NULL || tokens[0] == NULL) {
return NULL;
} else {
room_part = strdup(tokens[0]);
g_strfreev(tokens);
return room_part;
}
}
/*
* Get the nickname part of the full JID, e.g.
* Full JID = "test@conference.server/person"
* returns "person"
*/
char *
get_nick_from_full_jid(const char * const full_room_jid)
{
char **tokens = g_strsplit(full_room_jid, "/", 0);
char *nick_part;
if (tokens == NULL || tokens[1] == NULL) {
return NULL;
} else {
nick_part = strdup(tokens[1]);
g_strfreev(tokens);
return nick_part;
}
}

View File

@ -44,4 +44,12 @@ int str_contains(char str[], int size, char ch);
char* encode_xml(const char * const xml);
char * prof_getline(FILE *stream);
gboolean jid_is_room(const char * const room_jid);
char * create_full_room_jid(const char * const room,
const char * const nick);
char * get_room_from_full_jid(const char * const full_room_jid);
char * get_nick_from_full_jid(const char * const full_room_jid);
gboolean parse_room_jid(const char * const full_room_jid, char **room,
char **nick);
#endif

View File

@ -349,7 +349,7 @@ jabber_get_subscription_requests(void)
void
jabber_join(const char * const room, const char * const nick)
{
char *full_room_jid = room_create_full_room_jid(room, nick);
char *full_room_jid = create_full_room_jid(room, nick);
xmpp_stanza_t *presence = stanza_create_room_join_presence(jabber_conn.ctx,
full_room_jid);
xmpp_send(jabber_conn.conn, presence);
@ -363,7 +363,7 @@ jabber_join(const char * const room, const char * const nick)
void
jabber_change_room_nick(const char * const room, const char * const nick)
{
char *full_room_jid = room_create_full_room_jid(room, nick);
char *full_room_jid = create_full_room_jid(room, nick);
xmpp_stanza_t *presence = stanza_create_room_newnick_presence(jabber_conn.ctx,
full_room_jid);
xmpp_send(jabber_conn.conn, presence);
@ -458,7 +458,7 @@ jabber_update_presence(jabber_presence_t status, const char * const msg,
while (rooms != NULL) {
char *room = rooms->data;
char *nick = muc_get_room_nick(room);
char *full_room_jid = room_create_full_room_jid(room, nick);
char *full_room_jid = create_full_room_jid(room, nick);
xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, full_room_jid);
xmpp_send(jabber_conn.conn, presence);
@ -572,14 +572,14 @@ _groupchat_message_handler(xmpp_stanza_t * const stanza)
gchar *room_jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
// handle room broadcasts
if (room_from_jid_is_room(room_jid)) {
if (jid_is_room(room_jid)) {
xmpp_stanza_t *subject = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SUBJECT);
// handle subject
if (subject != NULL) {
message = xmpp_stanza_get_text(subject);
if (message != NULL) {
room = room_get_room_from_full_jid(room_jid);
room = get_room_from_full_jid(room_jid);
prof_handle_room_subject(room, message);
}
@ -600,7 +600,7 @@ _groupchat_message_handler(xmpp_stanza_t * const stanza)
}
// room jid not of form room/nick
if (!room_parse_room_jid(room_jid, &room, &nick)) {
if (!parse_room_jid(room_jid, &room, &nick)) {
log_error("Could not parse room jid: %s", room_jid);
g_free(room);
g_free(nick);
@ -977,7 +977,7 @@ _room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
char *room = NULL;
char *nick = NULL;
if (!room_parse_room_jid(jid, &room, &nick)) {
if (!parse_room_jid(jid, &room, &nick)) {
log_error("Could not parse room jid: %s", room);
g_free(room);
g_free(nick);

View File

@ -344,105 +344,6 @@ muc_complete_roster_nick_change(const char * const room,
return NULL;
}
/*
* Get the room name part of the full JID, e.g.
* Full JID = "test@conference.server/person"
* returns "test@conference.server"
*/
char *
room_get_room_from_full_jid(const char * const full_room_jid)
{
char **tokens = g_strsplit(full_room_jid, "/", 0);
char *room_part;
if (tokens == NULL || tokens[0] == NULL) {
return NULL;
} else {
room_part = strdup(tokens[0]);
g_strfreev(tokens);
return room_part;
}
}
/*
* Returns TRUE if the JID is a room JID
* The test is that the passed JID does not contain a "/"
*/
gboolean
room_from_jid_is_room(const char * const room_jid)
{
gchar *result = g_strrstr(room_jid, "/");
return (result == NULL);
}
/*
* Get the nickname part of the full JID, e.g.
* Full JID = "test@conference.server/person"
* returns "person"
*/
char *
room_get_nick_from_full_jid(const char * const full_room_jid)
{
char **tokens = g_strsplit(full_room_jid, "/", 0);
char *nick_part;
if (tokens == NULL || tokens[1] == NULL) {
return NULL;
} else {
nick_part = strdup(tokens[1]);
g_strfreev(tokens);
return nick_part;
}
}
/*
* Given a room name, and a nick name create and return a full JID of the form
* room@server/nick
* Will return a newly created string that must be freed by the caller
*/
char *
room_create_full_room_jid(const char * const room, const char * const nick)
{
GString *full_jid = g_string_new(room);
g_string_append(full_jid, "/");
g_string_append(full_jid, nick);
char *result = strdup(full_jid->str);
g_string_free(full_jid, TRUE);
return result;
}
/*
* Given a full room JID of the form
* room@server/nick
* Will create two new strings and point room and nick to them e.g.
* *room = "room@server", *nick = "nick"
* The strings must be freed by the caller
* Returns TRUE if the JID was parsed successfully, FALSE otherwise
*/
gboolean
room_parse_room_jid(const char * const full_room_jid, char **room, char **nick)
{
char **tokens = g_strsplit(full_room_jid, "/", 0);
if (tokens == NULL || tokens[0] == NULL || tokens[1] == NULL) {
return FALSE;
} else {
*room = strdup(tokens[0]);
*nick = strdup(tokens[1]);
g_strfreev(tokens);
return TRUE;
}
}
static void
_free_room(muc_room *room)
{

View File

@ -52,12 +52,4 @@ void muc_set_roster_pending_nick_change(const char * const room,
char* muc_complete_roster_nick_change(const char * const room,
const char * const nick);
gboolean room_from_jid_is_room(const char * const room_jid);
char * room_create_full_room_jid(const char * const room,
const char * const nick);
char * room_get_room_from_full_jid(const char * const full_room_jid);
char * room_get_nick_from_full_jid(const char * const full_room_jid);
gboolean room_parse_room_jid(const char * const full_room_jid, char **room,
char **nick);
#endif

View File

@ -47,6 +47,7 @@
#include "chat_log.h"
#include "chat_session.h"
#include "command.h"
#include "common.h"
#include "contact.h"
#include "contact_list.h"
#include "log.h"
@ -330,7 +331,7 @@ ui_show_incoming_msg(const char * const from, const char * const message,
win_type_t win_type;
if (priv) {
win_type = WIN_PRIVATE;
display_from = room_get_nick_from_full_jid(from);
display_from = get_nick_from_full_jid(from);
} else {
win_type = WIN_CHAT;
display_from = strdup(from);