701ffb0c3d9113bf32ead83a3955c6b4edf34063
[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 "password.h"
36 #include "plugin.h"
37 #include "prefs_common.h"
38 #include "utils.h"
39 #include "spam_report_prefs.h"
40 #include "statusbar.h"
41 #include "procmsg.h"
42 #include "log.h"
43 #include "inc.h"
44 #include "plugin.h"
45 #include "menu.h"
46 #include "defs.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_base64_encode(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                         gchar *pass = password_decrypt(spamreport_prefs.pass[id], NULL);
226                         auth = g_strdup_printf("%s:%s", spamreport_prefs.user[id], (pass != NULL ? pass : ""));
227                         if (pass != NULL) {
228                                 memset(pass, 0, strlen(pass));
229                         }
230                         g_free(pass);
231
232                         curl = curl_easy_init();
233                         curl_easy_setopt(curl, CURLOPT_URL, intf->url);
234                         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, reqbody);
235                         curl_easy_setopt(curl, CURLOPT_USERPWD, auth);
236                         curl_easy_setopt(curl, CURLOPT_TIMEOUT, prefs_common_get_prefs()->io_timeout_secs);
237                         curl_easy_setopt(curl, CURLOPT_USERAGENT,
238                                 SPAM_REPORT_USERAGENT "(" PLUGINS_URI ")");
239                         curl_easy_perform(curl);
240                         curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
241                         curl_easy_cleanup(curl);
242                         spamreport_http_response_log(intf->url, response);
243                         g_free(auth);
244                 }
245                 break;
246         case INTF_MAIL:
247                 if (spamreport_prefs.user[id] && *(spamreport_prefs.user[id])) {
248                         Compose *compose = compose_forward(NULL, msginfo, TRUE, NULL, TRUE, TRUE);
249                         compose->use_signing = FALSE;
250                         compose_entry_append(compose, spamreport_prefs.user[id], COMPOSE_TO, PREF_NONE);
251                         compose_send(compose);
252                 }
253                 break;
254         case INTF_HTTP_GET:
255                 curl = curl_easy_init();
256                 curl_easy_setopt(curl, CURLOPT_URL, geturl);
257                 curl_easy_setopt(curl, CURLOPT_USERAGENT,
258                                 SPAM_REPORT_USERAGENT "(" PLUGINS_URI ")");
259                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_writefunction_cb);
260                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
261                 curl_easy_perform(curl);
262                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
263                 curl_easy_cleanup(curl);
264                 spamreport_http_response_log(geturl, response);
265                 /* On success the page should return "OK: nominated <msgid>" */
266                 if (chunk.size < 13 || strstr(chunk.data, "OK: nominated") == NULL) {
267                         if (chunk.size > 0) {
268                                 log_error(LOG_PROTOCOL, "%s: response was %s\n", geturl, chunk.data);
269                         }
270                         else {
271                                 log_error(LOG_PROTOCOL, "%s: response was empty\n", geturl);
272                         }
273                 }
274                 break;
275         default:
276                 g_warning("Unknown method");
277         }
278         g_free(reqbody);
279         g_free(geturl);
280 }
281
282 static void report_spam_cb_ui(GtkAction *action, gpointer data)
283 {
284         MainWindow *mainwin = mainwindow_get_mainwindow();
285         SummaryView *summaryview = mainwin->summaryview;
286         GSList *msglist = summary_get_selected_msg_list(summaryview);
287         GSList *cur;
288         gint curnum=0, total=0;
289         if (summary_is_locked(summaryview) || !msglist) {
290                 if (msglist)
291                         g_slist_free(msglist);
292                 return;
293         }
294         main_window_cursor_wait(summaryview->mainwin);
295         gtk_cmclist_freeze(GTK_CMCLIST(summaryview->ctree));
296         folder_item_update_freeze();
297         inc_lock();
298
299         STATUSBAR_PUSH(mainwin, _("Reporting spam..."));
300         total = g_slist_length(msglist);
301
302         for (cur = msglist; cur; cur = cur->next) {
303                 MsgInfo *msginfo = (MsgInfo *)cur->data;
304                 gchar *file = procmsg_get_message_file(msginfo);
305                 gchar *contents = NULL;
306                 int i = 0;
307                 if (!file)
308                         continue;
309                 debug_print("reporting message %d (%s)\n", msginfo->msgnum, file);
310                 statusbar_progress_all(curnum, total, 1);
311                 GTK_EVENTS_FLUSH();
312                 curnum++;
313
314                 contents = file_read_to_str(file);
315                 
316                 for (i = 0; i < INTF_LAST; i++)
317                         report_spam(i, &(spam_interfaces[i]), msginfo, contents);
318                 
319                 g_free(contents);
320                 g_free(file);
321         }
322
323         statusbar_progress_all(0,0,0);
324         STATUSBAR_POP(mainwin);
325         inc_unlock();
326         folder_item_update_thaw();
327         gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
328         main_window_cursor_normal(summaryview->mainwin);
329         g_slist_free(msglist);
330 }
331
332 static GtkActionEntry spamreport_main_menu[] = {{
333         "Message/ReportSpam",
334         NULL, N_("Report spam online..."), NULL, NULL, G_CALLBACK(report_spam_cb_ui)
335 }};
336
337 static guint context_menu_id = 0;
338 static guint main_menu_id = 0;
339
340 gint plugin_init(gchar **error)
341 {
342         MainWindow *mainwin = mainwindow_get_mainwindow();
343
344         if (!check_plugin_version(MAKE_NUMERIC_VERSION(3,13,2,39),
345                                 VERSION_NUMERIC, _("SpamReport"), error))
346                 return -1;
347
348         spamreport_prefs_init();
349
350         curl_global_init(CURL_GLOBAL_DEFAULT);
351
352         gtk_action_group_add_actions(mainwin->action_group, spamreport_main_menu,
353                         1, (gpointer)mainwin);
354         MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menu/Message", "ReportSpam", 
355                           "Message/ReportSpam", GTK_UI_MANAGER_MENUITEM,
356                           main_menu_id)
357         MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "ReportSpam", 
358                           "Message/ReportSpam", GTK_UI_MANAGER_MENUITEM,
359                           context_menu_id)
360         return 0;
361 }
362
363 gboolean plugin_done(void)
364 {
365         MainWindow *mainwin = mainwindow_get_mainwindow();
366
367         if (mainwin == NULL)
368                 return TRUE;
369
370         MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "Message/ReportSpam", main_menu_id);
371         main_menu_id = 0;
372
373         MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "Message/ReportSpam", context_menu_id);
374         context_menu_id = 0;
375
376         spamreport_prefs_done();
377
378         return TRUE;
379 }
380
381 const gchar *plugin_name(void)
382 {
383         return _("SpamReport");
384 }
385
386 const gchar *plugin_desc(void)
387 {
388         return _("This plugin reports spam to various places.\n"
389                  "Currently the following sites or methods are supported:\n\n"
390                  " * spam-signal.fr\n"
391                  " * spamcop.net\n"
392                  " * lists.debian.org nomination system");
393 }
394
395 const gchar *plugin_type(void)
396 {
397         return "GTK2";
398 }
399
400 const gchar *plugin_licence(void)
401 {
402         return "GPL3+";
403 }
404
405 const gchar *plugin_version(void)
406 {
407         return VERSION;
408 }
409
410 struct PluginFeature *plugin_provides(void)
411 {
412         static struct PluginFeature features[] = 
413                 { {PLUGIN_UTILITY, N_("Spam reporting")},
414                   {PLUGIN_NOTHING, NULL}};
415         return features;
416 }