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? */
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 static 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 static 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 static 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 static void mime_download(char *name,
134 /* Silently go away if there's already a download open. */
138 ma = (struct ma_info *)cbuserdata;
140 if ((!IsEmptyStr(partnum) && (!strcasecmp(ma->printme, partnum)))) {
141 rc = write(STDOUT_FILENO, content, length);
148 * Callback function for mime parser that outputs a section all at once.
149 * We can specify the desired section by part number *or* content-id.
151 void mime_spew_section(char *name,
163 int *found_it = (int *)cbuserdata;
166 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
167 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
170 printf("%d %d|-1|%s|%s|%s\n",
177 fwrite(STDOUT, content, length);
186 int main(int argc, char* argv[])
190 char *filename = NULL;
201 setvbuf(stdout, NULL, _IONBF, 0);
202 memset(&ma, 0, sizeof(struct ma_info));
204 while ((a = getopt(argc, argv, "dpf:P:")) != EOF)
222 if (filename == NULL) {
223 printf("Filename requried! -f\n");
226 fd = open(filename, 0);
228 printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
231 if (fstat(fd, &statbuf) == -1) {
232 printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
235 MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
236 if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
237 printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
238 FreeStrBuf(&MimeBuf);
241 MimeLen = StrLength(MimeBuf);
242 MimeStr = SmashStrBuf(&MimeBuf);
244 if (ma.printme == NULL)
245 mime_parser(MimeStr, MimeStr + MimeLen,
246 (do_proto ? *list_this_part : NULL),
247 (do_proto ? *list_this_pref : NULL),
248 (do_proto ? *list_this_suff : NULL),
249 (void *)&ma, dont_decode);
251 mime_parser(MimeStr, MimeStr + MimeLen,
252 *mime_download, NULL, NULL, (void *)&ma, dont_decode);