2013-02-20 [colin] 3.9.0cvs95
[claws.git] / src / plugins / newmail / newmail.c
1 /*
2  * newmail - A plugin for Claws Mail
3  *
4  * Copyright (C) 2005-2005 H.Merijn Brand and the Claws Mail Team
5  *
6  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
7  * Copyright (C) 1999-2012 the Claws Mail Team
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */
23
24 #include <errno.h>
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28
29 #include "version.h"
30 #include "claws.h"
31 #include "plugin.h"
32 #include "utils.h"
33 #include "hooks.h"
34 #include "procmsg.h"
35
36 #include <inttypes.h>
37
38 #include "plugin.h"
39
40 static guint hook_id;
41
42 static FILE *NewLog   = NULL;
43 static char *LogName  = NULL;
44 static int   truncLog = 1;
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
58     tof = msginfo->folder;
59     (void)fprintf (NewLog, "---\n"
60         "Date:\t%s\n"
61         "Subject:\t%s\n"
62         "From:\t%s\n"
63         "To:\t%s\n"
64         "Cc:\t%s\n"
65         "Size:\t%jd\n"
66         "Path:\t%s\n"
67         "Message:\t%d\n"
68         "Folder:\t%s\n",
69             defstr (msginfo->date),
70             defstr (msginfo->subject),
71             defstr (msginfo->from),
72             defstr (msginfo->to),
73             defstr (msginfo->cc),
74             (intmax_t) msginfo->size,
75             defstr (procmsg_get_message_file_path (msginfo)),
76             msginfo->msgnum,
77             tof ? defstr (tof->name) : "(null)");
78
79     return (FALSE);
80     } /* newmail_hook */
81
82 gboolean plugin_done (void)
83 {
84     if (NewLog) {
85         (void)fclose (NewLog);
86         NewLog  = NULL;
87         LogName = NULL;
88         }
89     hooks_unregister_hook (MAIL_POSTFILTERING_HOOKLIST, hook_id);
90
91     printf (_("Newmail plugin unloaded\n"));
92     return TRUE;
93     } /* plugin_done */
94
95 gint plugin_init (gchar **error)
96 {
97         if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
98                                 VERSION_NUMERIC, _("NewMail"), error))
99                 return -1;
100
101         hook_id = hooks_register_hook (MAIL_POSTFILTERING_HOOKLIST, newmail_hook, NULL);
102   if (hook_id == -1) {
103                 *error = g_strdup (_("Failed to register newmail hook"));
104                 return (-1);
105         }
106
107     if (!NewLog) {
108         auto char *mode = truncLog ? "w" : "a";
109         if (!LogName) {
110             auto size_t l;
111             auto char   name[260];
112             (void)snprintf (name, 256, "%s/Mail/NewLog", getenv ("HOME"));
113             l = strlen (name);
114             if (l > 255 || !(LogName = (char *)malloc (l + 1))) {
115                 *error = g_strdup (_("Cannot load plugin NewMail\n"
116                                      "$HOME is too long\n"));
117                 plugin_done ();
118                 return (-1);
119                 }
120             (void)strcpy (LogName, name);
121             }
122         if (!(NewLog = fopen (LogName, mode))) {
123             *error = g_strdup (sys_errlist[errno]);
124             plugin_done ();
125             return (-1);
126             }
127         setbuf (NewLog, NULL);
128         }
129
130     printf (_("Newmail plugin loaded\n"
131               "Message header summaries written to %s\n"), LogName);
132     return (0);
133     } /* plugin_init */
134
135 const gchar *plugin_name (void)
136 {
137     return _("NewMail");
138     } /* plugin_name */
139
140 const gchar *plugin_desc (void)
141 {
142     return _("This Plugin writes a header summary to a log file for each "
143              "mail received after sorting.\n\n"
144              "Default is ~/Mail/NewLog");
145     } /* plugin_desc */
146
147 const gchar *plugin_type (void)
148 {
149     return ("Common");
150     } /* plugin_type */
151
152 const gchar *plugin_licence (void)
153 {
154     return ("GPL3+");
155     } /* plugin_licence */
156
157 const gchar *plugin_version (void)
158 {
159     return (VERSION);
160     } /* plugin_version */
161
162 struct PluginFeature *plugin_provides(void)
163 {
164         static struct PluginFeature features[] = 
165                 { {PLUGIN_NOTIFIER, N_("Log file")},
166                   {PLUGIN_NOTHING, NULL}};
167         return features;
168 }