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
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.
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.
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.
23 #include "claws-features.h"
30 #include <glib/gi18n.h>
32 #include "common/claws.h"
33 #include "common/version.h"
37 #include "prefs_common.h"
38 #include "file-utils.h"
40 #include "spam_report_prefs.h"
41 #include "statusbar.h"
48 #include "procheader.h"
54 #include <curl/curl.h>
55 #include <curl/curlver.h>
57 struct CurlReadWrite {
62 static gboolean check_debian_listid(MsgInfo *msginfo);
64 /* this interface struct is probably not enough for the various available
65 * reporting places/methods. It'll be extended as necessary. */
67 #define SSFR_URL "https://www.signal-spam.fr/api/signaler"
68 #define SSFR_BODY "message=%claws_mail_body_b64%"
70 #define DEBL_URL "https://lists.debian.org/cgi-bin/nominate-for-review.pl?Quiet=on&msgid=%claws_mail_msgid%"
72 ReportInterface spam_interfaces[] = {
73 { "Signal-Spam.fr", INTF_HTTP_AUTH, SSFR_URL, SSFR_BODY, NULL},
74 { "Spamcop.net", INTF_MAIL, NULL, NULL, NULL},
75 { "Debian Lists", INTF_HTTP_GET, DEBL_URL, NULL, check_debian_listid},
76 { NULL, INTF_NULL, NULL, NULL, NULL}
79 /* From RSSyl. This should be factorized to the core... */
80 static gchar *spamreport_strreplace(gchar *source, gchar *pattern,
83 gchar *new, *w_new, *c;
84 guint count = 0, final_length;
85 size_t len_pattern, len_replacement;
87 if( source == NULL || pattern == NULL ) {
88 debug_print("source or pattern is NULL!!!\n");
92 if( !g_utf8_validate(source, -1, NULL) ) {
93 debug_print("source is not an UTF-8 encoded text\n");
97 if( !g_utf8_validate(pattern, -1, NULL) ) {
98 debug_print("pattern is not an UTF-8 encoded text\n");
102 len_pattern = strlen(pattern);
103 len_replacement = replacement ? strlen(replacement) : 0;
106 while( ( c = g_strstr_len(c, strlen(c), pattern) ) ) {
111 final_length = strlen(source)
112 - ( count * len_pattern )
113 + ( count * len_replacement );
115 new = malloc(final_length + 1);
117 memset(new, '\0', final_length + 1);
121 while( *c != '\0' ) {
122 if( !memcmp(c, pattern, len_pattern) ) {
123 gboolean break_after_rep = FALSE;
125 if (*(c + len_pattern) == '\0')
126 break_after_rep = TRUE;
127 for (i = 0; i < len_replacement; i++) {
128 *w_new = replacement[i];
143 static gboolean check_debian_listid(MsgInfo *msginfo)
146 if (!procheader_get_header_from_msginfo(msginfo, &buf, "List-Id:") && buf != NULL) {
147 if (strstr(buf, "lists.debian.org")) {
156 static void spamreport_http_response_log(gchar *url, long response)
159 case 400: /* Bad Request */
160 log_error(LOG_PROTOCOL, "%s: Bad Request\n", url);
162 case 401: /* Not Authorized */
163 log_error(LOG_PROTOCOL, "%s: Wrong login or password\n", url);
165 case 404: /* Not Authorized */
166 log_error(LOG_PROTOCOL, "%s: Not found\n", url);
171 static void *myrealloc(void *pointer, size_t size) {
173 * There might be a realloc() out there that doesn't like reallocing
174 * NULL pointers, so we take care of it here.
177 return realloc(pointer, size);
183 static size_t curl_writefunction_cb(void *pointer, size_t size, size_t nmemb, void *data) {
184 size_t realsize = size * nmemb;
185 struct CurlReadWrite *mem = (struct CurlReadWrite *)data;
187 mem->data = myrealloc(mem->data, mem->size + realsize + 1);
189 memcpy(&(mem->data[mem->size]), pointer, realsize);
190 mem->size += realsize;
191 mem->data[mem->size] = 0;
196 static void report_spam(gint id, ReportInterface *intf, MsgInfo *msginfo, gchar *contents)
198 gchar *reqbody = NULL, *tmp = NULL, *auth = NULL, *b64 = NULL, *geturl = NULL;
202 struct CurlReadWrite chunk;
207 if (spamreport_prefs.enabled[id] == FALSE) {
208 debug_print("not reporting via %s (disabled)\n", intf->name);
211 if (intf->should_report != NULL && (intf->should_report)(msginfo) == FALSE) {
212 debug_print("not reporting via %s (unsuitable)\n", intf->name);
216 debug_print("reporting via %s\n", intf->name);
217 tmp = spamreport_strreplace(intf->body, "%claws_mail_body%", contents);
218 len_contents = strlen(contents);
219 b64 = g_base64_encode(contents, len_contents);
220 reqbody = spamreport_strreplace(tmp, "%claws_mail_body_b64%", b64);
221 geturl = spamreport_strreplace(intf->url, "%claws_mail_msgid%", msginfo->msgid);
227 if (spamreport_prefs.user[id] && *(spamreport_prefs.user[id])) {
228 gchar *pass = spamreport_passwd_get(spam_interfaces[id].name);
229 auth = g_strdup_printf("%s:%s", spamreport_prefs.user[id], (pass != NULL ? pass : ""));
231 memset(pass, 0, strlen(pass));
235 curl = curl_easy_init();
236 curl_easy_setopt(curl, CURLOPT_URL, intf->url);
237 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, reqbody);
238 curl_easy_setopt(curl, CURLOPT_USERPWD, auth);
239 curl_easy_setopt(curl, CURLOPT_TIMEOUT, prefs_common_get_prefs()->io_timeout_secs);
240 curl_easy_setopt(curl, CURLOPT_USERAGENT,
241 SPAM_REPORT_USERAGENT "(" PLUGINS_URI ")");
242 curl_easy_perform(curl);
243 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
244 curl_easy_cleanup(curl);
245 spamreport_http_response_log(intf->url, response);
250 if (spamreport_prefs.user[id] && *(spamreport_prefs.user[id])) {
251 Compose *compose = compose_forward(NULL, msginfo, TRUE, NULL, TRUE, TRUE);
252 compose->use_signing = FALSE;
253 compose_entry_append(compose, spamreport_prefs.user[id], COMPOSE_TO, PREF_NONE);
254 compose_send(compose);
258 curl = curl_easy_init();
259 curl_easy_setopt(curl, CURLOPT_URL, geturl);
260 curl_easy_setopt(curl, CURLOPT_USERAGENT,
261 SPAM_REPORT_USERAGENT "(" PLUGINS_URI ")");
262 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_writefunction_cb);
263 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
264 curl_easy_perform(curl);
265 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
266 curl_easy_cleanup(curl);
267 spamreport_http_response_log(geturl, response);
268 /* On success the page should return "OK: nominated <msgid>" */
269 if (chunk.size < 13 || strstr(chunk.data, "OK: nominated") == NULL) {
270 if (chunk.size > 0) {
271 log_error(LOG_PROTOCOL, "%s: response was %s\n", geturl, chunk.data);
274 log_error(LOG_PROTOCOL, "%s: response was empty\n", geturl);
279 g_warning("Unknown method");
285 static void report_spam_cb_ui(GtkAction *action, gpointer data)
287 MainWindow *mainwin = mainwindow_get_mainwindow();
288 SummaryView *summaryview = mainwin->summaryview;
289 GSList *msglist = summary_get_selected_msg_list(summaryview);
291 gint curnum=0, total=0;
292 if (summary_is_locked(summaryview) || !msglist) {
294 g_slist_free(msglist);
297 main_window_cursor_wait(summaryview->mainwin);
298 gtk_cmclist_freeze(GTK_CMCLIST(summaryview->ctree));
299 folder_item_update_freeze();
302 STATUSBAR_PUSH(mainwin, _("Reporting spam..."));
303 total = g_slist_length(msglist);
305 for (cur = msglist; cur; cur = cur->next) {
306 MsgInfo *msginfo = (MsgInfo *)cur->data;
307 gchar *file = procmsg_get_message_file(msginfo);
308 gchar *contents = NULL;
312 debug_print("reporting message %d (%s)\n", msginfo->msgnum, file);
313 statusbar_progress_all(curnum, total, 1);
317 contents = file_read_to_str(file);
319 for (i = 0; i < INTF_LAST; i++)
320 report_spam(i, &(spam_interfaces[i]), msginfo, contents);
326 statusbar_progress_all(0,0,0);
327 STATUSBAR_POP(mainwin);
329 folder_item_update_thaw();
330 gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
331 main_window_cursor_normal(summaryview->mainwin);
332 g_slist_free(msglist);
335 static GtkActionEntry spamreport_main_menu[] = {{
336 "Message/ReportSpam",
337 NULL, N_("Report spam online..."), NULL, NULL, G_CALLBACK(report_spam_cb_ui)
340 static guint context_menu_id = 0;
341 static guint main_menu_id = 0;
343 gint plugin_init(gchar **error)
345 MainWindow *mainwin = mainwindow_get_mainwindow();
347 if (!check_plugin_version(MAKE_NUMERIC_VERSION(3,13,2,39),
348 VERSION_NUMERIC, _("SpamReport"), error))
351 spamreport_prefs_init();
353 curl_global_init(CURL_GLOBAL_DEFAULT);
355 gtk_action_group_add_actions(mainwin->action_group, spamreport_main_menu,
356 1, (gpointer)mainwin);
357 MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menu/Message", "ReportSpam",
358 "Message/ReportSpam", GTK_UI_MANAGER_MENUITEM,
360 MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "ReportSpam",
361 "Message/ReportSpam", GTK_UI_MANAGER_MENUITEM,
366 gboolean plugin_done(void)
368 MainWindow *mainwin = mainwindow_get_mainwindow();
373 MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "Message/ReportSpam", main_menu_id);
376 MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "Message/ReportSpam", context_menu_id);
379 spamreport_prefs_done();
384 const gchar *plugin_name(void)
386 return _("SpamReport");
389 const gchar *plugin_desc(void)
391 return _("This plugin reports spam to various places.\n"
392 "Currently the following sites or methods are supported:\n\n"
393 " * spam-signal.fr\n"
395 " * lists.debian.org nomination system");
398 const gchar *plugin_type(void)
403 const gchar *plugin_licence(void)
408 const gchar *plugin_version(void)
413 struct PluginFeature *plugin_provides(void)
415 static struct PluginFeature features[] =
416 { {PLUGIN_UTILITY, N_("Spam reporting")},
417 {PLUGIN_NOTHING, NULL}};