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