Statistics
| Branch: | Tag: | Revision:

root / src / sylfilter-test.c @ master

History | View | Annotate | Download (4 kB)

1
/* SylFilter - a message filter
2
 *
3
 * Copyright (C) 2011 Hiroyuki Yamamoto
4
 * Copyright (C) 2011 Sylpheed Development Team
5
 */
6
7
#include "config.h"
8
9
#include <stdio.h>
10
#include <string.h>
11
#include <locale.h>
12
13
#include "filter.h"
14
#include "filter-manager.h"
15
#include "filter-utils.h"
16
#include "filter-kvs.h"
17
18
#ifdef USE_QDBM
19
#  include "filter-kvs-qdbm.h"
20
#elif defined(USE_SQLITE)
21
#  include "filter-kvs-sqlite.h"
22
#endif
23
24
#include "textcontent-filter.h"
25
#include "blacklist-filter.h"
26
#include "whitelist-filter.h"
27
#include "wordsep-filter.h"
28
#include "ngram-filter.h"
29
#include "bayes-filter.h"
30
31
int main(int argc, char *argv[])
32
{
33
        XFilterManager *mgr;
34
        XMessageData *msgdata;
35
        XMessageData *resdata;
36
        XFilterResult *res;
37
        XFilterStatus status;
38
        int retval = 0;
39
        int i;
40
        int is_junk = 1;
41
42
        XFilterConstructorFunc learn_junk_ctors[] = {
43
                xfilter_textcontent_new,
44
                xfilter_wordsep_new,
45
                xfilter_ngram_new,
46
                xfilter_bayes_learn_junk_new,
47
                NULL
48
        };
49
50
        XFilterConstructorFunc learn_nojunk_ctors[] = {
51
                xfilter_textcontent_new,
52
                xfilter_wordsep_new,
53
                xfilter_ngram_new,
54
                xfilter_bayes_learn_nojunk_new,
55
                NULL
56
        };
57
58
        XFilterConstructorFunc ctors[] = {
59
                xfilter_textcontent_new,
60
                xfilter_blacklist_new,
61
                xfilter_whitelist_new,
62
                xfilter_wordsep_new,
63
                xfilter_ngram_new,
64
                xfilter_bayes_new,
65
                NULL
66
        };
67
68
        const char *rfc822content =
69
                "From: foo@bar\n"
70
                "To: fuga@hoge\n"
71
                "Subject: test message\n"
72
                "\n"
73
                "This is a test message.\n"
74
                "...\n"
75
                "\n"
76
                "-- \n"
77
                "Foo Bar <foo@bar>\n";
78
79
        const char *rfc822_file = NULL;
80
81
        setlocale(LC_ALL, "");
82
83
        xfilter_init(XF_APP_MODE_STANDALONE);
84
85
#ifdef USE_QDBM
86
        xfilter_kvs_qdbm_set_engine();
87
#elif defined(USE_SQLITE)
88
        xfilter_kvs_sqlite_set_engine();
89
#endif
90
        if (xfilter_bayes_db_init(NULL) < 0) {
91
                fprintf(stderr, "Database initialization error\n");
92
                return 127;
93
        }
94
95
        // set message files
96
        for (i = 1; i < argc; i++) {
97
                if (!strcmp(argv[i], "-j"))
98
                        is_junk = 1;
99
                else if (!strcmp(argv[i], "-c"))
100
                        is_junk = 0;
101
                else
102
                        rfc822_file = argv[i];
103
        }
104
105
        // learning
106
        mgr = xfilter_manager_new();
107
108
        if (is_junk)
109
                xfilter_manager_add_filters(mgr, learn_junk_ctors);
110
        else
111
                xfilter_manager_add_filters(mgr, learn_nojunk_ctors);
112
113
        if (rfc822_file)
114
                msgdata = xfilter_message_data_read_file(rfc822_file, "message/rfc822");
115
        else
116
                msgdata = xfilter_message_data_new(rfc822content, "message/rfc822");
117
118
        res = xfilter_manager_run(mgr, msgdata);
119
        xfilter_result_print(res);
120
        status = xfilter_result_get_status(res);
121
122
        resdata = xfilter_result_get_message_data(res);
123
        if (resdata) {
124
                const char *content;
125
                content = xfilter_message_data_get_content(resdata);
126
                printf("content: %s\n", content);
127
        }
128
129
        xfilter_result_free(res);
130
        xfilter_message_data_free(msgdata);
131
        xfilter_manager_free(mgr);
132
133
        printf("--------\n");
134
135
        mgr = xfilter_manager_new();
136
137
        xfilter_manager_add_filters(mgr, ctors);
138
139
        // test 1
140
        msgdata = xfilter_message_data_new("test", "text/plain");
141
142
        res = xfilter_manager_run(mgr, msgdata);
143
        xfilter_result_print(res);
144
        status = xfilter_result_get_status(res);
145
146
        resdata = xfilter_result_get_message_data(res);
147
        if (resdata) {
148
                const char *content;
149
                content = xfilter_message_data_get_content(resdata);
150
                printf("content: %s\n", content);
151
        }
152
153
        xfilter_result_free(res);
154
        xfilter_message_data_free(msgdata);
155
156
        printf("--------\n");
157
158
        // test 2
159
        if (rfc822_file)
160
                msgdata = xfilter_message_data_read_file(rfc822_file, "message/rfc822");
161
        else
162
                msgdata = xfilter_message_data_new(rfc822content, "message/rfc822");
163
164
        res = xfilter_manager_run(mgr, msgdata);
165
        xfilter_result_print(res);
166
        status = xfilter_result_get_status(res);
167
        if (status == XF_JUNK || status == XF_REWRITTEN)
168
                retval = 0;
169
        else if (status == XF_UNCERTAIN)
170
                retval = 2;
171
        else if (status == XF_UNSUPPORTED_TYPE || status == XF_ERROR)
172
                retval = 127;
173
        else
174
                retval = 1;
175
176
        resdata = xfilter_result_get_message_data(res);
177
        if (resdata) {
178
                const char *content;
179
                content = xfilter_message_data_get_content(resdata);
180
                printf("content: %s\n", content);
181
        }
182
183
        xfilter_result_free(res);
184
        xfilter_message_data_free(msgdata);
185
186
        xfilter_manager_free(mgr);
187
188
        xfilter_bayes_db_done();
189
        xfilter_done();
190
191
        return retval;
192
}