16
|
1 /***
|
|
2 * lspst.c
|
|
3 * Part of the LibPST project
|
|
4 * Author: Joe Nahmias <joe@nahmias.net>
|
|
5 * Based on readpst.c by by David Smith <dave.s@earthcorp.com>
|
|
6 *
|
|
7 */
|
|
8
|
|
9 // header file includes {{{1
|
|
10 #include <stdio.h>
|
|
11 #include <stdlib.h>
|
|
12 #include <time.h>
|
|
13 #include <string.h>
|
|
14 #include <ctype.h>
|
|
15 #include <errno.h>
|
|
16
|
|
17 #include "libpst.h"
|
|
18 #include "define.h"
|
|
19 #include "timeconv.h"
|
|
20 // }}}1
|
|
21 // struct file_ll {{{1
|
|
22 struct file_ll {
|
|
23 char *name;
|
|
24 char *dname;
|
|
25 FILE * output;
|
|
26 int32_t stored_count;
|
|
27 int32_t email_count;
|
|
28 int32_t skip_count;
|
|
29 int32_t type;
|
|
30 struct file_ll *next;
|
|
31 };
|
|
32 // }}}1
|
|
33 // Function Declarations {{{1
|
|
34 void canonicalize_filename(char *fname);
|
|
35 int chr_count(char *str, char x);
|
|
36 void debug_print(char *fmt, ...);
|
|
37 char *rfc2426_escape(char *str);
|
|
38 char *rfc2445_datetime_format(FILETIME *ft);
|
|
39 // }}}1
|
31
|
40 #ifndef LSPST_DEBUG_MAIN
|
|
41 #define LSPST_DEBUG_MAIN(x) debug_print x;
|
16
|
42 #endif
|
|
43 // int main(int argc, char** argv) {{{1
|
|
44 int main(int argc, char** argv) {
|
|
45
|
|
46 // declarations {{{2
|
|
47 pst_item *item = NULL;
|
|
48 pst_file pstfile;
|
|
49 pst_desc_ll *d_ptr;
|
|
50 char *temp = NULL; //temporary char pointer
|
|
51 int skip_child = 0;
|
31
|
52 struct file_ll *f = NULL, *head = NULL;
|
16
|
53 // }}}2
|
|
54
|
|
55 if (argc <= 1)
|
|
56 DIE(("Missing PST filename.\n"));
|
|
57
|
|
58 // Open PST file
|
|
59 if ( pst_open(&pstfile, argv[1], "r") )
|
|
60 DIE(("Error opening File\n"));
|
|
61 // Load PST index
|
|
62 if ( pst_load_index(&pstfile) )
|
|
63 DIE(("Index Error\n"));
|
|
64 pst_load_extended_attributes(&pstfile);
|
|
65
|
|
66 d_ptr = pstfile.d_head; // first record is main record
|
|
67 if ((item = _pst_parse_item(&pstfile, d_ptr)) == NULL || item->message_store == NULL) {
|
|
68 DIE(("main: Could not get root record\n"));
|
|
69 }
|
|
70
|
|
71 // default the file_as to the same as the main filename if it doesn't exist
|
|
72 if (item->file_as == NULL) {
|
|
73 if ((temp = strrchr(argv[1], '/')) == NULL)
|
|
74 if ((temp = strrchr(argv[1], '\\')) == NULL)
|
|
75 temp = argv[1];
|
|
76 else
|
|
77 temp++; // get past the "\\"
|
|
78 else
|
|
79 temp++; // get past the "/"
|
|
80 item->file_as = (char*)xmalloc(strlen(temp)+1);
|
|
81 strcpy(item->file_as, temp);
|
|
82 }
|
|
83 fprintf(stderr, "item->file_as = '%s'.\n", item->file_as);
|
|
84
|
|
85 // setup head file_ll
|
|
86 head = (struct file_ll*) malloc(sizeof(struct file_ll));
|
|
87 memset(head, 0, sizeof(struct file_ll));
|
|
88 head->email_count = 0;
|
|
89 head->skip_count = 0;
|
|
90 head->next = NULL;
|
|
91 head->name = "mbox";
|
|
92 head->dname = (char*) malloc(strlen(item->file_as)+1);
|
|
93 strcpy(head->dname, item->file_as);
|
|
94 head->type = item->type;
|
31
|
95 LSPST_DEBUG_MAIN(("head @ %p: name = '%s', dname = '%s', next = %p.\n", head, head->name, head->dname, head->next));
|
16
|
96
|
|
97 if ((d_ptr = pst_getTopOfFolders(&pstfile, item)) == NULL) {
|
|
98 DIE(("Top of folders record not found. Cannot continue\n"));
|
|
99 }
|
31
|
100 LSPST_DEBUG_MAIN(("d_ptr(TOF) = %p.\n", d_ptr));
|
16
|
101
|
|
102 if (item){
|
|
103 _pst_freeItem(item);
|
|
104 item = NULL;
|
|
105 }
|
|
106
|
|
107 d_ptr = d_ptr->child; // do the children of TOPF
|
31
|
108 LSPST_DEBUG_MAIN(("d_ptr(TOF->child) = %p.\n", d_ptr));
|
16
|
109
|
31
|
110 LSPST_DEBUG_MAIN(("main: About to do email stuff\n"));
|
16
|
111 while (d_ptr != NULL) {
|
|
112 // Process d_ptr {{{2
|
31
|
113 LSPST_DEBUG_MAIN(("main: New item record, d_ptr = %p.\n", d_ptr));
|
16
|
114 if (d_ptr->desc == NULL) {
|
|
115 DEBUG_WARN(("main: ERROR ?? item's desc record is NULL\n"));
|
|
116 f->skip_count++;
|
|
117 goto check_parent;
|
|
118 }
|
31
|
119 LSPST_DEBUG_MAIN(("main: Desc Email ID %x [d_ptr->id = %x]\n", d_ptr->desc->id, d_ptr->id));
|
16
|
120
|
|
121 item = _pst_parse_item(&pstfile, d_ptr);
|
31
|
122 LSPST_DEBUG_MAIN(("main: About to process item @ %p.\n", item));
|
16
|
123 if (item != NULL) {
|
|
124
|
|
125 // there should only be one message_store, and we have already
|
|
126 // done it
|
|
127 if (item->message_store != NULL) {
|
|
128 DIE(("ERROR(main): A second message_store has been found.\n"));
|
|
129 }
|
|
130
|
|
131 if (item->folder != NULL) {
|
|
132 // Process Folder item {{{3
|
|
133 // if this is a folder, we want to recurse into it
|
|
134 printf("Folder");
|
|
135 if (item->file_as != NULL)
|
|
136 printf("\t%s/", item->file_as);
|
|
137 printf("\n");
|
|
138
|
31
|
139 LSPST_DEBUG_MAIN(("main: I think I may try to go into folder \"%s\"\n", item->file_as));
|
16
|
140 f = (struct file_ll*) malloc(sizeof(struct file_ll));
|
|
141 memset(f, 0, sizeof(struct file_ll));
|
|
142 f->next = head;
|
|
143 f->email_count = 0;
|
|
144 f->type = item->type;
|
|
145 f->stored_count = item->folder->email_count;
|
|
146 head = f;
|
|
147 f->name = "mbox";
|
|
148 f->dname = (char*) xmalloc(strlen(item->file_as)+1);
|
|
149 strcpy(f->dname, item->file_as);
|
|
150
|
31
|
151 LSPST_DEBUG_MAIN(("main: f->name = %s\nitem->folder_name = %s\n", f->name, item->file_as));
|
16
|
152 canonicalize_filename(f->name);
|
|
153
|
|
154 if (d_ptr->child != NULL) {
|
|
155 d_ptr = d_ptr->child;
|
|
156 skip_child = 1;
|
|
157 } else {
|
31
|
158 LSPST_DEBUG_MAIN(("main: Folder has NO children. Creating directory, and closing again\n"));
|
16
|
159 // printf("\tNo items to process in folder \"%s\", should have been %i\n", f->dname, f->stored_count);
|
|
160 head = f->next;
|
|
161 if (f->output != NULL)
|
|
162 fclose(f->output);
|
|
163 free(f->dname);
|
|
164 free(f->name);
|
|
165 free(f);
|
|
166
|
|
167 f = head;
|
|
168 }
|
|
169 _pst_freeItem(item);
|
|
170 item = NULL; // just for the odd situations!
|
|
171 goto check_parent;
|
|
172 // }}}3
|
|
173 } else if (item->contact != NULL) {
|
|
174 // Process Contact item {{{3
|
|
175 if (f->type != PST_TYPE_CONTACT) {
|
31
|
176 LSPST_DEBUG_MAIN(("main: I have a contact, but the folder isn't a contacts folder. "
|
16
|
177 "Will process anyway\n"));
|
|
178 }
|
|
179 if (item->type != PST_TYPE_CONTACT) {
|
31
|
180 LSPST_DEBUG_MAIN(("main: I have an item that has contact info, but doesn't say that"
|
16
|
181 " it is a contact. Type is \"%s\"\n", item->ascii_type));
|
31
|
182 LSPST_DEBUG_MAIN(("main: Processing anyway\n"));
|
16
|
183 }
|
|
184
|
|
185 printf("Contact");
|
|
186 if (item->contact->fullname != NULL)
|
|
187 printf("\t%s", rfc2426_escape(item->contact->fullname));
|
|
188 printf("\n");
|
|
189 // }}}3
|
|
190 } else if (item->email != NULL &&
|
|
191 (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_REPORT)) {
|
|
192 // Process Email item {{{3
|
|
193 printf("Email");
|
|
194 if (item->email->outlook_sender_name != NULL)
|
|
195 printf("\tFrom: %s", item->email->outlook_sender_name);
|
|
196 if (item->email->subject->subj != NULL)
|
|
197 printf("\tSubject: %s", item->email->subject->subj);
|
|
198 printf("\n");
|
|
199 // }}}3
|
|
200 } else if (item->type == PST_TYPE_JOURNAL) {
|
|
201 // Process Journal item {{{3
|
|
202 if (f->type != PST_TYPE_JOURNAL) {
|
31
|
203 LSPST_DEBUG_MAIN(("main: I have a journal entry, but folder isn't specified as a journal type. Processing...\n"));
|
16
|
204 }
|
|
205
|
|
206 printf("Journal\t%s\n", rfc2426_escape(item->email->subject->subj));
|
|
207 // }}}3
|
|
208 } else if (item->type == PST_TYPE_APPOINTMENT) {
|
|
209 // Process Calendar Appointment item {{{3
|
|
210 // deal with Calendar appointments
|
|
211
|
31
|
212 LSPST_DEBUG_MAIN(("main: Processing Appointment Entry\n"));
|
16
|
213 if (f->type != PST_TYPE_APPOINTMENT) {
|
31
|
214 LSPST_DEBUG_MAIN(("main: I have an appointment, but folder isn't specified as an appointment type. Processing...\n"));
|
16
|
215 }
|
|
216
|
|
217 printf("Appointment");
|
|
218 if (item->email != NULL && item->email->subject != NULL)
|
|
219 printf("\tSUMMARY: %s", rfc2426_escape(item->email->subject->subj));
|
31
|
220 if (item->appointment != NULL) {
|
|
221 if (item->appointment->start != NULL)
|
|
222 printf("\tSTART: %s", rfc2445_datetime_format(item->appointment->start));
|
|
223 if (item->appointment->end != NULL)
|
|
224 printf("\tEND: %s", rfc2445_datetime_format(item->appointment->end));
|
|
225 printf("\tALL DAY: %s", (item->appointment->all_day==1 ? "Yes" : "No"));
|
|
226 }
|
16
|
227 printf("\n");
|
|
228
|
|
229 // }}}3
|
|
230 } else {
|
|
231 f->skip_count++;
|
31
|
232 LSPST_DEBUG_MAIN(("main: Unknown item type. %i. Ascii1=\"%s\"\n", \
|
16
|
233 item->type, item->ascii_type));
|
|
234 }
|
|
235 } else {
|
|
236 f->skip_count++;
|
31
|
237 LSPST_DEBUG_MAIN(("main: A NULL item was seen\n"));
|
16
|
238 }
|
|
239
|
|
240 check_parent:
|
|
241 // _pst_freeItem(item);
|
|
242 while (!skip_child && d_ptr->next == NULL && d_ptr->parent != NULL) {
|
31
|
243 LSPST_DEBUG_MAIN(("main: Going to Parent\n"));
|
16
|
244 head = f->next;
|
|
245 if (f->output != NULL)
|
|
246 fclose(f->output);
|
31
|
247 LSPST_DEBUG_MAIN(("main: Email Count for folder %s is %i\n", f->dname, f->email_count));
|
16
|
248 /*
|
|
249 printf("\t\"%s\" - %i items done, skipped %i, should have been %i\n", \
|
|
250 f->dname, f->email_count, f->skip_count, f->stored_count);
|
|
251 */
|
|
252
|
|
253 free(f->name);
|
|
254 free(f->dname);
|
|
255 free(f);
|
|
256 f = head;
|
|
257 if (head == NULL) { //we can't go higher. Must be at start?
|
31
|
258 LSPST_DEBUG_MAIN(("main: We are now trying to go above the highest level. We must be finished\n"));
|
16
|
259 break; //from main while loop
|
|
260 }
|
|
261 d_ptr = d_ptr->parent;
|
|
262 skip_child = 0;
|
|
263 }
|
|
264
|
|
265 if (item != NULL) {
|
31
|
266 LSPST_DEBUG_MAIN(("main: Freeing memory used by item\n"));
|
16
|
267 _pst_freeItem(item);
|
|
268 item = NULL;
|
|
269 }
|
|
270
|
|
271 if (!skip_child)
|
|
272 d_ptr = d_ptr->next;
|
|
273 else
|
|
274 skip_child = 0;
|
|
275
|
31
|
276 if (d_ptr == NULL) { LSPST_DEBUG_MAIN(("main: d_ptr is now NULL\n")); }
|
16
|
277
|
|
278 // }}}2
|
|
279 } // end while(d_ptr != NULL)
|
31
|
280 LSPST_DEBUG_MAIN(("main: Finished.\n"));
|
16
|
281
|
|
282 // Cleanup {{{2
|
|
283 pst_close(&pstfile);
|
|
284 while (f != NULL) {
|
|
285 if (f->output != NULL)
|
|
286 fclose(f->output);
|
|
287 free(f->name);
|
|
288 free(f->dname);
|
|
289
|
|
290 head = f->next;
|
|
291 free(f);
|
|
292 f = head;
|
|
293 }
|
|
294 DEBUG_RET();
|
|
295 // }}}2
|
|
296
|
|
297 return 0;
|
|
298 }
|
|
299 // }}}1
|
|
300 // void canonicalize_filename(char *fname) {{{1
|
|
301 // This function will make sure that a filename is in cannonical form. That
|
|
302 // is, it will replace any slashes, backslashes, or colons with underscores.
|
|
303 void canonicalize_filename(char *fname) {
|
|
304 DEBUG_ENT("canonicalize_filename");
|
|
305 if (fname == NULL) {
|
|
306 DEBUG_RET();
|
|
307 return;
|
|
308 }
|
|
309 while ((fname = strpbrk(fname, "/\\:")) != NULL)
|
|
310 *fname = '_';
|
|
311 DEBUG_RET();
|
|
312 }
|
|
313 // }}}1
|
|
314 // int chr_count(char *str, char x) {{{1
|
|
315 int chr_count(char *str, char x) {
|
|
316 int r = 0;
|
|
317 if (str == NULL) return 0;
|
|
318 while (*str != '\0') {
|
|
319 if (*str == x)
|
|
320 r++;
|
|
321 str++;
|
|
322 }
|
|
323 return r;
|
|
324 }
|
|
325 // }}}1
|
|
326 // void debug_print(char *fmt, ...) {{{1
|
|
327 void debug_print(char *fmt, ...) {
|
|
328 // shamlessly stolen from minprintf() in K&R pg. 156
|
|
329 va_list ap;
|
|
330 char *p, *sval;
|
|
331 void *pval;
|
|
332 int ival;
|
|
333 double dval;
|
|
334 FILE *fp = stderr;
|
|
335
|
|
336 va_start(ap, fmt);
|
|
337 for(p = fmt; *p; p++) {
|
|
338 if (*p != '%') {
|
|
339 fputc(*p, fp);
|
|
340 continue;
|
|
341 }
|
|
342 switch (tolower(*++p)) {
|
|
343 case 'd': case 'i':
|
|
344 ival = va_arg(ap, int);
|
|
345 fprintf(fp, "%d", ival);
|
|
346 break;
|
|
347 case 'f':
|
|
348 dval = va_arg(ap, double);
|
|
349 fprintf(fp, "%f", dval);
|
|
350 break;
|
|
351 case 's':
|
|
352 for (sval = va_arg(ap, char *); *sval; ++sval)
|
|
353 fputc(*sval, fp);
|
|
354 break;
|
|
355 case 'p':
|
|
356 pval = va_arg(ap, void *);
|
|
357 fprintf(fp, "%p", pval);
|
|
358 break;
|
|
359 case 'x':
|
|
360 ival = va_arg(ap, int);
|
|
361 fprintf(fp, "%#010x", ival);
|
|
362 break;
|
|
363 default:
|
|
364 fputc(*p, fp);
|
|
365 break;
|
|
366 }
|
|
367 }
|
|
368 va_end(ap);
|
|
369 }
|
|
370 // }}}1
|
|
371 // char *rfc2426_escape(char *str) {{{1
|
|
372 char *rfc2426_escape(char *str) {
|
|
373 static char *buf = NULL;
|
|
374 char *a, *b;
|
|
375 int y, z;
|
|
376
|
|
377 DEBUG_ENT("rfc2426_escape");
|
|
378 if (str == NULL) {
|
|
379 DEBUG_RET();
|
|
380 return NULL;
|
|
381 }
|
|
382
|
|
383 // calculate space required to escape all the commas, semi-colons, backslashes, and newlines
|
|
384 y = chr_count(str, ',') + chr_count(str, '\\') + chr_count(str, ';') + chr_count(str, '\n');
|
|
385 // count how many carriage-returns we have to skip
|
|
386 z = chr_count(str, '\r');
|
|
387
|
|
388 if (y == 0 && z == 0) {
|
|
389 // there isn't any work required
|
|
390 DEBUG_RET();
|
|
391 return str;
|
|
392 }
|
|
393
|
|
394 buf = (char *) realloc( buf, strlen(str) + y - z + 1 );
|
|
395 for (a = str, b = buf; *a != '\0'; ++a, ++b)
|
|
396 switch (*a) {
|
|
397 case ',' : case '\\': case ';' : case '\n':
|
|
398 // insert backslash to escape
|
|
399 *(b++) = '\\';
|
|
400 *b = *a;
|
|
401 break;
|
|
402 case '\r':
|
|
403 // skip
|
|
404 break;
|
|
405 default:
|
|
406 *b = *a;
|
|
407 }
|
|
408 *b = '\0'; // NUL-terminate the string
|
|
409
|
|
410 DEBUG_RET();
|
|
411 return buf;
|
|
412 }
|
|
413 // }}}1
|
|
414 // char *rfc2445_datetime_format(FILETIME *ft) {{{1
|
|
415 char *rfc2445_datetime_format(FILETIME *ft) {
|
|
416 static char* buffer = NULL;
|
|
417 struct tm *stm = NULL;
|
|
418 DEBUG_ENT("rfc2445_datetime_format");
|
|
419 if (buffer == NULL)
|
|
420 buffer = malloc(30); // should be enough
|
|
421 stm = fileTimeToStructTM(ft);
|
|
422 if (strftime(buffer, 30, "%Y%m%dT%H%M%SZ", stm)==0) {
|
|
423 DEBUG_INFO(("Problem occured formatting date\n"));
|
|
424 }
|
|
425 DEBUG_RET();
|
|
426 return buffer;
|
|
427 }
|
|
428 // }}}1
|
|
429
|
|
430 // vim:sw=4 ts=4:
|
|
431 // vim600: set foldlevel=0 foldmethod=marker:
|