1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Handle nick changes from other users

This commit is contained in:
James Booth 2012-11-18 21:46:58 +00:00
parent e9d6ba2117
commit 49676e3fae
9 changed files with 227 additions and 26 deletions

View File

@ -709,7 +709,7 @@ _room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
} }
// handle self presence // handle self presence
if (strcmp(room_get_nick_for_room(room), nick) == 0) { if (stanza_is_muc_self_presence(stanza)) {
char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE); char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
// left room // left room
@ -724,12 +724,6 @@ _room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
} }
// handle presence from room members // handle presence from room members
} else {
// roster not yet complete, just add to roster
if (!room_get_roster_received(room)) {
room_add_to_roster(room, nick);
// deal with presence information
} else { } else {
char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE); char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
char *show_str, *status_str; char *show_str, *status_str;
@ -742,7 +736,13 @@ _room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
} }
if ((type != NULL) && (strcmp(type, STANZA_TYPE_UNAVAILABLE) == 0)) { if ((type != NULL) && (strcmp(type, STANZA_TYPE_UNAVAILABLE) == 0)) {
if (stanza_is_room_nick_change(stanza)) {
char *new_nick = stanza_get_new_nick(stanza);
room_add_pending_nick_change(room, new_nick, nick);
room_remove_from_roster(room, nick);
} else {
prof_handle_room_member_offline(room, nick, "offline", status_str); prof_handle_room_member_offline(room, nick, "offline", status_str);
}
} else { } else {
xmpp_stanza_t *show = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SHOW); xmpp_stanza_t *show = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SHOW);
if (show != NULL) { if (show != NULL) {
@ -750,10 +750,20 @@ _room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
} else { } else {
show_str = "online"; show_str = "online";
} }
if (!room_get_roster_received(room)) {
room_add_to_roster(room, nick, show_str, status_str);
} else {
char *old_nick = room_complete_pending_nick_change(room, nick);
if (old_nick != NULL) {
room_add_to_roster(room, nick, show_str, status_str);
prof_handle_room_member_nick_change(room, old_nick, nick);
} else {
prof_handle_room_member_online(room, nick, show_str, status_str); prof_handle_room_member_online(room, nick, show_str, status_str);
} }
} }
} }
}
return 1; return 1;
} }

View File

@ -250,8 +250,8 @@ void
prof_handle_room_member_online(const char * const room, const char * const nick, prof_handle_room_member_online(const char * const room, const char * const nick,
const char * const show, const char * const status) const char * const show, const char * const status)
{ {
room_add_to_roster(room, nick); room_add_to_roster(room, nick, show, status);
win_show_room_member_online(room, nick); win_show_room_member_online(room, nick, show, status);
win_page_off(); win_page_off();
} }
@ -302,6 +302,14 @@ prof_handle_contact_offline(char *contact, char *show, char *status)
} }
} }
void
prof_handle_room_member_nick_change(const char * const room,
const char * const old_nick, const char * const nick)
{
win_show_room_member_nick_change(room, old_nick, nick);
win_page_off();
}
static void static void
_create_config_directory(void) _create_config_directory(void)
{ {

View File

@ -52,5 +52,7 @@ void prof_handle_room_member_online(const char * const room,
void prof_handle_room_member_offline(const char * const room, void prof_handle_room_member_offline(const char * const room,
const char * const nick, const char * const show, const char * const status); const char * const nick, const char * const show, const char * const status);
void prof_handle_leave_room(const char * const room); void prof_handle_leave_room(const char * const room);
void prof_handle_room_member_nick_change(const char * const room,
const char * const old_nick, const char * const nick);
#endif #endif

View File

@ -31,6 +31,7 @@ typedef struct _muc_room_t {
char *room; char *room;
char *nick; char *nick;
GHashTable *roster; GHashTable *roster;
GHashTable *nick_changes;
gboolean roster_received; gboolean roster_received;
} muc_room; } muc_room;
@ -51,6 +52,8 @@ room_join(const char * const room, const char * const nick)
new_room->nick = strdup(nick); new_room->nick = strdup(nick);
new_room->roster = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, new_room->roster = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify)p_contact_free); (GDestroyNotify)p_contact_free);
new_room->nick_changes = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, g_free);
new_room->roster_received = FALSE; new_room->roster_received = FALSE;
g_hash_table_insert(rooms, strdup(room), new_room); g_hash_table_insert(rooms, strdup(room), new_room);
@ -174,12 +177,13 @@ room_parse_room_jid(const char * const full_room_jid, char **room, char **nick)
} }
void void
room_add_to_roster(const char * const room, const char * const nick) room_add_to_roster(const char * const room, const char * const nick,
const char * const show, const char * const status)
{ {
muc_room *chat_room = g_hash_table_lookup(rooms, room); muc_room *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room != NULL) { if (chat_room != NULL) {
PContact contact = p_contact_new(nick, NULL, "online", NULL, NULL); PContact contact = p_contact_new(nick, NULL, show, status, NULL);
g_hash_table_replace(chat_room->roster, strdup(nick), contact); g_hash_table_replace(chat_room->roster, strdup(nick), contact);
} }
} }
@ -228,6 +232,35 @@ room_get_roster_received(const char * const room)
} }
} }
void
room_add_pending_nick_change(const char * const room,
const char * const new_nick, const char * const old_nick)
{
muc_room *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room != NULL) {
g_hash_table_insert(chat_room->nick_changes, strdup(new_nick), strdup(old_nick));
}
}
char *
room_complete_pending_nick_change(const char * const room,
const char * const nick)
{
muc_room *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room != NULL) {
char *old_nick =
strdup(g_hash_table_lookup(chat_room->nick_changes, nick));
if (old_nick != NULL) {
return old_nick;
}
}
return NULL;
}
static void static void
_room_free(muc_room *room) _room_free(muc_room *room)
{ {
@ -244,6 +277,10 @@ _room_free(muc_room *room)
g_hash_table_remove_all(room->roster); g_hash_table_remove_all(room->roster);
room->roster = NULL; room->roster = NULL;
} }
if (room->nick_changes != NULL) {
g_hash_table_remove_all(room->nick_changes);
room->nick_changes = NULL;
}
g_free(room); g_free(room);
} }
room = NULL; room = NULL;

