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