root / src / sylfilter.c @ 28703c7f
History | View | Annotate | Download (3.3 kB)
| 1 | #include <stdio.h> |
|---|---|
| 2 | #include <locale.h> |
| 3 | |
| 4 | #include "filter.h" |
| 5 | #include "filter-manager.h" |
| 6 | #include "filter-utils.h" |
| 7 | #include "filter-kvs.h" |
| 8 | |
| 9 | #define USE_QDBM 1 |
| 10 | #ifdef USE_QDBM
|
| 11 | # include "filter-kvs-qdbm.h" |
| 12 | #endif
|
| 13 | |
| 14 | #include "textcontent-filter.h" |
| 15 | #include "blacklist-filter.h" |
| 16 | #include "whitelist-filter.h" |
| 17 | #include "wordsep-filter.h" |
| 18 | #include "ngram-filter.h" |
| 19 | #include "bayes-filter.h" |
| 20 | |
| 21 | int main(int argc, char *argv[]) |
| 22 | {
|
| 23 | XFilterManager *mgr; |
| 24 | XMessageData *msgdata; |
| 25 | XMessageData *resdata; |
| 26 | XFilterResult *res; |
| 27 | XFilterStatus status; |
| 28 | int retval = 0; |
| 29 | |
| 30 | XFilterConstructorFunc learn_ctors[] = {
|
| 31 | xfilter_textcontent_new, |
| 32 | xfilter_wordsep_new, |
| 33 | xfilter_ngram_new, |
| 34 | xfilter_bayes_learn_new, |
| 35 | NULL
|
| 36 | }; |
| 37 | |
| 38 | XFilterConstructorFunc ctors[] = {
|
| 39 | xfilter_textcontent_new, |
| 40 | xfilter_blacklist_new, |
| 41 | xfilter_whitelist_new, |
| 42 | xfilter_wordsep_new, |
| 43 | xfilter_ngram_new, |
| 44 | xfilter_bayes_new, |
| 45 | NULL
|
| 46 | }; |
| 47 | |
| 48 | const char *rfc822content = |
| 49 | "From: foo@bar\n"
|
| 50 | "To: fuga@hoge\n"
|
| 51 | "Subject: test message\n"
|
| 52 | "\n"
|
| 53 | "This is a test message.\n"
|
| 54 | "...\n"
|
| 55 | "\n"
|
| 56 | "-- \n"
|
| 57 | "Foo Bar <foo@bar>\n";
|
| 58 | |
| 59 | const char *rfc822_file = NULL; |
| 60 | |
| 61 | setlocale(LC_ALL, "");
|
| 62 | |
| 63 | xfilter_init(); |
| 64 | |
| 65 | #ifdef USE_QDBM
|
| 66 | xfilter_kvs_qdbm_set_engine(); |
| 67 | #endif
|
| 68 | xfilter_bayes_db_init(); |
| 69 | |
| 70 | // set message files
|
| 71 | if (argc > 1) |
| 72 | rfc822_file = argv[1];
|
| 73 | |
| 74 | // learning
|
| 75 | mgr = xfilter_manager_new(); |
| 76 | |
| 77 | xfilter_manager_add_filters(mgr, learn_ctors); |
| 78 | |
| 79 | if (rfc822_file)
|
| 80 | msgdata = xfilter_message_data_read_file(rfc822_file, "message/rfc822");
|
| 81 | else
|
| 82 | msgdata = xfilter_message_data_new(rfc822content, "message/rfc822");
|
| 83 | |
| 84 | res = xfilter_manager_run(mgr, msgdata); |
| 85 | xfilter_result_print(res); |
| 86 | status = xfilter_result_get_status(res); |
| 87 | |
| 88 | resdata = xfilter_result_get_message_data(res); |
| 89 | if (resdata) {
|
| 90 | const char *content; |
| 91 | content = xfilter_message_data_get_content(resdata); |
| 92 | printf("content: %s\n", content);
|
| 93 | } |
| 94 | |
| 95 | xfilter_result_free(res); |
| 96 | xfilter_message_data_free(msgdata); |
| 97 | xfilter_manager_free(mgr); |
| 98 | |
| 99 | printf("--------\n");
|
| 100 | |
| 101 | mgr = xfilter_manager_new(); |
| 102 | |
| 103 | xfilter_manager_add_filters(mgr, ctors); |
| 104 | |
| 105 | // test 1
|
| 106 | msgdata = xfilter_message_data_new("test", "text/plain"); |
| 107 | |
| 108 | res = xfilter_manager_run(mgr, msgdata); |
| 109 | xfilter_result_print(res); |
| 110 | status = xfilter_result_get_status(res); |
| 111 | |
| 112 | resdata = xfilter_result_get_message_data(res); |
| 113 | if (resdata) {
|
| 114 | const char *content; |
| 115 | content = xfilter_message_data_get_content(resdata); |
| 116 | printf("content: %s\n", content);
|
| 117 | } |
| 118 | |
| 119 | xfilter_result_free(res); |
| 120 | xfilter_message_data_free(msgdata); |
| 121 | |
| 122 | printf("--------\n");
|
| 123 | |
| 124 | // test 2
|
| 125 | if (rfc822_file)
|
| 126 | msgdata = xfilter_message_data_read_file(rfc822_file, "message/rfc822");
|
| 127 | else
|
| 128 | msgdata = xfilter_message_data_new(rfc822content, "message/rfc822");
|
| 129 | |
| 130 | res = xfilter_manager_run(mgr, msgdata); |
| 131 | xfilter_result_print(res); |
| 132 | status = xfilter_result_get_status(res); |
| 133 | if (status == XF_JUNK || status == XF_REWRITTEN)
|
| 134 | retval = 0;
|
| 135 | else if (status == XF_UNCERTAIN) |
| 136 | retval = 2;
|
| 137 | else if (status == XF_UNSUPPORTED_TYPE || status == XF_ERROR) |
| 138 | retval = 127;
|
| 139 | else
|
| 140 | retval = 1;
|
| 141 | |
| 142 | resdata = xfilter_result_get_message_data(res); |
| 143 | if (resdata) {
|
| 144 | const char *content; |
| 145 | content = xfilter_message_data_get_content(resdata); |
| 146 | printf("content: %s\n", content);
|
| 147 | } |
| 148 | |
| 149 | xfilter_result_free(res); |
| 150 | xfilter_message_data_free(msgdata); |
| 151 | |
| 152 | xfilter_manager_free(mgr); |
| 153 | |
| 154 | xfilter_bayes_db_done(); |
| 155 | xfilter_done(); |
| 156 | |
| 157 | return retval;
|
| 158 | } |