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 char *decoded = NULL;
142 size_t bytes_decoded;
143 rc = mime_decode_now (content,
149 printf("failed to decode content\n");
153 rc = write(STDOUT_FILENO, content, length);
156 rc = write(STDOUT_FILENO, decoded, bytes_decoded);
165 * Callback function for mime parser that outputs a section all at once.
166 * We can specify the desired section by part number *or* content-id.
168 void mime_spew_section(char *name,
180 int *found_it = (int *)cbuserdata;
183 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
184 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
187 printf("%d %d|-1|%s|%s|%s\n",
194 fwrite(STDOUT, content, length);
203 int main(int argc, char* argv[])
207 char *filename = NULL;
218 setvbuf(stdout, NULL, _IONBF, 0);
219 memset(&ma, 0, sizeof(struct ma_info));
221 while ((a = getopt(argc, argv, "dpf:P:")) != EOF)
238 if (filename == NULL) {
239 printf("Filename requried! -f\n");
242 fd = open(filename, 0);
244 printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
247 if (fstat(fd, &statbuf) == -1) {
248 printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
251 MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
252 if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
253 printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
254 FreeStrBuf(&MimeBuf);
257 MimeLen = StrLength(MimeBuf);
258 MimeStr = SmashStrBuf(&MimeBuf);
260 if (ma.printme == NULL)
261 mime_parser(MimeStr, MimeStr + MimeLen,
262 (do_proto ? *list_this_part : NULL),
263 (do_proto ? *list_this_pref : NULL),
264 (do_proto ? *list_this_suff : NULL),
265 (void *)&ma, dont_decode);
267 mime_parser(MimeStr, MimeStr + MimeLen,
268 *mime_download, NULL, NULL, (void *)&ma, dont_decode);