annotate src/readpst.c @ 41:183ae993b9ad

security fix for potential buffer overrun in lz decompress
author carl
date Tue, 02 Oct 2007 15:49:44 -0700
parents 2ad7ef0a3c4f
children f6db1f060a95
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
1 /***
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
2 * readpst.c
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
3 * Part of the LibPST project
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
4 * Written by David Smith
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
5 * dave.s@earthcorp.com
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
6 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
7 #include <stdio.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
8 #include <stdlib.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
9 #include <time.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
10 #include <string.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
11 #include <ctype.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
12 #include <limits.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
13 #include <errno.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
14
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
15 #ifndef _WIN32
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
16 # include <unistd.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
17 # include <sys/stat.h> //mkdir
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
18
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
19 // for reading of directory and clearing in function mk_seperate_dir
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
20 # include <sys/types.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
21 # include <dirent.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
22 #else
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
23 # include <direct.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
24 # define chdir _chdir
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
25 # define int32_t __int32
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
26 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
27
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
28 #ifndef __GNUC__
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
29 # include "XGetopt.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
30 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
31
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
32 #include "libstrfunc.h" // for base64_encoding
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
33
21
e5418051878c switch to automake/autoconf
carl
parents: 16
diff changeset
34 #include "version.h"
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
35 #include "define.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
36 #include "libpst.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
37 #include "common.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
38 #include "timeconv.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
39 #include "lzfu.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
40 #define OUTPUT_TEMPLATE "%s"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
41 #define OUTPUT_KMAIL_DIR_TEMPLATE ".%s.directory"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
42 #define KMAIL_INDEX ".%s.index"
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
43 #define SEP_MAIL_FILE_TEMPLATE "%i" /* "%09i" */
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
44
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
45 // max size of the c_time char*. It will store the date of the email
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
46 #define C_TIME_SIZE 500
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
47 #define PERM_DIRS 0777
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
48
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
49 // macro used for creating directories
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
50 #ifndef WIN32
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
51 #define D_MKDIR(x) mkdir(x, PERM_DIRS)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
52 #else
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
53 #define D_MKDIR(x) mkdir(x)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
54 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
55 struct file_ll {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
56 char *name;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
57 char *dname;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
58 FILE * output;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
59 int32_t stored_count;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
60 int32_t email_count;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
61 int32_t skip_count;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
62 int32_t type;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
63 };
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
64
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
65 void process(pst_item *outeritem, pst_desc_ll *d_ptr);
34
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
66 void write_email_body(FILE *f, char *body);
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
67 char* removeCR (char *c);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
68 int32_t usage();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
69 int32_t version();
34
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
70 char* mk_kmail_dir(char*);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
71 int32_t close_kmail_dir();
34
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
72 char* mk_recurse_dir(char*);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
73 int32_t close_recurse_dir();
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
74 char* mk_seperate_dir(char *dir);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
75 int32_t close_seperate_dir();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
76 int32_t mk_seperate_file(struct file_ll *f);
34
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
77 char* my_stristr(char *haystack, char *needle);
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
78 void check_filename(char *fname);
34
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
79 char* rfc2426_escape(char *str);
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
80 int32_t chr_count(char *str, char x);
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
81 char* rfc2425_datetime_format(FILETIME *ft);
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
82 char* rfc2445_datetime_format(FILETIME *ft);
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
83 char* skip_header_prologue(char *headers);
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
84 void write_separate_attachment(char f_name[], pst_item_attach* current_attach, int attach_num, pst_file* pst);
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
85 void write_inline_attachment(FILE* f_output, pst_item_attach* current_attach, char boundary[], pst_file* pst);
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
86 void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, int mode_MH, pst_file* pst, int save_rtf);
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
87 void write_vcard(FILE* f_output, pst_item_contact* contact, char comment[]);
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
88 void write_appointment(FILE* f_output, pst_item_appointment* appointment,
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
89 pst_item_email* email, FILETIME* create_date, FILETIME* modify_date);
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
90 void create_enter_dir(struct file_ll* f, pst_item *item);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
91 void close_enter_dir(struct file_ll *f);
34
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
92
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
93 char* prog_name;
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
94 char* output_dir = ".";
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
95 char* kmail_chdir = NULL;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
96 // Normal mode just creates mbox format files in the current directory. Each file is named
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
97 // the same as the folder's name that it represents
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
98 #define MODE_NORMAL 0
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
99 // KMail mode creates a directory structure suitable for being used directly
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
100 // by the KMail application
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
101 #define MODE_KMAIL 1
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
102 // recurse mode creates a directory structure like the PST file. Each directory
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
103 // contains only one file which stores the emails in mbox format.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
104 #define MODE_RECURSE 2
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
105 // seperate mode is similar directory structure to RECURSE. The emails are stored in
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
106 // seperate files, numbering from 1 upward. Attachments belonging to the emails are
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
107 // saved as email_no-filename (e.g. 1-samplefile.doc or 000001-Attachment2.zip)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
108 #define MODE_SEPERATE 3
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
109
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
110
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
111 // Output Normal just prints the standard information about what is going on
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
112 #define OUTPUT_NORMAL 0
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
113 // Output Quiet is provided so that only errors are printed
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
114 #define OUTPUT_QUIET 1
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
115
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
116 // default mime-type for attachments that have a null mime-type
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
117 #define MIME_TYPE_DEFAULT "application/octet-stream"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
118
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
119 // output mode for contacts
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
120 #define CMODE_VCARD 0
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
121 #define CMODE_LIST 1
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
122
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
123 // output settings for RTF bodies
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
124 // filename for the attachment
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
125 #define RTF_ATTACH_NAME "rtf-body.rtf"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
126 // mime type for the attachment
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
127 #define RTF_ATTACH_TYPE "application/rtf"
34
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
128
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
129 // global settings
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
130 int mode = MODE_NORMAL;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
131 int mode_MH = 0;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
132 int output_mode = OUTPUT_NORMAL;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
133 int contact_mode = CMODE_VCARD;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
134 int overwrite = 0;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
135 int save_rtf_body = 1;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
136 pst_file pstfile;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
137
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
138
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
139
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
140 void process(pst_item *outeritem, pst_desc_ll *d_ptr)
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
141 {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
142 struct file_ll ff;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
143 pst_item *item = NULL;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
144
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
145 DEBUG_ENT("process");
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
146 memset(&ff, 0, sizeof(ff));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
147 create_enter_dir(&ff, outeritem);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
148
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
149 while (d_ptr) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
150 DEBUG_MAIN(("main: New item record\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
151 if (!d_ptr->desc) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
152 DEBUG_WARN(("main: ERROR ?? item's desc record is NULL\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
153 ff.skip_count++;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
154 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
155 else {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
156 DEBUG_MAIN(("main: Desc Email ID %#x [d_ptr->id = %#x]\n", d_ptr->desc->id, d_ptr->id));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
157
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
158 item = _pst_parse_item(&pstfile, d_ptr);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
159 DEBUG_MAIN(("main: About to process item\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
160 if (item && item->email && item->email->subject && item->email->subject->subj) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
161 DEBUG_EMAIL(("item->email->subject = %p\n", item->email->subject));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
162 DEBUG_EMAIL(("item->email->subject->subj = %p\n", item->email->subject->subj));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
163 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
164 if (item) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
165 if (item->message_store) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
166 // there should only be one message_store, and we have already done it
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
167 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
168 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
169
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
170 if (item->folder && d_ptr->child && strcasecmp(item->file_as, "Deleted Items")) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
171 //if this is a non-empty folder other than deleted items, we want to recurse into it
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
172 if (output_mode != OUTPUT_QUIET) printf("Processing Folder \"%s\"\n", item->file_as);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
173 process(item, d_ptr->child);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
174
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
175 } else if (item->contact) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
176 // deal with a contact
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
177 // write them to the file, one per line in this format
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
178 // Desc Name <email@address>\n
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
179 if (mode == MODE_SEPERATE) mk_seperate_file(&ff);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
180 ff.email_count++;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
181 DEBUG_MAIN(("main: Processing Contact\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
182 if (ff.type != PST_TYPE_CONTACT) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
183 DEBUG_MAIN(("main: I have a contact, but the folder isn't a contacts folder. "
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
184 "Will process anyway\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
185 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
186 if (item->type != PST_TYPE_CONTACT) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
187 DEBUG_MAIN(("main: I have an item that has contact info, but doesn't say that"
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
188 " it is a contact. Type is \"%s\"\n", item->ascii_type));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
189 DEBUG_MAIN(("main: Processing anyway\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
190 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
191 if (!item->contact) { // this is an incorrect situation. Inform user
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
192 DEBUG_MAIN(("main: ERROR. This contact has not been fully parsed. one of the pre-requisties is NULL\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
193 } else {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
194 if (contact_mode == CMODE_VCARD)
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
195 write_vcard(ff.output, item->contact, item->comment);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
196 else
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
197 fprintf(ff.output, "%s <%s>\n", item->contact->fullname, item->contact->address1);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
198 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
199
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
200 } else if (item->email && (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_REPORT)) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
201 if (mode == MODE_SEPERATE) mk_seperate_file(&ff);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
202 ff.email_count++;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
203 DEBUG_MAIN(("main: seen an email\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
204 write_normal_email(ff.output, ff.name, item, mode, mode_MH, &pstfile, save_rtf_body);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
205
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
206 } else if (item->type == PST_TYPE_JOURNAL) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
207 // deal with journal items
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
208 if (mode == MODE_SEPERATE) mk_seperate_file(&ff);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
209 ff.email_count++;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
210
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
211 DEBUG_MAIN(("main: Processing Journal Entry\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
212 if (ff.type != PST_TYPE_JOURNAL) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
213 DEBUG_MAIN(("main: I have a journal entry, but folder isn't specified as a journal type. Processing...\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
214 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
215
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
216 /* if (item->type != PST_TYPE_JOURNAL) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
217 DEBUG_MAIN(("main: I have an item with journal info, but it's type is \"%s\" \n. Processing...\n",
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
218 item->ascii_type));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
219 }*/
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
220 fprintf(ff.output, "BEGIN:VJOURNAL\n");
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
221 if (item->email->subject)
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
222 fprintf(ff.output, "SUMMARY:%s\n", rfc2426_escape(item->email->subject->subj));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
223 if (item->email->body)
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
224 fprintf(ff.output, "DESCRIPTION:%s\n", rfc2426_escape(item->email->body));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
225 if (item->journal->start)
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
226 fprintf(ff.output, "DTSTART;VALUE=DATE-TIME:%s\n", rfc2445_datetime_format(item->journal->start));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
227 fprintf(ff.output, "END:VJOURNAL\n\n");
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
228
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
229 } else if (item->type == PST_TYPE_APPOINTMENT) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
230 // deal with Calendar appointments
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
231 if (mode == MODE_SEPERATE) mk_seperate_file(&ff);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
232 ff.email_count++;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
233 DEBUG_MAIN(("main: Processing Appointment Entry\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
234 if (ff.type != PST_TYPE_APPOINTMENT) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
235 DEBUG_MAIN(("main: I have an appointment, but folder isn't specified as an appointment type. Processing...\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
236 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
237 write_appointment(ff.output, item->appointment, item->email, item->create_date, item->modify_date);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
238
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
239 } else {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
240 ff.skip_count++;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
241 DEBUG_MAIN(("main: Unknown item type. %i. Ascii1=\"%s\"\n",
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
242 item->type, item->ascii_type));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
243 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
244 _pst_freeItem(item);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
245 } else {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
246 ff.skip_count++;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
247 DEBUG_MAIN(("main: A NULL item was seen\n"));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
248 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
249 d_ptr = d_ptr->next;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
250 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
251 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
252 close_enter_dir(&ff);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
253 DEBUG_RET();
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
254 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
255
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
256
34
07177825c91b fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents: 33
diff changeset
257
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
258 int main(int argc, char** argv) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
259 pst_item *item = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
260 pst_desc_ll *d_ptr;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
261 char * fname = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
262 char *d_log=NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
263 int c,x;
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
264 char *temp = NULL; //temporary char pointer
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
265 prog_name = argv[0];
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
266
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
267 // command-line option handling
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
268 while ((c = getopt(argc, argv, "bd:hko:qrMSVwc:"))!= -1) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
269 switch (c) {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
270 case 'b':
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
271 save_rtf_body = 0;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
272 break;
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
273 case 'c':
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
274 if (optarg && optarg[0]=='v')
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
275 contact_mode=CMODE_VCARD;
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
276 else if (optarg && optarg[0]=='l')
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
277 contact_mode=CMODE_LIST;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
278 else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
279 usage();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
280 exit(0);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
281 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
282 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
283 case 'd':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
284 d_log = optarg;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
285 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
286 case 'h':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
287 usage();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
288 exit(0);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
289 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
290 case 'V':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
291 version();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
292 exit(0);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
293 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
294 case 'k':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
295 mode = MODE_KMAIL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
296 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
297 case 'M':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
298 mode = MODE_SEPERATE;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
299 mode_MH = 1;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
300 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
301 case 'o':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
302 output_dir = optarg;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
303 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
304 case 'q':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
305 output_mode = OUTPUT_QUIET;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
306 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
307 case 'r':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
308 mode = MODE_RECURSE;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
309 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
310 case 'S':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
311 mode = MODE_SEPERATE;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
312 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
313 case 'w':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
314 overwrite = 1;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
315 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
316 default:
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
317 usage();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
318 exit(1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
319 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
320 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
321 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
322
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
323 if (argc > optind) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
324 fname = argv[optind];
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
325 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
326 usage();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
327 exit(2);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
328 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
329
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
330 #ifdef DEBUG_ALL
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
331 // force a log file
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
332 if (!d_log) d_log = "readpst.log";
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
333 #endif // defined DEBUG_ALL
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
334 DEBUG_INIT(d_log);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
335 DEBUG_REGISTER_CLOSE();
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
336 DEBUG_ENT("main");
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
337
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
338 if (output_mode != OUTPUT_QUIET) printf("Opening PST file and indexes...\n");
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
339
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
340 RET_DERROR(pst_open(&pstfile, fname, "r"), 1, ("Error opening File\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
341 RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
342
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
343 pst_load_extended_attributes(&pstfile);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
344
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
345 if (chdir(output_dir)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
346 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
347 pst_close(&pstfile);
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
348 DEBUG_RET();
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
349 DIE(("main: Cannot change to output dir %s: %s\n", output_dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
350 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
351
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
352 if (output_mode != OUTPUT_QUIET) printf("About to start processing first record...\n");
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
353
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
354 d_ptr = pstfile.d_head; // first record is main record
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
355 item = (pst_item*)_pst_parse_item(&pstfile, d_ptr);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
356 if (!item || !item->message_store) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
357 DEBUG_RET();
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
358 DIE(("main: Could not get root record\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
359 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
360
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
361 // default the file_as to the same as the main filename if it doesn't exist
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
362 if (!item->file_as) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
363 if (!(temp = strrchr(fname, '/')))
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
364 if (!(temp = strrchr(fname, '\\')))
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
365 temp = fname;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
366 else
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
367 temp++; // get past the "\\"
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
368 else
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
369 temp++; // get past the "/"
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
370 item->file_as = (char*)xmalloc(strlen(temp)+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
371 strcpy(item->file_as, temp);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
372 DEBUG_MAIN(("file_as was blank, so am using %s\n", item->file_as));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
373 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
374 DEBUG_MAIN(("main: Root Folder Name: %s\n", item->file_as));
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
375
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
376 d_ptr = pst_getTopOfFolders(&pstfile, item);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
377 if (!d_ptr) {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
378 DEBUG_RET();
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
379 DIE(("Top of folders record not found. Cannot continue\n"));
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
380 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
381
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
382 process(item, d_ptr->child); // do the children of TOPF
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
383 _pst_freeItem(item);
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
384 pst_close(&pstfile);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
385
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
386 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
387 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
388 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
389
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
390
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
391 void write_email_body(FILE *f, char *body) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
392 char *n = body;
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
393 // DEBUG_MAIN(("write_email_body(): \"%s\"\n", body));
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
394 DEBUG_ENT("write_email_body");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
395 while (n) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
396 if (strncmp(body, "From ", 5) == 0)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
397 fprintf(f, ">");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
398 if ((n = strchr(body, '\n'))) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
399 n++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
400 fwrite(body, n-body, 1, f); //write just a line
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
401
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
402 body = n;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
403 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
404 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
405 fwrite(body, strlen(body), 1, f);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
406 DEBUG_RET();
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
407 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
408
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
409
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
410 char *removeCR (char *c) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
411 // converts /r/n to /n
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
412 char *a, *b;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
413 DEBUG_ENT("removeCR");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
414 a = b = c;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
415 while (*a != '\0') {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
416 *b = *a;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
417 if (*a != '\r')
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
418 b++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
419 a++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
420 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
421 *b = '\0';
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
422 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
423 return c;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
424 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
425
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
426
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
427 int usage() {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
428 DEBUG_ENT("usage");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
429 version();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
430 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
431 printf("OPTIONS:\n");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
432 printf("\t-b\t- Don't save RTF-Body attachments\n");
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
433 printf("\t-c[v|l]\t- Set the Contact output mode. -cv = VCard, -cl = EMail list\n");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
434 printf("\t-d <filename> \t- Debug to file. This is a binary log. Use readlog to print it\n");
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
435 printf("\t-h\t- Help. This screen\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
436 printf("\t-k\t- KMail. Output in kmail format\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
437 printf("\t-M\t- MH. Write emails in the MH format\n");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
438 printf("\t-o <dirname>\t- Output Dir. Directory to write files to. CWD is changed *after* opening pst file\n");
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
439 printf("\t-q\t- Quiet. Only print error messages\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
440 printf("\t-r\t- Recursive. Output in a recursive format\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
441 printf("\t-S\t- Seperate. Write emails in the seperate format\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
442 printf("\t-V\t- Version. Display program version\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
443 printf("\t-w\t- Overwrite any output mbox files\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
444 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
445 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
446 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
447
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
448
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
449 int version() {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
450 DEBUG_ENT("version");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
451 printf("ReadPST v%s\n", VERSION);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
452 #if BYTE_ORDER == BIG_ENDIAN
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
453 printf("Big Endian implementation being used.\n");
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
454 #elif BYTE_ORDER == LITTLE_ENDIAN
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
455 printf("Little Endian implementation being used.\n");
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
456 #else
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
457 # error "Byte order not supported by this library"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
458 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
459 #ifdef __GNUC__
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
460 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
461 #endif
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
462 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
463 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
464 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
465
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
466
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
467 char *mk_kmail_dir(char *fname) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
468 //change to that directory
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
469 //make a directory based on OUTPUT_KMAIL_DIR_TEMPLATE
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
470 //allocate space for OUTPUT_TEMPLATE and form a char* with fname
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
471 //return that value
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
472 char *dir, *out_name, *index;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
473 int x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
474 DEBUG_ENT("mk_kmail_dir");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
475 if (kmail_chdir && chdir(kmail_chdir)) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
476 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
477 DIE(("mk_kmail_dir: Cannot change to directory %s: %s\n", kmail_chdir, strerror(x)));
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
478 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
479 dir = malloc(strlen(fname)+strlen(OUTPUT_KMAIL_DIR_TEMPLATE)+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
480 sprintf(dir, OUTPUT_KMAIL_DIR_TEMPLATE, fname);
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
481 check_filename(dir);
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
482 if (D_MKDIR(dir)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
483 //error occured
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
484 if (errno != EEXIST) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
485 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
486 DIE(("mk_kmail_dir: Cannot create directory %s: %s\n", dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
487 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
488 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
489 kmail_chdir = realloc(kmail_chdir, strlen(dir)+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
490 strcpy(kmail_chdir, dir);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
491 free (dir);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
492
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
493 //we should remove any existing indexes created by KMail, cause they might be different now
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
494 index = malloc(strlen(fname)+strlen(KMAIL_INDEX)+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
495 sprintf(index, KMAIL_INDEX, fname);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
496 unlink(index);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
497 free(index);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
498
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
499 out_name = malloc(strlen(fname)+strlen(OUTPUT_TEMPLATE)+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
500 sprintf(out_name, OUTPUT_TEMPLATE, fname);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
501 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
502 return out_name;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
503 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
504
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
505
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
506 int close_kmail_dir() {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
507 // change ..
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
508 int x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
509 DEBUG_ENT("close_kmail_dir");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
510 if (kmail_chdir) { //only free kmail_chdir if not NULL. do not change directory
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
511 free(kmail_chdir);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
512 kmail_chdir = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
513 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
514 if (chdir("..")) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
515 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
516 DIE(("close_kmail_dir: Cannot move up dir (..): %s\n", strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
517 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
518 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
519 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
520 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
521 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
522
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
523
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
524 // this will create a directory by that name, then make an mbox file inside
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
525 // that dir. any subsequent dirs will be created by name, and they will
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
526 // contain mbox files
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
527 char *mk_recurse_dir(char *dir) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
528 int x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
529 char *out_name;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
530 DEBUG_ENT("mk_recurse_dir");
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
531 check_filename(dir);
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
532 if (D_MKDIR (dir)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
533 if (errno != EEXIST) { // not an error because it exists
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
534 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
535 DIE(("mk_recurse_dir: Cannot create directory %s: %s\n", dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
536 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
537 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
538 if (chdir (dir)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
539 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
540 DIE(("mk_recurse_dir: Cannot change to directory %s: %s\n", dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
541 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
542 out_name = malloc(strlen("mbox")+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
543 strcpy(out_name, "mbox");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
544 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
545 return out_name;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
546 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
547
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
548
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
549 int close_recurse_dir() {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
550 int x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
551 DEBUG_ENT("close_recurse_dir");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
552 if (chdir("..")) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
553 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
554 DIE(("close_recurse_dir: Cannot go up dir (..): %s\n", strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
555 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
556 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
557 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
558 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
559
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
560
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
561 char *mk_seperate_dir(char *dir) {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
562 DEBUG_ENT("mk_seperate_dir");
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
563
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
564 size_t dirsize = strlen(dir) + 10;
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
565 char dir_name[dirsize];
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
566 int x = 0, y = 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
567
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
568 do {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
569 if (y == 0)
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
570 snprintf(dir_name, dirsize, "%s", dir);
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
571 else
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
572 snprintf(dir_name, dirsize, "%s" SEP_MAIL_FILE_TEMPLATE, dir, y); // enough for 9 digits allocated above
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
573
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
574 check_filename(dir_name);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
575 DEBUG_MAIN(("about to try creating %s\n", dir_name));
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
576 if (D_MKDIR(dir_name)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
577 if (errno != EEXIST) { // if there is an error, and it doesn't already exist
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
578 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
579 DIE(("mk_seperate_dir: Cannot create directory %s: %s\n", dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
580 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
581 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
582 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
583 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
584 y++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
585 } while (overwrite == 0);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
586
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
587 if (chdir(dir_name)) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
588 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
589 DIE(("mk_recurse_dir: Cannot change to directory %s: %s\n", dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
590 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
591
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
592 if (overwrite) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
593 // we should probably delete all files from this directory
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
594 #if !defined(WIN32) && !defined(__CYGWIN__)
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
595 DIR * sdir = NULL;
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
596 struct dirent *dirent = NULL;
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
597 struct stat filestat;
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
598 if (!(sdir = opendir("./"))) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
599 WARN(("mk_seperate_dir: Cannot open dir \"%s\" for deletion of old contents\n", "./"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
600 } else {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
601 while ((dirent = readdir(sdir))) {
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
602 if (lstat(dirent->d_name, &filestat) != -1)
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
603 if (S_ISREG(filestat.st_mode)) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
604 if (unlink(dirent->d_name)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
605 y = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
606 DIE(("mk_seperate_dir: unlink returned error on file %s: %s\n", dirent->d_name, strerror(y)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
607 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
608 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
609 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
610 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
611 #endif
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
612 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
613
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
614 // we don't return a filename here cause it isn't necessary.
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
615 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
616 return NULL;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
617 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
618
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
619
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
620 int close_seperate_dir() {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
621 int x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
622 DEBUG_ENT("close_seperate_dir");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
623 if (chdir("..")) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
624 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
625 DIE(("close_seperate_dir: Cannot go up dir (..): %s\n", strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
626 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
627 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
628 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
629 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
630
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
631
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
632 int mk_seperate_file(struct file_ll *f) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
633 const int name_offset = 1;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
634 DEBUG_ENT("mk_seperate_file");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
635 DEBUG_MAIN(("opening next file to save email\n"));
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
636 if (f->email_count > 999999999) { // bigger than nine 9's
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
637 DIE(("mk_seperate_file: The number of emails in this folder has become too high to handle"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
638 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
639 sprintf(f->name, SEP_MAIL_FILE_TEMPLATE, f->email_count + name_offset);
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
640 if (f->output) fclose(f->output);
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
641 f->output = NULL;
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
642 check_filename(f->name);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
643 if (!(f->output = fopen(f->name, "w"))) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
644 DIE(("mk_seperate_file: Cannot open file to save email \"%s\"\n", f->name));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
645 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
646 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
647 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
648 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
649
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
650
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
651 char *my_stristr(char *haystack, char *needle) {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
652 // my_stristr varies from strstr in that its searches are case-insensitive
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
653 char *x=haystack, *y=needle, *z = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
654 DEBUG_ENT("my_stristr");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
655 if (!haystack || !needle)
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
656 return NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
657 while (*y != '\0' && *x != '\0') {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
658 if (tolower(*y) == tolower(*x)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
659 // move y on one
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
660 y++;
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
661 if (!z) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
662 z = x; // store first position in haystack where a match is made
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
663 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
664 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
665 y = needle; // reset y to the beginning of the needle
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
666 z = NULL; // reset the haystack storage point
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
667 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
668 x++; // advance the search in the haystack
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
669 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
670 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
671 return z;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
672 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
673
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
674
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
675 void check_filename(char *fname) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
676 char *t = fname;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
677 DEBUG_ENT("check_filename");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
678 if (!t) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
679 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
680 return fname;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
681 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
682 while ((t = strpbrk(t, "/\\:"))) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
683 // while there are characters in the second string that we don't want
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
684 *t = '_'; //replace them with an underscore
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
685 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
686 DEBUG_RET();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
687 return fname;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
688 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
689
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
690
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
691 char *rfc2426_escape(char *str) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
692 static char* buf = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
693 char *ret, *a, *b;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
694 int x = 0, y, z;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
695 DEBUG_ENT("rfc2426_escape");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
696 if (!str)
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
697 ret = str;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
698 else {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
699
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
700 // calculate space required to escape all the following characters
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
701 y = chr_count(str, ',')
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
702 + chr_count(str, '\\')
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
703 + chr_count(str, ';')
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
704 + chr_count(str, '\n');
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
705 z = chr_count(str, '\r');
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
706 if (y == 0 && z == 0)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
707 // there isn't any extra space required
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
708 ret = str;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
709 else {
38
f5c024aa1dc5 more valgrind fixes
carl
parents: 37
diff changeset
710 x = strlen(str) + y - z + 1; // don't forget room for the NUL
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
711 buf = (char*) realloc(buf, x);
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
712 a = str;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
713 b = buf;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
714 while (*a != '\0') {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
715 switch (*a) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
716 case ',' :
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
717 case '\\':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
718 case ';' :
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
719 *(b++) = '\\';
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
720 *b = *a;
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
721 break;
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
722 case '\n': // newlines are encoded as "\n"
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
723 *(b++) = '\\';
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
724 *b = 'n';
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
725 break;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
726 case '\r': // skip cr
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
727 b--;
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
728 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
729 default:
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
730 *b=*a;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
731 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
732 b++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
733 a++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
734 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
735 *b = '\0'; // NUL-terminate the string (buf)
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
736 ret = buf;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
737 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
738 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
739 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
740 return ret;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
741 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
742
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
743
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
744 int chr_count(char *str, char x) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
745 int r = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
746 while (*str != '\0') {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
747 if (*str == x)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
748 r++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
749 str++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
750 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
751 return r;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
752 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
753
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
754
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
755 char *rfc2425_datetime_format(FILETIME *ft) {
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
756 static char* buffer = NULL;
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
757 struct tm *stm = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
758 DEBUG_ENT("rfc2425_datetime_format");
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
759 if (!buffer) buffer = malloc(30); // should be enough for the date as defined below
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
760 stm = fileTimeToStructTM(ft);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
761 if (strftime(buffer, 30, "%Y-%m-%dT%H:%M:%SZ", stm)==0) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
762 DEBUG_INFO(("Problem occured formatting date\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
763 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
764 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
765 return buffer;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
766 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
767
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
768
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
769 char *rfc2445_datetime_format(FILETIME *ft) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
770 static char* buffer = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
771 struct tm *stm = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
772 DEBUG_ENT("rfc2445_datetime_format");
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
773 if (!buffer) buffer = malloc(30); // should be enough for the date as defined below
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
774 stm = fileTimeToStructTM(ft);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
775 if (strftime(buffer, 30, "%Y%m%dT%H%M%SZ", stm)==0) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
776 DEBUG_INFO(("Problem occured formatting date\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
777 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
778 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
779 return buffer;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
780 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
781
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
782
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
783 // The sole purpose of this function is to bypass the pseudo-header prologue
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
784 // that Microsoft Outlook inserts at the beginning of the internet email
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
785 // headers for emails stored in their "Personal Folders" files.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
786 char *skip_header_prologue(char *headers) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
787 const char *bad = "Microsoft Mail Internet Headers";
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
788 if ( strncmp(headers, bad, strlen(bad)) == 0 ) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
789 // Found the offensive header prologue
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
790 char *pc = strchr(headers, '\n');
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
791 return pc + 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
792 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
793 return headers;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
794 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
795
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
796
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
797 void write_separate_attachment(char f_name[], pst_item_attach* current_attach, int attach_num, pst_file* pst)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
798 {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
799 DEBUG_ENT("write_separate_attachment");
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
800 FILE *fp = NULL;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
801 int x = 0;
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
802 char *temp = NULL;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
803
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
804 // If there is a long filename (filename2) use that, otherwise
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
805 // use the 8.3 filename (filename1)
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
806 char *attach_filename = (current_attach->filename2) ? current_attach->filename2
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
807 : current_attach->filename1;
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
808
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
809 check_filename(f_name);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
810 if (!attach_filename) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
811 // generate our own (dummy) filename for the attachement
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
812 temp = xmalloc(strlen(f_name)+15);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
813 sprintf(temp, "%s-attach%i", f_name, attach_num);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
814 } else {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
815 // have an attachment name, make sure it's unique
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
816 temp = xmalloc(strlen(f_name)+strlen(attach_filename)+15);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
817 do {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
818 if (fp) fclose(fp);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
819 if (x == 0)
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
820 sprintf(temp, "%s-%s", f_name, attach_filename);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
821 else
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
822 sprintf(temp, "%s-%s-%i", f_name, attach_filename, x);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
823 } while ((fp = fopen(temp, "r")) && ++x < 99999999);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
824 if (x > 99999999) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
825 DIE(("error finding attachment name. exhausted possibilities to %s\n", temp));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
826 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
827 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
828 DEBUG_EMAIL(("Saving attachment to %s\n", temp));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
829 if (!(fp = fopen(temp, "w"))) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
830 WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
831 } else {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
832 if (current_attach->data)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
833 fwrite(current_attach->data, 1, current_attach->size, fp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
834 else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
835 pst_attach_to_file(pst, current_attach, fp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
836 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
837 fclose(fp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
838 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
839 if (temp) free(temp);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
840 DEBUG_RET();
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
841 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
842
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
843
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
844 void write_inline_attachment(FILE* f_output, pst_item_attach* current_attach, char boundary[], pst_file* pst)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
845 {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
846 DEBUG_ENT("write_inline_attachment");
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
847 char *enc; // base64 encoded attachment
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
848 DEBUG_EMAIL(("Attachment Size is %i\n", current_attach->size));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
849 DEBUG_EMAIL(("Attachment Pointer is %p\n", current_attach->data));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
850 if (current_attach->data) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
851 enc = base64_encode (current_attach->data, current_attach->size);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
852 if (!enc) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
853 DEBUG_EMAIL(("ERROR base64_encode returned NULL. Must have failed\n"));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
854 return;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
855 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
856 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
857 if (boundary) {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
858 char *attach_filename;
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
859 fprintf(f_output, "\n--%s\n", boundary);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
860 if (!current_attach->mimetype) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
861 fprintf(f_output, "Content-type: %s\n", MIME_TYPE_DEFAULT);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
862 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
863 fprintf(f_output, "Content-type: %s\n", current_attach->mimetype);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
864 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
865 fprintf(f_output, "Content-transfer-encoding: base64\n");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
866 // If there is a long filename (filename2) use that, otherwise
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
867 // use the 8.3 filename (filename1)
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
868 if (current_attach->filename2) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
869 attach_filename = current_attach->filename2;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
870 } else {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
871 attach_filename = current_attach->filename1;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
872 }
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
873 if (!attach_filename) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
874 fprintf(f_output, "Content-Disposition: inline\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
875 } else {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
876 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", attach_filename);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
877 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
878 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
879 if (current_attach->data) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
880 fwrite(enc, 1, strlen(enc), f_output);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
881 DEBUG_EMAIL(("Attachment Size after encoding is %i\n", strlen(enc)));
36
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
882 free(enc); // caught by valgrind
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
883 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
884 pst_attach_to_file_base64(pst, current_attach, f_output);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
885 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
886 fprintf(f_output, "\n\n");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
887 DEBUG_RET();
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
888 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
889
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
890
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
891 void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, int mode_MH, pst_file* pst, int save_rtf)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
892 {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
893 DEBUG_ENT("write_normal_email");
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
894 char *boundary = NULL; // the boundary marker between multipart sections
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
895 int boundary_created = 0; // we have not (yet) created a new boundary
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
896 char *temp = NULL;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
897 int attach_num, base64_body = 0;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
898 time_t em_time;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
899 char *c_time;
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
900 pst_item_attach* current_attach;
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
901
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
902 // convert the sent date if it exists, or set it to a fixed date
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
903 if (item->email->sent_date) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
904 em_time = fileTimeToUnixTime(item->email->sent_date, 0);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
905 c_time = ctime(&em_time);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
906 if (c_time)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
907 c_time[strlen(c_time)-1] = '\0'; //remove end \n
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
908 else
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
909 c_time = "Fri Dec 28 12:06:21 2001";
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
910 } else
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
911 c_time= "Fri Dec 28 12:06:21 2001";
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
912
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
913 // we will always look at the header to discover some stuff
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
914 if (item->email->header ) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
915 char *b1, *b2;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
916 // see if there is a boundary variable there
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
917 // this search MUST be made case insensitive (DONE).
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
918 // Also, we should check to find out if we are looking
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
919 // at the boundary associated with content-type, and that
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
920 // the content type really is multipart
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
921
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
922 removeCR(item->email->header);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
923
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
924 if ((b2 = my_stristr(item->email->header, "boundary="))) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
925 int len;
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
926 b2 += strlen("boundary="); // move boundary to first char of marker
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
927
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
928 if (*b2 == '"') {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
929 b2++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
930 b1 = strchr(b2, '"'); // find terminating quote
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
931 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
932 b1 = b2;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
933 while (isgraph(*b1)) // find first char that isn't part of boundary
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
934 b1++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
935 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
936 len = b1 - b2;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
937 boundary = malloc(len+1); //malloc that length
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
938 strncpy(boundary, b2, len); // copy boundary to another variable
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
939 boundary[len] = '\0';
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
940 b1 = b2 = boundary;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
941 while (*b2 != '\0') { // remove any CRs and Tabs
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
942 if (*b2 != '\n' && *b2 != '\r' && *b2 != '\t') {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
943 *b1 = *b2;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
944 b1++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
945 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
946 b2++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
947 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
948 *b1 = '\0';
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
949
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
950 DEBUG_EMAIL(("Found boundary of - %s\n", boundary));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
951 } else {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
952 DEBUG_EMAIL(("boundary not found in header\n"));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
953 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
954
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
955 // also possible to set 7bit encoding detection here.
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
956 if ((b2 = my_stristr(item->email->header, "Content-Transfer-Encoding:"))) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
957 if ((b2 = strchr(b2, ':'))) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
958 b2++; // skip to the : at the end of the string
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
959
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
960 while (*b2 == ' ' || *b2 == '\t')
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
961 b2++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
962 if (pst_strincmp(b2, "base64", 6)==0) {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
963 DEBUG_EMAIL(("body is base64 encoded\n"));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
964 base64_body = 1;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
965 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
966 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
967 DEBUG_WARN(("found a ':' during the my_stristr, but not after that..\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
968 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
969 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
970 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
971
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
972 if (!boundary && (item->attach || (item->email->body && item->email->htmlbody)
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
973 || item->email->rtf_compressed || item->email->encrypted_body
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
974 || item->email->encrypted_htmlbody)) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
975 // we need to create a boundary here.
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
976 DEBUG_EMAIL(("must create own boundary. oh dear.\n"));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
977 boundary = malloc(50 * sizeof(char)); // allow 50 chars for boundary
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
978 boundary[0] = '\0';
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
979 sprintf(boundary, "--boundary-LibPST-iamunique-%i_-_-", rand());
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
980 DEBUG_EMAIL(("created boundary is %s\n", boundary));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
981 boundary_created = 1;
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
982 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
983
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
984 DEBUG_EMAIL(("About to print Header\n"));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
985
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
986 if (item && item->email && item->email->subject && item->email->subject->subj) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
987 DEBUG_EMAIL(("item->email->subject->subj = %s\n", item->email->subject->subj));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
988 }
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
989
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
990 if (item->email->header) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
991 int len;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
992 char *soh = NULL; // real start of headers.
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
993
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
994 // some of the headers we get from the file are not properly defined.
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
995 // they can contain some email stuff too. We will cut off the header
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
996 // when we see a \n\n or \r\n\r\n
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
997 removeCR(item->email->header);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
998 temp = strstr(item->email->header, "\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
999
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1000 if (temp) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1001 DEBUG_EMAIL(("Found body text in header\n"));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1002 temp[1] = '\0'; // stop after first \n
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1003 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1004
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1005 // Now, write out the header...
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1006 soh = skip_header_prologue(item->email->header);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1007 if (mode != MODE_SEPERATE) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1008 // don't put rubbish in if we are doing seperate
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1009 if (strncmp(soh, "X-From_: ", 9) == 0 ) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1010 fputs("From ", f_output);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1011 soh += 9;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1012 } else
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1013 fprintf(f_output, "From \"%s\" %s\n", item->email->outlook_sender_name, c_time);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1014 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1015 fprintf(f_output, "%s", soh);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1016 len = strlen(soh);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1017 if (!len || (soh[len-1] != '\n')) fprintf(f_output, "\n");
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1018
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1019 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1020 //make up our own header!
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1021 if (mode != MODE_SEPERATE) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1022 // don't want this first line for this mode
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1023 if (item->email->outlook_sender_name) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1024 temp = item->email->outlook_sender_name;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1025 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1026 temp = "(readpst_null)";
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1027 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1028 fprintf(f_output, "From \"%s\" %s\n", temp, c_time);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1029 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1030
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1031 temp = item->email->outlook_sender;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1032 if (!temp) temp = "";
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1033 fprintf(f_output, "From: \"%s\" <%s>\n", item->email->outlook_sender_name, temp);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1034
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1035 if (item->email->subject) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1036 fprintf(f_output, "Subject: %s\n", item->email->subject->subj);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1037 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1038 fprintf(f_output, "Subject: \n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1039 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1040
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1041 fprintf(f_output, "To: %s\n", item->email->sentto_address);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1042 if (item->email->cc_address) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1043 fprintf(f_output, "Cc: %s\n", item->email->cc_address);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1044 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1045
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1046 if (item->email->sent_date) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1047 c_time = (char*) xmalloc(C_TIME_SIZE);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1048 strftime(c_time, C_TIME_SIZE, "%a, %d %b %Y %H:%M:%S %z", gmtime(&em_time));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1049 fprintf(f_output, "Date: %s\n", c_time);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1050 free(c_time);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1051 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1052 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1053
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1054 fprintf(f_output, "MIME-Version: 1.0\n");
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1055 if (boundary && boundary_created) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1056 // if we created the boundary, then it has NOT already been printed
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1057 // in the headers above.
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1058 if (item->attach) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1059 // write the boundary stuff if we have attachments
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1060 fprintf(f_output, "Content-type: multipart/mixed;\n\tboundary=\"%s\"\n", boundary);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1061 } else if (boundary) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1062 // else if we have multipart/alternative then tell it so
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1063 fprintf(f_output, "Content-type: multipart/alternative;\n\tboundary=\"%s\"\n", boundary);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1064 } else if (item->email->htmlbody) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1065 fprintf(f_output, "Content-type: text/html\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1066 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1067 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1068 fprintf(f_output, "\n"); // start the body
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1069 DEBUG_EMAIL(("About to print Body\n"));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1070
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1071 if (item->email->body) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1072 if (boundary) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1073 fprintf(f_output, "\n--%s\n", boundary);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1074 fprintf(f_output, "Content-type: text/plain\n");
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1075 if (base64_body)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1076 fprintf(f_output, "Content-Transfer-Encoding: base64\n");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1077 fprintf(f_output, "\n");
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1078 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1079 removeCR(item->email->body);
36
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1080 if (base64_body) {
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1081 char *enc = base64_encode(item->email->body, strlen(item->email->body));
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1082 if (enc) {
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1083 write_email_body(f_output, enc);
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1084 free(enc);
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1085 }
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1086 }
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1087 else {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1088 write_email_body(f_output, item->email->body);
36
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1089 }
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1090 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1091
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1092 if (item->email->htmlbody) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1093 if (boundary) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1094 fprintf(f_output, "\n--%s\n", boundary);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1095 fprintf(f_output, "Content-type: text/html\n");
36
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1096 if (base64_body) fprintf(f_output, "Content-Transfer-Encoding: base64\n");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1097 fprintf(f_output, "\n");
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1098 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1099 removeCR(item->email->htmlbody);
36
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1100 if (base64_body) {
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1101 char *enc = base64_encode(item->email->htmlbody, strlen(item->email->htmlbody));
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1102 if (enc) {
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1103 write_email_body(f_output, enc);
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1104 free(enc);
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1105 }
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1106 }
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1107 else {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1108 write_email_body(f_output, item->email->htmlbody);
36
6fe121a971c9 valgrind fixes
carl
parents: 34
diff changeset
1109 }
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1110 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1111
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1112 if (item->email->rtf_compressed && save_rtf) {
37
ddfb25318812 more valgrind fixes
carl
parents: 36
diff changeset
1113 //int32_t tester;
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1114 DEBUG_EMAIL(("Adding RTF body as attachment\n"));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1115 current_attach = (pst_item_attach*)xmalloc(sizeof(pst_item_attach));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1116 memset(current_attach, 0, sizeof(pst_item_attach));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1117 current_attach->next = item->attach;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1118 item->attach = current_attach;
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
1119 current_attach->data = lzfu_decompress(item->email->rtf_compressed, item->email->rtf_compressed_size, &current_attach->size);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1120 current_attach->filename2 = xmalloc(strlen(RTF_ATTACH_NAME)+2);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1121 strcpy(current_attach->filename2, RTF_ATTACH_NAME);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1122 current_attach->mimetype = xmalloc(strlen(RTF_ATTACH_TYPE)+2);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1123 strcpy(current_attach->mimetype, RTF_ATTACH_TYPE);
37
ddfb25318812 more valgrind fixes
carl
parents: 36
diff changeset
1124 //memcpy(&tester, item->email->rtf_compressed+sizeof(int32_t), sizeof(int32_t));
ddfb25318812 more valgrind fixes
carl
parents: 36
diff changeset
1125 //LE32_CPU(tester);
ddfb25318812 more valgrind fixes
carl
parents: 36
diff changeset
1126 //printf("lz produced %d bytes, rtf claims %d bytes\n", current_attach->size, tester);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1127 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1128
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1129 if (item->email->encrypted_body || item->email->encrypted_htmlbody) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1130 // if either the body or htmlbody is encrypted, add them as attachments
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1131 if (item->email->encrypted_body) {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1132 DEBUG_EMAIL(("Adding Encrypted Body as attachment\n"));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1133 current_attach = (pst_item_attach*) xmalloc(sizeof(pst_item_attach));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1134 memset(current_attach, 0, sizeof(pst_item_attach));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1135 current_attach->next = item->attach;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1136 item->attach = current_attach;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1137 current_attach->data = item->email->encrypted_body;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1138 current_attach->size = item->email->encrypted_body_size;
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1139 item->email->encrypted_body = NULL;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1140 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1141
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1142 if (item->email->encrypted_htmlbody) {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1143 DEBUG_EMAIL(("Adding encrypted HTML body as attachment\n"));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1144 current_attach = (pst_item_attach*) xmalloc(sizeof(pst_item_attach));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1145 memset(current_attach, 0, sizeof(pst_item_attach));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1146 current_attach->next = item->attach;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1147 item->attach = current_attach;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1148 current_attach->data = item->email->encrypted_htmlbody;
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1149 current_attach->size = item->email->encrypted_htmlbody_size;
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1150 item->email->encrypted_htmlbody = NULL;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1151 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1152 write_email_body(f_output, "The body of this email is encrypted. This isn't supported yet, but the body is now an attachment\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1153 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1154
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1155 // attachments
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1156 attach_num = 0;
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1157 for (current_attach = item->attach; current_attach; current_attach = current_attach->next) {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1158 DEBUG_EMAIL(("Attempting Attachment encoding\n"));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1159 if (!current_attach->data) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1160 DEBUG_EMAIL(("Data of attachment is NULL!. Size is supposed to be %i\n", current_attach->size));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1161 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1162 if (mode == MODE_SEPERATE && !mode_MH)
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1163 write_separate_attachment(f_name, current_attach, ++attach_num, pst);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1164 else
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1165 write_inline_attachment(f_output, current_attach, boundary, pst);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1166 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1167 if (mode != MODE_SEPERATE) { /* do not add a boundary after the last attachment for mode_MH */
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1168 DEBUG_EMAIL(("Writing buffer between emails\n"));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1169 if (boundary) fprintf(f_output, "\n--%s--\n", boundary);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1170 fprintf(f_output, "\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1171 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1172 if (boundary) free (boundary);
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1173 DEBUG_RET();
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1174 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1175
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1176
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1177 void write_vcard(FILE* f_output, pst_item_contact* contact, char comment[])
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1178 {
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1179 // We can only call rfc escape once per printf, since the second call
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1180 // may free the buffer returned by the first call.
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1181 // I had tried to place those into a single printf - Carl.
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1182
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1183 DEBUG_ENT("write_vcard");
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1184 // the specification I am following is (hopefully) RFC2426 vCard Mime Directory Profile
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1185 fprintf(f_output, "BEGIN:VCARD\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1186 fprintf(f_output, "FN:%s\n", rfc2426_escape(contact->fullname));
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1187
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1188 //fprintf(f_output, "N:%s;%s;%s;%s;%s\n",
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1189 fprintf(f_output, "N:%s;", (!contact->surname) ? "" : rfc2426_escape(contact->surname));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1190 fprintf(f_output, "%s;", (!contact->first_name) ? "" : rfc2426_escape(contact->first_name));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1191 fprintf(f_output, "%s;", (!contact->middle_name) ? "" : rfc2426_escape(contact->middle_name));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1192 fprintf(f_output, "%s;", (!contact->display_name_prefix) ? "" : rfc2426_escape(contact->display_name_prefix));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1193 fprintf(f_output, "%s\n", (!contact->suffix) ? "" : rfc2426_escape(contact->suffix));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1194
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1195 if (contact->nickname)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1196 fprintf(f_output, "NICKNAME:%s\n", rfc2426_escape(contact->nickname));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1197 if (contact->address1)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1198 fprintf(f_output, "EMAIL:%s\n", rfc2426_escape(contact->address1));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1199 if (contact->address2)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1200 fprintf(f_output, "EMAIL:%s\n", rfc2426_escape(contact->address2));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1201 if (contact->address3)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1202 fprintf(f_output, "EMAIL:%s\n", rfc2426_escape(contact->address3));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1203 if (contact->birthday)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1204 fprintf(f_output, "BDAY:%s\n", rfc2425_datetime_format(contact->birthday));
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1205
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1206 if (contact->home_address) {
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1207 //fprintf(f_output, "ADR;TYPE=home:%s;%s;%s;%s;%s;%s;%s\n",
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1208 fprintf(f_output, "ADR;TYPE=home:%s;", (!contact->home_po_box) ? "" : rfc2426_escape(contact->home_po_box));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1209 fprintf(f_output, "%s;", ""); // extended Address
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1210 fprintf(f_output, "%s;", (!contact->home_street) ? "" : rfc2426_escape(contact->home_street));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1211 fprintf(f_output, "%s;", (!contact->home_city) ? "" : rfc2426_escape(contact->home_city));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1212 fprintf(f_output, "%s;", (!contact->home_state) ? "" : rfc2426_escape(contact->home_state));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1213 fprintf(f_output, "%s;", (!contact->home_postal_code) ? "" : rfc2426_escape(contact->home_postal_code));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1214 fprintf(f_output, "%s\n", (!contact->home_country) ? "" : rfc2426_escape(contact->home_country));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1215 fprintf(f_output, "LABEL;TYPE=home:%s\n", rfc2426_escape(contact->home_address));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1216 }
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1217
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1218 if (contact->business_address) {
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1219 //fprintf(f_output, "ADR;TYPE=work:%s;%s;%s;%s;%s;%s;%s\n",
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1220 fprintf(f_output, "ADR;TYPE=work:%s;", (!contact->business_po_box) ? "" : rfc2426_escape(contact->business_po_box));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1221 fprintf(f_output, "%s;", ""); // extended Address
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1222 fprintf(f_output, "%s;", (!contact->business_street) ? "" : rfc2426_escape(contact->business_street));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1223 fprintf(f_output, "%s;", (!contact->business_city) ? "" : rfc2426_escape(contact->business_city));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1224 fprintf(f_output, "%s;", (!contact->business_state) ? "" : rfc2426_escape(contact->business_state));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1225 fprintf(f_output, "%s;", (!contact->business_postal_code) ? "" : rfc2426_escape(contact->business_postal_code));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1226 fprintf(f_output, "%s\n", (!contact->business_country) ? "" : rfc2426_escape(contact->business_country));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1227 fprintf(f_output, "LABEL;TYPE=work:%s\n", rfc2426_escape(contact->business_address));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1228 }
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1229
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1230 if (contact->other_address) {
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1231 //fprintf(f_output, "ADR;TYPE=postal:%s;%s;%s;%s;%s;%s;%s\n",
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1232 fprintf(f_output, "ADR;TYPE=postal:%s;",(!contact->other_po_box) ? "" : rfc2426_escape(contact->other_po_box));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1233 fprintf(f_output, "%s;", ""); // extended Address
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1234 fprintf(f_output, "%s;", (!contact->other_street) ? "" : rfc2426_escape(contact->other_street));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1235 fprintf(f_output, "%s;", (!contact->other_city) ? "" : rfc2426_escape(contact->other_city));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1236 fprintf(f_output, "%s;", (!contact->other_state) ? "" : rfc2426_escape(contact->other_state));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1237 fprintf(f_output, "%s;", (!contact->other_postal_code) ? "" : rfc2426_escape(contact->other_postal_code));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1238 fprintf(f_output, "%s\n", (!contact->other_country) ? "" : rfc2426_escape(contact->other_country));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1239 fprintf(f_output, "LABEL;TYPE=postal:%s\n", rfc2426_escape(contact->other_address));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1240 }
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1241
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1242 if (contact->business_fax) fprintf(f_output, "TEL;TYPE=work,fax:%s\n", rfc2426_escape(contact->business_fax));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1243 if (contact->business_phone) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", rfc2426_escape(contact->business_phone));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1244 if (contact->business_phone2) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", rfc2426_escape(contact->business_phone2));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1245 if (contact->car_phone) fprintf(f_output, "TEL;TYPE=car,voice:%s\n", rfc2426_escape(contact->car_phone));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1246 if (contact->home_fax) fprintf(f_output, "TEL;TYPE=home,fax:%s\n", rfc2426_escape(contact->home_fax));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1247 if (contact->home_phone) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", rfc2426_escape(contact->home_phone));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1248 if (contact->home_phone2) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", rfc2426_escape(contact->home_phone2));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1249 if (contact->isdn_phone) fprintf(f_output, "TEL;TYPE=isdn:%s\n", rfc2426_escape(contact->isdn_phone));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1250 if (contact->mobile_phone) fprintf(f_output, "TEL;TYPE=cell,voice:%s\n", rfc2426_escape(contact->mobile_phone));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1251 if (contact->other_phone) fprintf(f_output, "TEL;TYPE=msg:%s\n", rfc2426_escape(contact->other_phone));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1252 if (contact->pager_phone) fprintf(f_output, "TEL;TYPE=pager:%s\n", rfc2426_escape(contact->pager_phone));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1253 if (contact->primary_fax) fprintf(f_output, "TEL;TYPE=fax,pref:%s\n", rfc2426_escape(contact->primary_fax));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1254 if (contact->primary_phone) fprintf(f_output, "TEL;TYPE=phone,pref:%s\n", rfc2426_escape(contact->primary_phone));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1255 if (contact->radio_phone) fprintf(f_output, "TEL;TYPE=pcs:%s\n", rfc2426_escape(contact->radio_phone));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1256 if (contact->telex) fprintf(f_output, "TEL;TYPE=bbs:%s\n", rfc2426_escape(contact->telex));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1257 if (contact->job_title) fprintf(f_output, "TITLE:%s\n", rfc2426_escape(contact->job_title));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1258 if (contact->profession) fprintf(f_output, "ROLE:%s\n", rfc2426_escape(contact->profession));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1259 if (contact->assistant_name || contact->assistant_phone) {
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1260 fprintf(f_output, "AGENT:BEGIN:VCARD\n");
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1261 if (contact->assistant_name) fprintf(f_output, "FN:%s\n", rfc2426_escape(contact->assistant_name));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1262 if (contact->assistant_phone) fprintf(f_output, "TEL:%s\n", rfc2426_escape(contact->assistant_phone));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1263 }
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1264 if (contact->company_name) fprintf(f_output, "ORG:%s\n", rfc2426_escape(contact->company_name));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1265 if (comment) fprintf(f_output, "NOTE:%s\n", rfc2426_escape(comment));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1266
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1267 fprintf(f_output, "VERSION: 3.0\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1268 fprintf(f_output, "END:VCARD\n\n");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1269 DEBUG_RET();
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1270 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1271
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1272
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1273 void write_appointment(FILE* f_output, pst_item_appointment* appointment,
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1274 pst_item_email* email, FILETIME* create_date, FILETIME* modify_date)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1275 {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1276 fprintf(f_output, "BEGIN:VEVENT\n");
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1277 if (create_date)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1278 fprintf(f_output, "CREATED:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1279 rfc2445_datetime_format(create_date));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1280 if (modify_date)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1281 fprintf(f_output, "LAST-MOD:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1282 rfc2445_datetime_format(modify_date));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1283 if (email && email->subject)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1284 fprintf(f_output, "SUMMARY:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1285 rfc2426_escape(email->subject->subj));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1286 if (email && email->body)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1287 fprintf(f_output, "DESCRIPTION:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1288 rfc2426_escape(email->body));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1289 if (appointment && appointment->start)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1290 fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1291 rfc2445_datetime_format(appointment->start));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1292 if (appointment && appointment->end)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1293 fprintf(f_output, "DTEND;VALUE=DATE-TIME:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1294 rfc2445_datetime_format(appointment->end));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1295 if (appointment && appointment->location)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1296 fprintf(f_output, "LOCATION:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1297 rfc2426_escape(appointment->location));
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1298 if (appointment) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1299 switch (appointment->showas) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1300 case PST_FREEBUSY_TENTATIVE:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1301 fprintf(f_output, "STATUS:TENTATIVE\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1302 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1303 case PST_FREEBUSY_FREE:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1304 // mark as transparent and as confirmed
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1305 fprintf(f_output, "TRANSP:TRANSPARENT\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1306 case PST_FREEBUSY_BUSY:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1307 case PST_FREEBUSY_OUT_OF_OFFICE:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1308 fprintf(f_output, "STATUS:CONFIRMED\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1309 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1310 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1311 switch (appointment->label) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1312 case PST_APP_LABEL_NONE:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1313 fprintf(f_output, "CATEGORIES:NONE\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1314 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1315 case PST_APP_LABEL_IMPORTANT:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1316 fprintf(f_output, "CATEGORIES:IMPORTANT\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1317 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1318 case PST_APP_LABEL_BUSINESS:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1319 fprintf(f_output, "CATEGORIES:BUSINESS\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1320 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1321 case PST_APP_LABEL_PERSONAL:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1322 fprintf(f_output, "CATEGORIES:PERSONAL\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1323 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1324 case PST_APP_LABEL_VACATION:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1325 fprintf(f_output, "CATEGORIES:VACATION\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1326 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1327 case PST_APP_LABEL_MUST_ATTEND:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1328 fprintf(f_output, "CATEGORIES:MUST-ATTEND\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1329 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1330 case PST_APP_LABEL_TRAVEL_REQ:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1331 fprintf(f_output, "CATEGORIES:TRAVEL-REQUIRED\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1332 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1333 case PST_APP_LABEL_NEEDS_PREP:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1334 fprintf(f_output, "CATEGORIES:NEEDS-PREPARATION\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1335 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1336 case PST_APP_LABEL_BIRTHDAY:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1337 fprintf(f_output, "CATEGORIES:BIRTHDAY\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1338 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1339 case PST_APP_LABEL_ANNIVERSARY:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1340 fprintf(f_output, "CATEGORIES:ANNIVERSARY\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1341 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1342 case PST_APP_LABEL_PHONE_CALL:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1343 fprintf(f_output, "CATEGORIES:PHONE-CALL\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1344 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1345 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1346 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1347 fprintf(f_output, "END:VEVENT\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1348 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1349
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1350
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1351 void create_enter_dir(struct file_ll* f, pst_item *item)
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1352 {
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1353 f->email_count = 0;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1354 f->skip_count = 0;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1355 f->type = item->type;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1356 f->stored_count = (item->folder) ? item->folder->email_count : 0;
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1357
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1358 DEBUG_ENT("create_enter_dir");
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1359 if (mode == MODE_KMAIL)
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1360 f->name = mk_kmail_dir(item->file_as); //create directory and form filename
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1361 else if (mode == MODE_RECURSE)
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1362 f->name = mk_recurse_dir(item->file_as);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1363 else if (mode == MODE_SEPERATE) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1364 // do similar stuff to recurse here.
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1365 mk_seperate_dir(item->file_as);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1366 f->name = (char*) xmalloc(10);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1367 memset(f->name, 0, 10);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1368 // sprintf(f->name, SEP_MAIL_FILE_TEMPLATE, f->email_count);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1369 } else {
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1370 f->name = (char*) xmalloc(strlen(item->file_as)+strlen(OUTPUT_TEMPLATE)+1);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1371 sprintf(f->name, OUTPUT_TEMPLATE, item->file_as);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1372 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1373
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1374 f->dname = (char*) xmalloc(strlen(item->file_as)+1);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1375 strcpy(f->dname, item->file_as);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1376
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1377 if (overwrite != 1) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1378 int x = 0;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1379 char *temp = (char*) xmalloc (strlen(f->name)+10); //enough room for 10 digits
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1380
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1381 sprintf(temp, "%s", f->name);
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
1382 check_filename(temp);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1383 while ((f->output = fopen(temp, "r"))) {
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1384 DEBUG_MAIN(("need to increase filename because one already exists with that name\n"));
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1385 DEBUG_MAIN(("- increasing it to %s%d\n", f->name, x));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1386 x++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1387 sprintf(temp, "%s%08d", f->name, x);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1388 DEBUG_MAIN(("- trying \"%s\"\n", f->name));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1389 if (x == 99999999) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1390 DIE(("create_enter_dir: Why can I not create a folder %s? I have tried %i extensions...\n", f->name, x));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1391 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1392 fclose(f->output);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1393 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1394 if (x > 0) { //then the f->name should change
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1395 free (f->name);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1396 f->name = temp;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1397 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1398 free(temp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1399 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1400 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1401
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1402 DEBUG_MAIN(("f->name = %s\nitem->folder_name = %s\n", f->name, item->file_as));
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1403 if (mode != MODE_SEPERATE) {
41
183ae993b9ad security fix for potential buffer overrun in lz decompress
carl
parents: 39
diff changeset
1404 check_filename(f->name);
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1405 if (!(f->output = fopen(f->name, "w"))) {
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1406 DIE(("create_enter_dir: Could not open file \"%s\" for write\n", f->name));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1407 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1408 }
31
b88ceb81dba2 mege changes from Joe Nahmias
carl
parents: 28
diff changeset
1409 DEBUG_RET();
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1410 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1411
39
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1412
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1413 void close_enter_dir(struct file_ll *f)
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1414 {
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1415 DEBUG_MAIN(("main: Email Count for folder %s is %i\n", f->dname, f->email_count));
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1416 if (output_mode != OUTPUT_QUIET)
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1417 printf("\t\"%s\" - %i items done, skipped %i, should have been %i\n",
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1418 f->dname, f->email_count, f->skip_count, f->stored_count);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1419 if (f->output) fclose(f->output);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1420 free(f->name);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1421 free(f->dname);
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1422
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1423 if (mode == MODE_KMAIL)
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1424 close_kmail_dir();
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1425 else if (mode == MODE_RECURSE)
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1426 close_recurse_dir();
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1427 else if (mode == MODE_SEPERATE)
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1428 close_seperate_dir();
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1429 }
2ad7ef0a3c4f more valgrind fixes
carl
parents: 38
diff changeset
1430