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