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
|
48
|
9 #include "define.h"
|
|
10
|
16
|
11 #include <stdio.h>
|
|
12 #include <stdlib.h>
|
|
13 #include <time.h>
|
|
14 #include <string.h>
|
|
15 #include <ctype.h>
|
|
16 #include <errno.h>
|
50
|
17 #include <unistd.h>
|
16
|
18
|
|
19 #include "libpst.h"
|
|
20 #include "timeconv.h"
|
43
|
21
|
16
|
22 struct file_ll {
|
43
|
23 char *dname;
|
|
24 int32_t stored_count;
|
|
25 int32_t email_count;
|
|
26 int32_t skip_count;
|
|
27 int32_t type;
|
16
|
28 };
|
43
|
29
|
|
30
|
16
|
31 void canonicalize_filename(char *fname);
|
|
32 void debug_print(char *fmt, ...);
|
50
|
33 int usage(char *prog_name);
|
|
34 int version();
|
43
|
35
|
|
36 // global settings
|
|
37 pst_file pstfile;
|
|
38
|
16
|
39
|
43
|
40 void create_enter_dir(struct file_ll* f, pst_item *item)
|
|
41 {
|
|
42 f->email_count = 0;
|
|
43 f->skip_count = 0;
|
|
44 f->type = item->type;
|
|
45 f->stored_count = (item->folder) ? item->folder->email_count : 0;
|
|
46 f->dname = (char*) xmalloc(strlen(item->file_as)+1);
|
|
47 strcpy(f->dname, item->file_as);
|
|
48 }
|
16
|
49
|
|
50
|
43
|
51 void close_enter_dir(struct file_ll *f)
|
|
52 {
|
|
53 free(f->dname);
|
|
54 }
|
16
|
55
|
|
56
|
43
|
57 void process(pst_item *outeritem, pst_desc_ll *d_ptr)
|
|
58 {
|
|
59 struct file_ll ff;
|
|
60 pst_item *item = NULL;
|
|
61
|
|
62 DEBUG_ENT("process");
|
|
63 memset(&ff, 0, sizeof(ff));
|
|
64 create_enter_dir(&ff, outeritem);
|
16
|
65
|
43
|
66 while (d_ptr) {
|
|
67 DEBUG_MAIN(("main: New item record, d_ptr = %p.\n", d_ptr));
|
|
68 if (!d_ptr->desc) {
|
|
69 DEBUG_WARN(("main: ERROR ?? item's desc record is NULL\n"));
|
|
70 ff.skip_count++;
|
|
71 }
|
|
72 else {
|
|
73 DEBUG_MAIN(("main: Desc Email ID %x [d_ptr->id = %x]\n", d_ptr->desc->id, d_ptr->id));
|
16
|
74
|
46
|
75 item = pst_parse_item(&pstfile, d_ptr);
|
43
|
76 DEBUG_MAIN(("main: About to process item @ %p.\n", item));
|
|
77 if (item) {
|
|
78 if (item->message_store) {
|
|
79 // there should only be one message_store, and we have already done it
|
|
80 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
|
|
81 }
|
16
|
82
|
43
|
83 if (item->folder && d_ptr->child) {
|
|
84 // if this is a folder, we want to recurse into it
|
|
85 printf("Folder \"%s\"\n", item->file_as);
|
|
86 process(item, d_ptr->child);
|
16
|
87
|
43
|
88 } else if (item->contact && (item->type == PST_TYPE_CONTACT)) {
|
|
89 // Process Contact item
|
|
90 if (ff.type != PST_TYPE_CONTACT) {
|
|
91 DEBUG_MAIN(("main: I have a contact, but the folder isn't a contacts folder. Processing anyway\n"));
|
|
92 }
|
|
93 printf("Contact");
|
50
|
94 if (item->contact->fullname)
|
43
|
95 printf("\t%s", pst_rfc2426_escape(item->contact->fullname));
|
|
96 printf("\n");
|
16
|
97
|
43
|
98 } else if (item->email && (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_REPORT)) {
|
|
99 // Process Email item
|
|
100 if ((ff.type != PST_TYPE_NOTE) && (ff.type != PST_TYPE_REPORT)) {
|
|
101 DEBUG_MAIN(("main: I have an email, but the folder isn't an email folder. Processing anyway\n"));
|
|
102 }
|
|
103 printf("Email");
|
50
|
104 if (item->email->outlook_sender_name)
|
43
|
105 printf("\tFrom: %s", item->email->outlook_sender_name);
|
50
|
106 if (item->email->subject && item->email->subject->subj)
|
43
|
107 printf("\tSubject: %s", item->email->subject->subj);
|
|
108 printf("\n");
|
16
|
109
|
43
|
110 } else if (item->journal && (item->type == PST_TYPE_JOURNAL)) {
|
|
111 // Process Journal item
|
|
112 if (ff.type != PST_TYPE_JOURNAL) {
|
|
113 DEBUG_MAIN(("main: I have a journal entry, but folder isn't specified as a journal type. Processing...\n"));
|
|
114 }
|
50
|
115 if (item->email && item->email->subject && item->email->subject->subj)
|
|
116 printf("Journal\t%s\n", pst_rfc2426_escape(item->email->subject->subj));
|
16
|
117
|
43
|
118 } else if (item->appointment && (item->type == PST_TYPE_APPOINTMENT)) {
|
|
119 // Process Calendar Appointment item
|
|
120 DEBUG_MAIN(("main: Processing Appointment Entry\n"));
|
|
121 if (ff.type != PST_TYPE_APPOINTMENT) {
|
|
122 DEBUG_MAIN(("main: I have an appointment, but folder isn't specified as an appointment type. Processing...\n"));
|
|
123 }
|
|
124 printf("Appointment");
|
50
|
125 if (item->email && item->email->subject)
|
43
|
126 printf("\tSUMMARY: %s", pst_rfc2426_escape(item->email->subject->subj));
|
50
|
127 if (item->appointment->start)
|
|
128 printf("\tSTART: %s", pst_rfc2445_datetime_format(item->appointment->start));
|
|
129 if (item->appointment->end)
|
|
130 printf("\tEND: %s", pst_rfc2445_datetime_format(item->appointment->end));
|
|
131 printf("\tALL DAY: %s", (item->appointment->all_day==1 ? "Yes" : "No"));
|
43
|
132 printf("\n");
|
16
|
133
|
43
|
134 } else {
|
|
135 ff.skip_count++;
|
|
136 DEBUG_MAIN(("main: Unknown item type. %i. Ascii1=\"%s\"\n",
|
|
137 item->type, item->ascii_type));
|
|
138 }
|
46
|
139 pst_freeItem(item);
|
43
|
140 } else {
|
|
141 ff.skip_count++;
|
|
142 DEBUG_MAIN(("main: A NULL item was seen\n"));
|
|
143 }
|
|
144 d_ptr = d_ptr->next;
|
|
145 }
|
|
146 }
|
|
147 close_enter_dir(&ff);
|
|
148 }
|
16
|
149
|
43
|
150
|
50
|
151 int usage(char *prog_name) {
|
|
152 DEBUG_ENT("usage");
|
|
153 version();
|
|
154 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
|
|
155 printf("OPTIONS:\n");
|
|
156 printf("\t-d <filename> \t- Debug to file. This is a binary log. Use readlog to print it\n");
|
|
157 printf("\t-h\t- Help. This screen\n");
|
|
158 printf("\t-V\t- Version. Display program version\n");
|
|
159 DEBUG_RET();
|
|
160 return 0;
|
|
161 }
|
|
162
|
|
163
|
|
164 int version() {
|
|
165 DEBUG_ENT("version");
|
|
166 printf("lspst / LibPST v%s\n", VERSION);
|
|
167 #if BYTE_ORDER == BIG_ENDIAN
|
|
168 printf("Big Endian implementation being used.\n");
|
|
169 #elif BYTE_ORDER == LITTLE_ENDIAN
|
|
170 printf("Little Endian implementation being used.\n");
|
|
171 #else
|
|
172 # error "Byte order not supported by this library"
|
|
173 #endif
|
|
174 #ifdef __GNUC__
|
|
175 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
|
|
176 #endif
|
|
177 DEBUG_RET();
|
|
178 return 0;
|
|
179 }
|
|
180
|
|
181
|
43
|
182 int main(int argc, char** argv) {
|
|
183 pst_item *item = NULL;
|
|
184 pst_desc_ll *d_ptr;
|
|
185 char *temp = NULL; //temporary char pointer
|
50
|
186 int c;
|
43
|
187 char *d_log = NULL;
|
16
|
188
|
50
|
189 while ((c = getopt(argc, argv, "d:hV"))!= -1) {
|
|
190 switch (c) {
|
|
191 case 'd':
|
|
192 d_log = optarg;
|
|
193 break;
|
|
194 case 'h':
|
|
195 usage(argv[0]);
|
|
196 exit(0);
|
|
197 break;
|
|
198 case 'V':
|
|
199 version();
|
|
200 exit(0);
|
|
201 break;
|
|
202 default:
|
|
203 usage(argv[0]);
|
|
204 exit(1);
|
|
205 break;
|
|
206 }
|
|
207 }
|
43
|
208
|
48
|
209 #ifdef DEBUG_ALL
|
|
210 // force a log file
|
|
211 if (!d_log) d_log = "lspst.log";
|
|
212 #endif // defined DEBUG_ALL
|
43
|
213 DEBUG_INIT(d_log);
|
|
214 DEBUG_REGISTER_CLOSE();
|
|
215 DEBUG_ENT("main");
|
|
216
|
50
|
217 if (argc <= optind) {
|
|
218 usage(argv[0]);
|
|
219 exit(2);
|
|
220 }
|
|
221
|
43
|
222 // Open PST file
|
50
|
223 if (pst_open(&pstfile, argv[optind], "r")) DIE(("Error opening File\n"));
|
16
|
224
|
43
|
225 // Load PST index
|
|
226 if (pst_load_index(&pstfile)) DIE(("Index Error\n"));
|
|
227
|
|
228 pst_load_extended_attributes(&pstfile);
|
16
|
229
|
43
|
230 d_ptr = pstfile.d_head; // first record is main record
|
46
|
231 item = pst_parse_item(&pstfile, d_ptr);
|
43
|
232 if (!item || !item->message_store) {
|
|
233 DEBUG_RET();
|
|
234 DIE(("main: Could not get root record\n"));
|
|
235 }
|
16
|
236
|
43
|
237 // default the file_as to the same as the main filename if it doesn't exist
|
|
238 if (!item->file_as) {
|
|
239 if (!(temp = strrchr(argv[1], '/')))
|
|
240 if (!(temp = strrchr(argv[1], '\\')))
|
|
241 temp = argv[1];
|
|
242 else
|
|
243 temp++; // get past the "\\"
|
|
244 else
|
|
245 temp++; // get past the "/"
|
|
246 item->file_as = (char*)xmalloc(strlen(temp)+1);
|
|
247 strcpy(item->file_as, temp);
|
|
248 }
|
|
249 fprintf(stderr, "item->file_as = '%s'.\n", item->file_as);
|
16
|
250
|
43
|
251 d_ptr = pst_getTopOfFolders(&pstfile, item);
|
|
252 if (!d_ptr) DIE(("Top of folders record not found. Cannot continue\n"));
|
|
253 DEBUG_MAIN(("d_ptr(TOF) = %p.\n", d_ptr));
|
16
|
254
|
43
|
255 process(item, d_ptr->child); // do the childred of TOPF
|
46
|
256 pst_freeItem(item);
|
43
|
257 pst_close(&pstfile);
|
16
|
258
|
43
|
259 DEBUG_RET();
|
|
260 return 0;
|
16
|
261 }
|
43
|
262
|
|
263
|
|
264 // This function will make sure that a filename is in cannonical form. That
|
16
|
265 // is, it will replace any slashes, backslashes, or colons with underscores.
|
|
266 void canonicalize_filename(char *fname) {
|
43
|
267 DEBUG_ENT("canonicalize_filename");
|
|
268 if (fname == NULL) {
|
|
269 DEBUG_RET();
|
|
270 return;
|
|
271 }
|
50
|
272 while (fname = strpbrk(fname, "/\\:"))
|
43
|
273 *fname = '_';
|
|
274 DEBUG_RET();
|
16
|
275 }
|
43
|
276
|
|
277
|
16
|
278 void debug_print(char *fmt, ...) {
|
43
|
279 // shamlessly stolen from minprintf() in K&R pg. 156
|
|
280 va_list ap;
|
|
281 char *p, *sval;
|
|
282 void *pval;
|
|
283 int ival;
|
|
284 double dval;
|
|
285 FILE *fp = stderr;
|
16
|
286
|
43
|
287 va_start(ap, fmt);
|
|
288 for(p = fmt; *p; p++) {
|
|
289 if (*p != '%') {
|
|
290 fputc(*p, fp);
|
|
291 continue;
|
|
292 }
|
|
293 switch (tolower(*++p)) {
|
|
294 case 'd': case 'i':
|
|
295 ival = va_arg(ap, int);
|
|
296 fprintf(fp, "%d", ival);
|
|
297 break;
|
|
298 case 'f':
|
|
299 dval = va_arg(ap, double);
|
|
300 fprintf(fp, "%f", dval);
|
|
301 break;
|
|
302 case 's':
|
|
303 for (sval = va_arg(ap, char *); *sval; ++sval)
|
|
304 fputc(*sval, fp);
|
|
305 break;
|
|
306 case 'p':
|
|
307 pval = va_arg(ap, void *);
|
|
308 fprintf(fp, "%p", pval);
|
|
309 break;
|
|
310 case 'x':
|
|
311 ival = va_arg(ap, int);
|
|
312 fprintf(fp, "%#010x", ival);
|
|
313 break;
|
|
314 default:
|
|
315 fputc(*p, fp);
|
|
316 break;
|
|
317 }
|
|
318 }
|
|
319 va_end(ap);
|
16
|
320 }
|
|
321
|
|
322
|