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