File: util.c

package info (click to toggle)
pidgin-twitter 0.9.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 660 kB
  • ctags: 452
  • sloc: ansic: 6,383; makefile: 86; sh: 45
file content (647 lines) | stat: -rw-r--r-- 17,563 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#include "pidgin-twitter.h"
#include <gtkutils.h>
#include <pidginstock.h>
#include <gtkblist.h>

extern GRegex *regp[];
extern guint64 reply_to_msgid;
extern PurpleAccount *account_for_twitter;

/* prototypes */
static gchar *twitter_memrchr(const gchar *s, int c, size_t n);


/* functions */

/* this function has been taken from autoaccept plugin */
gboolean
ensure_path_exists(const char *dir)
{
	if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) {
		if (purple_build_dir(dir, S_IRUSR | S_IWUSR | S_IXUSR))
			return FALSE;
	}

	return TRUE;
}

static gchar *
twitter_memrchr(const gchar *s, int c, size_t n)
{
    int nn = n;

    g_return_val_if_fail(s != NULL, NULL);

    while(nn+1) {
        if((int)*(s+nn) == c)
            return (gchar *)(s+nn);
        nn--;
    }
    return NULL;
}

static gchar *html_tags[] = {
    "<a ",
    "</a>",
    "<b>",
    "</b>",
    "<p>",
    "</p>",
    "<div ",
    "</div>",
    "<span ",
    "</span>",
    "<body>",
    "<body ",
    "</body>",
    "<i>",
    "</i>",
    "<font ",
    "</font>",
    "<br>",
    "<br/>",
    "<img ",
    "<html>",
    "<html ",
    "</html>",
    NULL
};

gchar *
strip_html_markup(const gchar *src)
{
    gchar *head, *tail;     /* head and tail of html */
    gchar *begin, *end;     /* begin:<  end:> */
    gchar *html, *str;      /* copied src and string to be returned */
/*    gchar *vis1, *vis2; */     /* begin and end of address part */
    gchar *startp;          /* starting point marker */
    gchar **tagp;           /* tag iterator */
    gchar *tmp, *tmp2;      /* scratches */

    g_return_val_if_fail(src != NULL, NULL);

    const gchar *ptr, *ent;
    gchar *ptr2;
    gint srclen;
    gint entlen;

    /* unescape &x; */
    srclen = strlen(src);
    html = g_malloc0(srclen + 1);
    ptr2 = html;
    for(ptr = src; *ptr; ) {
        if(*ptr == '&') {
            ent = purple_markup_unescape_entity(ptr, &entlen);
            if(ent != NULL) {
                while(*ent) {
                    if(ptr2 - html < srclen)
                        *ptr2++ = *ent++;
                    else
                        ent++;
                }
                ptr += entlen;
            }
            else {
                if(ptr2 - html < srclen)
                    *ptr2++ = *ptr++;
                else
                    ptr++;
            }
        }
        else {
            if(ptr2 - html < srclen)
                *ptr2++ = *ptr++;
            else
                ptr++;
        }
    } /* for */

    str = g_strdup("\0");

    head = html;
    tail = head + strlen(html);
    startp = head;

loop:
    begin = NULL;
    end = NULL;

    if(startp >= tail) {
        g_free(html);
        return str;
    }

    end = strchr(startp, '>');
    if(end) {
        begin = twitter_memrchr(startp, '<', end - startp);
        if(begin < startp)
            begin = NULL;

        if(!begin) { /* '>' found but no corresponding '<' */
            tmp = g_strndup(startp, end - startp + 1); /* concat until '>' */
            tmp2 = g_strconcat(str, tmp, NULL);
            g_free(str);
            g_free(tmp);
            str = tmp2;
            startp = end + 1;
            goto loop;
        }
    }
    else { /* neither '>' nor '<' were found */
        tmp = g_strconcat(str, startp, NULL); /* concat the rest */
        g_free(str);
        str = tmp;
        g_free(html);
        return str;
    }

    /* here, both < and > are found */
    /* concatenate leading part to dest */
    tmp = g_strndup(startp, begin - startp);
    tmp2 = g_strconcat(str, tmp, NULL);
    g_free(tmp);
    g_free(str);
    str = tmp2;

    /* find tag */
    for(tagp = html_tags; *tagp; tagp++) {
        if(!g_ascii_strncasecmp(begin, *tagp, strlen(*tagp))) {
            /* we found a valid tag */
            /* if tag is <a href=, extract address. */
#if 0
            if(!strcmp(*tagp, "<a href=")) {
                vis1 = NULL; vis2 = NULL;

                vis1 = strchr(begin, '\'');
                if(vis1)
                    vis2 = strchr(vis1+1, '\'');
                if(!vis1) {
                    vis1 = strchr(begin, '\"');
                    if(vis1)
                        vis2 = strchr(vis1+1, '\"');
                }
                if(vis1 && vis2) {
                    *vis2 = '\0';
                    /* generate "[ http://example.com/ ] anchor " */
                    tmp = g_strconcat(str, "[ ", vis1+1, " ]", " ", NULL);
                    g_free(str);
                    str = tmp;
                }
                startp = end + 1;
                goto loop;
            } /* <a href= */
            else {
                /* anything else: discard whole <>. */
                startp = end + 1;
                goto loop;
            }
#else
            /* anything else: discard whole <>. */
            startp = end + 1;
            goto loop;
#endif
        }  /* valid tag */
    }

    /* no valid tag was found: copy <brabra> */
    tmp = g_strndup(begin, end - begin + 1);
    tmp2 = g_strconcat(str, tmp, NULL);
    g_free(tmp);
    g_free(str);
    str = tmp2;
    startp = end + 1;
    goto loop;
}

