annotate readpst.c @ 5:3e9cea0af4a1

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