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
|
43
|
9 // header file includes
|
16
|
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"
|
43
|
20
|
16
|
21 struct file_ll {
|
43
|
22 char *dname;
|
|
23 int32_t stored_count;
|
|
24 int32_t email_count;
|
|
25 int32_t skip_count;
|
|
26 int32_t type;
|
16
|
27 };
|
43
|
28
|
|
29
|
16
|
30 void canonicalize_filename(char *fname);
|
|
31 void debug_print(char *fmt, ...);
|
43
|
32
|
|
33 // global settings
|
|
34 pst_file pstfile;
|
|
35
|
16
|
36
|
43
|
37 void create_enter_dir(struct file_ll* f, pst_item *item)
|
|
38 {
|
|
39 f->email_count = 0;
|
|
40 f->skip_count = 0;
|
|
41 f->type = item->type;
|
|
42 f->stored_count = (item->folder) ? item->folder->email_count : 0;
|
|
43 f->dname = (char*) xmalloc(strlen(item->file_as)+1);
|
|
44 strcpy(f->dname, item->file_as);
|
|
45 }
|
16
|
46
|
|
47
|
43
|
48 void close_enter_dir(struct file_ll *f)
|
|
49 {
|
|
50 free(f->dname);
|
|
51 }
|
16
|
52
|
|
53
|
43
|
54 void process(pst_item *outeritem, pst_desc_ll *d_ptr)
|
|
55 {
|
|
56 struct file_ll ff;
|
|
57 pst_item *item = NULL;
|
|
58
|
|
59 DEBUG_ENT("process");
|
|
60 memset(&ff, 0, sizeof(ff));
|
|
61 create_enter_dir(&ff, outeritem);
|
16
|
62
|
43
|
63 while (d_ptr) {
|
|
64 DEBUG_MAIN(("main: New item record, d_ptr = %p.\n", d_ptr));
|
|
65 if (!d_ptr->desc) {
|
|
66 DEBUG_WARN(("main: ERROR ?? item's desc record is NULL\n"));
|
|
67 ff.skip_count++;
|
|
68 }
|
|
69 else {
|
|
70 DEBUG_MAIN(("main: Desc Email ID %x [d_ptr->id = %x]\n", d_ptr->desc->id, d_ptr->id));
|
16
|
71
|
46
|
72 item = pst_parse_item(&pstfile, d_ptr);
|
43
|
73 DEBUG_MAIN(("main: About to process item @ %p.\n", item));
|
|
74 if (item) {
|
|
75 if (item->message_store) {
|
|
76 // there should only be one message_store, and we have already done it
|
|
77 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
|
|
78 }
|
16
|
79
|
43
|
80 if (item->folder && d_ptr->child) {
|
|
81 // if this is a folder, we want to recurse into it
|
|
82 printf("Folder \"%s\"\n", item->file_as);
|
|
83 process(item, d_ptr->child);
|
16
|
84
|
43
|
85 } else if (item->contact && (item->type == PST_TYPE_CONTACT)) {
|
|
86 // Process Contact item
|
|
87 if (ff.type != PST_TYPE_CONTACT) {
|
|
88 DEBUG_MAIN(("main: I have a contact, but the folder isn't a contacts folder. Processing anyway\n"));
|
|
89 }
|
|
90 printf("Contact");
|
|
91 if (item->contact->fullname != NULL)
|
|
92 printf("\t%s", pst_rfc2426_escape(item->contact->fullname));
|
|
93 printf("\n");
|
16
|
94
|
43
|
95 } else if (item->email && (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_REPORT)) {
|
|
96 // Process Email item
|
|
97 if ((ff.type != PST_TYPE_NOTE) && (ff.type != PST_TYPE_REPORT)) {
|
|
98 DEBUG_MAIN(("main: I have an email, but the folder isn't an email folder. Processing anyway\n"));
|
|
99 }
|
|
100 printf("Email");
|
|
101 if (item->email->outlook_sender_name != NULL)
|
|
102 printf("\tFrom: %s", item->email->outlook_sender_name);
|
|
103 if (item->email->subject->subj != NULL)
|
|
104 printf("\tSubject: %s", item->email->subject->subj);
|
|
105 printf("\n");
|
16
|
106
|
43
|
107 } else if (item->journal && (item->type == PST_TYPE_JOURNAL)) {
|
|
108 // Process Journal item
|
|
109 if (ff.type != PST_TYPE_JOURNAL) {
|
|
110 DEBUG_MAIN(("main: I have a journal entry, but folder isn't specified as a journal type. Processing...\n"));
|
|
111 }
|
|
112 printf("Journal\t%s\n", pst_rfc2426_escape(item->email->subject->subj));
|
16
|
113
|
43
|
114 } else if (item->appointment && (item->type == PST_TYPE_APPOINTMENT)) {
|
|
115 // Process Calendar Appointment item
|
|
116 DEBUG_MAIN(("main: Processing Appointment Entry\n"));
|
|
117 if (ff.type != PST_TYPE_APPOINTMENT) {
|
|
118 DEBUG_MAIN(("main: I have an appointment, but folder isn't specified as an appointment type. Processing...\n"));
|
|
119 }
|
|
120 printf("Appointment");
|
|
121 if (item->email != NULL && item->email->subject != NULL)
|
|
122 printf("\tSUMMARY: %s", pst_rfc2426_escape(item->email->subject->subj));
|
|
123 if (item->appointment != NULL) {
|
|
124 if (item->appointment->start != NULL)
|
|
125 printf("\tSTART: %s", pst_rfc2445_datetime_format(item->appointment->start));
|
|
126 if (item->appointment->end != NULL)
|
|
127 printf("\tEND: %s", pst_rfc2445_datetime_format(item->appointment->end));
|
|
128 printf("\tALL DAY: %s", (item->appointment->all_day==1 ? "Yes" : "No"));
|
|
129 }
|
|
130 printf("\n");
|
16
|
131
|
43
|
132 } else {
|
|
133 ff.skip_count++;
|
|
134 DEBUG_MAIN(("main: Unknown item type. %i. Ascii1=\"%s\"\n",
|
|
135 item->type, item->ascii_type));
|
|
136 }
|
46
|
137 pst_freeItem(item);
|
43
|
138 } else {
|
|
139 ff.skip_count++;
|
|
140 DEBUG_MAIN(("main: A NULL item was seen\n"));
|
|
141 }
|
|
142 d_ptr = d_ptr->next;
|
|
143 }
|
|
144 }
|
|
145 close_enter_dir(&ff);
|
|
146 }
|
16
|
147
|
43
|
148
|
|
149 int main(int argc, char** argv) {
|
|
150 pst_item *item = NULL;
|
|
151 pst_desc_ll *d_ptr;
|
|
152 char *temp = NULL; //temporary char pointer
|
|
153 char *d_log = NULL;
|
16
|
154
|
43
|
155 if (argc <= 1) DIE(("Missing PST filename.\n"));
|
|
156
|
|
157 DEBUG_INIT(d_log);
|
|
158 DEBUG_REGISTER_CLOSE();
|
|
159 DEBUG_ENT("main");
|
|
160
|
|
161 // Open PST file
|
|
162 if (pst_open(&pstfile, argv[1], "r")) DIE(("Error opening File\n"));
|
16
|
163
|
43
|
164 // Load PST index
|
|
165 if (pst_load_index(&pstfile)) DIE(("Index Error\n"));
|
|
166
|
|
167 pst_load_extended_attributes(&pstfile);
|
16
|
168
|
43
|
169 d_ptr = pstfile.d_head; // first record is main record
|
46
|
170 item = pst_parse_item(&pstfile, d_ptr);
|
43
|
171 if (!item || !item->message_store) {
|
|
172 DEBUG_RET();
|
|
173 DIE(("main: Could not get root record\n"));
|
|
174 }
|
16
|
175
|
43
|
176 // default the file_as to the same as the main filename if it doesn't exist
|
|
177 if (!item->file_as) {
|
|
178 if (!(temp = strrchr(argv[1], '/')))
|
|
179 if (!(temp = strrchr(argv[1], '\\')))
|
|
180 temp = argv[1];
|
|
181 else
|
|
182 temp++; // get past the "\\"
|
|
183 else
|
|
184 temp++; // get past the "/"
|
|
185 item->file_as = (char*)xmalloc(strlen(temp)+1);
|
|
186 strcpy(item->file_as, temp);
|
|
187 }
|
|
188 fprintf(stderr, "item->file_as = '%s'.\n", item->file_as);
|
16
|
189
|
43
|
190 d_ptr = pst_getTopOfFolders(&pstfile, item);
|
|
191 if (!d_ptr) DIE(("Top of folders record not found. Cannot continue\n"));
|
|
192 DEBUG_MAIN(("d_ptr(TOF) = %p.\n", d_ptr));
|
16
|
193
|
43
|
194 process(item, d_ptr->child); // do the childred of TOPF
|
46
|
195 pst_freeItem(item);
|
43
|
196 pst_close(&pstfile);
|
16
|
197
|
43
|
198 DEBUG_RET();
|
|
199 return 0;
|
16
|
200 }
|
43
|
201
|
|
202
|
|
203 // This function will make sure that a filename is in cannonical form. That
|
16
|
204 // is, it will replace any slashes, backslashes, or colons with underscores.
|
|
205 void canonicalize_filename(char *fname) {
|
43
|
206 DEBUG_ENT("canonicalize_filename");
|
|
207 if (fname == NULL) {
|
|
208 DEBUG_RET();
|
|
209 return;
|
|
210 }
|
|
211 while ((fname = strpbrk(fname, "/\\:")) != NULL)
|
|
212 *fname = '_';
|
|
213 DEBUG_RET();
|
16
|
214 }
|
43
|
215
|
|
216
|
16
|
217 void debug_print(char *fmt, ...) {
|
43
|
218 // shamlessly stolen from minprintf() in K&R pg. 156
|
|
219 va_list ap;
|
|
220 char *p, *sval;
|
|
221 void *pval;
|
|
222 int ival;
|
|
223 double dval;
|
|
224 FILE *fp = stderr;
|
16
|
225
|
43
|
226 va_start(ap, fmt);
|
|
227 for(p = fmt; *p; p++) {
|
|
228 if (*p != '%') {
|
|
229 fputc(*p, fp);
|
|
230 continue;
|
|
231 }
|
|
232 switch (tolower(*++p)) {
|
|
233 case 'd': case 'i':
|
|
234 ival = va_arg(ap, int);
|
|
235 fprintf(fp, "%d", ival);
|
|
236 break;
|
|
237 case 'f':
|
|
238 dval = va_arg(ap, double);
|
|
239 fprintf(fp, "%f", dval);
|
|
240 break;
|
|
241 case 's':
|
|
242 for (sval = va_arg(ap, char *); *sval; ++sval)
|
|
243 fputc(*sval, fp);
|
|
244 break;
|
|
245 case 'p':
|
|
246 pval = va_arg(ap, void *);
|
|
247 fprintf(fp, "%p", pval);
|
|
248 break;
|
|
249 case 'x':
|
|
250 ival = va_arg(ap, int);
|
|
251 fprintf(fp, "%#010x", ival);
|
|
252 break;
|
|
253 default:
|
|
254 fputc(*p, fp);
|
|
255 break;
|
|
256 }
|
|
257 }
|
|
258 va_end(ap);
|
16
|
259 }
|
|
260
|
|
261
|