root / src / sylfilter.c @ cfadedec
History | View | Annotate | Download (5.1 kB)
| 1 | #include <stdio.h> |
|---|---|
| 2 | #include <string.h> |
| 3 | #include <locale.h> |
| 4 | |
| 5 | #include "filter.h" |
| 6 | #include "filter-manager.h" |
| 7 | #include "filter-utils.h" |
| 8 | #include "filter-kvs.h" |
| 9 | |
| 10 | #define USE_QDBM 1 |
| 11 | #ifdef USE_QDBM
|
| 12 | # include "filter-kvs-qdbm.h" |
| 13 | #endif
|
| 14 | |
| 15 | #include "textcontent-filter.h" |
| 16 | #include "blacklist-filter.h" |
| 17 | #include "whitelist-filter.h" |
| 18 | #include "wordsep-filter.h" |
| 19 | #include "ngram-filter.h" |
| 20 | #include "bayes-filter.h" |
| 21 | |
| 22 | enum {
|
| 23 | MODE_TEST_JUNK, |
| 24 | MODE_LEARN_JUNK, |
| 25 | MODE_LEARN_CLEAN, |
| 26 | MODE_SHOW_STATUS |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | static int verbose = 0; |
| 31 | |
| 32 | static int learn_filter(int mode, const char *file); |
| 33 | static int test_filter(int mode, const char *file); |
| 34 | static void print_message_data(XMessageData *msgdata); |
| 35 | |
| 36 | |
| 37 | int main(int argc, char *argv[]) |
| 38 | {
|
| 39 | int retval = 0; |
| 40 | int i;
|
| 41 | int mode = MODE_TEST_JUNK;
|
| 42 | |
| 43 | setlocale(LC_ALL, "");
|
| 44 | |
| 45 | xfilter_init(); |
| 46 | |
| 47 | #ifdef USE_QDBM
|
| 48 | xfilter_kvs_qdbm_set_engine(); |
| 49 | #endif
|
| 50 | xfilter_bayes_db_init(); |
| 51 | |
| 52 | for (i = 1; i < argc; i++) { |
| 53 | if (!strcmp(argv[i], "-j")) |
| 54 | mode = MODE_LEARN_JUNK; |
| 55 | else if (!strcmp(argv[i], "-c")) |
| 56 | mode = MODE_LEARN_CLEAN; |
| 57 | else if (!strcmp(argv[i], "-t")) |
| 58 | mode = MODE_TEST_JUNK; |
| 59 | else if (!strcmp(argv[i], "-s")) |
| 60 | mode = MODE_SHOW_STATUS; |
| 61 | else if (!strcmp(argv[i], "-v")) |
| 62 | verbose = 1;
|
| 63 | else if (!strcmp(argv[i], "-d")) |
| 64 | xfilter_set_debug_mode(1);
|
| 65 | } |
| 66 | |
| 67 | if (mode == MODE_SHOW_STATUS) {
|
| 68 | retval = xfilter_bayes_db_show_contents(); |
| 69 | } else if (mode == MODE_LEARN_JUNK || mode == MODE_LEARN_CLEAN) { |
| 70 | for (i = 1; i < argc; i++) { |
| 71 | if (argv[i][0] != '-') { |
| 72 | retval = learn_filter(mode, argv[i]); |
| 73 | if (retval != 0) |
| 74 | break;
|
| 75 | } |
| 76 | } |
| 77 | } else {
|
| 78 | for (i = 1; i < argc; i++) { |
| 79 | if (argv[i][0] != '-') { |
| 80 | retval = test_filter(mode, argv[i]); |
| 81 | if (retval == 127) |
| 82 | break;
|
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | xfilter_bayes_db_done(); |
| 88 | xfilter_done(); |
| 89 | |
| 90 | return retval;
|
| 91 | } |
| 92 | |
| 93 | static int learn_filter(int mode, const char *file) |
| 94 | {
|
| 95 | XFilterManager *mgr; |
| 96 | XMessageData *msgdata; |
| 97 | XMessageData *resdata; |
| 98 | XFilterResult *res; |
| 99 | XFilterStatus status; |
| 100 | int retval = 0; |
| 101 | |
| 102 | XFilterConstructorFunc learn_junk_ctors[] = {
|
| 103 | xfilter_textcontent_new, |
| 104 | xfilter_wordsep_new, |
| 105 | xfilter_ngram_new, |
| 106 | xfilter_bayes_learn_junk_new, |
| 107 | NULL
|
| 108 | }; |
| 109 | |
| 110 | XFilterConstructorFunc learn_nojunk_ctors[] = {
|
| 111 | xfilter_textcontent_new, |
| 112 | xfilter_wordsep_new, |
| 113 | xfilter_ngram_new, |
| 114 | xfilter_bayes_learn_nojunk_new, |
| 115 | NULL
|
| 116 | }; |
| 117 | |
| 118 | if (verbose)
|
| 119 | printf("learning message file: %s\n", file);
|
| 120 | |
| 121 | mgr = xfilter_manager_new(); |
| 122 | |
| 123 | if (mode == MODE_LEARN_JUNK)
|
| 124 | xfilter_manager_add_filters(mgr, learn_junk_ctors); |
| 125 | else
|
| 126 | xfilter_manager_add_filters(mgr, learn_nojunk_ctors); |
| 127 | |
| 128 | msgdata = xfilter_message_data_read_file(file, "message/rfc822");
|
| 129 | |
| 130 | res = xfilter_manager_run(mgr, msgdata); |
| 131 | if (verbose)
|
| 132 | xfilter_result_print(res); |
| 133 | status = xfilter_result_get_status(res); |
| 134 | if (status == XF_UNSUPPORTED_TYPE || status == XF_ERROR) {
|
| 135 | printf("%s: Error on learning mail\n", file);
|
| 136 | retval = 127;
|
| 137 | } |
| 138 | |
| 139 | if (xfilter_get_debug_mode()) {
|
| 140 | resdata = xfilter_result_get_message_data(res); |
| 141 | print_message_data(resdata); |
| 142 | } |
| 143 | |
| 144 | xfilter_result_free(res); |
| 145 | xfilter_message_data_free(msgdata); |
| 146 | xfilter_manager_free(mgr); |
| 147 | |
| 148 | return retval;
|
| 149 | } |
| 150 | |
| 151 | static int test_filter(int mode, const char *file) |
| 152 | {
|
| 153 | XFilterManager *mgr; |
| 154 | XMessageData *msgdata; |
| 155 | XMessageData *resdata; |
| 156 | XFilterResult *res; |
| 157 | XFilterStatus status; |
| 158 | int retval = 0; |
| 159 | |
| 160 | XFilterConstructorFunc ctors[] = {
|
| 161 | xfilter_textcontent_new, |
| 162 | xfilter_blacklist_new, |
| 163 | xfilter_whitelist_new, |
| 164 | xfilter_wordsep_new, |
| 165 | xfilter_ngram_new, |
| 166 | xfilter_bayes_new, |
| 167 | NULL
|
| 168 | }; |
| 169 | |
| 170 | if (verbose)
|
| 171 | printf("testing message file: %s\n", file);
|
| 172 | |
| 173 | mgr = xfilter_manager_new(); |
| 174 | xfilter_manager_add_filters(mgr, ctors); |
| 175 | |
| 176 | msgdata = xfilter_message_data_read_file(file, "message/rfc822");
|
| 177 | |
| 178 | res = xfilter_manager_run(mgr, msgdata); |
| 179 | if (verbose)
|
| 180 | xfilter_result_print(res); |
| 181 | status = xfilter_result_get_status(res); |
| 182 | if (status == XF_JUNK) {
|
| 183 | printf("%s: This is a junk mail (prob: %f)\n", file, xfilter_result_get_probability(res));
|
| 184 | retval = 0;
|
| 185 | } else if (status == XF_UNCERTAIN) { |
| 186 | printf("%s: This mail could not be classified (prob: %f)\n", file, xfilter_result_get_probability(res));
|
| 187 | retval = 2;
|
| 188 | } else if (status == XF_UNSUPPORTED_TYPE || status == XF_ERROR) { |
| 189 | printf("%s: Error on testing mail\n", file);
|
| 190 | retval = 127;
|
| 191 | } else {
|
| 192 | printf("%s: This is a clean mail (prob: %f)\n", file, xfilter_result_get_probability(res));
|
| 193 | retval = 1;
|
| 194 | } |
| 195 | |
| 196 | if (xfilter_get_debug_mode()) {
|
| 197 | resdata = xfilter_result_get_message_data(res); |
| 198 | print_message_data(resdata); |
| 199 | } |
| 200 | |
| 201 | xfilter_result_free(res); |
| 202 | xfilter_message_data_free(msgdata); |
| 203 | |
| 204 | xfilter_manager_free(mgr); |
| 205 | |
| 206 | return retval;
|
| 207 | } |
| 208 | |
| 209 | static void print_message_data(XMessageData *msgdata) |
| 210 | {
|
| 211 | const char *content; |
| 212 | |
| 213 | if (!msgdata)
|
| 214 | return;
|
| 215 | |
| 216 | printf("\n");
|
| 217 | |
| 218 | content = xfilter_message_data_get_attribute(msgdata, XM_FROM); |
| 219 | if (content)
|
| 220 | printf("from: %s\n", content);
|
| 221 | content = xfilter_message_data_get_attribute(msgdata, XM_TO); |
| 222 | if (content)
|
| 223 | printf("to: %s\n", content);
|
| 224 | content = xfilter_message_data_get_attribute(msgdata, XM_CC); |
| 225 | if (content)
|
| 226 | printf("cc: %s\n", content);
|
| 227 | content = xfilter_message_data_get_attribute(msgdata, XM_SUBJECT); |
| 228 | if (content)
|
| 229 | printf("subject: %s\n", content);
|
| 230 | content = xfilter_message_data_get_content(msgdata); |
| 231 | printf("content: %s\n", content);
|
| 232 | } |