Main Page | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

lspst.c

Go to the documentation of this file.
00001 /***
00002  * lspst.c
00003  * Part of the LibPST project
00004  * Author: Joe Nahmias <joe@nahmias.net>
00005  * Based on readpst.c by by David Smith <dave.s@earthcorp.com>
00006  *
00007  */
00008 
00009 #include "define.h"
00010 
00011 struct file_ll {
00012     char *dname;
00013     int32_t stored_count;
00014     int32_t email_count;
00015     int32_t skip_count;
00016     int32_t type;
00017 };
00018 
00019 
00020 void canonicalize_filename(char *fname);
00021 void debug_print(char *fmt, ...);
00022 void usage(char *prog_name);
00023 void version();
00024 
00025 // global settings
00026 pst_file pstfile;
00027 
00028 
00029 void create_enter_dir(struct file_ll* f, pst_item *item)
00030 {
00031     f->email_count  = 0;
00032     f->skip_count   = 0;
00033     f->type         = item->type;
00034     f->stored_count = (item->folder) ? item->folder->email_count : 0;
00035     f->dname        = (char*) xmalloc(strlen(item->file_as)+1);
00036     strcpy(f->dname, item->file_as);
00037 }
00038 
00039 
00040 void close_enter_dir(struct file_ll *f)
00041 {
00042     free(f->dname);
00043 }
00044 
00045 
00046 void process(pst_item *outeritem, pst_desc_ll *d_ptr)
00047 {
00048     struct file_ll ff;
00049     pst_item *item = NULL;
00050 
00051     DEBUG_ENT("process");
00052     memset(&ff, 0, sizeof(ff));
00053     create_enter_dir(&ff, outeritem);
00054 
00055     while (d_ptr) {
00056         DEBUG_MAIN(("main: New item record, d_ptr = %p.\n", d_ptr));
00057         if (!d_ptr->desc) {
00058             DEBUG_WARN(("main: ERROR ?? item's desc record is NULL\n"));
00059             ff.skip_count++;
00060         }
00061         else {
00062             DEBUG_MAIN(("main: Desc Email ID %x [d_ptr->id = %x]\n", d_ptr->desc->id, d_ptr->id));
00063 
00064             item = pst_parse_item(&pstfile, d_ptr, NULL);
00065             DEBUG_MAIN(("main: About to process item @ %p.\n", item));
00066             if (item) {
00067                 if (item->message_store) {
00068                     // there should only be one message_store, and we have already done it
00069                     DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
00070                 }
00071 
00072                 if (item->folder && d_ptr->child) {
00073                     // if this is a folder, we want to recurse into it
00074                     printf("Folder \"%s\"\n", item->file_as);
00075                     process(item, d_ptr->child);
00076 
00077                 } else if (item->contact && (item->type == PST_TYPE_CONTACT)) {
00078                     // Process Contact item
00079                     if (ff.type != PST_TYPE_CONTACT) {
00080                         DEBUG_MAIN(("main: I have a contact, but the folder isn't a contacts folder. Processing anyway\n"));
00081                     }
00082                     printf("Contact");
00083                     if (item->contact->fullname)
00084                         printf("\t%s", pst_rfc2426_escape(item->contact->fullname));
00085                     printf("\n");
00086 
00087                 } else if (item->email && (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_REPORT || item->type == PST_TYPE_OTHER)) {
00088                     // Process Email item
00089                     if ((ff.type != PST_TYPE_NOTE) && (ff.type != PST_TYPE_REPORT) && (ff.type != PST_TYPE_OTHER)) {
00090                         DEBUG_MAIN(("main: I have an email, but the folder isn't an email folder. Processing anyway\n"));
00091                     }
00092                     printf("Email");
00093                     if (item->email->outlook_sender_name)
00094                         printf("\tFrom: %s", item->email->outlook_sender_name);
00095                     if (item->email->subject && item->email->subject->subj)
00096                         printf("\tSubject: %s", item->email->subject->subj);
00097                     printf("\n");
00098 
00099                 } else if (item->journal && (item->type == PST_TYPE_JOURNAL)) {
00100                     // Process Journal item
00101                     if (ff.type != PST_TYPE_JOURNAL) {
00102                         DEBUG_MAIN(("main: I have a journal entry, but folder isn't specified as a journal type. Processing...\n"));
00103                     }
00104                     if (item->email && item->email->subject && item->email->subject->subj)
00105                         printf("Journal\t%s\n", pst_rfc2426_escape(item->email->subject->subj));
00106 
00107                 } else if (item->appointment && (item->type == PST_TYPE_APPOINTMENT)) {
00108                     // Process Calendar Appointment item
00109                     DEBUG_MAIN(("main: Processing Appointment Entry\n"));
00110                     if (ff.type != PST_TYPE_APPOINTMENT) {
00111                         DEBUG_MAIN(("main: I have an appointment, but folder isn't specified as an appointment type. Processing...\n"));
00112                     }
00113                     printf("Appointment");
00114                     if (item->email && item->email->subject)
00115                         printf("\tSUMMARY: %s", pst_rfc2426_escape(item->email->subject->subj));
00116                     if (item->appointment->start)
00117                         printf("\tSTART: %s", pst_rfc2445_datetime_format(item->appointment->start));
00118                     if (item->appointment->end)
00119                         printf("\tEND: %s", pst_rfc2445_datetime_format(item->appointment->end));
00120                     printf("\tALL DAY: %s", (item->appointment->all_day==1 ? "Yes" : "No"));
00121                     printf("\n");
00122 
00123                 } else {
00124                     ff.skip_count++;
00125                     DEBUG_MAIN(("main: Unknown item type. %i. Ascii1=\"%s\"\n",
00126                                       item->type, item->ascii_type));
00127                 }
00128                 pst_freeItem(item);
00129             } else {
00130                 ff.skip_count++;
00131                 DEBUG_MAIN(("main: A NULL item was seen\n"));
00132             }
00133             d_ptr = d_ptr->next;
00134         }
00135     }
00136     close_enter_dir(&ff);
00137     DEBUG_RET();
00138 }
00139 
00140 
00141 void usage(char *prog_name) {
00142     DEBUG_ENT("usage");
00143     version();
00144     printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
00145     printf("OPTIONS:\n");
00146     printf("\t-d <filename> \t- Debug to file. This is a binary log. Use readlog to print it\n");
00147     printf("\t-h\t- Help. This screen\n");
00148     printf("\t-V\t- Version. Display program version\n");
00149     DEBUG_RET();
00150 }
00151 
00152 
00153 void version() {
00154     DEBUG_ENT("version");
00155     printf("lspst / LibPST v%s\n", VERSION);
00156 #if BYTE_ORDER == BIG_ENDIAN
00157     printf("Big Endian implementation being used.\n");
00158 #elif BYTE_ORDER == LITTLE_ENDIAN
00159     printf("Little Endian implementation being used.\n");
00160 #else
00161 #  error "Byte order not supported by this library"
00162 #endif
00163 #ifdef __GNUC__
00164              printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
00165 #endif
00166      DEBUG_RET();
00167 }
00168 
00169 
00170 int main(int argc, char* const* argv) {
00171     pst_item *item = NULL;
00172     pst_desc_ll *d_ptr;
00173     char *temp  = NULL; //temporary char pointer
00174     int  c;
00175     char *d_log = NULL;
00176 
00177     while ((c = getopt(argc, argv, "d:hV"))!= -1) {
00178         switch (c) {
00179             case 'd':
00180                 d_log = optarg;
00181                 break;
00182             case 'h':
00183                 usage(argv[0]);
00184                 exit(0);
00185                 break;
00186             case 'V':
00187                 version();
00188                 exit(0);
00189                 break;
00190             default:
00191                 usage(argv[0]);
00192                 exit(1);
00193                 break;
00194         }
00195     }
00196 
00197     #ifdef DEBUG_ALL
00198         // force a log file
00199         if (!d_log) d_log = "lspst.log";
00200     #endif // defined DEBUG_ALL
00201     DEBUG_INIT(d_log);
00202     DEBUG_REGISTER_CLOSE();
00203     DEBUG_ENT("main");
00204 
00205     if (argc <= optind) {
00206         usage(argv[0]);
00207         exit(2);
00208     }
00209 
00210     // Open PST file
00211     if (pst_open(&pstfile, argv[optind])) DIE(("Error opening File\n"));
00212 
00213     // Load PST index
00214     if (pst_load_index(&pstfile)) DIE(("Index Error\n"));
00215 
00216     pst_load_extended_attributes(&pstfile);
00217 
00218     d_ptr = pstfile.d_head; // first record is main record
00219     item  = pst_parse_item(&pstfile, d_ptr, NULL);
00220     if (!item || !item->message_store) {
00221         DEBUG_RET();
00222         DIE(("main: Could not get root record\n"));
00223     }
00224 
00225     // default the file_as to the same as the main filename if it doesn't exist
00226     if (!item->file_as) {
00227         if (!(temp = strrchr(argv[1], '/')))
00228             if (!(temp = strrchr(argv[1], '\\')))
00229                 temp = argv[1];
00230             else
00231                 temp++; // get past the "\\"
00232         else
00233             temp++; // get past the "/"
00234         item->file_as = (char*)xmalloc(strlen(temp)+1);
00235         strcpy(item->file_as, temp);
00236     }
00237     fprintf(stderr, "item->file_as = '%s'.\n", item->file_as);
00238 
00239     d_ptr = pst_getTopOfFolders(&pstfile, item);
00240     if (!d_ptr) DIE(("Top of folders record not found. Cannot continue\n"));
00241     DEBUG_MAIN(("d_ptr(TOF) = %p.\n", d_ptr));
00242 
00243     process(item, d_ptr->child);    // do the childred of TOPF
00244     pst_freeItem(item);
00245     pst_close(&pstfile);
00246 
00247     DEBUG_RET();
00248     return 0;
00249 }
00250 
00251 
00252 // This function will make sure that a filename is in cannonical form.  That
00253 // is, it will replace any slashes, backslashes, or colons with underscores.
00254 void canonicalize_filename(char *fname) {
00255     DEBUG_ENT("canonicalize_filename");
00256     if (fname == NULL) {
00257         DEBUG_RET();
00258         return;
00259     }
00260     while ((fname = strpbrk(fname, "/\\:")))
00261         *fname = '_';
00262     DEBUG_RET();
00263 }
00264 
00265 
00266 void debug_print(char *fmt, ...) {
00267     // shamlessly stolen from minprintf() in K&R pg. 156
00268     va_list ap;
00269     char *p, *sval;
00270     void *pval;
00271     int ival;
00272     double dval;
00273     FILE *fp = stderr;
00274 
00275     va_start(ap, fmt);
00276     for(p = fmt; *p; p++) {
00277         if (*p != '%') {
00278             fputc(*p, fp);
00279             continue;
00280         }
00281         switch (tolower(*++p)) {
00282             case 'd': case 'i':
00283                 ival = va_arg(ap, int);
00284                 fprintf(fp, "%d", ival);
00285                 break;
00286             case 'f':
00287                 dval = va_arg(ap, double);
00288                 fprintf(fp, "%f", dval);
00289                 break;
00290             case 's':
00291                 for (sval = va_arg(ap, char *); *sval; ++sval)
00292                     fputc(*sval, fp);
00293                 break;
00294             case 'p':
00295                 pval = va_arg(ap, void *);
00296                 fprintf(fp, "%p", pval);
00297                 break;
00298             case 'x':
00299                 ival = va_arg(ap, int);
00300                 fprintf(fp, "%#010x", ival);
00301                 break;
00302             default:
00303                 fputc(*p, fp);
00304                 break;
00305         }
00306     }
00307     va_end(ap);
00308 }
00309 
00310 

Generated on Thu Feb 26 13:40:07 2009 for 'LibPst' by  doxygen 1.3.9.1