View File

@ -34,7 +34,13 @@ 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); 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, gboolean room_parse_room_jid(const char * const full_room_jid, char **room,
char **nick); char **nick);
void room_add_to_roster(const char * const room, const char * const nick); void room_add_to_roster(const char * const room, const char * const nick,
const char * const show, const char * const status);
void room_add_pending_nick_change(const char * const room,
const char * const new_nick, const char * const old_nick);
char* room_complete_pending_nick_change(const char * const room,
const char * const nick);
GList * room_get_roster(const char * const room); GList * room_get_roster(const char * const room);
void room_set_roster_received(const char * const room); void room_set_roster_received(const char * const room);
gboolean room_get_roster_received(const char * const room); gboolean room_get_roster_received(const char * const room);

View File

@ -236,3 +236,111 @@ stanza_get_delay(xmpp_stanza_t * const stanza, GTimeVal *tv_stamp)
return FALSE; return FALSE;
} }
gboolean
stanza_is_muc_self_presence(xmpp_stanza_t * const stanza)
{
if (stanza == NULL) {
return FALSE;
}
if (strcmp(xmpp_stanza_get_name(stanza), STANZA_NAME_PRESENCE) != 0) {
return FALSE;
}
xmpp_stanza_t *x = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_X);
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;
}
xmpp_stanza_t *x_children = xmpp_stanza_get_children(x);
if (x_children == NULL) {
return FALSE;
}
while (x_children != NULL) {
if (strcmp(xmpp_stanza_get_name(x_children), STANZA_NAME_STATUS) == 0) {
char *code = xmpp_stanza_get_attribute(x_children, STANZA_ATTR_CODE);
if (strcmp(code, "110") == 0) {
return TRUE;
}
}
x_children = xmpp_stanza_get_next(x_children);
}
return FALSE;
}
gboolean
stanza_is_room_nick_change(xmpp_stanza_t * const stanza)
{
if (stanza == NULL) {
return FALSE;
}
if (strcmp(xmpp_stanza_get_name(stanza), STANZA_NAME_PRESENCE) != 0) {
return FALSE;
}
xmpp_stanza_t *x = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_X);
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;
}
xmpp_stanza_t *x_children = xmpp_stanza_get_children(x);
if (x_children == NULL) {
return FALSE;
}
while (x_children != NULL) {
if (strcmp(xmpp_stanza_get_name(x_children), STANZA_NAME_STATUS) == 0) {
char *code = xmpp_stanza_get_attribute(x_children, STANZA_ATTR_CODE);
if (strcmp(code, "303") == 0) {
return TRUE;
}
}
x_children = xmpp_stanza_get_next(x_children);
}
return FALSE;
}
char *
stanza_get_new_nick(xmpp_stanza_t * const stanza)
{
if (!stanza_is_room_nick_change(stanza)) {
return NULL;
} else {
xmpp_stanza_t *x = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_X);
xmpp_stanza_t *x_children = xmpp_stanza_get_children(x);
while (x_children != NULL) {
if (strcmp(xmpp_stanza_get_name(x_children), STANZA_NAME_ITEM) == 0) {
char *nick = xmpp_stanza_get_attribute(x_children, STANZA_ATTR_NICK);
if (nick != NULL) {
return strdup(nick);
}
}
x_children = xmpp_stanza_get_next(x_children);
}
return NULL;
}
}

View File

