comparison lspst.c @ 3:fac01758bd41

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