aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-junk-hook.c
blob: 0b63334d41a1391190afb76917d6ef892447c90e (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Vivek Jain <jvivek@novell.com>
 *
 *  Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 *  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 2 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include "em-junk-hook.h"
#include "mail-session.h"
#include "e-util/e-error.h"
#include "em-utils.h"
#include <camel/camel-junk-plugin.h>
#include <glib/gi18n.h>

static GHashTable *emjh_types;
static GObjectClass *parent_class = NULL;

static void *emjh_parent_class;
static GObjectClass *emj_parent;
#define emjh ((EMJunkHook *)eph)

#define d(x)

static const EPluginHookTargetKey emjh_flag_map[] = {
    { NULL }
};

/* ********************************************************************** */

/* Mail junk plugin */

/*
  <hook class="org.gnome.evolution.mail.junk:1.0">
  <group id="EMJunk">
     <item check_junk="sa_check_junk"
           report_junk="sa_report_junk"
       report_non_junk="sa_report_non_junk"
       commit_reports = "sa_commit_reports"/>
  </group>
  </hook>

*/

static void manage_error (const char *msg, GError *error);

GQuark
em_junk_error_quark (void)
{
    return g_quark_from_static_string ("em-junk-error-quark");
}

static const char *
em_junk_get_name (CamelJunkPlugin *csp);

static void
em_junk_init(CamelJunkPlugin *csp)
{
    struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

    ((EPluginClass *)G_OBJECT_GET_CLASS(item->hook->hook.plugin))->enable(item->hook->hook.plugin, 1);
}

static const char *
em_junk_get_name (CamelJunkPlugin *csp)
{
    struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

    if (item->hook && item->hook->hook.plugin->enabled) {
        return (item->hook->hook.plugin->name);
    } else
        return  _("None");

}

static gboolean
em_junk_check_junk(CamelJunkPlugin *csp, CamelMimeMessage *m)
{
    struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

    if (item->hook && item->hook->hook.plugin->enabled) {
        gboolean res;
        EMJunkHookTarget target = {
            m,
            NULL
        };

        res = e_plugin_invoke(item->hook->hook.plugin, item->check_junk, &target) != NULL;

        manage_error ("mail:junk-check-error", target.error);

        return res;
    }

    return FALSE;
}

static void
em_junk_report_junk(CamelJunkPlugin *csp, CamelMimeMessage *m)
{
    struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

    if (item->hook && item->hook->hook.plugin->enabled) {
        EMJunkHookTarget target = {
            m,
            NULL
        };

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

        manage_error ("mail:junk-report-error", target.error);
    }
}

static void
em_junk_report_non_junk(CamelJunkPlugin *csp, CamelMimeMessage *m)
{
    struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

    if (item->hook && item->hook->hook.plugin->enabled) {
        EMJunkHookTarget target = {
            m,
            NULL
        };
        e_plugin_invoke(item->hook->hook.plugin, item->report_non_junk, &target);
        manage_error ("mail:junk-not-report-error", target.error);
    }
}

static void
em_junk_commit_reports(CamelJunkPlugin *csp)
{
    struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

    if (item->hook && item->hook->hook.plugin->enabled)
        e_plugin_invoke(item->hook->hook.plugin, item->commit_reports, NULL);

}

static void
emj_dispose (GObject *object)
{
    if (parent_class->dispose)
        parent_class->dispose (object);
}

static void
emj_finalize (GObject *object)
{
    if (parent_class->finalize)
        parent_class->finalize (object);
}

static void
emjh_free_item(EMJunkHookItem *item)
{
    g_free (item->check_junk);
    g_free (item->report_junk);
    g_free (item->report_non_junk);
    g_free (item->commit_reports);
    g_free(item);
}

static void
emjh_free_group(EMJunkHookGroup *group)
{
    g_slist_foreach(group->items, (GFunc)emjh_free_item, NULL);
    g_slist_free(group->items);

    g_free(group->id);
    g_free(group);
}

static struct _EMJunkHookItem *
emjh_construct_item(EPluginHook *eph, EMJunkHookGroup *group, xmlNodePtr root)
{
    struct _EMJunkHookItem *item;
    CamelJunkPlugin junk_plugin = {
        em_junk_get_name,
        1,
        em_junk_check_junk,
        em_junk_report_junk,
        em_junk_report_non_junk,
        em_junk_commit_reports,
        em_junk_init,
    };

    d(printf("  loading group item\n"));
    item = g_malloc0(sizeof(*item));
    item->csp =  junk_plugin;
    item->check_junk = e_plugin_xml_prop(root, "check_junk");
    item->report_junk = e_plugin_xml_prop(root, "report_junk");
    item->report_non_junk = e_plugin_xml_prop(root, "report_non_junk");
    item->commit_reports = e_plugin_xml_prop(root, "commit_reports");
    item->validate_binary = e_plugin_xml_prop(root, "validate_binary");

    item->plugin_name = e_plugin_xml_prop(root, "name");
    item->hook = emjh;

    if (item->check_junk == NULL || item->report_junk == NULL || item->report_non_junk == NULL || item->commit_reports == NULL)
        goto error;

    /* Add the plugin to the session plugin list*/
    mail_session_add_junk_plugin (item->plugin_name, CAMEL_JUNK_PLUGIN (&(item->csp)));

    return item;
error:
    printf ("ERROR");
    emjh_free_item (item);
    return NULL;
}

static struct _EMJunkHookGroup *
emjh_construct_group(EPluginHook *eph, xmlNodePtr root)
{
    struct _EMJunkHookGroup *group;
    xmlNodePtr node;

    d(printf(" loading group\n"));
    group = g_malloc0(sizeof(*group));

    group->id = e_plugin_xml_prop(root, "id");
    if (group->id == NULL)
        goto error;

    node = root->children;

    /* We'll processs only  the first item from xml file*/
    while (node) {
        if (0 == strcmp((char *)node->name, "item")) {
            struct _EMJunkHookItem *item;

            item = emjh_construct_item(eph, group, node);
            if (item)
                group->items = g_slist_append(group->items, item);
            break;
        }

        node = node->next;
    }

    return group;
error:
    emjh_free_group(group);

    return NULL;
}

static int
emjh_construct(EPluginHook *eph, EPlugin *ep, xmlNodePtr root)
{
    xmlNodePtr node;

    d(printf("loading junk hook\n"));

    if (((EPluginHookClass *)emjh_parent_class)->construct(eph, ep, root) == -1)
        return -1;

    if (!ep->enabled) {
        g_warning ("ignored this junk plugin: not enabled");
        return -1;
    }

    node = root->children;
    while (node) {
        if (strcmp((char *)node->name, "group") == 0) {
            struct _EMJunkHookGroup *group;

            group = emjh_construct_group(eph, node);
            if (group) {
                emjh->groups = g_slist_append(emjh->groups, group);
            }
        }
        node = node->next;
    }

    eph->plugin = ep;

    return 0;
}

static void
manage_error (const char *msg, GError *error)
{
    GtkWidget *w;

    if (!error)
        return;

    w = e_error_new (NULL, msg, error->message, NULL);
    em_utils_show_error_silent (w);

    g_error_free (error);
}

/*XXX: don't think we need here*/
static void
emjh_enable(EPluginHook *eph, int state)
{
    GSList *g;

    g = emjh->groups;
    if (emjh_types == NULL)
        return;

}

static void
emjh_finalise(GObject *o)
{
    EPluginHook *eph = (EPluginHook *)o;

    g_slist_foreach(emjh->groups, (GFunc)emjh_free_group, NULL);
    g_slist_free(emjh->groups);

    ((GObjectClass *)emjh_parent_class)->finalize(o);
}

static void
emjh_class_init(EPluginHookClass *klass)
{
    ((GObjectClass *)klass)->finalize = emjh_finalise;
    klass->construct = emjh_construct;
    klass->enable = emjh_enable;
    klass->id = "org.gnome.evolution.mail.junk:1.0";
}

GType
em_junk_hook_get_type(void)
{
    static GType type = 0;

    if (!type) {
        static const GTypeInfo info = {
            sizeof(EMJunkHookClass), NULL, NULL, (GClassInitFunc) emjh_class_init, NULL, NULL,
            sizeof(EMJunkHook), 0, (GInstanceInitFunc) NULL,
        };

        emjh_parent_class = g_type_class_ref(e_plugin_hook_get_type());
        type = g_type_register_static(e_plugin_hook_get_type(), "EMJunkHook", &info, 0);
    }

    return type;
}

static void
emj_class_init (EMJunkClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    parent_class = g_type_class_peek_parent (klass);
    object_class->dispose = emj_dispose;
    object_class->finalize = emj_finalize;
}

GType
emj_get_type(void)
{
    static GType type = 0;

    if (!type) {
        static const GTypeInfo info = {
            sizeof(EMJunkClass), NULL, NULL, (GClassInitFunc) emj_class_init, NULL, NULL,
            sizeof(EMJunk), 0, (GInstanceInitFunc) NULL,
        };

        emj_parent = g_type_class_ref(G_TYPE_OBJECT);
        type = g_type_register_static(G_TYPE_OBJECT, "EMJunk", &info, 0);
    }

    return type;
}

void
em_junk_hook_register_type(GType type)
{
    EMJunk *klass;

    if (emjh_types == NULL)
        emjh_types = g_hash_table_new(g_str_hash, g_str_equal);

    d(printf("registering junk plugin type '%s'\n", g_type_name(type)));

    klass = g_type_class_ref(type);
    g_hash_table_insert(emjh_types, (void *)g_type_name(type), klass);
}