aboutsummaryrefslogtreecommitdiffstats
path: root/modules/mail/e-mail-junk-hook.c
blob: 24c1ac77fca319c617f43435122b2f2a2abe372a (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * e-mail-junk-hook.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include "e-mail-junk-hook.h"

#include <glib/gi18n.h>

#include "e-util/e-alert-dialog.h"
#include "shell/e-shell.h"

#include "mail/em-junk.h"
#include "mail/em-utils.h"
#include "mail/e-mail-backend.h"
#include "mail/e-mail-session.h"

#define E_MAIL_JUNK_HOOK_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_JUNK_HOOK, EMailJunkHookPrivate))

struct _EMailJunkHookPrivate {
    EMJunkInterface interface;
};

struct ErrorData {
    const gchar *error_message;
    GError *error;
};

static gpointer parent_class;
static GType mail_junk_hook_type;

static gboolean
mail_junk_hook_idle_cb (struct ErrorData *data)
{
    EShell *shell;
    EShellBackend *shell_backend;

    shell = e_shell_get_default ();
    shell_backend = e_shell_get_backend_by_name (shell, "mail");

    e_mail_backend_submit_alert (
        E_MAIL_BACKEND (shell_backend),
        data->error_message, data->error->message, NULL);

    g_error_free (data->error);
    g_slice_free (struct ErrorData, data);

    return FALSE;
}

static void
mail_junk_hook_error (const gchar *error_message,
                      GError *error)
{
    struct ErrorData *data;

    g_return_if_fail (error != NULL);

    data = g_slice_new (struct ErrorData);
    data->error_message = error_message;
    data->error = error;

    g_idle_add ((GSourceFunc) mail_junk_hook_idle_cb, data);
}

static const gchar *
mail_junk_hook_get_name (CamelJunkPlugin *junk_plugin)
{
    EMJunkInterface *interface;

    interface = (EMJunkInterface *) junk_plugin;

    if (!interface->hook->plugin->enabled) {
        /* Translators: "None" for a junk hook name,
         * when the junk plugin is not enabled. */
        return C_("mail-junk-hook", "None");
    }

    return interface->hook->plugin->name;
}

static void
mail_junk_hook_plugin_init (CamelJunkPlugin *junk_plugin)
{
    EMJunkInterface *interface;
    EPluginClass *class;

    interface = (EMJunkInterface *) junk_plugin;

    class = E_PLUGIN_GET_CLASS (interface->hook->plugin);
    g_return_if_fail (class->enable != NULL);

    class->enable (interface->hook->plugin, 1);
}

static gboolean
mail_junk_hook_check_junk (CamelJunkPlugin *junk_plugin,
                           CamelMimeMessage *mime_message)
{
    EMJunkTarget target = { mime_message, NULL };
    EMJunkInterface *interface;
    gpointer result;

    interface = (EMJunkInterface *) junk_plugin;

    if (!interface->hook->plugin->enabled)
        return FALSE;

    result = e_plugin_invoke (
        interface->hook->plugin,
        interface->check_junk, &target);

    if (target.error != NULL)
        mail_junk_hook_error ("mail:junk-check-error", target.error);

    return (result != NULL);
}

static void
mail_junk_hook_report_junk (CamelJunkPlugin *junk_plugin,
                            CamelMimeMessage *mime_message)
{
    EMJunkTarget target = { mime_message, NULL };
    EMJunkInterface *interface;

    interface = (EMJunkInterface *) junk_plugin;

    if (!interface->hook->plugin->enabled)
        return;

    e_plugin_invoke (
        interface->hook->plugin,
        interface->report_junk, &target);

    if (target.error != NULL)
        mail_junk_hook_error ("mail:junk-report-error", target.error);
}

static void
mail_junk_hook_report_notjunk (CamelJunkPlugin *junk_plugin,
                               CamelMimeMessage *mime_message)
{
    EMJunkTarget target = { mime_message, NULL };
    EMJunkInterface *interface;

    interface = (EMJunkInterface *) junk_plugin;

    if (!interface->hook->plugin->enabled)
        return;

    e_plugin_invoke (
        interface->hook->plugin,
        interface->report_notjunk, &target);

    if (target.error != NULL)
        mail_junk_hook_error (
            "mail:junk-not-report-error", target.error);
}

static void
mail_junk_hook_commit_reports (CamelJunkPlugin *junk_plugin)
{
    EMJunkInterface *interface;

    interface = (EMJunkInterface *) junk_plugin;

    if (!interface->hook->plugin->enabled)
        return;

    e_plugin_invoke (
        interface->hook->plugin,
        interface->commit_reports, NULL);
}

static void
mail_junk_hook_finalize (GObject *object)
{
    EMailJunkHookPrivate *priv;

    priv = E_MAIL_JUNK_HOOK_GET_PRIVATE (object);

    g_free (priv->interface.check_junk);
    g_free (priv->interface.report_junk);
    g_free (priv->interface.report_notjunk);
    g_free (priv->interface.commit_reports);
    g_free (priv->interface.validate_binary);
    g_free (priv->interface.plugin_name);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gint
mail_junk_hook_construct (EPluginHook *hook,
                          EPlugin *plugin,
                          xmlNodePtr node)
{
    EMailJunkHookPrivate *priv;
    EShell *shell;
    EShellBackend *shell_backend;
    EMailBackend *backend;
    EMailSession *session;
    gchar *property;

    priv = E_MAIL_JUNK_HOOK_GET_PRIVATE (hook);

    /* Chain up to parent's construct() method. */
    if (E_PLUGIN_HOOK_CLASS (parent_class)->construct (hook, plugin, node) == -1)
        return -1;

    if (!plugin->enabled)
        return -1;

    node = xmlFirstElementChild (node);

    if (node == NULL)
        return -1;

    if (g_strcmp0 ((gchar *) node->name, "interface") != 0)
        return -1;

    property = e_plugin_xml_prop (node, "check_junk");
    priv->interface.check_junk = property;

    property = e_plugin_xml_prop (node, "report_junk");
    priv->interface.report_junk = property;

    property = e_plugin_xml_prop (node, "report_non_junk");
    priv->interface.report_notjunk = property;

    property = e_plugin_xml_prop (node, "commit_reports");
    priv->interface.commit_reports = property;

    property = e_plugin_xml_prop (node, "validate_binary");
    priv->interface.validate_binary = property;

    property = e_plugin_xml_prop (node, "name");
    priv->interface.plugin_name = property;

    if (priv->interface.check_junk == NULL)
        return -1;

    if (priv->interface.report_junk == NULL)
        return -1;

    if (priv->interface.report_notjunk == NULL)
        return -1;

    if (priv->interface.commit_reports == NULL)
        return -1;

    shell = e_shell_get_default ();
    shell_backend = e_shell_get_backend_by_name (shell, "mail");

    backend = E_MAIL_BACKEND (shell_backend);
    session = e_mail_backend_get_session (backend);

    mail_session_add_junk_plugin (
        session, priv->interface.plugin_name,
        &priv->interface.camel);

    return 0;
}

static void
mail_junk_hook_class_init (EMailJunkHookClass *class)
{
    GObjectClass *object_class;
    EPluginHookClass *plugin_hook_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EMailJunkHookPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = mail_junk_hook_finalize;

    plugin_hook_class = E_PLUGIN_HOOK_CLASS (class);
    plugin_hook_class->construct = mail_junk_hook_construct;
    plugin_hook_class->id = "org.gnome.evolution.mail.junk:1.0";
}

static void
mail_junk_hook_init (EMailJunkHook *mail_junk_hook)
{
    EMJunkInterface *interface;

    mail_junk_hook->priv = E_MAIL_JUNK_HOOK_GET_PRIVATE (mail_junk_hook);

    interface = &mail_junk_hook->priv->interface;
    interface->camel.get_name = mail_junk_hook_get_name;
    interface->camel.api_version = 1;
    interface->camel.check_junk = mail_junk_hook_check_junk;
    interface->camel.report_junk = mail_junk_hook_report_junk;
    interface->camel.report_notjunk = mail_junk_hook_report_notjunk;
    interface->camel.commit_reports = mail_junk_hook_commit_reports;
    interface->camel.init = mail_junk_hook_plugin_init;
    interface->hook = E_PLUGIN_HOOK (mail_junk_hook);
}

GType
e_mail_junk_hook_get_type (void)
{
    return mail_junk_hook_type;
}

void
e_mail_junk_hook_register_type (GTypeModule *type_module)
{
    const GTypeInfo type_info = {
        sizeof (EMailJunkHookClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) mail_junk_hook_class_init,
        (GClassFinalizeFunc) NULL,
        NULL,  /* class_data */
        sizeof (EMailJunkHook),
        0,     /* n_preallocs */
        (GInstanceInitFunc) mail_junk_hook_init,
        NULL   /* value_table */
    };

    mail_junk_hook_type = g_type_module_register_type (
        type_module, E_TYPE_PLUGIN_HOOK,
        "EMailJunkHook", &type_info, 0);
}