2013-02-20 [colin] 3.9.0cvs95
[claws.git] / src / plugins / spam_report / spam_report.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net>
4  * and the Claws Mail Team
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 3 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 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include <unistd.h>
27 #include <stdio.h>
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include "common/claws.h"
33 #include "common/version.h"
34 #include "main.h"
35 #include "plugin.h"
36 #include "prefs_common.h"
37 #include "utils.h"
38 #include "spam_report_prefs.h"
39 #include "statusbar.h"
40 #include "procmsg.h"
41 #include "log.h"
42 #include "inc.h"
43 #include "plugin.h"
44 #include "menu.h"
45 #include "defs.h"
46 #include "base64.h"
47 #include "procheader.h"
48
49 #ifdef USE_PTHREAD
50 #include <pthread.h>
51 #endif
52
53 #include <curl/curl.h>
54 #include <curl/curlver.h>
55
56 struct CurlReadWrite {
57         char *data;
58         size_t size;
59 };
60
61 static gboolean check_debian_listid(MsgInfo *msginfo);
62
63 /* this interface struct is probably not enough for the various available 
64  * reporting places/methods. It'll be extended as necessary. */
65
66 ReportInterface spam_interfaces[] = {
67         { "Signal-Spam.fr", INTF_HTTP_AUTH, "https://www.signal-spam.fr/api/signaler",
68                 "message=%claws_mail_body_b64%", NULL},
69         { "Spamcop.net", INTF_MAIL, NULL, NULL, NULL},
70         { "Debian Lists", INTF_HTTP_GET, 
71                 "http://lists.debian.org/cgi-bin/nominate-for-review.pl?Quiet=on&msgid=%claws_mail_msgid%",
72                 NULL, check_debian_listid},
73         { NULL, INTF_NULL, NULL, NULL, NULL}
74 };
75
76 /* From RSSyl. This should be factorized to the core... */
77 static gchar *spamreport_strreplace(gchar *source, gchar *pattern,
78                 gchar *replacement)
79 {
80         gchar *new, *w_new, *c;
81         guint count = 0, final_length;
82         size_t len_pattern, len_replacement;
83
84         if( source == NULL || pattern == NULL ) {
85                 debug_print("source or pattern is NULL!!!\n");
86                 return NULL;
87         }
88
89         if( !g_utf8_validate(source, -1, NULL) ) {
90                 debug_print("source is not an UTF-8 encoded text\n");
91                 return NULL;
92         }
93
94         if( !g_utf8_validate(pattern, -1, NULL) ) {
95                 debug_print("pattern is not an UTF-8 encoded text\n");
96                 return NULL;
97         }
98
99         len_pattern = strlen(pattern);
100         len_replacement = replacement ? strlen(replacement) : 0;
101
102         c = source;
103         while( ( c = g_strstr_len(c, strlen(c), pattern) ) ) {
104                 count++;
105                 c += len_pattern;
106         }
107
108         final_length = strlen(source)
109                 - ( count * len_pattern )
110                 + ( count * len_replacement );
111
112         new = malloc(final_length + 1);
113         w_new = new;
114         memset(new, '\0', final_length + 1);
115
116         c = source;
117
118         while( *c != '\0' ) {
119                 if( !memcmp(c, pattern, len_pattern) ) {
120                         gboolean break_after_rep = FALSE;
121                         size_t i;
122                         if (*(c + len_pattern) == '\0')
123                                 break_after_rep = TRUE;
124                         for (i = 0; i < len_replacement; i++) {
125                                 *w_new = replacement[i];
126                                 w_new++;
127                         }
128                         if (break_after_rep)
129                                 break;
130                         c = c + len_pattern;
131                 } else {
132                         *w_new = *c;
133                         w_new++;
134                         c++;
135                 }
136         }
137         return new;
138 }
139
140 static gboolean check_debian_listid(MsgInfo *msginfo)
141 {
142         gchar buf[1024];
143         if (!procheader_get_header_from_msginfo(msginfo, buf, sizeof(buf), "List-Id:")) {
144                 if (strstr(buf, "lists.debian.org")) {
145                         return TRUE;
146                 }
147         }
148         return FALSE;
149 }
150
151 static void spamreport_http_response_log(gchar *url, long response)
152 {
153         switch (response) {
154         case 400: /* Bad Request */
155                 log_error(LOG_PROTOCOL, "%s: Bad Request\n", url);
156                 break;
157         case 401: /* Not Authorized */
158                 log_error(LOG_PROTOCOL, "%s: Wrong login or password\n", url);
159                 break;
160         case 404: /* Not Authorized */
161                 log_error(LOG_PROTOCOL, "%s: Not found\n", url);
162                 break;
163         }
164 }
165
166 static void *myrealloc(void *pointer, size_t size) {
167         /*
168          * There might be a realloc() out there that doesn't like reallocing
169          * NULL pointers, so we take care of it here.
170          */
171         if (pointer) {
172                 return realloc(pointer, size);
173         } else {
174                 return malloc(size);
175         }
176 }
177
178 static size_t curl_writefunction_cb(void *pointer, size_t size, size_t nmemb, void *data) {
179         size_t realsize = size * nmemb;
180         struct CurlReadWrite *mem = (struct CurlReadWrite *)data;
181
182         mem->data = myrealloc(mem->data, mem->size + realsize + 1);
183         if (mem->data) {
184                 memcpy(&(mem->data[mem->size]), pointer, realsize);
185                 mem->size += realsize;
186                 mem->data[mem->size] = 0;
187         }
188         return realsize;
189 }
190
191 static void report_spam(gint id, ReportInterface *intf, MsgInfo *msginfo, gchar *contents)
192 {
193         gchar *reqbody = NULL, *tmp = NULL, *auth = NULL, *b64 = NULL, *geturl = NULL;
194         size_t len_contents;
195         CURL *curl;
196         CURLcode res;
197         long response;
198         struct CurlReadWrite chunk;
199
200         chunk.data = NULL;
201         chunk.size = 0;
202         
203         if (spamreport_prefs.enabled[id] == FALSE) {
204                 debug_print("not reporting via %s (disabled)\n", intf->name);
205                 return;
206         }
207         if (intf->should_report != NULL && (intf->should_report)(msginfo) == FALSE) {
208                 debug_print("not reporting via %s (unsuitable)\n", intf->name);
209                 return;
210         }
211
212         debug_print("reporting via %s\n", intf->name);
213         tmp = spamreport_strreplace(intf->body, "%claws_mail_body%", contents);
214         len_contents = strlen(contents);
215         b64 = g_malloc0(B64LEN(len_contents) + 1);
216         base64_encode(b64, contents, len_contents);
217         reqbody = spamreport_strreplace(tmp, "%claws_mail_body_b64%", b64);
218         geturl = spamreport_strreplace(intf->url, "%claws_mail_msgid%", msginfo->msgid);
219         g_free(b64);
220         g_free(tmp);
221         
222         switch(intf->type) {
223         case INTF_HTTP_AUTH:
224                 if (spamreport_prefs.user[id] && *(spamreport_prefs.user[id])) {
225                         auth = g_strdup_printf("%s:%s", spamreport_prefs.user[id], spamreport_prefs.pass[id]);
226
227                         curl = curl_easy_init();
228                         curl_easy_setopt(curl, CURLOPT_URL, intf->url);
229                         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, reqbody);
230                         curl_easy_setopt(curl, CURLOPT_USERPWD, auth);
231                         curl_easy_setopt(curl, CURLOPT_TIMEOUT, prefs_common_get_prefs()->io_timeout_secs);
232                         curl_easy_setopt(curl, CURLOPT_USERAGENT,
233                                 SPAM_REPORT_USERAGENT "(" PLUGINS_URI ")");
234                         res = curl_easy_perform(curl);
235                         curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
236                         curl_easy_cleanup(curl);
237                         spamreport_http_response_log(intf->url, response);
238                         g_free(auth);
239                 }
240                 break;
241         case INTF_MAIL:
242                 if (spamreport_prefs.user[id] && *(spamreport_prefs.user[id])) {
243                         Compose *compose = compose_forward(NULL, msginfo, TRUE, NULL, TRUE, TRUE);
244                         compose->use_signing = FALSE;
245                         compose_entry_append(compose, spamreport_prefs.user[id], COMPOSE_TO, PREF_NONE);
246                         compose_send(compose);
247                 }
248                 break;
249         case INTF_HTTP_GET:
250                 curl = curl_easy_init();
251                 curl_easy_setopt(curl, CURLOPT_URL, geturl);
252                 curl_easy_setopt(curl, CURLOPT_USERAGENT,
253                                 SPAM_REPORT_USERAGENT "(" PLUGINS_URI ")");
254                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_writefunction_cb);
255                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
256                 res = curl_easy_perform(curl);
257                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
258                 curl_easy_cleanup(curl);
259                 spamreport_http_response_log(geturl, response);
260                 /* On success the page should return "OK: nominated <msgid>" */
261                 if (chunk.size < 13 || strstr(chunk.data, "OK: nominated") == NULL) {
262                         if (chunk.size > 0) {
263                                 log_error(LOG_PROTOCOL, "%s: response was %s\n", geturl, chunk.data);
264                         }
265                         else {
266                                 log_error(LOG_PROTOCOL, "%s: response was empty\n", geturl);
267                         }
268                 }
269                 break;
270         default:
271                 g_warning("Unknown method\n");
272         }
273         g_free(reqbody);
274         g_free(geturl);
275 }
276
277 static void report_spam_cb_ui(GtkAction *action, gpointer data)
278 {
279         MainWindow *mainwin = mainwindow_get_mainwindow();
280         SummaryView *summaryview = mainwin->summaryview;
281         GSList *msglist = summary_get_selected_msg_list(summaryview);
282         GSList *cur;
283         gint curnum=0, total=0;
284         if (summary_is_locked(summaryview) || !msglist) {
285                 if (msglist)
286                         g_slist_free(msglist);
287                 return;
288         }
289         main_window_cursor_wait(summaryview->mainwin);
290         gtk_cmclist_freeze(GTK_CMCLIST(summaryview->ctree));
291         folder_item_update_freeze();
292         inc_lock();
293
294         STATUSBAR_PUSH(mainwin, _("Reporting spam..."));
295         total = g_slist_length(msglist);
296
297         for (cur = msglist; cur; cur = cur->next) {
298                 MsgInfo *msginfo = (MsgInfo *)cur->data;
299                 gchar *file = procmsg_get_message_file(msginfo);
300                 gchar *contents = NULL;
301                 int i = 0;
302                 if (!file)
303                         continue;
304                 debug_print("reporting message %d (%s)\n", msginfo->msgnum, file);
305                 statusbar_progress_all(curnum, total, 1);
306                 GTK_EVENTS_FLUSH();
307                 curnum++;
308
309                 contents = file_read_to_str(file);
310                 
311                 for (i = 0; i < INTF_LAST; i++)
312                         report_spam(i, &(spam_interfaces[i]), msginfo, contents);
313                 
314                 g_free(contents);
315                 g_free(file);
316         }
317
318         statusbar_progress_all(0,0,0);
319         STATUSBAR_POP(mainwin);
320         inc_unlock();
321         folder_item_update_thaw();
322         gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
323         main_window_cursor_normal(summaryview->mainwin);
324         g_slist_free(msglist);
325 }
326
327 static GtkActionEntry spamreport_main_menu[] = {{
328         "Message/ReportSpam",
329         NULL, N_("Report spam online..."), NULL, NULL, G_CALLBACK(report_spam_cb_ui)
330 }};
331
332 static guint context_menu_id = 0;
333 static guint main_menu_id = 0;
334
335 gint plugin_init(gchar **error)
336 {
337         MainWindow *mainwin = mainwindow_get_mainwindow();
338
339         if (!check_plugin_version(MAKE_NUMERIC_VERSION(3,7,3,13),
340                                 VERSION_NUMERIC, _("SpamReport"), error))
341                 return -1;
342
343         spamreport_prefs_init();
344
345         curl_global_init(CURL_GLOBAL_DEFAULT);
346
347         gtk_action_group_add_actions(mainwin->action_group, spamreport_main_menu,
348                         1, (gpointer)mainwin);
349         MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menu/Message", "ReportSpam", 
350                           "Message/ReportSpam", GTK_UI_MANAGER_MENUITEM,
351                           main_menu_id)
352         MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "ReportSpam", 
353                           "Message/ReportSpam", GTK_UI_MANAGER_MENUITEM,
354                           context_menu_id)
355         return 0;
356 }
357
358 gboolean plugin_done(void)
359 {
360         MainWindow *mainwin = mainwindow_get_mainwindow();
361         SummaryView *summaryview = NULL;
362
363         if (mainwin == NULL)
364                 return TRUE;
365
366         summaryview = mainwin->summaryview;
367         MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "Message/ReportSpam", main_menu_id);
368         main_menu_id = 0;
369
370         MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "Message/ReportSpam", context_menu_id);
371         context_menu_id = 0;
372
373         spamreport_prefs_done();
374
375         return TRUE;
376 }
377
378 const gchar *plugin_name(void)
379 {
380         return _("SpamReport");
381 }
382
383 const gchar *plugin_desc(void)
384 {
385         return _("This plugin reports spam to various places.\n"
386                  "Currently the following sites or methods are supported:\n\n"
387                  " * spam-signal.fr\n"
388                  " * spamcop.net\n"
389                  " * lists.debian.org nomination system");
390 }
391
392 const gchar *plugin_type(void)
393 {
394         return "GTK2";
395 }
396
397 const gchar *plugin_licence(void)
398 {
399         return "GPL3+";
400 }
401
402 const gchar *plugin_version(void)
403 {
404         return VERSION;
405 }
406
407 struct PluginFeature *plugin_provides(void)
408 {
409         static struct PluginFeature features[] = 
410                 { {PLUGIN_UTILITY, N_("Spam reporting")},
411                   {PLUGIN_NOTHING, NULL}};
412         return features;
413 }