degesch: order the nicknames in /names

This commit is contained in:
Přemysl Eric Janouch 2015-07-26 23:12:36 +02:00
parent 74c9759932
commit 2bde385dc7
1 changed files with 69 additions and 18 deletions

View File

@ -5778,6 +5778,73 @@ irc_handle_rpl_namreply (struct server *s, const struct irc_message *msg)
cstr_split_ignore_empty (nicks, ' ', &channel->names_buf); cstr_split_ignore_empty (nicks, ' ', &channel->names_buf);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct channel_user_sort_entry
{
struct server *s; ///< Server
struct channel_user *channel_user; ///< Channel user
};
static int
channel_user_sort_entry_cmp (const void *entry_a, const void *entry_b)
{
const struct channel_user_sort_entry *a = entry_a;
const struct channel_user_sort_entry *b = entry_b;
struct server *s = a->s;
// First order by the most significant channel user prefix
const char *prio_a = strchr (s->irc_chanuser_prefixes,
*a->channel_user->prefixes.str);
const char *prio_b = strchr (s->irc_chanuser_prefixes,
*b->channel_user->prefixes.str);
// Put unrecognized prefixes at the end of the list
if (prio_a || prio_b)
{
if (!prio_a) return 1;
if (!prio_b) return -1;
if (prio_a != prio_b)
return prio_a - prio_b;
}
return irc_server_strcmp (s,
a->channel_user->user->nickname,
b->channel_user->user->nickname);
}
static char *
make_channel_users_list (struct server *s, struct channel *channel)
{
size_t n_users = 0;
LIST_FOR_EACH (struct channel_user, iter, channel->users)
n_users++;
struct channel_user_sort_entry entries[n_users];
size_t i = 0;
LIST_FOR_EACH (struct channel_user, iter, channel->users)
{
entries[i].s = s;
entries[i].channel_user = iter;
i++;
}
qsort (entries, n_users, sizeof *entries, channel_user_sort_entry_cmp);
struct str list;
str_init (&list);
for (i = 0; i < n_users; i++)
{
irc_get_channel_user_prefix (s, entries[i].channel_user, &list);
str_append (&list, entries[i].channel_user->user->nickname);
str_append_c (&list, ' ');
}
if (list.len)
list.str[--list.len] = '\0';
return str_steal (&list);
}
static void static void
irc_sync_channel_user (struct server *s, struct channel *channel, irc_sync_channel_user (struct server *s, struct channel *channel,
const char *nickname, const char *prefixes) const char *nickname, const char *prefixes)
@ -5802,24 +5869,6 @@ irc_sync_channel_user (struct server *s, struct channel *channel,
} }
} }
static char *
make_channel_users_list (struct server *s, struct channel *channel)
{
struct str_vector v;
str_vector_init (&v);
LIST_FOR_EACH (struct channel_user, iter, channel->users)
{
struct str item;
str_init (&item);
irc_get_channel_user_prefix (s, iter, &item);
str_append (&item, iter->user->nickname);
str_vector_add_owned (&v, str_steal (&item));
}
char *result = join_str_vector (&v, ' ');
str_vector_free (&v);
return result;
}
static void static void
irc_process_names (struct server *s, struct channel *channel) irc_process_names (struct server *s, struct channel *channel)
{ {
@ -5861,6 +5910,8 @@ irc_process_names (struct server *s, struct channel *channel)
free (all_users); free (all_users);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void static void
irc_handle_rpl_endofnames (struct server *s, const struct irc_message *msg) irc_handle_rpl_endofnames (struct server *s, const struct irc_message *msg)
{ {