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