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 item_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     pst_convert_utf8(item, &item->file_as);
00032     f->item_count   = 0;
00033     f->skip_count   = 0;
00034     f->type         = item->type;
00035     f->stored_count = (item->folder) ? item->folder->item_count : 0;
00036     f->dname        = strdup(item->file_as.str);
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_tree *d_ptr)
00047 {
00048     struct file_ll ff;
00049     pst_item *item = NULL;
00050     char *result = NULL;
00051     size_t resultlen = 0;
00052 
00053     DEBUG_ENT("process");
00054     memset(&ff, 0, sizeof(ff));
00055     create_enter_dir(&ff, outeritem);
00056 
00057     while (d_ptr) {
00058         if (!d_ptr->desc) {
00059             DEBUG_WARN(("ERROR item's desc record is NULL\n"));
00060             ff.skip_count++;
00061         }
00062         else {
00063             DEBUG_INFO(("Desc Email ID %"PRIx64" [d_ptr->d_id = %"PRIx64"]\n", d_ptr->desc->i_id, d_ptr->d_id));
00064 
00065             item = pst_parse_item(&pstfile, d_ptr, NULL);
00066             DEBUG_INFO(("About to process item @ %p.\n", item));
00067             if (item) {
00068                 if (item->message_store) {
00069                     // there should only be one message_store, and we have already done it
00070                     DIE(("A second message_store has been found. Sorry, this must be an error.\n"));
00071                 }
00072 
00073                 if (item->folder && d_ptr->child) {
00074                     // if this is a folder, we want to recurse into it
00075                     pst_convert_utf8(item, &item->file_as);
00076                     printf("Folder \"%s\"\n", item->file_as.str);
00077                     process(item, d_ptr->child);
00078 
00079                 } else if (item->contact && (item->type == PST_TYPE_CONTACT)) {
00080                     if (!ff.type) ff.type = item->type;
00081                     // Process Contact item
00082                     if (ff.type != PST_TYPE_CONTACT) {
00083                         DEBUG_INFO(("I have a contact, but the folder isn't a contacts folder. Processing anyway\n"));
00084                     }
00085                     printf("Contact");
00086                     if (item->contact->fullname.str)
00087                         printf("\t%s", pst_rfc2426_escape(item->contact->fullname.str, &result, &resultlen));
00088                     printf("\n");
00089 
00090                 } else if (item->email && ((item->type == PST_TYPE_NOTE) || (item->type == PST_TYPE_SCHEDULE) || (item->type == PST_TYPE_REPORT))) {
00091                     if (!ff.type) ff.type = item->type;
00092                     // Process Email item
00093                     if ((ff.type != PST_TYPE_NOTE) && (ff.type != PST_TYPE_SCHEDULE) && (ff.type != PST_TYPE_REPORT)) {
00094                         DEBUG_INFO(("I have an email, but the folder isn't an email folder. Processing anyway\n"));
00095                     }
00096                     printf("Email");
00097                     if (item->email->outlook_sender_name.str)
00098                         printf("\tFrom: %s", item->email->outlook_sender_name.str);
00099                     if (item->subject.str)
00100                         printf("\tSubject: %s", item->subject.str);
00101                     printf("\n");
00102 
00103                 } else if (item->journal && (item->type == PST_TYPE_JOURNAL)) {
00104                     if (!ff.type) ff.type = item->type;
00105                     // Process Journal item
00106                     if (ff.type != PST_TYPE_JOURNAL) {
00107                         DEBUG_INFO(("I have a journal entry, but folder isn't specified as a journal type. Processing...\n"));
00108                     }
00109                     if (item->subject.str)
00110                         printf("Journal\t%s\n", pst_rfc2426_escape(item->subject.str, &result, &resultlen));
00111 
00112                 } else if (item->appointment && (item->type == PST_TYPE_APPOINTMENT)) {
00113                     char time_buffer[30];
00114                     if (!ff.type) ff.type = item->type;
00115                     // Process Calendar Appointment item
00116                     DEBUG_INFO(("Processing Appointment Entry\n"));
00117                     if (ff.type != PST_TYPE_APPOINTMENT) {
00118                         DEBUG_INFO(("I have an appointment, but folder isn't specified as an appointment type. Processing...\n"));
00119                     }
00120                     printf("Appointment");
00121                     if (item->subject.str)
00122                         printf("\tSUMMARY: %s", pst_rfc2426_escape(item->subject.str, &result, &resultlen));
00123                     if (item->appointment->start)
00124                         printf("\tSTART: %s", pst_rfc2445_datetime_format(item->appointment->start, sizeof(time_buffer), time_buffer));
00125                     if (item->appointment->end)
00126                         printf("\tEND: %s", pst_rfc2445_datetime_format(item->appointment->end, sizeof(time_buffer), time_buffer));
00127                     printf("\tALL DAY: %s", (item->appointment->all_day==1 ? "Yes" : "No"));
00128                     printf("\n");
00129 
00130                 } else {
00131                     ff.skip_count++;
00132                     DEBUG_INFO(("Unknown item type. %i. Ascii1=\"%s\"\n",
00133                                       item->type, item->ascii_type));
00134                 }
00135                 pst_freeItem(item);
00136             } else {
00137                 ff.skip_count++;
00138                 DEBUG_INFO(("A NULL item was seen\n"));
00139             }
00140         }
00141         d_ptr = d_ptr->next;
00142     }
00143     close_enter_dir(&ff);
00144     if (result) free(result);
00145     DEBUG_RET();
00146 }
00147 
00148 
00149 void usage(char *prog_name) {
00150     DEBUG_ENT("usage");
00151     version();
00152     printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
00153     printf("OPTIONS:\n");
00154     printf("\t-d <filename> \t- Debug to file. This is a binary log. Use readlog to print it\n");
00155     printf("\t-h\t- Help. This screen\n");
00156     printf("\t-V\t- Version. Display program version\n");
00157     DEBUG_RET();
00158 }
00159 
00160 
00161 void version() {
00162     DEBUG_ENT("version");
00163     printf("lspst / LibPST v%s\n", VERSION);
00164 #if BYTE_ORDER == BIG_ENDIAN
00165     printf("Big Endian implementation being used.\n");
00166 #elif BYTE_ORDER == LITTLE_ENDIAN
00167     printf("Little Endian implementation being used.\n");
00168 #else
00169 #  error "Byte order not supported by this library"
00170 #endif
00171      DEBUG_RET();
00172 }
00173 
00174 
00175 int main(int argc, char* const* argv) {
00176     pst_item *item = NULL;
00177     pst_desc_tree *d_ptr;
00178     char *temp  = NULL; //temporary char pointer
00179     int  c;
00180     char *d_log = NULL;
00181 
00182     while ((c = getopt(argc, argv, "d:hV"))!= -1) {
00183         switch (c) {
00184             case 'd':
00185                 d_log = optarg;
00186                 break;
00187             case 'h':
00188                 usage(argv[0]);
00189                 exit(0);
00190                 break;
00191             case 'V':
00192                 version();
00193                 exit(0);
00194                 break;
00195             default:
00196                 usage(argv[0]);
00197                 exit(1);
00198                 break;
00199         }
00200     }
00201 
00202     #ifdef DEBUG_ALL
00203         // force a log file
00204         if (!d_log) d_log = "lspst.log";
00205     #endif // defined DEBUG_ALL
00206     DEBUG_INIT(d_log, NULL);
00207     DEBUG_ENT("main");
00208 
00209     if (argc <= optind) {
00210         usage(argv[0]);
00211         exit(2);
00212     }
00213 
00214     // Open PST file
00215     if (pst_open(&pstfile, argv[optind], NULL)) DIE(("Error opening File\n"));
00216 
00217     // Load PST index
00218     if (pst_load_index(&pstfile)) DIE(("Index Error\n"));
00219 
00220     pst_load_extended_attributes(&pstfile);
00221 
00222     d_ptr = pstfile.d_head; // first record is main record
00223     item  = pst_parse_item(&pstfile, d_ptr, NULL);
00224     if (!item || !item->message_store) {
00225         DEBUG_RET();
00226         DIE(("Could not get root record\n"));
00227     }
00228 
00229     // default the file_as to the same as the main filename if it doesn't exist
00230     if (!item->file_as.str) {
00231         if (!(temp = strrchr(argv[1], '/')))
00232             if (!(temp = strrchr(argv[1], '\\')))
00233                 temp = argv[1];
00234             else
00235                 temp++; // get past the "\\"
00236         else
00237             temp++; // get past the "/"
00238         item->file_as.str = strdup(temp);
00239         item->file_as.is_utf8 = 1;
00240     }
00241 
00242     d_ptr = pst_getTopOfFolders(&pstfile, item);
00243     if (!d_ptr) DIE(("Top of folders record not found. Cannot continue\n"));
00244 
00245     process(item, d_ptr->child);    // do the childred of TOPF
00246     pst_freeItem(item);
00247     pst_close(&pstfile);
00248 
00249     DEBUG_RET();
00250     return 0;
00251 }
00252 
00253 
00254 // This function will make sure that a filename is in cannonical form.  That
00255 // is, it will replace any slashes, backslashes, or colons with underscores.
00256 void canonicalize_filename(char *fname) {
00257     DEBUG_ENT("canonicalize_filename");
00258     if (fname == NULL) {
00259         DEBUG_RET();
00260         return;
00261     }
00262     while ((fname = strpbrk(fname, "/\\:")))
00263         *fname = '_';
00264     DEBUG_RET();
00265 }
00266 
00267 

Generated on 6 Jul 2016 for 'LibPst' by  doxygen 1.6.1