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 DEBUG_ENT("mk_separate_file");
00875 DEBUG_INFO(("opening next file to save email\n"));
00876 if (f->item_count > 999999999) {
00877 DIE(("mk_separate_file: The number of emails in this folder has become too high to handle\n"));
00878 }
00879 sprintf(f->name, SEP_MAIL_FILE_TEMPLATE, f->item_count, extension);
00880 if (f->output) fclose(f->output);
00881 f->output = NULL;
00882 check_filename(f->name);
00883 if (!(f->output = fopen(f->name, "w"))) {
00884 DIE(("mk_separate_file: Cannot open file to save email \"%s\"\n", f->name));
00885 }
00886 DEBUG_RET();
00887 return 0;
00888 }
00889
00890
00891 char *my_stristr(char *haystack, char *needle) {
00892
00893 char *x=haystack, *y=needle, *z = NULL;
00894 if (!haystack || !needle) {
00895 return NULL;
00896 }
00897 while (*y != '\0' && *x != '\0') {
00898 if (tolower(*y) == tolower(*x)) {
00899
00900 y++;
00901 if (!z) {
00902 z = x;
00903 }
00904 } else {
00905 y = needle;
00906 z = NULL;
00907 }
00908 x++;
00909 }
00910
00911 if (*y != '\0') return NULL;
00912 return z;
00913 }
00914
00915
00916 void check_filename(char *fname) {
00917 char *t = fname;
00918 DEBUG_ENT("check_filename");
00919 if (!t) {
00920 DEBUG_RET();
00921 return;
00922 }
00923 while ((t = strpbrk(t, "/\\:"))) {
00924
00925 *t = '_';
00926 }
00927 DEBUG_RET();
00928 }
00929
00930
00931 void write_separate_attachment(char f_name[], pst_item_attach* attach, int attach_num, pst_file* pst)
00932 {
00933 FILE *fp = NULL;
00934 int x = 0;
00935 char *temp = NULL;
00936
00937
00938
00939 char *attach_filename = (attach->filename2.str) ? attach->filename2.str
00940 : attach->filename1.str;
00941 DEBUG_ENT("write_separate_attachment");
00942
00943 if (!attach->data.data) {
00944
00945 pst_index_ll *ptr = pst_getID(pst, attach->i_id);
00946 if (!ptr) {
00947 DEBUG_WARN(("Couldn't find i_id %#"PRIx64". Cannot save attachment to file\n", attach->i_id));
00948 DEBUG_RET();
00949 return;
00950 }
00951 }
00952
00953 check_filename(f_name);
00954 if (!attach_filename) {
00955
00956 temp = pst_malloc(strlen(f_name)+15);
00957 sprintf(temp, "%s-attach%i", f_name, attach_num);
00958 } else {
00959
00960 temp = pst_malloc(strlen(f_name)+strlen(attach_filename)+15);
00961 do {
00962 if (fp) fclose(fp);
00963 if (x == 0)
00964 sprintf(temp, "%s-%s", f_name, attach_filename);
00965 else
00966 sprintf(temp, "%s-%s-%i", f_name, attach_filename, x);
00967 } while ((fp = fopen(temp, "r")) && ++x < 99999999);
00968 if (x > 99999999) {
00969 DIE(("error finding attachment name. exhausted possibilities to %s\n", temp));
00970 }
00971 }
00972 DEBUG_INFO(("Saving attachment to %s\n", temp));
00973 if (!(fp = fopen(temp, "w"))) {
00974 DEBUG_WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp));
00975 } else {
00976 (void)pst_attach_to_file(pst, attach, fp);
00977 fclose(fp);
00978 }
00979 if (temp) free(temp);
00980 DEBUG_RET();
00981 }
00982
00983
00984 void write_embedded_message(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pf, char** extra_mime_headers)
00985 {
00986 pst_index_ll *ptr;
00987 DEBUG_ENT("write_embedded_message");
00988 ptr = pst_getID(pf, attach->i_id);
00989
00990 pst_desc_tree d_ptr;
00991 d_ptr.d_id = 0;
00992 d_ptr.parent_d_id = 0;
00993 d_ptr.assoc_tree = NULL;
00994 d_ptr.desc = ptr;
00995 d_ptr.no_child = 0;
00996 d_ptr.prev = NULL;
00997 d_ptr.next = NULL;
00998 d_ptr.parent = NULL;
00999 d_ptr.child = NULL;
01000 d_ptr.child_tail = NULL;
01001
01002 pst_item *item = pst_parse_item(pf, &d_ptr, attach->id2_head);
01003
01004
01005
01006
01007
01008
01009 if (!item) {
01010 DEBUG_WARN(("write_embedded_message: pst_parse_item was unable to parse the embedded message in attachment ID %llu", attach->i_id));
01011 } else {
01012 fprintf(f_output, "\n--%s\n", boundary);
01013 fprintf(f_output, "Content-Type: %s\n\n", attach->mimetype.str);
01014 write_normal_email(f_output, "", item, MODE_NORMAL, 0, pf, 0, extra_mime_headers);
01015 pst_freeItem(item);
01016 }
01017
01018 DEBUG_RET();
01019 }
01020
01021
01022 void write_inline_attachment(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pst)
01023 {
01024 char *attach_filename;
01025 DEBUG_ENT("write_inline_attachment");
01026 DEBUG_INFO(("Attachment Size is %"PRIu64", id %#"PRIx64"\n", (uint64_t)attach->data.size, attach->i_id));
01027
01028 if (!attach->data.data) {
01029
01030 pst_index_ll *ptr = pst_getID(pst, attach->i_id);
01031 if (!ptr) {
01032 DEBUG_WARN(("Couldn't find ID pointer. Cannot save attachment to file\n"));
01033 DEBUG_RET();
01034 return;
01035 }
01036 }
01037
01038 fprintf(f_output, "\n--%s\n", boundary);
01039 if (!attach->mimetype.str) {
01040 fprintf(f_output, "Content-Type: %s\n", MIME_TYPE_DEFAULT);
01041 } else {
01042 fprintf(f_output, "Content-Type: %s\n", attach->mimetype.str);
01043 }
01044 fprintf(f_output, "Content-Transfer-Encoding: base64\n");
01045
01046
01047
01048 attach_filename = (attach->filename2.str) ? attach->filename2.str : attach->filename1.str;
01049 if (!attach_filename) {
01050 fprintf(f_output, "Content-Disposition: inline\n\n");
01051 } else {
01052 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", attach_filename);
01053 }
01054
01055 (void)pst_attach_to_file_base64(pst, attach, f_output);
01056 fprintf(f_output, "\n\n");
01057 DEBUG_RET();
01058 }
01059
01060
01061 void header_has_field(char *header, char *field, int *flag)
01062 {
01063 DEBUG_ENT("header_has_field");
01064 if (my_stristr(header, field) || (strncasecmp(header, field+1, strlen(field)-1) == 0)) {
01065 DEBUG_INFO(("header block has %s header\n", field+1));
01066 *flag = 1;
01067 }
01068 DEBUG_RET();
01069 }
01070
01071
01072 void header_get_subfield(char *field, const char *subfield, char *body_subfield, size_t size_subfield)
01073 {
01074 if (!field) return;
01075 DEBUG_ENT("header_get_subfield");
01076 char search[60];
01077 snprintf(search, sizeof(search), " %s=", subfield);
01078 field++;
01079 char *n = header_end_field(field);
01080 char *s = my_stristr(field, search);
01081 if (n && s && (s < n)) {
01082 char *e, *f, save;
01083 s += strlen(search);
01084 if (*s == '"') {
01085 s++;
01086 e = strchr(s, '"');
01087 }
01088 else {
01089 e = strchr(s, ';');
01090 f = strchr(s, '\n');
01091 if (e && f && (f < e)) e = f;
01092 }
01093 if (!e || (e > n)) e = n;
01094 save = *e;
01095 *e = '\0';
01096 snprintf(body_subfield, size_subfield, "%s", s);
01097 *e = save;
01098 DEBUG_INFO(("body %s %s from headers\n", subfield, body_subfield));
01099 }
01100 DEBUG_RET();
01101 }
01102
01103 char* header_get_field(char *header, char *field)
01104 {
01105 char *t = my_stristr(header, field);
01106 if (!t && (strncasecmp(header, field+1, strlen(field)-1) == 0)) t = header;
01107 return t;
01108 }
01109
01110
01111
01112
01113 char *header_end_field(char *field)
01114 {
01115 char *e = strchr(field+1, '\n');
01116 while (e && ((e[1] == ' ') || (e[1] == '\t'))) {
01117 e = strchr(e+1, '\n');
01118 }
01119 return e;
01120 }
01121
01122
01123 void header_strip_field(char *header, char *field)
01124 {
01125 char *t = header_get_field(header, field);
01126 if (t) {
01127 char *e = header_end_field(t);
01128 if (e) {
01129 if (t == header) e++;
01130 while (*e != '\0') {
01131 *t = *e;
01132 t++;
01133 e++;
01134 }
01135 *t = '\0';
01136 }
01137 else {
01138
01139 *t = '\0';
01140 }
01141 }
01142 }
01143
01144
01145 int test_base64(char *body)
01146 {
01147 int b64 = 0;
01148 uint8_t *b = (uint8_t *)body;
01149 DEBUG_ENT("test_base64");
01150 while (*b != 0) {
01151 if ((*b < 32) && (*b != 9) && (*b != 10)) {
01152 DEBUG_INFO(("found base64 byte %d\n", (int)*b));
01153 DEBUG_HEXDUMPC(body, strlen(body), 0x10);
01154 b64 = 1;
01155 break;
01156 }
01157 b++;
01158 }
01159 DEBUG_RET();
01160 return b64;
01161 }
01162
01163
01164 void find_html_charset(char *html, char *charset, size_t charsetlen)
01165 {
01166 const int index = 1;
01167 const int nmatch = index+1;
01168 regmatch_t match[nmatch];
01169 DEBUG_ENT("find_html_charset");
01170 int rc = regexec(&meta_charset_pattern, html, nmatch, match, 0);
01171 if (rc == 0) {
01172 int s = match[index].rm_so;
01173 int e = match[index].rm_eo;
01174 if (s != -1) {
01175 char save = html[e];
01176 html[e] = '\0';
01177 snprintf(charset, charsetlen, "%s", html+s);
01178 html[e] = save;
01179 DEBUG_INFO(("charset %s from html text\n", charset));
01180 }
01181 else {
01182 DEBUG_INFO(("matching %d %d %d %d\n", match[0].rm_so, match[0].rm_eo, match[1].rm_so, match[1].rm_eo));
01183 DEBUG_HEXDUMPC(html, strlen(html), 0x10);
01184 }
01185 }
01186 else {
01187 DEBUG_INFO(("regexec returns %d\n", rc));
01188 }
01189 DEBUG_RET();
01190 }
01191
01192
01193 void find_rfc822_headers(char** extra_mime_headers)
01194 {
01195 DEBUG_ENT("find_rfc822_headers");
01196 char *headers = *extra_mime_headers;
01197 if (headers) {
01198 char *temp, *t;
01199 while ((temp = strstr(headers, "\n\n"))) {
01200 temp[1] = '\0';
01201 t = header_get_field(headers, "\nContent-Type: ");
01202 if (t) {
01203 t++;
01204 DEBUG_INFO(("found content type header\n"));
01205 char *n = strchr(t, '\n');
01206 char *s = strstr(t, ": ");
01207 char *e = strchr(t, ';');
01208 if (!e || (e > n)) e = n;
01209 if (s && (s < e)) {
01210 s += 2;
01211 if (!strncasecmp(s, RFC822, e-s)) {
01212 headers = temp+2;
01213 DEBUG_INFO(("found 822 headers\n%s\n", headers));
01214 break;
01215 }
01216 }
01217 }
01218
01219 headers = temp+2;
01220 }
01221 *extra_mime_headers = headers;
01222 }
01223 DEBUG_RET();
01224 }
01225
01226
01227 void write_body_part(FILE* f_output, pst_string *body, char *mime, char *charset, char *boundary, pst_file* pst)
01228 {
01229 DEBUG_ENT("write_body_part");
01230 if (body->is_utf8 && (strcasecmp("utf-8", charset))) {
01231
01232
01233
01234 size_t rc;
01235 DEBUG_INFO(("Convert %s utf-8 to %s\n", mime, charset));
01236 pst_vbuf *newer = pst_vballoc(2);
01237 rc = pst_vb_utf8to8bit(newer, body->str, strlen(body->str), charset);
01238 if (rc == (size_t)-1) {
01239
01240 free(newer->b);
01241 DEBUG_INFO(("Failed to convert %s utf-8 to %s\n", mime, charset));
01242 charset = "utf-8";
01243 }
01244 else {
01245
01246 pst_vbgrow(newer, 1);
01247 newer->b[newer->dlen] = '\0';
01248 free(body->str);
01249 body->str = newer->b;
01250 }
01251 free(newer);
01252 }
01253 removeCR(body->str);
01254 int base64 = test_base64(body->str);
01255 fprintf(f_output, "\n--%s\n", boundary);
01256 fprintf(f_output, "Content-Type: %s; charset=\"%s\"\n", mime, charset);
01257 if (base64) fprintf(f_output, "Content-Transfer-Encoding: base64\n");
01258 fprintf(f_output, "\n");
01259 if (base64) {
01260 char *enc = pst_base64_encode(body->str, strlen(body->str));
01261 if (enc) {
01262 write_email_body(f_output, enc);
01263 fprintf(f_output, "\n");
01264 free(enc);
01265 }
01266 }
01267 else {
01268 write_email_body(f_output, body->str);
01269 }
01270 DEBUG_RET();
01271 }
01272
01273
01274 void write_schedule_part_data(FILE* f_output, pst_item* item, const char* sender, const char* method)
01275 {
01276 fprintf(f_output, "BEGIN:VCALENDAR\n");
01277 fprintf(f_output, "VERSION:2.0\n");
01278 fprintf(f_output, "PRODID:LibPST v%s\n", VERSION);
01279 if (method) fprintf(f_output, "METHOD:%s\n", method);
01280 fprintf(f_output, "BEGIN:VEVENT\n");
01281 if (sender) {
01282 if (item->email->outlook_sender_name.str) {
01283 fprintf(f_output, "ORGANIZER;CN=\"%s\":MAILTO:%s\n", item->email->outlook_sender_name.str, sender);
01284 } else {
01285 fprintf(f_output, "ORGANIZER;CN=\"\":MAILTO:%s\n", sender);
01286 }
01287 }
01288 write_appointment(f_output, item);
01289 fprintf(f_output, "END:VCALENDAR\n");
01290 }
01291
01292
01293 void write_schedule_part(FILE* f_output, pst_item* item, const char* sender, const char* boundary)
01294 {
01295 const char* method = "REQUEST";
01296 const char* charset = "utf-8";
01297 char fname[30];
01298 if (!item->appointment) return;
01299
01300
01301 fprintf(f_output, "\n--%s\n", boundary);
01302 fprintf(f_output, "Content-Type: %s; method=\"%s\"; charset=\"%s\"\n\n", "text/calendar", method, charset);
01303 write_schedule_part_data(f_output, item, sender, method);
01304 fprintf(f_output, "\n");
01305
01306
01307 snprintf(fname, sizeof(fname), "i%i.ics", rand());
01308 fprintf(f_output, "\n--%s\n", boundary);
01309 fprintf(f_output, "Content-Type: %s; charset=\"%s\"; name=\"%s\"\n", "text/calendar", "utf-8", fname);
01310 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", fname);
01311 write_schedule_part_data(f_output, item, sender, method);
01312 fprintf(f_output, "\n");
01313 }
01314
01315
01316 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)
01317 {
01318 char boundary[60];
01319 char altboundary[66];
01320 char *altboundaryp = NULL;
01321 char body_charset[30];
01322 char buffer_charset[30];
01323 char body_report[60];
01324 char sender[60];
01325 int sender_known = 0;
01326 char *temp = NULL;
01327 time_t em_time;
01328 char *c_time;
01329 char *headers = NULL;
01330 int has_from, has_subject, has_to, has_cc, has_date, has_msgid;
01331 has_from = has_subject = has_to = has_cc = has_date = has_msgid = 0;
01332 DEBUG_ENT("write_normal_email");
01333
01334 pst_convert_utf8_null(item, &item->email->header);
01335 headers = (item->email->header.str) ? item->email->header.str : *extra_mime_headers;
01336
01337
01338 strncpy(body_charset, pst_default_charset(item, sizeof(buffer_charset), buffer_charset), sizeof(body_charset));
01339 body_charset[sizeof(body_charset)-1] = '\0';
01340 strncpy(body_report, "delivery-status", sizeof(body_report));
01341 body_report[sizeof(body_report)-1] = '\0';
01342
01343
01344 pst_convert_utf8(item, &item->email->sender_address);
01345 if (item->email->sender_address.str && strchr(item->email->sender_address.str, '@')) {
01346 temp = item->email->sender_address.str;
01347 sender_known = 1;
01348 }
01349 else {
01350 temp = "MAILER-DAEMON";
01351 }
01352 strncpy(sender, temp, sizeof(sender));
01353 sender[sizeof(sender)-1] = '\0';
01354
01355
01356 if (item->email->sent_date) {
01357 em_time = pst_fileTimeToUnixTime(item->email->sent_date);
01358 c_time = ctime(&em_time);
01359 if (c_time)
01360 c_time[strlen(c_time)-1] = '\0';
01361 else
01362 c_time = "Fri Dec 28 12:06:21 2001";
01363 } else
01364 c_time= "Fri Dec 28 12:06:21 2001";
01365
01366
01367 snprintf(boundary, sizeof(boundary), "--boundary-LibPST-iamunique-%i_-_-", rand());
01368 snprintf(altboundary, sizeof(altboundary), "alt-%s", boundary);
01369
01370
01371 if (headers ) {
01372 char *t;
01373 removeCR(headers);
01374
01375 temp = strstr(headers, "\n\n");
01376 if (temp) {
01377
01378 temp[1] = '\0';
01379
01380
01381 *extra_mime_headers = temp+2;
01382 DEBUG_INFO(("Found extra mime headers\n%s\n", temp+2));
01383 }
01384
01385
01386 header_has_field(headers, "\nFrom: ", &has_from);
01387 header_has_field(headers, "\nTo: ", &has_to);
01388 header_has_field(headers, "\nSubject: ", &has_subject);
01389 header_has_field(headers, "\nDate: ", &has_date);
01390 header_has_field(headers, "\nCC: ", &has_cc);
01391 header_has_field(headers, "\nMessage-Id: ", &has_msgid);
01392
01393
01394 t = header_get_field(headers, "\nContent-Type: ");
01395 header_get_subfield(t, "charset", body_charset, sizeof(body_charset));
01396 header_get_subfield(t, "report-type", body_report, sizeof(body_report));
01397
01398
01399 if (!sender_known) {
01400 t = header_get_field(headers, "\nFrom: ");
01401 if (t) {
01402
01403 t++;
01404 char *n = strchr(t, '\n');
01405 char *s = strchr(t, '<');
01406 char *e = strchr(t, '>');
01407 if (s && e && n && (s < e) && (e < n)) {
01408 char save = *e;
01409 *e = '\0';
01410 snprintf(sender, sizeof(sender), "%s", s+1);
01411 *e = save;
01412 }
01413 }
01414 }
01415
01416
01417 header_strip_field(headers, "\nMicrosoft Mail Internet Headers");
01418 header_strip_field(headers, "\nMIME-Version: ");
01419 header_strip_field(headers, "\nContent-Type: ");
01420 header_strip_field(headers, "\nContent-Transfer-Encoding: ");
01421 header_strip_field(headers, "\nContent-class: ");
01422 header_strip_field(headers, "\nX-MimeOLE: ");
01423 header_strip_field(headers, "\nBcc:");
01424 header_strip_field(headers, "\nX-From_: ");
01425 }
01426
01427 DEBUG_INFO(("About to print Header\n"));
01428
01429 if (item && item->subject.str) {
01430 pst_convert_utf8(item, &item->subject);
01431 DEBUG_INFO(("item->subject = %s\n", item->subject.str));
01432 }
01433
01434 if (mode != MODE_SEPARATE) {
01435
01436
01437
01438
01439 fprintf(f_output, "From \"%s\" %s\n", sender, c_time);
01440 }
01441
01442
01443 if (headers) {
01444 int len = strlen(headers);
01445 if (len > 0) {
01446 fprintf(f_output, "%s", headers);
01447
01448 if (headers[len-1] != '\n') fprintf(f_output, "\n");
01449 }
01450 }
01451
01452
01453
01454 if (!has_from) {
01455 if (item->email->outlook_sender_name.str){
01456 fprintf(f_output, "From: \"%s\" <%s>\n", item->email->outlook_sender_name.str, sender);
01457 } else {
01458 fprintf(f_output, "From: <%s>\n", sender);
01459 }
01460 }
01461
01462 if (!has_subject) {
01463 if (item->subject.str) {
01464 fprintf(f_output, "Subject: %s\n", item->subject.str);
01465 } else {
01466 fprintf(f_output, "Subject: \n");
01467 }
01468 }
01469
01470 if (!has_to && item->email->sentto_address.str) {
01471 pst_convert_utf8(item, &item->email->sentto_address);
01472 fprintf(f_output, "To: %s\n", item->email->sentto_address.str);
01473 }
01474
01475 if (!has_cc && item->email->cc_address.str) {
01476 pst_convert_utf8(item, &item->email->cc_address);
01477 fprintf(f_output, "Cc: %s\n", item->email->cc_address.str);
01478 }
01479
01480 if (!has_date && item->email->sent_date) {
01481 char c_time[C_TIME_SIZE];
01482 struct tm stm;
01483 gmtime_r(&em_time, &stm);
01484 strftime(c_time, C_TIME_SIZE, "%a, %d %b %Y %H:%M:%S %z", &stm);
01485 fprintf(f_output, "Date: %s\n", c_time);
01486 }
01487
01488 if (!has_msgid && item->email->messageid.str) {
01489 pst_convert_utf8(item, &item->email->messageid);
01490 fprintf(f_output, "Message-Id: %s\n", item->email->messageid.str);
01491 }
01492
01493
01494
01495 pst_convert_utf8_null(item, &item->email->sender_address);
01496 if (item->email->sender_address.str && !strchr(item->email->sender_address.str, '@')
01497 && strcmp(item->email->sender_address.str, ".")
01498 && (strlen(item->email->sender_address.str) > 0)) {
01499 fprintf(f_output, "X-libpst-forensic-sender: %s\n", item->email->sender_address.str);
01500 }
01501
01502 if (item->email->bcc_address.str) {
01503 pst_convert_utf8(item, &item->email->bcc_address);
01504 fprintf(f_output, "X-libpst-forensic-bcc: %s\n", item->email->bcc_address.str);
01505 }
01506
01507
01508 fprintf(f_output, "MIME-Version: 1.0\n");
01509 if (item->type == PST_TYPE_REPORT) {
01510
01511 fprintf(f_output, "Content-Type: multipart/report; report-type=%s;\n\tboundary=\"%s\"\n", body_report, boundary);
01512 }
01513 else {
01514 fprintf(f_output, "Content-Type: multipart/mixed;\n\tboundary=\"%s\"\n", boundary);
01515 }
01516 fprintf(f_output, "\n");
01517
01518
01519 if ((item->type == PST_TYPE_REPORT) && (item->email->report_text.str)) {
01520 write_body_part(f_output, &item->email->report_text, "text/plain", body_charset, boundary, pst);
01521 fprintf(f_output, "\n");
01522 }
01523
01524 if (item->body.str && item->email->htmlbody.str) {
01525
01526 fprintf(f_output, "\n--%s\n", boundary);
01527 fprintf(f_output, "Content-Type: multipart/alternative;\n\tboundary=\"%s\"\n", altboundary);
01528 altboundaryp = altboundary;
01529 }
01530 else {
01531 altboundaryp = boundary;
01532 }
01533
01534 if (item->body.str) {
01535 write_body_part(f_output, &item->body, "text/plain", body_charset, altboundaryp, pst);
01536 }
01537
01538 if (item->email->htmlbody.str) {
01539 find_html_charset(item->email->htmlbody.str, body_charset, sizeof(body_charset));
01540 write_body_part(f_output, &item->email->htmlbody, "text/html", body_charset, altboundaryp, pst);
01541 }
01542
01543 if (item->body.str && item->email->htmlbody.str) {
01544
01545 fprintf(f_output, "\n--%s--\n", altboundary);
01546 }
01547
01548 if (item->email->rtf_compressed.data && save_rtf) {
01549 pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach));
01550 DEBUG_INFO(("Adding RTF body as attachment\n"));
01551 memset(attach, 0, sizeof(pst_item_attach));
01552 attach->next = item->attach;
01553 item->attach = attach;
01554 attach->data.data = pst_lzfu_decompress(item->email->rtf_compressed.data, item->email->rtf_compressed.size, &attach->data.size);
01555 attach->filename2.str = strdup(RTF_ATTACH_NAME);
01556 attach->filename2.is_utf8 = 1;
01557 attach->mimetype.str = strdup(RTF_ATTACH_TYPE);
01558 attach->mimetype.is_utf8 = 1;
01559 }
01560
01561 if (item->email->encrypted_body.data) {
01562 pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach));
01563 DEBUG_INFO(("Adding encrypted text body as attachment\n"));
01564 attach = (pst_item_attach*) pst_malloc(sizeof(pst_item_attach));
01565 memset(attach, 0, sizeof(pst_item_attach));
01566 attach->next = item->attach;
01567 item->attach = attach;
01568 attach->data.data = item->email->encrypted_body.data;
01569 attach->data.size = item->email->encrypted_body.size;
01570 item->email->encrypted_body.data = NULL;
01571 }
01572
01573 if (item->email->encrypted_htmlbody.data) {
01574 pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach));
01575 DEBUG_INFO(("Adding encrypted HTML body as attachment\n"));
01576 attach = (pst_item_attach*) pst_malloc(sizeof(pst_item_attach));
01577 memset(attach, 0, sizeof(pst_item_attach));
01578 attach->next = item->attach;
01579 item->attach = attach;
01580 attach->data.data = item->email->encrypted_htmlbody.data;
01581 attach->data.size = item->email->encrypted_htmlbody.size;
01582 item->email->encrypted_htmlbody.data = NULL;
01583 }
01584
01585 if (item->type == PST_TYPE_SCHEDULE) {
01586 write_schedule_part(f_output, item, sender, boundary);
01587 }
01588
01589
01590 {
01591 pst_item_attach* attach;
01592 int attach_num = 0;
01593 for (attach = item->attach; attach; attach = attach->next) {
01594 pst_convert_utf8_null(item, &attach->filename1);
01595 pst_convert_utf8_null(item, &attach->filename2);
01596 pst_convert_utf8_null(item, &attach->mimetype);
01597 DEBUG_INFO(("Attempting Attachment encoding\n"));
01598 if (attach->method == PST_ATTACH_EMBEDDED) {
01599 DEBUG_INFO(("have an embedded rfc822 message attachment\n"));
01600 if (attach->mimetype.str) {
01601 DEBUG_INFO(("which already has a mime-type of %s\n", attach->mimetype.str));
01602 free(attach->mimetype.str);
01603 }
01604 attach->mimetype.str = strdup(RFC822);
01605 attach->mimetype.is_utf8 = 1;
01606 find_rfc822_headers(extra_mime_headers);
01607 write_embedded_message(f_output, attach, boundary, pst, extra_mime_headers);
01608 }
01609 else if (attach->data.data || attach->i_id) {
01610 if (mode == MODE_SEPARATE && !mode_MH)
01611 write_separate_attachment(f_name, attach, ++attach_num, pst);
01612 else
01613 write_inline_attachment(f_output, attach, boundary, pst);
01614 }
01615 }
01616 }
01617
01618 fprintf(f_output, "\n--%s--\n\n", boundary);
01619 DEBUG_RET();
01620 }
01621
01622
01623 void write_vcard(FILE* f_output, pst_item* item, pst_item_contact* contact, char comment[])
01624 {
01625 char* result = NULL;
01626 size_t resultlen = 0;
01627 char time_buffer[30];
01628
01629
01630
01631
01632 DEBUG_ENT("write_vcard");
01633
01634
01635 pst_convert_utf8_null(item, &contact->fullname);
01636 pst_convert_utf8_null(item, &contact->surname);
01637 pst_convert_utf8_null(item, &contact->first_name);
01638 pst_convert_utf8_null(item, &contact->middle_name);
01639 pst_convert_utf8_null(item, &contact->display_name_prefix);
01640 pst_convert_utf8_null(item, &contact->suffix);
01641 pst_convert_utf8_null(item, &contact->nickname);
01642 pst_convert_utf8_null(item, &contact->address1);
01643 pst_convert_utf8_null(item, &contact->address2);
01644 pst_convert_utf8_null(item, &contact->address3);
01645 pst_convert_utf8_null(item, &contact->home_po_box);
01646 pst_convert_utf8_null(item, &contact->home_street);
01647 pst_convert_utf8_null(item, &contact->home_city);
01648 pst_convert_utf8_null(item, &contact->home_state);
01649 pst_convert_utf8_null(item, &contact->home_postal_code);
01650 pst_convert_utf8_null(item, &contact->home_country);
01651 pst_convert_utf8_null(item, &contact->home_address);
01652 pst_convert_utf8_null(item, &contact->business_po_box);
01653 pst_convert_utf8_null(item, &contact->business_street);
01654 pst_convert_utf8_null(item, &contact->business_city);
01655 pst_convert_utf8_null(item, &contact->business_state);
01656 pst_convert_utf8_null(item, &contact->business_postal_code);
01657 pst_convert_utf8_null(item, &contact->business_country);
01658 pst_convert_utf8_null(item, &contact->business_address);
01659 pst_convert_utf8_null(item, &contact->other_po_box);
01660 pst_convert_utf8_null(item, &contact->other_street);
01661 pst_convert_utf8_null(item, &contact->other_city);
01662 pst_convert_utf8_null(item, &contact->other_state);
01663 pst_convert_utf8_null(item, &contact->other_postal_code);
01664 pst_convert_utf8_null(item, &contact->other_country);
01665 pst_convert_utf8_null(item, &contact->other_address);
01666 pst_convert_utf8_null(item, &contact->business_fax);
01667 pst_convert_utf8_null(item, &contact->business_phone);
01668 pst_convert_utf8_null(item, &contact->business_phone2);
01669 pst_convert_utf8_null(item, &contact->car_phone);
01670 pst_convert_utf8_null(item, &contact->home_fax);
01671 pst_convert_utf8_null(item, &contact->home_phone);
01672 pst_convert_utf8_null(item, &contact->home_phone2);
01673 pst_convert_utf8_null(item, &contact->isdn_phone);
01674 pst_convert_utf8_null(item, &contact->mobile_phone);
01675 pst_convert_utf8_null(item, &contact->other_phone);
01676 pst_convert_utf8_null(item, &contact->pager_phone);
01677 pst_convert_utf8_null(item, &contact->primary_fax);
01678 pst_convert_utf8_null(item, &contact->primary_phone);
01679 pst_convert_utf8_null(item, &contact->radio_phone);
01680 pst_convert_utf8_null(item, &contact->telex);
01681 pst_convert_utf8_null(item, &contact->job_title);
01682 pst_convert_utf8_null(item, &contact->profession);
01683 pst_convert_utf8_null(item, &contact->assistant_name);
01684 pst_convert_utf8_null(item, &contact->assistant_phone);
01685 pst_convert_utf8_null(item, &contact->company_name);
01686 pst_convert_utf8_null(item, &item->body);
01687
01688
01689 fprintf(f_output, "BEGIN:VCARD\n");
01690 fprintf(f_output, "FN:%s\n", pst_rfc2426_escape(contact->fullname.str, &result, &resultlen));
01691
01692
01693 fprintf(f_output, "N:%s;", (!contact->surname.str) ? "" : pst_rfc2426_escape(contact->surname.str, &result, &resultlen));
01694 fprintf(f_output, "%s;", (!contact->first_name.str) ? "" : pst_rfc2426_escape(contact->first_name.str, &result, &resultlen));
01695 fprintf(f_output, "%s;", (!contact->middle_name.str) ? "" : pst_rfc2426_escape(contact->middle_name.str, &result, &resultlen));
01696 fprintf(f_output, "%s;", (!contact->display_name_prefix.str) ? "" : pst_rfc2426_escape(contact->display_name_prefix.str, &result, &resultlen));
01697 fprintf(f_output, "%s\n", (!contact->suffix.str) ? "" : pst_rfc2426_escape(contact->suffix.str, &result, &resultlen));
01698
01699 if (contact->nickname.str)
01700 fprintf(f_output, "NICKNAME:%s\n", pst_rfc2426_escape(contact->nickname.str, &result, &resultlen));
01701 if (contact->address1.str)
01702 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address1.str, &result, &resultlen));
01703 if (contact->address2.str)
01704 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address2.str, &result, &resultlen));
01705 if (contact->address3.str)
01706 fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address3.str, &result, &resultlen));
01707 if (contact->birthday)
01708 fprintf(f_output, "BDAY:%s\n", pst_rfc2425_datetime_format(contact->birthday, sizeof(time_buffer), time_buffer));
01709
01710 if (contact->home_address.str) {
01711
01712 fprintf(f_output, "ADR;TYPE=home:%s;", (!contact->home_po_box.str) ? "" : pst_rfc2426_escape(contact->home_po_box.str, &result, &resultlen));
01713 fprintf(f_output, "%s;", "");
01714 fprintf(f_output, "%s;", (!contact->home_street.str) ? "" : pst_rfc2426_escape(contact->home_street.str, &result, &resultlen));
01715 fprintf(f_output, "%s;", (!contact->home_city.str) ? "" : pst_rfc2426_escape(contact->home_city.str, &result, &resultlen));
01716 fprintf(f_output, "%s;", (!contact->home_state.str) ? "" : pst_rfc2426_escape(contact->home_state.str, &result, &resultlen));
01717 fprintf(f_output, "%s;", (!contact->home_postal_code.str) ? "" : pst_rfc2426_escape(contact->home_postal_code.str, &result, &resultlen));
01718 fprintf(f_output, "%s\n", (!contact->home_country.str) ? "" : pst_rfc2426_escape(contact->home_country.str, &result, &resultlen));
01719 fprintf(f_output, "LABEL;TYPE=home:%s\n", pst_rfc2426_escape(contact->home_address.str, &result, &resultlen));
01720 }
01721
01722 if (contact->business_address.str) {
01723
01724 fprintf(f_output, "ADR;TYPE=work:%s;", (!contact->business_po_box.str) ? "" : pst_rfc2426_escape(contact->business_po_box.str, &result, &resultlen));
01725 fprintf(f_output, "%s;", "");
01726 fprintf(f_output, "%s;", (!contact->business_street.str) ? "" : pst_rfc2426_escape(contact->business_street.str, &result, &resultlen));
01727 fprintf(f_output, "%s;", (!contact->business_city.str) ? "" : pst_rfc2426_escape(contact->business_city.str, &result, &resultlen));
01728 fprintf(f_output, "%s;", (!contact->business_state.str) ? "" : pst_rfc2426_escape(contact->business_state.str, &result, &resultlen));
01729 fprintf(f_output, "%s;", (!contact->business_postal_code.str) ? "" : pst_rfc2426_escape(contact->business_postal_code.str, &result, &resultlen));
01730 fprintf(f_output, "%s\n", (!contact->business_country.str) ? "" : pst_rfc2426_escape(contact->business_country.str, &result, &resultlen));
01731 fprintf(f_output, "LABEL;TYPE=work:%s\n", pst_rfc2426_escape(contact->business_address.str, &result, &resultlen));
01732 }
01733
01734 if (contact->other_address.str) {
01735
01736 fprintf(f_output, "ADR;TYPE=postal:%s;",(!contact->other_po_box.str) ? "" : pst_rfc2426_escape(contact->other_po_box.str, &result, &resultlen));
01737 fprintf(f_output, "%s;", "");
01738 fprintf(f_output, "%s;", (!contact->other_street.str) ? "" : pst_rfc2426_escape(contact->other_street.str, &result, &resultlen));
01739 fprintf(f_output, "%s;", (!contact->other_city.str) ? "" : pst_rfc2426_escape(contact->other_city.str, &result, &resultlen));
01740 fprintf(f_output, "%s;", (!contact->other_state.str) ? "" : pst_rfc2426_escape(contact->other_state.str, &result, &resultlen));
01741 fprintf(f_output, "%s;", (!contact->other_postal_code.str) ? "" : pst_rfc2426_escape(contact->other_postal_code.str, &result, &resultlen));
01742 fprintf(f_output, "%s\n", (!contact->other_country.str) ? "" : pst_rfc2426_escape(contact->other_country.str, &result, &resultlen));
01743 fprintf(f_output, "LABEL;TYPE=postal:%s\n", pst_rfc2426_escape(contact->other_address.str, &result, &resultlen));
01744 }
01745
01746 if (contact->business_fax.str) fprintf(f_output, "TEL;TYPE=work,fax:%s\n", pst_rfc2426_escape(contact->business_fax.str, &result, &resultlen));
01747 if (contact->business_phone.str) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", pst_rfc2426_escape(contact->business_phone.str, &result, &resultlen));
01748 if (contact->business_phone2.str) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", pst_rfc2426_escape(contact->business_phone2.str, &result, &resultlen));
01749 if (contact->car_phone.str) fprintf(f_output, "TEL;TYPE=car,voice:%s\n", pst_rfc2426_escape(contact->car_phone.str, &result, &resultlen));
01750 if (contact->home_fax.str) fprintf(f_output, "TEL;TYPE=home,fax:%s\n", pst_rfc2426_escape(contact->home_fax.str, &result, &resultlen));
01751 if (contact->home_phone.str) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", pst_rfc2426_escape(contact->home_phone.str, &result, &resultlen));
01752 if (contact->home_phone2.str) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", pst_rfc2426_escape(contact->home_phone2.str, &result, &resultlen));
01753 if (contact->isdn_phone.str) fprintf(f_output, "TEL;TYPE=isdn:%s\n", pst_rfc2426_escape(contact->isdn_phone.str, &result, &resultlen));
01754 if (contact->mobile_phone.str) fprintf(f_output, "TEL;TYPE=cell,voice:%s\n", pst_rfc2426_escape(contact->mobile_phone.str, &result, &resultlen));
01755 if (contact->other_phone.str) fprintf(f_output, "TEL;TYPE=msg:%s\n", pst_rfc2426_escape(contact->other_phone.str, &result, &resultlen));
01756 if (contact->pager_phone.str) fprintf(f_output, "TEL;TYPE=pager:%s\n", pst_rfc2426_escape(contact->pager_phone.str, &result, &resultlen));
01757 if (contact->primary_fax.str) fprintf(f_output, "TEL;TYPE=fax,pref:%s\n", pst_rfc2426_escape(contact->primary_fax.str, &result, &resultlen));
01758 if (contact->primary_phone.str) fprintf(f_output, "TEL;TYPE=phone,pref:%s\n", pst_rfc2426_escape(contact->primary_phone.str, &result, &resultlen));
01759 if (contact->radio_phone.str) fprintf(f_output, "TEL;TYPE=pcs:%s\n", pst_rfc2426_escape(contact->radio_phone.str, &result, &resultlen));
01760 if (contact->telex.str) fprintf(f_output, "TEL;TYPE=bbs:%s\n", pst_rfc2426_escape(contact->telex.str, &result, &resultlen));
01761 if (contact->job_title.str) fprintf(f_output, "TITLE:%s\n", pst_rfc2426_escape(contact->job_title.str, &result, &resultlen));
01762 if (contact->profession.str) fprintf(f_output, "ROLE:%s\n", pst_rfc2426_escape(contact->profession.str, &result, &resultlen));
01763 if (contact->assistant_name.str || contact->assistant_phone.str) {
01764 fprintf(f_output, "AGENT:BEGIN:VCARD\n");
01765 if (contact->assistant_name.str) fprintf(f_output, "FN:%s\n", pst_rfc2426_escape(contact->assistant_name.str, &result, &resultlen));
01766 if (contact->assistant_phone.str) fprintf(f_output, "TEL:%s\n", pst_rfc2426_escape(contact->assistant_phone.str, &result, &resultlen));
01767 }
01768 if (contact->company_name.str) fprintf(f_output, "ORG:%s\n", pst_rfc2426_escape(contact->company_name.str, &result, &resultlen));
01769 if (comment) fprintf(f_output, "NOTE:%s\n", pst_rfc2426_escape(comment, &result, &resultlen));
01770 if (item->body.str) fprintf(f_output, "NOTE:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen));
01771
01772 write_extra_categories(f_output, item);
01773
01774 fprintf(f_output, "VERSION: 3.0\n");
01775 fprintf(f_output, "END:VCARD\n\n");
01776 if (result) free(result);
01777 DEBUG_RET();
01778 }
01779
01780
01788 int write_extra_categories(FILE* f_output, pst_item* item)
01789 {
01790 char* result = NULL;
01791 size_t resultlen = 0;
01792 pst_item_extra_field *ef = item->extra_fields;
01793 const char *fmt = "CATEGORIES:%s";
01794 int category_started = 0;
01795 while (ef) {
01796 if (strcmp(ef->field_name, "Keywords") == 0) {
01797 fprintf(f_output, fmt, pst_rfc2426_escape(ef->value, &result, &resultlen));
01798 fmt = ", %s";
01799 category_started = 1;
01800 }
01801 ef = ef->next;
01802 }
01803 if (category_started) fprintf(f_output, "\n");
01804 if (result) free(result);
01805 return category_started;
01806 }
01807
01808
01809 void write_journal(FILE* f_output, pst_item* item)
01810 {
01811 char* result = NULL;
01812 size_t resultlen = 0;
01813 char time_buffer[30];
01814 pst_item_journal* journal = item->journal;
01815
01816
01817 pst_convert_utf8_null(item, &item->subject);
01818 pst_convert_utf8_null(item, &item->body);
01819
01820 fprintf(f_output, "BEGIN:VJOURNAL\n");
01821 fprintf(f_output, "DTSTAMP:%s\n", pst_rfc2445_datetime_format_now(sizeof(time_buffer), time_buffer));
01822 if (item->create_date)
01823 fprintf(f_output, "CREATED:%s\n", pst_rfc2445_datetime_format(item->create_date, sizeof(time_buffer), time_buffer));
01824 if (item->modify_date)
01825 fprintf(f_output, "LAST-MOD:%s\n", pst_rfc2445_datetime_format(item->modify_date, sizeof(time_buffer), time_buffer));
01826 if (item->subject.str)
01827 fprintf(f_output, "SUMMARY:%s\n", pst_rfc2426_escape(item->subject.str, &result, &resultlen));
01828 if (item->body.str)
01829 fprintf(f_output, "DESCRIPTION:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen));
01830 if (journal && journal->start)
01831 fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(journal->start, sizeof(time_buffer), time_buffer));
01832 fprintf(f_output, "END:VJOURNAL\n");
01833 if (result) free(result);
01834 }
01835
01836
01837 void write_appointment(FILE* f_output, pst_item* item)
01838 {
01839 char* result = NULL;
01840 size_t resultlen = 0;
01841 char time_buffer[30];
01842 pst_item_appointment* appointment = item->appointment;
01843
01844
01845 pst_convert_utf8_null(item, &item->subject);
01846 pst_convert_utf8_null(item, &item->body);
01847 pst_convert_utf8_null(item, &appointment->location);
01848
01849 fprintf(f_output, "DTSTAMP:%s\n", pst_rfc2445_datetime_format_now(sizeof(time_buffer), time_buffer));
01850 if (item->create_date)
01851 fprintf(f_output, "CREATED:%s\n", pst_rfc2445_datetime_format(item->create_date, sizeof(time_buffer), time_buffer));
01852 if (item->modify_date)
01853 fprintf(f_output, "LAST-MOD:%s\n", pst_rfc2445_datetime_format(item->modify_date, sizeof(time_buffer), time_buffer));
01854 if (item->subject.str)
01855 fprintf(f_output, "SUMMARY:%s\n", pst_rfc2426_escape(item->subject.str, &result, &resultlen));
01856 if (item->body.str)
01857 fprintf(f_output, "DESCRIPTION:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen));
01858 if (appointment && appointment->start)
01859 fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(appointment->start, sizeof(time_buffer), time_buffer));
01860 if (appointment && appointment->end)
01861 fprintf(f_output, "DTEND;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(appointment->end, sizeof(time_buffer), time_buffer));
01862 if (appointment && appointment->location.str)
01863 fprintf(f_output, "LOCATION:%s\n", pst_rfc2426_escape(appointment->location.str, &result, &resultlen));
01864 if (appointment) {
01865 switch (appointment->showas) {
01866 case PST_FREEBUSY_TENTATIVE:
01867 fprintf(f_output, "STATUS:TENTATIVE\n");
01868 break;
01869 case PST_FREEBUSY_FREE:
01870
01871 fprintf(f_output, "TRANSP:TRANSPARENT\n");
01872 case PST_FREEBUSY_BUSY:
01873 case PST_FREEBUSY_OUT_OF_OFFICE:
01874 fprintf(f_output, "STATUS:CONFIRMED\n");
01875 break;
01876 }
01877 if (appointment->is_recurring) {
01878 const char* rules[] = {"DAILY", "WEEKLY", "MONTHLY", "YEARLY"};
01879 const char* days[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"};
01880 pst_recurrence *rdata = pst_convert_recurrence(appointment);
01881 fprintf(f_output, "RRULE:FREQ=%s", rules[rdata->type]);
01882 if (rdata->count) fprintf(f_output, ";COUNT=%u", rdata->count);
01883 if ((rdata->interval != 1) &&
01884 (rdata->interval)) fprintf(f_output, ";INTERVAL=%u", rdata->interval);
01885 if (rdata->dayofmonth) fprintf(f_output, ";BYMONTHDAY=%d", rdata->dayofmonth);
01886 if (rdata->monthofyear) fprintf(f_output, ";BYMONTH=%d", rdata->monthofyear);
01887 if (rdata->position) fprintf(f_output, ";BYSETPOS=%d", rdata->position);
01888 if (rdata->bydaymask) {
01889 char byday[40];
01890 int empty = 1;
01891 int i=0;
01892 memset(byday, 0, sizeof(byday));
01893 for (i=0; i<6; i++) {
01894 int bit = 1 << i;
01895 if (bit & rdata->bydaymask) {
01896 char temp[40];
01897 snprintf(temp, sizeof(temp), "%s%s%s", byday, (empty) ? ";BYDAY=" : ";", days[i]);
01898 strcpy(byday, temp);
01899 empty = 0;
01900 }
01901 }
01902 fprintf(f_output, "%s", byday);
01903 }
01904 fprintf(f_output, "\n");
01905 pst_free_recurrence(rdata);
01906 }
01907 switch (appointment->label) {
01908 case PST_APP_LABEL_NONE:
01909 if (!write_extra_categories(f_output, item)) fprintf(f_output, "CATEGORIES:NONE\n");
01910 break;
01911 case PST_APP_LABEL_IMPORTANT:
01912 fprintf(f_output, "CATEGORIES:IMPORTANT\n");
01913 break;
01914 case PST_APP_LABEL_BUSINESS:
01915 fprintf(f_output, "CATEGORIES:BUSINESS\n");
01916 break;
01917 case PST_APP_LABEL_PERSONAL:
01918 fprintf(f_output, "CATEGORIES:PERSONAL\n");
01919 break;
01920 case PST_APP_LABEL_VACATION:
01921 fprintf(f_output, "CATEGORIES:VACATION\n");
01922 break;
01923 case PST_APP_LABEL_MUST_ATTEND:
01924 fprintf(f_output, "CATEGORIES:MUST-ATTEND\n");
01925 break;
01926 case PST_APP_LABEL_TRAVEL_REQ:
01927 fprintf(f_output, "CATEGORIES:TRAVEL-REQUIRED\n");
01928 break;
01929 case PST_APP_LABEL_NEEDS_PREP:
01930 fprintf(f_output, "CATEGORIES:NEEDS-PREPARATION\n");
01931 break;
01932 case PST_APP_LABEL_BIRTHDAY:
01933 fprintf(f_output, "CATEGORIES:BIRTHDAY\n");
01934 break;
01935 case PST_APP_LABEL_ANNIVERSARY:
01936 fprintf(f_output, "CATEGORIES:ANNIVERSARY\n");
01937 break;
01938 case PST_APP_LABEL_PHONE_CALL:
01939 fprintf(f_output, "CATEGORIES:PHONE-CALL\n");
01940 break;
01941 }
01942 }
01943 fprintf(f_output, "END:VEVENT\n");
01944 if (result) free(result);
01945 }
01946
01947
01948 void create_enter_dir(struct file_ll* f, pst_item *item)
01949 {
01950 pst_convert_utf8(item, &item->file_as);
01951 f->type = item->type;
01952 f->stored_count = (item->folder) ? item->folder->item_count : 0;
01953
01954 DEBUG_ENT("create_enter_dir");
01955 if (mode == MODE_KMAIL)
01956 f->name = mk_kmail_dir(item->file_as.str);
01957 else if (mode == MODE_RECURSE) {
01958 f->name = mk_recurse_dir(item->file_as.str, f->type);
01959 if (mode_thunder) {
01960 FILE *type_file = fopen(".type", "w");
01961 fprintf(type_file, "%d\n", item->type);
01962 fclose(type_file);
01963 }
01964 } else if (mode == MODE_SEPARATE) {
01965
01966 mk_separate_dir(item->file_as.str);
01967 f->name = (char*) pst_malloc(file_name_len);
01968 memset(f->name, 0, file_name_len);
01969 } else {
01970 f->name = (char*) pst_malloc(strlen(item->file_as.str)+strlen(OUTPUT_TEMPLATE)+1);
01971 sprintf(f->name, OUTPUT_TEMPLATE, item->file_as.str);
01972 }
01973
01974 f->dname = (char*) pst_malloc(strlen(item->file_as.str)+1);
01975 strcpy(f->dname, item->file_as.str);
01976
01977 if (overwrite != 1) {
01978 int x = 0;
01979 char *temp = (char*) pst_malloc (strlen(f->name)+10);
01980
01981 sprintf(temp, "%s", f->name);
01982 check_filename(temp);
01983 while ((f->output = fopen(temp, "r"))) {
01984 DEBUG_INFO(("need to increase filename because one already exists with that name\n"));
01985 DEBUG_INFO(("- increasing it to %s%d\n", f->name, x));
01986 x++;
01987 sprintf(temp, "%s%08d", f->name, x);
01988 DEBUG_INFO(("- trying \"%s\"\n", f->name));
01989 if (x == 99999999) {
01990 DIE(("create_enter_dir: Why can I not create a folder %s? I have tried %i extensions...\n", f->name, x));
01991 }
01992 fclose(f->output);
01993 }
01994 if (x > 0) {
01995 free (f->name);
01996 f->name = temp;
01997 } else {
01998 free(temp);
01999 }
02000 }
02001
02002 DEBUG_INFO(("f->name = %s\nitem->folder_name = %s\n", f->name, item->file_as.str));
02003 if (mode != MODE_SEPARATE) {
02004 check_filename(f->name);
02005 if (!(f->output = fopen(f->name, "w"))) {
02006 DIE(("create_enter_dir: Could not open file \"%s\" for write\n", f->name));
02007 }
02008 }
02009 DEBUG_RET();
02010 }
02011
02012
02013 void close_enter_dir(struct file_ll *f)
02014 {
02015 DEBUG_INFO(("processed item count for folder %s is %i, skipped %i, total %i \n",
02016 f->dname, f->item_count, f->skip_count, f->stored_count));
02017 if (output_mode != OUTPUT_QUIET) {
02018 pst_debug_lock();
02019 printf("\t\"%s\" - %i items done, %i items skipped.\n", f->dname, f->item_count, f->skip_count);
02020 fflush(stdout);
02021 pst_debug_unlock();
02022 }
02023 if (f->output) {
02024 struct stat st;
02025 fclose(f->output);
02026 stat(f->name, &st);
02027 if (!st.st_size) {
02028 DEBUG_WARN(("removing empty output file %s\n", f->name));
02029 remove(f->name);
02030 }
02031 }
02032 free(f->name);
02033 free(f->dname);
02034
02035 if (mode == MODE_KMAIL)
02036 close_kmail_dir();
02037 else if (mode == MODE_RECURSE) {
02038 if (mode_thunder) {
02039 FILE *type_file = fopen(".size", "w");
02040 fprintf(type_file, "%i %i\n", f->item_count, f->stored_count);
02041 fclose(type_file);
02042 }
02043 close_recurse_dir();
02044 } else if (mode == MODE_SEPARATE)
02045 close_separate_dir();
02046 }
02047