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

Generated on 12 Jan 2020 for 'LibPst' by  doxygen 1.6.1