comparison finch/plugins/gnttinyurl.c @ 26500:8986c3804ada

Add a gnt TinyURL plugin. Closes #2017
author Richard Nelson <wabz@pidgin.im>
date Mon, 06 Apr 2009 10:36:43 +0000
parents
children f9a923fde1e4
comparison
equal deleted inserted replaced
26499:5617edc6c7a5 26500:8986c3804ada
1 /**
2 * @file gnttinyurl.c
3 *
4 * Copyright (C) 2009 Richard Nelson <wabz@whatsbeef.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 */
20
21
22 #include "internal.h"
23 #include <glib.h>
24
25 #define PLUGIN_STATIC_NAME TinyURL
26 #define PREFS_BASE "/plugins/gnt/tinyurl"
27 #define PREF_LENGTH PREFS_BASE "/length"
28 #define PREF_URL PREFS_BASE "/url"
29
30
31 #include <conversation.h>
32 #include <signals.h>
33
34 #include <glib.h>
35
36 #include <plugin.h>
37 #include <version.h>
38 #include <debug.h>
39 #include <notify.h>
40
41 #include <gntconv.h>
42
43 #include <gntplugin.h>
44 #include <gnttextview.h>
45
46 static int tag_num = 0;
47
48 typedef struct
49 {
50 PurpleConversation *conv;
51 gchar *tag;
52 int num;
53 } CbInfo;
54
55 /* 3 functions from util.c */
56 static gboolean
57 badchar(char c)
58 {
59 switch (c) {
60 case ' ':
61 case ',':
62 case '\0':
63 case '\n':
64 case '\r':
65 case '<':
66 case '>':
67 case '"':
68 case '\'':
69 return TRUE;
70 default:
71 return FALSE;
72 }
73 }
74
75 static gboolean
76 badentity(const char *c)
77 {
78 if (!g_ascii_strncasecmp(c, "&lt;", 4) ||
79 !g_ascii_strncasecmp(c, "&gt;", 4) ||
80 !g_ascii_strncasecmp(c, "&quot;", 6)) {
81 return TRUE;
82 }
83 return FALSE;
84 }
85
86 static GList *extract_urls(char *text) {
87 const char *t, *c, *q = NULL;
88 char *url_buf;
89 GList *ret = NULL;
90 gboolean inside_html = FALSE;
91 int inside_paren = 0;
92 c = text;
93 while (*c) {
94 if (*c == '(' && !inside_html) {
95 inside_paren++;
96 c++;
97 }
98 if (inside_html) {
99 if (*c == '>') {
100 inside_html = FALSE;
101 } else if (!q && (*c == '\"' || *c == '\'')) {
102 q = c;
103 } else if(q) {
104 if(*c == *q)
105 q = NULL;
106 }
107 } else if (*c == '<') {
108 inside_html = TRUE;
109 if (!g_ascii_strncasecmp(c, "<A", 2)) {
110 while (1) {
111 if (*c == '>') {
112 inside_html = FALSE;
113 break;
114 }
115 c++;
116 if (!(*c))
117 break;
118 }
119 }
120 } else if ((*c=='h') && (!g_ascii_strncasecmp(c, "http://", 7) ||
121 (!g_ascii_strncasecmp(c, "https://", 8)))) {
122 t = c;
123 while (1) {
124 if (badchar(*t) || badentity(t)) {
125
126 if ((!g_ascii_strncasecmp(c, "http://", 7) && (t - c == 7)) ||
127 (!g_ascii_strncasecmp(c, "https://", 8) && (t - c == 8))) {
128 break;
129 }
130
131 if (*(t) == ',' && (*(t + 1) != ' ')) {
132 t++;
133 continue;
134 }
135
136 if (*(t - 1) == '.')
137 t--;
138 if ((*(t - 1) == ')' && (inside_paren > 0))) {
139 t--;
140 }
141
142 url_buf = g_strndup(c, t - c);
143 if (!g_list_find_custom(ret, url_buf, (GCompareFunc)g_strcmp0)) {
144 purple_debug_info("TinyURL", "Added URL %s\n", url_buf);
145 ret = g_list_append(ret, g_strdup(url_buf));
146 }
147 c = t;
148 break;
149 }
150 t++;
151
152 }
153 } else if (!g_ascii_strncasecmp(c, "www.", 4) && (c == text || badchar(c[-1]) || badentity(c-1))) {
154 if (c[4] != '.') {
155 t = c;
156 while (1) {
157 if (badchar(*t) || badentity(t)) {
158 if (t - c == 4) {
159 break;
160 }
161
162 if (*(t) == ',' && (*(t + 1) != ' ')) {
163 t++;
164 continue;
165 }
166
167 if (*(t - 1) == '.')
168 t--;
169 if ((*(t - 1) == ')' && (inside_paren > 0))) {
170 t--;
171 }
172 url_buf = g_strndup(c, t - c);
173 if (!g_list_find_custom(ret, url_buf, (GCompareFunc)g_strcmp0)) {
174 purple_debug_info("TinyURL", "Added URL %s\n", url_buf);
175 ret = g_list_append(ret, url_buf);
176 }
177 c = t;
178 break;
179 }
180 t++;
181 }
182 }
183 }
184 if (*c == ')' && !inside_html) {
185 inside_paren--;
186 c++;
187 }
188 if (*c == 0)
189 break;
190 c++;
191 }
192 return ret;
193 }
194
195 static void url_fetched(PurpleUtilFetchUrlData *url_data, gpointer cb_data,
196 const gchar *url_text, gsize len, const gchar *error_message)
197 {
198 CbInfo *data = (CbInfo *)cb_data;
199 PurpleConversation *conv = data->conv;
200 GList *convs = purple_get_conversations();
201 /* ensure the conversation still exists */
202 for (; convs; convs = convs->next) {
203 if ((PurpleConversation *)(convs->data) == conv) {
204 FinchConv *fconv = FINCH_CONV(conv);
205 gchar *str = g_strdup_printf("[%d] %s", data->num, url_text);
206 GntTextView *tv = GNT_TEXT_VIEW(fconv->tv);
207 gnt_text_view_tag_change(tv, data->tag, str, FALSE);
208 g_free(str);
209 g_free(data->tag);
210 return;
211 }
212 }
213 g_free(data->tag);
214 purple_debug_info("TinyURL", "Conversation no longer exists... :(\n");
215 }
216
217 static void free_urls(gpointer data, gpointer null)
218 {
219 g_free(data);
220 }
221
222 static gboolean receiving_msg(PurpleAccount *account, char **sender, char **message,
223 PurpleConversation *conv, PurpleMessageFlags *flags) {
224 GString *t;
225 GList *iter, *urls;
226 int c = 0;
227
228 if (!(*flags & PURPLE_MESSAGE_RECV) || *flags & PURPLE_MESSAGE_INVISIBLE)
229 return FALSE;
230
231 t = g_string_new(*message);
232 urls = purple_conversation_get_data(conv, "TinyURLs");
233 if (urls != NULL) /* message was cancelled somewhere? Reset. */
234 g_list_foreach(urls, free_urls, NULL);
235 g_list_free(urls);
236 urls = extract_urls(t->str);
237 g_free(*message);
238 for (iter = urls; iter; iter = iter->next) {
239 if (g_utf8_strlen((char *)iter->data, -1) >= purple_prefs_get_int(PREF_LENGTH)) {
240 int pos, x = 0;
241 gchar *j, *s, *str, *orig;
242 glong len = g_utf8_strlen(iter->data, -1);
243 s = g_strdup(t->str);
244 orig = s;
245 str = g_strdup_printf("[%d]", ++c);
246 while ((j = strstr(s, iter->data))) { /* replace all occurrences */
247 pos = j - orig + (x++ * 3);
248 s = j + len;
249 t = g_string_insert(t, pos + len, str);
250 if (*s == '\0') break;
251 }
252 g_free(orig);
253 g_free(str);
254 continue;
255 } else {
256 if (iter->prev) {
257 iter = iter->prev;
258 g_free(iter->next->data);
259 urls = g_list_delete_link(urls, iter->next);
260 } else {
261 g_free(iter->data);
262 g_list_free(urls);
263 urls = NULL;
264 }
265 }
266 }
267 *message = t->str;
268 g_string_free(t, FALSE);
269 if (conv == NULL)
270 conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, *sender);
271 purple_conversation_set_data(conv, "TinyURLs", urls);
272 return FALSE;
273 }
274
275 static void received_msg(PurpleAccount *account, char *sender, char *message,
276 PurpleConversation *conv, PurpleMessageFlags flags) {
277 int c;
278 GList *urls, *iter;
279 FinchConv *fconv = FINCH_CONV(conv);
280 GntTextView *tv = GNT_TEXT_VIEW(fconv->tv);
281
282 urls = purple_conversation_get_data(conv, "TinyURLs");
283 if (!(flags & PURPLE_MESSAGE_RECV) || urls == NULL)
284 return;
285
286 for (iter = urls, c = 0; iter; iter = iter->next) {
287 int i;
288 CbInfo *cbdata;
289 gchar *url, *str, *tmp;
290 cbdata = g_new(CbInfo, 1);
291 cbdata->num = ++c;
292 cbdata->tag = g_strdup_printf("%s%d", "tiny_", tag_num++);
293 cbdata->conv = conv;
294 tmp = purple_unescape_html((char *)iter->data);
295 if (g_ascii_strncasecmp(tmp, "http://", 7) && g_ascii_strncasecmp(tmp, "https://", 8)) {
296 url = g_strdup_printf("%shttp://%s", purple_prefs_get_string(PREF_URL), tmp);
297 } else {
298 url = g_strdup_printf("%s%s", purple_prefs_get_string(PREF_URL), tmp);
299 }
300 g_free(tmp);
301 purple_util_fetch_url(url, TRUE, "finch", FALSE, url_fetched, cbdata);
302 i = gnt_text_view_get_lines_below(tv);
303 str = g_strdup_printf(_("\nFetching TinyURL..."));
304 gnt_text_view_append_text_with_tag((tv), str, GNT_TEXT_FLAG_DIM, cbdata->tag);
305 g_free(str);
306 if (i == 0)
307 gnt_text_view_scroll(tv, 0);
308 g_free(iter->data);
309 g_free(url);
310 }
311 g_list_free(urls);
312 purple_conversation_set_data(conv, "TinyURLs", NULL);
313 }
314
315 static void
316 free_conv_urls(PurpleConversation *conv)
317 {
318 GList *urls = purple_conversation_get_data(conv, "TinyURLs");
319 if (urls)
320 g_list_foreach(urls, free_urls, NULL);
321 g_list_free(urls);
322 }
323
324 static gboolean
325 plugin_load(PurplePlugin *plugin) {
326 purple_signal_connect(purple_conversations_get_handle(),
327 "wrote-im-msg",
328 plugin, PURPLE_CALLBACK(received_msg), NULL);
329 purple_signal_connect(purple_conversations_get_handle(),
330 "wrote-chat-msg",
331 plugin, PURPLE_CALLBACK(received_msg), NULL);
332 purple_signal_connect(purple_conversations_get_handle(),
333 "receiving-im-msg",
334 plugin, PURPLE_CALLBACK(receiving_msg), NULL);
335 purple_signal_connect(purple_conversations_get_handle(),
336 "receiving-chat-msg",
337 plugin, PURPLE_CALLBACK(receiving_msg), NULL);
338 purple_signal_connect(purple_conversations_get_handle(),
339 "deleting-conversation",
340 plugin, PURPLE_CALLBACK(free_conv_urls), NULL);
341
342 return TRUE;
343 }
344
345 static PurplePluginPrefFrame *
346 get_plugin_pref_frame(PurplePlugin *plugin) {
347
348 PurplePluginPrefFrame *frame;
349 PurplePluginPref *pref;
350
351 frame = purple_plugin_pref_frame_new();
352
353 pref = purple_plugin_pref_new_with_name(PREF_LENGTH);
354 purple_plugin_pref_set_label(pref, _("Only create TinyURL for urls"
355 " of this length or greater"));
356 purple_plugin_pref_frame_add(frame, pref);
357 pref = purple_plugin_pref_new_with_name(PREF_URL);
358 purple_plugin_pref_set_label(pref, _("TinyURL (or other) address prefix"));
359 purple_plugin_pref_frame_add(frame, pref);
360
361 return frame;
362 }
363
364 static PurplePluginUiInfo prefs_info = {
365 get_plugin_pref_frame,
366 0, /* page_num (Reserved) */
367 NULL, /* frame (Reserved) */
368
369 /* padding */
370 NULL,
371 NULL,
372 NULL,
373 NULL
374 };
375
376 static PurplePluginInfo info =
377 {
378 PURPLE_PLUGIN_MAGIC,
379 PURPLE_MAJOR_VERSION,
380 PURPLE_MINOR_VERSION,
381 PURPLE_PLUGIN_STANDARD,
382 FINCH_PLUGIN_TYPE,
383 0,
384 NULL,
385 PURPLE_PRIORITY_DEFAULT,
386 "TinyURL",
387 N_("TinyURL"),
388 DISPLAY_VERSION,
389 N_("TinyURL plugin"),
390 N_("When receiving a message with URL(s), TinyURL for easier copying"),
391 "Richard Nelson <wabz@whatsbeef.net>",
392 PURPLE_WEBSITE,
393 plugin_load,
394 NULL,
395 NULL,
396 NULL,
397 NULL,
398 &prefs_info, /**< prefs_info */
399 NULL,
400
401 /* padding */
402 NULL,
403 NULL,
404 NULL,
405 NULL
406 };
407
408 static void
409 init_plugin(PurplePlugin *plugin) {
410 purple_prefs_add_none(PREFS_BASE);
411 purple_prefs_add_int(PREF_LENGTH, 30);
412 purple_prefs_add_string(PREF_URL, "http://tinyurl.com/api-create.php?url=");
413 }
414
415 PURPLE_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info)