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
|
43
|
72 item = _pst_parse_item(&pstfile, d_ptr);
|
|
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 }
|
|
137 _pst_freeItem(item);
|
|
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;
|
|
154 struct file_ll *f = NULL, *head = NULL;
|
16
|
155
|
43
|
156 if (argc <= 1) DIE(("Missing PST filename.\n"));
|
|
157
|
|
158 DEBUG_INIT(d_log);
|
|
159 DEBUG_REGISTER_CLOSE();
|
|
160 DEBUG_ENT("main");
|
|
161
|
|
162 // Open PST file
|
|
163 if (pst_open(&pstfile, argv[1], "r")) DIE(("Error opening File\n"));
|
16
|
164
|
43
|
165 // Load PST index
|
|
166 if (pst_load_index(&pstfile)) DIE(("Index Error\n"));
|
|
167
|
|
168 pst_load_extended_attributes(&pstfile);
|
16
|
169
|
43
|
170 d_ptr = pstfile.d_head; // first record is main record
|
|
171 item = _pst_parse_item(&pstfile, d_ptr);
|
|
172 if (!item || !item->message_store) {
|
|
173 DEBUG_RET();
|
|
174 DIE(("main: Could not get root record\n"));
|
|
175 }
|
16
|
176
|
43
|
177 // default the file_as to the same as the main filename if it doesn't exist
|
|
178 if (!item->file_as) {
|
|
179 if (!(temp = strrchr(argv[1], '/')))
|
|
180 if (!(temp = strrchr(argv[1], '\\')))
|
|
181 temp = argv[1];
|
|
182 else
|
|
183 temp++; // get past the "\\"
|
|
184 else
|
|
185 temp++; // get past the "/"
|
|
186 item->file_as = (char*)xmalloc(strlen(temp)+1);
|
|
187 strcpy(item->file_as, temp);
|
|
188 }
|
|
189 fprintf(stderr, "item->file_as = '%s'.\n", item->file_as);
|
16
|
190
|
43
|
191 d_ptr = pst_getTopOfFolders(&pstfile, item);
|
|
192 if (!d_ptr) DIE(("Top of folders record not found. Cannot continue\n"));
|
|
193 DEBUG_MAIN(("d_ptr(TOF) = %p.\n", d_ptr));
|
16
|
194
|
43
|
195 process(item, d_ptr->child); // do the childred of TOPF
|
|
196 _pst_freeItem(item);
|
|
197 pst_close(&pstfile);
|
16
|
198
|
43
|
199 DEBUG_RET();
|
|
200 return 0;
|
16
|
201 }
|
43
|
202
|
|
203
|
|
204 // This function will make sure that a filename is in cannonical form. That
|
16
|
205 // is, it will replace any slashes, backslashes, or colons with underscores.
|
|
206 void canonicalize_filename(char *fname) {
|
43
|
207 DEBUG_ENT("canonicalize_filename");
|
|
208 if (fname == NULL) {
|
|
209 DEBUG_RET();
|
|
210 return;
|
|
211 }
|
|
212 while ((fname = strpbrk(fname, "/\\:")) != NULL)
|
|
213 *fname = '_';
|
|
214 DEBUG_RET();
|
16
|
215 }
|
43
|
216
|
|
217
|
16
|
218 void debug_print(char *fmt, ...) {
|
43
|
219 // shamlessly stolen from minprintf() in K&R pg. 156
|
|
220 va_list ap;
|
|
221 char *p, *sval;
|
|
222 void *pval;
|
|
223 int ival;
|
|
224 double dval;
|
|
225 FILE *fp = stderr;
|
16
|
226
|
43
|
227 va_start(ap, fmt);
|
|
228 for(p = fmt; *p; p++) {
|
|
229 if (*p != '%') {
|
|
230 fputc(*p, fp);
|
|
231 continue;
|
|
232 }
|
|
233 switch (tolower(*++p)) {
|
|
234 case 'd': case 'i':
|
|
235 ival = va_arg(ap, int);
|
|
236 fprintf(fp, "%d", ival);
|
|
237 break;
|
|
238 case 'f':
|
|
239 dval = va_arg(ap, double);
|
|
240 fprintf(fp, "%f", dval);
|
|
241 break;
|
|
242 case 's':
|
|
243 for (sval = va_arg(ap, char *); *sval; ++sval)
|
|
244 fputc(*sval, fp);
|
|
245 break;
|
|
246 case 'p':
|
|
247 pval = va_arg(ap, void *);
|
|
248 fprintf(fp, "%p", pval);
|
|
249 break;
|
|
250 case 'x':
|
|
251 ival = va_arg(ap, int);
|
|
252 fprintf(fp, "%#010x", ival);
|
|
253 break;
|
|
254 default:
|
|
255 fputc(*p, fp);
|
|
256 break;
|
|
257 }
|
|
258 }
|
|
259 va_end(ap);
|
16
|
260 }
|
|
261
|
|
262
|