diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2012-02-08 00:24:12 +0000 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2012-02-08 00:24:12 +0000 |
commit | 7d27962b17487ec1c783dc9e028859b01a20e6f0 (patch) | |
tree | 616fd315d52857228aeac822474ed10da8fa1bba /protocols/twitter/twitter.c | |
parent | d2abcae711eb4aca5b67e405607f40156ba0a29a (diff) |
Take t.co URL lengthe^Wshortening 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!
Diffstat (limited to 'protocols/twitter/twitter.c')
-rw-r--r-- | protocols/twitter/twitter.c | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/protocols/twitter/twitter.c b/protocols/twitter/twitter.c index 91b6e28c..c389677f 100644 --- a/protocols/twitter/twitter.c +++ b/protocols/twitter/twitter.c @@ -196,11 +196,40 @@ static char *set_eval_mode(set_t * set, char *value) return NULL; } +int twitter_url_len_diff(gchar *msg, unsigned int target_len) +{ + int url_len_diff = 0; + + static GRegex *regex = NULL; + GMatchInfo *match_info; + + if (regex == NULL) + regex = g_regex_new("(^|\\s)(http(s)?://[^\\s$]+)", 0, 0, NULL); + + g_regex_match(regex, msg, 0, &match_info); + while (g_match_info_matches(match_info)) { + gchar *url = g_match_info_fetch(match_info, 2); + url_len_diff += target_len - g_utf8_strlen(url, -1); + if (g_match_info_fetch(match_info, 3) != NULL) + url_len_diff += 1; + g_free(url); + g_match_info_next(match_info, NULL); + } + g_match_info_free(match_info); + + return url_len_diff; +} + static gboolean twitter_length_check(struct im_connection *ic, gchar * msg) { int max = set_getint(&ic->acc->set, "message_length"), len; + int target_len = set_getint(&ic->acc->set, "target_url_length"); + int url_len_diff = 0; + + if (target_len > 0) + url_len_diff = twitter_url_len_diff(msg, target_len); - if (max == 0 || (len = g_utf8_strlen(msg, -1)) <= max) + if (max == 0 || (len = g_utf8_strlen(msg, -1) + url_len_diff) <= max) return TRUE; imcb_error(ic, "Maximum message length exceeded: %d > %d", len, max); @@ -213,13 +242,16 @@ static void twitter_init(account_t * acc) set_t *s; char *def_url; char *def_oauth; + char *def_tul; if (strcmp(acc->prpl->name, "twitter") == 0) { def_url = TWITTER_API_URL; def_oauth = "true"; + def_tul = "20"; } else { /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */ def_url = IDENTICA_API_URL; def_oauth = "true"; + def_tul = "0"; } s = set_add(&acc->set, "auto_reply_timeout", "10800", set_eval_int, acc); @@ -236,6 +268,8 @@ static void twitter_init(account_t * acc) s = set_add(&acc->set, "message_length", "140", set_eval_int, acc); + s = set_add(&acc->set, "target_url_length", def_tul, set_eval_int, acc); + s = set_add(&acc->set, "mode", "chat", set_eval_mode, acc); s->flags |= ACC_SET_OFFLINE_ONLY; |