Mercurial > libpst
annotate src/readpst.c @ 36:6fe121a971c9 stable-0-5-7
valgrind fixes
author | carl |
---|---|
date | Thu, 09 Aug 2007 15:46:34 -0700 |
parents | 07177825c91b |
children | ddfb25318812 |
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))); |
36 | 1008 free(enc); // caught by valgrind |
25 | 1009 } else { |
1010 pst_attach_to_file_base64(pst, current_attach, f_output); | |
1011 } | |
1012 fprintf(f_output, "\n\n"); | |
31 | 1013 DEBUG_RET(); |
25 | 1014 } |
1015 | |
31 | 1016 |
1017 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 | 1018 { |
31 | 1019 DEBUG_ENT("write_normal_email"); |
1020 char *boundary = NULL; // the boundary marker between multipart sections | |
1021 int boundary_created = 0; // we have not (yet) created a new boundary | |
25 | 1022 char *temp = NULL; |
1023 int attach_num, base64_body = 0; | |
1024 time_t em_time; | |
1025 char *c_time; | |
31 | 1026 pst_item_attach* current_attach; |
25 | 1027 |
1028 // convert the sent date if it exists, or set it to a fixed date | |
31 | 1029 if (item->email->sent_date) { |
25 | 1030 em_time = fileTimeToUnixTime(item->email->sent_date, 0); |
1031 c_time = ctime(&em_time); | |
31 | 1032 if (c_time) |
25 | 1033 c_time[strlen(c_time)-1] = '\0'; //remove end \n |
1034 else | |
1035 c_time = "Fri Dec 28 12:06:21 2001"; | |
1036 } else | |
1037 c_time= "Fri Dec 28 12:06:21 2001"; | |
1038 | |
1039 // we will always look at the header to discover some stuff | |
31 | 1040 if (item->email->header ) { |
25 | 1041 char *b1, *b2; |
1042 // see if there is a boundary variable there | |
1043 // this search MUST be made case insensitive (DONE). | |
31 | 1044 // Also, we should check to find out if we are looking |
1045 // at the boundary associated with content-type, and that | |
1046 // the content type really is multipart | |
25 | 1047 |
1048 removeCR(item->email->header); | |
1049 | |
31 | 1050 if ((b2 = my_stristr(item->email->header, "boundary="))) { |
1051 int len; | |
25 | 1052 b2 += strlen("boundary="); // move boundary to first char of marker |
31 | 1053 |
25 | 1054 if (*b2 == '"') { |
1055 b2++; | |
1056 b1 = strchr(b2, '"'); // find terminating quote | |
1057 } else { | |
1058 b1 = b2; | |
1059 while (isgraph(*b1)) // find first char that isn't part of boundary | |
1060 b1++; | |
1061 } | |
31 | 1062 len = b1 - b2; |
1063 boundary = malloc(len+1); //malloc that length | |
1064 strncpy(boundary, b2, len); // copy boundary to another variable | |
1065 boundary[len] = '\0'; | |
25 | 1066 b1 = b2 = boundary; |
1067 while (*b2 != '\0') { // remove any CRs and Tabs | |
1068 if (*b2 != '\n' && *b2 != '\r' && *b2 != '\t') { | |
1069 *b1 = *b2; | |
1070 b1++; | |
1071 } | |
1072 b2++; | |
1073 } | |
1074 *b1 = '\0'; | |
31 | 1075 |
1076 DEBUG_EMAIL(("Found boundary of - %s\n", boundary)); | |
25 | 1077 } else { |
31 | 1078 DEBUG_EMAIL(("boundary not found in header\n")); |
25 | 1079 } |
1080 | |
1081 // also possible to set 7bit encoding detection here. | |
31 | 1082 if ((b2 = my_stristr(item->email->header, "Content-Transfer-Encoding:"))) { |
1083 if ((b2 = strchr(b2, ':'))) { | |
25 | 1084 b2++; // skip to the : at the end of the string |
31 | 1085 |
25 | 1086 while (*b2 == ' ' || *b2 == '\t') |
1087 b2++; | |
1088 if (pst_strincmp(b2, "base64", 6)==0) { | |
31 | 1089 DEBUG_EMAIL(("body is base64 encoded\n")); |
25 | 1090 base64_body = 1; |
1091 } | |
1092 } else { | |
1093 DEBUG_WARN(("found a ':' during the my_stristr, but not after that..\n")); | |
1094 } | |
1095 } | |
1096 } | |
1097 | |
31 | 1098 if (!boundary && (item->attach || (item->email->body && item->email->htmlbody) |
1099 || item->email->rtf_compressed || item->email->encrypted_body | |
1100 || item->email->encrypted_htmlbody)) { | |
1101 // we need to create a boundary here. | |
1102 DEBUG_EMAIL(("must create own boundary. oh dear.\n")); | |
1103 boundary = malloc(50 * sizeof(char)); // allow 50 chars for boundary | |
1104 boundary[0] = '\0'; | |
1105 sprintf(boundary, "--boundary-LibPST-iamunique-%i_-_-", rand()); | |
1106 DEBUG_EMAIL(("created boundary is %s\n", boundary)); | |
1107 boundary_created = 1; | |
25 | 1108 } |
1109 | |
31 | 1110 DEBUG_EMAIL(("About to print Header\n")); |
1111 | |
1112 if (item && item->email && item->email->subject && item->email->subject->subj) { | |
1113 DEBUG_EMAIL(("item->email->subject->subj = %s\n", item->email->subject->subj)); | |
1114 } | |
1115 | |
1116 if (item->email->header) { | |
1117 int len; | |
1118 char *soh = NULL; // real start of headers. | |
1119 | |
25 | 1120 // some of the headers we get from the file are not properly defined. |
1121 // they can contain some email stuff too. We will cut off the header | |
1122 // when we see a \n\n or \r\n\r\n | |
31 | 1123 removeCR(item->email->header); |
25 | 1124 temp = strstr(item->email->header, "\n\n"); |
1125 | |
31 | 1126 if (temp) { |
1127 DEBUG_EMAIL(("Found body text in header\n")); | |
1128 temp[1] = '\0'; // stop after first \n | |
25 | 1129 } |
31 | 1130 |
1131 // Now, write out the header... | |
1132 soh = skip_header_prologue(item->email->header); | |
25 | 1133 if (mode != MODE_SEPERATE) { |
1134 // don't put rubbish in if we are doing seperate | |
31 | 1135 if (strncmp(soh, "X-From_: ", 9) == 0 ) { |
1136 fputs("From ", f_output); | |
1137 soh += 9; | |
1138 } else | |
1139 fprintf(f_output, "From \"%s\" %s\n", item->email->outlook_sender_name, c_time); | |
25 | 1140 } |
31 | 1141 fprintf(f_output, "%s", soh); |
1142 len = strlen(soh); | |
1143 if (!len || (soh[len-1] != '\n')) fprintf(f_output, "\n"); | |
1144 | |
25 | 1145 } else { |
1146 //make up our own header! | |
1147 if (mode != MODE_SEPERATE) { | |
1148 // don't want this first line for this mode | |
31 | 1149 if (item->email->outlook_sender_name) { |
25 | 1150 temp = item->email->outlook_sender_name; |
1151 } else { | |
1152 temp = "(readpst_null)"; | |
1153 } | |
1154 fprintf(f_output, "From \"%s\" %s\n", temp, c_time); | |
1155 } | |
31 | 1156 |
1157 temp = item->email->outlook_sender; | |
1158 if (!temp) temp = ""; | |
25 | 1159 fprintf(f_output, "From: \"%s\" <%s>\n", item->email->outlook_sender_name, temp); |
31 | 1160 |
1161 if (item->email->subject) { | |
25 | 1162 fprintf(f_output, "Subject: %s\n", item->email->subject->subj); |
1163 } else { | |
1164 fprintf(f_output, "Subject: \n"); | |
1165 } | |
31 | 1166 |
25 | 1167 fprintf(f_output, "To: %s\n", item->email->sentto_address); |
31 | 1168 if (item->email->cc_address) { |
25 | 1169 fprintf(f_output, "Cc: %s\n", item->email->cc_address); |
1170 } | |
31 | 1171 |
1172 if (item->email->sent_date) { | |
25 | 1173 c_time = (char*) xmalloc(C_TIME_SIZE); |
1174 strftime(c_time, C_TIME_SIZE, "%a, %d %b %Y %H:%M:%S %z", gmtime(&em_time)); | |
1175 fprintf(f_output, "Date: %s\n", c_time); | |
1176 free(c_time); | |
1177 } | |
1178 } | |
1179 | |
31 | 1180 fprintf(f_output, "MIME-Version: 1.0\n"); |
1181 if (boundary && boundary_created) { | |
1182 // if we created the boundary, then it has NOT already been printed | |
1183 // in the headers above. | |
1184 if (item->attach) { | |
25 | 1185 // write the boundary stuff if we have attachments |
31 | 1186 fprintf(f_output, "Content-type: multipart/mixed;\n\tboundary=\"%s\"\n", boundary); |
1187 } else if (boundary) { | |
25 | 1188 // else if we have multipart/alternative then tell it so |
31 | 1189 fprintf(f_output, "Content-type: multipart/alternative;\n\tboundary=\"%s\"\n", boundary); |
25 | 1190 } else if (item->email->htmlbody) { |
1191 fprintf(f_output, "Content-type: text/html\n"); | |
1192 } | |
1193 } | |
31 | 1194 fprintf(f_output, "\n"); // start the body |
1195 DEBUG_EMAIL(("About to print Body\n")); | |
25 | 1196 |
31 | 1197 if (item->email->body) { |
25 | 1198 if (boundary) { |
1199 fprintf(f_output, "\n--%s\n", boundary); | |
31 | 1200 fprintf(f_output, "Content-type: text/plain\n"); |
25 | 1201 if (base64_body) |
1202 fprintf(f_output, "Content-Transfer-Encoding: base64\n"); | |
31 | 1203 fprintf(f_output, "\n"); |
25 | 1204 } |
1205 removeCR(item->email->body); | |
36 | 1206 if (base64_body) { |
1207 char *enc = base64_encode(item->email->body, strlen(item->email->body)); | |
1208 if (enc) { | |
1209 write_email_body(f_output, enc); | |
1210 free(enc); | |
1211 } | |
1212 } | |
1213 else { | |
25 | 1214 write_email_body(f_output, item->email->body); |
36 | 1215 } |
25 | 1216 } |
31 | 1217 |
1218 if (item->email->htmlbody) { | |
25 | 1219 if (boundary) { |
1220 fprintf(f_output, "\n--%s\n", boundary); | |
31 | 1221 fprintf(f_output, "Content-type: text/html\n"); |
36 | 1222 if (base64_body) fprintf(f_output, "Content-Transfer-Encoding: base64\n"); |
31 | 1223 fprintf(f_output, "\n"); |
25 | 1224 } |
1225 removeCR(item->email->htmlbody); | |
36 | 1226 if (base64_body) { |
1227 char *enc = base64_encode(item->email->htmlbody, strlen(item->email->htmlbody)); | |
1228 if (enc) { | |
1229 write_email_body(f_output, enc); | |
1230 free(enc); | |
1231 } | |
1232 } | |
1233 else { | |
25 | 1234 write_email_body(f_output, item->email->htmlbody); |
36 | 1235 } |
25 | 1236 } |
1237 | |
31 | 1238 if (item->email->rtf_compressed && save_rtf) { |
1239 DEBUG_EMAIL(("Adding RTF body as attachment\n")); | |
1240 current_attach = (pst_item_attach*)xmalloc(sizeof(pst_item_attach)); | |
1241 memset(current_attach, 0, sizeof(pst_item_attach)); | |
1242 current_attach->next = item->attach; | |
1243 item->attach = current_attach; | |
36 | 1244 current_attach->data = lzfu_decompress(item->email->rtf_compressed, ¤t_attach->size); |
31 | 1245 current_attach->filename2 = xmalloc(strlen(RTF_ATTACH_NAME)+2); |
1246 strcpy(current_attach->filename2, RTF_ATTACH_NAME); | |
1247 current_attach->mimetype = xmalloc(strlen(RTF_ATTACH_TYPE)+2); | |
1248 strcpy(current_attach->mimetype, RTF_ATTACH_TYPE); | |
1249 memcpy(&(current_attach->size), item->email->rtf_compressed+sizeof(int32_t), sizeof(int32_t)); | |
1250 LE32_CPU(current_attach->size); | |
25 | 1251 } |
31 | 1252 |
25 | 1253 if (item->email->encrypted_body || item->email->encrypted_htmlbody) { |
1254 // if either the body or htmlbody is encrypted, add them as attachments | |
1255 if (item->email->encrypted_body) { | |
31 | 1256 DEBUG_EMAIL(("Adding Encrypted Body as attachment\n")); |
1257 current_attach = (pst_item_attach*) xmalloc(sizeof(pst_item_attach)); | |
1258 memset(current_attach, 0, sizeof(pst_item_attach)); | |
1259 current_attach->next = item->attach; | |
1260 item->attach = current_attach; | |
1261 current_attach->data = item->email->encrypted_body; | |
1262 current_attach->size = item->email->encrypted_body_size; | |
25 | 1263 item->email->encrypted_body = NULL; |
1264 } | |
31 | 1265 |
25 | 1266 if (item->email->encrypted_htmlbody) { |
31 | 1267 DEBUG_EMAIL(("Adding encrypted HTML body as attachment\n")); |
1268 current_attach = (pst_item_attach*) xmalloc(sizeof(pst_item_attach)); | |
1269 memset(current_attach, 0, sizeof(pst_item_attach)); | |
1270 current_attach->next = item->attach; | |
1271 item->attach = current_attach; | |
1272 current_attach->data = item->email->encrypted_htmlbody; | |
1273 current_attach->size = item->email->encrypted_htmlbody_size; | |
25 | 1274 item->email->encrypted_htmlbody = NULL; |
1275 } | |
1276 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"); | |
1277 } | |
31 | 1278 |
25 | 1279 // attachments |
1280 attach_num = 0; | |
31 | 1281 for (current_attach = item->attach; |
1282 current_attach; | |
1283 current_attach = current_attach->next) { | |
1284 DEBUG_EMAIL(("Attempting Attachment encoding\n")); | |
1285 if (!current_attach->data) { | |
1286 DEBUG_EMAIL(("Data of attachment is NULL!. Size is supposed to be %i\n", current_attach->size)); | |
25 | 1287 } |
1288 if (mode == MODE_SEPERATE && !mode_MH) | |
31 | 1289 write_separate_attachment(f_name, current_attach, ++attach_num, pst); |
25 | 1290 else |
31 | 1291 write_inline_attachment(f_output, current_attach, boundary, pst); |
25 | 1292 } |
1293 if (mode != MODE_SEPERATE) { /* do not add a boundary after the last attachment for mode_MH */ | |
31 | 1294 DEBUG_EMAIL(("Writing buffer between emails\n")); |
1295 if (boundary) fprintf(f_output, "\n--%s--\n", boundary); | |
25 | 1296 fprintf(f_output, "\n\n"); |
1297 } | |
31 | 1298 if (boundary) free (boundary); |
1299 DEBUG_RET(); | |
25 | 1300 } |
1301 | |
31 | 1302 |
25 | 1303 void write_vcard(FILE* f_output, pst_item_contact* contact, char comment[]) |
1304 { | |
31 | 1305 DEBUG_ENT("write_vcard"); |
25 | 1306 // the specification I am following is (hopefully) RFC2426 vCard Mime Directory Profile |
1307 fprintf(f_output, "BEGIN:VCARD\n"); | |
1308 fprintf(f_output, "FN:%s\n", rfc2426_escape(contact->fullname)); | |
1309 fprintf(f_output, "N:%s;%s;%s;%s;%s\n", | |
31 | 1310 (!contact->surname) ? "" : rfc2426_escape(contact->surname), |
1311 (!contact->first_name) ? "" : rfc2426_escape(contact->first_name), | |
1312 (!contact->middle_name) ? "" : rfc2426_escape(contact->middle_name), | |
1313 (!contact->display_name_prefix) ? "" : rfc2426_escape(contact->display_name_prefix), | |
1314 (!contact->suffix) ? "" : rfc2426_escape(contact->suffix)); | |
1315 if (contact->nickname) | |
25 | 1316 fprintf(f_output, "NICKNAME:%s\n", rfc2426_escape(contact->nickname)); |
31 | 1317 if (contact->address1) |
25 | 1318 fprintf(f_output, "EMAIL:%s\n", rfc2426_escape(contact->address1)); |
31 | 1319 if (contact->address2) |
25 | 1320 fprintf(f_output, "EMAIL:%s\n", rfc2426_escape(contact->address2)); |
31 | 1321 if (contact->address3) |
25 | 1322 fprintf(f_output, "EMAIL:%s\n", rfc2426_escape(contact->address3)); |
31 | 1323 if (contact->birthday) |
25 | 1324 fprintf(f_output, "BDAY:%s\n", rfc2425_datetime_format(contact->birthday)); |
31 | 1325 if (contact->home_address) { |
25 | 1326 fprintf(f_output, "ADR;TYPE=home:%s;%s;%s;%s;%s;%s;%s\n", |
31 | 1327 (!contact->home_po_box) ? "" : rfc2426_escape(contact->home_po_box), |
1328 "", // extended Address | |
1329 (!contact->home_street) ? "" : rfc2426_escape(contact->home_street), | |
1330 (!contact->home_city) ? "" : rfc2426_escape(contact->home_city), | |
1331 (!contact->home_state) ? "" : rfc2426_escape(contact->home_state), | |
1332 (!contact->home_postal_code) ? "" : rfc2426_escape(contact->home_postal_code), | |
1333 (!contact->home_country) ? "" : rfc2426_escape(contact->home_country)); | |
25 | 1334 fprintf(f_output, "LABEL;TYPE=home:%s\n", rfc2426_escape(contact->home_address)); |
1335 } | |
31 | 1336 if (contact->business_address) { |
36 | 1337 // these should be equivalent, but valgrind complains about the single large fprintf |
1338 // | |
1339 char *ab = (!contact->business_po_box ) ? "" : rfc2426_escape(contact->business_po_box ); | |
1340 char *ac = (!contact->business_street ) ? "" : rfc2426_escape(contact->business_street ); | |
1341 char *ad = (!contact->business_city ) ? "" : rfc2426_escape(contact->business_city ); | |
1342 char *ae = (!contact->business_state ) ? "" : rfc2426_escape(contact->business_state ); | |
1343 char *af = (!contact->business_postal_code) ? "" : rfc2426_escape(contact->business_postal_code); | |
1344 char *ag = (!contact->business_country ) ? "" : rfc2426_escape(contact->business_country ); | |
1345 fprintf(f_output, "ADR;TYPE=work:%s;%s;%s;%s;%s;%s;%s\n", ab, "", ac, ad, ae, af, ag); | |
1346 //fprintf(f_output, "ADR;TYPE=work:%s;%s;%s;%s;%s;%s;%s\n", | |
1347 // (!contact->business_po_box) ? "" : rfc2426_escape(contact->business_po_box), | |
1348 // "", // extended Address | |
1349 // (!contact->business_street) ? "" : rfc2426_escape(contact->business_street), | |
1350 // (!contact->business_city) ? "" : rfc2426_escape(contact->business_city), | |
1351 // (!contact->business_state) ? "" : rfc2426_escape(contact->business_state), | |
1352 // (!contact->business_postal_code) ? "" : rfc2426_escape(contact->business_postal_code), | |
1353 // (!contact->business_country) ? "" : rfc2426_escape(contact->business_country)); | |
25 | 1354 fprintf(f_output, "LABEL;TYPE=work:%s\n", rfc2426_escape(contact->business_address)); |
1355 } | |
31 | 1356 if (contact->other_address) { |
25 | 1357 fprintf(f_output, "ADR;TYPE=postal:%s;%s;%s;%s;%s;%s;%s\n", |
36 | 1358 (!contact->other_po_box) ? "" : rfc2426_escape(contact->other_po_box), |
25 | 1359 "", // extended Address |
31 | 1360 (!contact->other_street) ? "" : rfc2426_escape(contact->other_street), |
1361 (!contact->other_city) ? "" : rfc2426_escape(contact->other_city), | |
1362 (!contact->other_state) ? "" : rfc2426_escape(contact->other_state), | |
1363 (!contact->other_postal_code) ? "" : rfc2426_escape(contact->other_postal_code), | |
1364 (!contact->other_country) ? "" : rfc2426_escape(contact->other_country)); | |
1365 fprintf(f_output, "LABEL;TYPE=postal:%s\n", rfc2426_escape(contact->other_address)); | |
25 | 1366 } |
31 | 1367 if (contact->business_fax) |
25 | 1368 fprintf(f_output, "TEL;TYPE=work,fax:%s\n", |
1369 rfc2426_escape(contact->business_fax)); | |
31 | 1370 if (contact->business_phone) |
25 | 1371 fprintf(f_output, "TEL;TYPE=work,voice:%s\n", |
1372 rfc2426_escape(contact->business_phone)); | |
31 | 1373 if (contact->business_phone2) |
25 | 1374 fprintf(f_output, "TEL;TYPE=work,voice:%s\n", |
1375 rfc2426_escape(contact->business_phone2)); | |
31 | 1376 if (contact->car_phone) |
25 | 1377 fprintf(f_output, "TEL;TYPE=car,voice:%s\n", |
1378 rfc2426_escape(contact->car_phone)); | |
31 | 1379 if (contact->home_fax) |
25 | 1380 fprintf(f_output, "TEL;TYPE=home,fax:%s\n", |
1381 rfc2426_escape(contact->home_fax)); | |
31 | 1382 if (contact->home_phone) |
25 | 1383 fprintf(f_output, "TEL;TYPE=home,voice:%s\n", |
1384 rfc2426_escape(contact->home_phone)); | |
31 | 1385 if (contact->home_phone2) |
25 | 1386 fprintf(f_output, "TEL;TYPE=home,voice:%s\n", |
1387 rfc2426_escape(contact->home_phone2)); | |
31 | 1388 if (contact->isdn_phone) |
25 | 1389 fprintf(f_output, "TEL;TYPE=isdn:%s\n", |
1390 rfc2426_escape(contact->isdn_phone)); | |
31 | 1391 if (contact->mobile_phone) |
25 | 1392 fprintf(f_output, "TEL;TYPE=cell,voice:%s\n", |
1393 rfc2426_escape(contact->mobile_phone)); | |
31 | 1394 if (contact->other_phone) |
25 | 1395 fprintf(f_output, "TEL;TYPE=msg:%s\n", |
1396 rfc2426_escape(contact->other_phone)); | |
31 | 1397 if (contact->pager_phone) |
25 | 1398 fprintf(f_output, "TEL;TYPE=pager:%s\n", |
1399 rfc2426_escape(contact->pager_phone)); | |
31 | 1400 if (contact->primary_fax) |
25 | 1401 fprintf(f_output, "TEL;TYPE=fax,pref:%s\n", |
1402 rfc2426_escape(contact->primary_fax)); | |
31 | 1403 if (contact->primary_phone) |
25 | 1404 fprintf(f_output, "TEL;TYPE=phone,pref:%s\n", |
1405 rfc2426_escape(contact->primary_phone)); | |
31 | 1406 if (contact->radio_phone) |
25 | 1407 fprintf(f_output, "TEL;TYPE=pcs:%s\n", |
1408 rfc2426_escape(contact->radio_phone)); | |
31 | 1409 if (contact->telex) |
25 | 1410 fprintf(f_output, "TEL;TYPE=bbs:%s\n", |
1411 rfc2426_escape(contact->telex)); | |
31 | 1412 if (contact->job_title) |
25 | 1413 fprintf(f_output, "TITLE:%s\n", |
1414 rfc2426_escape(contact->job_title)); | |
31 | 1415 if (contact->profession) |
25 | 1416 fprintf(f_output, "ROLE:%s\n", |
1417 rfc2426_escape(contact->profession)); | |
31 | 1418 if (contact->assistant_name |
1419 || contact->assistant_phone) { | |
1420 fprintf(f_output, "AGENT:BEGIN:VCARD\n"); | |
1421 if (contact->assistant_name) | |
1422 fprintf(f_output, "FN:%s\n", | |
25 | 1423 rfc2426_escape(contact->assistant_name)); |
31 | 1424 if (contact->assistant_phone) |
1425 fprintf(f_output, "TEL:%s\n", | |
25 | 1426 rfc2426_escape(contact->assistant_phone)); |
1427 } | |
31 | 1428 if (contact->company_name) |
25 | 1429 fprintf(f_output, "ORG:%s\n", |
1430 rfc2426_escape(contact->company_name)); | |
31 | 1431 if (comment) |
25 | 1432 fprintf(f_output, "NOTE:%s\n", rfc2426_escape(comment)); |
1433 | |
1434 fprintf(f_output, "VERSION: 3.0\n"); | |
1435 fprintf(f_output, "END:VCARD\n\n"); | |
31 | 1436 DEBUG_RET(); |
25 | 1437 } |
1438 | |
31 | 1439 |
25 | 1440 void write_appointment(FILE* f_output, pst_item_appointment* appointment, |
31 | 1441 pst_item_email* email, FILETIME* create_date, FILETIME* modify_date) |
25 | 1442 { |
1443 fprintf(f_output, "BEGIN:VEVENT\n"); | |
31 | 1444 if (create_date) |
25 | 1445 fprintf(f_output, "CREATED:%s\n", |
1446 rfc2445_datetime_format(create_date)); | |
31 | 1447 if (modify_date) |
25 | 1448 fprintf(f_output, "LAST-MOD:%s\n", |
1449 rfc2445_datetime_format(modify_date)); | |
31 | 1450 if (email && email->subject) |
25 | 1451 fprintf(f_output, "SUMMARY:%s\n", |
1452 rfc2426_escape(email->subject->subj)); | |
31 | 1453 if (email && email->body) |
25 | 1454 fprintf(f_output, "DESCRIPTION:%s\n", |
1455 rfc2426_escape(email->body)); | |
31 | 1456 if (appointment && appointment->start) |
25 | 1457 fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n", |
1458 rfc2445_datetime_format(appointment->start)); | |
31 | 1459 if (appointment && appointment->end) |
25 | 1460 fprintf(f_output, "DTEND;VALUE=DATE-TIME:%s\n", |
1461 rfc2445_datetime_format(appointment->end)); | |
31 | 1462 if (appointment && appointment->location) |
25 | 1463 fprintf(f_output, "LOCATION:%s\n", |
1464 rfc2426_escape(appointment->location)); | |
31 | 1465 if (appointment) { |
25 | 1466 switch (appointment->showas) { |
1467 case PST_FREEBUSY_TENTATIVE: | |
1468 fprintf(f_output, "STATUS:TENTATIVE\n"); | |
1469 break; | |
1470 case PST_FREEBUSY_FREE: | |
1471 // mark as transparent and as confirmed | |
1472 fprintf(f_output, "TRANSP:TRANSPARENT\n"); | |
1473 case PST_FREEBUSY_BUSY: | |
1474 case PST_FREEBUSY_OUT_OF_OFFICE: | |
1475 fprintf(f_output, "STATUS:CONFIRMED\n"); | |
1476 break; | |
1477 } | |
1478 switch (appointment->label) { | |
1479 case PST_APP_LABEL_NONE: | |
1480 fprintf(f_output, "CATEGORIES:NONE\n"); | |
1481 break; | |
1482 case PST_APP_LABEL_IMPORTANT: | |
1483 fprintf(f_output, "CATEGORIES:IMPORTANT\n"); | |
1484 break; | |
1485 case PST_APP_LABEL_BUSINESS: | |
1486 fprintf(f_output, "CATEGORIES:BUSINESS\n"); | |
1487 break; | |
1488 case PST_APP_LABEL_PERSONAL: | |
1489 fprintf(f_output, "CATEGORIES:PERSONAL\n"); | |
1490 break; | |
1491 case PST_APP_LABEL_VACATION: | |
1492 fprintf(f_output, "CATEGORIES:VACATION\n"); | |
1493 break; | |
1494 case PST_APP_LABEL_MUST_ATTEND: | |
1495 fprintf(f_output, "CATEGORIES:MUST-ATTEND\n"); | |
1496 break; | |
1497 case PST_APP_LABEL_TRAVEL_REQ: | |
1498 fprintf(f_output, "CATEGORIES:TRAVEL-REQUIRED\n"); | |
1499 break; | |
1500 case PST_APP_LABEL_NEEDS_PREP: | |
1501 fprintf(f_output, "CATEGORIES:NEEDS-PREPARATION\n"); | |
1502 break; | |
1503 case PST_APP_LABEL_BIRTHDAY: | |
1504 fprintf(f_output, "CATEGORIES:BIRTHDAY\n"); | |
1505 break; | |
1506 case PST_APP_LABEL_ANNIVERSARY: | |
1507 fprintf(f_output, "CATEGORIES:ANNIVERSARY\n"); | |
1508 break; | |
1509 case PST_APP_LABEL_PHONE_CALL: | |
1510 fprintf(f_output, "CATEGORIES:PHONE-CALL\n"); | |
1511 break; | |
1512 } | |
1513 } | |
1514 fprintf(f_output, "END:VEVENT\n\n"); | |
1515 } | |
1516 | |
31 | 1517 |
25 | 1518 void create_enter_dir(struct file_ll* f, char file_as[], int mode, int overwrite) |
1519 { | |
31 | 1520 DEBUG_ENT("create_enter_dir"); |
25 | 1521 if (mode == MODE_KMAIL) |
1522 f->name = mk_kmail_dir(file_as); //create directory and form filename | |
1523 else if (mode == MODE_RECURSE) | |
1524 f->name = mk_recurse_dir(file_as); | |
1525 else if (mode == MODE_SEPERATE) { | |
1526 // do similar stuff to recurse here. | |
1527 mk_seperate_dir(file_as, overwrite); | |
1528 f->name = (char*) xmalloc(10); | |
1529 memset(f->name, 0, 10); | |
1530 // sprintf(f->name, SEP_MAIL_FILE_TEMPLATE, f->email_count); | |
1531 } else { | |
31 | 1532 f->name = (char*) xmalloc(strlen(file_as)+strlen(OUTPUT_TEMPLATE)+1); |
25 | 1533 sprintf(f->name, OUTPUT_TEMPLATE, file_as); |
1534 } | |
1535 | |
1536 f->dname = (char*) xmalloc(strlen(file_as)+1); | |
1537 strcpy(f->dname, file_as); | |
1538 | |
1539 if (overwrite != 1) { | |
1540 int x = 0; | |
1541 char *temp = (char*) xmalloc (strlen(f->name)+10); //enough room for 10 digits | |
1542 | |
1543 sprintf(temp, "%s", f->name); | |
1544 temp = check_filename(temp); | |
31 | 1545 while ((f->output = fopen(temp, "r"))) { |
1546 DEBUG_MAIN(("need to increase filename because one already exists with that name\n")); | |
1547 DEBUG_MAIN(("- increasing it to %s%d\n", f->name, x)); | |
25 | 1548 x++; |
1549 sprintf(temp, "%s%08d", f->name, x); | |
31 | 1550 DEBUG_MAIN(("- trying \"%s\"\n", f->name)); |
25 | 1551 if (x == 99999999) { |
1552 DIE(("create_enter_dir: Why can I not create a folder %s? I have tried %i extensions...\n", f->name, x)); | |
1553 } | |
1554 fclose(f->output); | |
1555 } | |
1556 if (x > 0) { //then the f->name should change | |
1557 free (f->name); | |
1558 f->name = temp; | |
1559 } else { | |
1560 free(temp); | |
1561 } | |
1562 } | |
1563 | |
31 | 1564 DEBUG_MAIN(("f->name = %s\nitem->folder_name = %s\n", f->name, file_as)); |
25 | 1565 if (mode != MODE_SEPERATE) { |
1566 f->name = check_filename(f->name); | |
31 | 1567 if (!(f->output = fopen(f->name, "w"))) { |
25 | 1568 DIE(("create_enter_dir: Could not open file \"%s\" for write\n", f->name)); |
1569 } | |
1570 } | |
31 | 1571 DEBUG_RET(); |
25 | 1572 } |
1573 |