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