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