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