Mercurial > libpst
annotate src/readpst.c @ 52:034641c26ab9
code cleanup
author | carl |
---|---|
date | Thu, 31 Jan 2008 08:03:30 -0800 |
parents | fb3818370dd6 |
children | 7d5c637aaafb |
rev | line source |
---|---|
16 | 1 /*** |
2 * readpst.c | |
3 * Part of the LibPST project | |
4 * Written by David Smith | |
43 | 5 * dave.s@earthcorp.com |
16 | 6 */ |
48 | 7 #include "define.h" |
8 | |
16 | 9 #include <stdio.h> |
10 #include <stdlib.h> | |
11 #include <time.h> | |
12 #include <string.h> | |
13 #include <ctype.h> | |
14 #include <limits.h> | |
15 #include <errno.h> | |
43 | 16 #include "vbuf.h" |
16 | 17 |
18 #ifndef _WIN32 | |
19 # include <unistd.h> | |
20 # include <sys/stat.h> //mkdir | |
21 | |
22 // for reading of directory and clearing in function mk_seperate_dir | |
23 # include <sys/types.h> | |
24 # include <dirent.h> | |
25 #else | |
26 # include <direct.h> | |
27 # define chdir _chdir | |
28 # define int32_t __int32 | |
29 #endif | |
30 | |
31 #ifndef __GNUC__ | |
32 # include "XGetopt.h" | |
33 #endif | |
34 | |
35 #include "libstrfunc.h" // for base64_encoding | |
36 | |
37 #include "libpst.h" | |
38 #include "common.h" | |
39 #include "timeconv.h" | |
40 #include "lzfu.h" | |
41 #define OUTPUT_TEMPLATE "%s" | |
42 #define OUTPUT_KMAIL_DIR_TEMPLATE ".%s.directory" | |
43 #define KMAIL_INDEX ".%s.index" | |
25 | 44 #define SEP_MAIL_FILE_TEMPLATE "%i" /* "%09i" */ |
16 | 45 |
46 // max size of the c_time char*. It will store the date of the email | |
47 #define C_TIME_SIZE 500 | |
48 #define PERM_DIRS 0777 | |
49 | |
50 // macro used for creating directories | |
51 #ifndef WIN32 | |
52 #define D_MKDIR(x) mkdir(x, PERM_DIRS) | |
53 #else | |
54 #define D_MKDIR(x) mkdir(x) | |
55 #endif | |
56 struct file_ll { | |
43 | 57 char *name; |
58 char *dname; | |
59 FILE * output; | |
60 int32_t stored_count; | |
61 int32_t email_count; | |
62 int32_t skip_count; | |
63 int32_t type; | |
16 | 64 }; |
31 | 65 |
43 | 66 void process(pst_item *outeritem, pst_desc_ll *d_ptr); |
67 void write_email_body(FILE *f, char *body); | |
68 char* removeCR (char *c); | |
69 int usage(); | |
70 int version(); | |
71 char* mk_kmail_dir(char*); | |
72 int close_kmail_dir(); | |
73 char* mk_recurse_dir(char*); | |
74 int close_recurse_dir(); | |
75 char* mk_seperate_dir(char *dir); | |
76 int close_seperate_dir(); | |
77 int mk_seperate_file(struct file_ll *f); | |
78 char* my_stristr(char *haystack, char *needle); | |
79 void check_filename(char *fname); | |
80 char* skip_header_prologue(char *headers); | |
81 void write_separate_attachment(char f_name[], pst_item_attach* current_attach, int attach_num, pst_file* pst); | |
82 void write_inline_attachment(FILE* f_output, pst_item_attach* current_attach, char boundary[], pst_file* pst); | |
83 void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, int mode_MH, pst_file* pst, int save_rtf); | |
84 void write_vcard(FILE* f_output, pst_item_contact* contact, char comment[]); | |
85 void write_appointment(FILE* f_output, pst_item_appointment* appointment, | |
86 pst_item_email* email, FILETIME* create_date, FILETIME* modify_date); | |
87 void create_enter_dir(struct file_ll* f, pst_item *item); | |
88 void close_enter_dir(struct file_ll *f); | |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
33
diff
changeset
|
89 |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
33
diff
changeset
|
90 char* prog_name; |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
33
diff
changeset
|
91 char* output_dir = "."; |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
33
diff
changeset
|
92 char* kmail_chdir = NULL; |
16 | 93 // Normal mode just creates mbox format files in the current directory. Each file is named |
94 // the same as the folder's name that it represents | |
95 #define MODE_NORMAL 0 | |
96 // KMail mode creates a directory structure suitable for being used directly | |
97 // by the KMail application | |
98 #define MODE_KMAIL 1 | |
99 // recurse mode creates a directory structure like the PST file. Each directory | |
100 // contains only one file which stores the emails in mbox format. | |
101 #define MODE_RECURSE 2 | |
102 // seperate mode is similar directory structure to RECURSE. The emails are stored in | |
103 // seperate files, numbering from 1 upward. Attachments belonging to the emails are | |
104 // saved as email_no-filename (e.g. 1-samplefile.doc or 000001-Attachment2.zip) | |
105 #define MODE_SEPERATE 3 | |
43 | 106 // Decrypt the whole file (even the parts that aren't encrypted) and ralph it to stdout |
107 #define MODE_DECSPEW 4 | |
16 | 108 |
109 | |
110 // Output Normal just prints the standard information about what is going on | |
111 #define OUTPUT_NORMAL 0 | |
112 // Output Quiet is provided so that only errors are printed | |
113 #define OUTPUT_QUIET 1 | |
114 | |
115 // default mime-type for attachments that have a null mime-type | |
116 #define MIME_TYPE_DEFAULT "application/octet-stream" | |
117 | |
118 // output mode for contacts | |
119 #define CMODE_VCARD 0 | |
43 | 120 #define CMODE_LIST 1 |
16 | 121 |
122 // output settings for RTF bodies | |
123 // filename for the attachment | |
124 #define RTF_ATTACH_NAME "rtf-body.rtf" | |
125 // mime type for the attachment | |
126 #define RTF_ATTACH_TYPE "application/rtf" | |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
33
diff
changeset
|
127 |
39 | 128 // global settings |
129 int mode = MODE_NORMAL; | |
130 int mode_MH = 0; | |
131 int output_mode = OUTPUT_NORMAL; | |
132 int contact_mode = CMODE_VCARD; | |
133 int overwrite = 0; | |
134 int save_rtf_body = 1; | |
135 pst_file pstfile; | |
136 | |
137 | |
138 | |
139 void process(pst_item *outeritem, pst_desc_ll *d_ptr) | |
140 { | |
43 | 141 struct file_ll ff; |
142 pst_item *item = NULL; | |
39 | 143 |
43 | 144 DEBUG_ENT("process"); |
145 memset(&ff, 0, sizeof(ff)); | |
146 create_enter_dir(&ff, outeritem); | |
39 | 147 |
43 | 148 while (d_ptr) { |
149 DEBUG_MAIN(("main: New item record\n")); | |
150 if (!d_ptr->desc) { | |
151 DEBUG_WARN(("main: ERROR ?? item's desc record is NULL\n")); | |
152 ff.skip_count++; | |
153 } | |
154 else { | |
155 DEBUG_MAIN(("main: Desc Email ID %#x [d_ptr->id = %#x]\n", d_ptr->desc->id, d_ptr->id)); | |
39 | 156 |
46 | 157 item = pst_parse_item(&pstfile, d_ptr); |
43 | 158 DEBUG_MAIN(("main: About to process item\n")); |
159 if (item && item->email && item->email->subject && item->email->subject->subj) { | |
160 DEBUG_EMAIL(("item->email->subject = %p\n", item->email->subject)); | |
161 DEBUG_EMAIL(("item->email->subject->subj = %p\n", item->email->subject->subj)); | |
162 } | |
163 if (item) { | |
164 if (item->message_store) { | |
165 // there should only be one message_store, and we have already done it | |
166 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n")); | |
167 } | |
39 | 168 |
43 | 169 if (item->folder && d_ptr->child && strcasecmp(item->file_as, "Deleted Items")) { |
170 //if this is a non-empty folder other than deleted items, we want to recurse into it | |
171 if (output_mode != OUTPUT_QUIET) printf("Processing Folder \"%s\"\n", item->file_as); | |
172 process(item, d_ptr->child); | |
173 | |
174 } else if (item->contact && (item->type == PST_TYPE_CONTACT)) { | |
175 // deal with a contact | |
176 // write them to the file, one per line in this format | |
177 // Desc Name <email@address>\n | |
178 if (mode == MODE_SEPERATE) mk_seperate_file(&ff); | |
179 ff.email_count++; | |
180 DEBUG_MAIN(("main: Processing Contact\n")); | |
181 if (ff.type != PST_TYPE_CONTACT) { | |
182 DEBUG_MAIN(("main: I have a contact, but the folder isn't a contacts folder. Processing anyway\n")); | |
183 } | |
184 if (contact_mode == CMODE_VCARD) | |
185 write_vcard(ff.output, item->contact, item->comment); | |
186 else | |
187 fprintf(ff.output, "%s <%s>\n", item->contact->fullname, item->contact->address1); | |
39 | 188 |
43 | 189 } else if (item->email && (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_REPORT)) { |
190 if (mode == MODE_SEPERATE) mk_seperate_file(&ff); | |
191 ff.email_count++; | |
192 DEBUG_MAIN(("main: Processing Email\n")); | |
193 if ((ff.type != PST_TYPE_NOTE) && (ff.type != PST_TYPE_REPORT)) { | |
194 DEBUG_MAIN(("main: I have an email, but the folder isn't an email folder. Processing anyway\n")); | |
195 } | |
196 write_normal_email(ff.output, ff.name, item, mode, mode_MH, &pstfile, save_rtf_body); | |
39 | 197 |
43 | 198 } else if (item->journal && (item->type == PST_TYPE_JOURNAL)) { |
199 // deal with journal items | |
200 if (mode == MODE_SEPERATE) mk_seperate_file(&ff); | |
201 ff.email_count++; | |
202 DEBUG_MAIN(("main: Processing Journal Entry\n")); | |
203 if (ff.type != PST_TYPE_JOURNAL) { | |
204 DEBUG_MAIN(("main: I have a journal entry, but the folder isn't a journal folder. Processing anyway\n")); | |
205 } | |
206 fprintf(ff.output, "BEGIN:VJOURNAL\n"); | |
50 | 207 if (item->email && item->email->subject && item->email->subject->subj) |
43 | 208 fprintf(ff.output, "SUMMARY:%s\n", pst_rfc2426_escape(item->email->subject->subj)); |
50 | 209 if (item->email && item->email->body) |
43 | 210 fprintf(ff.output, "DESCRIPTION:%s\n", pst_rfc2426_escape(item->email->body)); |
211 if (item->journal->start) | |
212 fprintf(ff.output, "DTSTART;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(item->journal->start)); | |
213 fprintf(ff.output, "END:VJOURNAL\n\n"); | |
39 | 214 |
43 | 215 } else if (item->appointment && (item->type == PST_TYPE_APPOINTMENT)) { |
216 // deal with Calendar appointments | |
217 if (mode == MODE_SEPERATE) mk_seperate_file(&ff); | |
218 ff.email_count++; | |
219 DEBUG_MAIN(("main: Processing Appointment Entry\n")); | |
220 if (ff.type != PST_TYPE_APPOINTMENT) { | |
221 DEBUG_MAIN(("main: I have an appointment, but folder isn't specified as an appointment type. Processing...\n")); | |
222 } | |
223 write_appointment(ff.output, item->appointment, item->email, item->create_date, item->modify_date); | |
39 | 224 |
43 | 225 } else { |
226 ff.skip_count++; | |
227 DEBUG_MAIN(("main: Unknown item type. %i. Ascii1=\"%s\"\n", | |
228 item->type, item->ascii_type)); | |
229 } | |
46 | 230 pst_freeItem(item); |
43 | 231 } else { |
232 ff.skip_count++; | |
233 DEBUG_MAIN(("main: A NULL item was seen\n")); | |
234 } | |
235 d_ptr = d_ptr->next; | |
236 } | |
237 } | |
238 close_enter_dir(&ff); | |
239 DEBUG_RET(); | |
39 | 240 } |
241 | |
242 | |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
33
diff
changeset
|
243 |
16 | 244 int main(int argc, char** argv) { |
43 | 245 pst_item *item = NULL; |
246 pst_desc_ll *d_ptr; | |
247 char * fname = NULL; | |
48 | 248 char *d_log = NULL; |
43 | 249 int c,x; |
250 char *temp = NULL; //temporary char pointer | |
251 prog_name = argv[0]; | |
16 | 252 |
43 | 253 // command-line option handling |
254 while ((c = getopt(argc, argv, "bCc:d:hko:qrSMVw"))!= -1) { | |
255 switch (c) { | |
256 case 'b': | |
257 save_rtf_body = 0; | |
258 break; | |
259 case 'C': | |
260 mode = MODE_DECSPEW; | |
261 break; | |
262 case 'c': | |
263 if (optarg && optarg[0]=='v') | |
264 contact_mode=CMODE_VCARD; | |
265 else if (optarg && optarg[0]=='l') | |
266 contact_mode=CMODE_LIST; | |
267 else { | |
268 usage(); | |
269 exit(0); | |
270 } | |
271 break; | |
272 case 'd': | |
273 d_log = optarg; | |
274 break; | |
275 case 'h': | |
276 usage(); | |
277 exit(0); | |
278 break; | |
279 case 'V': | |
280 version(); | |
281 exit(0); | |
282 break; | |
283 case 'k': | |
284 mode = MODE_KMAIL; | |
285 break; | |
286 case 'M': | |
287 mode = MODE_SEPERATE; | |
288 mode_MH = 1; | |
289 break; | |
290 case 'o': | |
291 output_dir = optarg; | |
292 break; | |
293 case 'q': | |
294 output_mode = OUTPUT_QUIET; | |
295 break; | |
296 case 'r': | |
297 mode = MODE_RECURSE; | |
298 break; | |
299 case 'S': | |
300 mode = MODE_SEPERATE; | |
301 break; | |
302 case 'w': | |
303 overwrite = 1; | |
304 break; | |
305 default: | |
306 usage(); | |
307 exit(1); | |
308 break; | |
309 } | |
310 } | |
311 | |
312 if (argc > optind) { | |
313 fname = argv[optind]; | |
314 } else { | |
315 usage(); | |
316 exit(2); | |
317 } | |
318 | |
319 #ifdef DEBUG_ALL | |
320 // force a log file | |
321 if (!d_log) d_log = "readpst.log"; | |
322 #endif // defined DEBUG_ALL | |
323 DEBUG_INIT(d_log); | |
324 DEBUG_REGISTER_CLOSE(); | |
325 DEBUG_ENT("main"); | |
16 | 326 |
43 | 327 if (mode == MODE_DECSPEW) { |
328 FILE *fp; | |
329 char buf[1024]; | |
330 int l=0; | |
331 if (NULL == (fp = fopen(fname, "rb"))) { | |
332 fprintf(stderr, "Couldn't open file %s\n", fname ); | |
52 | 333 DEBUG_RET(); |
43 | 334 return 1; |
335 } | |
39 | 336 |
43 | 337 while (0 != ( l = fread( buf, 1, 1024, fp))) { |
46 | 338 if (0 != pst_decrypt( buf, l, PST_COMP_ENCRYPT)) |
339 fprintf(stderr, "pst_decrypt() failed (I'll try to continue)\n"); | |
16 | 340 |
43 | 341 if (l != fwrite( buf, 1, l, stdout)) { |
342 fprintf(stderr, "Couldn't output to stdout?\n"); | |
52 | 343 DEBUG_RET(); |
43 | 344 return 1; |
345 } | |
346 } | |
52 | 347 DEBUG_RET(); |
43 | 348 return 0; |
349 } | |
16 | 350 |
43 | 351 if (output_mode != OUTPUT_QUIET) printf("Opening PST file and indexes...\n"); |
352 | |
353 RET_DERROR(pst_open(&pstfile, fname, "r"), 1, ("Error opening File\n")); | |
354 RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n")); | |
355 | |
356 pst_load_extended_attributes(&pstfile); | |
16 | 357 |
43 | 358 if (chdir(output_dir)) { |
359 x = errno; | |
360 pst_close(&pstfile); | |
361 DEBUG_RET(); | |
362 DIE(("main: Cannot change to output dir %s: %s\n", output_dir, strerror(x))); | |
363 } | |
364 | |
365 if (output_mode != OUTPUT_QUIET) printf("About to start processing first record...\n"); | |
366 | |
367 d_ptr = pstfile.d_head; // first record is main record | |
46 | 368 item = pst_parse_item(&pstfile, d_ptr); |
43 | 369 if (!item || !item->message_store) { |
370 DEBUG_RET(); | |
371 DIE(("main: Could not get root record\n")); | |
372 } | |
16 | 373 |
43 | 374 // default the file_as to the same as the main filename if it doesn't exist |
375 if (!item->file_as) { | |
376 if (!(temp = strrchr(fname, '/'))) | |
377 if (!(temp = strrchr(fname, '\\'))) | |
378 temp = fname; | |
379 else | |
380 temp++; // get past the "\\" | |
381 else | |
382 temp++; // get past the "/" | |
383 item->file_as = (char*)xmalloc(strlen(temp)+1); | |
384 strcpy(item->file_as, temp); | |
385 DEBUG_MAIN(("file_as was blank, so am using %s\n", item->file_as)); | |
386 } | |
387 DEBUG_MAIN(("main: Root Folder Name: %s\n", item->file_as)); | |
16 | 388 |
43 | 389 d_ptr = pst_getTopOfFolders(&pstfile, item); |
390 if (!d_ptr) { | |
391 DEBUG_RET(); | |
392 DIE(("Top of folders record not found. Cannot continue\n")); | |
393 } | |
16 | 394 |
43 | 395 process(item, d_ptr->child); // do the children of TOPF |
46 | 396 pst_freeItem(item); |
43 | 397 pst_close(&pstfile); |
16 | 398 |
43 | 399 DEBUG_RET(); |
400 return 0; | |
16 | 401 } |
31 | 402 |
403 | |
16 | 404 void write_email_body(FILE *f, char *body) { |
43 | 405 char *n = body; |
406 // DEBUG_MAIN(("write_email_body(): \"%s\"\n", body)); | |
407 DEBUG_ENT("write_email_body"); | |
408 while (n) { | |
409 if (strncmp(body, "From ", 5) == 0) | |
410 fprintf(f, ">"); | |
411 if ((n = strchr(body, '\n'))) { | |
412 n++; | |
413 fwrite(body, n-body, 1, f); //write just a line | |
16 | 414 |
43 | 415 body = n; |
416 } | |
417 } | |
418 fwrite(body, strlen(body), 1, f); | |
419 DEBUG_RET(); | |
16 | 420 } |
31 | 421 |
422 | |
16 | 423 char *removeCR (char *c) { |
43 | 424 // converts /r/n to /n |
425 char *a, *b; | |
426 DEBUG_ENT("removeCR"); | |
427 a = b = c; | |
428 while (*a != '\0') { | |
429 *b = *a; | |
430 if (*a != '\r') | |
431 b++; | |
432 a++; | |
433 } | |
434 *b = '\0'; | |
435 DEBUG_RET(); | |
436 return c; | |
16 | 437 } |
31 | 438 |
439 | |
16 | 440 int usage() { |
43 | 441 DEBUG_ENT("usage"); |
442 version(); | |
443 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name); | |
444 printf("OPTIONS:\n"); | |
445 printf("\t-b\t- Don't save RTF-Body attachments\n"); | |
446 printf("\t-c[v|l]\t- Set the Contact output mode. -cv = VCard, -cl = EMail list\n"); | |
447 printf("\t-d <filename> \t- Debug to file. This is a binary log. Use readlog to print it\n"); | |
448 printf("\t-h\t- Help. This screen\n"); | |
449 printf("\t-k\t- KMail. Output in kmail format\n"); | |
450 printf("\t-M\t- MH. Write emails in the MH format\n"); | |
451 printf("\t-o <dirname>\t- Output Dir. Directory to write files to. CWD is changed *after* opening pst file\n"); | |
452 printf("\t-q\t- Quiet. Only print error messages\n"); | |
453 printf("\t-r\t- Recursive. Output in a recursive format\n"); | |
454 printf("\t-S\t- Seperate. Write emails in the seperate format\n"); | |
455 printf("\t-V\t- Version. Display program version\n"); | |
456 printf("\t-w\t- Overwrite any output mbox files\n"); | |
457 printf("\t-C\t- Decrypt the entire file and output on stdout (not typically useful)\n"); | |
458 DEBUG_RET(); | |
459 return 0; | |
16 | 460 } |
31 | 461 |
462 | |
16 | 463 int version() { |
43 | 464 DEBUG_ENT("version"); |
50 | 465 printf("ReadPST / LibPST v%s\n", VERSION); |
16 | 466 #if BYTE_ORDER == BIG_ENDIAN |
43 | 467 printf("Big Endian implementation being used.\n"); |
16 | 468 #elif BYTE_ORDER == LITTLE_ENDIAN |
43 | 469 printf("Little Endian implementation being used.\n"); |
16 | 470 #else |
471 # error "Byte order not supported by this library" | |
472 #endif | |
473 #ifdef __GNUC__ | |
43 | 474 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__); |
16 | 475 #endif |
43 | 476 DEBUG_RET(); |
477 return 0; | |
16 | 478 } |
31 | 479 |
480 | |
16 | 481 char *mk_kmail_dir(char *fname) { |
43 | 482 //change to that directory |
483 //make a directory based on OUTPUT_KMAIL_DIR_TEMPLATE | |
484 //allocate space for OUTPUT_TEMPLATE and form a char* with fname | |
485 //return that value | |
486 char *dir, *out_name, *index; | |
487 int x; | |
488 DEBUG_ENT("mk_kmail_dir"); | |
489 if (kmail_chdir && chdir(kmail_chdir)) { | |
490 x = errno; | |
491 DIE(("mk_kmail_dir: Cannot change to directory %s: %s\n", kmail_chdir, strerror(x))); | |
492 } | |
493 dir = malloc(strlen(fname)+strlen(OUTPUT_KMAIL_DIR_TEMPLATE)+1); | |
494 sprintf(dir, OUTPUT_KMAIL_DIR_TEMPLATE, fname); | |
495 check_filename(dir); | |
496 if (D_MKDIR(dir)) { | |
497 //error occured | |
498 if (errno != EEXIST) { | |
499 x = errno; | |
500 DIE(("mk_kmail_dir: Cannot create directory %s: %s\n", dir, strerror(x))); | |
501 } | |
502 } | |
503 kmail_chdir = realloc(kmail_chdir, strlen(dir)+1); | |
504 strcpy(kmail_chdir, dir); | |
505 free (dir); | |
16 | 506 |
43 | 507 //we should remove any existing indexes created by KMail, cause they might be different now |
508 index = malloc(strlen(fname)+strlen(KMAIL_INDEX)+1); | |
509 sprintf(index, KMAIL_INDEX, fname); | |
510 unlink(index); | |
511 free(index); | |
16 | 512 |
43 | 513 out_name = malloc(strlen(fname)+strlen(OUTPUT_TEMPLATE)+1); |
514 sprintf(out_name, OUTPUT_TEMPLATE, fname); | |
515 DEBUG_RET(); | |
516 return out_name; | |
16 | 517 } |
31 | 518 |
519 | |
16 | 520 int close_kmail_dir() { |
43 | 521 // change .. |
522 int x; | |
523 DEBUG_ENT("close_kmail_dir"); | |
524 if (kmail_chdir) { //only free kmail_chdir if not NULL. do not change directory | |
525 free(kmail_chdir); | |
526 kmail_chdir = NULL; | |
527 } else { | |
528 if (chdir("..")) { | |
529 x = errno; | |
530 DIE(("close_kmail_dir: Cannot move up dir (..): %s\n", strerror(x))); | |
531 } | |
532 } | |
533 DEBUG_RET(); | |
534 return 0; | |
16 | 535 } |
31 | 536 |
537 | |
16 | 538 // this will create a directory by that name, then make an mbox file inside |
539 // that dir. any subsequent dirs will be created by name, and they will | |
540 // contain mbox files | |
541 char *mk_recurse_dir(char *dir) { | |
43 | 542 int x; |
543 char *out_name; | |
544 DEBUG_ENT("mk_recurse_dir"); | |
545 check_filename(dir); | |
546 if (D_MKDIR (dir)) { | |
547 if (errno != EEXIST) { // not an error because it exists | |
548 x = errno; | |
549 DIE(("mk_recurse_dir: Cannot create directory %s: %s\n", dir, strerror(x))); | |
550 } | |
551 } | |
552 if (chdir (dir)) { | |
553 x = errno; | |
554 DIE(("mk_recurse_dir: Cannot change to directory %s: %s\n", dir, strerror(x))); | |
555 } | |
556 out_name = malloc(strlen("mbox")+1); | |
557 strcpy(out_name, "mbox"); | |
558 DEBUG_RET(); | |
559 return out_name; | |
16 | 560 } |
31 | 561 |
562 | |
16 | 563 int close_recurse_dir() { |
43 | 564 int x; |
565 DEBUG_ENT("close_recurse_dir"); | |
566 if (chdir("..")) { | |
567 x = errno; | |
568 DIE(("close_recurse_dir: Cannot go up dir (..): %s\n", strerror(x))); | |
569 } | |
570 DEBUG_RET(); | |
571 return 0; | |
16 | 572 } |
31 | 573 |
574 | |
39 | 575 char *mk_seperate_dir(char *dir) { |
43 | 576 size_t dirsize = strlen(dir) + 10; |
577 char dir_name[dirsize]; | |
578 int x = 0, y = 0; | |
16 | 579 |
46 | 580 DEBUG_ENT("mk_seperate_dir"); |
43 | 581 do { |
582 if (y == 0) | |
583 snprintf(dir_name, dirsize, "%s", dir); | |
584 else | |
585 snprintf(dir_name, dirsize, "%s" SEP_MAIL_FILE_TEMPLATE, dir, y); // enough for 9 digits allocated above | |
16 | 586 |
43 | 587 check_filename(dir_name); |
588 DEBUG_MAIN(("about to try creating %s\n", dir_name)); | |
589 if (D_MKDIR(dir_name)) { | |
590 if (errno != EEXIST) { // if there is an error, and it doesn't already exist | |
591 x = errno; | |
592 DIE(("mk_seperate_dir: Cannot create directory %s: %s\n", dir, strerror(x))); | |
593 } | |
594 } else { | |
595 break; | |
596 } | |
597 y++; | |
598 } while (overwrite == 0); | |
16 | 599 |
43 | 600 if (chdir(dir_name)) { |
601 x = errno; | |
602 DIE(("mk_recurse_dir: Cannot change to directory %s: %s\n", dir, strerror(x))); | |
603 } | |
16 | 604 |
43 | 605 if (overwrite) { |
606 // we should probably delete all files from this directory | |
16 | 607 #if !defined(WIN32) && !defined(__CYGWIN__) |
43 | 608 DIR * sdir = NULL; |
609 struct dirent *dirent = NULL; | |
610 struct stat filestat; | |
611 if (!(sdir = opendir("./"))) { | |
612 WARN(("mk_seperate_dir: Cannot open dir \"%s\" for deletion of old contents\n", "./")); | |
613 } else { | |
614 while ((dirent = readdir(sdir))) { | |
615 if (lstat(dirent->d_name, &filestat) != -1) | |
616 if (S_ISREG(filestat.st_mode)) { | |
617 if (unlink(dirent->d_name)) { | |
618 y = errno; | |
619 DIE(("mk_seperate_dir: unlink returned error on file %s: %s\n", dirent->d_name, strerror(y))); | |
620 } | |
621 } | |
622 } | |
623 } | |
26 | 624 #endif |
43 | 625 } |
16 | 626 |
43 | 627 // we don't return a filename here cause it isn't necessary. |
628 DEBUG_RET(); | |
629 return NULL; | |
16 | 630 } |
31 | 631 |
632 | |
16 | 633 int close_seperate_dir() { |
43 | 634 int x; |
635 DEBUG_ENT("close_seperate_dir"); | |
636 if (chdir("..")) { | |
637 x = errno; | |
638 DIE(("close_seperate_dir: Cannot go up dir (..): %s\n", strerror(x))); | |
639 } | |
640 DEBUG_RET(); | |
641 return 0; | |
16 | 642 } |
31 | 643 |
644 | |
16 | 645 int mk_seperate_file(struct file_ll *f) { |
43 | 646 const int name_offset = 1; |
647 DEBUG_ENT("mk_seperate_file"); | |
648 DEBUG_MAIN(("opening next file to save email\n")); | |
649 if (f->email_count > 999999999) { // bigger than nine 9's | |
650 DIE(("mk_seperate_file: The number of emails in this folder has become too high to handle")); | |
651 } | |
652 sprintf(f->name, SEP_MAIL_FILE_TEMPLATE, f->email_count + name_offset); | |
653 if (f->output) fclose(f->output); | |
654 f->output = NULL; | |
655 check_filename(f->name); | |
656 if (!(f->output = fopen(f->name, "w"))) { | |
657 DIE(("mk_seperate_file: Cannot open file to save email \"%s\"\n", f->name)); | |
658 } | |
659 DEBUG_RET(); | |
660 return 0; | |
16 | 661 } |
31 | 662 |
663 | |
16 | 664 char *my_stristr(char *haystack, char *needle) { |
43 | 665 // my_stristr varies from strstr in that its searches are case-insensitive |
666 char *x=haystack, *y=needle, *z = NULL; | |
667 DEBUG_ENT("my_stristr"); | |
52 | 668 if (!haystack || !needle) { |
669 DEBUG_RET(); | |
43 | 670 return NULL; |
52 | 671 } |
43 | 672 while (*y != '\0' && *x != '\0') { |
673 if (tolower(*y) == tolower(*x)) { | |
674 // move y on one | |
675 y++; | |
676 if (!z) { | |
677 z = x; // store first position in haystack where a match is made | |
678 } | |
679 } else { | |
680 y = needle; // reset y to the beginning of the needle | |
681 z = NULL; // reset the haystack storage point | |
682 } | |
683 x++; // advance the search in the haystack | |
684 } | |
685 DEBUG_RET(); | |
686 return z; | |
16 | 687 } |
31 | 688 |
689 | |
41
183ae993b9ad
security fix for potential buffer overrun in lz decompress
carl
parents:
39
diff
changeset
|
690 void check_filename(char *fname) { |
43 | 691 char *t = fname; |
692 DEBUG_ENT("check_filename"); | |
693 if (!t) { | |
694 DEBUG_RET(); | |
52 | 695 return; |
43 | 696 } |
697 while ((t = strpbrk(t, "/\\:"))) { | |
698 // while there are characters in the second string that we don't want | |
699 *t = '_'; //replace them with an underscore | |
700 } | |
701 DEBUG_RET(); | |
16 | 702 } |
31 | 703 |
704 | |
16 | 705 // The sole purpose of this function is to bypass the pseudo-header prologue |
706 // that Microsoft Outlook inserts at the beginning of the internet email | |
707 // headers for emails stored in their "Personal Folders" files. | |
708 char *skip_header_prologue(char *headers) { | |
43 | 709 const char *bad = "Microsoft Mail Internet Headers"; |
710 if ( strncmp(headers, bad, strlen(bad)) == 0 ) { | |
711 // Found the offensive header prologue | |
712 char *pc = strchr(headers, '\n'); | |
713 return pc + 1; | |
714 } | |
715 return headers; | |
16 | 716 } |
717 | |
31 | 718 |
25 | 719 void write_separate_attachment(char f_name[], pst_item_attach* current_attach, int attach_num, pst_file* pst) |
720 { | |
43 | 721 FILE *fp = NULL; |
722 int x = 0; | |
723 char *temp = NULL; | |
31 | 724 |
43 | 725 // If there is a long filename (filename2) use that, otherwise |
726 // use the 8.3 filename (filename1) | |
727 char *attach_filename = (current_attach->filename2) ? current_attach->filename2 | |
728 : current_attach->filename1; | |
46 | 729 DEBUG_ENT("write_separate_attachment"); |
25 | 730 |
43 | 731 check_filename(f_name); |
732 if (!attach_filename) { | |
733 // generate our own (dummy) filename for the attachement | |
734 temp = xmalloc(strlen(f_name)+15); | |
735 sprintf(temp, "%s-attach%i", f_name, attach_num); | |
736 } else { | |
737 // have an attachment name, make sure it's unique | |
738 temp = xmalloc(strlen(f_name)+strlen(attach_filename)+15); | |
739 do { | |
740 if (fp) fclose(fp); | |
741 if (x == 0) | |
742 sprintf(temp, "%s-%s", f_name, attach_filename); | |
743 else | |
744 sprintf(temp, "%s-%s-%i", f_name, attach_filename, x); | |
745 } while ((fp = fopen(temp, "r")) && ++x < 99999999); | |
746 if (x > 99999999) { | |
747 DIE(("error finding attachment name. exhausted possibilities to %s\n", temp)); | |
748 } | |
749 } | |
750 DEBUG_EMAIL(("Saving attachment to %s\n", temp)); | |
751 if (!(fp = fopen(temp, "w"))) { | |
752 WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp)); | |
753 } else { | |
754 if (current_attach->data) | |
755 fwrite(current_attach->data, 1, current_attach->size, fp); | |
756 else { | |
46 | 757 (void)pst_attach_to_file(pst, current_attach, fp); |
43 | 758 } |
759 fclose(fp); | |
760 } | |
761 if (temp) free(temp); | |
762 DEBUG_RET(); | |
25 | 763 } |
764 | |
31 | 765 |
25 | 766 void write_inline_attachment(FILE* f_output, pst_item_attach* current_attach, char boundary[], pst_file* pst) |
767 { | |
46 | 768 char *enc = NULL; // base64 encoded attachment |
43 | 769 DEBUG_ENT("write_inline_attachment"); |
770 DEBUG_EMAIL(("Attachment Size is %i\n", current_attach->size)); | |
771 DEBUG_EMAIL(("Attachment Pointer is %p\n", current_attach->data)); | |
772 if (current_attach->data) { | |
773 enc = base64_encode (current_attach->data, current_attach->size); | |
774 if (!enc) { | |
775 DEBUG_EMAIL(("ERROR base64_encode returned NULL. Must have failed\n")); | |
52 | 776 DEBUG_RET(); |
43 | 777 return; |
778 } | |
779 } | |
780 if (boundary) { | |
781 char *attach_filename; | |
782 fprintf(f_output, "\n--%s\n", boundary); | |
783 if (!current_attach->mimetype) { | |
784 fprintf(f_output, "Content-type: %s\n", MIME_TYPE_DEFAULT); | |
785 } else { | |
786 fprintf(f_output, "Content-type: %s\n", current_attach->mimetype); | |
787 } | |
788 fprintf(f_output, "Content-transfer-encoding: base64\n"); | |
789 // If there is a long filename (filename2) use that, otherwise | |
790 // use the 8.3 filename (filename1) | |
791 if (current_attach->filename2) { | |
48 | 792 attach_filename = current_attach->filename2; |
43 | 793 } else { |
48 | 794 attach_filename = current_attach->filename1; |
43 | 795 } |
796 if (!attach_filename) { | |
797 fprintf(f_output, "Content-Disposition: inline\n\n"); | |
798 } else { | |
799 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", attach_filename); | |
800 } | |
801 } | |
802 if (current_attach->data) { | |
803 fwrite(enc, 1, strlen(enc), f_output); | |
804 DEBUG_EMAIL(("Attachment Size after encoding is %i\n", strlen(enc))); | |
805 free(enc); // caught by valgrind | |
806 } else { | |
46 | 807 (void)pst_attach_to_file_base64(pst, current_attach, f_output); |
43 | 808 } |
809 fprintf(f_output, "\n\n"); | |
810 DEBUG_RET(); | |
25 | 811 } |
812 | |
31 | 813 |
814 void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, int mode_MH, pst_file* pst, int save_rtf) | |
25 | 815 { |
43 | 816 char *boundary = NULL; // the boundary marker between multipart sections |
817 int boundary_created = 0; // we have not (yet) created a new boundary | |
818 char *temp = NULL; | |
819 int attach_num, base64_body = 0; | |
820 time_t em_time; | |
821 char *c_time; | |
822 pst_item_attach* current_attach; | |
46 | 823 DEBUG_ENT("write_normal_email"); |
25 | 824 |
43 | 825 // convert the sent date if it exists, or set it to a fixed date |
826 if (item->email->sent_date) { | |
827 em_time = fileTimeToUnixTime(item->email->sent_date, 0); | |
828 c_time = ctime(&em_time); | |
829 if (c_time) | |
830 c_time[strlen(c_time)-1] = '\0'; //remove end \n | |
831 else | |
832 c_time = "Fri Dec 28 12:06:21 2001"; | |
833 } else | |
834 c_time= "Fri Dec 28 12:06:21 2001"; | |
25 | 835 |
43 | 836 // we will always look at the header to discover some stuff |
837 if (item->email->header ) { | |
838 char *b1, *b2; | |
839 // see if there is a boundary variable there | |
840 // this search MUST be made case insensitive (DONE). | |
841 // Also, we should check to find out if we are looking | |
842 // at the boundary associated with content-type, and that | |
843 // the content type really is multipart | |
25 | 844 |
43 | 845 removeCR(item->email->header); |
25 | 846 |
43 | 847 if ((b2 = my_stristr(item->email->header, "boundary="))) { |
848 int len; | |
849 b2 += strlen("boundary="); // move boundary to first char of marker | |
31 | 850 |
43 | 851 if (*b2 == '"') { |
852 b2++; | |
853 b1 = strchr(b2, '"'); // find terminating quote | |
854 } else { | |
855 b1 = b2; | |
856 while (isgraph(*b1)) // find first char that isn't part of boundary | |
857 b1++; | |
858 } | |
859 len = b1 - b2; | |
860 boundary = malloc(len+1); //malloc that length | |
861 strncpy(boundary, b2, len); // copy boundary to another variable | |
862 boundary[len] = '\0'; | |
863 b1 = b2 = boundary; | |
864 while (*b2 != '\0') { // remove any CRs and Tabs | |
865 if (*b2 != '\n' && *b2 != '\r' && *b2 != '\t') { | |
866 *b1 = *b2; | |
867 b1++; | |
868 } | |
869 b2++; | |
870 } | |
871 *b1 = '\0'; | |
31 | 872 |
43 | 873 DEBUG_EMAIL(("Found boundary of - %s\n", boundary)); |
874 } else { | |
875 DEBUG_EMAIL(("boundary not found in header\n")); | |
876 } | |
25 | 877 |
43 | 878 // also possible to set 7bit encoding detection here. |
879 if ((b2 = my_stristr(item->email->header, "Content-Transfer-Encoding:"))) { | |
880 if ((b2 = strchr(b2, ':'))) { | |
881 b2++; // skip to the : at the end of the string | |
31 | 882 |
43 | 883 while (*b2 == ' ' || *b2 == '\t') |
884 b2++; | |
885 if (pst_strincmp(b2, "base64", 6)==0) { | |
886 DEBUG_EMAIL(("body is base64 encoded\n")); | |
887 base64_body = 1; | |
888 } | |
889 } else { | |
890 DEBUG_WARN(("found a ':' during the my_stristr, but not after that..\n")); | |
891 } | |
892 } | |
893 } | |
25 | 894 |
43 | 895 if (!boundary && (item->attach || (item->email->body && item->email->htmlbody) |
896 || item->email->rtf_compressed || item->email->encrypted_body | |
897 || item->email->encrypted_htmlbody)) { | |
898 // we need to create a boundary here. | |
899 DEBUG_EMAIL(("must create own boundary. oh dear.\n")); | |
900 boundary = malloc(50 * sizeof(char)); // allow 50 chars for boundary | |
901 boundary[0] = '\0'; | |
902 sprintf(boundary, "--boundary-LibPST-iamunique-%i_-_-", rand()); | |
903 DEBUG_EMAIL(("created boundary is %s\n", boundary)); | |
904 boundary_created = 1; | |
905 } | |
25 | 906 |
43 | 907 DEBUG_EMAIL(("About to print Header\n")); |
31 | 908 |
43 | 909 if (item && item->email && item->email->subject && item->email->subject->subj) { |
910 DEBUG_EMAIL(("item->email->subject->subj = %s\n", item->email->subject->subj)); | |
911 } | |
31 | 912 |
43 | 913 if (item->email->header) { |
914 int len; | |
915 char *soh = NULL; // real start of headers. | |
31 | 916 |
43 | 917 // some of the headers we get from the file are not properly defined. |
918 // they can contain some email stuff too. We will cut off the header | |
919 // when we see a \n\n or \r\n\r\n | |
920 removeCR(item->email->header); | |
921 temp = strstr(item->email->header, "\n\n"); | |
25 | 922 |
43 | 923 if (temp) { |
924 DEBUG_EMAIL(("Found body text in header\n")); | |
925 temp[1] = '\0'; // stop after first \n | |
926 } | |
31 | 927 |
43 | 928 // Now, write out the header... |
929 soh = skip_header_prologue(item->email->header); | |
930 if (mode != MODE_SEPERATE) { | |
931 // don't put rubbish in if we are doing seperate | |
932 if (strncmp(soh, "X-From_: ", 9) == 0 ) { | |
933 fputs("From ", f_output); | |
934 soh += 9; | |
935 } else | |
936 fprintf(f_output, "From \"%s\" %s\n", item->email->outlook_sender_name, c_time); | |
937 } | |
938 fprintf(f_output, "%s", soh); | |
939 len = strlen(soh); | |
940 if (!len || (soh[len-1] != '\n')) fprintf(f_output, "\n"); | |
31 | 941 |
43 | 942 } else { |
943 //make up our own header! | |
944 if (mode != MODE_SEPERATE) { | |
945 // don't want this first line for this mode | |
946 if (item->email->outlook_sender_name) { | |
947 temp = item->email->outlook_sender_name; | |
948 } else { | |
949 temp = "(readpst_null)"; | |
950 } | |
951 fprintf(f_output, "From \"%s\" %s\n", temp, c_time); | |
952 } | |
31 | 953 |
43 | 954 temp = item->email->outlook_sender; |
955 if (!temp) temp = ""; | |
956 fprintf(f_output, "From: \"%s\" <%s>\n", item->email->outlook_sender_name, temp); | |
31 | 957 |
43 | 958 if (item->email->subject) { |
959 fprintf(f_output, "Subject: %s\n", item->email->subject->subj); | |
960 } else { | |
961 fprintf(f_output, "Subject: \n"); | |
962 } | |
31 | 963 |
43 | 964 fprintf(f_output, "To: %s\n", item->email->sentto_address); |
965 if (item->email->cc_address) { | |
966 fprintf(f_output, "Cc: %s\n", item->email->cc_address); | |
967 } | |
31 | 968 |
43 | 969 if (item->email->sent_date) { |
970 c_time = (char*) xmalloc(C_TIME_SIZE); | |
971 strftime(c_time, C_TIME_SIZE, "%a, %d %b %Y %H:%M:%S %z", gmtime(&em_time)); | |
972 fprintf(f_output, "Date: %s\n", c_time); | |
973 free(c_time); | |
974 } | |
975 } | |
25 | 976 |
43 | 977 fprintf(f_output, "MIME-Version: 1.0\n"); |
978 if (boundary && boundary_created) { | |
979 // if we created the boundary, then it has NOT already been printed | |
980 // in the headers above. | |
981 if (item->attach) { | |
982 // write the boundary stuff if we have attachments | |
983 fprintf(f_output, "Content-type: multipart/mixed;\n\tboundary=\"%s\"\n", boundary); | |
984 } else if (boundary) { | |
985 // else if we have multipart/alternative then tell it so | |
986 fprintf(f_output, "Content-type: multipart/alternative;\n\tboundary=\"%s\"\n", boundary); | |
987 } else if (item->email->htmlbody) { | |
988 fprintf(f_output, "Content-type: text/html\n"); | |
989 } | |
990 } | |
991 fprintf(f_output, "\n"); // start the body | |
992 DEBUG_EMAIL(("About to print Body\n")); | |
25 | 993 |
43 | 994 if (item->email->body) { |
995 if (boundary) { | |
996 fprintf(f_output, "\n--%s\n", boundary); | |
997 fprintf(f_output, "Content-type: text/plain\n"); | |
998 if (base64_body) | |
999 fprintf(f_output, "Content-Transfer-Encoding: base64\n"); | |
1000 fprintf(f_output, "\n"); | |
1001 } | |
1002 removeCR(item->email->body); | |
1003 if (base64_body) { | |
1004 char *enc = base64_encode(item->email->body, strlen(item->email->body)); | |
1005 if (enc) { | |
1006 write_email_body(f_output, enc); | |
1007 free(enc); | |
1008 } | |
1009 } | |
1010 else { | |
1011 write_email_body(f_output, item->email->body); | |
1012 } | |
1013 } | |
31 | 1014 |
43 | 1015 if (item->email->htmlbody) { |
1016 if (boundary) { | |
1017 fprintf(f_output, "\n--%s\n", boundary); | |
1018 fprintf(f_output, "Content-type: text/html\n"); | |
1019 if (base64_body) fprintf(f_output, "Content-Transfer-Encoding: base64\n"); | |
1020 fprintf(f_output, "\n"); | |
1021 } | |
1022 removeCR(item->email->htmlbody); | |
1023 if (base64_body) { | |
1024 char *enc = base64_encode(item->email->htmlbody, strlen(item->email->htmlbody)); | |
1025 if (enc) { | |
1026 write_email_body(f_output, enc); | |
1027 free(enc); | |
1028 } | |
1029 } | |
1030 else { | |
1031 write_email_body(f_output, item->email->htmlbody); | |
1032 } | |
1033 } | |
25 | 1034 |
43 | 1035 if (item->email->rtf_compressed && save_rtf) { |
1036 //int32_t tester; | |
1037 DEBUG_EMAIL(("Adding RTF body as attachment\n")); | |
1038 current_attach = (pst_item_attach*)xmalloc(sizeof(pst_item_attach)); | |
1039 memset(current_attach, 0, sizeof(pst_item_attach)); | |
1040 current_attach->next = item->attach; | |
1041 item->attach = current_attach; | |
1042 current_attach->data = lzfu_decompress(item->email->rtf_compressed, item->email->rtf_compressed_size, ¤t_attach->size); | |
1043 current_attach->filename2 = xmalloc(strlen(RTF_ATTACH_NAME)+2); | |
1044 strcpy(current_attach->filename2, RTF_ATTACH_NAME); | |
1045 current_attach->mimetype = xmalloc(strlen(RTF_ATTACH_TYPE)+2); | |
1046 strcpy(current_attach->mimetype, RTF_ATTACH_TYPE); | |
1047 //memcpy(&tester, item->email->rtf_compressed+sizeof(int32_t), sizeof(int32_t)); | |
1048 //LE32_CPU(tester); | |
1049 //printf("lz produced %d bytes, rtf claims %d bytes\n", current_attach->size, tester); | |
1050 } | |
31 | 1051 |
43 | 1052 if (item->email->encrypted_body || item->email->encrypted_htmlbody) { |
1053 // if either the body or htmlbody is encrypted, add them as attachments | |
1054 if (item->email->encrypted_body) { | |
1055 DEBUG_EMAIL(("Adding Encrypted Body as attachment\n")); | |
1056 current_attach = (pst_item_attach*) xmalloc(sizeof(pst_item_attach)); | |
1057 memset(current_attach, 0, sizeof(pst_item_attach)); | |
1058 current_attach->next = item->attach; | |
1059 item->attach = current_attach; | |
1060 current_attach->data = item->email->encrypted_body; | |
1061 current_attach->size = item->email->encrypted_body_size; | |
1062 item->email->encrypted_body = NULL; | |
1063 } | |
31 | 1064 |
43 | 1065 if (item->email->encrypted_htmlbody) { |
1066 DEBUG_EMAIL(("Adding encrypted HTML body as attachment\n")); | |
1067 current_attach = (pst_item_attach*) xmalloc(sizeof(pst_item_attach)); | |
1068 memset(current_attach, 0, sizeof(pst_item_attach)); | |
1069 current_attach->next = item->attach; | |
1070 item->attach = current_attach; | |
1071 current_attach->data = item->email->encrypted_htmlbody; | |
1072 current_attach->size = item->email->encrypted_htmlbody_size; | |
1073 item->email->encrypted_htmlbody = NULL; | |
1074 } | |
1075 write_email_body(f_output, "The body of this email is encrypted. This isn't supported yet, but the body is now an attachment\n"); | |
1076 } | |
31 | 1077 |
43 | 1078 // attachments |
1079 attach_num = 0; | |
1080 for (current_attach = item->attach; current_attach; current_attach = current_attach->next) { | |
1081 DEBUG_EMAIL(("Attempting Attachment encoding\n")); | |
1082 if (!current_attach->data) { | |
1083 DEBUG_EMAIL(("Data of attachment is NULL!. Size is supposed to be %i\n", current_attach->size)); | |
1084 } | |
1085 if (mode == MODE_SEPERATE && !mode_MH) | |
1086 write_separate_attachment(f_name, current_attach, ++attach_num, pst); | |
1087 else | |
1088 write_inline_attachment(f_output, current_attach, boundary, pst); | |
1089 } | |
1090 if (mode != MODE_SEPERATE) { /* do not add a boundary after the last attachment for mode_MH */ | |
1091 DEBUG_EMAIL(("Writing buffer between emails\n")); | |
1092 if (boundary) fprintf(f_output, "\n--%s--\n", boundary); | |
1093 fprintf(f_output, "\n\n"); | |
1094 } | |
1095 if (boundary) free (boundary); | |
1096 DEBUG_RET(); | |
25 | 1097 } |
1098 | |
31 | 1099 |
25 | 1100 void write_vcard(FILE* f_output, pst_item_contact* contact, char comment[]) |
1101 { | |
43 | 1102 // We can only call rfc escape once per printf, since the second call |
1103 // may free the buffer returned by the first call. | |
1104 // I had tried to place those into a single printf - Carl. | |
39 | 1105 |
43 | 1106 DEBUG_ENT("write_vcard"); |
50 | 1107 // the specification I am following is (hopefully) RFC2426 vCard Mime Directory Profile |
43 | 1108 fprintf(f_output, "BEGIN:VCARD\n"); |
1109 fprintf(f_output, "FN:%s\n", pst_rfc2426_escape(contact->fullname)); | |
39 | 1110 |
43 | 1111 //fprintf(f_output, "N:%s;%s;%s;%s;%s\n", |
1112 fprintf(f_output, "N:%s;", (!contact->surname) ? "" : pst_rfc2426_escape(contact->surname)); | |
1113 fprintf(f_output, "%s;", (!contact->first_name) ? "" : pst_rfc2426_escape(contact->first_name)); | |
1114 fprintf(f_output, "%s;", (!contact->middle_name) ? "" : pst_rfc2426_escape(contact->middle_name)); | |
1115 fprintf(f_output, "%s;", (!contact->display_name_prefix) ? "" : pst_rfc2426_escape(contact->display_name_prefix)); | |
1116 fprintf(f_output, "%s\n", (!contact->suffix) ? "" : pst_rfc2426_escape(contact->suffix)); | |
39 | 1117 |
43 | 1118 if (contact->nickname) |
1119 fprintf(f_output, "NICKNAME:%s\n", pst_rfc2426_escape(contact->nickname)); | |
1120 if (contact->address1) | |
1121 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address1)); | |
1122 if (contact->address2) | |
1123 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address2)); | |
1124 if (contact->address3) | |
1125 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address3)); | |
1126 if (contact->birthday) | |
1127 fprintf(f_output, "BDAY:%s\n", pst_rfc2425_datetime_format(contact->birthday)); | |
39 | 1128 |
43 | 1129 if (contact->home_address) { |
1130 //fprintf(f_output, "ADR;TYPE=home:%s;%s;%s;%s;%s;%s;%s\n", | |
1131 fprintf(f_output, "ADR;TYPE=home:%s;", (!contact->home_po_box) ? "" : pst_rfc2426_escape(contact->home_po_box)); | |
1132 fprintf(f_output, "%s;", ""); // extended Address | |
1133 fprintf(f_output, "%s;", (!contact->home_street) ? "" : pst_rfc2426_escape(contact->home_street)); | |
1134 fprintf(f_output, "%s;", (!contact->home_city) ? "" : pst_rfc2426_escape(contact->home_city)); | |
1135 fprintf(f_output, "%s;", (!contact->home_state) ? "" : pst_rfc2426_escape(contact->home_state)); | |
1136 fprintf(f_output, "%s;", (!contact->home_postal_code) ? "" : pst_rfc2426_escape(contact->home_postal_code)); | |
1137 fprintf(f_output, "%s\n", (!contact->home_country) ? "" : pst_rfc2426_escape(contact->home_country)); | |
1138 fprintf(f_output, "LABEL;TYPE=home:%s\n", pst_rfc2426_escape(contact->home_address)); | |
1139 } | |
39 | 1140 |
43 | 1141 if (contact->business_address) { |
1142 //fprintf(f_output, "ADR;TYPE=work:%s;%s;%s;%s;%s;%s;%s\n", | |
1143 fprintf(f_output, "ADR;TYPE=work:%s;", (!contact->business_po_box) ? "" : pst_rfc2426_escape(contact->business_po_box)); | |
1144 fprintf(f_output, "%s;", ""); // extended Address | |
1145 fprintf(f_output, "%s;", (!contact->business_street) ? "" : pst_rfc2426_escape(contact->business_street)); | |
1146 fprintf(f_output, "%s;", (!contact->business_city) ? "" : pst_rfc2426_escape(contact->business_city)); | |
1147 fprintf(f_output, "%s;", (!contact->business_state) ? "" : pst_rfc2426_escape(contact->business_state)); | |
1148 fprintf(f_output, "%s;", (!contact->business_postal_code) ? "" : pst_rfc2426_escape(contact->business_postal_code)); | |
1149 fprintf(f_output, "%s\n", (!contact->business_country) ? "" : pst_rfc2426_escape(contact->business_country)); | |
1150 fprintf(f_output, "LABEL;TYPE=work:%s\n", pst_rfc2426_escape(contact->business_address)); | |
1151 } | |
39 | 1152 |
43 | 1153 if (contact->other_address) { |
1154 //fprintf(f_output, "ADR;TYPE=postal:%s;%s;%s;%s;%s;%s;%s\n", | |
1155 fprintf(f_output, "ADR;TYPE=postal:%s;",(!contact->other_po_box) ? "" : pst_rfc2426_escape(contact->other_po_box)); | |
1156 fprintf(f_output, "%s;", ""); // extended Address | |
1157 fprintf(f_output, "%s;", (!contact->other_street) ? "" : pst_rfc2426_escape(contact->other_street)); | |
1158 fprintf(f_output, "%s;", (!contact->other_city) ? "" : pst_rfc2426_escape(contact->other_city)); | |
1159 fprintf(f_output, "%s;", (!contact->other_state) ? "" : pst_rfc2426_escape(contact->other_state)); | |
1160 fprintf(f_output, "%s;", (!contact->other_postal_code) ? "" : pst_rfc2426_escape(contact->other_postal_code)); | |
1161 fprintf(f_output, "%s\n", (!contact->other_country) ? "" : pst_rfc2426_escape(contact->other_country)); | |
1162 fprintf(f_output, "LABEL;TYPE=postal:%s\n", pst_rfc2426_escape(contact->other_address)); | |
1163 } | |
39 | 1164 |
43 | 1165 if (contact->business_fax) fprintf(f_output, "TEL;TYPE=work,fax:%s\n", pst_rfc2426_escape(contact->business_fax)); |
1166 if (contact->business_phone) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", pst_rfc2426_escape(contact->business_phone)); | |
1167 if (contact->business_phone2) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", pst_rfc2426_escape(contact->business_phone2)); | |
1168 if (contact->car_phone) fprintf(f_output, "TEL;TYPE=car,voice:%s\n", pst_rfc2426_escape(contact->car_phone)); | |
1169 if (contact->home_fax) fprintf(f_output, "TEL;TYPE=home,fax:%s\n", pst_rfc2426_escape(contact->home_fax)); | |
1170 if (contact->home_phone) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", pst_rfc2426_escape(contact->home_phone)); | |
1171 if (contact->home_phone2) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", pst_rfc2426_escape(contact->home_phone2)); | |
1172 if (contact->isdn_phone) fprintf(f_output, "TEL;TYPE=isdn:%s\n", pst_rfc2426_escape(contact->isdn_phone)); | |
1173 if (contact->mobile_phone) fprintf(f_output, "TEL;TYPE=cell,voice:%s\n", pst_rfc2426_escape(contact->mobile_phone)); | |
1174 if (contact->other_phone) fprintf(f_output, "TEL;TYPE=msg:%s\n", pst_rfc2426_escape(contact->other_phone)); | |
1175 if (contact->pager_phone) fprintf(f_output, "TEL;TYPE=pager:%s\n", pst_rfc2426_escape(contact->pager_phone)); | |
1176 if (contact->primary_fax) fprintf(f_output, "TEL;TYPE=fax,pref:%s\n", pst_rfc2426_escape(contact->primary_fax)); | |
1177 if (contact->primary_phone) fprintf(f_output, "TEL;TYPE=phone,pref:%s\n", pst_rfc2426_escape(contact->primary_phone)); | |
1178 if (contact->radio_phone) fprintf(f_output, "TEL;TYPE=pcs:%s\n", pst_rfc2426_escape(contact->radio_phone)); | |
1179 if (contact->telex) fprintf(f_output, "TEL;TYPE=bbs:%s\n", pst_rfc2426_escape(contact->telex)); | |
1180 if (contact->job_title) fprintf(f_output, "TITLE:%s\n", pst_rfc2426_escape(contact->job_title)); | |
1181 if (contact->profession) fprintf(f_output, "ROLE:%s\n", pst_rfc2426_escape(contact->profession)); | |
1182 if (contact->assistant_name || contact->assistant_phone) { | |
1183 fprintf(f_output, "AGENT:BEGIN:VCARD\n"); | |
1184 if (contact->assistant_name) fprintf(f_output, "FN:%s\n", pst_rfc2426_escape(contact->assistant_name)); | |
1185 if (contact->assistant_phone) fprintf(f_output, "TEL:%s\n", pst_rfc2426_escape(contact->assistant_phone)); | |
1186 } | |
1187 if (contact->company_name) fprintf(f_output, "ORG:%s\n", pst_rfc2426_escape(contact->company_name)); | |
1188 if (comment) fprintf(f_output, "NOTE:%s\n", pst_rfc2426_escape(comment)); | |
25 | 1189 |
43 | 1190 fprintf(f_output, "VERSION: 3.0\n"); |
1191 fprintf(f_output, "END:VCARD\n\n"); | |
1192 DEBUG_RET(); | |
25 | 1193 } |
1194 | |
31 | 1195 |
25 | 1196 void write_appointment(FILE* f_output, pst_item_appointment* appointment, |
43 | 1197 pst_item_email* email, FILETIME* create_date, FILETIME* modify_date) |
25 | 1198 { |
43 | 1199 fprintf(f_output, "BEGIN:VEVENT\n"); |
1200 if (create_date) | |
1201 fprintf(f_output, "CREATED:%s\n", | |
1202 pst_rfc2445_datetime_format(create_date)); | |
1203 if (modify_date) | |
1204 fprintf(f_output, "LAST-MOD:%s\n", | |
1205 pst_rfc2445_datetime_format(modify_date)); | |
1206 if (email && email->subject) | |
1207 fprintf(f_output, "SUMMARY:%s\n", | |
1208 pst_rfc2426_escape(email->subject->subj)); | |
1209 if (email && email->body) | |
1210 fprintf(f_output, "DESCRIPTION:%s\n", | |
1211 pst_rfc2426_escape(email->body)); | |
1212 if (appointment && appointment->start) | |
1213 fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n", | |
1214 pst_rfc2445_datetime_format(appointment->start)); | |
1215 if (appointment && appointment->end) | |
1216 fprintf(f_output, "DTEND;VALUE=DATE-TIME:%s\n", | |
1217 pst_rfc2445_datetime_format(appointment->end)); | |
1218 if (appointment && appointment->location) | |
1219 fprintf(f_output, "LOCATION:%s\n", | |
1220 pst_rfc2426_escape(appointment->location)); | |
1221 if (appointment) { | |
1222 switch (appointment->showas) { | |
50 | 1223 case PST_FREEBUSY_TENTATIVE: |
1224 fprintf(f_output, "STATUS:TENTATIVE\n"); | |
1225 break; | |
1226 case PST_FREEBUSY_FREE: | |
1227 // mark as transparent and as confirmed | |
1228 fprintf(f_output, "TRANSP:TRANSPARENT\n"); | |
1229 case PST_FREEBUSY_BUSY: | |
1230 case PST_FREEBUSY_OUT_OF_OFFICE: | |
1231 fprintf(f_output, "STATUS:CONFIRMED\n"); | |
1232 break; | |
43 | 1233 } |
1234 switch (appointment->label) { | |
50 | 1235 case PST_APP_LABEL_NONE: |
1236 fprintf(f_output, "CATEGORIES:NONE\n"); | |
1237 break; | |
1238 case PST_APP_LABEL_IMPORTANT: | |
1239 fprintf(f_output, "CATEGORIES:IMPORTANT\n"); | |
1240 break; | |
1241 case PST_APP_LABEL_BUSINESS: | |
1242 fprintf(f_output, "CATEGORIES:BUSINESS\n"); | |
1243 break; | |
1244 case PST_APP_LABEL_PERSONAL: | |
1245 fprintf(f_output, "CATEGORIES:PERSONAL\n"); | |
1246 break; | |
1247 case PST_APP_LABEL_VACATION: | |
1248 fprintf(f_output, "CATEGORIES:VACATION\n"); | |
1249 break; | |
1250 case PST_APP_LABEL_MUST_ATTEND: | |
1251 fprintf(f_output, "CATEGORIES:MUST-ATTEND\n"); | |
1252 break; | |
1253 case PST_APP_LABEL_TRAVEL_REQ: | |
1254 fprintf(f_output, "CATEGORIES:TRAVEL-REQUIRED\n"); | |
1255 break; | |
1256 case PST_APP_LABEL_NEEDS_PREP: | |
1257 fprintf(f_output, "CATEGORIES:NEEDS-PREPARATION\n"); | |
1258 break; | |
1259 case PST_APP_LABEL_BIRTHDAY: | |
1260 fprintf(f_output, "CATEGORIES:BIRTHDAY\n"); | |
1261 break; | |
1262 case PST_APP_LABEL_ANNIVERSARY: | |
1263 fprintf(f_output, "CATEGORIES:ANNIVERSARY\n"); | |
1264 break; | |
1265 case PST_APP_LABEL_PHONE_CALL: | |
1266 fprintf(f_output, "CATEGORIES:PHONE-CALL\n"); | |
1267 break; | |
43 | 1268 } |
1269 } | |
1270 fprintf(f_output, "END:VEVENT\n\n"); | |
25 | 1271 } |
1272 | |
31 | 1273 |
39 | 1274 void create_enter_dir(struct file_ll* f, pst_item *item) |
25 | 1275 { |
43 | 1276 f->email_count = 0; |
1277 f->skip_count = 0; | |
1278 f->type = item->type; | |
1279 f->stored_count = (item->folder) ? item->folder->email_count : 0; | |
39 | 1280 |
43 | 1281 DEBUG_ENT("create_enter_dir"); |
1282 if (mode == MODE_KMAIL) | |
1283 f->name = mk_kmail_dir(item->file_as); //create directory and form filename | |
1284 else if (mode == MODE_RECURSE) | |
1285 f->name = mk_recurse_dir(item->file_as); | |
1286 else if (mode == MODE_SEPERATE) { | |
1287 // do similar stuff to recurse here. | |
1288 mk_seperate_dir(item->file_as); | |
1289 f->name = (char*) xmalloc(10); | |
1290 memset(f->name, 0, 10); | |
1291 // sprintf(f->name, SEP_MAIL_FILE_TEMPLATE, f->email_count); | |
1292 } else { | |
1293 f->name = (char*) xmalloc(strlen(item->file_as)+strlen(OUTPUT_TEMPLATE)+1); | |
1294 sprintf(f->name, OUTPUT_TEMPLATE, item->file_as); | |
1295 } | |
25 | 1296 |
43 | 1297 f->dname = (char*) xmalloc(strlen(item->file_as)+1); |
1298 strcpy(f->dname, item->file_as); | |
25 | 1299 |
43 | 1300 if (overwrite != 1) { |
1301 int x = 0; | |
1302 char *temp = (char*) xmalloc (strlen(f->name)+10); //enough room for 10 digits | |
25 | 1303 |
43 | 1304 sprintf(temp, "%s", f->name); |
1305 check_filename(temp); | |
1306 while ((f->output = fopen(temp, "r"))) { | |
1307 DEBUG_MAIN(("need to increase filename because one already exists with that name\n")); | |
1308 DEBUG_MAIN(("- increasing it to %s%d\n", f->name, x)); | |
1309 x++; | |
1310 sprintf(temp, "%s%08d", f->name, x); | |
1311 DEBUG_MAIN(("- trying \"%s\"\n", f->name)); | |
1312 if (x == 99999999) { | |
1313 DIE(("create_enter_dir: Why can I not create a folder %s? I have tried %i extensions...\n", f->name, x)); | |
1314 } | |
1315 fclose(f->output); | |
1316 } | |
1317 if (x > 0) { //then the f->name should change | |
1318 free (f->name); | |
1319 f->name = temp; | |
1320 } else { | |
1321 free(temp); | |
1322 } | |
1323 } | |
25 | 1324 |
43 | 1325 DEBUG_MAIN(("f->name = %s\nitem->folder_name = %s\n", f->name, item->file_as)); |
1326 if (mode != MODE_SEPERATE) { | |
1327 check_filename(f->name); | |
1328 if (!(f->output = fopen(f->name, "w"))) { | |
1329 DIE(("create_enter_dir: Could not open file \"%s\" for write\n", f->name)); | |
1330 } | |
1331 } | |
1332 DEBUG_RET(); | |
25 | 1333 } |
1334 | |
39 | 1335 |
1336 void close_enter_dir(struct file_ll *f) | |
1337 { | |
43 | 1338 DEBUG_MAIN(("main: Email Count for folder %s is %i\n", f->dname, f->email_count)); |
1339 if (output_mode != OUTPUT_QUIET) | |
1340 printf("\t\"%s\" - %i items done, skipped %i, should have been %i\n", | |
1341 f->dname, f->email_count, f->skip_count, f->stored_count); | |
1342 if (f->output) fclose(f->output); | |
1343 free(f->name); | |
1344 free(f->dname); | |
39 | 1345 |
43 | 1346 if (mode == MODE_KMAIL) |
1347 close_kmail_dir(); | |
1348 else if (mode == MODE_RECURSE) | |
1349 close_recurse_dir(); | |
1350 else if (mode == MODE_SEPERATE) | |
1351 close_seperate_dir(); | |
39 | 1352 } |
1353 |