@ -46,6 +46,7 @@
#define STANZA_NAME_PING "ping" #define STANZA_NAME_PING "ping"
#define STANZA_NAME_TEXT "text" #define STANZA_NAME_TEXT "text"
#define STANZA_NAME_SUBJECT "subject" #define STANZA_NAME_SUBJECT "subject"
#define STANZA_NAME_ITEM "item"
#define STANZA_TYPE_CHAT "chat" #define STANZA_TYPE_CHAT "chat"
#define STANZA_TYPE_GROUPCHAT "groupchat" #define STANZA_TYPE_GROUPCHAT "groupchat"
@ -60,10 +61,12 @@
#define STANZA_ATTR_FROM "from" #define STANZA_ATTR_FROM "from"
#define STANZA_ATTR_STAMP "stamp" #define STANZA_ATTR_STAMP "stamp"
#define STANZA_ATTR_TYPE "type" #define STANZA_ATTR_TYPE "type"
#define STANZA_ATTR_CODE "code"
#define STANZA_ATTR_JID "jid" #define STANZA_ATTR_JID "jid"
#define STANZA_ATTR_NAME "name" #define STANZA_ATTR_NAME "name"
#define STANZA_ATTR_SUBSCRIPTION "subscription" #define STANZA_ATTR_SUBSCRIPTION "subscription"
#define STANZA_ATTR_XMLNS "xmlns" #define STANZA_ATTR_XMLNS "xmlns"
#define STANZA_ATTR_NICK "nick"
#define STANZA_TEXT_AWAY "away" #define STANZA_TEXT_AWAY "away"
#define STANZA_TEXT_DND "dnd" #define STANZA_TEXT_DND "dnd"
@ -73,6 +76,7 @@
#define STANZA_NS_CHATSTATES "http://jabber.org/protocol/chatstates" #define STANZA_NS_CHATSTATES "http://jabber.org/protocol/chatstates"
#define STANZA_NS_MUC "http://jabber.org/protocol/muc" #define STANZA_NS_MUC "http://jabber.org/protocol/muc"
#define STANZA_NS_MUC_USER "http://jabber.org/protocol/muc#user"
#define STANZA_NS_PING "urn:xmpp:ping" #define STANZA_NS_PING "urn:xmpp:ping"
xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx, xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx,
@ -101,4 +105,9 @@ gboolean stanza_contains_chat_state(xmpp_stanza_t *stanza);
gboolean stanza_get_delay(xmpp_stanza_t * const stanza, GTimeVal *tv_stamp); gboolean stanza_get_delay(xmpp_stanza_t * const stanza, GTimeVal *tv_stamp);
gboolean stanza_is_muc_self_presence(xmpp_stanza_t * const stanza);
gboolean stanza_is_room_nick_change(xmpp_stanza_t * const stanza);
char* stanza_get_new_nick(xmpp_stanza_t * const stanza);
#endif #endif

View File

@ -126,10 +126,13 @@ void win_show_room_message(const char * const room_jid, const char * const nick,
void win_show_room_subject(const char * const room_jid, void win_show_room_subject(const char * const room_jid,
const char * const subject); const char * const subject);
void win_show_room_member_offline(const char * const room, const char * const nick); void win_show_room_member_offline(const char * const room, const char * const nick);
void win_show_room_member_online(const char * const room, const char * const nick); void win_show_room_member_online(const char * const room,
const char * const nick, const char * const show, const char * const status);
void win_show_status(const char * const contact); void win_show_status(const char * const contact);
void win_show_wins(void); void win_show_wins(void);
int win_in_private_chat(void); int win_in_private_chat(void);
void win_show_room_member_nick_change(const char * const room,
const char * const old_nick, const char * const nick);
// console window actions // console window actions
void cons_about(void); void cons_about(void);

View File

@ -706,7 +706,8 @@ win_show_room_member_offline(const char * const room, const char * const nick)
} }
void void
win_show_room_member_online(const char * const room, const char * const nick) win_show_room_member_online(const char * const room, const char * const nick,
const char * const show, const char * const status)
{ {
int win_index = _find_prof_win_index(room); int win_index = _find_prof_win_index(room);
WINDOW *win = _wins[win_index].win; WINDOW *win = _wins[win_index].win;
@ -720,6 +721,23 @@ win_show_room_member_online(const char * const room, const char * const nick)
dirty = TRUE; dirty = TRUE;
} }
void
win_show_room_member_nick_change(const char * const room,
const char * const old_nick, const char * const nick)
{
int win_index = _find_prof_win_index(room);
WINDOW *win = _wins[win_index].win;
_win_show_time(win);
wattron(win, COLOUR_ONLINE);
wprintw(win, "** %s is now known as %s\n", old_nick, nick);
wattroff(win, COLOUR_ONLINE);
if (win_index == _curr_prof_win)
dirty = TRUE;
}
void void
win_show_room_history(const char * const room_jid, const char * const nick, win_show_room_history(const char * const room_jid, const char * const nick,
GTimeVal tv_stamp, const char * const message) GTimeVal tv_stamp, const char * const message)