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