57de9222472126c305f89978ebc1c1f3a510402c
[claws.git] / src / plugins / fetchinfo / fetchinfo_plugin.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #  include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 /* common */
31 #include "version.h"
32 #include "claws.h"
33 #include "plugin.h"
34 #include "utils.h"
35 #include "hooks.h"
36 #include "inc.h"
37 #include "prefs.h"
38 #include "prefs_gtk.h"
39 #include "fetchinfo_plugin.h"
40 /* add headers */
41 #include "pop.h"
42 #include "quoted-printable.h"
43 /* parse headers */
44 #include "procheader.h"
45 #include "plugin.h"
46
47 static guint mail_receive_hook_id;
48
49 static FetchinfoConfig config;
50
51 static PrefParam param[] = {
52         {"fetchinfo_enable",    "FALSE", &config.fetchinfo_enable,              
53                                 P_BOOL, NULL, NULL, NULL},
54         {"fetchinfo_uidl",      "TRUE", &config.fetchinfo_uidl,         
55                                 P_BOOL, NULL, NULL, NULL},
56         {"fetchinfo_account",   "TRUE", &config.fetchinfo_account,
57                                 P_BOOL, NULL, NULL, NULL},
58         {"fetchinfo_server",    "TRUE", &config.fetchinfo_server,
59                                 P_BOOL, NULL, NULL, NULL},
60         {"fetchinfo_userid",    "TRUE", &config.fetchinfo_userid,
61                                 P_BOOL, NULL, NULL, NULL},
62         {"fetchinfo_time",      "TRUE", &config.fetchinfo_time,
63                                 P_BOOL, NULL, NULL, NULL},
64
65         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
66 };
67
68 gchar *fetchinfo_add_header(gchar **data, const gchar *header,const gchar *value)
69 {
70         gchar *line;
71         gchar *qpline;
72         gchar *newdata;
73
74         line = g_strdup_printf("%s: %s", header, value);
75         qpline = g_malloc(strlen(line)*4);
76         qp_encode_line(qpline, line);
77         newdata = g_strconcat(*data, qpline, NULL);
78         g_free(line);
79         g_free(qpline);
80         g_free(*data);
81         *data = newdata;
82         return newdata;
83 }
84
85 static gboolean mail_receive_hook(gpointer source, gpointer data)
86 {
87         MailReceiveData *mail_receive_data = (MailReceiveData *) source;
88         Pop3Session *session;
89         gchar *newheaders;
90         gchar *newdata;
91         gchar date[PREFSBUFSIZE];
92         
93         if (!config.fetchinfo_enable) {
94                 return FALSE;
95         }
96
97         g_return_val_if_fail( 
98                               mail_receive_data
99                               && mail_receive_data->session
100                               && mail_receive_data->data,
101                               FALSE );
102
103         session = mail_receive_data->session;
104         get_rfc822_date(date, PREFSBUFSIZE);
105         newheaders = g_strdup("");
106
107         if (config.fetchinfo_uidl)
108                 fetchinfo_add_header(&newheaders, "X-FETCH-UIDL", 
109                         session->msg[session->cur_msg].uidl);
110         if (config.fetchinfo_account)
111                 fetchinfo_add_header(&newheaders, "X-FETCH-ACCOUNT", 
112                         session->ac_prefs->account_name);
113         if (config.fetchinfo_server)
114                 fetchinfo_add_header(&newheaders, "X-FETCH-SERVER", 
115                         session->ac_prefs->recv_server);
116         if (config.fetchinfo_userid)
117                 fetchinfo_add_header(&newheaders, "X-FETCH-USERID", 
118                         session->ac_prefs->userid);
119         if (config.fetchinfo_time)
120                 fetchinfo_add_header(&newheaders, "X-FETCH-TIME", 
121                         date);
122
123         newdata = g_strconcat(newheaders, mail_receive_data->data, NULL);
124         g_free(newheaders);
125         g_free(mail_receive_data->data);
126         mail_receive_data->data = newdata;
127         mail_receive_data->data_len = strlen(newdata);
128         return FALSE;
129 }
130
131 FetchinfoConfig *fetchinfo_get_config(void)
132 {
133         return &config;
134 }
135
136 void fetchinfo_save_config(void)
137 {
138         PrefFile *pfile;
139         gchar *rcpath;
140
141         debug_print("Saving Fetchinfo Page\n");
142
143         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
144         pfile = prefs_write_open(rcpath);
145         g_free(rcpath);
146         if (!pfile || (prefs_set_block_label(pfile, "Fetchinfo") < 0))
147                 return;
148
149         if (prefs_write_param(param, pfile->fp) < 0) {
150                 /* i18n: Possible error message during plugin load */
151                 g_warning(_("failed to write Fetchinfo configuration to file\n"));
152                 prefs_file_close_revert(pfile);
153                 return;
154         }
155         if (fprintf(pfile->fp, "\n") < 0) {
156                 FILE_OP_ERROR(rcpath, "fprintf");
157                 prefs_file_close_revert(pfile);
158         } else
159                 prefs_file_close(pfile);
160 }
161
162 gint plugin_init(gchar **error)
163 {
164         gchar *rcpath;
165
166         if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
167                                 VERSION_NUMERIC, _("Fetchinfo"), error))
168                 return -1;
169
170         mail_receive_hook_id = hooks_register_hook(MAIL_RECEIVE_HOOKLIST, mail_receive_hook, NULL);
171         if (mail_receive_hook_id == (guint)-1) {
172                 /* i18n: Possible error message during plugin load */
173                 *error = g_strdup(_("Failed to register mail receive hook"));
174                 return -1;
175         }
176
177         prefs_set_default(param);
178         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
179         prefs_read_config(param, "Fetchinfo", rcpath, NULL);
180         g_free(rcpath);
181
182         fetchinfo_gtk_init();
183
184         debug_print("Fetchinfo plugin loaded\n");
185
186         return 0;
187 }
188
189 gboolean plugin_done(void)
190 {
191         hooks_unregister_hook(MAIL_RECEIVE_HOOKLIST, mail_receive_hook_id);
192         fetchinfo_gtk_done();
193
194         debug_print("Fetchinfo plugin unloaded\n");
195         return TRUE;
196 }
197
198 const gchar *plugin_name(void)
199 {
200         return _("Fetchinfo");
201 }
202
203 const gchar *plugin_desc(void)
204 {
205         /* i18n: Description seen in plugins dialog.
206          * Translation of "Plugins" part of preferences path should to be
207          * the same as translation of "Plugins" string in Claws Mail message
208          * catalog. */
209         return _("This plugin modifies the downloaded messages. "
210                  "It inserts headers containing some download "
211                  "information: UIDL, Claws Mail account name, "
212                  "POP server, user ID and retrieval time.\n"
213              "\n"
214              "Options can be found in /Configuration/Preferences/Plugins/Fetchinfo");
215 }
216
217 const gchar *plugin_type(void)
218 {
219         return "GTK2";
220 }
221
222 const gchar *plugin_licence(void)
223 {
224                 return "GPL3+";
225 }
226
227 const gchar *plugin_version(void)
228 {
229         return VERSION;
230 }
231
232 struct PluginFeature *plugin_provides(void)
233 {
234         static struct PluginFeature features[] = 
235                 /* i18n: Description of functionality added by this plugin */
236                 { {PLUGIN_UTILITY, N_("Mail marking")},
237                   {PLUGIN_NOTHING, NULL}};
238         return features;
239 }