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