root / lib / filters / textcontent-filter.c @ 4012ec30
History | View | Annotate | Download (1.4 kB)
| 1 | #include <glib.h> |
|---|---|
| 2 | #include <string.h> |
| 3 | |
| 4 | #include "filter.h" |
| 5 | #include "textcontent-filter.h" |
| 6 | |
| 7 | |
| 8 | static char *xfilter_rfc822_to_text(const XMessageData *data) |
| 9 | {
|
| 10 | const char *rfc822content; |
| 11 | const char *p, *eolp; |
| 12 | |
| 13 | p = rfc822content = xfilter_message_data_get_content(data); |
| 14 | if (!rfc822content)
|
| 15 | return NULL; |
| 16 | |
| 17 | /* find empty line */
|
| 18 | while (*p) {
|
| 19 | eolp = strchr(p, '\n');
|
| 20 | if (!eolp)
|
| 21 | return g_strdup(""); |
| 22 | if (p == eolp || (*p == '\r' && p + 1 == eolp)) { |
| 23 | p = eolp + 1;
|
| 24 | break;
|
| 25 | } |
| 26 | p = eolp + 1;
|
| 27 | } |
| 28 | |
| 29 | return g_strdup(p);
|
| 30 | } |
| 31 | |
| 32 | static XFilterStatus xfilter_content_func(XFilter *filter, const XMessageData *data, XMessageData **result) |
| 33 | {
|
| 34 | const char *mime_type; |
| 35 | const char *content; |
| 36 | char *new_content;
|
| 37 | |
| 38 | *result = NULL;
|
| 39 | |
| 40 | mime_type = xfilter_message_data_get_mime_type(data); |
| 41 | if (!mime_type)
|
| 42 | return XF_UNSUPPORTED_TYPE;
|
| 43 | |
| 44 | if (!g_strncasecmp(mime_type, "text/", 5)) { |
| 45 | content = xfilter_message_data_get_content(data); |
| 46 | *result = xfilter_message_data_new(content, "text/plain");
|
| 47 | } else if (!g_strcasecmp(mime_type, "message/rfc822")) { |
| 48 | new_content = xfilter_rfc822_to_text(data); |
| 49 | *result = xfilter_message_data_new(new_content, "text/plain");
|
| 50 | g_free(new_content); |
| 51 | } else
|
| 52 | return XF_UNSUPPORTED_TYPE;
|
| 53 | |
| 54 | |
| 55 | return XF_REWRITTEN;
|
| 56 | } |
| 57 | |
| 58 | XFilter *xfilter_textcontent_new(void)
|
| 59 | {
|
| 60 | XFilter *filter; |
| 61 | |
| 62 | filter = xfilter_new(XF_CONTENT, "text-content");
|
| 63 | xfilter_set_content_filter_func(X_CONTENT_FILTER(filter), |
| 64 | xfilter_content_func); |
| 65 | |
| 66 | return filter;
|
| 67 | } |