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