Changeset e3e2059


Ignore:
Timestamp:
2015-04-06T12:35:57Z (9 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
8bcd160
Parents:
69982f8
git-author:
dequis <dx@…> (06-04-15 12:30:30)
git-committer:
dequis <dx@…> (06-04-15 12:35:57)
Message:

irc: split bee_irc_chat_name_hint in a few functions

Also split underscore_dedupe from nick_dedupe.

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • irc.h

    r69982f8 re3e2059  
    299299void irc_channel_name_strip(char *name);
    300300int irc_channel_name_cmp(const char *a_, const char *b_);
     301char *irc_channel_name_gen(bee_t *bee, const char *name);
     302gboolean irc_channel_name_hint(irc_channel_t *ic, const char *name);
    301303void irc_channel_update_ops(irc_channel_t *ic, char *value);
    302304char *set_eval_irc_channel_ops(struct set *set, char *value);
  • irc_channel.c

    r69982f8 re3e2059  
    556556}
    557557
     558gboolean irc_channel_is_unused(bee_t *bee, char *name)
     559{
     560        char *type, *chat_type;
     561        irc_channel_t *oic;
     562
     563        if (!irc_channel_name_ok(name)) {
     564                return FALSE;
     565        }
     566
     567        if (!(oic = irc_channel_by_name(bee->ui_data, name))) {
     568                return TRUE;
     569        }
     570
     571        type = set_getstr(&oic->set, "type");
     572        chat_type = set_getstr(&oic->set, "chat_type");
     573
     574        if (type && chat_type && oic->data == FALSE &&
     575            strcmp(type, "chat") == 0 &&
     576            strcmp(chat_type, "groupchat") == 0) {
     577                /* There's a channel with this name already, but it looks
     578                   like it's not in use yet. Most likely the IRC client
     579                   rejoined the channel after a reconnect. Remove it so
     580                   we can reuse its name. */
     581                irc_channel_free(oic);
     582                return TRUE;
     583        }
     584
     585        return FALSE;
     586}
     587
     588char *irc_channel_name_gen(bee_t *bee, const char *hint)
     589{
     590        char name[MAX_NICK_LENGTH + 1] = { 0 };
     591        char *translit_name;
     592        gsize bytes_written;
     593
     594        translit_name = g_convert_with_fallback(hint, -1, "ASCII//TRANSLIT", "UTF-8", "", NULL, &bytes_written, NULL);
     595        if (bytes_written > MAX_NICK_LENGTH) {
     596                translit_name[MAX_NICK_LENGTH] = '\0';
     597        }
     598
     599        name[0] = '#';
     600        strncpy(name + 1, translit_name, MAX_NICK_LENGTH - 1);
     601        name[MAX_NICK_LENGTH] = '\0';
     602
     603        g_free(translit_name);
     604
     605        irc_channel_name_strip(name);
     606
     607        if (set_getbool(&bee->set, "lcnicks")) {
     608                nick_lc(bee->ui_data, name + 1);
     609        }
     610
     611        while (!irc_channel_is_unused(bee, name)) {
     612                underscore_dedupe(name);
     613        }
     614
     615        return g_strdup(name);
     616}
     617
     618gboolean irc_channel_name_hint(irc_channel_t *ic, const char *name)
     619{
     620        irc_t *irc = ic->irc;
     621        char *full_name;
     622
     623        /* Don't rename a channel if the user's in it already. */
     624        if (ic->flags & IRC_CHANNEL_JOINED) {
     625                return FALSE;
     626        }
     627
     628        if (!(full_name = irc_channel_name_gen(irc->b, name))) {
     629                return FALSE;
     630        }
     631
     632        g_free(ic->name);
     633        ic->name = full_name;
     634
     635        return TRUE;
     636}
     637
    558638static gint irc_channel_user_cmp(gconstpointer a_, gconstpointer b_)
    559639{
  • irc_im.c

    r69982f8 re3e2059  
    696696static gboolean bee_irc_chat_name_hint(bee_t *bee, struct groupchat *c, const char *name)
    697697{
    698         irc_t *irc = bee->ui_data;
    699         irc_channel_t *ic = c->ui_data, *oic;
    700         char *stripped, *full_name;
    701         gsize bytes_written;
    702 
    703         if (ic == NULL) {
    704                 return FALSE;
    705         }
    706 
    707         /* Don't rename a channel if the user's in it already. */
    708         if (ic->flags & IRC_CHANNEL_JOINED) {
    709                 return FALSE;
    710         }
    711 
    712         stripped = g_convert_with_fallback(name, -1, "ASCII//TRANSLIT", "UTF-8", "", NULL, &bytes_written, NULL);
    713         if (bytes_written > MAX_NICK_LENGTH) {
    714                 stripped[MAX_NICK_LENGTH] = '\0';
    715         }
    716 
    717         irc_channel_name_strip(stripped);
    718         if (set_getbool(&bee->set, "lcnicks")) {
    719                 nick_lc(irc, stripped);
    720         }
    721 
    722         if (stripped[0] == '\0') {
    723                 g_free(stripped);
    724                 return FALSE;
    725         }
    726 
    727         full_name = g_strdup_printf("#%s", stripped);
    728         g_free(stripped);
    729         if ((oic = irc_channel_by_name(irc, full_name))) {
    730                 char *type, *chat_type;
    731 
    732                 type = set_getstr(&oic->set, "type");
    733                 chat_type = set_getstr(&oic->set, "chat_type");
    734 
    735                 if (type && chat_type && oic->data == FALSE &&
    736                     strcmp(type, "chat") == 0 &&
    737                     strcmp(chat_type, "groupchat") == 0) {
    738                         /* There's a channel with this name already, but it looks
    739                            like it's not in use yet. Most likely the IRC client
    740                            rejoined the channel after a reconnect. Remove it so
    741                            we can reuse its name. */
    742                         irc_channel_free(oic);
    743                 } else {
    744                         g_free(full_name);
    745                         return FALSE;
    746                 }
    747         }
    748 
    749         g_free(ic->name);
    750         ic->name = full_name;
    751 
    752         return TRUE;
     698        return irc_channel_name_hint(c->ui_data, name);
    753699}
    754700
  • nick.c

    r69982f8 re3e2059  
    214214}
    215215
     216/* Used for nicks and channel names too! */
     217void underscore_dedupe(char nick[MAX_NICK_LENGTH + 1])
     218{
     219        if (strlen(nick) < (MAX_NICK_LENGTH - 1)) {
     220                nick[strlen(nick) + 1] = 0;
     221                nick[strlen(nick)] = '_';
     222        } else {
     223                /* We've got no more space for underscores,
     224                   so truncate it and replace the last three
     225                   chars with a random "_XX" suffix */
     226                int len = truncate_utf8(nick, MAX_NICK_LENGTH - 3);
     227                nick[len] = '_';
     228                g_snprintf(nick + len + 1, 3, "%2x", rand());
     229        }
     230}
     231
    216232void nick_dedupe(bee_user_t *bu, char nick[MAX_NICK_LENGTH + 1])
    217233{
     
    224240        while (!nick_ok(irc, nick) ||
    225241               ((iu = irc_user_by_name(irc, nick)) && iu->bu != bu)) {
    226                 if (strlen(nick) < (MAX_NICK_LENGTH - 1)) {
    227                         nick[strlen(nick) + 1] = 0;
    228                         nick[strlen(nick)] = '_';
    229                 } else {
    230                         /* We've got no more space for underscores,
    231                            so truncate it and replace the last three
    232                            chars with a random "_XX" suffix */
    233                         int len = truncate_utf8(nick, MAX_NICK_LENGTH - 3);
    234                         nick[len] = '_';
    235                         g_snprintf(nick + len + 1, 3, "%2x", rand());
    236                 }
     242
     243                underscore_dedupe(nick);
    237244
    238245                if (inf_protection-- == 0) {
  • nick.h

    r69982f8 re3e2059  
    2828char *nick_get(bee_user_t *bu);
    2929char *nick_gen(bee_user_t *bu);
     30void underscore_dedupe(char nick[MAX_NICK_LENGTH + 1]);
    3031void nick_dedupe(bee_user_t * bu, char nick[MAX_NICK_LENGTH + 1]);
    3132int nick_saved(bee_user_t *bu);
Note: See TracChangeset for help on using the changeset viewer.