Statistics
| Branch: | Tag: | Revision:

root / lib / filter.c @ 8d7dcace

History | View | Annotate | Download (6.97 KB)

1
#include <stdio.h>
2
#include <string.h>
3
#include <glib.h>
4

    
5
#include "filter.h"
6
#include "filter-private.h"
7
#include "filter-utils.h"
8

    
9

    
10
static int xfilter_debug_mode = 1;
11

    
12

    
13
int xfilter_init(void)
14
{
15
        return 0;
16
}
17

    
18
void xfilter_done(void)
19
{
20
}
21

    
22
XFilter *xfilter_new(XFilterType type, const char *name)
23
{
24
        XFilter *filter;
25

    
26
        if (type == XF_CONTENT) {
27
                filter = XFILTER(g_new0(XContentFilter, 1));
28
        } else {
29
                filter = XFILTER(g_new0(XTestFilter, 1));
30
        }
31

    
32
        filter->type = type;
33
        filter->name = g_strdup(name);
34

    
35
        return filter;
36
}
37

    
38
void xfilter_free(XFilter *filter)
39
{
40
        if (!filter)
41
                return;
42
        g_free(filter->name);
43
        g_free(filter);
44
}
45

    
46
XFilterType xfilter_get_type(XFilter *filter)
47
{
48
        g_return_val_if_fail(filter != NULL, -1);
49

    
50
        return filter->type;
51
}
52

    
53
const char *xfilter_get_name(XFilter *filter)
54
{
55
        g_return_val_if_fail(filter != NULL, NULL);
56

    
57
        return filter->name;
58
}
59

    
60
void xfilter_set_input_mime_types(XFilter *filter, const char **types)
61
{
62
}
63

    
64
void xfilter_set_output_mime_type(XFilter *filter, const char *type)
65
{
66
}
67

    
68
XMessageData *xfilter_message_data_new(const char *src, const char *mime_type)
69
{
70
        XMessageData *data;
71

    
72
        g_return_val_if_fail(mime_type != NULL, NULL);
73

    
74
        data = g_new0(XMessageData, 1);
75
        data->file = NULL;
76
        data->content = g_strdup(src);
77
        data->mime_type = g_strdup(mime_type);
78
        return data;
79
}
80

    
81
XMessageData *xfilter_message_data_read_file(const char *file, const char *mime_type)
82
{
83
        XMessageData *data;
84

    
85
        g_return_val_if_fail(file != NULL, NULL);
86
        g_return_val_if_fail(mime_type != NULL, NULL);
87

    
88
        data = g_new0(XMessageData, 1);
89
        data->file = g_strdup(file);
90
        data->content = NULL;
91
        data->mime_type = g_strdup(mime_type);
92
        return data;
93
}
94

    
95
void xfilter_message_data_free(XMessageData *msgdata)
96
{
97
        if (!msgdata)
98
                return;
99

    
100
        g_free(msgdata->mime_type);
101
        g_free(msgdata->file);
102
        g_free(msgdata->content);
103
        g_free(msgdata->from);
104
        g_free(msgdata->to);
105
        g_free(msgdata->cc);
106
        g_free(msgdata->subject);
107

    
108
        g_free(msgdata);
109
}
110

    
111
void xfilter_message_data_set_file(XMessageData *msgdata, const char *file)
112
{
113
        xfilter_message_data_set_content(msgdata, NULL);
114
        g_free(msgdata->file);
115
        msgdata->file = g_strdup(file);
116
}
117

    
118
void xfilter_message_data_set_content(XMessageData *msgdata, char *content)
119
{
120
        if (msgdata->content)
121
                g_free(msgdata->content);
122
        msgdata->content = content;
123
}
124

    
125
void xfilter_message_data_set_attribute(XMessageData *msgdata, XMessageAttr attr, const char *value, int append)
126
{
127
        int len;
128

    
129
#define APPEND_STR(d, s)                                \
130
        if (d) {                                        \
131
                len = strlen(d);                        \
132
                d = g_realloc(d, len + strlen(s) + 2);        \
133
                d[len] = '\n';                                \
134
                strcpy(d + len + 1, s);                        \
135
        } else                                                \
136
                d = g_strdup(s);
137

    
138
#define SET_STR(d, s)        { g_free(d); d = g_strdup(s); }
139

    
140
        if (!value)
141
                return;
142

    
143
        switch (attr) {
144
        case XM_FROM:
145
                if (append) {
146
                        APPEND_STR(msgdata->from, value);
147
                } else {
148
                        SET_STR(msgdata->from, value);
149
                }
150
                break;
151
        case XM_TO:
152
                if (append) {
153
                        APPEND_STR(msgdata->to, value);
154
                } else {
155
                        SET_STR(msgdata->to, value);
156
                }
157
                break;
158
        case XM_CC:
159
                if (append) {
160
                        APPEND_STR(msgdata->cc, value);
161
                } else {
162
                        SET_STR(msgdata->cc, value);
163
                }
164
                break;
165
        case XM_SUBJECT:
166
                if (append) {
167
                        APPEND_STR(msgdata->subject, value);
168
                } else {
169
                        SET_STR(msgdata->subject, value);
170
                }
171
                break;
172
        default:
173
                break;
174
        }
175

    
176
#undef APPEND_STR
177
}
178

    
179
void xfilter_message_data_copy_attributes(XMessageData *msgdata, const XMessageData *srcdata)
180
{
181
        g_free(msgdata->from);
182
        msgdata->from = g_strdup(srcdata->from);
183
        g_free(msgdata->to);
184
        msgdata->to = g_strdup(srcdata->to);
185
        g_free(msgdata->cc);
186
        msgdata->cc = g_strdup(srcdata->cc);
187
        g_free(msgdata->subject);
188
        msgdata->subject = g_strdup(srcdata->subject);
189
}
190

    
191
const char *xfilter_message_data_get_attribute(const XMessageData *msgdata, XMessageAttr attr)
192
{
193
        g_return_val_if_fail(msgdata != NULL, NULL);
194

    
195
        switch (attr) {
196
        case XM_FROM:
197
                return msgdata->from;
198
        case XM_TO:
199
                return msgdata->to;
200
        case XM_CC:
201
                return msgdata->cc;
202
        case XM_SUBJECT:
203
                return msgdata->subject;
204
        default:
205
                break;
206
        }
207

    
208
        return NULL;
209
}
210

    
211
const char *xfilter_message_data_get_mime_type(const XMessageData *msgdata)
212
{
213
        g_return_val_if_fail(msgdata != NULL, NULL);
214

    
215
        return msgdata->mime_type;
216
}
217

    
218
const char *xfilter_message_data_get_file(const XMessageData *msgdata)
219
{
220
        g_return_val_if_fail(msgdata != NULL, NULL);
221

    
222
        return msgdata->file;
223
}
224

    
225
const char *xfilter_message_data_get_content(const XMessageData *msgdata)
226
{
227
        g_return_val_if_fail(msgdata != NULL, NULL);
228

    
229
        if (msgdata->content)
230
                return msgdata->content;
231

    
232
        if (msgdata->file) {
233
                char *content;
234

    
235
                g_print("xfilter_message_data_get_content: getting file content: %s\n", msgdata->file);
236
                content = xfilter_utils_get_file_contents(msgdata->file);
237
                if (content)
238
                        ((XMessageData *)msgdata)->content = content;
239

    
240
                return content;
241
        }
242

    
243
        return NULL;
244
}
245

    
246
void xfilter_set_content_filter_func(XContentFilter *filter, XContentFilterFunc func)
247
{
248
        g_return_if_fail(XFILTER(filter)->type == XF_CONTENT);
249

    
250
        filter->content_filter_func = func;
251
}
252

    
253
void xfilter_set_test_filter_func(XTestFilter *filter, XTestFilterFunc func)
254
{
255
        g_return_if_fail(XFILTER(filter)->type == XF_TEST);
256

    
257
        filter->test_filter_func = func;
258
}
259

    
260
XFilterStatus xfilter_exec(XFilter *filter, const XMessageData *msgdata, XFilterResult *result)
261
{
262
        g_return_val_if_fail(filter != NULL, XF_ERROR);
263
        g_return_val_if_fail(msgdata != NULL, XF_ERROR);
264

    
265
        if (filter->type == XF_CONTENT) {
266
                if (!X_CONTENT_FILTER(filter)->content_filter_func)
267
                        return XF_ERROR;
268
                return X_CONTENT_FILTER(filter)->content_filter_func(filter, msgdata, result);
269
        } else {
270
                if (!X_TEST_FILTER(filter)->test_filter_func)
271
                        return XF_ERROR;
272
                return X_TEST_FILTER(filter)->test_filter_func(filter, msgdata, result);
273
        }
274
}
275

    
276
void xfilter_result_print(XFilterResult *result)
277
{
278
        printf("XFilterResult: status = %d, probability = %f\n", result->status, result->probability);
279
}
280

    
281
XFilterResult *xfilter_result_new(void)
282
{
283
        XFilterResult *res;
284

    
285
        res = g_new0(XFilterResult, 1);
286
        res->status = XF_NONE;
287
        res->probability = 0.5;
288
        return res;
289
}
290

    
291
void xfilter_result_set_status(XFilterResult *result, XFilterStatus status)
292
{
293
        result->status = status;
294
}
295

    
296
void xfilter_result_set_message_data(XFilterResult *result, XMessageData *msgdata)
297
{
298
        if (result->msgdata)
299
                xfilter_message_data_free(result->msgdata);
300
        result->msgdata = msgdata;
301
}
302

    
303
void xfilter_result_set_probability(XFilterResult *result, double prob)
304
{
305
        result->probability = prob;
306
}
307

    
308
XFilterStatus xfilter_result_get_status(XFilterResult *result)
309
{
310
        g_return_val_if_fail(result != NULL, XF_ERROR);
311

    
312
        return result->status;
313
}
314

    
315
XMessageData *xfilter_result_get_message_data(XFilterResult *result)
316
{
317
        g_return_val_if_fail(result != NULL, NULL);
318

    
319
        return result->msgdata;
320
}
321

    
322
double xfilter_result_get_probability(XFilterResult *result)
323
{
324
        g_return_val_if_fail(result != NULL, 0.0);
325

    
326
        return result->probability;
327
}
328

    
329
void xfilter_result_free(XFilterResult *result)
330
{
331
        if (!result)
332
                return;
333
        xfilter_message_data_free(result->msgdata);
334
        g_free(result);
335
}
336

    
337
void xfilter_debug_print(const char *format, ...)
338
{
339
        va_list args;
340
        char buf[1024];
341

    
342
        if (!xfilter_debug_mode)
343
                return;
344

    
345
        va_start(args, format);
346
        g_vsnprintf(buf, sizeof(buf), format, args);
347
        va_end(args);
348

    
349
        fputs(buf, stderr);
350
}