Make SpamReport plugin use the password store.
[claws.git] / src / plugins / spam_report / spam_report_prefs.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 "defs.h"
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31
32 #include "gtkutils.h"
33 #include "password.h"
34 #include "prefs.h"
35 #include "prefs_gtk.h"
36 #include "prefswindow.h"
37 #include "alertpanel.h"
38 #include "utils.h"
39
40 #include "spam_report_prefs.h"
41
42 #define PREFS_BLOCK_NAME "SpamReport"
43
44 SpamReportPrefs spamreport_prefs;
45 void spamreport_clear_cache(void);
46
47 typedef struct _SpamReportPage SpamReportPage;
48
49 struct _SpamReportPage {
50         PrefsPage page;
51         GtkWidget *frame[INTF_LAST];
52         GtkWidget *enabled_chkbtn[INTF_LAST];
53         GtkWidget *user_entry[INTF_LAST];
54         GtkWidget *pass_entry[INTF_LAST];
55 };
56
57 static PrefParam param[] = {
58         {"signalspam_enabled", "FALSE", &spamreport_prefs.enabled[INTF_SIGNALSPAM], P_BOOL, NULL, NULL, NULL},
59         {"signalspam_user", "", &spamreport_prefs.user[INTF_SIGNALSPAM], P_STRING, NULL, NULL, NULL},
60         {"signalspam_pass", "", &spamreport_prefs.pass[INTF_SIGNALSPAM], P_PASSWORD, NULL, NULL, NULL},
61         {"spamcop_enabled",    "FALSE", &spamreport_prefs.enabled[INTF_SPAMCOP], P_BOOL, NULL, NULL, NULL},
62         {"spamcop_user",    "", &spamreport_prefs.user[INTF_SPAMCOP], P_STRING, NULL, NULL, NULL},
63         {"debianspam_enabled", "FALSE", &spamreport_prefs.enabled[INTF_DEBIANSPAM], P_BOOL, NULL, NULL, NULL},
64        {0,0,0,0,0,0,0}
65 };
66
67 static SpamReportPage spamreport_prefs_page;
68
69 static void create_spamreport_prefs_page        (PrefsPage *page,
70                                          GtkWindow *window,
71                                          gpointer   data);
72 static void destroy_spamreport_prefs_page       (PrefsPage *page);
73 static void save_spamreport_prefs               (PrefsPage *page);
74
75 void spamreport_prefs_init(void)
76 {
77         static gchar *path[3];
78         gchar *rcpath;
79         guint i;
80
81         path[0] = _("Plugins");
82         path[1] = _("SpamReport");
83         path[2] = NULL;
84
85         prefs_set_default(param);
86         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
87         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
88         g_free(rcpath);
89
90         /* Move passwords that are still in main config to password store. */
91         for (i = 0; i < INTF_LAST; i++) {
92                 if (spamreport_prefs.pass[i] != NULL &&
93                                 strlen(spamreport_prefs.pass[i]) > 0) {
94                         spamreport_passwd_set(spam_interfaces[i].name,
95                                         spamreport_prefs.pass[i]);
96                 }
97         }
98
99         spamreport_prefs_page.page.path = path;
100         spamreport_prefs_page.page.create_widget = create_spamreport_prefs_page;
101         spamreport_prefs_page.page.destroy_widget = destroy_spamreport_prefs_page;
102         spamreport_prefs_page.page.save_page = save_spamreport_prefs;
103         spamreport_prefs_page.page.weight = 30.0;
104         
105         prefs_gtk_register_page((PrefsPage *) &spamreport_prefs_page);
106 }
107
108 void spamreport_prefs_done(void)
109 {
110         prefs_gtk_unregister_page((PrefsPage *) &spamreport_prefs_page);
111 }
112
113 static void create_spamreport_prefs_page(PrefsPage *page,
114                                     GtkWindow *window,
115                                     gpointer data)
116 {
117         SpamReportPage *prefs_page = (SpamReportPage *) page;
118
119         GtkWidget *vbox, *table;
120         GtkWidget *tmp;
121         int i = 0;
122         
123         vbox = gtk_vbox_new(FALSE, VSPACING_NARROW);
124         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
125         gtk_widget_show(vbox);
126        
127         for (i = 0; i < INTF_LAST; i++) {
128                 gchar *pass;
129                 prefs_page->frame[i] = gtk_frame_new(spam_interfaces[i].name);
130                 gtk_box_pack_start(GTK_BOX(vbox), prefs_page->frame[i], FALSE, FALSE, 6);
131
132                 prefs_page->user_entry[i] = gtk_entry_new();
133                 prefs_page->pass_entry[i] = gtk_entry_new();
134                 prefs_page->enabled_chkbtn[i] = gtk_check_button_new_with_label(_("Enabled"));
135
136                 gtk_entry_set_visibility(GTK_ENTRY(prefs_page->pass_entry[i]), FALSE);
137
138                 gtk_entry_set_text(GTK_ENTRY(prefs_page->user_entry[i]),
139                         spamreport_prefs.user[i] ? spamreport_prefs.user[i]:"");
140
141                 pass = spamreport_passwd_get(spam_interfaces[i].name);
142                 gtk_entry_set_text(GTK_ENTRY(prefs_page->pass_entry[i]), pass ? pass:"");
143                 if (pass != NULL) {
144                         memset(pass, 0, strlen(pass));
145                 }
146                 g_free(pass);
147                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_page->enabled_chkbtn[i]),
148                         spamreport_prefs.enabled[i]);
149
150                 table = gtk_table_new(3, 2, FALSE);
151                 gtk_container_set_border_width(GTK_CONTAINER(table), 8);
152                 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
153                 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
154         
155                 gtk_container_add(GTK_CONTAINER(prefs_page->frame[i]), table);
156                 gtk_widget_show(prefs_page->frame[i]);
157                 gtk_widget_show(table);
158
159                 gtk_table_attach(GTK_TABLE(table), prefs_page->enabled_chkbtn[i], 0, 2, 0, 1,
160                                 GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 
161                                 0, 0);
162                 gtk_widget_show(prefs_page->enabled_chkbtn[i]);
163
164                 switch(spam_interfaces[i].type) {
165                 case INTF_MAIL:
166                         tmp = gtk_label_new(_("Forward to:"));
167                         break;
168                 default:
169                         tmp = gtk_label_new(_("Username:"));
170                 }
171                 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
172                                 0, 0, 
173                                 0, 0);
174                 gtk_table_attach(GTK_TABLE(table), prefs_page->user_entry[i], 1, 2, 1, 2,
175                                 GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 
176                                 0, 0);
177                 if (spam_interfaces[i].type != INTF_HTTP_GET) {
178                         gtk_widget_show(tmp);
179                         gtk_widget_show(prefs_page->user_entry[i]);
180                 }
181
182                 tmp = gtk_label_new(_("Password:"));
183                 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 2, 3,
184                                 0, 0, 
185                                 0, 0);
186                 gtk_table_attach(GTK_TABLE(table), prefs_page->pass_entry[i], 1, 2, 2, 3,
187                                 GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 
188                                 0, 0);
189                 if (spam_interfaces[i].type != INTF_MAIL && spam_interfaces[i].type != INTF_HTTP_GET) {
190                         gtk_widget_show(tmp);
191                         gtk_widget_show(prefs_page->pass_entry[i]);
192                 }
193         }
194         prefs_page->page.widget = vbox;
195 }
196
197 static void destroy_spamreport_prefs_page(PrefsPage *page)
198 {
199         /* Do nothing! */
200 }
201
202 static void save_spamreport_prefs(PrefsPage *page)
203 {
204         SpamReportPage *prefs_page = (SpamReportPage *) page;
205         PrefFile *pref_file;
206         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
207                                           COMMON_RC, NULL);
208         int i = 0;
209         
210         for (i = 0; i < INTF_LAST; i++) {
211                 gchar *pass;
212
213                 g_free(spamreport_prefs.user[i]);
214                 g_free(spamreport_prefs.pass[i]);
215
216                 spamreport_prefs.enabled[i] = gtk_toggle_button_get_active(
217                         GTK_TOGGLE_BUTTON(prefs_page->enabled_chkbtn[i]));
218                 spamreport_prefs.user[i] = gtk_editable_get_chars(
219                         GTK_EDITABLE(prefs_page->user_entry[i]), 0, -1);
220
221                 pass = gtk_editable_get_chars(GTK_EDITABLE(prefs_page->pass_entry[i]), 0, -1);
222                 if (strlen(pass) > 0) {
223                         spamreport_passwd_set(spam_interfaces[i].name, pass);
224                         memset(pass, 0, strlen(pass));
225                 }
226                 g_free(pass);
227         }
228
229         pref_file = prefs_write_open(rc_file_path);
230         g_free(rc_file_path);
231         
232         if (!(pref_file) ||
233             (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
234           return;
235         
236         if (prefs_write_param(param, pref_file->fp) < 0) {
237           g_warning("failed to write SpamReport Plugin configuration");
238           prefs_file_close_revert(pref_file);
239           return;
240         }
241         if (fprintf(pref_file->fp, "\n") < 0) {
242                 FILE_OP_ERROR(rc_file_path, "fprintf");
243                 prefs_file_close_revert(pref_file);
244         } else
245                 prefs_file_close(pref_file);
246 }