void server::p_join_list(const std::vector<favchannel> &favorites) { bool first_item = true; /* determine whether we add commas or not */ bool send_keys = false; /* if none of our channels have keys, we can omit the 'x' fillers altogether */ int len = 9; /* JOIN<space>channels<space>keys\r\n\0 */ std::string chanlist; std::string keylist; for (const auto & fav : favorites) { len += fav.name.size(); if (fav.key) { len += fav.key->size(); } if (len >= 512) /* command length exceeds the IRC hard limit, flush it and start from scratch */ { irc_join_list_flush(this, chanlist, keylist, send_keys); chanlist.clear(); keylist.clear(); len = 9; first_item = true; /* list dumped, omit commas once again */ send_keys = false; /* also omit keys until we actually find one */ } if (!first_item) { /* This should be done before the length check, but channel names * are already at least 2 characters long so it would trigger the * flush anyway. */ len += 2; /* add separators but only if it's not the 1st element */ chanlist.push_back(','); keylist.push_back(','); } chanlist += fav.name; if (fav.key) { keylist += *(fav.key); send_keys = true; } else { keylist.push_back('x'); /* 'x' filler for keyless channels so that our JOIN command is always well-formatted */ } first_item = false; } irc_join_list_flush(this, chanlist, keylist, send_keys); }
static void irc_join_list (server *serv, GSList *channels, GSList *keys) { GSList *clist; GSList *klist; GString *c = g_string_new (NULL); GString *k = g_string_new (NULL); int len; int add; int i, j; i = j = 0; len = 9; /* "JOIN<space><space>\r\n" */ clist = channels; klist = keys; while (clist) { /* measure how many bytes this channel would add... */ if (1) { add = strlen (clist->data); if (i != 0) add++; /* comma */ } if (klist->data) { add += strlen (klist->data); } else { add++; /* 'x' filler */ } if (j != 0) add++; /* comma */ /* too big? dump buffer and start a fresh one */ if (len + add > 512) { irc_join_list_flush (serv, c, k); c = g_string_new (NULL); k = g_string_new (NULL); i = j = 0; len = 9; } /* now actually add it to our GStrings */ if (1) { add = strlen (clist->data); if (i != 0) { add++; g_string_append_c (c, ','); } g_string_append (c, clist->data); i++; } if (klist->data) { add += strlen (klist->data); if (j != 0) { add++; g_string_append_c (k, ','); } g_string_append (k, klist->data); j++; } else { add++; if (j != 0) { add++; g_string_append_c (k, ','); } g_string_append_c (k, 'x'); j++; } len += add; klist = klist->next; clist = clist->next; } irc_join_list_flush (serv, c, k); }
static void irc_join_list (server *serv, GSList *favorites) { int first_item = 1; /* determine whether we add commas or not */ int send_keys = 0; /* if none of our channels have keys, we can omit the 'x' fillers altogether */ int len = 9; /* JOIN<space>channels<space>keys\r\n\0 */ favchannel *fav; GString *chanlist = g_string_new (NULL); GString *keylist = g_string_new (NULL); GSList *favlist; favlist = favorites; while (favlist) { fav = favlist->data; len += strlen (fav->name); if (fav->key) { len += strlen (fav->key); } if (len >= 512) /* command length exceeds the IRC hard limit, flush it and start from scratch */ { irc_join_list_flush (serv, chanlist, keylist, send_keys); chanlist = g_string_new (NULL); keylist = g_string_new (NULL); len = 9; first_item = 1; /* list dumped, omit commas once again */ send_keys = 0; /* also omit keys until we actually find one */ } if (!first_item) { /* This should be done before the length check, but channel names * are already at least 2 characters long so it would trigger the * flush anyway. */ len += 2; /* add separators but only if it's not the 1st element */ g_string_append_c (chanlist, ','); g_string_append_c (keylist, ','); } g_string_append (chanlist, fav->name); if (fav->key) { g_string_append (keylist, fav->key); send_keys = 1; } else { g_string_append_c (keylist, 'x'); /* 'x' filler for keyless channels so that our JOIN command is always well-formatted */ } first_item = 0; favlist = favlist->next; } irc_join_list_flush (serv, chanlist, keylist, send_keys); g_slist_free (favlist); }