16 #include "../lib/libcitadel.h"
18 /* shamelesly copied from msgbase.h */
20 int is_ma; /* Set to 1 if we are using this stuff */
21 int freeze; /* Freeze the replacement chain because we're
22 * digging through a subsection */
23 int did_print; /* One alternative has been displayed */
24 char chosen_part[128]; /* Which part of a m/a did we choose? */
25 int chosen_pref; /* Chosen part preference level (lower is better) */
26 int use_fo_hooks; /* Use fixed output hooks */
27 int dont_decode; /* should we call the decoder or not? */
32 * Callback function for mime parser that simply lists the part
34 void list_this_part(char *name,
48 ma = (struct ma_info *)cbuserdata;
50 printf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
63 * Callback function for multipart prefix
65 void list_this_pref(char *name,
79 ma = (struct ma_info *)cbuserdata;
80 if (!strcasecmp(cbtype, "multipart/alternative")) {
85 printf("pref=%s|%s\n", partnum, cbtype);
90 * Callback function for multipart sufffix
92 void list_this_suff(char *name,
106 ma = (struct ma_info *)cbuserdata;
107 if (ma->is_ma == 0) {
108 printf("suff=%s|%s\n", partnum, cbtype);
110 if (!strcasecmp(cbtype, "multipart/alternative")) {
117 * Callback function for mime parser that opens a section for downloading
119 void mime_download(char *name,
134 /* Silently go away if there's already a download open. * /
137 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
138 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
140 download_fp = STDOUT;
143 rv = fwrite(content, length, 1, download_fp);
144 fflush(CC->download_fp);
145 rewind(CC->download_fp);
147 OpenCmdResult(filename, cbtype);
154 * Callback function for mime parser that outputs a section all at once.
155 * We can specify the desired section by part number *or* content-id.
157 void mime_spew_section(char *name,
169 int *found_it = (int *)cbuserdata;
172 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
173 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
176 printf("%d %d|-1|%s|%s|%s\n",
183 fwrite(STDOUT, content, length);
192 int main(int argc, char* argv[])
196 char *filename = NULL;
207 setvbuf(stdout, NULL, _IONBF, 0);
210 while ((a = getopt(argc, argv, "dpf:")) != EOF)
226 if (filename == NULL) {
227 printf("Filename requried! -f\n");
230 fd = open(filename, 0);
232 printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
235 if (fstat(fd, &statbuf) == -1) {
236 printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
239 MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
240 if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
241 printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
242 FreeStrBuf(&MimeBuf);
245 MimeLen = StrLength(MimeBuf);
246 MimeStr = SmashStrBuf(&MimeBuf);
248 memset(&ma, 0, sizeof(struct ma_info));
250 mime_parser(MimeStr, MimeStr + MimeLen,
251 (do_proto ? *list_this_part : NULL),
252 (do_proto ? *list_this_pref : NULL),
253 (do_proto ? *list_this_suff : NULL),
254 (void *)&ma, dont_decode);