]> code.citadel.org Git - citadel.git/blob - libcitadel/tests/mimeparser_test.c
Add mime-parser-testing tool
[citadel.git] / libcitadel / tests / mimeparser_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <dirent.h>
8 #include <errno.h>
9
10 #include <fcntl.h>
11
12 #include <unistd.h>
13 #include <stddef.h>
14
15
16 #include "stringbuf_test.h"
17 #include "../lib/libcitadel.h"
18
19 /* shamelesly copied from msgbase.h */
20 struct ma_info {
21         int is_ma;              /* Set to 1 if we are using this stuff */
22         int freeze;             /* Freeze the replacement chain because we're
23                                  * digging through a subsection */
24         int did_print;          /* One alternative has been displayed */
25         char chosen_part[128];  /* Which part of a m/a did we choose? */
26         int chosen_pref;        /* Chosen part preference level (lower is better) */
27         int use_fo_hooks;       /* Use fixed output hooks */
28         int dont_decode;        /* should we call the decoder or not? */
29 };
30
31
32 /*
33  * Callback function for mime parser that simply lists the part
34  */
35 void list_this_part(char *name, 
36                     char *filename, 
37                     char *partnum, 
38                     char *disp,
39                     void *content, 
40                     char *cbtype, 
41                     char *cbcharset, 
42                     size_t length, 
43                     char *encoding,
44                     char *cbid, 
45                     void *cbuserdata)
46 {
47         struct ma_info *ma;
48         
49         ma = (struct ma_info *)cbuserdata;
50         if (ma->is_ma == 0) {
51                 printf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
52                         name, 
53                         filename, 
54                         partnum, 
55                         disp, 
56                         cbtype, 
57                         (long)length, 
58                         cbid, 
59                         cbcharset);
60         }
61 }
62
63 /* 
64  * Callback function for multipart prefix
65  */
66 void list_this_pref(char *name, 
67                     char *filename, 
68                     char *partnum, 
69                     char *disp,
70                     void *content, 
71                     char *cbtype, 
72                     char *cbcharset, 
73                     size_t length, 
74                     char *encoding,
75                     char *cbid, 
76                     void *cbuserdata)
77 {
78         struct ma_info *ma;
79         
80         ma = (struct ma_info *)cbuserdata;
81         if (!strcasecmp(cbtype, "multipart/alternative")) {
82                 ++ma->is_ma;
83         }
84
85         if (ma->is_ma == 0) {
86                 printf("pref=%s|%s\n", partnum, cbtype);
87         }
88 }
89
90 /* 
91  * Callback function for multipart sufffix
92  */
93 void list_this_suff(char *name, 
94                     char *filename, 
95                     char *partnum, 
96                     char *disp,
97                     void *content, 
98                     char *cbtype, 
99                     char *cbcharset, 
100                     size_t length, 
101                     char *encoding,
102                     char *cbid, 
103                     void *cbuserdata)
104 {
105         struct ma_info *ma;
106         
107         ma = (struct ma_info *)cbuserdata;
108         if (ma->is_ma == 0) {
109                 printf("suff=%s|%s\n", partnum, cbtype);
110         }
111         if (!strcasecmp(cbtype, "multipart/alternative")) {
112                 --ma->is_ma;
113         }
114 }
115
116
117 /*
118  * Callback function for mime parser that opens a section for downloading
119  * /
120 void mime_download(char *name, 
121                    char *filename, 
122                    char *partnum, 
123                    char *disp,
124                    void *content, 
125                    char *cbtype, 
126                    char *cbcharset, 
127                    size_t length,
128                    char *encoding, 
129                    char *cbid, 
130                    void *cbuserdata)
131 {
132         int rv = 0;
133         FILE *download_fp;
134
135         /* Silently go away if there's already a download open. * /
136
137         if (
138                 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
139         ||      (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
140         ) {
141                 download_fp = STDOUT;
142
143         
144                 rv = fwrite(content, length, 1, download_fp);
145                 fflush(CC->download_fp);
146                 rewind(CC->download_fp);
147         
148                 OpenCmdResult(filename, cbtype);
149         }
150 }
151 */
152
153
154 /*
155  * Callback function for mime parser that outputs a section all at once.
156  * We can specify the desired section by part number *or* content-id.
157  * /
158 void mime_spew_section(char *name, 
159                        char *filename, 
160                        char *partnum, 
161                        char *disp,
162                        void *content, 
163                        char *cbtype, 
164                        char *cbcharset, 
165                        size_t length,
166                        char *encoding, 
167                        char *cbid, 
168                        void *cbuserdata)
169 {
170         int *found_it = (int *)cbuserdata;
171
172         if (
173                 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
174         ||      (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
175         ) {
176                 *found_it = 1;
177                 printf("%d %d|-1|%s|%s|%s\n",
178                         BINARY_FOLLOWS,
179                         (int)length,
180                         filename,
181                         cbtype,
182                         cbcharset
183                 );
184                 fwrite(STDOUT, content, length);
185         }
186 }
187
188 */
189
190
191
192
193 int main(int argc, char* argv[])
194 {
195         char a;
196         int fd;
197         char *filename = NULL;
198         struct stat statbuf;
199         const char *Err;
200
201         StrBuf *MimeBuf;
202         long MimeLen;
203         char *MimeStr;
204         struct ma_info ma;
205         int do_proto = 0;
206
207         setvbuf(stdout, NULL, _IONBF, 0);
208
209
210         while ((a = getopt(argc, argv, "F:f:p")) != EOF)
211         {
212                 switch (a) {
213                 case 'f':
214                         filename = optarg;
215                         break;
216                 case 'p':
217                         do_proto = 1;
218                         break;
219                 }
220         }
221         StartLibCitadel(8);
222
223         if (filename == NULL) {
224                 printf("Filename requried! -f\n");
225                 return 1;
226         }
227         fd = open(filename, 0);
228         if (fd < 0) {
229                 printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
230                 return 1;
231         }
232         if (fstat(fd, &statbuf) == -1) {
233                 printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
234                 return 1;
235         }
236         MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
237         if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
238                 printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
239                 FreeStrBuf(&MimeBuf);
240                 return 1;
241         }
242         MimeLen = StrLength(MimeBuf);
243         MimeStr = SmashStrBuf(&MimeBuf);
244
245         memset(&ma, 0, sizeof(struct ma_info));
246
247         mime_parser(MimeStr, MimeStr + MimeLen,
248                     (do_proto ? *list_this_part : NULL),
249                     (do_proto ? *list_this_pref : NULL),
250                     (do_proto ? *list_this_suff : NULL),
251                     (void *)&ma, 1);
252
253
254         free(MimeStr);
255         return 0;
256 }