Fix CID 1491401 and 1491402: (possible) modulo by zero.
[claws.git] / src / plugins / newmail / newmail.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2005-2015 H.Merijn Brand 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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <errno.h>
20
21 #include <glib.h>
22 #include <glib/gi18n.h>
23
24 #include "version.h"
25 #include "claws.h"
26 #include "plugin.h"
27 #include "utils.h"
28 #include "hooks.h"
29 #include "procmsg.h"
30
31 #include <inttypes.h>
32
33 #include "file-utils.h"
34
35 #define LOG_NAME        "NewLog"
36 #define DEFAULT_DIR     "Mail"
37 #define BUFSIZE         2048
38
39 static gulong hook_id = HOOK_NONE;
40
41 static FILE *NewLog   = NULL;
42 static char *LogName  = NULL;
43 static int   truncLog = 1;
44 static char *pluginDesc = NULL;
45
46 static gchar *defstr (gchar *s)
47 {
48         return s ? s : "(null)";
49 } /* defstr */
50
51 gboolean newmail_hook (gpointer source, gpointer data)
52 {
53         auto MsgInfo    *msginfo = (MsgInfo *)source;
54         auto FolderItem *tof;
55
56         if (!msginfo) return FALSE;
57         if (!NewLog) return FALSE;
58
59         tof = msginfo->folder;
60         (void)fprintf (NewLog, "---\n"
61                 "Date:\t%s\n"
62                 "Subject:\t%s\n"
63                 "From:\t%s\n"
64                 "To:\t%s\n"
65                 "Cc:\t%s\n"
66                 "Size:\t%jd\n"
67                 "Path:\t%s\n"
68                 "Message:\t%d\n"
69                 "Folder:\t%s\n",
70                 defstr (msginfo->date),
71                 defstr (msginfo->subject),
72                 defstr (msginfo->from),
73                 defstr (msginfo->to),
74                 defstr (msginfo->cc),
75                 (intmax_t) msginfo->size,
76                 defstr (procmsg_get_message_file_path (msginfo)),
77                 msginfo->msgnum,
78                 tof ? defstr (tof->name) : "(null)");
79
80         return (FALSE);
81 } /* newmail_hook */
82
83 gboolean plugin_done (void)
84 {
85         if (NewLog) {
86                 (void)claws_fclose (NewLog);
87                 NewLog  = NULL;
88         }
89         if (LogName) {
90                 g_free(LogName);
91                 LogName = NULL;
92         }
93         if (pluginDesc) {
94                 g_free(pluginDesc);
95                 pluginDesc = NULL;
96         }
97         hooks_unregister_hook (MAIL_POSTFILTERING_HOOKLIST, hook_id);
98
99         debug_print ("Newmail plugin unloaded\n");
100         return TRUE;
101 } /* plugin_done */
102
103 gint plugin_init (gchar **error)
104 {
105         if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
106                                 VERSION_NUMERIC, _("NewMail"), error))
107                 return -1;
108
109         hook_id = hooks_register_hook (MAIL_POSTFILTERING_HOOKLIST, newmail_hook, NULL);
110         if (hook_id == HOOK_NONE) {
111                 *error = g_strdup (_("Failed to register newmail hook"));
112                 return (-1);
113         }
114
115         if (!NewLog) {
116                 auto char *mode = truncLog ? "w" : "a";
117                 if (!LogName) {
118                         LogName = g_strconcat(g_getenv ("HOME"), G_DIR_SEPARATOR_S, DEFAULT_DIR,
119                                         G_DIR_SEPARATOR_S, LOG_NAME, NULL);
120                 }
121                 if (!(NewLog = claws_fopen (LogName, mode))) {
122                         debug_print ("Failed to open default log %s\n", LogName);
123                         /* try fallback location */
124                         g_free(LogName);
125                         LogName = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, LOG_NAME, NULL);
126                         if (!(NewLog = claws_fopen (LogName, mode))) {
127                                 debug_print ("Failed to open fallback log %s\n", LogName);
128                                 *error = g_strdup_printf(_("Could not open log file %s: %s\n"),
129                                                 LogName, g_strerror(errno));
130                                 plugin_done ();
131                                 return (-1);
132                         }
133                 }
134                 setbuf (NewLog, NULL);
135         }
136
137         debug_print ("Newmail plugin loaded\n"
138               "Message header summaries written to %s\n", LogName);
139         if (pluginDesc == NULL)
140                 pluginDesc = g_strdup_printf(
141                         _("This plugin writes a header summary to a log file for each "
142                         "mail received after sorting.\n\n"
143                         "Default is ~/Mail/NewLog\n\nCurrent log is %s"), LogName);
144         return (0);
145 } /* plugin_init */
146
147 const gchar *plugin_name (void)
148 {
149     return _("NewMail");
150 } /* plugin_name */
151
152 const gchar *plugin_desc (void)
153 {
154     return pluginDesc;
155 } /* plugin_desc */
156
157 const gchar *plugin_type (void)
158 {
159     return ("Common");
160 } /* plugin_type */
161
162 const gchar *plugin_licence (void)
163 {
164     return ("GPL3+");
165 } /* plugin_licence */
166
167 const gchar *plugin_version (void)
168 {
169     return (VERSION);
170 } /* plugin_version */
171
172 struct PluginFeature *plugin_provides(void)
173 {
174         static struct PluginFeature features[] = 
175                 { {PLUGIN_NOTIFIER, N_("Log file")},
176                   {PLUGIN_NOTHING, NULL}};
177         return features;
178 }