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