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