Main Page | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

readpst.c

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

Generated on Sun Sep 20 11:19:48 2009 for 'LibPst' by  doxygen 1.3.9.1