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

Generated on Thu Dec 11 11:58:49 2008 for 'LibPst' by  doxygen 1.3.9.1