Statistics
| Branch: | Tag: | Revision:

root / lib / filter-manager.c @ 4012ec30

History | View | Annotate | Download (2.8 KB)

1

    
2
#include <glib.h>
3

    
4
#include "filter.h"
5
#include "filter-manager.h"
6
#include "filter-private.h"
7

    
8

    
9
XFilterManager *xfilter_manager_new(void)
10
{
11
        XFilterManager *mgr;
12

    
13
        mgr = g_new0(XFilterManager, 1);
14
        return mgr;
15
}
16

    
17
void xfilter_manager_free(XFilterManager *mgr)
18
{
19
        XFilter *list;
20
        XFilter *next;
21

    
22
        if (!mgr)
23
                return;
24

    
25
        for (list = mgr->filter_list; list != NULL; list = next) {
26
                next = list->next;
27
                xfilter_free(list);
28
        }
29
        g_free(mgr->original_encoding);
30
        g_free(mgr->sender);
31
        g_free(mgr->recipient);
32
        g_free(mgr->subject);
33

    
34
        g_free(mgr);
35
}
36

    
37
void xfilter_manager_filter_add(XFilterManager *mgr, XFilter *filter)
38
{
39
        XFilter *list;
40

    
41
        g_return_if_fail(mgr != NULL);
42
        g_return_if_fail(filter != NULL);
43

    
44
        list = mgr->filter_list;
45
        if (list) {
46
                while (list->next) {
47
                        list = list->next;
48
                }
49
                list->next = filter;
50
        } else {
51
                mgr->filter_list = filter;
52
        }
53

    
54
        filter->manager = mgr;
55
}
56

    
57
void xfilter_manager_filter_remove(XFilterManager *mgr, XFilter *filter)
58
{
59
        XFilter *list;
60
        XFilter *prev = NULL;
61

    
62
        g_return_if_fail(mgr != NULL);
63
        g_return_if_fail(filter != NULL);
64

    
65
        for (list = mgr->filter_list; list != NULL; list = list->next) {
66
                if (list == filter) {
67
                        if (prev)
68
                                prev->next = list->next;
69
                        list->next = NULL;
70
                        filter->manager = NULL;
71
                        break;
72
                }
73
                prev = list;
74
        }
75
}
76

    
77
void xfilter_manager_done(XFilterManager *mgr)
78
{
79
        g_return_if_fail(mgr != NULL);
80

    
81
        g_free(mgr->original_encoding);
82
        mgr->original_encoding = NULL;
83
        g_free(mgr->sender);
84
        mgr->sender = NULL;
85
        g_free(mgr->recipient);
86
        mgr->recipient = NULL;
87
        g_free(mgr->subject);
88
        mgr->subject = NULL;
89
}
90

    
91
XFilterResult *xfilter_manager_run(XFilterManager *mgr, XMessageData *msgdata)
92
{
93
        XFilter *list;
94
        XMessageData *retdata;
95
        const XMessageData *curdata;
96
        XFilterResult *res;
97
        XFilterStatus status;
98

    
99
        g_return_val_if_fail(mgr != NULL, NULL);
100

    
101
        xfilter_debug_print("%s: %s: run filter chain\n", __FILE__, G_STRFUNC);
102

    
103
        res = xfilter_result_new();
104

    
105
        curdata = msgdata;
106

    
107
        for (list = mgr->filter_list; list != NULL; list = list->next) {
108
                retdata = NULL;
109
                xfilter_debug_print("exec filter: %s [%s] in data type: %s\n", xfilter_get_name(list), xfilter_get_type(list) == XF_CONTENT ? "content filter" : "test filter", xfilter_message_data_get_mime_type(curdata));
110
                status = xfilter_exec(list, curdata, &retdata);
111
                res->status = status;
112
                if (retdata) {
113
                        xfilter_message_data_free(res->msgdata);
114
                        res->msgdata = retdata;
115
                        curdata = retdata;
116
                }
117
                xfilter_debug_print("exec filter: %s: status %d: out data type: %s\n", xfilter_get_name(list), status, xfilter_message_data_get_mime_type(curdata));
118
                if (status == XF_JUNK) {
119
                        xfilter_debug_print("filter returned XF_JUNK, end filter chain\n");
120
                        break;
121
                }
122
                if (status == XF_UNSUPPORTED_TYPE || status == XF_ERROR) {
123
                        xfilter_debug_print("filter returned error, end filter chain\n");
124
                        break;
125
                }
126
        }
127

    
128
        xfilter_manager_done(mgr);
129

    
130
        return res;
131
}