/* string utilities */
void
escape(gchar **str)
{
    GMatchInfo *match_info = NULL;
    gchar *newstr = NULL, *match = NULL;
    gboolean flag = FALSE;

    /* search genuine command */
    g_regex_match(regp[COMMAND], *str, 0, &match_info);
    while(g_match_info_matches(match_info)) {
        match = g_match_info_fetch(match_info, 1);
        twitter_debug("command = %s\n", match);
        g_free(match);
        g_match_info_next(match_info, NULL);
        flag = TRUE;
    }
    g_match_info_free(match_info);
    match_info = NULL;

    if(flag)
        return;

    /* if not found, check pseudo command */
    g_regex_match(regp[PSEUDO], *str, 0, &match_info);
    while(g_match_info_matches(match_info)) {
        match = g_match_info_fetch(match_info, 1);
        twitter_debug("pseudo = %s\n", match);
        g_free(match);
        g_match_info_next(match_info, NULL);
        flag = TRUE;
    }
    g_match_info_free(match_info);
    match_info = NULL;

    /* if there is pseudo one, escape it */
    if(flag) {
        /* put ". " to the beginning of buffer */
        newstr = g_strdup_printf(". %s", *str);
        twitter_debug("*str = %s newstr = %s\n", *str, newstr);
        g_free(*str);
        *str = newstr;
    }
}

void
strip_markup(gchar **str, gboolean escape)
{
    gchar *plain;

    plain = strip_html_markup(*str);
    g_free(*str);
    if(escape) {
        *str = g_markup_escape_text(plain, -1);
        g_free(plain);
    }
    else {
        *str = plain;
    }
    twitter_debug("result=%s\n", *str);
}

gboolean
is_twitter_account(PurpleAccount *account, const char *name)
{
    const gchar *proto = purple_account_get_protocol_id(account);

    if(g_strstr_len(name,  19, "twitter@twitter.com") &&
       g_strstr_len(proto, 11, "prpl-jabber")) {
        return TRUE;
    }

    return FALSE;
}

gboolean
is_twitter_conv(PurpleConversation *conv)
{
    g_return_val_if_fail(conv != NULL, FALSE);

    const char *name = purple_conversation_get_name(conv);
    PurpleAccount *account = purple_conversation_get_account(conv);

    return is_twitter_account(account, name);
}

gboolean
is_wassr_account(PurpleAccount *account, const char *name)
{
    const gchar *proto = purple_account_get_protocol_id(account);

    if(g_strstr_len(name,  18, "wassr-bot@wassr.jp") &&
       g_strstr_len(proto, 11, "prpl-jabber")) {
        return TRUE;
    }

    return FALSE;
}

gboolean
is_wassr_conv(PurpleConversation *conv)
{
    g_return_val_if_fail(conv != NULL, FALSE);

    const char *name = purple_conversation_get_name(conv);
    PurpleAccount *account = purple_conversation_get_account(conv);

    return is_wassr_account(account, name);
}

gboolean
is_identica_account(PurpleAccount *account, const char *name)
{
    const gchar *proto = purple_account_get_protocol_id(account);

    if(g_strstr_len(name,  16, "update@identi.ca") &&
       g_strstr_len(proto, 11, "prpl-jabber")) {
        return TRUE;
    }

    return FALSE;
}

gboolean
is_identica_conv(PurpleConversation *conv)
{
    g_return_val_if_fail(conv != NULL, FALSE);

    const char *name = purple_conversation_get_name(conv);
    PurpleAccount *account = purple_conversation_get_account(conv);

    return is_identica_account(account, name);
}

gboolean
is_jisko_account(PurpleAccount *account, const char *name)
{
    const gchar *proto = purple_account_get_protocol_id(account);

    if(g_strstr_len(name,  16, "bot@jisko.net") &&
       g_strstr_len(proto, 11, "prpl-jabber")) {
        return TRUE;
    }

    return FALSE;
}

