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