btool.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       btool.cc
00003 ///             Barry library tester
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 //
00023 // This define is used in barry/barry.h to signal inclusion of Boost
00024 // serialization headers.  It is intended to be used by applications,
00025 // so we shouldn't mess with it.
00026 //
00027 // But all actual Boost related code is now stuffed into util.cc, safely
00028 // locked away from other code.  So we don't need the Boost headers, we
00029 // just need a flag for our own functionality.  So translate this define
00030 // into our own, and undef to skip the Boost headers, and the compile speed
00031 // slowdown that it creates.
00032 //
00033 #ifdef __BARRY_BOOST_MODE__
00034 #define __BTOOL_BOOST_MODE__
00035 #endif
00036 #undef __BARRY_BOOST_MODE__
00037 
00038 #include <barry/barry.h>
00039 #ifdef __BARRY_SYNC_MODE__
00040 #include <barry/barrysync.h>
00041 #endif
00042 #ifdef __BARRY_BACKUP_MODE__
00043 #include <barry/barrybackup.h>
00044 #endif
00045 
00046 #include <iomanip>
00047 #include <iostream>
00048 #include <fstream>
00049 #include <sstream>
00050 #include <vector>
00051 #include <string>
00052 #include <algorithm>
00053 #include <stdlib.h>
00054 #include <tr1/memory>
00055 #include "i18n.h"
00056 #include "util.h"
00057 #include "boostwrap.h"
00058 
00059 #include "barrygetopt.h"
00060 
00061 using namespace std;
00062 using namespace std::tr1;
00063 using namespace Barry;
00064 
00065 std::map<std::string, std::string> SortKeys;
00066 
00067 void Usage()
00068 {
00069    int logical, major, minor;
00070    const char *Version = Barry::Version(logical, major, minor);
00071 
00072    cerr
00073    << "btool - Command line USB Blackberry Test Tool\n"
00074    << "        Copyright 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
00075    << "        Using: " << Version << "\n"
00076    << "        Compiled "
00077 #ifdef __BTOOL_BOOST_MODE__
00078    << "with"
00079 #else
00080    << "without"
00081 #endif
00082    << " Boost support\n"
00083    << "\n"
00084    << "   -b file   Filename to save or load a Barry Backup to (tar.gz)\n"
00085    << "   -B bus    Specify which USB bus to search on\n"
00086    << "   -N dev    Specify which system device, using system specific string\n"
00087    << "\n"
00088    << "   -a db     Erase / clear database 'db' FROM device, deleting all\n"
00089    << "             its records.  Can be used multiple times to clear more\n"
00090    << "             than one DB.\n"
00091    << "   -c dn     Convert address book database to LDIF format, using the\n"
00092    << "             specified baseDN\n"
00093    << "   -C dnattr LDIF attribute name to use when building the FQDN\n"
00094    << "             Defaults to 'cn'\n"
00095    << "   -d db     Load database 'db' FROM device and dump to screen\n"
00096    << "             Can be used multiple times to fetch more than one DB\n"
00097    << "   -e epp    Override endpoint pair detection.  'epp' is a single\n"
00098    << "             string separated by a comma, holding the read,write\n"
00099    << "             endpoint pair.  Example: -e 83,5\n"
00100    << "             Note: Endpoints are specified in hex.\n"
00101    << "             You should never need to use this option.\n"
00102 #ifdef __BTOOL_BOOST_MODE__
00103    << "   -f file   Filename to save or load handheld data to/from\n"
00104 #endif
00105    << "   -F sort   Field name by which to sort the output.  Note that the\n"
00106    << "             format of this field is special: 'DBName:field1,field2'\n"
00107    << "             with no spaces unless the spaces are part of the name.\n"
00108    << "             Can be used multiple times, to match your -d options.\n"
00109    << "             Example: -F 'Address Book:Company,LastName,FirstName'\n"
00110    << "   -h        This help\n"
00111    << "   -i cs     International charset for string conversions\n"
00112    << "             Valid values here are available with 'iconv --list'\n"
00113    << "   -I        Sort records before output\n"
00114    << "   -l        List devices\n"
00115    << "   -L        List Contact field names\n"
00116    << "   -m        Map LDIF name to Contact field / Unmap LDIF name\n"
00117    << "                Map: ldif,read,write - maps ldif to read/write Contact fields\n"
00118    << "                Unmap: ldif name alone\n"
00119    << "   -M        List current LDIF mapping\n"
00120    << "   -n        Use null parser on all databases.\n"
00121    << "   -p pin    PIN of device to talk with\n"
00122    << "             If only one device is plugged in, this flag is optional\n"
00123    << "   -P pass   Simplistic method to specify device password\n"
00124    << "   -s db     Save database 'db' TO device from data loaded from -f file\n"
00125    << "   -S        Show list of supported database parsers.  Use twice to\n"
00126    << "             display fields names as well.\n"
00127    << "   -t        Show database database table\n"
00128    << "   -T db     Show record state table for given database\n"
00129    << "   -v        Dump protocol data during operation\n"
00130 #ifdef __BARRY_SYNC_MODE__
00131    << "   -V        Dump records using MIME vformats where possible\n"
00132 #endif
00133    << "   -X        Reset device\n"
00134    << "   -z        Use non-threaded sockets\n"
00135    << "   -Z        Use threaded socket router (default)\n"
00136    << "\n"
00137    << " -d Command modifiers:   (can be used multiple times for more than 1 record)\n"
00138    << "\n"
00139    << "   -r #      Record index number as seen in the -T state table.\n"
00140    << "             This overrides the default -d behaviour, and only\n"
00141    << "             downloads the one specified record, sending to stdout.\n"
00142    << "   -R #      Same as -r, but also clears the record's dirty flags.\n"
00143    << "   -D #      Record index number as seen in the -T state table,\n"
00144    << "             which indicates the record to delete.  Used with the -d\n"
00145    << "             command to specify the database.\n"
00146    << endl;
00147 }
00148 
00149 class Contact2Ldif
00150 {
00151 public:
00152         Barry::ContactLdif &ldif;
00153 
00154         Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
00155 
00156         void operator()(const Contact &rec)
00157         {
00158                 ldif.DumpLdif(cout, rec);
00159         }
00160 };
00161 
00162 template <class Record>
00163 struct Store
00164 {
00165         std::vector<Record> records;
00166         mutable typename std::vector<Record>::const_iterator rec_it;
00167         std::string filename;
00168         bool load;
00169         bool immediate_display;
00170         bool vformat_mode;
00171         int from_device_count;
00172         mutable int to_device_count;
00173 
00174         Store(const string &filename, bool load, bool immediate_display,
00175                         bool vformat_mode)
00176                 : rec_it(records.end()),
00177                 filename(filename),
00178                 load(load),
00179                 immediate_display(immediate_display && !SortKeys.size()),
00180                 vformat_mode(vformat_mode),
00181                 from_device_count(0),
00182                 to_device_count(0)
00183         {
00184 #ifdef __BTOOL_BOOST_MODE__
00185                 if( load && filename.size() ) {
00186                         // filename is available, attempt to load
00187                         cout << "Loading: " << filename << endl;
00188                         string errmsg, dbName;
00189                         if( !LoadBoostFile(filename, records, dbName, errmsg) ) {
00190                                 cerr << errmsg << endl;
00191                         }
00192                         cout << records.size()
00193                              << " records loaded from '"
00194                              << filename << "'" << endl;
00195                         sort(records.begin(), records.end());
00196                         rec_it = records.begin();
00197 
00198                         // debugging aid
00199                         typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
00200                         for( ; beg != end; beg++ ) {
00201                                 cout << (*beg) << endl;
00202                         }
00203                 }
00204 #endif
00205         }
00206 
00207         ~Store()
00208         {
00209                 if( !immediate_display ) {
00210                         // not dumped yet, sort then dump
00211                         if( SortKeys.size() && SortKeys.find(Record::GetDBName()) != SortKeys.end() ) {
00212                                 sort(records.begin(), records.end(),
00213                                         NamedFieldCmp<Record>(SortKeys[Record::GetDBName()]));
00214                         }
00215                         else {
00216                                 sort(records.begin(), records.end());
00217                         }
00218                         DumpAll();
00219                 }
00220 
00221                 cout << "Store counted " << dec << from_device_count << " records read from device, and " << dec << to_device_count << " records written to device." << endl;
00222 #ifdef __BTOOL_BOOST_MODE__
00223                 if( !load && filename.size() ) {
00224                         // filename is available, attempt to save
00225                         cout << "Saving: " << filename << endl;
00226                         string errmsg;
00227                         if( !SaveBoostFile(filename, records, errmsg) ) {
00228                                 cerr << errmsg << endl;
00229                         }
00230                         cout << dec << records.size() << " records saved to '"
00231                                 << filename << "'" << endl;
00232                 }
00233 #endif
00234         }
00235 
00236         void DumpAll()
00237         {
00238                 typename vector<Record>::const_iterator i = records.begin();
00239                 for( ; i != records.end(); ++i ) {
00240                         Dump(*i);
00241                 }
00242         }
00243 
00244         void Dump(const Record &rec)
00245         {
00246                 if( vformat_mode ) {
00247 #ifdef __BARRY_SYNC_MODE__
00248                         MimeDump<Record> md;
00249                         md.Dump(cout, rec);
00250 #endif
00251                 }
00252                 else {
00253                         cout << rec << endl;
00254                 }
00255         }
00256 
00257         // storage operator
00258         void operator()(const Record &rec)
00259         {
00260                 from_device_count++;
00261                 if( immediate_display )
00262                         Dump(rec);
00263                 records.push_back(rec);
00264         }
00265 
00266         // retrieval operator
00267         bool operator()(Record &rec, Builder &builder) const
00268         {
00269                 if( rec_it == records.end() )
00270                         return false;
00271                 to_device_count++;
00272                 rec = *rec_it;
00273                 rec_it++;
00274                 return true;
00275         }
00276 };
00277 
00278 shared_ptr<Parser> GetParser(const string &name,
00279                         const string &filename,
00280                         bool null_parser,
00281                         bool immediate_display,
00282                         bool vformat_mode,
00283                         bool bbackup_mode)
00284 {
00285         bool dnow = immediate_display;
00286         bool vmode = vformat_mode;
00287 
00288         if( null_parser ) {
00289                 // use null parser
00290                 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
00291         }
00292         else if( bbackup_mode ) {
00293 #ifdef __BARRY_BACKUP_MODE__
00294                 // Only one backup file per run
00295                 static shared_ptr<Parser> backup;
00296                 if( !backup.get() ) {
00297                         backup.reset( new Backup(filename) );
00298                 }
00299                 return backup;
00300 #else
00301                 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
00302 #endif
00303         }
00304         // check for recognized database names
00305         else if( name == Contact::GetDBName() ) {
00306                 return shared_ptr<Parser>(
00307                         new RecordParser<Contact, Store<Contact> > (
00308                                 new Store<Contact>(filename, false, dnow, vmode)));
00309         }
00310         else if( name == Message::GetDBName() ) {
00311                 return shared_ptr<Parser>(
00312                         new RecordParser<Message, Store<Message> > (
00313                                 new Store<Message>(filename, false, dnow, vmode)));
00314         }
00315         else if( name == Calendar::GetDBName() ) {
00316                 return shared_ptr<Parser>(
00317                         new RecordParser<Calendar, Store<Calendar> > (
00318                                 new Store<Calendar>(filename, false, dnow, vmode)));
00319         }
00320         else if( name == CalendarAll::GetDBName() ) {
00321                 return shared_ptr<Parser>(
00322                         new RecordParser<CalendarAll, Store<CalendarAll> > (
00323                                 new Store<CalendarAll>(filename, false, dnow, vmode)));
00324         }
00325         else if( name == CallLog::GetDBName() ) {
00326                 return shared_ptr<Parser>(
00327                         new RecordParser<CallLog, Store<CallLog> > (
00328                                 new Store<CallLog>(filename, false, dnow, vmode)));
00329         }
00330         else if( name == Bookmark::GetDBName() ) {
00331                 return shared_ptr<Parser>(
00332                         new RecordParser<Bookmark, Store<Bookmark> > (
00333                                 new Store<Bookmark>(filename, false, dnow, vmode)));
00334         }
00335         else if( name == ServiceBook::GetDBName() ) {
00336                 return shared_ptr<Parser>(
00337                         new RecordParser<ServiceBook, Store<ServiceBook> > (
00338                                 new Store<ServiceBook>(filename, false, dnow, vmode)));
00339         }
00340 
00341         else if( name == Memo::GetDBName() ) {
00342                 return shared_ptr<Parser>(
00343                         new RecordParser<Memo, Store<Memo> > (
00344                                 new Store<Memo>(filename, false, dnow, vmode)));
00345         }
00346         else if( name == Task::GetDBName() ) {
00347                 return shared_ptr<Parser>(
00348                         new RecordParser<Task, Store<Task> > (
00349                                 new Store<Task>(filename, false, dnow, vmode)));
00350         }
00351         else if( name == PINMessage::GetDBName() ) {
00352                 return shared_ptr<Parser>(
00353                         new RecordParser<PINMessage, Store<PINMessage> > (
00354                                 new Store<PINMessage>(filename, false, dnow, vmode)));
00355         }
00356         else if( name == SavedMessage::GetDBName() ) {
00357                 return shared_ptr<Parser>(
00358                         new RecordParser<SavedMessage, Store<SavedMessage> > (
00359                                 new Store<SavedMessage>(filename, false, dnow, vmode)));
00360         }
00361         else if( name == Sms::GetDBName() ) {
00362                 return shared_ptr<Parser>(
00363                         new RecordParser<Sms, Store<Sms> > (
00364                                 new Store<Sms>(filename, false, dnow, vmode)));
00365         }
00366         else if( name == Folder::GetDBName() ) {
00367                 return shared_ptr<Parser>(
00368                         new RecordParser<Folder, Store<Folder> > (
00369                                 new Store<Folder>(filename, false, dnow, vmode)));
00370         }
00371         else if( name == TimeZone::GetDBName() ) {
00372                 return shared_ptr<Parser>(
00373                         new RecordParser<TimeZone, Store<TimeZone> > (
00374                                 new Store<TimeZone>(filename, false, dnow, vmode)));
00375         }
00376         else if( name == HandheldAgent::GetDBName() ) {
00377                 return shared_ptr<Parser>(
00378                         new RecordParser<HandheldAgent, Store<HandheldAgent> > (
00379                                 new Store<HandheldAgent>(filename, false, dnow, vmode)));
00380         }
00381         else {
00382                 // unknown database, use null parser
00383                 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
00384         }
00385 }
00386 
00387 shared_ptr<Builder> GetBuilder(const string &name, const string &filename)
00388 {
00389         // check for recognized database names
00390         if( name == Contact::GetDBName() ) {
00391                 return shared_ptr<Builder>(
00392                         new RecordBuilder<Contact, Store<Contact> > (
00393                                 new Store<Contact>(filename, true, true, false)));
00394         }
00395         else if( name == Calendar::GetDBName() ) {
00396                 return shared_ptr<Builder>(
00397                         new RecordBuilder<Calendar, Store<Calendar> > (
00398                                 new Store<Calendar>(filename, true, true, false)));
00399         }
00400         else if( name == CalendarAll::GetDBName() ) {
00401                 return shared_ptr<Builder>(
00402                         new RecordBuilder<CalendarAll, Store<CalendarAll> > (
00403                                 new Store<CalendarAll>(filename, true, true, false)));
00404         }
00405         else if( name == Memo::GetDBName() ) {
00406                 return shared_ptr<Builder>(
00407                         new RecordBuilder<Memo, Store<Memo> > (
00408                                 new Store<Memo>(filename, true, true, false)));
00409         }
00410         else if( name == Task::GetDBName() ) {
00411                 return shared_ptr<Builder>(
00412                         new RecordBuilder<Task, Store<Task> > (
00413                                 new Store<Task>(filename, true, true, false)));
00414         }
00415 /*
00416         else if( name == "Messages" ) {
00417                 return shared_ptr<Parser>(
00418                         new RecordParser<Message, Store<Message> > (
00419                                 new Store<Message>(filename, true, true, false)));
00420         }
00421         else if( name == "Service Book" ) {
00422                 return shared_ptr<Parser>(
00423                         new RecordParser<ServiceBook, Store<ServiceBook> > (
00424                                 new Store<ServiceBook>(filename, true, true, false)));
00425         }
00426 */
00427         else {
00428                 throw std::runtime_error("No Builder available for database");
00429         }
00430 }
00431 
00432 struct StateTableCommand
00433 {
00434         char flag;
00435         bool clear;
00436         unsigned int index;
00437 
00438         StateTableCommand(char f, bool c, unsigned int i)
00439                 : flag(f), clear(c), index(i) {}
00440 };
00441 
00442 bool SplitMap(const string &map, string &ldif, string &read, string &write)
00443 {
00444         string::size_type a = map.find(',');
00445         if( a == string::npos )
00446                 return false;
00447 
00448         string::size_type b = map.find(',', a+1);
00449         if( b == string::npos )
00450                 return false;
00451 
00452         ldif.assign(map, 0, a);
00453         read.assign(map, a + 1, b - a - 1);
00454         write.assign(map, b + 1, map.size() - b - 1);
00455 
00456         return ldif.size() && read.size() && write.size();
00457 }
00458 
00459 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
00460 {
00461         for(    vector<string>::const_iterator i = mapCommands.begin();
00462                 i != mapCommands.end();
00463                 ++i )
00464         {
00465                 // single names mean unmapping
00466                 if( i->find(',') == string::npos ) {
00467                         // unmap
00468                         cerr << "Unmapping: " << *i << endl;
00469                         ldif.Unmap(*i);
00470                 }
00471                 else {
00472                         cerr << "Mapping: " << *i << endl;
00473 
00474                         // map... extract ldif/read/write names
00475                         string ldifname, read, write;
00476                         if( SplitMap(*i, ldifname, read, write) ) {
00477                                 if( !ldif.Map(ldifname, read, write) ) {
00478                                         cerr << "Read/Write name unknown: " << *i << endl;
00479                                 }
00480                         }
00481                         else {
00482                                 cerr << "Invalid map format: " << *i << endl;
00483                         }
00484                 }
00485         }
00486 }
00487 
00488 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
00489 {
00490         int read, write;
00491         char comma;
00492         istringstream iss(arg);
00493         iss >> hex >> read >> comma >> write;
00494         if( !iss )
00495                 return false;
00496         epp->read = read;
00497         epp->write = write;
00498         return true;
00499 }
00500 
00501 void ParseSortKey(const std::string &key)
00502 {
00503         istringstream iss(key);
00504         string db, spec;
00505         getline(iss, db, ':');
00506         getline(iss, spec, ':');
00507 
00508         if( db.size() && spec.size() )
00509                 SortKeys[db] = spec;
00510 }
00511 
00512 int main(int argc, char *argv[])
00513 {
00514         INIT_I18N(PACKAGE);
00515 
00516         cout.sync_with_stdio(true);     // leave this on, since libusb uses
00517                                         // stdio for debug messages
00518 
00519         try {
00520 
00521                 uint32_t pin = 0;
00522                 bool    list_only = false,
00523                         show_dbdb = false,
00524                         ldif_contacts = false,
00525                         data_dump = false,
00526                         vformat_mode = false,
00527                         reset_device = false,
00528                         list_contact_fields = false,
00529                         list_ldif_map = false,
00530                         epp_override = false,
00531                         threaded_sockets = true,
00532                         record_state_table = false,
00533                         clear_database = false,
00534                         null_parser = false,
00535                         bbackup_mode = false,
00536                         sort_records = false,
00537                         show_parsers = false,
00538                         show_fields = false;
00539                 string ldifBaseDN, ldifDnAttr;
00540                 string filename;
00541                 string password;
00542                 string busname;
00543                 string devname;
00544                 string iconvCharset;
00545                 vector<string> dbNames, saveDbNames, mapCommands, clearDbNames;
00546                 vector<StateTableCommand> stCommands;
00547                 Usb::EndpointPair epOverride;
00548 
00549                 // process command line options
00550                 for(;;) {
00551                         int cmd = getopt(argc, argv, "a:b:B:c:C:d:D:e:f:F:hi:IlLm:MnN:p:P:r:R:Ss:tT:vVXzZ");
00552                         if( cmd == -1 )
00553                                 break;
00554 
00555                         switch( cmd )
00556                         {
00557                         case 'a':       // Clear Database
00558                                 clear_database = true;
00559                                 clearDbNames.push_back(string(optarg));
00560                                 break;
00561 
00562                         case 'b':       // Barry backup filename (tar.gz)
00563 #ifdef __BARRY_BACKUP_MODE__
00564                                 if( filename.size() == 0 ) {
00565                                         filename = optarg;
00566                                         bbackup_mode = true;
00567                                 }
00568                                 else {
00569                                         cerr << "Do not use -f with -b\n";
00570                                         return 1;
00571                                 }
00572 #else
00573                                 cerr << "-b option not supported - no Barry "
00574                                         "Backup library support available\n";
00575                                 return 1;
00576 #endif
00577                                 break;
00578 
00579                         case 'B':       // busname
00580                                 busname = optarg;
00581                                 break;
00582 
00583                         case 'c':       // contacts to ldap ldif
00584                                 ldif_contacts = true;
00585                                 ldifBaseDN = optarg;
00586                                 break;
00587 
00588                         case 'C':       // DN Attribute for FQDN
00589                                 ldifDnAttr = optarg;
00590                                 break;
00591 
00592                         case 'd':       // show dbname
00593                                 dbNames.push_back(string(optarg));
00594                                 break;
00595 
00596                         case 'D':       // delete record
00597                                 stCommands.push_back(
00598                                         StateTableCommand('D', false, atoi(optarg)));
00599                                 break;
00600 
00601                         case 'e':       // endpoint override
00602                                 if( !ParseEpOverride(optarg, &epOverride) ) {
00603                                         Usage();
00604                                         return 1;
00605                                 }
00606                                 epp_override = true;
00607                                 break;
00608 
00609                         case 'f':       // filename
00610 #ifdef __BTOOL_BOOST_MODE__
00611                                 if( !bbackup_mode && filename.size() == 0 ) {
00612                                         filename = optarg;
00613                                 }
00614                                 else {
00615                                         cerr << "Do not use -f with -b\n";
00616                                         return 1;
00617                                 }
00618 #else
00619                                 cerr << "-f option not supported - no Boost "
00620                                         "serialization support available\n";
00621                                 return 1;
00622 #endif
00623                                 break;
00624 
00625                         case 'F':       // sort key
00626                                 ParseSortKey(optarg);
00627                                 break;
00628 
00629                         case 'i':       // international charset (iconv)
00630                                 iconvCharset = optarg;
00631                                 break;
00632 
00633                         case 'I':       // sort before dump
00634                                 sort_records = true;
00635                                 break;
00636 
00637                         case 'l':       // list only
00638                                 list_only = true;
00639                                 break;
00640 
00641                         case 'L':       // List Contact field names
00642                                 list_contact_fields = true;
00643                                 break;
00644 
00645                         case 'm':       // Map / Unmap
00646                                 mapCommands.push_back(string(optarg));
00647                                 break;
00648 
00649                         case 'M':       // List LDIF map
00650                                 list_ldif_map = true;
00651                                 break;
00652 
00653                         case 'n':       // use null parser
00654                                 null_parser = true;
00655                                 break;
00656 
00657                         case 'N':       // Devname
00658                                 devname = optarg;
00659                                 break;
00660 
00661                         case 'p':       // Blackberry PIN
00662                                 pin = strtoul(optarg, NULL, 16);
00663                                 break;
00664 
00665                         case 'P':       // Device password
00666                                 password = optarg;
00667                                 break;
00668 
00669                         case 'r':       // get specific record index
00670                                 stCommands.push_back(
00671                                         StateTableCommand('r', false, atoi(optarg)));
00672                                 break;
00673 
00674                         case 'R':       // same as 'r', and clears dirty
00675                                 stCommands.push_back(
00676                                         StateTableCommand('r', true, atoi(optarg)));
00677                                 break;
00678 
00679                         case 's':       // save dbname
00680                                 saveDbNames.push_back(string(optarg));
00681                                 break;
00682 
00683                         case 'S':       // show supported databases
00684                                 if( show_parsers )
00685                                         show_fields = true;
00686                                 else
00687                                         show_parsers = true;
00688                                 break;
00689 
00690                         case 't':       // display database database
00691                                 show_dbdb = true;
00692                                 break;
00693 
00694                         case 'T':       // show RecordStateTable
00695                                 record_state_table = true;
00696                                 dbNames.push_back(string(optarg));
00697                                 break;
00698 
00699                         case 'v':       // data dump on
00700                                 data_dump = true;
00701                                 break;
00702 
00703                         case 'V':       // vformat MIME mode
00704 #ifdef __BARRY_SYNC_MODE__
00705                                 vformat_mode = true;
00706 #else
00707                                 cerr << "-V option not supported - no Sync "
00708                                         "library support available\n";
00709                                 return 1;
00710 #endif
00711                                 break;
00712 
00713                         case 'X':       // reset device
00714                                 reset_device = true;
00715                                 break;
00716 
00717                         case 'z':       // non-threaded sockets
00718                                 threaded_sockets = false;
00719                                 break;
00720 
00721                         case 'Z':       // threaded socket router
00722                                 threaded_sockets = true;
00723                                 break;
00724 
00725                         case 'h':       // help
00726                         default:
00727                                 Usage();
00728                                 return 0;
00729                         }
00730                 }
00731 
00732                 if( show_parsers ) {
00733                         ShowParsers(show_fields, true);
00734                         ShowBuilders();
00735                         return 0;
00736                 }
00737 
00738                 // Initialize the barry library.  Must be called before
00739                 // anything else.
00740                 Barry::Init(data_dump);
00741                 if( data_dump ) {
00742                         int logical, major, minor;
00743                         const char *Version = Barry::Version(logical, major, minor);
00744                         cout << Version << endl;
00745                 }
00746 
00747                 // Create an IConverter object if needed
00748                 auto_ptr<IConverter> ic;
00749                 if( iconvCharset.size() ) {
00750                         ic.reset( new IConverter(iconvCharset.c_str(), true) );
00751                 }
00752 
00753                 // LDIF class... only needed if ldif output turned on
00754                 ContactLdif ldif(ldifBaseDN);
00755                 DoMapping(ldif, mapCommands);
00756                 if( ldifDnAttr.size() ) {
00757                         if( !ldif.SetDNAttr(ldifDnAttr) ) {
00758                                 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
00759                         }
00760                 }
00761 
00762                 // Probe the USB bus for Blackberry devices and display.
00763                 // If user has specified a PIN, search for it in the
00764                 // available device list here as well
00765                 Barry::Probe probe(busname.c_str(), devname.c_str(),
00766                         epp_override ? &epOverride : 0);
00767                 int activeDevice = -1;
00768 
00769                 // show any errors during probe first
00770                 if( probe.GetFailCount() ) {
00771                         if( ldif_contacts )
00772                                 cout << "# ";
00773                         cout << "Blackberry device errors with errors during probe:" << endl;
00774                         for( int i = 0; i < probe.GetFailCount(); i++ ) {
00775                                 if( ldif_contacts )
00776                                         cout << "# ";
00777                                 cout << probe.GetFailMsg(i) << endl;
00778                         }
00779                 }
00780 
00781                 // show all successfully found devices
00782                 if( ldif_contacts )
00783                         cout << "# ";
00784                 cout << "Blackberry devices found:" << endl;
00785                 for( int i = 0; i < probe.GetCount(); i++ ) {
00786                         if( ldif_contacts )
00787                                 cout << "# ";
00788                         if( data_dump )
00789                                 probe.Get(i).DumpAll(cout);
00790                         else
00791                                 cout << probe.Get(i);
00792                         cout << endl;
00793                         if( probe.Get(i).m_pin == pin )
00794                                 activeDevice = i;
00795                 }
00796 
00797                 if( list_only )
00798                         return 0;       // done
00799 
00800                 if( activeDevice == -1 ) {
00801                         if( pin == 0 ) {
00802                                 // can we default to single device?
00803                                 if( probe.GetCount() == 1 )
00804                                         activeDevice = 0;
00805                                 else {
00806                                         cerr << "No device selected" << endl;
00807                                         return 1;
00808                                 }
00809                         }
00810                         else {
00811                                 cerr << "PIN " << setbase(16) << pin
00812                                         << " not found" << endl;
00813                                 return 1;
00814                         }
00815                 }
00816 
00817                 if( ldif_contacts )
00818                         cout << "# ";
00819                 cout << "Using device (PIN): "
00820                         << probe.Get(activeDevice).m_pin.Str() << endl;
00821 
00822                 if( reset_device ) {
00823                         Usb::Device dev(probe.Get(activeDevice).m_dev);
00824                         dev.Reset();
00825                         return 0;
00826                 }
00827 
00828                 // Override device endpoints if user asks
00829                 Barry::ProbeResult device = probe.Get(activeDevice);
00830                 if( epp_override ) {
00831                         device.m_ep.read = epOverride.read;
00832                         device.m_ep.write = epOverride.write;
00833                         // FIXME - override this too?
00834                         device.m_ep.type = Usb::EndpointDescriptor::BulkType;
00835                         cout << "Endpoint pair (read,write) overridden with: "
00836                              << hex
00837                              << (unsigned int) device.m_ep.read << ","
00838                              << (unsigned int) device.m_ep.write << endl;
00839                 }
00840 
00841                 //
00842                 // execute each mode that was turned on
00843                 //
00844 
00845 
00846                 // Dump current LDIF mapping
00847                 if( list_ldif_map ) {
00848                         cout << ldif << endl;
00849                 }
00850 
00851                 // Dump list of Contact field names
00852                 if( list_contact_fields ) {
00853                         for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
00854                                 cout.fill(' ');
00855                                 cout << "  " << left << setw(20) << n->name << ": "
00856                                         << n->description << endl;
00857                         }
00858                 }
00859 
00860                 // Check if Desktop access is needed
00861                 if( !(  show_dbdb ||
00862                         ldif_contacts ||
00863                         record_state_table ||
00864                         clear_database ||
00865                         stCommands.size() ||
00866                         dbNames.size() ||
00867                         saveDbNames.size() ) )
00868                         return 0;       // done
00869 
00870                 //
00871                 // Create our controller object
00872                 //
00873                 // Order is important in the following auto_ptr<> objects,
00874                 // since Controller must get destroyed before router.
00875                 // Normally you'd pick one method, and not bother
00876                 // with auto_ptr<> and so the normal C++ constructor
00877                 // rules would guarantee this safety for you, but
00878                 // here we want the user to pick.
00879                 //
00880                 auto_ptr<SocketRoutingQueue> router;
00881                 if( threaded_sockets ) {
00882                         router.reset( new SocketRoutingQueue );
00883                         router->SpinoffSimpleReadThread();
00884                 }
00885 
00886                 DesktopConnector connector(password.c_str(),
00887                         iconvCharset, device, router.get());
00888                 if( !connector.Connect() ) {
00889                         // bad password (default action is not to prompt)
00890                         cerr << connector.GetBadPassword().what() << endl;
00891                         return 1;
00892                 }
00893 
00894                 Barry::Mode::Desktop &desktop = connector.GetDesktop();
00895 
00896                 // Dump list of all databases to stdout
00897                 if( show_dbdb ) {
00898                         // open desktop mode socket
00899                         cout << desktop.GetDBDB() << endl;
00900                 }
00901 
00902                 // Dump list of contacts to an LDAP LDIF file
00903                 // This uses the Controller convenience templates
00904                 if( ldif_contacts ) {
00905                         // create a storage functor object that accepts
00906                         // Barry::Contact objects as input
00907                         Contact2Ldif storage(ldif);
00908 
00909                         // load all the Contact records into storage
00910                         desktop.LoadDatabaseByType<Barry::Contact>(storage);
00911                 }
00912 
00913                 // Dump record state table to stdout
00914                 if( record_state_table ) {
00915                         if( dbNames.size() == 0 ) {
00916                                 cout << "No db names to process" << endl;
00917                                 return 1;
00918                         }
00919 
00920                         vector<string>::iterator b = dbNames.begin();
00921                         for( ; b != dbNames.end(); b++ ) {
00922                                 unsigned int id = desktop.GetDBID(*b);
00923                                 RecordStateTable state;
00924                                 desktop.GetRecordStateTable(id, state);
00925                                 cout << "Record state table for: " << *b << endl;
00926                                 cout << state;
00927                         }
00928                         return 0;
00929                 }
00930 
00931                 // Get Record mode overrides the default name mode
00932                 if( stCommands.size() ) {
00933                         if( dbNames.size() != 1 ) {
00934                                 cout << "Must have 1 db name to process" << endl;
00935                                 return 1;
00936                         }
00937 
00938                         unsigned int id = desktop.GetDBID(dbNames[0]);
00939                         shared_ptr<Parser> parse = GetParser(dbNames[0],filename,
00940                                 null_parser, true, vformat_mode, bbackup_mode);
00941 
00942                         for( unsigned int i = 0; i < stCommands.size(); i++ ) {
00943                                 desktop.GetRecord(id, stCommands[i].index, *parse.get());
00944 
00945                                 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
00946                                         cout << "Clearing record's dirty flags..." << endl;
00947                                         desktop.ClearDirty(id, stCommands[i].index);
00948                                 }
00949 
00950                                 if( stCommands[i].flag == 'D' ) {
00951                                         desktop.DeleteRecord(id, stCommands[i].index);
00952                                 }
00953                         }
00954 
00955                         return 0;
00956                 }
00957 
00958                 // Dump contents of selected databases to stdout, or
00959                 // to file if specified.
00960                 // This is retrieving data from the Blackberry.
00961                 if( dbNames.size() ) {
00962                         vector<string>::iterator b = dbNames.begin();
00963 
00964                         for( ; b != dbNames.end(); b++ ) {
00965                                 shared_ptr<Parser> parse = GetParser(*b,
00966                                         filename, null_parser, !sort_records,
00967                                         vformat_mode, bbackup_mode);
00968                                 unsigned int id = desktop.GetDBID(*b);
00969                                 desktop.LoadDatabase(id, *parse.get());
00970                         }
00971                 }
00972 
00973                 // Clear databases
00974                 if( clear_database ) {
00975                         if( clearDbNames.size() == 0 ) {
00976                                 cout << "No db names to erase" << endl;
00977                                 return 1;
00978                         }
00979 
00980                         vector<string>::iterator b = clearDbNames.begin();
00981 
00982                         for( ; b != clearDbNames.end(); b++ ) {
00983                                 unsigned int id = desktop.GetDBID(*b);
00984                                 cout << "Deleting all records from " << (*b) << "..." << endl;
00985                                 desktop.ClearDatabase(id);
00986                         }
00987 
00988                         return 0;
00989                 }
00990 
00991                 // Save contents of file to specified databases
00992                 // This is writing data to the Blackberry.
00993                 if( saveDbNames.size() ) {
00994                         vector<string>::iterator b = saveDbNames.begin();
00995 
00996                         for( ; b != saveDbNames.end(); b++ ) {
00997                                 shared_ptr<Builder> build = GetBuilder(*b,
00998                                         filename);
00999                                 unsigned int id = desktop.GetDBID(*b);
01000                                 desktop.SaveDatabase(id, *build);
01001                         }
01002                 }
01003 
01004         }
01005         catch( Usb::Error &ue ) {
01006                 std::cerr << "Usb::Error caught: " << ue.what() << endl;
01007                 return 1;
01008         }
01009         catch( Barry::Error &se ) {
01010                 std::cerr << "Barry::Error caught: " << se.what() << endl;
01011                 return 1;
01012         }
01013         catch( std::exception &e ) {
01014                 std::cerr << "std::exception caught: " << e.what() << endl;
01015                 return 1;
01016         }
01017 
01018         return 0;
01019 }
01020