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 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        = (char*) xmalloc(strlen(item->file_as.str)+1);
00037     strcpy(f->dname, item->file_as.str);
00038 }
00039 
00040 
00041 void close_enter_dir(struct file_ll *f)
00042 {
00043     free(f->dname);
00044 }
00045 
00046 
00047 void process(pst_item *outeritem, pst_desc_ll *d_ptr)
00048 {
00049     struct file_ll ff;
00050     pst_item *item = NULL;
00051 
00052     DEBUG_ENT("process");
00053     memset(&ff, 0, sizeof(ff));
00054     create_enter_dir(&ff, outeritem);
00055 
00056     while (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 %"PRIx64" [d_ptr->d_id = %"PRIx64"]\n", d_ptr->desc->i_id, d_ptr->d_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                     pst_convert_utf8(item, &item->file_as);
00075                     printf("Folder \"%s\"\n", item->file_as.str);
00076                     process(item, d_ptr->child);
00077 
00078                 } else if (item->contact && (item->type == PST_TYPE_CONTACT)) {
00079                     // Process Contact item
00080                     if (ff.type != PST_TYPE_CONTACT) {
00081                         DEBUG_MAIN(("main: I have a contact, but the folder isn't a contacts folder. Processing anyway\n"));
00082                     }
00083                     printf("Contact");
00084                     if (item->contact->fullname.str)
00085                         printf("\t%s", pst_rfc2426_escape(item->contact->fullname.str));
00086                     printf("\n");
00087 
00088                 } else if (item->email && (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_REPORT)) {
00089                     // Process Email item
00090                     if ((ff.type != PST_TYPE_NOTE) && (ff.type != PST_TYPE_REPORT)) {
00091                         DEBUG_MAIN(("main: I have an email, but the folder isn't an email folder. Processing anyway\n"));
00092                     }
00093                     printf("Email");
00094                     if (item->email->outlook_sender_name.str)
00095                         printf("\tFrom: %s", item->email->outlook_sender_name.str);
00096                     if (item->subject.str)
00097                         printf("\tSubject: %s", item->subject.str);
00098                     printf("\n");
00099 
00100                 } else if (item->journal && (item->type == PST_TYPE_JOURNAL)) {
00101                     // Process Journal item
00102                     if (ff.type != PST_TYPE_JOURNAL) {
00103                         DEBUG_MAIN(("main: I have a journal entry, but folder isn't specified as a journal type. Processing...\n"));
00104                     }
00105                     if (item->subject.str)
00106                         printf("Journal\t%s\n", pst_rfc2426_escape(item->subject.str));
00107 
00108                 } else if (item->appointment && (item->type == PST_TYPE_APPOINTMENT)) {
00109                     // Process Calendar Appointment item
00110                     DEBUG_MAIN(("main: Processing Appointment Entry\n"));
00111                     if (ff.type != PST_TYPE_APPOINTMENT) {
00112                         DEBUG_MAIN(("main: I have an appointment, but folder isn't specified as an appointment type. Processing...\n"));
00113                     }
00114                     printf("Appointment");
00115                     if (item->subject.str)
00116                         printf("\tSUMMARY: %s", pst_rfc2426_escape(item->subject.str));
00117                     if (item->appointment->start)
00118                         printf("\tSTART: %s", pst_rfc2445_datetime_format(item->appointment->start));
00119                     if (item->appointment->end)
00120                         printf("\tEND: %s", pst_rfc2445_datetime_format(item->appointment->end));
00121                     printf("\tALL DAY: %s", (item->appointment->all_day==1 ? "Yes" : "No"));
00122                     printf("\n");
00123 
00124                 } else {
00125                     ff.skip_count++;
00126                     DEBUG_MAIN(("main: Unknown item type. %i. Ascii1=\"%s\"\n",
00127                                       item->type, item->ascii_type));
00128                 }
00129                 pst_freeItem(item);
00130             } else {
00131                 ff.skip_count++;
00132                 DEBUG_MAIN(("main: A NULL item was seen\n"));
00133             }
00134             d_ptr = d_ptr->next;
00135         }
00136     }
00137     close_enter_dir(&ff);
00138     DEBUG_RET();
00139 }
00140 
00141 
00142 void usage(char *prog_name) {
00143     DEBUG_ENT("usage");
00144     version();
00145     printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
00146     printf("OPTIONS:\n");
00147     printf("\t-d <filename> \t- Debug to file. This is a binary log. Use readlog to print it\n");
00148     printf("\t-h\t- Help. This screen\n");
00149     printf("\t-V\t- Version. Display program version\n");
00150     DEBUG_RET();
00151 }
00152 
00153 
00154 void version() {
00155     DEBUG_ENT("version");
00156     printf("lspst / LibPST v%s\n", VERSION);
00157 #if BYTE_ORDER == BIG_ENDIAN
00158     printf("Big Endian implementation being used.\n");
00159 #elif BYTE_ORDER == LITTLE_ENDIAN
00160     printf("Little Endian implementation being used.\n");
00161 #else
00162 #  error "Byte order not supported by this library"
00163 #endif
00164 #ifdef __GNUC__
00165              printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
00166 #endif
00167      DEBUG_RET();
00168 }
00169 
00170 
00171 int main(int argc, char* const* argv) {
00172     pst_item *item = NULL;
00173     pst_desc_ll *d_ptr;
00174     char *temp  = NULL; //temporary char pointer
00175     int  c;
00176     char *d_log = NULL;
00177 
00178     while ((c = getopt(argc, argv, "d:hV"))!= -1) {
00179         switch (c) {
00180             case 'd':
00181                 d_log = optarg;
00182                 break;
00183             case 'h':
00184                 usage(argv[0]);
00185                 exit(0);
00186                 break;
00187             case 'V':
00188                 version();
00189                 exit(0);
00190                 break;
00191             default:
00192                 usage(argv[0]);
00193                 exit(1);
00194                 break;
00195         }
00196     }
00197 
00198     #ifdef DEBUG_ALL
00199         // force a log file
00200         if (!d_log) d_log = "lspst.log";
00201     #endif // defined DEBUG_ALL
00202     DEBUG_INIT(d_log);
00203     DEBUG_REGISTER_CLOSE();
00204     DEBUG_ENT("main");
00205 
00206     if (argc <= optind) {
00207         usage(argv[0]);
00208         exit(2);
00209     }
00210 
00211     // Open PST file
00212     if (pst_open(&pstfile, argv[optind])) DIE(("Error opening File\n"));
00213 
00214     // Load PST index
00215     if (pst_load_index(&pstfile)) DIE(("Index Error\n"));
00216 
00217     pst_load_extended_attributes(&pstfile);
00218 
00219     d_ptr = pstfile.d_head; // first record is main record
00220     item  = pst_parse_item(&pstfile, d_ptr, NULL);
00221     if (!item || !item->message_store) {
00222         DEBUG_RET();
00223         DIE(("main: Could not get root record\n"));
00224     }
00225 
00226     // default the file_as to the same as the main filename if it doesn't exist
00227     if (!item->file_as.str) {
00228         if (!(temp = strrchr(argv[1], '/')))
00229             if (!(temp = strrchr(argv[1], '\\')))
00230                 temp = argv[1];
00231             else
00232                 temp++; // get past the "\\"
00233         else
00234             temp++; // get past the "/"
00235         item->file_as.str = (char*)xmalloc(strlen(temp)+1);
00236         strcpy(item->file_as.str, temp);
00237         item->file_as.is_utf8 = 1;
00238     }
00239     WARN(("item->file_as = '%s'.\n", item->file_as.str));
00240 
00241     d_ptr = pst_getTopOfFolders(&pstfile, item);
00242     if (!d_ptr) DIE(("Top of folders record not found. Cannot continue\n"));
00243     DEBUG_MAIN(("d_ptr(TOF) = %p.\n", d_ptr));
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 Thu Mar 19 16:39:26 2009 for 'LibPst' by  doxygen 1.3.9.1