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