Remove inclusion of sys/socket.h from nntp-thread.c
[claws.git] / src / plugins / newmail / newmail.c
blobd50a42a98e2844893f6970deed929da0c81242ad
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
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.
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.
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/>.
19 #include "config.h"
21 #include <errno.h>
23 #include <glib.h>
24 #include <glib/gi18n.h>
26 #include "version.h"
27 #include "claws.h"
28 #include "plugin.h"
29 #include "utils.h"
30 #include "hooks.h"
31 #include "procmsg.h"
33 #include <inttypes.h>
35 #include "file-utils.h"
37 #define LOG_NAME "NewLog"
38 #define DEFAULT_DIR "Mail"
40 static gulong hook_id = HOOK_NONE;
42 static FILE *NewLog = NULL;
43 static char *LogName = NULL;
44 static int truncLog = 1;
45 static char *pluginDesc = NULL;
47 static gchar *defstr (gchar *s)
49 return s ? s : "(null)";
50 } /* defstr */
52 gboolean newmail_hook (gpointer source, gpointer data)
54 auto MsgInfo *msginfo = (MsgInfo *)source;
55 auto FolderItem *tof;
57 if (!msginfo) return FALSE;
58 if (!NewLog) return FALSE;
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)");
81 return (FALSE);
82 } /* newmail_hook */
84 gboolean plugin_done (void)
86 if (NewLog) {
87 (void)claws_fclose (NewLog);
88 NewLog = NULL;
90 if (LogName) {
91 g_free(LogName);
92 LogName = NULL;
94 if (pluginDesc) {
95 g_free(pluginDesc);
96 pluginDesc = NULL;
98 hooks_unregister_hook (MAIL_POSTFILTERING_HOOKLIST, hook_id);
100 debug_print ("Newmail plugin unloaded\n");
101 return TRUE;
102 } /* plugin_done */
104 gint plugin_init (gchar **error)
106 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
107 VERSION_NUMERIC, _("NewMail"), error))
108 return -1;
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);
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);
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);
135 setbuf (NewLog, NULL);
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 */
148 const gchar *plugin_name (void)
150 return _("NewMail");
151 } /* plugin_name */
153 const gchar *plugin_desc (void)
155 return pluginDesc;
156 } /* plugin_desc */
158 const gchar *plugin_type (void)
160 return ("Common");
161 } /* plugin_type */
163 const gchar *plugin_licence (void)
165 return ("GPL3+");
166 } /* plugin_licence */
168 const gchar *plugin_version (void)
170 return (VERSION);
171 } /* plugin_version */
173 struct PluginFeature *plugin_provides(void)
175 static struct PluginFeature features[] =
176 { {PLUGIN_NOTIFIER, N_("Log file")},
177 {PLUGIN_NOTHING, NULL}};
178 return features;