gboolean
is_jisko_conv(PurpleConversation *conv)
{
    g_return_val_if_fail(conv != NULL, FALSE);

    const char *name = purple_conversation_get_name(conv);
    PurpleAccount *account = purple_conversation_get_account(conv);

    return is_jisko_account(account, name);
}

gboolean
is_ffeed_account(PurpleAccount *account, const char *name)
{
    const gchar *proto = purple_account_get_protocol_id(account);

    if(g_strstr_len(name,  22, "ff@chat.friendfeed.com") &&
       g_strstr_len(proto, 11, "prpl-jabber")) {
        return TRUE;
    }

    return FALSE;
}

gboolean
is_ffeed_conv(PurpleConversation *conv)
{
    g_return_val_if_fail(conv != NULL, FALSE);

    const char *name = purple_conversation_get_name(conv);
    PurpleAccount *account = purple_conversation_get_account(conv);

    return is_ffeed_account(account, name);
}

gint
get_service_type_by_account(PurpleAccount *account, const char *sender)
{
    gint service = unknown_service;

    g_return_val_if_fail(account != NULL, unknown_service);
    g_return_val_if_fail(sender != NULL, unknown_service);

    if(is_twitter_account(account, sender))
        service = twitter_service;
    else if(is_wassr_account(account, sender))
        service = wassr_service;
    else if(is_identica_account(account, sender))
        service = identica_service;
    else if(is_jisko_account(account, sender))
        service = jisko_service;
    else if(is_ffeed_account(account, sender))
        service = ffeed_service;

    return service;
}

gint
get_service_type(PurpleConversation *conv)
{
    gint service = unknown_service;

    g_return_val_if_fail(conv != NULL, unknown_service);

    if(is_twitter_conv(conv))
        service = twitter_service;
    else if(is_wassr_conv(conv))
        service = wassr_service;
    else if(is_identica_conv(conv))
        service = identica_service;
    else if(is_jisko_conv(conv))
        service = jisko_service;
    else if(is_ffeed_conv(conv))
        service = ffeed_service;

    return service;
}

void cancel_retweet(void *data)
{
    /* do nothing */
}

void do_retweet(void *data)
{
    guint64 msgid = *(guint64 *)data;
    twitter_debug("msgid=%llu\n", (long long unsigned int)msgid);
    retweet_with_api(msgid);
}

gboolean
pt_uri_handler(const char *proto, const char *cmd, GHashTable *params)
{
    char *idstr = NULL;
	const char *acct_id = NULL;
	PurpleConversation *conv = NULL;
	PidginConversation *gtkconv = NULL;
    guint64 msgid = 0;
    gchar *sender = NULL, *recipient = NULL, *msg = NULL;

    if(g_ascii_strcasecmp(proto, "pt"))
       return FALSE;

    twitter_debug("params=%p\n", params);

    acct_id = purple_prefs_get_string(OPT_SCREEN_NAME_TWITTER);
    twitter_debug("acct_id=%s\n", acct_id);

    if(strstr(cmd, "reply-twitter")) {
        sender = g_hash_table_lookup(params, "user");
        idstr = g_hash_table_lookup(params, "id");
        if(idstr)
            msgid = strtoull(idstr, NULL, 10);

        /* find conv */
        conv = purple_find_conversation_with_account(
            PURPLE_CONV_TYPE_ANY, "twitter@twitter.com",
            account_for_twitter); /* xxx */
        twitter_debug("conv=%p\n", conv);
        gtkconv = PIDGIN_CONVERSATION(conv);

        twitter_debug("sender=%s, id=%llu\n", sender, (unsigned long long)msgid);

        recipient = g_strdup_printf("@%s ", sender);
        gtk_text_buffer_insert_at_cursor(gtkconv->entry_buffer,
                                         recipient, -1);

        gtk_widget_grab_focus(GTK_WIDGET(gtkconv->entry));
        g_free(recipient);
        reply_to_msgid = msgid;

        return TRUE;
    }
    else if(strstr(cmd, "fav-twitter")) {
        idstr = g_hash_table_lookup(params, "id");
        fav_with_api(strtoull(idstr, NULL, 10));
        return TRUE;
    }
    else if(strstr(cmd, "retweet-twitter")) {
        GtkWidget *retweet_dialog;
        static guint64 retweet_msgid = 0;

        idstr = g_hash_table_lookup(params, "id");
        retweet_msgid = strtoull(idstr, NULL, 10);
        twitter_debug("retweet_msgid=%llu\n", (long long unsigned int)retweet_msgid);

        retweet_dialog = pidgin_make_mini_dialog(
            NULL,
            PIDGIN_STOCK_DIALOG_INFO,
            "Are you sure to retweet this message?",
            NULL, /* secondary */
            (void *)&retweet_msgid, /* user data */
            "Cancel", PURPLE_CALLBACK(cancel_retweet),
            "Retweet", PURPLE_CALLBACK(do_retweet),
            NULL);
        pidgin_blist_add_alert(retweet_dialog);

        return TRUE;
    }
    else if(strstr(cmd, "quotetweet-twitter")) {
        gchar *msg0;
        sender = g_hash_table_lookup(params, "user");
        idstr = g_hash_table_lookup(params, "id");
        msg0 = g_hash_table_lookup(params, "msg");
        msg = g_uri_unescape_string(msg0, NULL);

        if(idstr)
            msgid = strtoull(idstr, NULL, 10);

        /* find conv */
        conv = purple_find_conversation_with_account(
            PURPLE_CONV_TYPE_ANY, "twitter@twitter.com",
            account_for_twitter); /* xxx */
        twitter_debug("conv=%p\n", conv);
        gtkconv = PIDGIN_CONVERSATION(conv);

        twitter_debug("sender=%s, id=%llu\n", sender, (unsigned long long)msgid);

        recipient = g_strdup_printf("QT @%s: %s", sender, msg);
        g_free(msg);
        gtk_text_buffer_insert_at_cursor(gtkconv->entry_buffer,
                                         recipient, -1);

        GtkTextIter iter;
        gtk_text_buffer_get_start_iter(gtkconv->entry_buffer, &iter);
        gtk_text_buffer_place_cursor(gtkconv->entry_buffer, &iter);

        gtk_widget_grab_focus(GTK_WIDGET(gtkconv->entry));
        g_free(recipient);
        reply_to_msgid = msgid;

        return TRUE;
    }
    return FALSE;
}

