Changeset 7d27962


Ignore:
Timestamp:
2012-02-08T00:24:12Z (13 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
e0a0a42
Parents:
d2abcae
Message:

Take t.co URL lengtheWshortening into account when counting message length
before posting a Twitter message. Disable this functionality by default for
identi.ca accounts. Hardcode post-t.co URL length to 20 since it's probably
not going to change any time soon. Bug #855. Patch by Artem Savkov, thanks!

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • doc/user-guide/commands.xml

    rd2abcae r7d27962  
    10321032
    10331033        </bitlbee-setting>
     1034       
     1035        <bitlbee-setting name="target_url_length" type="integer" scope="account">
     1036                <default>20</default>
     1037
     1038                <description>
     1039                        <para>
     1040                                Twitter replaces every URL with fixed-length t.co URLs. BitlBee is able to take t.co urls into account when calculating <emphasis>message_length</emphasis> replacing the actual URL length with target_url_length. Setting target_url_length to 0 disables this feature.
     1041                        </para>
     1042
     1043                        <para>
     1044                                This setting is disabled for identica accounts by default and will not affect anything other than message safety checks (i.e. Twitter will still replace your URLs with t.co links, even if that makes them longer).
     1045                        </para>
     1046                </description>
     1047
     1048        </bitlbee-setting>
    10341049
    10351050        <bitlbee-setting name="mode" type="string" scope="account">
  • protocols/twitter/twitter.c

    rd2abcae r7d27962  
    197197}
    198198
     199int twitter_url_len_diff(gchar *msg, unsigned int target_len)
     200{
     201        int url_len_diff = 0;
     202
     203        static GRegex *regex = NULL;
     204        GMatchInfo *match_info;
     205
     206        if (regex == NULL)
     207                regex = g_regex_new("(^|\\s)(http(s)?://[^\\s$]+)", 0, 0, NULL);
     208       
     209        g_regex_match(regex, msg, 0, &match_info);
     210        while (g_match_info_matches(match_info)) {
     211                gchar *url = g_match_info_fetch(match_info, 2);
     212                url_len_diff += target_len - g_utf8_strlen(url, -1);
     213                if (g_match_info_fetch(match_info, 3) != NULL)
     214                        url_len_diff += 1;
     215                g_free(url);
     216                g_match_info_next(match_info, NULL);
     217        }
     218        g_match_info_free(match_info);
     219
     220        return url_len_diff;
     221}
     222
    199223static gboolean twitter_length_check(struct im_connection *ic, gchar * msg)
    200224{
    201225        int max = set_getint(&ic->acc->set, "message_length"), len;
    202 
    203         if (max == 0 || (len = g_utf8_strlen(msg, -1)) <= max)
     226        int target_len = set_getint(&ic->acc->set, "target_url_length");
     227        int url_len_diff = 0;
     228   
     229        if (target_len > 0)
     230                url_len_diff = twitter_url_len_diff(msg, target_len);
     231
     232        if (max == 0 || (len = g_utf8_strlen(msg, -1) + url_len_diff) <= max)
    204233                return TRUE;
    205234
     
    214243        char *def_url;
    215244        char *def_oauth;
     245        char *def_tul;
    216246
    217247        if (strcmp(acc->prpl->name, "twitter") == 0) {
    218248                def_url = TWITTER_API_URL;
    219249                def_oauth = "true";
     250                def_tul = "20";
    220251        } else {                /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */
    221252                def_url = IDENTICA_API_URL;
    222253                def_oauth = "true";
     254                def_tul = "0";
    223255        }
    224256
     
    236268
    237269        s = set_add(&acc->set, "message_length", "140", set_eval_int, acc);
     270
     271        s = set_add(&acc->set, "target_url_length", def_tul, set_eval_int, acc);
    238272
    239273        s = set_add(&acc->set, "mode", "chat", set_eval_mode, acc);
Note: See TracChangeset for help on using the changeset viewer.