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