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