00001
00002
00003
00004
00005
00006
00007
00008 #include "define.h"
00009 #include "lzfu.h"
00010 #include "msg.h"
00011
00012 #define OUTPUT_TEMPLATE "%s.%s"
00013 #define OUTPUT_KMAIL_DIR_TEMPLATE ".%s.directory"
00014 #define KMAIL_INDEX "../.%s.index"
00015 #define SEP_MAIL_FILE_TEMPLATE "%i%s"
00016
00017
00018 #define C_TIME_SIZE 500
00019
00020 struct file_ll {
00021 char *name[PST_TYPE_MAX];
00022 char *dname;
00023 FILE * output[PST_TYPE_MAX];
00024 int32_t stored_count;
00025 int32_t item_count;
00026 int32_t skip_count;
00027 };
00028
00029 int grim_reaper();
00030 pid_t try_fork(char* folder);
00031 void process(pst_item *outeritem, pst_desc_tree *d_ptr);
00032 void write_email_body(FILE *f, char *body);
00033 void removeCR(char *c);
00034 void usage();
00035 void version();
00036 void mk_kmail_dir(char* fname);
00037 int close_kmail_dir();
00038 void mk_recurse_dir(char* dir);
00039 int close_recurse_dir();
00040 void mk_separate_dir(char *dir);
00041 int close_separate_dir();
00042 void mk_separate_file(struct file_ll *f, int32_t t, char *extension, int openit);
00043 void close_separate_file(struct file_ll *f);
00044 char* my_stristr(char *haystack, char *needle);
00045 void check_filename(char *fname);
00046 int acceptable_ext(pst_item_attach* attach);
00047 void write_separate_attachment(char f_name[], pst_item_attach* attach, int attach_num, pst_file* pst);
00048 void write_embedded_message(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pf, int save_rtf, char** extra_mime_headers);
00049 void write_inline_attachment(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pst);
00050 int valid_headers(char *header);
00051 void header_has_field(char *header, char *field, int *flag);
00052 void header_get_subfield(char *field, const char *subfield, char *body_subfield, size_t size_subfield);
00053 char* header_get_field(char *header, char *field);
00054 char* header_end_field(char *field);
00055 void header_strip_field(char *header, char *field);
00056 int test_base64(char *body, size_t len);
00057 void find_html_charset(char *html, char *charset, size_t charsetlen);
00058 void find_rfc822_headers(char** extra_mime_headers);
00059 void write_body_part(FILE* f_output, pst_string *body, char *mime, char *charset, char *boundary, pst_file* pst);
00060 void write_schedule_part_data(FILE* f_output, pst_item* item, const char* sender, const char* method);
00061 void write_schedule_part(FILE* f_output, pst_item* item, const char* sender, const char* boundary);
00062 void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, int mode_MH, pst_file* pst, int save_rtf, int embedding, char** extra_mime_headers);
00063 void write_vcard(FILE* f_output, pst_item *item, pst_item_contact* contact, char comment[]);
00064 int write_extra_categories(FILE* f_output, pst_item* item);
00065 void write_journal(FILE* f_output, pst_item* item);
00066 void write_appointment(FILE* f_output, pst_item *item);
00067 void create_enter_dir(struct file_ll* f, pst_item *item);
00068 void close_enter_dir(struct file_ll *f);
00069
00070 const char* prog_name;
00071 char* output_dir = ".";
00072
00073
00074
00075 #define MODE_NORMAL 0
00076
00077
00078
00079 #define MODE_KMAIL 1
00080
00081
00082
00083 #define MODE_RECURSE 2
00084
00085
00086
00087
00088 #define MODE_SEPARATE 3
00089
00090
00091
00092 #define OUTPUT_NORMAL 0
00093
00094
00095 #define OUTPUT_QUIET 1
00096
00097
00098 #define MIME_TYPE_DEFAULT "application/octet-stream"
00099 #define RFC822 "message/rfc822"
00100
00101
00102 #define CMODE_VCARD 0
00103 #define CMODE_LIST 1
00104
00105
00106 #define DMODE_EXCLUDE 0
00107 #define DMODE_INCLUDE 1
00108
00109
00110 #define OTMODE_EMAIL 1
00111 #define OTMODE_APPOINTMENT 2
00112 #define OTMODE_JOURNAL 4
00113 #define OTMODE_CONTACT 8
00114
00115
00116
00117 #define RTF_ATTACH_NAME "rtf-body.rtf"
00118
00119 #define RTF_ATTACH_TYPE "application/rtf"
00120
00121
00122 int mode = MODE_NORMAL;
00123 int mode_MH = 0;
00124 int mode_EX = 0;
00125 int mode_MSG = 0;
00126 int mode_thunder = 0;
00127 int output_mode = OUTPUT_NORMAL;
00128 int contact_mode = CMODE_VCARD;
00129 int deleted_mode = DMODE_EXCLUDE;
00130 int output_type_mode = 0xff;
00131 int contact_mode_specified = 0;
00132 int overwrite = 0;
00133 int prefer_utf8 = 0;
00134 int save_rtf_body = 1;
00135 int file_name_len = 10;
00136 pst_file pstfile;
00137 regex_t meta_charset_pattern;
00138 char* default_charset = NULL;
00139 char* acceptable_extensions = NULL;
00140
00141 int number_processors = 1;
00142 int max_children = 0;
00143 int max_child_specified = 0;
00144 int active_children;
00145 pid_t* child_processes;
00146
00147 #ifdef HAVE_SEMAPHORE_H
00148 int shared_memory_id;
00149 sem_t* global_children = NULL;
00150 sem_t* output_mutex = NULL;
00151 #endif
00152
00153
00154 int grim_reaper(int waitall)
00155 {
00156 int available = 0;
00157 #ifdef HAVE_FORK
00158 #ifdef HAVE_SEMAPHORE_H
00159 if (global_children) {
00160
00161
00162
00163 int i,j;
00164 for (i=0; i<active_children; i++) {
00165 int status;
00166 pid_t child = child_processes[i];
00167 pid_t ch = waitpid(child, &status, ((waitall) ? 0 : WNOHANG));
00168 if (ch == child) {
00169
00170
00171
00172
00173
00174
00175 if (WIFSIGNALED(status)) {
00176 int sig = WTERMSIG(status);
00177 DEBUG_INFO(("Process %d terminated with signal %d\n", child, sig));
00178
00179
00180 }
00181 if (status != 0) {
00182 exit(status);
00183 }
00184
00185 for (j=i; j<active_children-1; j++) {
00186 child_processes[j] = child_processes[j+1];
00187 }
00188 active_children--;
00189 i--;
00190 }
00191 }
00192 sem_getvalue(global_children, &available);
00193
00194
00195 }
00196 #endif
00197 #endif
00198 return available;
00199 }
00200
00201
00202 pid_t try_fork(char *folder)
00203 {
00204 #ifdef HAVE_FORK
00205 #ifdef HAVE_SEMAPHORE_H
00206 int available = grim_reaper(0);
00207
00208 if (available && active_children < max_children) {
00209 sem_wait(global_children);
00210 pid_t child = fork();
00211 if (child < 0) {
00212
00213 return 0;
00214 }
00215 else if (child == 0) {
00216
00217 active_children = 0;
00218 memset(child_processes, 0, sizeof(pid_t) * max_children);
00219 pst_reopen(&pstfile);
00220 }
00221 else {
00222
00223
00224
00225
00226 child_processes[active_children++] = child;
00227 }
00228 return child;
00229 }
00230 else {
00231 return 0;
00232 }
00233 #endif
00234 #endif
00235 return 0;
00236 }
00237
00238
00239 void process(pst_item *outeritem, pst_desc_tree *d_ptr)
00240 {
00241 struct file_ll ff;
00242 pst_item *item = NULL;
00243
00244 DEBUG_ENT("process");
00245 create_enter_dir(&ff, outeritem);
00246
00247 for (; d_ptr; d_ptr = d_ptr->next) {
00248 DEBUG_INFO(("New item record\n"));
00249 if (!d_ptr->desc) {
00250 ff.skip_count++;
00251 DEBUG_WARN(("ERROR item's desc record is NULL\n"));
00252 continue;
00253 }
00254 DEBUG_INFO(("Desc Email ID %#"PRIx64" [d_ptr->d_id = %#"PRIx64"]\n", d_ptr->desc->i_id, d_ptr->d_id));
00255
00256 item = pst_parse_item(&pstfile, d_ptr, NULL);
00257 DEBUG_INFO(("About to process item\n"));
00258
00259 if (!item) {
00260 ff.skip_count++;
00261 DEBUG_INFO(("A NULL item was seen\n"));
00262 continue;
00263 }
00264
00265 if (item->subject.str) {
00266 DEBUG_INFO(("item->subject = %s\n", item->subject.str));
00267 }
00268
00269 if (item->folder && item->file_as.str) {
00270 DEBUG_INFO(("Processing Folder \"%s\"\n", item->file_as.str));
00271 if (output_mode != OUTPUT_QUIET) {
00272 pst_debug_lock();
00273 printf("Processing Folder \"%s\"\n", item->file_as.str);
00274 fflush(stdout);
00275 pst_debug_unlock();
00276 }
00277 ff.item_count++;
00278 if (d_ptr->child && (deleted_mode == DMODE_INCLUDE || strcasecmp(item->file_as.str, "Deleted Items"))) {
00279
00280 pid_t parent = getpid();
00281 pid_t child = try_fork(item->file_as.str);
00282 if (child == 0) {
00283
00284 pid_t me = getpid();
00285 process(item, d_ptr->child);
00286 #ifdef HAVE_FORK
00287 #ifdef HAVE_SEMAPHORE_H
00288 if (me != parent) {
00289
00290
00291
00292 sem_post(global_children);
00293 grim_reaper(1);
00294 exit(0);
00295 }
00296 #endif
00297 #endif
00298 }
00299 }
00300
00301 } else if (item->contact && (item->type == PST_TYPE_CONTACT)) {
00302 DEBUG_INFO(("Processing Contact\n"));
00303 if (!(output_type_mode & OTMODE_CONTACT)) {
00304 ff.skip_count++;
00305 DEBUG_INFO(("skipping contact: not in output type list\n"));
00306 }
00307 else {
00308 ff.item_count++;
00309 if (mode == MODE_SEPARATE) mk_separate_file(&ff, PST_TYPE_CONTACT, (mode_EX) ? ".vcf" : "", 1);
00310 if (contact_mode == CMODE_VCARD) {
00311 pst_convert_utf8_null(item, &item->comment);
00312 write_vcard(ff.output[PST_TYPE_CONTACT], item, item->contact, item->comment.str);
00313 }
00314 else {
00315 pst_convert_utf8(item, &item->contact->fullname);
00316 pst_convert_utf8(item, &item->contact->address1);
00317 fprintf(ff.output[PST_TYPE_CONTACT], "%s <%s>\n", item->contact->fullname.str, item->contact->address1.str);
00318 }
00319 if (mode == MODE_SEPARATE) close_separate_file(&ff);
00320 }
00321
00322 } else if (item->email && ((item->type == PST_TYPE_NOTE) || (item->type == PST_TYPE_SCHEDULE) || (item->type == PST_TYPE_REPORT))) {
00323 DEBUG_INFO(("Processing Email\n"));
00324 if (!(output_type_mode & OTMODE_EMAIL)) {
00325 ff.skip_count++;
00326 DEBUG_INFO(("skipping email: not in output type list\n"));
00327 }
00328 else {
00329 char *extra_mime_headers = NULL;
00330 ff.item_count++;
00331 if (mode == MODE_SEPARATE) {
00332
00333 pid_t parent = getpid();
00334 pid_t child = try_fork(item->file_as.str);
00335 if (child == 0) {
00336
00337 pid_t me = getpid();
00338 mk_separate_file(&ff, PST_TYPE_NOTE, (mode_EX) ? ".eml" : "", 1);
00339 write_normal_email(ff.output[PST_TYPE_NOTE], ff.name[PST_TYPE_NOTE], item, mode, mode_MH, &pstfile, save_rtf_body, PST_TYPE_NOTE, &extra_mime_headers);
00340 close_separate_file(&ff);
00341 if (mode_MSG) {
00342 mk_separate_file(&ff, PST_TYPE_NOTE, ".msg", 0);
00343 write_msg_email(ff.name[PST_TYPE_NOTE], item, &pstfile);
00344 }
00345 #ifdef HAVE_FORK
00346 #ifdef HAVE_SEMAPHORE_H
00347 if (me != parent) {
00348
00349
00350
00351 sem_post(global_children);
00352 grim_reaper(1);
00353 exit(0);
00354 }
00355 #endif
00356 #endif
00357 }
00358 }
00359 else {
00360
00361 write_normal_email(ff.output[PST_TYPE_NOTE], ff.name[PST_TYPE_NOTE], item, mode, mode_MH, &pstfile, save_rtf_body, 0, &extra_mime_headers);
00362 }
00363 }
00364
00365 } else if (item->journal && (item->type == PST_TYPE_JOURNAL)) {
00366 DEBUG_INFO(("Processing Journal Entry\n"));
00367 if (!(output_type_mode & OTMODE_JOURNAL)) {
00368 ff.skip_count++;
00369 DEBUG_INFO(("skipping journal entry: not in output type list\n"));
00370 }
00371 else {
00372 ff.item_count++;
00373 if (mode == MODE_SEPARATE) mk_separate_file(&ff, PST_TYPE_JOURNAL, (mode_EX) ? ".ics" : "", 1);
00374 write_journal(ff.output[PST_TYPE_JOURNAL], item);
00375 fprintf(ff.output[PST_TYPE_JOURNAL], "\n");
00376 if (mode == MODE_SEPARATE) close_separate_file(&ff);
00377 }
00378
00379 } else if (item->appointment && (item->type == PST_TYPE_APPOINTMENT)) {
00380 DEBUG_INFO(("Processing Appointment Entry\n"));
00381 if (!(output_type_mode & OTMODE_APPOINTMENT)) {
00382 ff.skip_count++;
00383 DEBUG_INFO(("skipping appointment: not in output type list\n"));
00384 }
00385 else {
00386 ff.item_count++;
00387 if (mode == MODE_SEPARATE) mk_separate_file(&ff, PST_TYPE_APPOINTMENT, (mode_EX) ? ".ics" : "", 1);
00388 write_schedule_part_data(ff.output[PST_TYPE_APPOINTMENT], item, NULL, NULL);
00389 fprintf(ff.output[PST_TYPE_APPOINTMENT], "\n");
00390 if (mode == MODE_SEPARATE) close_separate_file(&ff);
00391 }
00392
00393 } else if (item->message_store) {
00394
00395 ff.skip_count++;
00396 DEBUG_WARN(("item with message store content, type %i %s, skipping it\n", item->type, item->ascii_type));
00397
00398 } else {
00399 ff.skip_count++;
00400 DEBUG_WARN(("Unknown item type %i (%s) name (%s)\n",
00401 item->type, item->ascii_type, item->file_as.str));
00402 }
00403 pst_freeItem(item);
00404 }
00405 close_enter_dir(&ff);
00406 DEBUG_RET();
00407 }
00408
00409
00410
00411 int main(int argc, char* const* argv) {
00412 pst_item *item = NULL;
00413 pst_desc_tree *d_ptr;
00414 char * fname = NULL;
00415 char *d_log = NULL;
00416 int c,x;
00417 char *temp = NULL;
00418 prog_name = argv[0];
00419
00420 time_t now = time(NULL);
00421 srand((unsigned)now);
00422
00423 if (regcomp(&meta_charset_pattern, "<meta[^>]*content=\"[^>]*charset=([^>\";]*)[\";]", REG_ICASE | REG_EXTENDED)) {
00424 printf("cannot compile regex pattern to find content charset in html bodies\n");
00425 exit(3);
00426 }
00427
00428
00429 while ((c = getopt(argc, argv, "a:bC:c:Dd:emhj:kMo:qrSt:uVwL:8"))!= -1) {
00430 switch (c) {
00431 case 'a':
00432 if (optarg) {
00433 int n = strlen(optarg);
00434 acceptable_extensions = (char*)pst_malloc(n+2);
00435 strcpy(acceptable_extensions, optarg);
00436 acceptable_extensions[n+1] = '\0';
00437 char *p = acceptable_extensions;
00438 while (*p) {
00439 if (*p == ',') *p = '\0';
00440 p++;
00441 }
00442 }
00443 break;
00444 case 'b':
00445 save_rtf_body = 0;
00446 break;
00447 case 'C':
00448 if (optarg) {
00449 default_charset = optarg;
00450 }
00451 else {
00452 usage();
00453 exit(0);
00454 }
00455 break;
00456 case 'c':
00457 if (optarg && optarg[0]=='v') {
00458 contact_mode=CMODE_VCARD;
00459 contact_mode_specified = 1;
00460 }
00461 else if (optarg && optarg[0]=='l') {
00462 contact_mode=CMODE_LIST;
00463 contact_mode_specified = 1;
00464 }
00465 else {
00466 usage();
00467 exit(0);
00468 }
00469 break;
00470 case 'D':
00471 deleted_mode = DMODE_INCLUDE;
00472 break;
00473 case 'd':
00474 d_log = optarg;
00475 break;
00476 case 'h':
00477 usage();
00478 exit(0);
00479 break;
00480 case 'j':
00481 max_children = atoi(optarg);
00482 max_child_specified = 1;
00483 break;
00484 case 'k':
00485 mode = MODE_KMAIL;
00486 break;
00487 case 'M':
00488 mode = MODE_SEPARATE;
00489 mode_MH = 1;
00490 mode_EX = 0;
00491 mode_MSG = 0;
00492 break;
00493 case 'e':
00494 mode = MODE_SEPARATE;
00495 mode_MH = 1;
00496 mode_EX = 1;
00497 mode_MSG = 0;
00498 file_name_len = 14;
00499 break;
00500 case 'L':
00501 pst_debug_setlevel(atoi(optarg));
00502 break;
00503 case 'm':
00504 mode = MODE_SEPARATE;
00505 mode_MH = 1;
00506 mode_EX = 1;
00507 mode_MSG = 1;
00508 file_name_len = 14;
00509 break;
00510 case 'o':
00511 output_dir = optarg;
00512 break;
00513 case 'q':
00514 output_mode = OUTPUT_QUIET;
00515 break;
00516 case 'r':
00517 mode = MODE_RECURSE;
00518 mode_thunder = 0;
00519 break;
00520 case 'S':
00521 mode = MODE_SEPARATE;
00522 mode_MH = 0;
00523 mode_EX = 0;
00524 mode_MSG = 0;
00525 break;
00526 case 't':
00527
00528 if (!optarg) {
00529 usage();
00530 exit(0);
00531 }
00532 temp = optarg;
00533 output_type_mode = 0;
00534 while (*temp > 0) {
00535 switch (temp[0]) {
00536 case 'e':
00537 output_type_mode |= OTMODE_EMAIL;
00538 break;
00539 case 'a':
00540 output_type_mode |= OTMODE_APPOINTMENT;
00541 break;
00542 case 'j':
00543 output_type_mode |= OTMODE_JOURNAL;
00544 break;
00545 case 'c':
00546 output_type_mode |= OTMODE_CONTACT;
00547 break;
00548 default:
00549 usage();
00550 exit(0);
00551 break;
00552 }
00553 temp++;
00554 }
00555 break;
00556 case 'u':
00557 mode = MODE_RECURSE;
00558 mode_thunder = 1;
00559 break;
00560 case 'V':
00561 version();
00562 exit(0);
00563 break;
00564 case 'w':
00565 overwrite = 1;
00566 break;
00567 case '8':
00568 prefer_utf8 = 1;
00569 break;
00570 default:
00571 usage();
00572 exit(1);
00573 break;
00574 }
00575 }
00576
00577 if (argc > optind) {
00578 fname = argv[optind];
00579 } else {
00580 usage();
00581 exit(2);
00582 }
00583
00584 #ifdef _SC_NPROCESSORS_ONLN
00585 number_processors = sysconf(_SC_NPROCESSORS_ONLN);
00586 #endif
00587 max_children = (max_child_specified) ? max_children : number_processors * 4;
00588 active_children = 0;
00589 child_processes = (pid_t *)pst_malloc(sizeof(pid_t) * max_children);
00590 memset(child_processes, 0, sizeof(pid_t) * max_children);
00591
00592 #ifdef HAVE_SEMAPHORE_H
00593 if (max_children) {
00594 shared_memory_id = shmget(IPC_PRIVATE, sizeof(sem_t)*2, 0777);
00595 if (shared_memory_id >= 0) {
00596 global_children = (sem_t *)shmat(shared_memory_id, NULL, 0);
00597 if (global_children == (sem_t *)-1) global_children = NULL;
00598 if (global_children) {
00599 output_mutex = &(global_children[1]);
00600 sem_init(global_children, 1, max_children);
00601 sem_init(output_mutex, 1, 1);
00602 }
00603 shmctl(shared_memory_id, IPC_RMID, NULL);
00604 }
00605 }
00606 #endif
00607
00608 #ifdef DEBUG_ALL
00609
00610 if (!d_log) d_log = "readpst.log";
00611 #endif // defined DEBUG_ALL
00612 #ifdef HAVE_SEMAPHORE_H
00613 DEBUG_INIT(d_log, output_mutex);
00614 #else
00615 DEBUG_INIT(d_log, NULL);
00616 #endif
00617 DEBUG_ENT("main");
00618
00619 if (output_mode != OUTPUT_QUIET) printf("Opening PST file and indexes...\n");
00620 RET_DERROR(pst_open(&pstfile, fname, default_charset), 1, ("Error opening File\n"));
00621 RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
00622
00623 pst_load_extended_attributes(&pstfile);
00624
00625 if (chdir(output_dir)) {
00626 x = errno;
00627 pst_close(&pstfile);
00628 DEBUG_RET();
00629 DIE(("Cannot change to output dir %s: %s\n", output_dir, strerror(x)));
00630 }
00631
00632 d_ptr = pstfile.d_head;
00633 item = pst_parse_item(&pstfile, d_ptr, NULL);
00634 if (!item || !item->message_store) {
00635 DEBUG_RET();
00636 DIE(("Could not get root record\n"));
00637 }
00638
00639
00640 if (!item->file_as.str) {
00641 if (!(temp = strrchr(fname, '/')))
00642 if (!(temp = strrchr(fname, '\\')))
00643 temp = fname;
00644 else
00645 temp++;
00646 else
00647 temp++;
00648 item->file_as.str = (char*)pst_malloc(strlen(temp)+1);
00649 strcpy(item->file_as.str, temp);
00650 item->file_as.is_utf8 = 1;
00651 DEBUG_INFO(("file_as was blank, so am using %s\n", item->file_as.str));
00652 }
00653 DEBUG_INFO(("Root Folder Name: %s\n", item->file_as.str));
00654
00655 d_ptr = pst_getTopOfFolders(&pstfile, item);
00656 if (!d_ptr) {
00657 DEBUG_RET();
00658 DIE(("Top of folders record not found. Cannot continue\n"));
00659 }
00660
00661 process(item, d_ptr->child);
00662 grim_reaper(1);
00663
00664 pst_freeItem(item);
00665 pst_close(&pstfile);
00666 DEBUG_RET();
00667
00668 #ifdef HAVE_SEMAPHORE_H
00669 if (global_children) {
00670 sem_destroy(global_children);
00671 sem_destroy(output_mutex);
00672 shmdt(global_children);
00673 }
00674 #endif
00675
00676 regfree(&meta_charset_pattern);
00677 return 0;
00678 }
00679
00680
00681 void write_email_body(FILE *f, char *body) {
00682 char *n = body;
00683 DEBUG_ENT("write_email_body");
00684 if (mode != MODE_SEPARATE) {
00685 while (n) {
00686 char *p = body;
00687 while (*p == '>') p++;
00688 if (strncmp(p, "From ", 5) == 0) fprintf(f, ">");
00689 if ((n = strchr(body, '\n'))) {
00690 n++;
00691 pst_fwrite(body, n-body, 1, f);
00692 body = n;
00693 }
00694 }
00695 }
00696 pst_fwrite(body, strlen(body), 1, f);
00697 DEBUG_RET();
00698 }
00699
00700
00701 void removeCR (char *c) {
00702
00703 char *a, *b;
00704 DEBUG_ENT("removeCR");
00705 a = b = c;
00706 while (*a != '\0') {
00707 *b = *a;
00708 if (*a != '\r') b++;
00709 a++;
00710 }
00711 *b = '\0';
00712 DEBUG_RET();
00713 }
00714
00715
00716 void usage() {
00717 DEBUG_ENT("usage");
00718 version();
00719 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
00720 printf("OPTIONS:\n");
00721 printf("\t-V\t- Version. Display program version\n");
00722 printf("\t-C charset\t- character set for items with an unspecified character set\n");
00723 printf("\t-D\t- Include deleted items in output\n");
00724 printf("\t-L <level> \t- Set debug level; 1=debug,2=info,3=warn.\n");
00725 printf("\t-M\t- Write emails in the MH (rfc822) format\n");
00726 printf("\t-S\t- Separate. Write emails in the separate format\n");
00727 printf("\t-a <attachment-extension-list>\t- Discard any attachment without an extension on the list\n");
00728 printf("\t-b\t- Don't save RTF-Body attachments\n");
00729 printf("\t-c[v|l]\t- Set the Contact output mode. -cv = VCard, -cl = EMail list\n");
00730 printf("\t-d <filename> \t- Debug to file.\n");
00731 printf("\t-e\t- As with -M, but include extensions on output files\n");
00732 printf("\t-h\t- Help. This screen\n");
00733 printf("\t-j <integer>\t- Number of parallel jobs to run\n");
00734 printf("\t-k\t- KMail. Output in kmail format\n");
00735 printf("\t-m\t- As with -e, but write .msg files also\n");
00736 printf("\t-o <dirname>\t- Output directory to write files to. CWD is changed *after* opening pst file\n");
00737 printf("\t-q\t- Quiet. Only print error messages\n");
00738 printf("\t-r\t- Recursive. Output in a recursive format\n");
00739 printf("\t-t[eajc]\t- Set the output type list. e = email, a = attachment, j = journal, c = contact\n");
00740 printf("\t-u\t- Thunderbird mode. Write two extra .size and .type files\n");
00741 printf("\t-w\t- Overwrite any output mbox files\n");
00742 printf("\t-8\t- Output bodies in UTF-8, rather than original encoding, if UTF-8 version is available\n");
00743 printf("\n");
00744 printf("Only one of -M -S -e -k -m -r should be specified\n");
00745 DEBUG_RET();
00746 }
00747
00748
00749 void version() {
00750 DEBUG_ENT("version");
00751 printf("ReadPST / LibPST v%s\n", VERSION);
00752 #if BYTE_ORDER == BIG_ENDIAN
00753 printf("Big Endian implementation being used.\n");
00754 #elif BYTE_ORDER == LITTLE_ENDIAN
00755 printf("Little Endian implementation being used.\n");
00756 #else
00757 # error "Byte order not supported by this library"
00758 #endif
00759 DEBUG_RET();
00760 }
00761
00762
00763 void mk_kmail_dir(char *fname) {
00764
00765
00766 char *dir, *index;
00767 int x;
00768 DEBUG_ENT("mk_kmail_dir");
00769 dir = pst_malloc(strlen(fname)+strlen(OUTPUT_KMAIL_DIR_TEMPLATE)+1);
00770 sprintf(dir, OUTPUT_KMAIL_DIR_TEMPLATE, fname);
00771 check_filename(dir);
00772 if (D_MKDIR(dir)) {
00773 if (errno != EEXIST) {
00774 x = errno;
00775 DIE(("mk_kmail_dir: Cannot create directory %s: %s\n", dir, strerror(x)));
00776 }
00777 }
00778 if (chdir(dir)) {
00779 x = errno;
00780 DIE(("mk_kmail_dir: Cannot change to directory %s: %s\n", dir, strerror(x)));
00781 }
00782 free (dir);
00783
00784
00785 index = pst_malloc(strlen(fname)+strlen(KMAIL_INDEX)+1);
00786 sprintf(index, KMAIL_INDEX, fname);
00787 unlink(index);
00788 free(index);
00789
00790 DEBUG_RET();
00791 }
00792
00793
00794 int close_kmail_dir() {
00795 int x;
00796 DEBUG_ENT("close_kmail_dir");
00797 if (chdir("..")) {
00798 x = errno;
00799 DIE(("close_kmail_dir: Cannot move up dir (..): %s\n", strerror(x)));
00800 }
00801 DEBUG_RET();
00802 return 0;
00803 }
00804
00805
00806 char *item_type_to_name(int32_t item_type) {
00807 char *name;
00808 switch (item_type) {
00809 case PST_TYPE_APPOINTMENT:
00810 name = "calendar";
00811 break;
00812 case PST_TYPE_CONTACT:
00813 name = "contacts";
00814 break;
00815 case PST_TYPE_JOURNAL:
00816 name = "journal";
00817 break;
00818 case PST_TYPE_STICKYNOTE:
00819 case PST_TYPE_TASK:
00820 case PST_TYPE_NOTE:
00821 case PST_TYPE_OTHER:
00822 case PST_TYPE_REPORT:
00823 default:
00824 name = "mbox";
00825 break;
00826 }
00827 return name;
00828 }
00829
00830
00831 int32_t reduced_item_type(int32_t item_type) {
00832 int32_t reduced;
00833 switch (item_type) {
00834 case PST_TYPE_APPOINTMENT:
00835 case PST_TYPE_CONTACT:
00836 case PST_TYPE_JOURNAL:
00837 reduced = item_type;
00838 break;
00839 case PST_TYPE_STICKYNOTE:
00840 case PST_TYPE_TASK:
00841 case PST_TYPE_NOTE:
00842 case PST_TYPE_OTHER:
00843 case PST_TYPE_REPORT:
00844 default:
00845 reduced = PST_TYPE_NOTE;
00846 break;
00847 }
00848 return reduced;
00849 }
00850
00851
00852
00853 void mk_recurse_dir(char *dir) {
00854 int x;
00855 DEBUG_ENT("mk_recurse_dir");
00856 check_filename(dir);
00857 if (D_MKDIR (dir)) {
00858 if (errno != EEXIST) {
00859 x = errno;
00860 DIE(("mk_recurse_dir: Cannot create directory %s: %s\n", dir, strerror(x)));
00861 }
00862 }
00863 if (chdir(dir)) {
00864 x = errno;
00865 DIE(("mk_recurse_dir: Cannot change to directory %s: %s\n", dir, strerror(x)));
00866 }
00867 DEBUG_RET();
00868 }
00869
00870
00871 int close_recurse_dir() {
00872 int x;
00873 DEBUG_ENT("close_recurse_dir");
00874 if (chdir("..")) {
00875 x = errno;
00876 DIE(("close_recurse_dir: Cannot go up dir (..): %s\n", strerror(x)));
00877 }
00878 DEBUG_RET();
00879 return 0;
00880 }
00881
00882
00883 void mk_separate_dir(char *dir) {
00884 size_t dirsize = strlen(dir) + 10;
00885 char dir_name[dirsize];
00886 int x = 0, y = 0;
00887
00888 DEBUG_ENT("mk_separate_dir");
00889 do {
00890 if (y == 0)
00891 snprintf(dir_name, dirsize, "%s", dir);
00892 else
00893 snprintf(dir_name, dirsize, "%s" SEP_MAIL_FILE_TEMPLATE, dir, y, "");
00894
00895 check_filename(dir_name);
00896 DEBUG_INFO(("about to try creating %s\n", dir_name));
00897 if (D_MKDIR(dir_name)) {
00898 if (errno != EEXIST) {
00899 x = errno;
00900 DIE(("mk_separate_dir: Cannot create directory %s: %s\n", dir, strerror(x)));
00901 }
00902 } else {
00903 break;
00904 }
00905 y++;
00906 } while (overwrite == 0);
00907
00908 if (chdir(dir_name)) {
00909 x = errno;
00910 DIE(("mk_separate_dir: Cannot change to directory %s: %s\n", dir, strerror(x)));
00911 }
00912
00913 if (overwrite) {
00914
00915 #if !defined(WIN32) && !defined(__CYGWIN__)
00916 DIR * sdir = NULL;
00917 struct dirent *dirent = NULL;
00918 struct stat filestat;
00919 if (!(sdir = opendir("./"))) {
00920 DEBUG_WARN(("mk_separate_dir: Cannot open dir \"%s\" for deletion of old contents\n", "./"));
00921 } else {
00922 while ((dirent = readdir(sdir))) {
00923 if (lstat(dirent->d_name, &filestat) != -1)
00924 if (S_ISREG(filestat.st_mode)) {
00925 if (unlink(dirent->d_name)) {
00926 y = errno;
00927 DIE(("mk_separate_dir: unlink returned error on file %s: %s\n", dirent->d_name, strerror(y)));
00928 }
00929 }
00930 }
00931 closedir(sdir);
00932 }
00933 #endif
00934 }
00935
00936 DEBUG_RET();
00937 }
00938
00939
00940 int close_separate_dir() {
00941 int x;
00942 DEBUG_ENT("close_separate_dir");
00943 if (chdir("..")) {
00944 x = errno;
00945 DIE(("close_separate_dir: Cannot go up dir (..): %s\n", strerror(x)));
00946 }
00947 DEBUG_RET();
00948 return 0;
00949 }
00950
00951
00952 void mk_separate_file(struct file_ll *f, int32_t t, char *extension, int openit) {
00953 DEBUG_ENT("mk_separate_file");
00954 DEBUG_INFO(("opening next file to save email type %s\n", item_type_to_name(t)));
00955 if (f->item_count > 999999999) {
00956 DIE(("mk_separate_file: The number of emails in this folder has become too high to handle\n"));
00957 }
00958 sprintf(f->name[t], SEP_MAIL_FILE_TEMPLATE, f->item_count, extension);
00959 check_filename(f->name[t]);
00960 if (openit) {
00961 if (!(f->output[t] = fopen(f->name[t], "w"))) {
00962 DIE(("mk_separate_file: Cannot open file to save email \"%s\"\n", f->name[t]));
00963 }
00964 }
00965 DEBUG_RET();
00966 }
00967
00968
00969 void close_separate_file(struct file_ll *f) {
00970 int32_t t;
00971 DEBUG_ENT("close_separate_file");
00972 for (t=0; t<PST_TYPE_MAX; t++) {
00973 if (f->output[t]) {
00974 struct stat st;
00975 fclose(f->output[t]);
00976 stat(f->name[t], &st);
00977 if (!st.st_size) {
00978 DEBUG_WARN(("removing empty output file %s\n", f->name[t]));
00979 remove(f->name[t]);
00980 }
00981 f->output[t] = NULL;
00982 }
00983 }
00984 DEBUG_RET();
00985 }
00986
00987
00988 char *my_stristr(char *haystack, char *needle) {
00989
00990 char *x=haystack, *y=needle, *z = NULL;
00991 if (!haystack || !needle) {
00992 return NULL;
00993 }
00994 while (*y != '\0' && *x != '\0') {
00995 if (tolower(*y) == tolower(*x)) {
00996
00997 y++;
00998 if (!z) {
00999 z = x;
01000 }
01001 } else {
01002 y = needle;
01003 z = NULL;
01004 }
01005 x++;
01006 }
01007
01008 if (*y != '\0') return NULL;
01009 return z;
01010 }
01011
01012
01013 void check_filename(char *fname) {
01014 char *t = fname;
01015 DEBUG_ENT("check_filename");
01016 if (!t) {
01017 DEBUG_RET();
01018 return;
01019 }
01020 while ((t = strpbrk(t, "/\\:"))) {
01021
01022 *t = '_';
01023 }
01024 DEBUG_RET();
01025 }
01026
01027
01034 int acceptable_ext(pst_item_attach* attach)
01035 {
01036 if (!acceptable_extensions || *acceptable_extensions == '\0') return 1;
01037 char *attach_filename = (attach->filename2.str) ? attach->filename2.str
01038 : attach->filename1.str;
01039 if (!attach_filename) return 1;
01040 char *e = strrchr(attach_filename, '.');
01041 if (!e) return 1;
01042 DEBUG_ENT("acceptable_ext");
01043 DEBUG_INFO(("attachment extension %s\n", e));
01044 int rc = 0;
01045 char *a = acceptable_extensions;
01046 while (*a) {
01047 if (pst_stricmp(a, e) == 0) {
01048 rc = 1;
01049 break;
01050 }
01051 a += strlen(a) + 1;
01052 }
01053 DEBUG_INFO(("attachment acceptable returns %d\n", rc));
01054 DEBUG_RET();
01055 return rc;
01056 }
01057
01058
01059 void write_separate_attachment(char f_name[], pst_item_attach* attach, int attach_num, pst_file* pst)
01060 {
01061 FILE *fp = NULL;
01062 int x = 0;
01063 char *temp = NULL;
01064
01065
01066
01067 char *attach_filename = (attach->filename2.str) ? attach->filename2.str
01068 : attach->filename1.str;
01069 DEBUG_ENT("write_separate_attachment");
01070 DEBUG_INFO(("Attachment %s Size is %#"PRIx64", data = %#"PRIxPTR", id %#"PRIx64"\n", attach_filename, (uint64_t)attach->data.size, attach->data.data, attach->i_id));
01071
01072 if (!attach->data.data) {
01073
01074 pst_index_ll *ptr = pst_getID(pst, attach->i_id);
01075 if (!ptr) {
01076 DEBUG_WARN(("Couldn't find i_id %#"PRIx64". Cannot save attachment to file\n", attach->i_id));
01077 DEBUG_RET();
01078 return;
01079 }
01080 }
01081
01082 check_filename(f_name);
01083 if (!attach_filename) {
01084
01085 temp = pst_malloc(strlen(f_name)+15);
01086 sprintf(temp, "%s-attach%i", f_name, attach_num);
01087 } else {
01088
01089 temp = pst_malloc(strlen(f_name)+strlen(attach_filename)+15);
01090 do {
01091 if (fp) fclose(fp);
01092 if (x == 0)
01093 sprintf(temp, "%s-%s", f_name, attach_filename);
01094 else
01095 sprintf(temp, "%s-%s-%i", f_name, attach_filename, x);
01096 } while ((fp = fopen(temp, "r")) && ++x < 99999999);
01097 if (x > 99999999) {
01098 DIE(("error finding attachment name. exhausted possibilities to %s\n", temp));
01099 }
01100 }
01101 DEBUG_INFO(("Saving attachment to %s\n", temp));
01102 if (!(fp = fopen(temp, "w"))) {
01103 DEBUG_WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp));
01104 } else {
01105 (void)pst_attach_to_file(pst, attach, fp);
01106 fclose(fp);
01107 }
01108 if (temp) free(temp);
01109 DEBUG_RET();
01110 }
01111
01112
01113 void write_embedded_message(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pf, int save_rtf, char** extra_mime_headers)
01114 {
01115 pst_index_ll *ptr;
01116 DEBUG_ENT("write_embedded_message");
01117 ptr = pst_getID(pf, attach->i_id);
01118
01119 pst_desc_tree d_ptr;
01120 d_ptr.d_id = 0;
01121 d_ptr.parent_d_id = 0;
01122 d_ptr.assoc_tree = NULL;
01123 d_ptr.desc = ptr;
01124 d_ptr.no_child = 0;
01125 d_ptr.prev = NULL;
01126 d_ptr.next = NULL;
01127 d_ptr.parent = NULL;
01128 d_ptr.child = NULL;
01129 d_ptr.child_tail = NULL;
01130
01131 pst_item *item = pst_parse_item(pf, &d_ptr, attach->id2_head);
01132
01133
01134
01135
01136
01137
01138 if (!item) {
01139 DEBUG_WARN(("write_embedded_message: pst_parse_item was unable to parse the embedded message in attachment ID %llu", attach->i_id));
01140 } else {
01141 if (!item->email) {
01142 DEBUG_WARN(("write_embedded_message: pst_parse_item returned type %d, not an email message", item->type));
01143 } else {
01144 fprintf(f_output, "\n--%s\n", boundary);
01145 fprintf(f_output, "Content-Type: %s\n\n", attach->mimetype.str);
01146 write_normal_email(f_output, "", item, MODE_NORMAL, 0, pf, save_rtf, 1, extra_mime_headers);
01147 }
01148 pst_freeItem(item);
01149 }
01150
01151 DEBUG_RET();
01152 }
01153
01154
01155 void write_inline_attachment(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pst)
01156 {
01157 DEBUG_ENT("write_inline_attachment");
01158 DEBUG_INFO(("Attachment Size is %#"PRIx64", data = %#"PRIxPTR", id %#"PRIx64"\n", (uint64_t)attach->data.size, attach->data.data, attach->i_id));
01159
01160 if (!attach->data.data) {
01161
01162 pst_index_ll *ptr = pst_getID(pst, attach->i_id);
01163 if (!ptr) {
01164 DEBUG_WARN(("Couldn't find ID pointer. Cannot save attachment to file\n"));
01165 DEBUG_RET();
01166 return;
01167 }
01168 }
01169
01170 fprintf(f_output, "\n--%s\n", boundary);
01171 if (!attach->mimetype.str) {
01172 fprintf(f_output, "Content-Type: %s\n", MIME_TYPE_DEFAULT);
01173 } else {
01174 fprintf(f_output, "Content-Type: %s\n", attach->mimetype.str);
01175 }
01176 fprintf(f_output, "Content-Transfer-Encoding: base64\n");
01177
01178 if (attach->content_id.str) {
01179 fprintf(f_output, "Content-ID: <%s>\n", attach->content_id.str);
01180 }
01181
01182 if (attach->filename2.str) {
01183
01184
01185 pst_rfc2231(&attach->filename2);
01186 fprintf(f_output, "Content-Disposition: attachment; \n filename*=%s\n\n", attach->filename2.str);
01187 }
01188 else if (attach->filename1.str) {
01189
01190 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", attach->filename1.str);
01191 }
01192 else {
01193
01194 fprintf(f_output, "Content-Disposition: inline\n\n");
01195 }
01196
01197 (void)pst_attach_to_file_base64(pst, attach, f_output);
01198 fprintf(f_output, "\n\n");
01199 DEBUG_RET();
01200 }
01201
01202
01203 int header_match(char *header, char*field) {
01204 int n = strlen(field);
01205 if (strncasecmp(header, field, n) == 0) return 1;
01206 if ((field[n-1] == ' ') && (strncasecmp(header, field, n-1) == 0)) {
01207 char *crlftab = "\r\n\t";
01208 DEBUG_INFO(("Possible wrapped header = %s\n", header));
01209 if (strncasecmp(header+n-1, crlftab, 3) == 0) return 1;
01210 }
01211 return 0;
01212 }
01213
01214 int valid_headers(char *header)
01215 {
01216
01217
01218
01219
01220
01221 if (header) {
01222 if (header_match(header, "Content-Type: " )) return 1;
01223 if (header_match(header, "Date: " )) return 1;
01224 if (header_match(header, "From: " )) return 1;
01225 if (header_match(header, "MIME-Version: " )) return 1;
01226 if (header_match(header, "Microsoft Mail Internet Headers")) return 1;
01227 if (header_match(header, "Received: " )) return 1;
01228 if (header_match(header, "Return-Path: " )) return 1;
01229 if (header_match(header, "Subject: " )) return 1;
01230 if (header_match(header, "To: " )) return 1;
01231 if (header_match(header, "X-ASG-Debug-ID: " )) return 1;
01232 if (header_match(header, "X-Barracuda-URL: " )) return 1;
01233 if (header_match(header, "X-x: " )) return 1;
01234 if (strlen(header) > 2) {
01235 DEBUG_INFO(("Ignore bogus headers = %s\n", header));
01236 }
01237 return 0;
01238 }
01239 else return 0;
01240 }
01241
01242
01243 void header_has_field(char *header, char *field, int *flag)
01244 {
01245 DEBUG_ENT("header_has_field");
01246 if (my_stristr(header, field) || (strncasecmp(header, field+1, strlen(field)-1) == 0)) {
01247 DEBUG_INFO(("header block has %s header\n", field+1));
01248 *flag = 1;
01249 }
01250 DEBUG_RET();
01251 }
01252
01253
01254 void header_get_subfield(char *field, const char *subfield, char *body_subfield, size_t size_subfield)
01255 {
01256 if (!field) return;
01257 DEBUG_ENT("header_get_subfield");
01258 char search[60];
01259 snprintf(search, sizeof(search), " %s=", subfield);
01260 field++;
01261 char *n = header_end_field(field);
01262 char *s = my_stristr(field, search);
01263 if (n && s && (s < n)) {
01264 char *e, *f, save;
01265 s += strlen(search);
01266 if (*s == '"') {
01267 s++;
01268 e = strchr(s, '"');
01269 }
01270 else {
01271 e = strchr(s, ';');
01272 f = strchr(s, '\n');
01273 if (e && f && (f < e)) e = f;
01274 }
01275 if (!e || (e > n)) e = n;
01276 save = *e;
01277 *e = '\0';
01278 snprintf(body_subfield, size_subfield, "%s", s);
01279 *e = save;
01280 DEBUG_INFO(("body %s %s from headers\n", subfield, body_subfield));
01281 }
01282 DEBUG_RET();
01283 }
01284
01285 char* header_get_field(char *header, char *field)
01286 {
01287 char *t = my_stristr(header, field);
01288 if (!t && (strncasecmp(header, field+1, strlen(field)-1) == 0)) t = header;
01289 return t;
01290 }
01291
01292
01293
01294
01295 char *header_end_field(char *field)
01296 {
01297 char *e = strchr(field+1, '\n');
01298 while (e && ((e[1] == ' ') || (e[1] == '\t'))) {
01299 e = strchr(e+1, '\n');
01300 }
01301 return e;
01302 }
01303
01304
01305 void header_strip_field(char *header, char *field)
01306 {
01307 char *t;
01308 while ((t = header_get_field(header, field))) {
01309 char *e = header_end_field(t);
01310 if (e) {
01311 if (t == header) e++;
01312 while (*e != '\0') {
01313 *t = *e;
01314 t++;
01315 e++;
01316 }
01317 *t = '\0';
01318 }
01319 else {
01320
01321 *t = '\0';
01322 }
01323 }
01324 }
01325
01326
01327 int test_base64(char *body, size_t len)
01328 {
01329 int b64 = 0;
01330 uint8_t *b = (uint8_t *)body;
01331 DEBUG_ENT("test_base64");
01332 while (len--) {
01333 if ((*b < 32) && (*b != 9) && (*b != 10)) {
01334 DEBUG_INFO(("found base64 byte %d\n", (int)*b));
01335 DEBUG_HEXDUMPC(body, strlen(body), 0x10);
01336 b64 = 1;
01337 break;
01338 }
01339 b++;
01340 }
01341 DEBUG_RET();
01342 return b64;
01343 }
01344
01345
01346 void find_html_charset(char *html, char *charset, size_t charsetlen)
01347 {
01348 const int index = 1;
01349 const int nmatch = index+1;
01350 regmatch_t match[nmatch];
01351 DEBUG_ENT("find_html_charset");
01352 int rc = regexec(&meta_charset_pattern, html, nmatch, match, 0);
01353 if (rc == 0) {
01354 int s = match[index].rm_so;
01355 int e = match[index].rm_eo;
01356 if (s != -1) {
01357 char save = html[e];
01358 html[e] = '\0';
01359 snprintf(charset, charsetlen, "%s", html+s);
01360 html[e] = save;
01361 DEBUG_INFO(("charset %s from html text\n", charset));
01362 }
01363 else {
01364 DEBUG_INFO(("matching %d %d %d %d\n", match[0].rm_so, match[0].rm_eo, match[1].rm_so, match[1].rm_eo));
01365 DEBUG_HEXDUMPC(html, strlen(html), 0x10);
01366 }
01367 }
01368 else {
01369 DEBUG_INFO(("regexec returns %d\n", rc));
01370 }
01371 DEBUG_RET();
01372 }
01373
01374
01375 void find_rfc822_headers(char** extra_mime_headers)
01376 {
01377 DEBUG_ENT("find_rfc822_headers");
01378 char *headers = *extra_mime_headers;
01379 if (headers) {
01380 char *temp, *t;
01381 while ((temp = strstr(headers, "\n\n"))) {
01382 temp[1] = '\0';
01383 t = header_get_field(headers, "\nContent-Type:");
01384 if (t) {
01385 t++;
01386 DEBUG_INFO(("found content type header\n"));
01387 char *n = strchr(t, '\n');
01388 char *s = strstr(t, ": ");
01389 char *e = strchr(t, ';');
01390 if (!e || (e > n)) e = n;
01391 if (s && (s < e)) {
01392 s += 2;
01393 if (!strncasecmp(s, RFC822, e-s)) {
01394 headers = temp+2;
01395 DEBUG_INFO(("found 822 headers\n%s\n", headers));
01396 break;
01397 }
01398 }
01399 }
01400
01401 headers = temp+2;
01402 }
01403 *extra_mime_headers = headers;
01404 }
01405 DEBUG_RET();
01406 }
01407
01408
01409 void write_body_part(FILE* f_output, pst_string *body, char *mime, char *charset, char *boundary, pst_file* pst)
01410 {
01411 DEBUG_ENT("write_body_part");
01412 removeCR(body->str);
01413 size_t body_len = strlen(body->str);
01414
01415 if (body->is_utf8 && (strcasecmp("utf-8", charset))) {
01416 if (prefer_utf8) {
01417 charset = "utf-8";
01418 } else {
01419
01420
01421
01422 size_t rc;
01423 DEBUG_INFO(("Convert %s utf-8 to %s\n", mime, charset));
01424 pst_vbuf *newer = pst_vballoc(2);
01425 rc = pst_vb_utf8to8bit(newer, body->str, body_len, charset);
01426 if (rc == (size_t)-1) {
01427
01428 free(newer->b);
01429 DEBUG_INFO(("Failed to convert %s utf-8 to %s\n", mime, charset));
01430 charset = "utf-8";
01431 } else {
01432
01433 pst_vbgrow(newer, 1);
01434 newer->b[newer->dlen] = '\0';
01435 free(body->str);
01436 body->str = newer->b;
01437 body_len = newer->dlen;
01438 }
01439 free(newer);
01440 }
01441 }
01442 int base64 = test_base64(body->str, body_len);
01443 fprintf(f_output, "\n--%s\n", boundary);
01444 fprintf(f_output, "Content-Type: %s; charset=\"%s\"\n", mime, charset);
01445 if (base64) fprintf(f_output, "Content-Transfer-Encoding: base64\n");
01446 fprintf(f_output, "\n");
01447
01448 if (base64) {
01449 char *enc = pst_base64_encode(body->str, body_len);
01450 if (enc) {
01451 write_email_body(f_output, enc);
01452 fprintf(f_output, "\n");
01453 free(enc);
01454 }
01455 }
01456 else {
01457 write_email_body(f_output, body->str);
01458 }
01459 DEBUG_RET();
01460 }
01461
01462
01463 void write_schedule_part_data(FILE* f_output, pst_item* item, const char* sender, const char* method)
01464 {
01465 fprintf(f_output, "BEGIN:VCALENDAR\n");
01466 fprintf(f_output, "VERSION:2.0\n");
01467 fprintf(f_output, "PRODID:LibPST v%s\n", VERSION);
01468 if (method) fprintf(f_output, "METHOD:%s\n", method);
01469 fprintf(f_output, "BEGIN:VEVENT\n");
01470 if (sender) {
01471 if (item->email->outlook_sender_name.str) {
01472 fprintf(f_output, "ORGANIZER;CN=\"%s\":MAILTO:%s\n", item->email->outlook_sender_name.str, sender);
01473 } else {
01474 fprintf(f_output, "ORGANIZER;CN=\"\":MAILTO:%s\n", sender);
01475 }
01476 }
01477 write_appointment(f_output, item);
01478 fprintf(f_output, "END:VCALENDAR\n");
01479 }
01480
01481
01482 void write_schedule_part(FILE* f_output, pst_item* item, const char* sender, const char* boundary)
01483 {
01484 const char* method = "REQUEST";
01485 const char* charset = "utf-8";
01486 char fname[30];
01487 if (!item->appointment) return;
01488
01489
01490 fprintf(f_output, "\n--%s\n", boundary);
01491 fprintf(f_output, "Content-Type: %s; method=\"%s\"; charset=\"%s\"\n\n", "text/calendar", method, charset);
01492 write_schedule_part_data(f_output, item, sender, method);
01493 fprintf(f_output, "\n");
01494
01495
01496 snprintf(fname, sizeof(fname), "i%i.ics", rand());
01497 fprintf(f_output, "\n--%s\n", boundary);
01498 fprintf(f_output, "Content-Type: %s; charset=\"%s\"; name=\"%s\"\n", "text/calendar", "utf-8", fname);
01499 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", fname);
01500 write_schedule_part_data(f_output, item, sender, method);
01501 fprintf(f_output, "\n");
01502 }
01503
01504
01505 void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, int mode_MH, pst_file* pst, int save_rtf, int embedding, char** extra_mime_headers)
01506 {
01507 char boundary[60];
01508 char altboundary[66];
01509 char *altboundaryp = NULL;
01510 char body_charset[30];
01511 char buffer_charset[30];
01512 char body_report[60];
01513 char sender[60];
01514 int sender_known = 0;
01515 char *temp = NULL;
01516 time_t em_time;
01517 char *c_time;
01518 char *headers = NULL;
01519 int has_from, has_subject, has_to, has_cc, has_date, has_msgid;
01520 has_from = has_subject = has_to = has_cc = has_date = has_msgid = 0;
01521 DEBUG_ENT("write_normal_email");
01522
01523 pst_convert_utf8_null(item, &item->email->header);
01524 headers = valid_headers(item->email->header.str) ? item->email->header.str :
01525 valid_headers(*extra_mime_headers) ? *extra_mime_headers :
01526 NULL;
01527
01528
01529 strncpy(body_charset, pst_default_charset(item, sizeof(buffer_charset), buffer_charset), sizeof(body_charset));
01530 body_charset[sizeof(body_charset)-1] = '\0';
01531 strncpy(body_report, "delivery-status", sizeof(body_report));
01532 body_report[sizeof(body_report)-1] = '\0';
01533
01534
01535 pst_convert_utf8(item, &item->email->sender_address);
01536 if (item->email->sender_address.str && strchr(item->email->sender_address.str, '@')) {
01537 temp = item->email->sender_address.str;
01538 sender_known = 1;
01539 }
01540 else {
01541 temp = "MAILER-DAEMON";
01542 }
01543 strncpy(sender, temp, sizeof(sender));
01544 sender[sizeof(sender)-1] = '\0';
01545
01546
01547 if (item->email->sent_date) {
01548 em_time = pst_fileTimeToUnixTime(item->email->sent_date);
01549 c_time = ctime(&em_time);
01550 if (c_time)
01551 c_time[strlen(c_time)-1] = '\0';
01552 else
01553 c_time = "Thu Jan 1 00:00:00 1970";
01554 } else
01555 c_time = "Thu Jan 1 00:00:00 1970";
01556
01557
01558 snprintf(boundary, sizeof(boundary), "--boundary-LibPST-iamunique-%i_-_-", rand());
01559 snprintf(altboundary, sizeof(altboundary), "alt-%s", boundary);
01560
01561
01562 if (headers ) {
01563 char *t;
01564 removeCR(headers);
01565
01566 temp = strstr(headers, "\n\n");
01567 if (temp) {
01568
01569 temp[1] = '\0';
01570
01571
01572
01573 if (!*extra_mime_headers) *extra_mime_headers = temp+2;
01574 DEBUG_INFO(("Found extra mime headers\n%s\n", temp+2));
01575 }
01576
01577
01578 header_has_field(headers, "\nFrom:", &has_from);
01579 header_has_field(headers, "\nTo:", &has_to);
01580 header_has_field(headers, "\nSubject:", &has_subject);
01581 header_has_field(headers, "\nDate:", &has_date);
01582 header_has_field(headers, "\nCC:", &has_cc);
01583 header_has_field(headers, "\nMessage-Id:", &has_msgid);
01584
01585
01586 t = header_get_field(headers, "\nContent-Type:");
01587 header_get_subfield(t, "charset", body_charset, sizeof(body_charset));
01588 header_get_subfield(t, "report-type", body_report, sizeof(body_report));
01589
01590
01591 if (!sender_known) {
01592 t = header_get_field(headers, "\nFrom:");
01593 if (t) {
01594
01595 t++;
01596 char *n = strchr(t, '\n');
01597 char *s = strchr(t, '<');
01598 char *e = strchr(t, '>');
01599 if (s && e && n && (s < e) && (e < n)) {
01600 char save = *e;
01601 *e = '\0';
01602 snprintf(sender, sizeof(sender), "%s", s+1);
01603 *e = save;
01604 }
01605 }
01606 }
01607
01608
01609 header_strip_field(headers, "\nMicrosoft Mail Internet Headers");
01610 header_strip_field(headers, "\nMIME-Version:");
01611 header_strip_field(headers, "\nContent-Type:");
01612 header_strip_field(headers, "\nContent-Transfer-Encoding:");
01613 header_strip_field(headers, "\nContent-class:");
01614 header_strip_field(headers, "\nX-MimeOLE:");
01615 header_strip_field(headers, "\nX-From_:");
01616 }
01617
01618 DEBUG_INFO(("About to print Header\n"));
01619
01620 if (item && item->subject.str) {
01621 pst_convert_utf8(item, &item->subject);
01622 DEBUG_INFO(("item->subject = %s\n", item->subject.str));
01623 }
01624
01625 if (mode != MODE_SEPARATE) {
01626
01627
01628
01629
01630 char *quo = (embedding) ? ">" : "";
01631 fprintf(f_output, "%sFrom \"%s\" %s\n", quo, sender, c_time);
01632 }
01633
01634
01635 if (headers) {
01636 int len = strlen(headers);
01637 if (len > 0) {
01638 fprintf(f_output, "%s", headers);
01639
01640 if (headers[len-1] != '\n') fprintf(f_output, "\n");
01641
01642
01643
01644
01645
01646
01647
01648
01649
01650
01651
01652
01653 }
01654 }
01655
01656
01657 if ((item->flags & PST_FLAG_READ) == PST_FLAG_READ) {
01658 fprintf(f_output, "Status: RO\n");
01659 }
01660
01661
01662
01663 if (!has_from) {
01664 if (item->email->outlook_sender_name.str){
01665 pst_rfc2047(item, &item->email->outlook_sender_name, 1);
01666 fprintf(f_output, "From: %s <%s>\n", item->email->outlook_sender_name.str, sender);
01667 } else {
01668 fprintf(f_output, "From: <%s>\n", sender);
01669 }
01670 }
01671
01672 if (!has_subject) {
01673 if (item->subject.str) {
01674 pst_rfc2047(item, &item->subject, 0);
01675 fprintf(f_output, "Subject: %s\n", item->subject.str);
01676 } else {
01677 fprintf(f_output, "Subject: \n");
01678 }
01679 }
01680
01681 if (!has_to && item->email->sentto_address.str) {
01682 pst_rfc2047(item, &item->email->sentto_address, 0);
01683 fprintf(f_output, "To: %s\n", item->email->sentto_address.str);
01684 }
01685
01686 if (!has_cc && item->email->cc_address.str) {
01687 pst_rfc2047(item, &item->email->cc_address, 0);
01688 fprintf(f_output, "Cc: %s\n", item->email->cc_address.str);
01689 }
01690
01691 if (!has_date && item->email->sent_date) {
01692 char c_time[C_TIME_SIZE];
01693 struct tm stm;
01694 gmtime_r(&em_time, &stm);
01695 strftime(c_time, C_TIME_SIZE, "%a, %d %b %Y %H:%M:%S %z", &stm);
01696 fprintf(f_output, "Date: %s\n", c_time);
01697 }
01698
01699 if (!has_msgid && item->email->messageid.str) {
01700 pst_convert_utf8(item, &item->email->messageid);
01701 fprintf(f_output, "Message-Id: %s\n", item->email->messageid.str);
01702 }
01703
01704
01705
01706 pst_convert_utf8_null(item, &item->email->sender_address);
01707 if (item->email->sender_address.str && !strchr(item->email->sender_address.str, '@')
01708 && strcmp(item->email->sender_address.str, ".")
01709 && (strlen(item->email->sender_address.str) > 0)) {
01710 fprintf(f_output, "X-libpst-forensic-sender: %s\n", item->email->sender_address.str);
01711 }
01712
01713 if (item->email->bcc_address.str) {
01714 pst_convert_utf8(item, &item->email->bcc_address);
01715 fprintf(f_output, "X-libpst-forensic-bcc: %s\n", item->email->bcc_address.str);
01716 }
01717
01718
01719 fprintf(f_output, "MIME-Version: 1.0\n");
01720 if (item->type == PST_TYPE_REPORT) {
01721
01722 fprintf(f_output, "Content-Type: multipart/report; report-type=%s;\n\tboundary=\"%s\"\n", body_report, boundary);
01723 }
01724 else {
01725 fprintf(f_output, "Content-Type: multipart/mixed;\n\tboundary=\"%s\"\n", boundary);
01726 }
01727 fprintf(f_output, "\n");
01728
01729
01730 if ((item->type == PST_TYPE_REPORT) && (item->email->report_text.str)) {
01731 write_body_part(f_output, &item->email->report_text, "text/plain", body_charset, boundary, pst);
01732 fprintf(f_output, "\n");
01733 }
01734
01735 if (item->body.str && item->email->htmlbody.str) {
01736
01737 fprintf(f_output, "\n--%s\n", boundary);
01738 fprintf(f_output, "Content-Type: multipart/alternative;\n\tboundary=\"%s\"\n", altboundary);
01739 altboundaryp = altboundary;
01740 }
01741 else {
01742 altboundaryp = boundary;
01743 }
01744
01745 if (item->body.str) {
01746 write_body_part(f_output, &item->body, "text/plain", body_charset, altboundaryp, pst);
01747 }
01748
01749 if (item->email->htmlbody.str) {
01750 find_html_charset(item->email->htmlbody.str, body_charset, sizeof(body_charset));
01751 write_body_part(f_output, &item->email->htmlbody, "text/html", body_charset, altboundaryp, pst);
01752 }
01753
01754 if (item->body.str && item->email->htmlbody.str) {
01755
01756 fprintf(f_output, "\n--%s--\n", altboundary);
01757 }
01758
01759 if (item->email->rtf_compressed.data && save_rtf) {
01760 pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach));
01761 DEBUG_INFO(("Adding RTF body as attachment\n"));
01762 memset(attach, 0, sizeof(pst_item_attach));
01763 attach->next = item->attach;
01764 item->attach = attach;
01765 attach->data.data = pst_lzfu_decompress(item->email->rtf_compressed.data, item->email->rtf_compressed.size, &attach->data.size);
01766 attach->filename2.str = strdup(RTF_ATTACH_NAME);
01767 attach->filename2.is_utf8 = 1;
01768 attach->mimetype.str = strdup(RTF_ATTACH_TYPE);
01769 attach->mimetype.is_utf8 = 1;
01770 }
01771
01772 if (item->email->encrypted_body.data) {
01773 pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach));
01774 DEBUG_INFO(("Adding encrypted text body as attachment\n"));
01775 memset(attach, 0, sizeof(pst_item_attach));
01776 attach->next = item->attach;
01777 item->attach = attach;
01778 attach->data.data = item->email->encrypted_body.data;
01779 attach->data.size = item->email->encrypted_body.size;
01780 item->email->encrypted_body.data = NULL;
01781 }
01782
01783 if (item->email->encrypted_htmlbody.data) {
01784 pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach));
01785 DEBUG_INFO(("Adding encrypted HTML body as attachment\n"));
01786 memset(attach, 0, sizeof(pst_item_attach));
01787 attach->next = item->attach;
01788 item->attach = attach;
01789 attach->data.data = item->email->encrypted_htmlbody.data;
01790 attach->data.size = item->email->encrypted_htmlbody.size;
01791 item->email->encrypted_htmlbody.data = NULL;
01792 }
01793
01794 if (item->type == PST_TYPE_SCHEDULE) {
01795 write_schedule_part(f_output, item, sender, boundary);
01796 }
01797
01798
01799 {
01800 pst_item_attach* attach;
01801 int attach_num = 0;
01802 for (attach = item->attach; attach; attach = attach->next) {
01803 pst_convert_utf8_null(item, &attach->filename1);
01804 pst_convert_utf8_null(item, &attach->filename2);
01805 pst_convert_utf8_null(item, &attach->mimetype);
01806 DEBUG_INFO(("Attempting Attachment encoding\n"));
01807 if (attach->method == PST_ATTACH_EMBEDDED) {
01808 DEBUG_INFO(("have an embedded rfc822 message attachment\n"));
01809 if (attach->mimetype.str) {
01810 DEBUG_INFO(("which already has a mime-type of %s\n", attach->mimetype.str));
01811 free(attach->mimetype.str);
01812 }
01813 attach->mimetype.str = strdup(RFC822);
01814 attach->mimetype.is_utf8 = 1;
01815 find_rfc822_headers(extra_mime_headers);
01816 write_embedded_message(f_output, attach, boundary, pst, save_rtf, extra_mime_headers);
01817 }
01818 else if (attach->data.data || attach->i_id) {
01819 if (acceptable_ext(attach)) {
01820 if (mode == MODE_SEPARATE && !mode_MH)
01821 write_separate_attachment(f_name, attach, ++attach_num, pst);
01822 else
01823 write_inline_attachment(f_output, attach, boundary, pst);
01824 }
01825 }
01826 }
01827 }
01828
01829 fprintf(f_output, "\n--%s--\n\n", boundary);
01830 DEBUG_RET();
01831 }
01832
01833
01834 void write_vcard(FILE* f_output, pst_item* item, pst_item_contact* contact, char comment[])
01835 {
01836 char* result = NULL;
01837 size_t resultlen = 0;
01838 char time_buffer[30];
01839
01840
01841
01842
01843 DEBUG_ENT("write_vcard");
01844
01845
01846 pst_convert_utf8_null(item, &contact->fullname);
01847 pst_convert_utf8_null(item, &contact->surname);
01848 pst_convert_utf8_null(item, &contact->first_name);
01849 pst_convert_utf8_null(item, &contact->middle_name);
01850 pst_convert_utf8_null(item, &contact->display_name_prefix);
01851 pst_convert_utf8_null(item, &contact->suffix);
01852 pst_convert_utf8_null(item, &contact->nickname);
01853 pst_convert_utf8_null(item, &contact->address1);
01854 pst_convert_utf8_null(item, &contact->address2);
01855 pst_convert_utf8_null(item, &contact->address3);
01856 pst_convert_utf8_null(item, &contact->home_po_box);
01857 pst_convert_utf8_null(item, &contact->home_street);
01858 pst_convert_utf8_null(item, &contact->home_city);
01859 pst_convert_utf8_null(item, &contact->home_state);
01860 pst_convert_utf8_null(item, &contact->home_postal_code);
01861 pst_convert_utf8_null(item, &contact->home_country);
01862 pst_convert_utf8_null(item, &contact->home_address);
01863 pst_convert_utf8_null(item, &contact->business_po_box);
01864 pst_convert_utf8_null(item, &contact->business_street);
01865 pst_convert_utf8_null(item, &contact->business_city);
01866 pst_convert_utf8_null(item, &contact->business_state);
01867 pst_convert_utf8_null(item, &contact->business_postal_code);
01868 pst_convert_utf8_null(item, &contact->business_country);
01869 pst_convert_utf8_null(item, &contact->business_address);
01870 pst_convert_utf8_null(item, &contact->other_po_box);
01871 pst_convert_utf8_null(item, &contact->other_street);
01872 pst_convert_utf8_null(item, &contact->other_city);
01873 pst_convert_utf8_null(item, &contact->other_state);
01874 pst_convert_utf8_null(item, &contact->other_postal_code);
01875 pst_convert_utf8_null(item, &contact->other_country);
01876 pst_convert_utf8_null(item, &contact->other_address);
01877 pst_convert_utf8_null(item, &contact->business_fax);
01878 pst_convert_utf8_null(item, &contact->business_phone);
01879 pst_convert_utf8_null(item, &contact->business_phone2);
01880 pst_convert_utf8_null(item, &contact->car_phone);
01881 pst_convert_utf8_null(item, &contact->home_fax);
01882 pst_convert_utf8_null(item, &contact->home_phone);
01883 pst_convert_utf8_null(item, &contact->home_phone2);
01884 pst_convert_utf8_null(item, &contact->isdn_phone);
01885 pst_convert_utf8_null(item, &contact->mobile_phone);
01886 pst_convert_utf8_null(item, &contact->other_phone);
01887 pst_convert_utf8_null(item, &contact->pager_phone);
01888 pst_convert_utf8_null(item, &contact->primary_fax);
01889 pst_convert_utf8_null(item, &contact->primary_phone);
01890 pst_convert_utf8_null(item, &contact->radio_phone);
01891 pst_convert_utf8_null(item, &contact->telex);
01892 pst_convert_utf8_null(item, &contact->job_title);
01893 pst_convert_utf8_null(item, &contact->profession);
01894 pst_convert_utf8_null(item, &contact->assistant_name);
01895 pst_convert_utf8_null(item, &contact->assistant_phone);
01896 pst_convert_utf8_null(item, &contact->company_name);
01897 pst_convert_utf8_null(item, &item->body);
01898
01899
01900 fprintf(f_output, "BEGIN:VCARD\n");
01901 fprintf(f_output, "FN:%s\n", pst_rfc2426_escape(contact->fullname.str, &result, &resultlen));
01902
01903
01904 fprintf(f_output, "N:%s;", (!contact->surname.str) ? "" : pst_rfc2426_escape(contact->surname.str, &result, &resultlen));
01905 fprintf(f_output, "%s;", (!contact->first_name.str) ? "" : pst_rfc2426_escape(contact->first_name.str, &result, &resultlen));
01906 fprintf(f_output, "%s;", (!contact->middle_name.str) ? "" : pst_rfc2426_escape(contact->middle_name.str, &result, &resultlen));
01907 fprintf(f_output, "%s;", (!contact->display_name_prefix.str) ? "" : pst_rfc2426_escape(contact->display_name_prefix.str, &result, &resultlen));
01908 fprintf(f_output, "%s\n", (!contact->suffix.str) ? "" : pst_rfc2426_escape(contact->suffix.str, &result, &resultlen));
01909
01910 if (contact->nickname.str)
01911 fprintf(f_output, "NICKNAME:%s\n", pst_rfc2426_escape(contact->nickname.str, &result, &resultlen));
01912 if (contact->address1.str)
01913 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address1.str, &result, &resultlen));
01914 if (contact->address2.str)
01915 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address2.str, &result, &resultlen));
01916 if (contact->address3.str)
01917 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address3.str, &result, &resultlen));
01918 if (contact->birthday)
01919 fprintf(f_output, "BDAY:%s\n", pst_rfc2425_datetime_format(contact->birthday, sizeof(time_buffer), time_buffer));
01920
01921 if (contact->home_address.str) {
01922
01923 fprintf(f_output, "ADR;TYPE=home:%s;", (!contact->home_po_box.str) ? "" : pst_rfc2426_escape(contact->home_po_box.str, &result, &resultlen));
01924 fprintf(f_output, "%s;", "");
01925 fprintf(f_output, "%s;", (!contact->home_street.str) ? "" : pst_rfc2426_escape(contact->home_street.str, &result, &resultlen));
01926 fprintf(f_output, "%s;", (!contact->home_city.str) ? "" : pst_rfc2426_escape(contact->home_city.str, &result, &resultlen));
01927 fprintf(f_output, "%s;", (!contact->home_state.str) ? "" : pst_rfc2426_escape(contact->home_state.str, &result, &resultlen));
01928 fprintf(f_output, "%s;", (!contact->home_postal_code.str) ? "" : pst_rfc2426_escape(contact->home_postal_code.str, &result, &resultlen));
01929 fprintf(f_output, "%s\n", (!contact->home_country.str) ? "" : pst_rfc2426_escape(contact->home_country.str, &result, &resultlen));
01930 fprintf(f_output, "LABEL;TYPE=home:%s\n", pst_rfc2426_escape(contact->home_address.str, &result, &resultlen));
01931 }
01932
01933 if (contact->business_address.str) {
01934
01935 fprintf(f_output, "ADR;TYPE=work:%s;", (!contact->business_po_box.str) ? "" : pst_rfc2426_escape(contact->business_po_box.str, &result, &resultlen));
01936 fprintf(f_output, "%s;", "");
01937 fprintf(f_output, "%s;", (!contact->business_street.str) ? "" : pst_rfc2426_escape(contact->business_street.str, &result, &resultlen));
01938 fprintf(f_output, "%s;", (!contact->business_city.str) ? "" : pst_rfc2426_escape(contact->business_city.str, &result, &resultlen));
01939 fprintf(f_output, "%s;", (!contact->business_state.str) ? "" : pst_rfc2426_escape(contact->business_state.str, &result, &resultlen));
01940 fprintf(f_output, "%s;", (!contact->business_postal_code.str) ? "" : pst_rfc2426_escape(contact->business_postal_code.str, &result, &resultlen));
01941 fprintf(f_output, "%s\n", (!contact->business_country.str) ? "" : pst_rfc2426_escape(contact->business_country.str, &result, &resultlen));
01942 fprintf(f_output, "LABEL;TYPE=work:%s\n", pst_rfc2426_escape(contact->business_address.str, &result, &resultlen));
01943 }
01944
01945 if (contact->other_address.str) {
01946
01947 fprintf(f_output, "ADR;TYPE=postal:%s;",(!contact->other_po_box.str) ? "" : pst_rfc2426_escape(contact->other_po_box.str, &result, &resultlen));
01948 fprintf(f_output, "%s;", "");
01949 fprintf(f_output, "%s;", (!contact->other_street.str) ? "" : pst_rfc2426_escape(contact->other_street.str, &result, &resultlen));
01950 fprintf(f_output, "%s;", (!contact->other_city.str) ? "" : pst_rfc2426_escape(contact->other_city.str, &result, &resultlen));
01951 fprintf(f_output, "%s;", (!contact->other_state.str) ? "" : pst_rfc2426_escape(contact->other_state.str, &result, &resultlen));
01952 fprintf(f_output, "%s;", (!contact->other_postal_code.str) ? "" : pst_rfc2426_escape(contact->other_postal_code.str, &result, &resultlen));
01953 fprintf(f_output, "%s\n", (!contact->other_country.str) ? "" : pst_rfc2426_escape(contact->other_country.str, &result, &resultlen));
01954 fprintf(f_output, "LABEL;TYPE=postal:%s\n", pst_rfc2426_escape(contact->other_address.str, &result, &resultlen));
01955 }
01956
01957 if (contact->business_fax.str) fprintf(f_output, "TEL;TYPE=work,fax:%s\n", pst_rfc2426_escape(contact->business_fax.str, &result, &resultlen));
01958 if (contact->business_phone.str) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", pst_rfc2426_escape(contact->business_phone.str, &result, &resultlen));
01959 if (contact->business_phone2.str) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", pst_rfc2426_escape(contact->business_phone2.str, &result, &resultlen));
01960 if (contact->car_phone.str) fprintf(f_output, "TEL;TYPE=car,voice:%s\n", pst_rfc2426_escape(contact->car_phone.str, &result, &resultlen));
01961 if (contact->home_fax.str) fprintf(f_output, "TEL;TYPE=home,fax:%s\n", pst_rfc2426_escape(contact->home_fax.str, &result, &resultlen));
01962 if (contact->home_phone.str) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", pst_rfc2426_escape(contact->home_phone.str, &result, &resultlen));
01963 if (contact->home_phone2.str) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", pst_rfc2426_escape(contact->home_phone2.str, &result, &resultlen));
01964 if (contact->isdn_phone.str) fprintf(f_output, "TEL;TYPE=isdn:%s\n", pst_rfc2426_escape(contact->isdn_phone.str, &result, &resultlen));
01965 if (contact->mobile_phone.str) fprintf(f_output, "TEL;TYPE=cell,voice:%s\n", pst_rfc2426_escape(contact->mobile_phone.str, &result, &resultlen));
01966 if (contact->other_phone.str) fprintf(f_output, "TEL;TYPE=msg:%s\n", pst_rfc2426_escape(contact->other_phone.str, &result, &resultlen));
01967 if (contact->pager_phone.str) fprintf(f_output, "TEL;TYPE=pager:%s\n", pst_rfc2426_escape(contact->pager_phone.str, &result, &resultlen));
01968 if (contact->primary_fax.str) fprintf(f_output, "TEL;TYPE=fax,pref:%s\n", pst_rfc2426_escape(contact->primary_fax.str, &result, &resultlen));
01969 if (contact->primary_phone.str) fprintf(f_output, "TEL;TYPE=phone,pref:%s\n", pst_rfc2426_escape(contact->primary_phone.str, &result, &resultlen));
01970 if (contact->radio_phone.str) fprintf(f_output, "TEL;TYPE=pcs:%s\n", pst_rfc2426_escape(contact->radio_phone.str, &result, &resultlen));
01971 if (contact->telex.str) fprintf(f_output, "TEL;TYPE=bbs:%s\n", pst_rfc2426_escape(contact->telex.str, &result, &resultlen));
01972 if (contact->job_title.str) fprintf(f_output, "TITLE:%s\n", pst_rfc2426_escape(contact->job_title.str, &result, &resultlen));
01973 if (contact->profession.str) fprintf(f_output, "ROLE:%s\n", pst_rfc2426_escape(contact->profession.str, &result, &resultlen));
01974 if (contact->assistant_name.str || contact->assistant_phone.str) {
01975 fprintf(f_output, "AGENT:BEGIN:VCARD\n");
01976 if (contact->assistant_name.str) fprintf(f_output, "FN:%s\n", pst_rfc2426_escape(contact->assistant_name.str, &result, &resultlen));
01977 if (contact->assistant_phone.str) fprintf(f_output, "TEL:%s\n", pst_rfc2426_escape(contact->assistant_phone.str, &result, &resultlen));
01978 }
01979 if (contact->company_name.str) fprintf(f_output, "ORG:%s\n", pst_rfc2426_escape(contact->company_name.str, &result, &resultlen));
01980 if (comment) fprintf(f_output, "NOTE:%s\n", pst_rfc2426_escape(comment, &result, &resultlen));
01981 if (item->body.str) fprintf(f_output, "NOTE:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen));
01982
01983 write_extra_categories(f_output, item);
01984
01985 fprintf(f_output, "VERSION: 3.0\n");
01986 fprintf(f_output, "END:VCARD\n\n");
01987 if (result) free(result);
01988 DEBUG_RET();
01989 }
01990
01991
01999 int write_extra_categories(FILE* f_output, pst_item* item)
02000 {
02001 char* result = NULL;
02002 size_t resultlen = 0;
02003 pst_item_extra_field *ef = item->extra_fields;
02004 const char *fmt = "CATEGORIES:%s";
02005 int category_started = 0;
02006 while (ef) {
02007 if (strcmp(ef->field_name, "Keywords") == 0) {
02008 fprintf(f_output, fmt, pst_rfc2426_escape(ef->value, &result, &resultlen));
02009 fmt = ", %s";
02010 category_started = 1;
02011 }
02012 ef = ef->next;
02013 }
02014 if (category_started) fprintf(f_output, "\n");
02015 if (result) free(result);
02016 return category_started;
02017 }
02018
02019
02020 void write_journal(FILE* f_output, pst_item* item)
02021 {
02022 char* result = NULL;
02023 size_t resultlen = 0;
02024 char time_buffer[30];
02025 pst_item_journal* journal = item->journal;
02026
02027
02028 pst_convert_utf8_null(item, &item->subject);
02029 pst_convert_utf8_null(item, &item->body);
02030
02031 fprintf(f_output, "BEGIN:VJOURNAL\n");
02032 fprintf(f_output, "DTSTAMP:%s\n", pst_rfc2445_datetime_format_now(sizeof(time_buffer), time_buffer));
02033 if (item->create_date)
02034 fprintf(f_output, "CREATED:%s\n", pst_rfc2445_datetime_format(item->create_date, sizeof(time_buffer), time_buffer));
02035 if (item->modify_date)
02036 fprintf(f_output, "LAST-MOD:%s\n", pst_rfc2445_datetime_format(item->modify_date, sizeof(time_buffer), time_buffer));
02037 if (item->subject.str)
02038 fprintf(f_output, "SUMMARY:%s\n", pst_rfc2426_escape(item->subject.str, &result, &resultlen));
02039 if (item->body.str)
02040 fprintf(f_output, "DESCRIPTION:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen));
02041 if (journal && journal->start)
02042 fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(journal->start, sizeof(time_buffer), time_buffer));
02043 fprintf(f_output, "END:VJOURNAL\n");
02044 if (result) free(result);
02045 }
02046
02047
02048 void write_appointment(FILE* f_output, pst_item* item)
02049 {
02050 char* result = NULL;
02051 size_t resultlen = 0;
02052 char time_buffer[30];
02053 pst_item_appointment* appointment = item->appointment;
02054
02055
02056 pst_convert_utf8_null(item, &item->subject);
02057 pst_convert_utf8_null(item, &item->body);
02058 pst_convert_utf8_null(item, &appointment->location);
02059
02060 fprintf(f_output, "UID:%#"PRIx64"\n", item->block_id);
02061 fprintf(f_output, "DTSTAMP:%s\n", pst_rfc2445_datetime_format_now(sizeof(time_buffer), time_buffer));
02062 if (item->create_date)
02063 fprintf(f_output, "CREATED:%s\n", pst_rfc2445_datetime_format(item->create_date, sizeof(time_buffer), time_buffer));
02064 if (item->modify_date)
02065 fprintf(f_output, "LAST-MOD:%s\n", pst_rfc2445_datetime_format(item->modify_date, sizeof(time_buffer), time_buffer));
02066 if (item->subject.str)
02067 fprintf(f_output, "SUMMARY:%s\n", pst_rfc2426_escape(item->subject.str, &result, &resultlen));
02068 if (item->body.str)
02069 fprintf(f_output, "DESCRIPTION:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen));
02070 if (appointment && appointment->start)
02071 fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(appointment->start, sizeof(time_buffer), time_buffer));
02072 if (appointment && appointment->end)
02073 fprintf(f_output, "DTEND;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(appointment->end, sizeof(time_buffer), time_buffer));
02074 if (appointment && appointment->location.str)
02075 fprintf(f_output, "LOCATION:%s\n", pst_rfc2426_escape(appointment->location.str, &result, &resultlen));
02076 if (appointment) {
02077 switch (appointment->showas) {
02078 case PST_FREEBUSY_TENTATIVE:
02079 fprintf(f_output, "STATUS:TENTATIVE\n");
02080 break;
02081 case PST_FREEBUSY_FREE:
02082
02083 fprintf(f_output, "TRANSP:TRANSPARENT\n");
02084 case PST_FREEBUSY_BUSY:
02085 case PST_FREEBUSY_OUT_OF_OFFICE:
02086 fprintf(f_output, "STATUS:CONFIRMED\n");
02087 break;
02088 }
02089 if (appointment->is_recurring) {
02090 const char* rules[] = {"DAILY", "WEEKLY", "MONTHLY", "YEARLY"};
02091 const char* days[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"};
02092 pst_recurrence *rdata = pst_convert_recurrence(appointment);
02093 fprintf(f_output, "RRULE:FREQ=%s", rules[rdata->type]);
02094 if (rdata->count) fprintf(f_output, ";COUNT=%u", rdata->count);
02095 if ((rdata->interval != 1) &&
02096 (rdata->interval)) fprintf(f_output, ";INTERVAL=%u", rdata->interval);
02097 if (rdata->dayofmonth) fprintf(f_output, ";BYMONTHDAY=%d", rdata->dayofmonth);
02098 if (rdata->monthofyear) fprintf(f_output, ";BYMONTH=%d", rdata->monthofyear);
02099 if (rdata->position) fprintf(f_output, ";BYSETPOS=%d", rdata->position);
02100 if (rdata->bydaymask) {
02101 char byday[40];
02102 int empty = 1;
02103 int i=0;
02104 memset(byday, 0, sizeof(byday));
02105 for (i=0; i<6; i++) {
02106 int bit = 1 << i;
02107 if (bit & rdata->bydaymask) {
02108 char temp[40];
02109 snprintf(temp, sizeof(temp), "%s%s%s", byday, (empty) ? ";BYDAY=" : ";", days[i]);
02110 strcpy(byday, temp);
02111 empty = 0;
02112 }
02113 }
02114 fprintf(f_output, "%s", byday);
02115 }
02116 fprintf(f_output, "\n");
02117 pst_free_recurrence(rdata);
02118 }
02119 switch (appointment->label) {
02120 case PST_APP_LABEL_NONE:
02121 if (!write_extra_categories(f_output, item)) fprintf(f_output, "CATEGORIES:NONE\n");
02122 break;
02123 case PST_APP_LABEL_IMPORTANT:
02124 fprintf(f_output, "CATEGORIES:IMPORTANT\n");
02125 break;
02126 case PST_APP_LABEL_BUSINESS:
02127 fprintf(f_output, "CATEGORIES:BUSINESS\n");
02128 break;
02129 case PST_APP_LABEL_PERSONAL:
02130 fprintf(f_output, "CATEGORIES:PERSONAL\n");
02131 break;
02132 case PST_APP_LABEL_VACATION:
02133 fprintf(f_output, "CATEGORIES:VACATION\n");
02134 break;
02135 case PST_APP_LABEL_MUST_ATTEND:
02136 fprintf(f_output, "CATEGORIES:MUST-ATTEND\n");
02137 break;
02138 case PST_APP_LABEL_TRAVEL_REQ:
02139 fprintf(f_output, "CATEGORIES:TRAVEL-REQUIRED\n");
02140 break;
02141 case PST_APP_LABEL_NEEDS_PREP:
02142 fprintf(f_output, "CATEGORIES:NEEDS-PREPARATION\n");
02143 break;
02144 case PST_APP_LABEL_BIRTHDAY:
02145 fprintf(f_output, "CATEGORIES:BIRTHDAY\n");
02146 break;
02147 case PST_APP_LABEL_ANNIVERSARY:
02148 fprintf(f_output, "CATEGORIES:ANNIVERSARY\n");
02149 break;
02150 case PST_APP_LABEL_PHONE_CALL:
02151 fprintf(f_output, "CATEGORIES:PHONE-CALL\n");
02152 break;
02153 }
02154
02155 if (appointment->alarm && (appointment->alarm_minutes >= 0) && (appointment->alarm_minutes < 1440)) {
02156 fprintf(f_output, "BEGIN:VALARM\n");
02157 fprintf(f_output, "TRIGGER:-PT%dM\n", appointment->alarm_minutes);
02158 fprintf(f_output, "ACTION:DISPLAY\n");
02159 fprintf(f_output, "DESCRIPTION:Reminder\n");
02160 fprintf(f_output, "END:VALARM\n");
02161 }
02162 }
02163 fprintf(f_output, "END:VEVENT\n");
02164 if (result) free(result);
02165 }
02166
02167
02168 void create_enter_dir(struct file_ll* f, pst_item *item)
02169 {
02170 memset(f, 0, sizeof(*f));
02171 f->stored_count = (item->folder) ? item->folder->item_count : 0;
02172 pst_convert_utf8(item, &item->file_as);
02173 f->dname = (char*) pst_malloc(strlen(item->file_as.str)+1);
02174 strcpy(f->dname, item->file_as.str);
02175
02176 DEBUG_ENT("create_enter_dir");
02177 if (mode == MODE_KMAIL) {
02178 int32_t t;
02179 mk_kmail_dir(item->file_as.str);
02180 for (t=0; t<PST_TYPE_MAX; t++) {
02181 if (t == reduced_item_type(t)) {
02182 f->name[t] = (char*) pst_malloc(strlen(item->file_as.str)+strlen(OUTPUT_TEMPLATE)+30);
02183 sprintf(f->name[t], OUTPUT_TEMPLATE, item->file_as.str, item_type_to_name(t));
02184 }
02185 }
02186 } else if (mode == MODE_RECURSE) {
02187 int32_t t;
02188 mk_recurse_dir(item->file_as.str);
02189 for (t=0; t<PST_TYPE_MAX; t++) {
02190 if (t == reduced_item_type(t)) {
02191 f->name[t] = strdup(item_type_to_name(t));
02192 }
02193 }
02194 if (mode_thunder) {
02195 FILE *type_file = fopen(".type", "w");
02196 fprintf(type_file, "%d\n", item->type);
02197 fclose(type_file);
02198 }
02199 } else if (mode == MODE_SEPARATE) {
02200
02201 int32_t t;
02202 mk_separate_dir(item->file_as.str);
02203 for (t=0; t<PST_TYPE_MAX; t++) {
02204 if (t == reduced_item_type(t)) {
02205 f->name[t] = (char*) pst_malloc(file_name_len);
02206 memset(f->name[t], 0, file_name_len);
02207 }
02208 }
02209 } else {
02210
02211 int32_t t;
02212 for (t=0; t<PST_TYPE_MAX; t++) {
02213 if (t == reduced_item_type(t)) {
02214 f->name[t] = (char*) pst_malloc(strlen(item->file_as.str)+strlen(OUTPUT_TEMPLATE)+30);
02215 sprintf(f->name[t], OUTPUT_TEMPLATE, item->file_as.str, item_type_to_name(t));
02216 }
02217 }
02218 }
02219
02220 if (mode != MODE_SEPARATE) {
02221 int32_t t;
02222 for (t=0; t<PST_TYPE_MAX; t++) {
02223 if (f->name[t]) {
02224 if (!overwrite) {
02225 int x = 0;
02226 char *temp = (char*) pst_malloc (strlen(f->name[t])+10);
02227
02228 sprintf(temp, "%s", f->name[t]);
02229 check_filename(temp);
02230 while ((f->output[t] = fopen(temp, "r"))) {
02231 DEBUG_INFO(("need to increase filename because one already exists with that name\n"));
02232 x++;
02233 sprintf(temp, "%s%08d", f->name[t], x);
02234 DEBUG_INFO(("- bump file name and try \"%s\"\n", temp));
02235 if (x == 99999999) {
02236 DIE(("create_enter_dir: Why can I not create a folder %s? I have tried %i extensions...\n", f->name[t], x));
02237 }
02238 fclose(f->output[t]);
02239 }
02240 if (x > 0) {
02241 free (f->name[t]);
02242 f->name[t] = temp;
02243 } else {
02244 free(temp);
02245 }
02246 }
02247 check_filename(f->name[t]);
02248 if (!(f->output[t] = fopen(f->name[t], "w"))) {
02249 DIE(("create_enter_dir: Could not open file \"%s\" for write\n", f->name[t]));
02250 }
02251 DEBUG_INFO(("f->name = %s\nitem->folder_name = %s\n", f->name[t], item->file_as.str));
02252 }
02253 }
02254 }
02255 DEBUG_RET();
02256 }
02257
02258
02259 void close_enter_dir(struct file_ll *f)
02260 {
02261 int32_t t;
02262 DEBUG_INFO(("processed item count for folder %s is %i, skipped %i, total %i \n",
02263 f->dname, f->item_count, f->skip_count, f->stored_count));
02264 if (output_mode != OUTPUT_QUIET) {
02265 pst_debug_lock();
02266 printf("\t\"%s\" - %i items done, %i items skipped.\n", f->dname, f->item_count, f->skip_count);
02267 fflush(stdout);
02268 pst_debug_unlock();
02269 }
02270 for (t=0; t<PST_TYPE_MAX; t++) {
02271 if (f->output[t]) {
02272 if (mode == MODE_SEPARATE) DEBUG_WARN(("close_enter_dir finds open separate file\n"));
02273 fclose(f->output[t]);
02274 f->output[t] = NULL;
02275 }
02276 if (f->name[t]) {
02277 struct stat st;
02278 stat(f->name[t], &st);
02279 if (!st.st_size) {
02280 DEBUG_WARN(("removing empty output file %s\n", f->name[t]));
02281 remove(f->name[t]);
02282 }
02283 free(f->name[t]);
02284 f->name[t] = NULL;
02285 }
02286 }
02287 free(f->dname);
02288
02289 if (mode == MODE_KMAIL)
02290 close_kmail_dir();
02291 else if (mode == MODE_RECURSE) {
02292 if (mode_thunder) {
02293 FILE *type_file = fopen(".size", "w");
02294 fprintf(type_file, "%i %i\n", f->item_count, f->stored_count);
02295 fclose(type_file);
02296 }
02297 close_recurse_dir();
02298 } else if (mode == MODE_SEPARATE)
02299 close_separate_dir();
02300 }
02301