File: newmail.c

package info (click to toggle)
claws-mail-extra-plugins 3.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 47,352 kB
  • sloc: ansic: 100,073; sh: 20,031; makefile: 6,918; perl: 990; yacc: 219; lex: 168
file content (171 lines) | stat: -rw-r--r-- 4,322 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * newmail - A plugin for Claws Mail
 *
 * Copyright (C) 2005-2005 H.Merijn Brand and the Claws Mail Team
 *
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
 * Copyright (C) 1999-2002 Hiroyuki Yamamoto and the Claws Mail Team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <errno.h>

#include <glib.h>
#include <glib/gi18n.h>

#include "version.h"
#include "claws.h"
#include "plugin.h"
#include "utils.h"
#include "hooks.h"
#include "procmsg.h"

#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__netBSD__) || defined(__DragonFlyBSD__)
#include <inttypes.h>
#endif

#include "pluginconfig.h"
#include "plugin.h"

static guint hook_id;

static FILE *NewLog   = NULL;
static char *LogName  = NULL;
static int   truncLog = 1;

static gchar *defstr (gchar *s)
{
    return s ? s : "(null)";
    } /* defstr */

gboolean newmail_hook (gpointer source, gpointer data)
{
    auto MsgInfo    *msginfo = (MsgInfo *)source;
    auto FolderItem *tof;

    if (!msginfo) return (FALSE);

    tof = msginfo->folder;
    (void)fprintf (NewLog, "---\n"
	"Date:\t%s\n"
	"Subject:\t%s\n"
	"From:\t%s\n"
	"To:\t%s\n"
	"Cc:\t%s\n"
	"Size:\t%jd\n"
	"Path:\t%s\n"
	"Message:\t%d\n"
	"Folder:\t%s\n",
	    defstr (msginfo->date),
	    defstr (msginfo->subject),
	    defstr (msginfo->from),
	    defstr (msginfo->to),
	    defstr (msginfo->cc),
	    (intmax_t) msginfo->size,
	    defstr (procmsg_get_message_file_path (msginfo)),
	    msginfo->msgnum,
	    tof ? defstr (tof->name) : "(null)");

    return (FALSE);
    } /* newmail_hook */

gboolean plugin_done (void)
{
    if (NewLog) {
	(void)fclose (NewLog);
	NewLog  = NULL;
	LogName = NULL;
	}
    hooks_unregister_hook (MAIL_POSTFILTERING_HOOKLIST, hook_id);

    printf (_("Newmail plugin unloaded\n"));
    return TRUE;
    } /* plugin_done */

gint plugin_init (gchar **error)
{
	if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
				VERSION_NUMERIC, _("NewMail"), error))
		return -1;

	hook_id = hooks_register_hook (MAIL_POSTFILTERING_HOOKLIST, newmail_hook, NULL);
  if (hook_id == -1) {
		*error = g_strdup (_("Failed to register newmail hook"));
		return (-1);
	}

    if (!NewLog) {
	auto char *mode = truncLog ? "w" : "a";
	if (!LogName) {
	    auto size_t l;
	    auto char   name[260];
	    (void)snprintf (name, 256, "%s/Mail/NewLog", getenv ("HOME"));
	    l = strlen (name);
	    if (l > 255 || !(LogName = (char *)malloc (l + 1))) {
		*error = g_strdup (_("Cannot load plugin NewMail\n"
				     "$HOME is too long\n"));
		plugin_done ();
		return (-1);
		}
	    (void)strcpy (LogName, name);
	    }
	if (!(NewLog = fopen (LogName, mode))) {
	    *error = g_strdup (sys_errlist[errno]);
	    plugin_done ();
	    return (-1);
	    }
	setbuf (NewLog, NULL);
	}

    printf (_("Newmail plugin loaded\n"
              "Message header summaries written to %s\n"), LogName);
    return (0);
    } /* plugin_init */

const gchar *plugin_name (void)
{
    return _("NewMail");
    } /* plugin_name */

const gchar *plugin_desc (void)
{
    return _("This Plugin writes a header summary to a log file for each "
	     "mail received after sorting.\n\n"
	     "Default is ~/Mail/NewLog");
    } /* plugin_desc */

const gchar *plugin_type (void)
{
    return ("Common");
    } /* plugin_type */

const gchar *plugin_licence (void)
{
    return ("GPL3+");
    } /* plugin_licence */

const gchar *plugin_version (void)
{
    return (PLUGINVERSION);
    } /* plugin_version */

struct PluginFeature *plugin_provides(void)
{
	static struct PluginFeature features[] = 
		{ {PLUGIN_NOTIFIER, N_("Log file")},
		  {PLUGIN_NOTHING, NULL}};
	return features;
}