root / lib / filter.c @ aebfd4cc
History | View | Annotate | Download (7.3 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 <glib.h> |
| 10 | |
| 11 | #include "filter.h" |
| 12 | #include "filter-private.h" |
| 13 | #include "filter-utils.h" |
| 14 | |
| 15 | |
| 16 | static int xfilter_debug_mode = 0; |
| 17 | |
| 18 | |
| 19 | int xfilter_init(void) |
| 20 | {
|
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | void xfilter_done(void) |
| 25 | {
|
| 26 | } |
| 27 | |
| 28 | XFilter *xfilter_new(XFilterType type, const char *name) |
| 29 | {
|
| 30 | XFilter *filter; |
| 31 | |
| 32 | if (type == XF_CONTENT) {
|
| 33 | filter = XFILTER(g_new0(XContentFilter, 1));
|
| 34 | } else {
|
| 35 | filter = XFILTER(g_new0(XTestFilter, 1));
|
| 36 | } |
| 37 | |
| 38 | filter->type = type; |
| 39 | filter->name = g_strdup(name); |
| 40 | |
| 41 | return filter;
|
| 42 | } |
| 43 | |
| 44 | void xfilter_free(XFilter *filter)
|
| 45 | {
|
| 46 | if (!filter)
|
| 47 | return;
|
| 48 | g_free(filter->name); |
| 49 | g_free(filter); |
| 50 | } |
| 51 | |
| 52 | XFilterType xfilter_get_type(XFilter *filter) |
| 53 | {
|
| 54 | g_return_val_if_fail(filter != NULL, -1); |
| 55 | |
| 56 | return filter->type;
|
| 57 | } |
| 58 | |
| 59 | const char *xfilter_get_name(XFilter *filter) |
| 60 | {
|
| 61 | g_return_val_if_fail(filter != NULL, NULL); |
| 62 | |
| 63 | return filter->name;
|
| 64 | } |
| 65 | |
| 66 | void xfilter_set_input_mime_types(XFilter *filter, const char **types) |
| 67 | {
|
| 68 | } |
| 69 | |
| 70 | void xfilter_set_output_mime_type(XFilter *filter, const char *type) |
| 71 | {
|
| 72 | } |
| 73 | |
| 74 | XMessageData *xfilter_message_data_new(const char *src, const char *mime_type) |
| 75 | {
|
| 76 | XMessageData *data; |
| 77 | |
| 78 | g_return_val_if_fail(mime_type != NULL, NULL); |
| 79 | |
| 80 | data = g_new0(XMessageData, 1);
|
| 81 | data->file = NULL;
|
| 82 | data->content = g_strdup(src); |
| 83 | data->mime_type = g_strdup(mime_type); |
| 84 | return data;
|
| 85 | } |
| 86 | |
| 87 | XMessageData *xfilter_message_data_read_file(const char *file, const char *mime_type) |
| 88 | {
|
| 89 | XMessageData *data; |
| 90 | |
| 91 | g_return_val_if_fail(file != NULL, NULL); |
| 92 | g_return_val_if_fail(mime_type != NULL, NULL); |
| 93 | |
| 94 | data = g_new0(XMessageData, 1);
|
| 95 | data->file = g_strdup(file); |
| 96 | data->content = NULL;
|
| 97 | data->mime_type = g_strdup(mime_type); |
| 98 | return data;
|
| 99 | } |
| 100 | |
| 101 | void xfilter_message_data_free(XMessageData *msgdata)
|
| 102 | {
|
| 103 | if (!msgdata)
|
| 104 | return;
|
| 105 | |
| 106 | g_free(msgdata->mime_type); |
| 107 | g_free(msgdata->file); |
| 108 | g_free(msgdata->content); |
| 109 | g_free(msgdata->from); |
| 110 | g_free(msgdata->to); |
| 111 | g_free(msgdata->cc); |
| 112 | g_free(msgdata->subject); |
| 113 | |
| 114 | g_free(msgdata); |
| 115 | } |
| 116 | |
| 117 | void xfilter_message_data_set_file(XMessageData *msgdata, const char *file) |
| 118 | {
|
| 119 | xfilter_message_data_set_content(msgdata, NULL);
|
| 120 | g_free(msgdata->file); |
| 121 | msgdata->file = g_strdup(file); |
| 122 | } |
| 123 | |
| 124 | void xfilter_message_data_set_content(XMessageData *msgdata, char *content) |
| 125 | {
|
| 126 | if (msgdata->content)
|
| 127 | g_free(msgdata->content); |
| 128 | msgdata->content = content; |
| 129 | } |
| 130 | |
| 131 | void xfilter_message_data_set_attribute(XMessageData *msgdata, XMessageAttr attr, const char *value, int append) |
| 132 | {
|
| 133 | int len;
|
| 134 | |
| 135 | #define APPEND_STR(d, s) \
|
| 136 | if (d) { \
|
| 137 | len = strlen(d); \ |
| 138 | d = g_realloc(d, len + strlen(s) + 2); \
|
| 139 | d[len] = '\n'; \
|
| 140 | strcpy(d + len + 1, s); \
|
| 141 | } else \
|
| 142 | d = g_strdup(s); |
| 143 | |
| 144 | #define SET_STR(d, s) { g_free(d); d = g_strdup(s); }
|
| 145 | |
| 146 | if (!value)
|
| 147 | return;
|
| 148 | |
| 149 | switch (attr) {
|
| 150 | case XM_FROM:
|
| 151 | if (append) {
|
| 152 | APPEND_STR(msgdata->from, value); |
| 153 | } else {
|
| 154 | SET_STR(msgdata->from, value); |
| 155 | } |
| 156 | break;
|
| 157 | case XM_TO:
|
| 158 | if (append) {
|
| 159 | APPEND_STR(msgdata->to, value); |
| 160 | } else {
|
| 161 | SET_STR(msgdata->to, value); |
| 162 | } |
| 163 | break;
|
| 164 | case XM_CC:
|
| 165 | if (append) {
|
| 166 | APPEND_STR(msgdata->cc, value); |
| 167 | } else {
|
| 168 | SET_STR(msgdata->cc, value); |
| 169 | } |
| 170 | break;
|
| 171 | case XM_SUBJECT:
|
| 172 | if (append) {
|
| 173 | APPEND_STR(msgdata->subject, value); |
| 174 | } else {
|
| 175 | SET_STR(msgdata->subject, value); |
| 176 | } |
| 177 | break;
|
| 178 | default:
|
| 179 | break;
|
| 180 | } |
| 181 | |
| 182 | #undef APPEND_STR
|
| 183 | #undef SET_STR
|
| 184 | } |
| 185 | |
| 186 | void xfilter_message_data_copy_attributes(XMessageData *msgdata, const XMessageData *srcdata) |
| 187 | {
|
| 188 | g_free(msgdata->from); |
| 189 | msgdata->from = g_strdup(srcdata->from); |
| 190 | g_free(msgdata->to); |
| 191 | msgdata->to = g_strdup(srcdata->to); |
| 192 | g_free(msgdata->cc); |
| 193 | msgdata->cc = g_strdup(srcdata->cc); |
| 194 | g_free(msgdata->subject); |
| 195 | msgdata->subject = g_strdup(srcdata->subject); |
| 196 | } |
| 197 | |
| 198 | const char *xfilter_message_data_get_attribute(const XMessageData *msgdata, XMessageAttr attr) |
| 199 | {
|
| 200 | g_return_val_if_fail(msgdata != NULL, NULL); |
| 201 | |
| 202 | switch (attr) {
|
| 203 | case XM_FROM:
|
| 204 | return msgdata->from;
|
| 205 | case XM_TO:
|
| 206 | return msgdata->to;
|
| 207 | case XM_CC:
|
| 208 | return msgdata->cc;
|
| 209 | case XM_SUBJECT:
|
| 210 | return msgdata->subject;
|
| 211 | default:
|
| 212 | break;
|
| 213 | } |
| 214 | |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | const char *xfilter_message_data_get_mime_type(const XMessageData *msgdata) |
| 219 | {
|
| 220 | g_return_val_if_fail(msgdata != NULL, NULL); |
| 221 | |
| 222 | return msgdata->mime_type;
|
| 223 | } |
| 224 | |
| 225 | const char *xfilter_message_data_get_file(const XMessageData *msgdata) |
| 226 | {
|
| 227 | g_return_val_if_fail(msgdata != NULL, NULL); |
| 228 | |
| 229 | return msgdata->file;
|
| 230 | } |
| 231 | |
| 232 | const char *xfilter_message_data_get_content(const XMessageData *msgdata) |
| 233 | {
|
| 234 | g_return_val_if_fail(msgdata != NULL, NULL); |
| 235 | |
| 236 | if (msgdata->content)
|
| 237 | return msgdata->content;
|
| 238 | |
| 239 | if (msgdata->file) {
|
| 240 | char *content;
|
| 241 | |
| 242 | xfilter_debug_print("xfilter_message_data_get_content: getting file content: %s\n", msgdata->file);
|
| 243 | content = xfilter_utils_get_file_contents(msgdata->file); |
| 244 | if (content)
|
| 245 | ((XMessageData *)msgdata)->content = content; |
| 246 | |
| 247 | return content;
|
| 248 | } |
| 249 | |
| 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | void xfilter_set_content_filter_func(XContentFilter *filter, XContentFilterFunc func)
|
| 254 | {
|
| 255 | g_return_if_fail(XFILTER(filter)->type == XF_CONTENT); |
| 256 | |
| 257 | filter->content_filter_func = func; |
| 258 | } |
| 259 | |
| 260 | void xfilter_set_test_filter_func(XTestFilter *filter, XTestFilterFunc func)
|
| 261 | {
|
| 262 | g_return_if_fail(XFILTER(filter)->type == XF_TEST); |
| 263 | |
| 264 | filter->test_filter_func = func; |
| 265 | } |
| 266 | |
| 267 | XFilterStatus xfilter_exec(XFilter *filter, const XMessageData *msgdata, XFilterResult *result)
|
| 268 | {
|
| 269 | g_return_val_if_fail(filter != NULL, XF_ERROR);
|
| 270 | g_return_val_if_fail(msgdata != NULL, XF_ERROR);
|
| 271 | |
| 272 | if (filter->type == XF_CONTENT) {
|
| 273 | if (!X_CONTENT_FILTER(filter)->content_filter_func)
|
| 274 | return XF_ERROR;
|
| 275 | return X_CONTENT_FILTER(filter)->content_filter_func(filter, msgdata, result);
|
| 276 | } else {
|
| 277 | if (!X_TEST_FILTER(filter)->test_filter_func)
|
| 278 | return XF_ERROR;
|
| 279 | return X_TEST_FILTER(filter)->test_filter_func(filter, msgdata, result);
|
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void xfilter_result_print(XFilterResult *result)
|
| 284 | {
|
| 285 | printf("XFilterResult: status = %d, probability = %f\n", result->status, result->probability);
|
| 286 | } |
| 287 | |
| 288 | XFilterResult *xfilter_result_new(void)
|
| 289 | {
|
| 290 | XFilterResult *res; |
| 291 | |
| 292 | res = g_new0(XFilterResult, 1);
|
| 293 | res->status = XF_NONE; |
| 294 | res->probability = 0.5; |
| 295 | return res;
|
| 296 | } |
| 297 | |
| 298 | void xfilter_result_set_status(XFilterResult *result, XFilterStatus status)
|
| 299 | {
|
| 300 | result->status = status; |
| 301 | } |
| 302 | |
| 303 | void xfilter_result_set_message_data(XFilterResult *result, XMessageData *msgdata)
|
| 304 | {
|
| 305 | if (result->msgdata)
|
| 306 | xfilter_message_data_free(result->msgdata); |
| 307 | result->msgdata = msgdata; |
| 308 | } |
| 309 | |
| 310 | void xfilter_result_set_probability(XFilterResult *result, double prob) |
| 311 | {
|
| 312 | result->probability = prob; |
| 313 | } |
| 314 | |
| 315 | XFilterStatus xfilter_result_get_status(XFilterResult *result) |
| 316 | {
|
| 317 | g_return_val_if_fail(result != NULL, XF_ERROR);
|
| 318 | |
| 319 | return result->status;
|
| 320 | } |
| 321 | |
| 322 | XMessageData *xfilter_result_get_message_data(XFilterResult *result) |
| 323 | {
|
| 324 | g_return_val_if_fail(result != NULL, NULL); |
| 325 | |
| 326 | return result->msgdata;
|
| 327 | } |
| 328 | |
| 329 | double xfilter_result_get_probability(XFilterResult *result)
|
| 330 | {
|
| 331 | g_return_val_if_fail(result != NULL, 0.0); |
| 332 | |
| 333 | return result->probability;
|
| 334 | } |
| 335 | |
| 336 | void xfilter_result_free(XFilterResult *result)
|
| 337 | {
|
| 338 | if (!result)
|
| 339 | return;
|
| 340 | xfilter_message_data_free(result->msgdata); |
| 341 | g_free(result); |
| 342 | } |
| 343 | |
| 344 | void xfilter_set_debug_mode(int mode) |
| 345 | {
|
| 346 | xfilter_debug_mode = mode; |
| 347 | } |
| 348 | |
| 349 | int xfilter_get_debug_mode(void) |
| 350 | {
|
| 351 | return xfilter_debug_mode;
|
| 352 | } |
| 353 | |
| 354 | void xfilter_debug_print(const char *format, ...) |
| 355 | {
|
| 356 | va_list args; |
| 357 | char buf[1024]; |
| 358 | |
| 359 | if (!xfilter_debug_mode)
|
| 360 | return;
|
| 361 | |
| 362 | va_start(args, format); |
| 363 | g_vsnprintf(buf, sizeof(buf), format, args);
|
| 364 | va_end(args); |
| 365 | |
| 366 | fputs(buf, stderr); |
| 367 | } |