1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Refactroed self presence checks, handle null stanzas

Issue #341
This commit is contained in:
James Booth 2014-05-06 22:07:44 +01:00
parent ba8d1325ad
commit 143c5a3493
2 changed files with 74 additions and 109 deletions

View File

@ -667,8 +667,8 @@ _muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
return 1; return 1;
} }
char *room = from_jid->barejid; char *from_room = from_jid->barejid;
char *nick = from_jid->resourcepart; char *from_nick = from_jid->resourcepart;
// handle self presence // handle self presence
if (stanza_is_muc_self_presence(stanza, jabber_get_fulljid())) { if (stanza_is_muc_self_presence(stanza, jabber_get_fulljid())) {
@ -679,19 +679,19 @@ _muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
// leave room if not self nick change // leave room if not self nick change
if (new_nick != NULL) { if (new_nick != NULL) {
muc_set_room_pending_nick_change(room, new_nick); muc_set_room_pending_nick_change(from_room, new_nick);
} else { } else {
handle_leave_room(room); handle_leave_room(from_room);
} }
// handle self nick change // handle self nick change
} else if (muc_is_room_pending_nick_change(room)) { } else if (muc_is_room_pending_nick_change(from_room)) {
muc_complete_room_nick_change(room, nick); muc_complete_room_nick_change(from_room, from_nick);
handle_room_nick_change(room, nick); handle_room_nick_change(from_room, from_nick);
// handle roster complete // handle roster complete
} else if (!muc_get_roster_received(room)) { } else if (!muc_get_roster_received(from_room)) {
handle_room_roster_complete(room); handle_room_roster_complete(from_room);
} }
// handle presence from room members // handle presence from room members
@ -714,28 +714,28 @@ _muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
if (stanza_is_room_nick_change(stanza)) { if (stanza_is_room_nick_change(stanza)) {
char *new_nick = stanza_get_new_nick(stanza); char *new_nick = stanza_get_new_nick(stanza);
if (new_nick != NULL) { if (new_nick != NULL) {
muc_set_roster_pending_nick_change(room, new_nick, nick); muc_set_roster_pending_nick_change(from_room, new_nick, from_nick);
free(new_nick); free(new_nick);
} }
} else { } else {
handle_room_member_offline(room, nick, "offline", status_str); handle_room_member_offline(from_room, from_nick, "offline", status_str);
} }
} else { } else {
char *show_str = stanza_get_show(stanza, "online"); char *show_str = stanza_get_show(stanza, "online");
if (!muc_get_roster_received(room)) { if (!muc_get_roster_received(from_room)) {
muc_add_to_roster(room, nick, show_str, status_str, caps_key); muc_add_to_roster(from_room, from_nick, show_str, status_str, caps_key);
} else { } else {
char *old_nick = muc_complete_roster_nick_change(room, nick); char *old_nick = muc_complete_roster_nick_change(from_room, from_nick);
if (old_nick != NULL) { if (old_nick != NULL) {
muc_add_to_roster(room, nick, show_str, status_str, caps_key); muc_add_to_roster(from_room, from_nick, show_str, status_str, caps_key);
handle_room_member_nick_change(room, old_nick, nick); handle_room_member_nick_change(from_room, old_nick, from_nick);
free(old_nick); free(old_nick);
} else { } else {
if (!muc_nick_in_roster(room, nick)) { if (!muc_nick_in_roster(from_room, from_nick)) {
handle_room_member_online(room, nick, show_str, status_str, caps_key); handle_room_member_online(from_room, from_nick, show_str, status_str, caps_key);
} else { } else {
handle_room_member_presence(room, nick, show_str, status_str, caps_key); handle_room_member_presence(from_room, from_nick, show_str, status_str, caps_key);
} }
} }
} }

View File

@ -634,128 +634,93 @@ gboolean
stanza_is_muc_self_presence(xmpp_stanza_t * const stanza, stanza_is_muc_self_presence(xmpp_stanza_t * const stanza,
const char * const self_jid) const char * const self_jid)
{ {
if (stanza == NULL) { // no stanza, or not presence stanza
return FALSE; if ((stanza == NULL) || (g_strcmp0(xmpp_stanza_get_name(stanza), STANZA_NAME_PRESENCE) != 0)) {
}
if (strcmp(xmpp_stanza_get_name(stanza), STANZA_NAME_PRESENCE) != 0) {
return FALSE; return FALSE;
} }
xmpp_stanza_t *x = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_X); // muc user namespaced x element
xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
if (x == NULL) { if (x != NULL) {
return FALSE;
}
char *ns = xmpp_stanza_get_ns(x);
if (ns == NULL) {
return FALSE;
}
if (strcmp(ns, STANZA_NS_MUC_USER) != 0) {
return FALSE;
}
// check for status child element with 110 code
xmpp_stanza_t *x_children = xmpp_stanza_get_children(x); xmpp_stanza_t *x_children = xmpp_stanza_get_children(x);
if (x_children == NULL) {
return FALSE;
}
while (x_children != NULL) { while (x_children != NULL) {
if (strcmp(xmpp_stanza_get_name(x_children), STANZA_NAME_STATUS) == 0) { if (g_strcmp0(xmpp_stanza_get_name(x_children), STANZA_NAME_STATUS) == 0) {
char *code = xmpp_stanza_get_attribute(x_children, STANZA_ATTR_CODE); char *code = xmpp_stanza_get_attribute(x_children, STANZA_ATTR_CODE);
if (strcmp(code, "110") == 0) { if (g_strcmp0(code, "110") == 0) {
return TRUE; return TRUE;
} }
} }
x_children = xmpp_stanza_get_next(x_children); x_children = xmpp_stanza_get_next(x_children);
} }
// for older server that don't send status 110 // check for item child element with jid property
x_children = xmpp_stanza_get_children(x); xmpp_stanza_t *item = xmpp_stanza_get_child_by_name(x, STANZA_NAME_ITEM);
while (x_children != NULL) { if (item != NULL) {
if (strcmp(xmpp_stanza_get_name(x_children), STANZA_NAME_ITEM) == 0) { char *jid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID);
char *jid = xmpp_stanza_get_attribute(x_children, STANZA_ATTR_JID);
if (jid != NULL) { if (jid != NULL) {
if (g_str_has_prefix(jid, self_jid)) { if (g_str_has_prefix(self_jid, jid)) {
return TRUE; return TRUE;
} }
} }
} }
x_children = xmpp_stanza_get_next(x_children);
}
// for servers that don't send status 110 or Jid property // check if 'from' attribute identifies this user
// first check if 'from' attribute identifies this user
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
if (from != NULL) { if (from != NULL) {
Jid *jidp = jid_create(from); Jid *from_jid = jid_create(from);
if (muc_room_is_active(jidp->barejid)) { if (muc_room_is_active(from_jid->barejid)) {
char *nick = muc_get_room_nick(jidp->barejid); char *nick = muc_get_room_nick(from_jid->barejid);
if (g_strcmp0(jidp->resourcepart, nick) == 0) { if (g_strcmp0(from_jid->resourcepart, nick) == 0) {
return TRUE; return TRUE;
} }
} }
jid_destroy(jidp);
}
// secondly check if the new nickname maps to a pending nick change for this user // check if a new nickname maps to a pending nick change for this user
if (from != NULL) { if (muc_is_room_pending_nick_change(from_jid->barejid)) {
Jid *jidp = jid_create(from); char *new_nick = from_jid->resourcepart;
if (muc_is_room_pending_nick_change(jidp->barejid)) {
char *new_nick = jidp->resourcepart;
if (new_nick != NULL) { if (new_nick != NULL) {
char *nick = muc_get_room_nick(jidp->barejid); char *nick = muc_get_room_nick(from_jid->barejid);
char *old_nick = muc_get_old_nick(jidp->barejid, new_nick); char *old_nick = muc_get_old_nick(from_jid->barejid, new_nick);
if (g_strcmp0(old_nick, nick) == 0) { if (g_strcmp0(old_nick, nick) == 0) {
return TRUE; return TRUE;
} }
} }
} }
jid_destroy(jidp);
jid_destroy(from_jid);
}
} }
// self presence not found
return FALSE; return FALSE;
} }
gboolean gboolean
stanza_is_room_nick_change(xmpp_stanza_t * const stanza) stanza_is_room_nick_change(xmpp_stanza_t * const stanza)
{ {
if (stanza == NULL) { // no stanza, or not presence stanza
return FALSE; if ((stanza == NULL) || (g_strcmp0(xmpp_stanza_get_name(stanza), STANZA_NAME_PRESENCE) != 0)) {
}
if (strcmp(xmpp_stanza_get_name(stanza), STANZA_NAME_PRESENCE) != 0) {
return FALSE; return FALSE;
} }
xmpp_stanza_t *x = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_X); // muc user namespaced x element
xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
if (x == NULL) { if (x != NULL) {
return FALSE;
}
char *ns = xmpp_stanza_get_ns(x);
if (ns == NULL) {
return FALSE;
}
if (strcmp(ns, STANZA_NS_MUC_USER) != 0) {
return FALSE;
}
// check for status child element with 303 code
xmpp_stanza_t *x_children = xmpp_stanza_get_children(x); xmpp_stanza_t *x_children = xmpp_stanza_get_children(x);
if (x_children == NULL) {
return FALSE;
}
while (x_children != NULL) { while (x_children != NULL) {
if (strcmp(xmpp_stanza_get_name(x_children), STANZA_NAME_STATUS) == 0) { if (g_strcmp0(xmpp_stanza_get_name(x_children), STANZA_NAME_STATUS) == 0) {
char *code = xmpp_stanza_get_attribute(x_children, STANZA_ATTR_CODE); char *code = xmpp_stanza_get_attribute(x_children, STANZA_ATTR_CODE);
if (strcmp(code, "303") == 0) { if (g_strcmp0(code, "303") == 0) {
return TRUE; return TRUE;
} }
} }
x_children = xmpp_stanza_get_next(x_children); x_children = xmpp_stanza_get_next(x_children);
} }
}
return FALSE; return FALSE;
} }