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