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