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