diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2015-06-06 00:38:58 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2015-06-06 00:44:09 +0200 |
commit | ac6ee9ea8023c8e0399f8cd406f6eee4c57c91c9 (patch) | |
tree | f3799f89be9cc0782cf5cda0f5b5692241d0f9c1 | |
parent | f9eddb8ee21e58e7a5896c01e3df7f1d88b9d2da (diff) | |
download | xK-ac6ee9ea8023c8e0399f8cd406f6eee4c57c91c9.tar.gz xK-ac6ee9ea8023c8e0399f8cd406f6eee4c57c91c9.tar.xz xK-ac6ee9ea8023c8e0399f8cd406f6eee4c57c91c9.zip |
degesch: refactor irc_process_names()
-rw-r--r-- | degesch.c | 61 |
1 files changed, 33 insertions, 28 deletions
@@ -4680,11 +4680,35 @@ irc_handle_rpl_namreply (struct server *s, const struct irc_message *msg) } static void +irc_sync_channel_user (struct server *s, struct channel *channel, + const char *nickname, const char *prefixes) +{ + struct user *user = irc_get_or_make_user (s, nickname); + struct channel_user *channel_user = + irc_channel_get_user (channel, user); + if (!channel_user) + { + irc_channel_link_user (channel, user, prefixes); + return; + } + + user_unref (user); + + // If our idea of the user's modes disagrees with what the server's + // sent us (the most powerful modes differ), use the latter one + if (channel_user->prefixes.str[0] != prefixes[0]) + { + str_reset (&channel_user->prefixes); + str_append (&channel_user->prefixes, prefixes); + } +} + +static void irc_process_names (struct server *s, struct channel *channel) { - struct str_map map; - str_map_init (&map); - map.key_xfrm = s->irc_strxfrm; + struct str_map present; + str_map_init (&present); + present.key_xfrm = s->irc_strxfrm; struct str_vector *updates = &channel->names_buf; for (size_t i = 0; i < updates->len; i++) @@ -4694,38 +4718,19 @@ irc_process_names (struct server *s, struct channel *channel) const char *nickname = item + n_prefixes; // Store the nickname in a hashset - str_map_set (&map, nickname, (void *) 1); - - char prefixes[n_prefixes + 1]; - memcpy (prefixes, item, n_prefixes); - prefixes[n_prefixes] = '\0'; - - struct user *user = irc_get_or_make_user (s, nickname); - struct channel_user *channel_user = - irc_channel_get_user (channel, user); - if (!channel_user) - { - irc_channel_link_user (channel, user, prefixes); - continue; - } - - user_unref (user); - - // If our idea of the user's modes disagrees with what the server's - // sent us (the most powerful modes differ), use the latter one - if (channel_user->prefixes.str[0] == prefixes[0]) - continue; + str_map_set (&present, nickname, (void *) 1); - str_reset (&channel_user->prefixes); - str_append (&channel_user->prefixes, prefixes); + char *prefixes = xstrndup (item, n_prefixes); + irc_sync_channel_user (s, channel, nickname, prefixes); + free (prefixes); } // Get rid of channel users missing from "updates" LIST_FOR_EACH (struct channel_user, iter, channel->users) - if (!str_map_find (&map, iter->user->nickname)) + if (!str_map_find (&present, iter->user->nickname)) irc_channel_unlink_user (channel, iter); - str_map_free (&map); + str_map_free (&present); str_vector_reset (&channel->names_buf); struct str_vector v; |