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