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