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

Generated on 24 Dec 2011 for 'LibPst' by  doxygen 1.6.1