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