16 #include "stringbuf_test.h"
17 #include "../lib/libcitadel.h"
19 /* shamelesly copied from msgbase.h */
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? */
33 * Callback function for mime parser that simply lists the part
35 void list_this_part(char *name,
49 ma = (struct ma_info *)cbuserdata;
51 printf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
64 * Callback function for multipart prefix
66 void list_this_pref(char *name,
80 ma = (struct ma_info *)cbuserdata;
81 if (!strcasecmp(cbtype, "multipart/alternative")) {
86 printf("pref=%s|%s\n", partnum, cbtype);
91 * Callback function for multipart sufffix
93 void list_this_suff(char *name,
107 ma = (struct ma_info *)cbuserdata;
108 if (ma->is_ma == 0) {
109 printf("suff=%s|%s\n", partnum, cbtype);
111 if (!strcasecmp(cbtype, "multipart/alternative")) {
118 * Callback function for mime parser that opens a section for downloading
120 void mime_download(char *name,
135 /* Silently go away if there's already a download open. * /
138 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
139 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
141 download_fp = STDOUT;
144 rv = fwrite(content, length, 1, download_fp);
145 fflush(CC->download_fp);
146 rewind(CC->download_fp);
148 OpenCmdResult(filename, cbtype);
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.
158 void mime_spew_section(char *name,
170 int *found_it = (int *)cbuserdata;
173 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
174 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
177 printf("%d %d|-1|%s|%s|%s\n",
184 fwrite(STDOUT, content, length);
193 int main(int argc, char* argv[])
197 char *filename = NULL;
207 setvbuf(stdout, NULL, _IONBF, 0);
210 while ((a = getopt(argc, argv, "F:f:p")) != EOF)
223 if (filename == NULL) {
224 printf("Filename requried! -f\n");
227 fd = open(filename, 0);
229 printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
232 if (fstat(fd, &statbuf) == -1) {
233 printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
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);
242 MimeLen = StrLength(MimeBuf);
243 MimeStr = SmashStrBuf(&MimeBuf);
245 memset(&ma, 0, sizeof(struct ma_info));
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),