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 gulong mail_receive_hook_id = HOOK_NONE;
48 
49 static FetchinfoConfig config;
50 
51 static PrefParam param[] = {
52 	{"fetchinfo_enable",  "FALSE", &config.fetchinfo_enable, P_BOOL, NULL, NULL, NULL},
53 	{"fetchinfo_uidl",    "TRUE", &config.fetchinfo_uidl, P_BOOL, NULL, NULL, NULL},
54 	{"fetchinfo_account", "TRUE", &config.fetchinfo_account, P_BOOL, NULL, NULL, NULL},
55 	{"fetchinfo_server",  "TRUE", &config.fetchinfo_server, P_BOOL,	NULL, NULL, NULL},
56 	{"fetchinfo_userid",  "TRUE", &config.fetchinfo_userid, P_BOOL,	NULL, NULL, NULL},
57 	{"fetchinfo_time",    "TRUE", &config.fetchinfo_time, P_BOOL, NULL, NULL, NULL},
58 
59 	{NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
60 };
61 
fetchinfo_add_header(gchar ** data,const gchar * header,const gchar * value)62 gchar *fetchinfo_add_header(gchar **data, const gchar *header,const gchar *value)
63 {
64 	gchar *line;
65 	gchar *qpline;
66 	gchar *newdata;
67 
68 	line = g_strdup_printf("%s: %s", header, value);
69 	qpline = g_malloc(strlen(line)*4);
70 	qp_encode_line(qpline, line);
71 	newdata = g_strconcat(*data, qpline, NULL);
72 	g_free(line);
73 	g_free(qpline);
74 	g_free(*data);
75 	*data = newdata;
76 	return newdata;
77 }
78 
mail_receive_hook(gpointer source,gpointer data)79 static gboolean mail_receive_hook(gpointer source, gpointer data)
80 {
81 	MailReceiveData *mail_receive_data = (MailReceiveData *) source;
82 	Pop3Session *session;
83 	gchar *newheaders;
84 	gchar *newdata;
85 	gchar date[RFC822_DATE_BUFFSIZE];
86 
87 	if (!config.fetchinfo_enable) {
88 		return FALSE;
89 	}
90 
91 	g_return_val_if_fail(
92 			      mail_receive_data
93 			      && mail_receive_data->session
94 			      && mail_receive_data->data,
95 			      FALSE );
96 
97 	session = mail_receive_data->session;
98 	get_rfc822_date(date, sizeof(date));
99 	newheaders = g_strdup("");
100 
101 	if (config.fetchinfo_uidl)
102 		fetchinfo_add_header(&newheaders, "X-FETCH-UIDL",
103 			session->msg[session->cur_msg].uidl);
104 	if (config.fetchinfo_account)
105 		fetchinfo_add_header(&newheaders, "X-FETCH-ACCOUNT",
106 			session->ac_prefs->account_name);
107 	if (config.fetchinfo_server)
108 		fetchinfo_add_header(&newheaders, "X-FETCH-SERVER",
109 			session->ac_prefs->recv_server);
110 	if (config.fetchinfo_userid)
111 		fetchinfo_add_header(&newheaders, "X-FETCH-USERID",
112 			session->ac_prefs->userid);
113 	if (config.fetchinfo_time)
114 		fetchinfo_add_header(&newheaders, "X-FETCH-TIME",
115 			date);
116 
117 	newdata = g_strconcat(newheaders, mail_receive_data->data, NULL);
118 	g_free(newheaders);
119 	g_free(mail_receive_data->data);
120 	mail_receive_data->data = newdata;
121 	mail_receive_data->data_len = strlen(newdata);
122 	return FALSE;
123 }
124 
fetchinfo_get_config(void)125 FetchinfoConfig *fetchinfo_get_config(void)
126 {
127 	return &config;
128 }
129 
fetchinfo_save_config(void)130 void fetchinfo_save_config(void)
131 {
132 	PrefFile *pfile;
133 	gchar *rcpath;
134 
135 	debug_print("Saving Fetchinfo Page\n");
136 
137 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
138 	pfile = prefs_write_open(rcpath);
139 	g_free(rcpath);
140 	if (!pfile || (prefs_set_block_label(pfile, "Fetchinfo") < 0))
141 		return;
142 
143 	if (prefs_write_param(param, pfile->fp) < 0) {
144 		/* i18n: Possible error message during plugin load */
145 		g_warning("failed to write Fetchinfo configuration to file");
146 		prefs_file_close_revert(pfile);
147 		return;
148 	}
149         if (fprintf(pfile->fp, "\n") < 0) {
150 		FILE_OP_ERROR(rcpath, "fprintf");
151 		prefs_file_close_revert(pfile);
152 	} else
153 	        prefs_file_close(pfile);
154 }
155 
plugin_init(gchar ** error)156 gint plugin_init(gchar **error)
157 {
158 	gchar *rcpath;
159 
160 	if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
161 				VERSION_NUMERIC, _("Fetchinfo"), error))
162 		return -1;
163 
164 	mail_receive_hook_id = hooks_register_hook(MAIL_RECEIVE_HOOKLIST, mail_receive_hook, NULL);
165 	if (mail_receive_hook_id == HOOK_NONE) {
166 		/* i18n: Possible error message during plugin load */
167 		*error = g_strdup(_("Failed to register mail receive hook"));
168 		return -1;
169 	}
170 
171 	prefs_set_default(param);
172 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
173 	prefs_read_config(param, "Fetchinfo", rcpath, NULL);
174 	g_free(rcpath);
175 
176 	fetchinfo_gtk_init();
177 
178 	debug_print("Fetchinfo plugin loaded\n");
179 
180 	return 0;
181 }
182 
plugin_done(void)183 gboolean plugin_done(void)
184 {
185 	hooks_unregister_hook(MAIL_RECEIVE_HOOKLIST, mail_receive_hook_id);
186 	fetchinfo_gtk_done();
187 
188 	debug_print("Fetchinfo plugin unloaded\n");
189 	return TRUE;
190 }
191 
plugin_name(void)192 const gchar *plugin_name(void)
193 {
194 	return _("Fetchinfo");
195 }
196 
plugin_desc(void)197 const gchar *plugin_desc(void)
198 {
199 	/* i18n: Description seen in plugins dialog.
200 	 * Translation of "Plugins" part of preferences path should to be
201 	 * the same as translation of "Plugins" string in Claws Mail message
202 	 * catalog. */
203 	return _("This plugin modifies the downloaded messages. "
204 	         "It inserts headers containing some download "
205 		 "information: UIDL, Claws Mail account name, "
206 		 "POP server, user ID and retrieval time.\n"
207 	     "\n"
208 	     "Options can be found in /Configuration/Preferences/Plugins/Fetchinfo");
209 }
210 
plugin_type(void)211 const gchar *plugin_type(void)
212 {
213 	return "GTK2";
214 }
215 
plugin_licence(void)216 const gchar *plugin_licence(void)
217 {
218 		return "GPL3+";
219 }
220 
plugin_version(void)221 const gchar *plugin_version(void)
222 {
223 	return VERSION;
224 }
225 
plugin_provides(void)226 struct PluginFeature *plugin_provides(void)
227 {
228 	static struct PluginFeature features[] =
229 		/* i18n: Description of functionality added by this plugin */
230 		{ {PLUGIN_UTILITY, N_("Mail marking")},
231 		  {PLUGIN_NOTHING, NULL}};
232 	return features;
233 }
234