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->filename2.str) {
01194
01195
01196 pst_rfc2231(&attach->filename2);
01197 fprintf(f_output, "Content-Disposition: attachment; \n filename*=%s\n\n", attach->filename2.str);
01198 }
01199 else if (attach->filename1.str) {
01200
01201 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", attach->filename1.str);
01202 }
01203 else {
01204
01205 fprintf(f_output, "Content-Disposition: inline\n\n");
01206 }
01207
01208 (void)pst_attach_to_file_base64(pst, attach, f_output);
01209 fprintf(f_output, "\n\n");
01210 DEBUG_RET();
01211 }
01212
01213
01214 int valid_headers(char *header)
01215 {
01216
01217
01218
01219
01220
01221 if (header) {
01222 if ((strncasecmp(header, "X-Barracuda-URL: ", 17) == 0) ||
01223 (strncasecmp(header, "X-ASG-Debug-ID: ", 16) == 0) ||
01224 (strncasecmp(header, "Return-Path: ", 13) == 0) ||
01225 (strncasecmp(header, "Received: ", 10) == 0) ||
01226 (strncasecmp(header, "Subject: ", 9) == 0) ||
01227 (strncasecmp(header, "Date: ", 6) == 0) ||
01228 (strncasecmp(header, "From: ", 6) == 0) ||
01229 (strncasecmp(header, "X-x: ", 5) == 0) ||
01230 (strncasecmp(header, "Microsoft Mail Internet Headers", 31) == 0)) {
01231 return 1;
01232 }
01233 else {
01234 if (strlen(header) > 2) {
01235 DEBUG_INFO(("Ignore bogus headers = %s\n", header));
01236 }
01237 return 0;
01238 }
01239 }
01240 else return 0;
01241 }
01242
01243
01244 void header_has_field(char *header, char *field, int *flag)
01245 {
01246 DEBUG_ENT("header_has_field");
01247 if (my_stristr(header, field) || (strncasecmp(header, field+1, strlen(field)-1) == 0)) {
01248 DEBUG_INFO(("header block has %s header\n", field+1));
01249 *flag = 1;
01250 }
01251 DEBUG_RET();
01252 }
01253
01254
01255 void header_get_subfield(char *field, const char *subfield, char *body_subfield, size_t size_subfield)
01256 {
01257 if (!field) return;
01258 DEBUG_ENT("header_get_subfield");
01259 char search[60];
01260 snprintf(search, sizeof(search), " %s=", subfield);
01261 field++;
01262 char *n = header_end_field(field);
01263 char *s = my_stristr(field, search);
01264 if (n && s && (s < n)) {
01265 char *e, *f, save;
01266 s += strlen(search);
01267 if (*s == '"') {
01268 s++;
01269 e = strchr(s, '"');
01270 }
01271 else {
01272 e = strchr(s, ';');
01273 f = strchr(s, '\n');
01274 if (e && f && (f < e)) e = f;
01275 }
01276 if (!e || (e > n)) e = n;
01277 save = *e;
01278 *e = '\0';
01279 snprintf(body_subfield, size_subfield, "%s", s);
01280 *e = save;
01281 DEBUG_INFO(("body %s %s from headers\n", subfield, body_subfield));
01282 }
01283 DEBUG_RET();
01284 }
01285
01286 char* header_get_field(char *header, char *field)
01287 {
01288 char *t = my_stristr(header, field);
01289 if (!t && (strncasecmp(header, field+1, strlen(field)-1) == 0)) t = header;
01290 return t;
01291 }
01292
01293
01294
01295
01296 char *header_end_field(char *field)
01297 {
01298 char *e = strchr(field+1, '\n');
01299 while (e && ((e[1] == ' ') || (e[1] == '\t'))) {
01300 e = strchr(e+1, '\n');
01301 }
01302 return e;
01303 }
01304
01305
01306 void header_strip_field(char *header, char *field)
01307 {
01308 char *t;
01309 while ((t = header_get_field(header, field))) {
01310 char *e = header_end_field(t);
01311 if (e) {
01312 if (t == header) e++;
01313 while (*e != '\0') {
01314 *t = *e;
01315 t++;
01316 e++;
01317 }
01318 *t = '\0';
01319 }
01320 else {
01321
01322 *t = '\0';
01323 }
01324 }
01325 }
01326
01327
01328 int test_base64(char *body, size_t len)
01329 {
01330 int b64 = 0;
01331 uint8_t *b = (uint8_t *)body;
01332 DEBUG_ENT("test_base64");
01333 while (len--) {
01334 if ((*b < 32) && (*b != 9) && (*b != 10)) {
01335 DEBUG_INFO(("found base64 byte %d\n", (int)*b));
01336 DEBUG_HEXDUMPC(body, strlen(body), 0x10);
01337 b64 = 1;
01338 break;
01339 }
01340 b++;
01341 }
01342 DEBUG_RET();
01343 return b64;
01344 }
01345
01346
01347 void find_html_charset(char *html, char *charset, size_t charsetlen)
01348 {
01349 const int index = 1;
01350 const int nmatch = index+1;
01351 regmatch_t match[nmatch];
01352 DEBUG_ENT("find_html_charset");
01353 int rc = regexec(&meta_charset_pattern, html, nmatch, match, 0);
01354 if (rc == 0) {
01355 int s = match[index].rm_so;
01356 int e = match[index].rm_eo;
01357 if (s != -1) {
01358 char save = html[e];
01359 html[e] = '\0';
01360 snprintf(charset, charsetlen, "%s", html+s);
01361 html[e] = save;
01362 DEBUG_INFO(("charset %s from html text\n", charset));
01363 }
01364 else {
01365 DEBUG_INFO(("matching %d %d %d %d\n", match[0].rm_so, match[0].rm_eo, match[1].rm_so, match[1].rm_eo));
01366 DEBUG_HEXDUMPC(html, strlen(html), 0x10);
01367 }
01368 }
01369 else {
01370 DEBUG_INFO(("regexec returns %d\n", rc));
01371 }
01372 DEBUG_RET();
01373 }
01374
01375
01376 void find_rfc822_headers(char** extra_mime_headers)
01377 {
01378 DEBUG_ENT("find_rfc822_headers");
01379 char *headers = *extra_mime_headers;
01380 if (headers) {
01381 char *temp, *t;
01382 while ((temp = strstr(headers, "\n\n"))) {
01383 temp[1] = '\0';
01384 t = header_get_field(headers, "\nContent-Type:");
01385 if (t) {
01386 t++;
01387 DEBUG_INFO(("found content type header\n"));
01388 char *n = strchr(t, '\n');
01389 char *s = strstr(t, ": ");
01390 char *e = strchr(t, ';');
01391 if (!e || (e > n)) e = n;
01392 if (s && (s < e)) {
01393 s += 2;
01394 if (!strncasecmp(s, RFC822, e-s)) {
01395 headers = temp+2;
01396 DEBUG_INFO(("found 822 headers\n%s\n", headers));
01397 break;
01398 }
01399 }
01400 }
01401
01402 headers = temp+2;
01403 }
01404 *extra_mime_headers = headers;
01405 }
01406 DEBUG_RET();
01407 }
01408
01409
01410 void write_body_part(FILE* f_output, pst_string *body, char *mime, char *charset, char *boundary, pst_file* pst)
01411 {
01412 DEBUG_ENT("write_body_part");
01413 removeCR(body->str);
01414 size_t body_len = strlen(body->str);
01415
01416 if (body->is_utf8 && (strcasecmp("utf-8", charset))) {
01417 if (prefer_utf8) {
01418 charset = "utf-8";
01419 } else {
01420
01421
01422
01423 size_t rc;
01424 DEBUG_INFO(("Convert %s utf-8 to %s\n", mime, charset));
01425 pst_vbuf *newer = pst_vballoc(2);
01426 rc = pst_vb_utf8to8bit(newer, body->str, body_len, charset);
01427 if (rc == (size_t)-1) {
01428
01429 free(newer->b);
01430 DEBUG_INFO(("Failed to convert %s utf-8 to %s\n", mime, charset));
01431 charset = "utf-8";
01432 } else {
01433
01434 pst_vbgrow(newer, 1);
01435 newer->b[newer->dlen] = '\0';
01436 free(body->str);
01437 body->str = newer->b;
01438 body_len = newer->dlen;
01439 }
01440 free(newer);
01441 }
01442 }
01443 int base64 = test_base64(body->str, body_len);
01444 fprintf(f_output, "\n--%s\n", boundary);
01445 fprintf(f_output, "Content-Type: %s; charset=\"%s\"\n", mime, charset);
01446 if (base64) fprintf(f_output, "Content-Transfer-Encoding: base64\n");
01447 fprintf(f_output, "\n");
01448
01449 if (base64) {
01450 char *enc = pst_base64_encode(body->str, body_len);
01451 if (enc) {
01452 write_email_body(f_output, enc);
01453 fprintf(f_output, "\n");
01454 free(enc);
01455 }
01456 }
01457 else {
01458 write_email_body(f_output, body->str);
01459 }
01460 DEBUG_RET();
01461 }
01462
01463
01464 void write_schedule_part_data(FILE* f_output, pst_item* item, const char* sender, const char* method)
01465 {
01466 fprintf(f_output, "BEGIN:VCALENDAR\n");
01467 fprintf(f_output, "VERSION:2.0\n");
01468 fprintf(f_output, "PRODID:LibPST v%s\n", VERSION);
01469 if (method) fprintf(f_output, "METHOD:%s\n", method);
01470 fprintf(f_output, "BEGIN:VEVENT\n");
01471 if (sender) {
01472 if (item->email->outlook_sender_name.str) {
01473 fprintf(f_output, "ORGANIZER;CN=\"%s\":MAILTO:%s\n", item->email->outlook_sender_name.str, sender);
01474 } else {
01475 fprintf(f_output, "ORGANIZER;CN=\"\":MAILTO:%s\n", sender);
01476 }
01477 }
01478 write_appointment(f_output, item);
01479 fprintf(f_output, "END:VCALENDAR\n");
01480 }
01481
01482
01483 void write_schedule_part(FILE* f_output, pst_item* item, const char* sender, const char* boundary)
01484 {
01485 const char* method = "REQUEST";
01486 const char* charset = "utf-8";
01487 char fname[30];
01488 if (!item->appointment) return;
01489
01490
01491 fprintf(f_output, "\n--%s\n", boundary);
01492 fprintf(f_output, "Content-Type: %s; method=\"%s\"; charset=\"%s\"\n\n", "text/calendar", method, charset);
01493 write_schedule_part_data(f_output, item, sender, method);
01494 fprintf(f_output, "\n");
01495
01496
01497 snprintf(fname, sizeof(fname), "i%i.ics", rand());
01498 fprintf(f_output, "\n--%s\n", boundary);
01499 fprintf(f_output, "Content-Type: %s; charset=\"%s\"; name=\"%s\"\n", "text/calendar", "utf-8", fname);
01500 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", fname);
01501 write_schedule_part_data(f_output, item, sender, method);
01502 fprintf(f_output, "\n");
01503 }
01504
01505
01506 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)
01507 {
01508 char boundary[60];
01509 char altboundary[66];
01510 char *altboundaryp = NULL;
01511 char body_charset[30];
01512 char buffer_charset[30];
01513 char body_report[60];
01514 char sender[60];
01515 int sender_known = 0;
01516 char *temp = NULL;
01517 time_t em_time;
01518 char *c_time;
01519 char *headers = NULL;
01520 int has_from, has_subject, has_to, has_cc, has_date, has_msgid;
01521 has_from = has_subject = has_to = has_cc = has_date = has_msgid = 0;
01522 DEBUG_ENT("write_normal_email");
01523
01524 pst_convert_utf8_null(item, &item->email->header);
01525 headers = valid_headers(item->email->header.str) ? item->email->header.str :
01526 valid_headers(*extra_mime_headers) ? *extra_mime_headers :
01527 NULL;
01528
01529
01530 strncpy(body_charset, pst_default_charset(item, sizeof(buffer_charset), buffer_charset), sizeof(body_charset));
01531 body_charset[sizeof(body_charset)-1] = '\0';
01532 strncpy(body_report, "delivery-status", sizeof(body_report));
01533 body_report[sizeof(body_report)-1] = '\0';
01534
01535
01536 pst_convert_utf8(item, &item->email->sender_address);
01537 if (item->email->sender_address.str && strchr(item->email->sender_address.str, '@')) {
01538 temp = item->email->sender_address.str;
01539 sender_known = 1;
01540 }
01541 else {
01542 temp = "MAILER-DAEMON";
01543 }
01544 strncpy(sender, temp, sizeof(sender));
01545 sender[sizeof(sender)-1] = '\0';
01546
01547
01548 if (item->email->sent_date) {
01549 em_time = pst_fileTimeToUnixTime(item->email->sent_date);
01550 c_time = ctime(&em_time);
01551 if (c_time)
01552 c_time[strlen(c_time)-1] = '\0';
01553 else
01554 c_time = "Thu Jan 1 00:00:00 1970";
01555 } else
01556 c_time = "Thu Jan 1 00:00:00 1970";
01557
01558
01559 snprintf(boundary, sizeof(boundary), "--boundary-LibPST-iamunique-%i_-_-", rand());
01560 snprintf(altboundary, sizeof(altboundary), "alt-%s", boundary);
01561
01562
01563 if (headers ) {
01564 char *t;
01565 removeCR(headers);
01566
01567 temp = strstr(headers, "\n\n");
01568 if (temp) {
01569
01570 temp[1] = '\0';
01571
01572
01573
01574 if (!*extra_mime_headers) *extra_mime_headers = temp+2;
01575 DEBUG_INFO(("Found extra mime headers\n%s\n", temp+2));
01576 }
01577
01578
01579 header_has_field(headers, "\nFrom:", &has_from);
01580 header_has_field(headers, "\nTo:", &has_to);
01581 header_has_field(headers, "\nSubject:", &has_subject);
01582 header_has_field(headers, "\nDate:", &has_date);
01583 header_has_field(headers, "\nCC:", &has_cc);
01584 header_has_field(headers, "\nMessage-Id:", &has_msgid);
01585
01586
01587 t = header_get_field(headers, "\nContent-Type:");
01588 header_get_subfield(t, "charset", body_charset, sizeof(body_charset));
01589 header_get_subfield(t, "report-type", body_report, sizeof(body_report));
01590
01591
01592 if (!sender_known) {
01593 t = header_get_field(headers, "\nFrom:");
01594 if (t) {
01595
01596 t++;
01597 char *n = strchr(t, '\n');
01598 char *s = strchr(t, '<');
01599 char *e = strchr(t, '>');
01600 if (s && e && n && (s < e) && (e < n)) {
01601 char save = *e;
01602 *e = '\0';
01603 snprintf(sender, sizeof(sender), "%s", s+1);
01604 *e = save;
01605 }
01606 }
01607 }
01608
01609
01610 header_strip_field(headers, "\nMicrosoft Mail Internet Headers");
01611 header_strip_field(headers, "\nMIME-Version:");
01612 header_strip_field(headers, "\nContent-Type:");
01613 header_strip_field(headers, "\nContent-Transfer-Encoding:");
01614 header_strip_field(headers, "\nContent-class:");
01615 header_strip_field(headers, "\nX-MimeOLE:");
01616 header_strip_field(headers, "\nX-From_:");
01617 }
01618
01619 DEBUG_INFO(("About to print Header\n"));
01620
01621 if (item && item->subject.str) {
01622 pst_convert_utf8(item, &item->subject);
01623 DEBUG_INFO(("item->subject = %s\n", item->subject.str));
01624 }
01625
01626 if (mode != MODE_SEPARATE) {
01627
01628
01629
01630
01631 char *quo = (embedding) ? ">" : "";
01632 fprintf(f_output, "%sFrom \"%s\" %s\n", quo, sender, c_time);
01633 }
01634
01635
01636 if (headers) {
01637 int len = strlen(headers);
01638 if (len > 0) {
01639 fprintf(f_output, "%s", headers);
01640
01641 if (headers[len-1] != '\n') fprintf(f_output, "\n");
01642
01643
01644
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654 }
01655 }
01656
01657
01658 if ((item->flags & PST_FLAG_READ) == PST_FLAG_READ) {
01659 fprintf(f_output, "Status: RO\n");
01660 }
01661
01662
01663
01664 if (!has_from) {
01665 if (item->email->outlook_sender_name.str){
01666 pst_rfc2047(item, &item->email->outlook_sender_name, 1);
01667 fprintf(f_output, "From: %s <%s>\n", item->email->outlook_sender_name.str, sender);
01668 } else {
01669 fprintf(f_output, "From: <%s>\n", sender);
01670 }
01671 }
01672
01673 if (!has_subject) {
01674 if (item->subject.str) {
01675 pst_rfc2047(item, &item->subject, 0);
01676 fprintf(f_output, "Subject: %s\n", item->subject.str);
01677 } else {
01678 fprintf(f_output, "Subject: \n");
01679 }
01680 }
01681
01682 if (!has_to && item->email->sentto_address.str) {
01683 pst_rfc2047(item, &item->email->sentto_address, 0);
01684 fprintf(f_output, "To: %s\n", item->email->sentto_address.str);
01685 }
01686
01687 if (!has_cc && item->email->cc_address.str) {
01688 pst_rfc2047(item, &item->email->cc_address, 0);
01689 fprintf(f_output, "Cc: %s\n", item->email->cc_address.str);
01690 }
01691
01692 if (!has_date && item->email->sent_date) {
01693 char c_time[C_TIME_SIZE];
01694 struct tm stm;
01695 gmtime_r(&em_time, &stm);
01696 strftime(c_time, C_TIME_SIZE, "%a, %d %b %Y %H:%M:%S %z", &stm);
01697 fprintf(f_output, "Date: %s\n", c_time);
01698 }
01699
01700 if (!has_msgid && item->email->messageid.str) {
01701 pst_convert_utf8(item, &item->email->messageid);
01702 fprintf(f_output, "Message-Id: %s\n", item->email->messageid.str);
01703 }
01704
01705
01706
01707 pst_convert_utf8_null(item, &item->email->sender_address);
01708 if (item->email->sender_address.str && !strchr(item->email->sender_address.str, '@')
01709 && strcmp(item->email->sender_address.str, ".")
01710 && (strlen(item->email->sender_address.str) > 0)) {
01711 fprintf(f_output, "X-libpst-forensic-sender: %s\n", item->email->sender_address.str);
01712 }
01713
01714 if (item->email->bcc_address.str) {
01715 pst_convert_utf8(item, &item->email->bcc_address);
01716 fprintf(f_output, "X-libpst-forensic-bcc: %s\n", item->email->bcc_address.str);
01717 }
01718
01719
01720 fprintf(f_output, "MIME-Version: 1.0\n");
01721 if (item->type == PST_TYPE_REPORT) {
01722
01723 fprintf(f_output, "Content-Type: multipart/report; report-type=%s;\n\tboundary=\"%s\"\n", body_report, boundary);
01724 }
01725 else {
01726 fprintf(f_output, "Content-Type: multipart/mixed;\n\tboundary=\"%s\"\n", boundary);
01727 }
01728 fprintf(f_output, "\n");
01729
01730
01731 if ((item->type == PST_TYPE_REPORT) && (item->email->report_text.str)) {
01732 write_body_part(f_output, &item->email->report_text, "text/plain", body_charset, boundary, pst);
01733 fprintf(f_output, "\n");
01734 }
01735
01736 if (item->body.str && item->email->htmlbody.str) {
01737
01738 fprintf(f_output, "\n--%s\n", boundary);
01739 fprintf(f_output, "Content-Type: multipart/alternative;\n\tboundary=\"%s\"\n", altboundary);
01740 altboundaryp = altboundary;
01741 }
01742 else {
01743 altboundaryp = boundary;
01744 }
01745
01746 if (item->body.str) {
01747 write_body_part(f_output, &item->body, "text/plain", body_charset, altboundaryp, pst);
01748 }
01749
01750 if (item->email->htmlbody.str) {
01751 find_html_charset(item->email->htmlbody.str, body_charset, sizeof(body_charset));
01752 write_body_part(f_output, &item->email->htmlbody, "text/html", body_charset, altboundaryp, pst);
01753 }
01754
01755 if (item->body.str && item->email->htmlbody.str) {
01756
01757 fprintf(f_output, "\n--%s--\n", altboundary);
01758 }
01759
01760 if (item->email->rtf_compressed.data && save_rtf) {
01761 pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach));
01762 DEBUG_INFO(("Adding RTF body as attachment\n"));
01763 memset(attach, 0, sizeof(pst_item_attach));
01764 attach->next = item->attach;
01765 item->attach = attach;
01766 attach->data.data = pst_lzfu_decompress(item->email->rtf_compressed.data, item->email->rtf_compressed.size, &attach->data.size);
01767 attach->filename2.str = strdup(RTF_ATTACH_NAME);
01768 attach->filename2.is_utf8 = 1;
01769 attach->mimetype.str = strdup(RTF_ATTACH_TYPE);
01770 attach->mimetype.is_utf8 = 1;
01771 }
01772
01773 if (item->email->encrypted_body.data) {
01774 pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach));
01775 DEBUG_INFO(("Adding encrypted text body as attachment\n"));
01776 attach = (pst_item_attach*) pst_malloc(sizeof(pst_item_attach));
01777 memset(attach, 0, sizeof(pst_item_attach));
01778 attach->next = item->attach;
01779 item->attach = attach;
01780 attach->data.data = item->email->encrypted_body.data;
01781 attach->data.size = item->email->encrypted_body.size;
01782 item->email->encrypted_body.data = NULL;
01783 }
01784
01785 if (item->email->encrypted_htmlbody.data) {
01786 pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach));
01787 DEBUG_INFO(("Adding encrypted HTML body as attachment\n"));
01788 attach = (pst_item_attach*) pst_malloc(sizeof(pst_item_attach));
01789 memset(attach, 0, sizeof(pst_item_attach));
01790 attach->next = item->attach;
01791 item->attach = attach;
01792 attach->data.data = item->email->encrypted_htmlbody.data;
01793 attach->data.size = item->email->encrypted_htmlbody.size;
01794 item->email->encrypted_htmlbody.data = NULL;
01795 }
01796
01797 if (item->type == PST_TYPE_SCHEDULE) {
01798 write_schedule_part(f_output, item, sender, boundary);
01799 }
01800
01801
01802 {
01803 pst_item_attach* attach;
01804 int attach_num = 0;
01805 for (attach = item->attach; attach; attach = attach->next) {
01806 pst_convert_utf8_null(item, &attach->filename1);
01807 pst_convert_utf8_null(item, &attach->filename2);
01808 pst_convert_utf8_null(item, &attach->mimetype);
01809 DEBUG_INFO(("Attempting Attachment encoding\n"));
01810 if (attach->method == PST_ATTACH_EMBEDDED) {
01811 DEBUG_INFO(("have an embedded rfc822 message attachment\n"));
01812 if (attach->mimetype.str) {
01813 DEBUG_INFO(("which already has a mime-type of %s\n", attach->mimetype.str));
01814 free(attach->mimetype.str);
01815 }
01816 attach->mimetype.str = strdup(RFC822);
01817 attach->mimetype.is_utf8 = 1;
01818 find_rfc822_headers(extra_mime_headers);
01819 write_embedded_message(f_output, attach, boundary, pst, save_rtf, extra_mime_headers);
01820 }
01821 else if (attach->data.data || attach->i_id) {
01822 if (acceptable_ext(attach)) {
01823 if (mode == MODE_SEPARATE && !mode_MH)
01824 write_separate_attachment(f_name, attach, ++attach_num, pst);
01825 else
01826 write_inline_attachment(f_output, attach, boundary, pst);
01827 }
01828 }
01829 }
01830 }
01831
01832 fprintf(f_output, "\n--%s--\n\n", boundary);
01833 DEBUG_RET();
01834 }
01835
01836
01837 void write_vcard(FILE* f_output, pst_item* item, pst_item_contact* contact, char comment[])
01838 {
01839 char* result = NULL;
01840 size_t resultlen = 0;
01841 char time_buffer[30];
01842
01843
01844
01845
01846 DEBUG_ENT("write_vcard");
01847
01848
01849 pst_convert_utf8_null(item, &contact->fullname);
01850 pst_convert_utf8_null(item, &contact->surname);
01851 pst_convert_utf8_null(item, &contact->first_name);
01852 pst_convert_utf8_null(item, &contact->middle_name);
01853 pst_convert_utf8_null(item, &contact->display_name_prefix);
01854 pst_convert_utf8_null(item, &contact->suffix);
01855 pst_convert_utf8_null(item, &contact->nickname);
01856 pst_convert_utf8_null(item, &contact->address1);
01857 pst_convert_utf8_null(item, &contact->address2);
01858 pst_convert_utf8_null(item, &contact->address3);
01859 pst_convert_utf8_null(item, &contact->home_po_box);
01860 pst_convert_utf8_null(item, &contact->home_street);
01861 pst_convert_utf8_null(item, &contact->home_city);
01862 pst_convert_utf8_null(item, &contact->home_state);
01863 pst_convert_utf8_null(item, &contact->home_postal_code);
01864 pst_convert_utf8_null(item, &contact->home_country);
01865 pst_convert_utf8_null(item, &contact->home_address);
01866 pst_convert_utf8_null(item, &contact->business_po_box);
01867 pst_convert_utf8_null(item, &contact->business_street);
01868 pst_convert_utf8_null(item, &contact->business_city);
01869 pst_convert_utf8_null(item, &contact->business_state);
01870 pst_convert_utf8_null(item, &contact->business_postal_code);
01871 pst_convert_utf8_null(item, &contact->business_country);
01872 pst_convert_utf8_null(item, &contact->business_address);
01873 pst_convert_utf8_null(item, &contact->other_po_box);
01874 pst_convert_utf8_null(item, &contact->other_street);
01875 pst_convert_utf8_null(item, &contact->other_city);
01876 pst_convert_utf8_null(item, &contact->other_state);
01877 pst_convert_utf8_null(item, &contact->other_postal_code);
01878 pst_convert_utf8_null(item, &contact->other_country);
01879 pst_convert_utf8_null(item, &contact->other_address);
01880 pst_convert_utf8_null(item, &contact->business_fax);
01881 pst_convert_utf8_null(item, &contact->business_phone);
01882 pst_convert_utf8_null(item, &contact->business_phone2);
01883 pst_convert_utf8_null(item, &contact->car_phone);
01884 pst_convert_utf8_null(item, &contact->home_fax);
01885 pst_convert_utf8_null(item, &contact->home_phone);
01886 pst_convert_utf8_null(item, &contact->home_phone2);
01887 pst_convert_utf8_null(item, &contact->isdn_phone);
01888 pst_convert_utf8_null(item, &contact->mobile_phone);
01889 pst_convert_utf8_null(item, &contact->other_phone);
01890 pst_convert_utf8_null(item, &contact->pager_phone);
01891 pst_convert_utf8_null(item, &contact->primary_fax);
01892 pst_convert_utf8_null(item, &contact->primary_phone);
01893 pst_convert_utf8_null(item, &contact->radio_phone);
01894 pst_convert_utf8_null(item, &contact->telex);
01895 pst_convert_utf8_null(item, &contact->job_title);
01896 pst_convert_utf8_null(item, &contact->profession);
01897 pst_convert_utf8_null(item, &contact->assistant_name);
01898 pst_convert_utf8_null(item, &contact->assistant_phone);
01899 pst_convert_utf8_null(item, &contact->company_name);
01900 pst_convert_utf8_null(item, &item->body);
01901
01902
01903 fprintf(f_output, "BEGIN:VCARD\n");
01904 fprintf(f_output, "FN:%s\n", pst_rfc2426_escape(contact->fullname.str, &result, &resultlen));
01905
01906
01907 fprintf(f_output, "N:%s;", (!contact->surname.str) ? "" : pst_rfc2426_escape(contact->surname.str, &result, &resultlen));
01908 fprintf(f_output, "%s;", (!contact->first_name.str) ? "" : pst_rfc2426_escape(contact->first_name.str, &result, &resultlen));
01909 fprintf(f_output, "%s;", (!contact->middle_name.str) ? "" : pst_rfc2426_escape(contact->middle_name.str, &result, &resultlen));
01910 fprintf(f_output, "%s;", (!contact->display_name_prefix.str) ? "" : pst_rfc2426_escape(contact->display_name_prefix.str, &result, &resultlen));
01911 fprintf(f_output, "%s\n", (!contact->suffix.str) ? "" : pst_rfc2426_escape(contact->suffix.str, &result, &resultlen));
01912
01913 if (contact->nickname.str)
01914 fprintf(f_output, "NICKNAME:%s\n", pst_rfc2426_escape(contact->nickname.str, &result, &resultlen));
01915 if (contact->address1.str)
01916 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address1.str, &result, &resultlen));
01917 if (contact->address2.str)
01918 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address2.str, &result, &resultlen));
01919 if (contact->address3.str)
01920 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address3.str, &result, &resultlen));
01921 if (contact->birthday)
01922 fprintf(f_output, "BDAY:%s\n", pst_rfc2425_datetime_format(contact->birthday, sizeof(time_buffer), time_buffer));
01923
01924 if (contact->home_address.str) {
01925
01926 fprintf(f_output, "ADR;TYPE=home:%s;", (!contact->home_po_box.str) ? "" : pst_rfc2426_escape(contact->home_po_box.str, &result, &resultlen));
01927 fprintf(f_output, "%s;", "");
01928 fprintf(f_output, "%s;", (!contact->home_street.str) ? "" : pst_rfc2426_escape(contact->home_street.str, &result, &resultlen));
01929 fprintf(f_output, "%s;", (!contact->home_city.str) ? "" : pst_rfc2426_escape(contact->home_city.str, &result, &resultlen));
01930 fprintf(f_output, "%s;", (!contact->home_state.str) ? "" : pst_rfc2426_escape(contact->home_state.str, &result, &resultlen));
01931 fprintf(f_output, "%s;", (!contact->home_postal_code.str) ? "" : pst_rfc2426_escape(contact->home_postal_code.str, &result, &resultlen));
01932 fprintf(f_output, "%s\n", (!contact->home_country.str) ? "" : pst_rfc2426_escape(contact->home_country.str, &result, &resultlen));
01933 fprintf(f_output, "LABEL;TYPE=home:%s\n", pst_rfc2426_escape(contact->home_address.str, &result, &resultlen));
01934 }
01935
01936 if (contact->business_address.str) {
01937
01938 fprintf(f_output, "ADR;TYPE=work:%s;", (!contact->business_po_box.str) ? "" : pst_rfc2426_escape(contact->business_po_box.str, &result, &resultlen));
01939 fprintf(f_output, "%s;", "");
01940 fprintf(f_output, "%s;", (!contact->business_street.str) ? "" : pst_rfc2426_escape(contact->business_street.str, &result, &resultlen));
01941 fprintf(f_output, "%s;", (!contact->business_city.str) ? "" : pst_rfc2426_escape(contact->business_city.str, &result, &resultlen));
01942 fprintf(f_output, "%s;", (!contact->business_state.str) ? "" : pst_rfc2426_escape(contact->business_state.str, &result, &resultlen));
01943 fprintf(f_output, "%s;", (!contact->business_postal_code.str) ? "" : pst_rfc2426_escape(contact->business_postal_code.str, &result, &resultlen));
01944 fprintf(f_output, "%s\n", (!contact->business_country.str) ? "" : pst_rfc2426_escape(contact->business_country.str, &result, &resultlen));
01945 fprintf(f_output, "LABEL;TYPE=work:%s\n", pst_rfc2426_escape(contact->business_address.str, &result, &resultlen));
01946 }
01947
01948 if (contact->other_address.str) {
01949
01950 fprintf(f_output, "ADR;TYPE=postal:%s;",(!contact->other_po_box.str) ? "" : pst_rfc2426_escape(contact->other_po_box.str, &result, &resultlen));
01951 fprintf(f_output, "%s;", "");
01952 fprintf(f_output, "%s;", (!contact->other_street.str) ? "" : pst_rfc2426_escape(contact->other_street.str, &result, &resultlen));
01953 fprintf(f_output, "%s;", (!contact->other_city.str) ? "" : pst_rfc2426_escape(contact->other_city.str, &result, &resultlen));
01954 fprintf(f_output, "%s;", (!contact->other_state.str) ? "" : pst_rfc2426_escape(contact->other_state.str, &result, &resultlen));
01955 fprintf(f_output, "%s;", (!contact->other_postal_code.str) ? "" : pst_rfc2426_escape(contact->other_postal_code.str, &result, &resultlen));
01956 fprintf(f_output, "%s\n", (!contact->other_country.str) ? "" : pst_rfc2426_escape(contact->other_country.str, &result, &resultlen));
01957 fprintf(f_output, "LABEL;TYPE=postal:%s\n", pst_rfc2426_escape(contact->other_address.str, &result, &resultlen));
01958 }
01959
01960 if (contact->business_fax.str) fprintf(f_output, "TEL;TYPE=work,fax:%s\n", pst_rfc2426_escape(contact->business_fax.str, &result, &resultlen));
01961 if (contact->business_phone.str) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", pst_rfc2426_escape(contact->business_phone.str, &result, &resultlen));
01962 if (contact->business_phone2.str) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", pst_rfc2426_escape(contact->business_phone2.str, &result, &resultlen));
01963 if (contact->car_phone.str) fprintf(f_output, "TEL;TYPE=car,voice:%s\n", pst_rfc2426_escape(contact->car_phone.str, &result, &resultlen));
01964 if (contact->home_fax.str) fprintf(f_output, "TEL;TYPE=home,fax:%s\n", pst_rfc2426_escape(contact->home_fax.str, &result, &resultlen));
01965 if (contact->home_phone.str) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", pst_rfc2426_escape(contact->home_phone.str, &result, &resultlen));
01966 if (contact->home_phone2.str) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", pst_rfc2426_escape(contact->home_phone2.str, &result, &resultlen));
01967 if (contact->isdn_phone.str) fprintf(f_output, "TEL;TYPE=isdn:%s\n", pst_rfc2426_escape(contact->isdn_phone.str, &result, &resultlen));
01968 if (contact->mobile_phone.str) fprintf(f_output, "TEL;TYPE=cell,voice:%s\n", pst_rfc2426_escape(contact->mobile_phone.str, &result, &resultlen));
01969 if (contact->other_phone.str) fprintf(f_output, "TEL;TYPE=msg:%s\n", pst_rfc2426_escape(contact->other_phone.str, &result, &resultlen));
01970 if (contact->pager_phone.str) fprintf(f_output, "TEL;TYPE=pager:%s\n", pst_rfc2426_escape(contact->pager_phone.str, &result, &resultlen));
01971 if (contact->primary_fax.str) fprintf(f_output, "TEL;TYPE=fax,pref:%s\n", pst_rfc2426_escape(contact->primary_fax.str, &result, &resultlen));
01972 if (contact->primary_phone.str) fprintf(f_output, "TEL;TYPE=phone,pref:%s\n", pst_rfc2426_escape(contact->primary_phone.str, &result, &resultlen));
01973 if (contact->radio_phone.str) fprintf(f_output, "TEL;TYPE=pcs:%s\n", pst_rfc2426_escape(contact->radio_phone.str, &result, &resultlen));
01974 if (contact->telex.str) fprintf(f_output, "TEL;TYPE=bbs:%s\n", pst_rfc2426_escape(contact->telex.str, &result, &resultlen));
01975 if (contact->job_title.str) fprintf(f_output, "TITLE:%s\n", pst_rfc2426_escape(contact->job_title.str, &result, &resultlen));
01976 if (contact->profession.str) fprintf(f_output, "ROLE:%s\n", pst_rfc2426_escape(contact->profession.str, &result, &resultlen));
01977 if (contact->assistant_name.str || contact->assistant_phone.str) {
01978 fprintf(f_output, "AGENT:BEGIN:VCARD\n");
01979 if (contact->assistant_name.str) fprintf(f_output, "FN:%s\n", pst_rfc2426_escape(contact->assistant_name.str, &result, &resultlen));
01980 if (contact->assistant_phone.str) fprintf(f_output, "TEL:%s\n", pst_rfc2426_escape(contact->assistant_phone.str, &result, &resultlen));
01981 }
01982 if (contact->company_name.str) fprintf(f_output, "ORG:%s\n", pst_rfc2426_escape(contact->company_name.str, &result, &resultlen));
01983 if (comment) fprintf(f_output, "NOTE:%s\n", pst_rfc2426_escape(comment, &result, &resultlen));
01984 if (item->body.str) fprintf(f_output, "NOTE:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen));
01985
01986 write_extra_categories(f_output, item);
01987
01988 fprintf(f_output, "VERSION: 3.0\n");
01989 fprintf(f_output, "END:VCARD\n\n");
01990 if (result) free(result);
01991 DEBUG_RET();
01992 }
01993
01994
02002 int write_extra_categories(FILE* f_output, pst_item* item)
02003 {
02004 char* result = NULL;
02005 size_t resultlen = 0;
02006 pst_item_extra_field *ef = item->extra_fields;
02007 const char *fmt = "CATEGORIES:%s";
02008 int category_started = 0;
02009 while (ef) {
02010 if (strcmp(ef->field_name, "Keywords") == 0) {
02011 fprintf(f_output, fmt, pst_rfc2426_escape(ef->value, &result, &resultlen));
02012 fmt = ", %s";
02013 category_started = 1;
02014 }
02015 ef = ef->next;
02016 }
02017 if (category_started) fprintf(f_output, "\n");
02018 if (result) free(result);
02019 return category_started;
02020 }
02021
02022
02023 void write_journal(FILE* f_output, pst_item* item)
02024 {
02025 char* result = NULL;
02026 size_t resultlen = 0;
02027 char time_buffer[30];
02028 pst_item_journal* journal = item->journal;
02029
02030
02031 pst_convert_utf8_null(item, &item->subject);
02032 pst_convert_utf8_null(item, &item->body);
02033
02034 fprintf(f_output, "BEGIN:VJOURNAL\n");
02035 fprintf(f_output, "DTSTAMP:%s\n", pst_rfc2445_datetime_format_now(sizeof(time_buffer), time_buffer));
02036 if (item->create_date)
02037 fprintf(f_output, "CREATED:%s\n", pst_rfc2445_datetime_format(item->create_date, sizeof(time_buffer), time_buffer));
02038 if (item->modify_date)
02039 fprintf(f_output, "LAST-MOD:%s\n", pst_rfc2445_datetime_format(item->modify_date, sizeof(time_buffer), time_buffer));
02040 if (item->subject.str)
02041 fprintf(f_output, "SUMMARY:%s\n", pst_rfc2426_escape(item->subject.str, &result, &resultlen));
02042 if (item->body.str)
02043 fprintf(f_output, "DESCRIPTION:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen));
02044 if (journal && journal->start)
02045 fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(journal->start, sizeof(time_buffer), time_buffer));
02046 fprintf(f_output, "END:VJOURNAL\n");
02047 if (result) free(result);
02048 }
02049
02050
02051 void write_appointment(FILE* f_output, pst_item* item)
02052 {
02053 char* result = NULL;
02054 size_t resultlen = 0;
02055 char time_buffer[30];
02056 pst_item_appointment* appointment = item->appointment;
02057
02058
02059 pst_convert_utf8_null(item, &item->subject);
02060 pst_convert_utf8_null(item, &item->body);
02061 pst_convert_utf8_null(item, &appointment->location);
02062
02063 fprintf(f_output, "UID:%#"PRIx64"\n", item->block_id);
02064 fprintf(f_output, "DTSTAMP:%s\n", pst_rfc2445_datetime_format_now(sizeof(time_buffer), time_buffer));
02065 if (item->create_date)
02066 fprintf(f_output, "CREATED:%s\n", pst_rfc2445_datetime_format(item->create_date, sizeof(time_buffer), time_buffer));
02067 if (item->modify_date)
02068 fprintf(f_output, "LAST-MOD:%s\n", pst_rfc2445_datetime_format(item->modify_date, sizeof(time_buffer), time_buffer));
02069 if (item->subject.str)
02070 fprintf(f_output, "SUMMARY:%s\n", pst_rfc2426_escape(item->subject.str, &result, &resultlen));
02071 if (item->body.str)
02072 fprintf(f_output, "DESCRIPTION:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen));
02073 if (appointment && appointment->start)
02074 fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(appointment->start, sizeof(time_buffer), time_buffer));
02075 if (appointment && appointment->end)
02076 fprintf(f_output, "DTEND;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(appointment->end, sizeof(time_buffer), time_buffer));
02077 if (appointment && appointment->location.str)
02078 fprintf(f_output, "LOCATION:%s\n", pst_rfc2426_escape(appointment->location.str, &result, &resultlen));
02079 if (appointment) {
02080 switch (appointment->showas) {
02081 case PST_FREEBUSY_TENTATIVE:
02082 fprintf(f_output, "STATUS:TENTATIVE\n");
02083 break;
02084 case PST_FREEBUSY_FREE:
02085
02086 fprintf(f_output, "TRANSP:TRANSPARENT\n");
02087 case PST_FREEBUSY_BUSY:
02088 case PST_FREEBUSY_OUT_OF_OFFICE:
02089 fprintf(f_output, "STATUS:CONFIRMED\n");
02090 break;
02091 }
02092 if (appointment->is_recurring) {
02093 const char* rules[] = {"DAILY", "WEEKLY", "MONTHLY", "YEARLY"};
02094 const char* days[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"};
02095 pst_recurrence *rdata = pst_convert_recurrence(appointment);
02096 fprintf(f_output, "RRULE:FREQ=%s", rules[rdata->type]);
02097 if (rdata->count) fprintf(f_output, ";COUNT=%u", rdata->count);
02098 if ((rdata->interval != 1) &&
02099 (rdata->interval)) fprintf(f_output, ";INTERVAL=%u", rdata->interval);
02100 if (rdata->dayofmonth) fprintf(f_output, ";BYMONTHDAY=%d", rdata->dayofmonth);
02101 if (rdata->monthofyear) fprintf(f_output, ";BYMONTH=%d", rdata->monthofyear);
02102 if (rdata->position) fprintf(f_output, ";BYSETPOS=%d", rdata->position);
02103 if (rdata->bydaymask) {
02104 char byday[40];
02105 int empty = 1;
02106 int i=0;
02107 memset(byday, 0, sizeof(byday));
02108 for (i=0; i<6; i++) {
02109 int bit = 1 << i;
02110 if (bit & rdata->bydaymask) {
02111 char temp[40];
02112 snprintf(temp, sizeof(temp), "%s%s%s", byday, (empty) ? ";BYDAY=" : ";", days[i]);
02113 strcpy(byday, temp);
02114 empty = 0;
02115 }
02116 }
02117 fprintf(f_output, "%s", byday);
02118 }
02119 fprintf(f_output, "\n");
02120 pst_free_recurrence(rdata);
02121 }
02122 switch (appointment->label) {
02123 case PST_APP_LABEL_NONE:
02124 if (!write_extra_categories(f_output, item)) fprintf(f_output, "CATEGORIES:NONE\n");
02125 break;
02126 case PST_APP_LABEL_IMPORTANT:
02127 fprintf(f_output, "CATEGORIES:IMPORTANT\n");
02128 break;
02129 case PST_APP_LABEL_BUSINESS:
02130 fprintf(f_output, "CATEGORIES:BUSINESS\n");
02131 break;
02132 case PST_APP_LABEL_PERSONAL:
02133 fprintf(f_output, "CATEGORIES:PERSONAL\n");
02134 break;
02135 case PST_APP_LABEL_VACATION:
02136 fprintf(f_output, "CATEGORIES:VACATION\n");
02137 break;
02138 case PST_APP_LABEL_MUST_ATTEND:
02139 fprintf(f_output, "CATEGORIES:MUST-ATTEND\n");
02140 break;
02141 case PST_APP_LABEL_TRAVEL_REQ:
02142 fprintf(f_output, "CATEGORIES:TRAVEL-REQUIRED\n");
02143 break;
02144 case PST_APP_LABEL_NEEDS_PREP:
02145 fprintf(f_output, "CATEGORIES:NEEDS-PREPARATION\n");
02146 break;
02147 case PST_APP_LABEL_BIRTHDAY:
02148 fprintf(f_output, "CATEGORIES:BIRTHDAY\n");
02149 break;
02150 case PST_APP_LABEL_ANNIVERSARY:
02151 fprintf(f_output, "CATEGORIES:ANNIVERSARY\n");
02152 break;
02153 case PST_APP_LABEL_PHONE_CALL:
02154 fprintf(f_output, "CATEGORIES:PHONE-CALL\n");
02155 break;
02156 }
02157
02158 if (appointment->alarm && (appointment->alarm_minutes >= 0) && (appointment->alarm_minutes < 1440)) {
02159 fprintf(f_output, "BEGIN:VALARM\n");
02160 fprintf(f_output, "TRIGGER:-PT%dM\n", appointment->alarm_minutes);
02161 fprintf(f_output, "ACTION:DISPLAY\n");
02162 fprintf(f_output, "DESCRIPTION:Reminder\n");
02163 fprintf(f_output, "END:VALARM\n");
02164 }
02165 }
02166 fprintf(f_output, "END:VEVENT\n");
02167 if (result) free(result);
02168 }
02169
02170
02171 void create_enter_dir(struct file_ll* f, pst_item *item)
02172 {
02173 pst_convert_utf8(item, &item->file_as);
02174 f->type = item->type;
02175 f->stored_count = (item->folder) ? item->folder->item_count : 0;
02176
02177 DEBUG_ENT("create_enter_dir");
02178 if (mode == MODE_KMAIL)
02179 f->name = mk_kmail_dir(item->file_as.str);
02180 else if (mode == MODE_RECURSE) {
02181 f->name = mk_recurse_dir(item->file_as.str, f->type);
02182 if (mode_thunder) {
02183 FILE *type_file = fopen(".type", "w");
02184 fprintf(type_file, "%d\n", item->type);
02185 fclose(type_file);
02186 }
02187 } else if (mode == MODE_SEPARATE) {
02188
02189 mk_separate_dir(item->file_as.str);
02190 f->name = (char*) pst_malloc(file_name_len);
02191 memset(f->name, 0, file_name_len);
02192 } else {
02193 f->name = (char*) pst_malloc(strlen(item->file_as.str)+strlen(OUTPUT_TEMPLATE)+1);
02194 sprintf(f->name, OUTPUT_TEMPLATE, item->file_as.str);
02195 }
02196
02197 f->dname = (char*) pst_malloc(strlen(item->file_as.str)+1);
02198 strcpy(f->dname, item->file_as.str);
02199
02200 if (overwrite != 1) {
02201 int x = 0;
02202 char *temp = (char*) pst_malloc (strlen(f->name)+10);
02203
02204 sprintf(temp, "%s", f->name);
02205 check_filename(temp);
02206 while ((f->output = fopen(temp, "r"))) {
02207 DEBUG_INFO(("need to increase filename because one already exists with that name\n"));
02208 DEBUG_INFO(("- increasing it to %s%d\n", f->name, x));
02209 x++;
02210 sprintf(temp, "%s%08d", f->name, x);
02211 DEBUG_INFO(("- trying \"%s\"\n", f->name));
02212 if (x == 99999999) {
02213 DIE(("create_enter_dir: Why can I not create a folder %s? I have tried %i extensions...\n", f->name, x));
02214 }
02215 fclose(f->output);
02216 }
02217 if (x > 0) {
02218 free (f->name);
02219 f->name = temp;
02220 } else {
02221 free(temp);
02222 }
02223 }
02224
02225 DEBUG_INFO(("f->name = %s\nitem->folder_name = %s\n", f->name, item->file_as.str));
02226 if (mode != MODE_SEPARATE) {
02227 check_filename(f->name);
02228 if (!(f->output = fopen(f->name, "w"))) {
02229 DIE(("create_enter_dir: Could not open file \"%s\" for write\n", f->name));
02230 }
02231 }
02232 DEBUG_RET();
02233 }
02234
02235
02236 void close_enter_dir(struct file_ll *f)
02237 {
02238 DEBUG_INFO(("processed item count for folder %s is %i, skipped %i, total %i \n",
02239 f->dname, f->item_count, f->skip_count, f->stored_count));
02240 if (output_mode != OUTPUT_QUIET) {
02241 pst_debug_lock();
02242 printf("\t\"%s\" - %i items done, %i items skipped.\n", f->dname, f->item_count, f->skip_count);
02243 fflush(stdout);
02244 pst_debug_unlock();
02245 }
02246 if (f->output) {
02247 if (mode == MODE_SEPARATE) DEBUG_WARN(("close_enter_dir finds open separate file\n"));
02248 struct stat st;
02249 fclose(f->output);
02250 stat(f->name, &st);
02251 if (!st.st_size) {
02252 DEBUG_WARN(("removing empty output file %s\n", f->name));
02253 remove(f->name);
02254 }
02255 f->output = NULL;
02256 }
02257 free(f->name);
02258 free(f->dname);
02259
02260 if (mode == MODE_KMAIL)
02261 close_kmail_dir();
02262 else if (mode == MODE_RECURSE) {
02263 if (mode_thunder) {
02264 FILE *type_file = fopen(".size", "w");
02265 fprintf(type_file, "%i %i\n", f->item_count, f->stored_count);
02266 fclose(type_file);
02267 }
02268 close_recurse_dir();
02269 } else if (mode == MODE_SEPARATE)
02270 close_separate_dir();
02271 }
02272