gchar *
twitter_rip_link_string(gchar **str)
{
    GMatchInfo *match_info = NULL;
    gchar *body0 = NULL, *body = NULL;
    gchar *newstr = NULL, *linkstr = NULL;
    gchar *user = NULL;

    twitter_debug("called\n");

    /* buffer without pttag= */
    body0 = g_regex_replace(regp[SENDER], *str, -1, 0, "", 0, NULL);
    body = g_regex_replace(regp[PTTAG_TWITTER], body0, -1, 0, "", 0, NULL);
    g_free(body0);
    body0 = NULL;
    twitter_debug("body = %s\n", body);

    body0 = g_uri_escape_string(body, " !$()*,;:@/?#[]", TRUE);
    g_free(body);
    body = body0;

    /* sender */
    g_regex_match(regp[SENDER], *str, 0, &match_info);
    if(g_match_info_matches(match_info)) {
        user = g_match_info_fetch(match_info, 2);
        twitter_debug("user = %s\n", user);
        g_match_info_free(match_info);
        match_info = NULL;
    }

    /* link string */
    g_regex_match(regp[PTTAG_TWITTER], *str, 0, &match_info);
    if(match_info) {
        gchar *msg_id_str = NULL;
        gchar *in_reply_to_status_id_str = NULL;
        long long unsigned int in_reply_to_status_id = 0;

        msg_id_str = g_match_info_fetch(match_info, 1);
        in_reply_to_status_id_str = g_match_info_fetch(match_info, 2);
        if(in_reply_to_status_id_str)
            in_reply_to_status_id = strtoull(in_reply_to_status_id_str, NULL, 10);
        g_free(in_reply_to_status_id_str);
        in_reply_to_status_id_str = NULL;

        if(in_reply_to_status_id) {
            gchar *in_reply_to_screen_name;

            in_reply_to_screen_name = g_match_info_fetch(match_info, 3);
            linkstr = g_strdup_printf(IN_REPLY_TO_FORMAT_TWITTER LINK_FORMAT_TWITTER,
                                      in_reply_to_screen_name,
                                      in_reply_to_status_id,
                                      in_reply_to_screen_name,
                                      msg_id_str, user,         /* Reply */
                                      msg_id_str,               /* Favorite */
                                      msg_id_str,               /* Retweet */
                                      msg_id_str, user, body); /* Quotetweet */

            g_free(in_reply_to_screen_name);
            in_reply_to_screen_name = NULL;
        }
        else {
            linkstr = g_strdup_printf(LINK_FORMAT_TWITTER,
                                      msg_id_str, user,         /* Reply */
                                      msg_id_str,               /* Favorite */
                                      msg_id_str,               /* Retweet */
                                      msg_id_str, user, body); /* Quotetweet */
        }

        twitter_debug("linkstr = %s\n", linkstr);

        newstr = g_regex_replace(regp[PTTAG_TWITTER], *str, -1, 0, "", 0, NULL);

        twitter_debug("newstr = %s\n", newstr);

        g_free(*str);
        *str = newstr;

        g_free(msg_id_str);
        msg_id_str = NULL;

        g_match_info_free(match_info);
        match_info = NULL;
    }

    g_free(user);
    g_free(body);

    return linkstr;
}