-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpand.c
746 lines (604 loc) · 25.8 KB
/
expand.c
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include "conversation.h"
#include "core.h"
#include "debug.h"
#include "util.h"
#include "version.h"
#include "gtkplugin.h"
#include "gtkconv.h"
#include "gtkimhtml.h"
#include <ctype.h>
#include "expand.h"
#ifdef WIN32
#include "win32dep.h"
#endif
#ifdef ENABLE_NLS
#include <glib/gi18n-lib.h>
#else
#define _(String) ((/* const */ char *) (String))
#define N_(String) ((/* const */ char *) (String))
#endif // ENABLE NLS
#include "local_util.h"
struct AccountConv {
PurpleAccount *account;
PurpleConversation *conv;
char *name;
};
typedef void (*SHORTENER_CB) (const char *, gpointer);
struct Shortener {
char *name;
SHORTENER_CB cb;
char *pref;
};
static PurplePlugin *expand_plugin = NULL;
struct ExpandData {
gchar *original_url;
gpointer userdata;
};
static void expand_pic_cb(PurpleUtilFetchUrlData * url_data, gpointer userdata, const gchar * url_text, gsize len, const gchar * error_message);
static void expand_twitlonger_cb(PurpleUtilFetchUrlData * url_data, gpointer userdata, const gchar * url_text, gsize len, const gchar * error_message);
static void expand_shortlink_cb(PurpleUtilFetchUrlData * url_data, gpointer userdata, const gchar * url_text, gsize len, const gchar * error_message);
static void replace(PurpleAccount * account, PurpleConversation * conv, const gchar * original, const gchar * new, gboolean link);
static void expand_picplz(const char *url, gpointer userdata);
static void expand_lightbox(const char *url, gpointer userdata);
static void expand_twitpic(const char *url, gpointer userdata);
static void expand_lockerz(const char *url, gpointer userdata);
static void expand_yfrog(const char *url, gpointer userdata);
static void expand_twitgoo(const char *url, gpointer userdata);
static void expand_instagram(const char *url, gpointer userdata);
static void expand_twitlonger(const char *url, gpointer userdata);
static void expand_shortlink(const char *url, gpointer userdata);
static GList *get_links(const char *text);
static gboolean displaying_msg(PurpleAccount * account, const char *message, PurpleConversation * conv);
static gboolean displaying_im_msg_cb(PurpleAccount * account, const char *who, char **message, PurpleConversation * conv, PurpleMessageFlags flags);
static gboolean displaying_chat_msg_cb(PurpleAccount * account, const char *who, char **message, PurpleConversation * conv, PurpleMessageFlags flags);
static gboolean plugin_load(PurplePlugin * plugin);
static gboolean plugin_unload(PurplePlugin * plugin);
static PurplePluginPrefFrame *get_plugin_pref_frame(PurplePlugin * plugin);
static void plugin_destroy(PurplePlugin * plugin);
static void plugin_init(PurplePlugin * plugin);
static gchar *xmlnode_get_child_data(const xmlnode * node, const char *name);
struct Shortener shorteners[] = {
{"tinyurl.com", expand_shortlink, EXPAND_PREF_EXPAND_ALL_LINKS},
{"bit.ly", expand_shortlink, EXPAND_PREF_EXPAND_ALL_LINKS},
{"t.co", expand_shortlink, EXPAND_PREF_EXPAND_ALL_LINKS},
{"is.gd", expand_shortlink, EXPAND_PREF_EXPAND_ALL_LINKS},
{"j.mp", expand_shortlink, EXPAND_PREF_EXPAND_ALL_LINKS},
{"goo.gl", expand_shortlink, EXPAND_PREF_EXPAND_ALL_LINKS},
{"ow.ly", expand_shortlink, EXPAND_PREF_EXPAND_ALL_LINKS},
{"tl.gd", expand_shortlink, EXPAND_PREF_EXPAND_ALL_LINKS},
{"www.picplz.com", expand_picplz, EXPAND_PREF_EXPAND_PICS},
{"picplz.com", expand_picplz, EXPAND_PREF_EXPAND_PICS},
{"www.lightbox.com", expand_lightbox, EXPAND_PREF_EXPAND_PICS},
{"lightbox.com", expand_lightbox, EXPAND_PREF_EXPAND_PICS},
{"twitpic.com", expand_twitpic, EXPAND_PREF_EXPAND_PICS},
{"www.twitpic.com", expand_twitpic, EXPAND_PREF_EXPAND_PICS},
{"lockerz.com", expand_lockerz, EXPAND_PREF_EXPAND_PICS},
{"www.lockerz.com", expand_lockerz, EXPAND_PREF_EXPAND_PICS},
{"yfrog.com", expand_yfrog, EXPAND_PREF_EXPAND_PICS},
{"www.twitgoo.com", expand_twitgoo, EXPAND_PREF_EXPAND_PICS},
{"twitgoo.com", expand_twitgoo, EXPAND_PREF_EXPAND_PICS},
{"www.yfrog.com", expand_yfrog, EXPAND_PREF_EXPAND_PICS},
{"instagr.am", expand_instagram, EXPAND_PREF_EXPAND_PICS},
{"twitlonger.com", expand_twitlonger, EXPAND_PREF_EXPAND_TL},
{"www.twitlonger.com", expand_twitlonger, EXPAND_PREF_EXPAND_TL}
};
static gchar *xmlnode_get_child_data(const xmlnode * node, const char *name)
{
xmlnode *child = xmlnode_get_child(node, name);
if (!child)
return NULL;
return xmlnode_get_data_unescaped(child);
}
static void expand_pic_cb(PurpleUtilFetchUrlData * url_data, gpointer userdata, const gchar * url_text, gsize len, const gchar * error_message)
{
GdkPixbufLoader *loader = NULL;
gboolean status = FALSE;
GError *err = NULL;
struct ExpandData *store = userdata;
struct AccountConv *convmsg = NULL;
GdkPixbuf *pixbuf = NULL;
GdkPixbuf *scaled = NULL;
if (store) {
convmsg = store->userdata;
}
if (!(convmsg && convmsg->name)) {
purple_debug_warning(PLUGIN_ID, "Invalid convmsg somehow\n");
}
convmsg->conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY, convmsg->name, convmsg->account);
if (!(convmsg->conv && PIDGIN_CONVERSATION(convmsg->conv) && PIDGIN_CONVERSATION(convmsg->conv)->imhtml)) {
purple_debug_warning(PLUGIN_ID, "COnversation seems to have disappeared. Oh well.\n");
len = 0;
}
if (url_text && len) {
loader = gdk_pixbuf_loader_new();
status = gdk_pixbuf_loader_write(loader, (const guchar *) url_text, len, &err);
}
if (status) {
status = gdk_pixbuf_loader_close(loader, &err);
}
if (status) {
pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
}
if (pixbuf) {
int height = purple_prefs_get_int(EXPAND_PREF_EXPAND_PICS_SIZE);
int width = height * (gdouble) gdk_pixbuf_get_width(pixbuf) / (gdouble) gdk_pixbuf_get_height(pixbuf);
scaled = gdk_pixbuf_scale_simple(pixbuf, width, height, GDK_INTERP_BILINEAR);
g_object_ref(G_OBJECT(scaled));
}
if (scaled) {
GtkIMHtml *imhtml = NULL;
GtkTextBuffer *text_buffer = NULL;
GtkTextIter buf_end;
GtkTextIter text_start;
GtkTextIter text_end;
imhtml = GTK_IMHTML(PIDGIN_CONVERSATION(convmsg->conv)->imhtml);
text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(imhtml));
/* Work backwards to be faster on large buffers */
gtk_text_buffer_get_end_iter(text_buffer, &buf_end);
if (!gtk_text_iter_backward_search(&buf_end, store->original_url, 0, &text_start, &text_end, NULL)) {
purple_debug_warning(PLUGIN_ID, "Couldn't find the text (%s) in the buffer!\n", store->original_url);
} else {
// gtk_text_buffer_delete(text_buffer, &text_start, &text_end);
if (gtk_text_iter_get_pixbuf(&text_end)) {
purple_debug_warning(PLUGIN_ID, "There's already a pixbuf here!\n");
} else {
gtk_text_buffer_insert_pixbuf(text_buffer, &text_end, scaled);
}
}
} else {
purple_debug_error(PLUGIN_ID, "Couldn't expand %s\n", store->original_url);
}
g_object_unref(G_OBJECT(loader));
g_free(convmsg->name);
g_free(convmsg);
g_free(store->original_url);
g_free(store);
}
static void expand_twitlonger_cb(PurpleUtilFetchUrlData * url_data, gpointer userdata, const gchar * url_text, gsize len, const gchar * error_message)
{
struct ExpandData *store = userdata;
struct AccountConv *convmsg = NULL;
xmlnode *response_node = NULL;
xmlnode *post = NULL;
gchar *fulltext;
if (store) {
convmsg = store->userdata;
}
if (!(convmsg && convmsg->name)) {
purple_debug_warning(PLUGIN_ID, "Invalid convmsg somehow\n");
}
convmsg->conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY, convmsg->name, convmsg->account);
if (!(convmsg->conv && PIDGIN_CONVERSATION(convmsg->conv) && PIDGIN_CONVERSATION(convmsg->conv)->imhtml)) {
purple_debug_warning(PLUGIN_ID, "COnversation seems to have disappeared. Oh well.\n");
len = 0;
}
if (url_text && len) {
response_node = xmlnode_from_str(url_text, strlen(url_text));
} else {
purple_debug_error(PLUGIN_ID, "Couldn't retreive! Error: %s\n", error_message);
}
if (response_node) {
post = xmlnode_get_child(response_node, "post");
}
if (post) {
gchar *content = xmlnode_get_child_data(post, "content");
fulltext = g_strdup_printf("... (%s)", content);
g_free(content);
replace(convmsg->account, convmsg->conv, store->original_url, fulltext, FALSE);
} else {
purple_debug_error(PLUGIN_ID, "Couldn't expand %s\n", store->original_url);
return;
}
if (response_node)
xmlnode_free(response_node);
if (post)
xmlnode_free(post);
g_free(convmsg->name);
g_free(convmsg);
g_free(fulltext);
g_free(store->original_url);
g_free(store);
}
static void expand_shortlink_cb(PurpleUtilFetchUrlData * url_data, gpointer userdata, const gchar * url_text, gsize len, const gchar * error_message)
{
struct ExpandData *store = userdata;
gchar *location = NULL;
gchar *new_location = NULL;
struct AccountConv *convmsg = NULL;
if (store) {
convmsg = store->userdata;
}
if (!(convmsg && convmsg->name)) {
purple_debug_warning(PLUGIN_ID, "Invalid convmsg somehow\n");
}
convmsg->conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY, convmsg->name, convmsg->account);
if (!(convmsg->conv && PIDGIN_CONVERSATION(convmsg->conv) && PIDGIN_CONVERSATION(convmsg->conv)->imhtml)) {
purple_debug_warning(PLUGIN_ID, "COnversation seems to have disappeared. Oh well.\n");
len = 0;
}
if (url_text && len) {
gchar *url_text_lower = g_ascii_strdown(url_text, -1);
location = g_strstr_len(url_text_lower, len, "\nlocation: ");
if (location) {
location = url_text + (location - url_text_lower);
}
g_free(url_text_lower);
} else {
purple_debug_error(PLUGIN_ID, "Couldn't retreive! Error: %s\n", error_message);
}
if (location) {
location += 11;
new_location = g_strndup(location, strchr(location, '\r') - location);
}
if (new_location) {
/* Check for nesting */
displaying_msg(convmsg->account, new_location, convmsg->conv);
purple_debug_info(PLUGIN_ID, "Expanded |%s| into |%s|\n", store->original_url, new_location);
replace(convmsg->account, convmsg->conv, store->original_url, new_location, TRUE);
} else {
purple_debug_error(PLUGIN_ID, "Couldn't expand %s\n", store->original_url);
return;
}
g_free(convmsg->name);
g_free(convmsg);
g_free(new_location);
g_free(store->original_url);
g_free(store);
}
static void expand_lockerz(const char *url, gpointer userdata)
{
struct ExpandData *store;
gchar *request_url;
store = g_new0(struct ExpandData, 1);
request_url = g_strdup_printf("http://api.plixi.com/api/tpapi.svc/imagefromurl?url=%s&size=thumbnail", url);
store->original_url = g_strdup(url);
store->userdata = userdata;
purple_util_fetch_url_request(request_url, TRUE, "Mozilla/4.0 (compatible; MSIE 5.5)", FALSE, NULL, FALSE, expand_pic_cb, store);
g_free(request_url);
}
static void expand_picplz(const char *url, gpointer userdata)
{
struct ExpandData *store;
gchar *request_url;
purple_debug_info(PLUGIN_ID, "%s()\n", G_STRFUNC);
store = g_new0(struct ExpandData, 1);
request_url = g_strdup_printf("%s/thumb/200", url);
store->original_url = g_strdup(url);
store->userdata = userdata;
purple_debug_misc(PLUGIN_ID, "Getting |%s| using |%s|...\n", url, request_url);
purple_util_fetch_url_request(request_url, TRUE, "Mozilla/4.0 (compatible; MSIE 5.5)", FALSE, NULL, FALSE, expand_pic_cb, store);
g_free(request_url);
}
static void expand_lightbox(const char *url, gpointer userdata)
{
struct ExpandData *store;
gchar *request_url;
char *host;
int port;
char *path;
char *user;
char *passwd;
purple_debug_info(PLUGIN_ID, "%s()\n", G_STRFUNC);
if (!purple_url_parse(url, &host, &port, &path, &user, &passwd)) {
purple_debug_error(PLUGIN_ID, "Can't parse URL %s!\n", url);
}
store = g_new0(struct ExpandData, 1);
request_url = g_strdup_printf("http://lightbox.com/show/thumb/%s", path);
store->original_url = g_strdup(url);
store->userdata = userdata;
if (host)
g_free(host);
if (path)
g_free(path);
if (user)
g_free(user);
if (passwd)
g_free(passwd);
purple_debug_misc(PLUGIN_ID, "Getting |%s| using |%s|...\n", url, request_url);
purple_util_fetch_url_request(request_url, TRUE, "Mozilla/4.0 (compatible; MSIE 5.5)", FALSE, NULL, FALSE, expand_pic_cb, store);
g_free(request_url);
}
static void expand_twitpic(const char *url, gpointer userdata)
{
struct ExpandData *store;
gchar *request_url;
char *host;
int port;
char *path;
char *user;
char *passwd;
purple_debug_info(PLUGIN_ID, "%s()\n", G_STRFUNC);
if (!purple_url_parse(url, &host, &port, &path, &user, &passwd)) {
purple_debug_error(PLUGIN_ID, "Can't parse URL %s!\n", url);
}
store = g_new0(struct ExpandData, 1);
request_url = g_strdup_printf("http://twitpic.com/show/thumb/%s", path);
store->original_url = g_strdup(url);
store->userdata = userdata;
if (host)
g_free(host);
if (path)
g_free(path);
if (user)
g_free(user);
if (passwd)
g_free(passwd);
purple_debug_misc(PLUGIN_ID, "Getting |%s| using |%s|...\n", url, request_url);
purple_util_fetch_url_request(request_url, TRUE, "Mozilla/4.0 (compatible; MSIE 5.5)", FALSE, NULL, FALSE, expand_pic_cb, store);
g_free(request_url);
}
static void expand_yfrog(const char *url, gpointer userdata)
{
struct ExpandData *store;
gchar *request_url;
purple_debug_info(PLUGIN_ID, "%s()\n", G_STRFUNC);
store = g_new0(struct ExpandData, 1);
request_url = g_strdup_printf("%s:small", url);
store->original_url = g_strdup(url);
store->userdata = userdata;
purple_debug_misc(PLUGIN_ID, "Getting |%s| using |%s|...\n", url, request_url);
purple_util_fetch_url_request(request_url, TRUE, "Mozilla/4.0 (compatible; MSIE 5.5)", FALSE, NULL, FALSE, expand_pic_cb, store);
g_free(request_url);
}
static void expand_twitgoo(const char *url, gpointer userdata)
{
struct ExpandData *store;
gchar *request_url;
purple_debug_info(PLUGIN_ID, "%s()\n", G_STRFUNC);
store = g_new0(struct ExpandData, 1);
request_url = g_strdup_printf("%s/thumb", url);
store->original_url = g_strdup(url);
store->userdata = userdata;
purple_debug_misc(PLUGIN_ID, "Getting |%s| using |%s|...\n", url, request_url);
purple_util_fetch_url_request(request_url, TRUE, "Mozilla/4.0 (compatible; MSIE 5.5)", FALSE, NULL, FALSE, expand_pic_cb, store);
g_free(request_url);
}
static void expand_instagram(const char *url, gpointer userdata)
{
struct ExpandData *store;
gchar *request_url;
purple_debug_info(PLUGIN_ID, "%s()\n", G_STRFUNC);
store = g_new0(struct ExpandData, 1);
request_url = g_strdup_printf("%s/media/?size=t", url);
store->original_url = g_strdup(url);
store->userdata = userdata;
purple_debug_misc(PLUGIN_ID, "Getting |%s| using |%s|...\n", url, request_url);
purple_util_fetch_url_request(request_url, TRUE, "Mozilla/4.0 (compatible; MSIE 5.5)", FALSE, NULL, FALSE, expand_pic_cb, store);
g_free(request_url);
}
static void expand_twitlonger(const char *url, gpointer userdata)
{
struct ExpandData *store;
gchar *request_url;
purple_debug_info(PLUGIN_ID, "%s()\n", G_STRFUNC);
store = g_new0(struct ExpandData, 1);
request_url = g_strdup_printf("%s/fulltext", url);
store->original_url = g_strdup(url);
store->userdata = userdata;
purple_debug_misc(PLUGIN_ID, "Getting |%s| using |%s|...\n", url, request_url);
purple_util_fetch_url_request(request_url, TRUE, "Mozilla/4.0 (compatible; MSIE 5.5)", FALSE, NULL, FALSE, expand_twitlonger_cb, store);
g_free(request_url);
}
static void expand_shortlink(const char *url, gpointer userdata)
{
struct ExpandData *store;
gchar *request;
purple_debug_info(PLUGIN_ID, "%s()\n", G_STRFUNC);
store = g_new0(struct ExpandData, 1);
store->original_url = g_strdup(url);
store->userdata = userdata;
/* Host? */
request = g_strdup_printf("HEAD %s HTTP/1.0\r\n" "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\nContent-Length: 0\r\n\r\n", url);
purple_debug_misc(PLUGIN_ID, "Getting |%s| using |%s|...\n", url, request);
/* Custom version that doesn't follow redirects */
local_purple_util_fetch_url_request(url, TRUE, "Mozilla/4.0 (compatible; MSIE 5.5)", FALSE, request, TRUE, expand_shortlink_cb, store);
g_free(request);
}
static GList *get_links(const char *text)
{
GList *urls = NULL;
const char *p;
const char *url_start;
const char *url_end;
p = text;
while (NULL != (url_start = strstr(p, "http"))) {
gchar *url = NULL;
if (!memcmp(url_start + 4, "://", 3)) {
p = url_start + 7;
} else if (!memcmp(url_start + 4, "s://", 4)) {
p = url_start + 8;
} else {
p = url_start + 4;
continue;
}
/* TODO: replace this line with a better one. */
/* TODO: replace this whole function with a better one! */
while (*p && (*p == '.' || *p == '/' || *p == '-' || *p == '_' || (*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')))
p++;
url_end = p;
url = g_strndup(url_start, url_end - url_start);
/* Don't add duplicates */
if (!urls || strcmp(urls->data, url)) {
urls = g_list_prepend(urls, url);
}
/* URL will be freed later */
}
return urls;
}
static void replace(PurpleAccount * account, PurpleConversation * conv, const gchar * original, const gchar * new, gboolean link)
{
GtkIMHtml *imhtml = NULL;
GtkTextBuffer *text_buffer = NULL;
GtkTextIter buf_end;
GtkTextIter text_start;
GtkTextIter text_end;
imhtml = GTK_IMHTML(PIDGIN_CONVERSATION(conv)->imhtml);
text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(imhtml));
/* Work backwards to be faster on large buffers */
gtk_text_buffer_get_end_iter(text_buffer, &buf_end);
if (!gtk_text_iter_backward_search(&buf_end, original, 0, &text_start, &text_end, NULL)) {
purple_debug_warning(PLUGIN_ID, "Couldn't find the text (%s) in the buffer!\n", original);
return;
}
gtk_text_buffer_delete(text_buffer, &text_start, &text_end);
if (link) {
GtkTextMark *mark;
mark = gtk_text_buffer_create_mark(text_buffer, "new_url", &text_start, TRUE);
gtk_imhtml_insert_link(imhtml, mark, new, new);
gtk_text_buffer_delete_mark(text_buffer, mark);
} else {
gtk_text_buffer_insert(text_buffer, &text_start, new, -1);
}
return;
}
static gboolean displaying_msg(PurpleAccount * account, const char *message, PurpleConversation * conv)
{
gchar *url;
GList *urls;
GList *cur;
cur = urls = get_links(message);
if (!urls) {
return FALSE;
}
do {
char *host;
int port;
char *path;
char *user;
char *passwd;
url = cur->data;
if (purple_url_parse(url, &host, &port, &path, &user, &passwd)) {
int i;
int found = 0;
for (i = 0; i < sizeof (shorteners) / sizeof (shorteners[0]) && !found; i++) {
if (!strcmp(host, shorteners[i].name)) {
struct AccountConv *convmsg;
if (purple_prefs_get_bool(shorteners[i].pref)) {
convmsg = g_new0(struct AccountConv, 1);
convmsg->account = account;
convmsg->conv = NULL; /* TODO: remove this completely */
convmsg->name = g_strdup(purple_conversation_get_name(conv));
(*shorteners[i].cb) (url, convmsg);
}
found = 1;
}
}
if (host)
g_free(host);
if (path)
g_free(path);
if (user)
g_free(user);
if (passwd)
g_free(passwd);
} else {
purple_debug_error(PLUGIN_ID, "Couldn't parse URL! (%s)\n", url);
}
g_free(url);
} while (NULL != (cur = g_list_next(cur)));
g_list_free(urls);
return FALSE;
}
static gboolean displaying_im_msg_cb(PurpleAccount * account, const char *who, char **message, PurpleConversation * conv, PurpleMessageFlags flags)
{
if (flags & PURPLE_MESSAGE_RECV) {
return displaying_msg(account, *message, conv);
}
return FALSE;
}
static gboolean displaying_chat_msg_cb(PurpleAccount * account, const char *who, char **message, PurpleConversation * conv, PurpleMessageFlags flags)
{
if (flags & PURPLE_MESSAGE_RECV) {
return displaying_msg(account, *message, conv);
}
return FALSE;
}
static gboolean plugin_load(PurplePlugin * plugin)
{
expand_plugin = plugin;
purple_signal_connect(pidgin_conversations_get_handle(), "displaying-im-msg", plugin, PURPLE_CALLBACK(displaying_im_msg_cb), NULL);
purple_signal_connect(pidgin_conversations_get_handle(), "displaying-chat-msg", plugin, PURPLE_CALLBACK(displaying_chat_msg_cb), NULL);
return TRUE;
}
static gboolean plugin_unload(PurplePlugin * plugin)
{
purple_prefs_disconnect_by_handle(plugin);
purple_signals_disconnect_by_handle(plugin);
return TRUE;
}
static PurplePluginPrefFrame *get_plugin_pref_frame(PurplePlugin * plugin)
{
PurplePluginPrefFrame *frame;
PurplePluginPref *ppref;
frame = purple_plugin_pref_frame_new();
ppref = purple_plugin_pref_new_with_name_and_label(EXPAND_PREF_EXPAND_ALL_LINKS, _("Automatically expand shortened links"));
purple_plugin_pref_frame_add(frame, ppref);
ppref = purple_plugin_pref_new_with_name_and_label(EXPAND_PREF_EXPAND_TL, _("Automatically expand shortened texts (twitlonger)"));
purple_plugin_pref_frame_add(frame, ppref);
ppref = purple_plugin_pref_new_with_name_and_label(EXPAND_PREF_EXPAND_PICS, _("Automatically insert inline pictures (twitpic, etc)"));
purple_plugin_pref_frame_add(frame, ppref);
ppref = purple_plugin_pref_new_with_name_and_label(EXPAND_PREF_EXPAND_PICS_SIZE, _("Inline picture size"));
purple_plugin_pref_set_bounds(ppref, 32, 128);
purple_plugin_pref_frame_add(frame, ppref);
return frame;
}
static void plugin_destroy(PurplePlugin * plugin)
{
}
static PurplePluginUiInfo prefs_info = {
get_plugin_pref_frame,
0, /* page_num (Reserved) */
NULL, /* frame (Reserved) */
/* Padding */
NULL,
NULL,
NULL,
NULL
};
static PurplePluginInfo info = {
PURPLE_PLUGIN_MAGIC,
PURPLE_MAJOR_VERSION, /**< major version */
PURPLE_MINOR_VERSION, /**< minor version */
PURPLE_PLUGIN_STANDARD, /**< type */
PIDGIN_PLUGIN_TYPE, /**< ui_requirement */
0, /**< flags */
NULL, /**< dependencies */
PURPLE_PRIORITY_DEFAULT, /**< priority */
PLUGIN_ID, /**< id */
"Expand shortened URLs", /**< name */
PACKAGE_VERSION, /**< version */
"Expand inline short URLs", /**< summary */
"Expand inline short URLs", /**< description */
"Mike Miller <mikeage@gmail.com>", /* author */
"https://code.google.com/p/expand/", /* homepage */
plugin_load, /**< load */
plugin_unload, /**< unload */
plugin_destroy, /**< destroy */
NULL, /**< ui_info */
NULL, /**< extra_info */
&prefs_info, /**< prefs_info */
NULL, /**< actions */
NULL, /* padding... */
NULL,
NULL,
NULL,
};
static void plugin_init(PurplePlugin * plugin)
{
#ifdef ENABLE_NLS
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
#endif /* ENABLE_NLS */
info.summary = _("Expand inline short URLs");
info.description = _("Expand inline short URLs");
expand_plugin = plugin;
purple_prefs_add_none(PREF_PREFIX);
purple_prefs_add_bool(EXPAND_PREF_EXPAND_ALL_LINKS, TRUE);
purple_prefs_add_bool(EXPAND_PREF_EXPAND_TL, TRUE);
purple_prefs_add_bool(EXPAND_PREF_EXPAND_PICS, TRUE);
purple_prefs_add_int(EXPAND_PREF_EXPAND_PICS_SIZE, 72);
}
PURPLE_INIT_PLUGIN(expand_plugin, plugin_init, info)