Statistics
| Branch: | Tag: | Revision:

root / src / sylfilter-test.c @ aebfd4cc

History | View | Annotate | Download (3.9 KB)

1
/* SylFilter - a message filter
2
 *
3
 * Copyright (C) 2011 Hiroyuki Yamamoto
4
 * Copyright (C) 2011 Sylpheed Development Team
5
 */
6

    
7
#include <stdio.h>
8
#include <string.h>
9
#include <locale.h>
10

    
11
#include "filter.h"
12
#include "filter-manager.h"
13
#include "filter-utils.h"
14
#include "filter-kvs.h"
15

    
16
#define USE_QDBM 1
17
#ifdef USE_QDBM
18
#  include "filter-kvs-qdbm.h"
19
#endif
20

    
21
#include "textcontent-filter.h"
22
#include "blacklist-filter.h"
23
#include "whitelist-filter.h"
24
#include "wordsep-filter.h"
25
#include "ngram-filter.h"
26
#include "bayes-filter.h"
27

    
28
int main(int argc, char *argv[])
29
{
30
        XFilterManager *mgr;
31
        XMessageData *msgdata;
32
        XMessageData *resdata;
33
        XFilterResult *res;
34
        XFilterStatus status;
35
        int retval = 0;
36
        int i;
37
        int is_junk = 1;
38

    
39
        XFilterConstructorFunc learn_junk_ctors[] = {
40
                xfilter_textcontent_new,
41
                xfilter_wordsep_new,
42
                xfilter_ngram_new,
43
                xfilter_bayes_learn_junk_new,
44
                NULL
45
        };
46

    
47
        XFilterConstructorFunc learn_nojunk_ctors[] = {
48
                xfilter_textcontent_new,
49
                xfilter_wordsep_new,
50
                xfilter_ngram_new,
51
                xfilter_bayes_learn_nojunk_new,
52
                NULL
53
        };
54

    
55
        XFilterConstructorFunc ctors[] = {
56
                xfilter_textcontent_new,
57
                xfilter_blacklist_new,
58
                xfilter_whitelist_new,
59
                xfilter_wordsep_new,
60
                xfilter_ngram_new,
61
                xfilter_bayes_new,
62
                NULL
63
        };
64

    
65
        const char *rfc822content =
66
                "From: foo@bar\n"
67
                "To: fuga@hoge\n"
68
                "Subject: test message\n"
69
                "\n"
70
                "This is a test message.\n"
71
                "...\n"
72
                "\n"
73
                "-- \n"
74
                "Foo Bar <foo@bar>\n";
75

    
76
        const char *rfc822_file = NULL;
77

    
78
        setlocale(LC_ALL, "");
79

    
80
        xfilter_init();
81

    
82
#ifdef USE_QDBM
83
        xfilter_kvs_qdbm_set_engine();
84
#endif
85
        if (xfilter_bayes_db_init(NULL) < 0) {
86
                fprintf(stderr, "Database initialization error\n");
87
                return 127;
88
        }
89

    
90
        // set message files
91
        for (i = 1; i < argc; i++) {
92
                if (!strcmp(argv[i], "-j"))
93
                        is_junk = 1;
94
                else if (!strcmp(argv[i], "-c"))
95
                        is_junk = 0;
96
                else
97
                        rfc822_file = argv[i];
98
        }
99

    
100
        // learning
101
        mgr = xfilter_manager_new();
102

    
103
        if (is_junk)
104
                xfilter_manager_add_filters(mgr, learn_junk_ctors);
105
        else
106
                xfilter_manager_add_filters(mgr, learn_nojunk_ctors);
107

    
108
        if (rfc822_file)
109
                msgdata = xfilter_message_data_read_file(rfc822_file, "message/rfc822");
110
        else
111
                msgdata = xfilter_message_data_new(rfc822content, "message/rfc822");
112

    
113
        res = xfilter_manager_run(mgr, msgdata);
114
        xfilter_result_print(res);
115
        status = xfilter_result_get_status(res);
116

    
117
        resdata = xfilter_result_get_message_data(res);
118
        if (resdata) {
119
                const char *content;
120
                content = xfilter_message_data_get_content(resdata);
121
                printf("content: %s\n", content);
122
        }
123

    
124
        xfilter_result_free(res);
125
        xfilter_message_data_free(msgdata);
126
        xfilter_manager_free(mgr);
127

    
128
        printf("--------\n");
129

    
130
        mgr = xfilter_manager_new();
131

    
132
        xfilter_manager_add_filters(mgr, ctors);
133

    
134
        // test 1
135
        msgdata = xfilter_message_data_new("test", "text/plain");
136

    
137
        res = xfilter_manager_run(mgr, msgdata);
138
        xfilter_result_print(res);
139
        status = xfilter_result_get_status(res);
140

    
141
        resdata = xfilter_result_get_message_data(res);
142
        if (resdata) {
143
                const char *content;
144
                content = xfilter_message_data_get_content(resdata);
145
                printf("content: %s\n", content);
146
        }
147

    
148
        xfilter_result_free(res);
149
        xfilter_message_data_free(msgdata);
150

    
151
        printf("--------\n");
152

    
153
        // test 2
154
        if (rfc822_file)
155
                msgdata = xfilter_message_data_read_file(rfc822_file, "message/rfc822");
156
        else
157
                msgdata = xfilter_message_data_new(rfc822content, "message/rfc822");
158

    
159
        res = xfilter_manager_run(mgr, msgdata);
160
        xfilter_result_print(res);
161
        status = xfilter_result_get_status(res);
162
        if (status == XF_JUNK || status == XF_REWRITTEN)
163
                retval = 0;
164
        else if (status == XF_UNCERTAIN)
165
                retval = 2;
166
        else if (status == XF_UNSUPPORTED_TYPE || status == XF_ERROR)
167
                retval = 127;
168
        else
169
                retval = 1;
170

    
171
        resdata = xfilter_result_get_message_data(res);
172
        if (resdata) {
173
                const char *content;
174
                content = xfilter_message_data_get_content(resdata);
175
                printf("content: %s\n", content);
176
        }
177

    
178
        xfilter_result_free(res);
179
        xfilter_message_data_free(msgdata);
180

    
181
        xfilter_manager_free(mgr);
182

    
183
        xfilter_bayes_db_done();
184
        xfilter_done();
185

    
186
        return retval;
187
}