Statistics
| Branch: | Tag: | Revision:

root / lib / filters / textcontent-filter.c @ 8d7dcace

History | View | Annotate | Download (4.86 KB)

1
#include <glib.h>
2
#include <stdio.h>
3
#include <string.h>
4

    
5
#include "filter.h"
6
#include "textcontent-filter.h"
7

    
8
#include "libsylph/procheader.h"
9
#include "libsylph/procmsg.h"
10
#include "libsylph/procmime.h"
11
#include "libsylph/utils.h"
12

    
13

    
14
static XMessageData *xfilter_rfc822_to_text(const XMessageData *data)
15
{
16
        const char *file;
17
        MsgInfo *msginfo;
18
        MsgFlags flags = {0};
19
        XMessageData *newdata;
20
        FILE *fp;
21
        char *text;
22

    
23
        file = xfilter_message_data_get_file(data);
24
        if (!file)
25
                return NULL;
26

    
27
        msginfo = procheader_parse_file(file, flags, TRUE);
28
        if (!msginfo)
29
                return NULL;
30
        msginfo->file_path = g_strdup(file);
31

    
32
        fp = procmime_get_first_text_content(msginfo, NULL);
33
        if (!fp) {
34
                procmsg_msginfo_free(msginfo);
35
                return NULL;
36
        }
37
        text = file_read_stream_to_str(fp);
38
        if (!text) {
39
                fclose(fp);
40
                procmsg_msginfo_free(msginfo);
41
                return NULL;
42
        }
43

    
44
        newdata = xfilter_message_data_new(text, "text/plain");
45
        if (msginfo->from)
46
                xfilter_message_data_set_attribute(newdata, XM_FROM, msginfo->from, FALSE);
47
        if (msginfo->to)
48
                xfilter_message_data_set_attribute(newdata, XM_TO, msginfo->to, FALSE);
49
        if (msginfo->cc)
50
                xfilter_message_data_set_attribute(newdata, XM_CC, msginfo->cc, FALSE);
51
        if (msginfo->subject)
52
                xfilter_message_data_set_attribute(newdata, XM_SUBJECT, msginfo->subject, FALSE);
53

    
54
        procmsg_msginfo_free(msginfo);
55
        return newdata;
56
}
57

    
58
#if 0
59
static XMessageData *xfilter_rfc822_to_text(const XMessageData *data)
60
{
61
        const char *rfc822content;
62
        const char *p, *eolp, *prev_eolp = NULL, *vp = NULL;
63
        XMessageData *newdata;
64
        XMessageAttr attr = -1;
65
        GString *from, *to, *cc, *subject;
66

67
        p = rfc822content = xfilter_message_data_get_content(data);
68
        if (!rfc822content)
69
                return NULL;
70

71
        from = g_string_new("");
72
        to = g_string_new("");
73
        cc = g_string_new("");
74
        subject = g_string_new("");
75

76
        /* parse headers */
77
        while (*p) {
78
                eolp = strchr(p, '\n');
79
                if (!eolp) {
80
                        p = NULL;
81
                        break;
82
                }
83
                if (p == eolp || (*p == '\r' && p + 1 == eolp)) {
84
                        p = eolp + 1;
85
                        break;
86
                }
87

88
                if (!g_ascii_isspace(*p)) {
89
                        if (vp && vp < prev_eolp) {
90
                                switch (attr) {
91
                                case XM_FROM:
92
                                        if (from->len > 0)
93
                                                g_string_append_c(from, ' ');
94
                                        g_string_append_len(from, vp, prev_eolp - vp);
95
                                        break;
96
                                case XM_TO:
97
                                        if (to->len > 0)
98
                                                g_string_append_c(to, ' ');
99
                                        g_string_append_len(to, vp, prev_eolp - vp);
100
                                        break;
101
                                case XM_CC:
102
                                        if (cc->len > 0)
103
                                                g_string_append_c(cc, ' ');
104
                                        g_string_append_len(cc, vp, prev_eolp - vp);
105
                                        break;
106
                                case XM_SUBJECT:
107
                                        if (subject->len > 0)
108
                                                g_string_append_c(subject, ' ');
109
                                        g_string_append_len(subject, vp, prev_eolp - vp);
110
                                        break;
111
                                default:
112
                                        break;
113
                                }
114
                                attr = -1;
115
                                vp = NULL;
116
                        }
117

118
                        if (!g_ascii_strncasecmp(p, "From:", 5)) {
119
                                attr = XM_FROM;
120
                                vp = p + 5;
121
                        } else if (!g_ascii_strncasecmp(p, "To:", 3)) {
122
                                attr = XM_TO;
123
                                vp = p + 3;
124
                        } else if (!g_ascii_strncasecmp(p, "Cc:", 3)) {
125
                                attr = XM_CC;
126
                                vp = p + 3;
127
                        } else if (!g_ascii_strncasecmp(p, "Subject:", 8)) {
128
                                attr = XM_SUBJECT;
129
                                vp = p + 8;
130
                        }
131
                }
132

133
                prev_eolp = eolp;
134
                p = eolp + 1;
135
        }
136

137
        newdata = xfilter_message_data_new(p ? p : "", "text/plain");
138
        if (from->len)
139
                xfilter_message_data_set_attribute(newdata, XM_FROM, from->str, FALSE);
140
        if (to->len)
141
                xfilter_message_data_set_attribute(newdata, XM_TO, to->str, FALSE);
142
        if (cc->len)
143
                xfilter_message_data_set_attribute(newdata, XM_CC, cc->str, FALSE);
144
        if (subject->len)
145
                xfilter_message_data_set_attribute(newdata, XM_SUBJECT, subject->str, FALSE);
146

147
        g_string_free(subject, TRUE);
148
        g_string_free(cc, TRUE);
149
        g_string_free(to, TRUE);
150
        g_string_free(from, TRUE);
151

152
        return newdata;
153
}
154
#endif
155

    
156
static XFilterStatus xfilter_content_func(XFilter *filter, const XMessageData *data, XFilterResult *result)
157
{
158
        const char *mime_type;
159
        const char *content;
160
        XMessageData *newdata;
161

    
162
        g_return_val_if_fail(result != NULL, XF_ERROR);
163

    
164
        mime_type = xfilter_message_data_get_mime_type(data);
165
        if (!mime_type)
166
                return XF_UNSUPPORTED_TYPE;
167

    
168
        if (!g_strncasecmp(mime_type, "text/", 5)) {
169
                content = xfilter_message_data_get_content(data);
170
                newdata = xfilter_message_data_new(content, "text/plain");
171
                xfilter_result_set_message_data(result, newdata);
172
        } else if (!g_strcasecmp(mime_type, "message/rfc822")) {
173
                newdata = xfilter_rfc822_to_text(data);
174
                if (!newdata)
175
                        return XF_ERROR;
176
                xfilter_result_set_message_data(result, newdata);
177
                g_print("from:%s\n", xfilter_message_data_get_attribute(newdata, XM_FROM));
178
                g_print("to:%s\n", xfilter_message_data_get_attribute(newdata, XM_TO));
179
                g_print("cc:%s\n", xfilter_message_data_get_attribute(newdata, XM_CC));
180
                g_print("subject:%s\n", xfilter_message_data_get_attribute(newdata, XM_SUBJECT));
181
        } else
182
                return XF_UNSUPPORTED_TYPE;
183

    
184

    
185
        return XF_REWRITTEN;
186
}
187

    
188
XFilter *xfilter_textcontent_new(void)
189
{
190
        XFilter *filter;
191

    
192
        filter = xfilter_new(XF_CONTENT, "text-content");
193
        xfilter_set_content_filter_func(X_CONTENT_FILTER(filter),
194
                                        xfilter_content_func);
195

    
196
        return filter;
197
}