Updated root README file
[barry.git] / tools / btool.cc
blobd4ffe498ba1d7856ee69ec4479c205411809209f
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
7 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include <barry/barry.h>
23 #ifdef __BARRY_SYNC_MODE__
24 #include <barry/barrysync.h>
25 #include "mimedump.h"
26 #endif
27 #ifdef __BARRY_BACKUP_MODE__
28 #include <barry/barrybackup.h>
29 #endif
31 #include <iomanip>
32 #include <iostream>
33 #include <fstream>
34 #include <sstream>
35 #include <vector>
36 #include <string>
37 #include <algorithm>
38 #include <getopt.h>
39 #include <tr1/memory>
40 #include "i18n.h"
43 using namespace std;
44 using namespace std::tr1;
45 using namespace Barry;
47 void Usage()
49 int major, minor;
50 const char *Version = Barry::Version(major, minor);
52 cerr
53 << "btool - Command line USB Blackberry Test Tool\n"
54 << " Copyright 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
55 << " Using: " << Version << "\n"
56 << " Compiled "
57 #ifdef __BARRY_BOOST_MODE__
58 << "with"
59 #else
60 << "without"
61 #endif
62 << " Boost support\n"
63 << "\n"
64 << " -b file Filename to save or load a Barry Backup to (tar.gz)\n"
65 << " -B bus Specify which USB bus to search on\n"
66 << " -N dev Specify which system device, using system specific string\n"
67 << "\n"
68 << " -a db Erase / clear database 'db' FROM device, deleting all\n"
69 << " its records. Can be used multiple times to clear more\n"
70 << " than one DB.\n"
71 << " -c dn Convert address book database to LDIF format, using the\n"
72 << " specified baseDN\n"
73 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
74 << " Defaults to 'cn'\n"
75 << " -d db Load database 'db' FROM device and dump to screen\n"
76 << " Can be used multiple times to fetch more than one DB\n"
77 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
78 << " string separated by a comma, holding the read,write\n"
79 << " endpoint pair. Example: -e 83,5\n"
80 << " Note: Endpoints are specified in hex.\n"
81 << " You should never need to use this option.\n"
82 #ifdef __BARRY_BOOST_MODE__
83 << " -f file Filename to save or load handheld data to/from\n"
84 #endif
85 << " -h This help\n"
86 << " -i cs International charset for string conversions\n"
87 << " Valid values here are available with 'iconv --list'\n"
88 << " -I Sort records before output\n"
89 << " -l List devices\n"
90 << " -L List Contact field names\n"
91 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
92 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
93 << " Unmap: ldif name alone\n"
94 << " -M List current LDIF mapping\n"
95 << " -n Use null parser on all databases.\n"
96 << " -p pin PIN of device to talk with\n"
97 << " If only one device is plugged in, this flag is optional\n"
98 << " -P pass Simplistic method to specify device password\n"
99 << " -s db Save database 'db' TO device from data loaded from -f file\n"
100 << " -S Show list of supported database parsers\n"
101 << " -t Show database database table\n"
102 << " -T db Show record state table for given database\n"
103 << " -v Dump protocol data during operation\n"
104 #ifdef __BARRY_SYNC_MODE__
105 << " -V Dump records using MIME vformats where possible\n"
106 #endif
107 << " -X Reset device\n"
108 << " -z Use non-threaded sockets\n"
109 << " -Z Use threaded socket router (default)\n"
110 << "\n"
111 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
112 << "\n"
113 << " -r # Record index number as seen in the -T state table.\n"
114 << " This overrides the default -d behaviour, and only\n"
115 << " downloads the one specified record, sending to stdout.\n"
116 << " -R # Same as -r, but also clears the record's dirty flags.\n"
117 << " -D # Record index number as seen in the -T state table,\n"
118 << " which indicates the record to delete. Used with the -d\n"
119 << " command to specify the database.\n"
120 << endl;
123 class Contact2Ldif
125 public:
126 Barry::ContactLdif &ldif;
128 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
130 void operator()(const Contact &rec)
132 ldif.DumpLdif(cout, rec);
136 template <class Record>
137 struct Store
139 std::vector<Record> records;
140 mutable typename std::vector<Record>::const_iterator rec_it;
141 std::string filename;
142 bool load;
143 bool immediate_display;
144 bool vformat_mode;
145 int count;
147 Store(const string &filename, bool load, bool immediate_display,
148 bool vformat_mode)
149 : rec_it(records.end()),
150 filename(filename),
151 load(load),
152 immediate_display(immediate_display),
153 vformat_mode(vformat_mode),
154 count(0)
156 #ifdef __BARRY_BOOST_MODE__
157 try {
159 if( load && filename.size() ) {
160 // filename is available, attempt to load
161 cout << "Loading: " << filename << endl;
162 ifstream ifs(filename.c_str());
163 std::string dbName;
164 getline(ifs, dbName);
165 boost::archive::text_iarchive ia(ifs);
166 ia >> records;
167 cout << records.size()
168 << " records loaded from '"
169 << filename << "'" << endl;
170 sort(records.begin(), records.end());
171 rec_it = records.begin();
173 // debugging aid
174 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
175 for( ; beg != end; beg++ ) {
176 cout << (*beg) << endl;
180 } catch( boost::archive::archive_exception &ae ) {
181 cerr << "Archive exception in ~Store(): "
182 << ae.what() << endl;
184 #endif
187 ~Store()
189 if( !immediate_display ) {
190 // not dumped yet, sort then dump
191 sort(records.begin(), records.end());
192 DumpAll();
195 cout << "Store counted " << dec << count << " records." << endl;
196 #ifdef __BARRY_BOOST_MODE__
197 try {
199 if( !load && filename.size() ) {
200 // filename is available, attempt to save
201 cout << "Saving: " << filename << endl;
202 const std::vector<Record> &r = records;
203 ofstream ofs(filename.c_str());
204 ofs << Record::GetDBName() << endl;
205 boost::archive::text_oarchive oa(ofs);
206 oa << r;
207 cout << dec << r.size() << " records saved to '"
208 << filename << "'" << endl;
211 } catch( boost::archive::archive_exception &ae ) {
212 cerr << "Archive exception in ~Store(): "
213 << ae.what() << endl;
215 #endif
218 void DumpAll()
220 typename vector<Record>::const_iterator i = records.begin();
221 for( ; i != records.end(); ++i ) {
222 Dump(*i);
226 void Dump(const Record &rec)
228 if( vformat_mode ) {
229 #ifdef __BARRY_SYNC_MODE__
230 MimeDump<Record> md;
231 md.Dump(cout, rec);
232 #endif
234 else {
235 cout << rec << endl;
239 // storage operator
240 void operator()(const Record &rec)
242 count++;
243 if( immediate_display )
244 Dump(rec);
245 records.push_back(rec);
248 // retrieval operator
249 bool operator()(Record &rec, Builder &builder) const
251 if( rec_it == records.end() )
252 return false;
253 rec = *rec_it;
254 rec_it++;
255 return true;
259 shared_ptr<Parser> GetParser(const string &name,
260 const string &filename,
261 bool null_parser,
262 bool immediate_display,
263 bool vformat_mode,
264 bool bbackup_mode)
266 bool dnow = immediate_display;
267 bool vmode = vformat_mode;
269 if( null_parser ) {
270 // use null parser
271 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
273 else if( bbackup_mode ) {
274 #ifdef __BARRY_BACKUP_MODE__
275 // Only one backup file per run
276 static shared_ptr<Parser> backup;
277 if( !backup.get() ) {
278 backup.reset( new Backup(filename) );
280 return backup;
281 #else
282 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
283 #endif
285 // check for recognized database names
286 else if( name == Contact::GetDBName() ) {
287 return shared_ptr<Parser>(
288 new RecordParser<Contact, Store<Contact> > (
289 new Store<Contact>(filename, false, dnow, vmode)));
291 else if( name == Message::GetDBName() ) {
292 return shared_ptr<Parser>(
293 new RecordParser<Message, Store<Message> > (
294 new Store<Message>(filename, false, dnow, vmode)));
296 else if( name == Calendar::GetDBName() ) {
297 return shared_ptr<Parser>(
298 new RecordParser<Calendar, Store<Calendar> > (
299 new Store<Calendar>(filename, false, dnow, vmode)));
301 else if( name == CalendarAll::GetDBName() ) {
302 return shared_ptr<Parser>(
303 new RecordParser<CalendarAll, Store<CalendarAll> > (
304 new Store<CalendarAll>(filename, false, dnow, vmode)));
306 else if( name == CallLog::GetDBName() ) {
307 return shared_ptr<Parser>(
308 new RecordParser<CallLog, Store<CallLog> > (
309 new Store<CallLog>(filename, false, dnow, vmode)));
311 else if( name == Bookmark::GetDBName() ) {
312 return shared_ptr<Parser>(
313 new RecordParser<Bookmark, Store<Bookmark> > (
314 new Store<Bookmark>(filename, false, dnow, vmode)));
316 else if( name == ServiceBook::GetDBName() ) {
317 return shared_ptr<Parser>(
318 new RecordParser<ServiceBook, Store<ServiceBook> > (
319 new Store<ServiceBook>(filename, false, dnow, vmode)));
322 else if( name == Memo::GetDBName() ) {
323 return shared_ptr<Parser>(
324 new RecordParser<Memo, Store<Memo> > (
325 new Store<Memo>(filename, false, dnow, vmode)));
327 else if( name == Task::GetDBName() ) {
328 return shared_ptr<Parser>(
329 new RecordParser<Task, Store<Task> > (
330 new Store<Task>(filename, false, dnow, vmode)));
332 else if( name == PINMessage::GetDBName() ) {
333 return shared_ptr<Parser>(
334 new RecordParser<PINMessage, Store<PINMessage> > (
335 new Store<PINMessage>(filename, false, dnow, vmode)));
337 else if( name == SavedMessage::GetDBName() ) {
338 return shared_ptr<Parser>(
339 new RecordParser<SavedMessage, Store<SavedMessage> > (
340 new Store<SavedMessage>(filename, false, dnow, vmode)));
342 else if( name == Sms::GetDBName() ) {
343 return shared_ptr<Parser>(
344 new RecordParser<Sms, Store<Sms> > (
345 new Store<Sms>(filename, false, dnow, vmode)));
347 else if( name == Folder::GetDBName() ) {
348 return shared_ptr<Parser>(
349 new RecordParser<Folder, Store<Folder> > (
350 new Store<Folder>(filename, false, dnow, vmode)));
352 else if( name == Timezone::GetDBName() ) {
353 return shared_ptr<Parser>(
354 new RecordParser<Timezone, Store<Timezone> > (
355 new Store<Timezone>(filename, false, dnow, vmode)));
357 else {
358 // unknown database, use null parser
359 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
363 shared_ptr<Builder> GetBuilder(const string &name, const string &filename)
365 // check for recognized database names
366 if( name == Contact::GetDBName() ) {
367 return shared_ptr<Builder>(
368 new RecordBuilder<Contact, Store<Contact> > (
369 new Store<Contact>(filename, true, true, false)));
371 else if( name == Calendar::GetDBName() ) {
372 return shared_ptr<Builder>(
373 new RecordBuilder<Calendar, Store<Calendar> > (
374 new Store<Calendar>(filename, true, true, false)));
376 else if( name == CalendarAll::GetDBName() ) {
377 return shared_ptr<Builder>(
378 new RecordBuilder<CalendarAll, Store<CalendarAll> > (
379 new Store<CalendarAll>(filename, true, true, false)));
381 else if( name == Memo::GetDBName() ) {
382 return shared_ptr<Builder>(
383 new RecordBuilder<Memo, Store<Memo> > (
384 new Store<Memo>(filename, true, true, false)));
386 else if( name == Task::GetDBName() ) {
387 return shared_ptr<Builder>(
388 new RecordBuilder<Task, Store<Task> > (
389 new Store<Task>(filename, true, true, false)));
392 else if( name == "Messages" ) {
393 return shared_ptr<Parser>(
394 new RecordParser<Message, Store<Message> > (
395 new Store<Message>(filename, true, true, false)));
397 else if( name == "Service Book" ) {
398 return shared_ptr<Parser>(
399 new RecordParser<ServiceBook, Store<ServiceBook> > (
400 new Store<ServiceBook>(filename, true, true, false)));
403 else {
404 throw std::runtime_error("No Builder available for database");
408 void ShowParsers()
410 cout << "Supported Database parsers:\n"
411 #undef HANDLE_PARSER
412 #ifdef __BARRY_SYNC_MODE__
413 << " (* = can display in vformat MIME mode)\n"
414 #define HANDLE_PARSER(tname) << " " << tname::GetDBName() << (MimeDump<tname>::Supported() ? " *" : "") << "\n"
416 #else
417 #define HANDLE_PARSER(tname) << " " << tname::GetDBName() << "\n"
419 #endif
420 ALL_KNOWN_PARSER_TYPES
422 << "\n"
424 << "Supported Database builders:\n"
425 #undef HANDLE_BUILDER
426 #define HANDLE_BUILDER(tname) << " " << tname::GetDBName() << "\n"
427 ALL_KNOWN_BUILDER_TYPES
428 << endl;
431 struct StateTableCommand
433 char flag;
434 bool clear;
435 unsigned int index;
437 StateTableCommand(char f, bool c, unsigned int i)
438 : flag(f), clear(c), index(i) {}
441 bool SplitMap(const string &map, string &ldif, string &read, string &write)
443 string::size_type a = map.find(',');
444 if( a == string::npos )
445 return false;
447 string::size_type b = map.find(',', a+1);
448 if( b == string::npos )
449 return false;
451 ldif.assign(map, 0, a);
452 read.assign(map, a + 1, b - a - 1);
453 write.assign(map, b + 1, map.size() - b - 1);
455 return ldif.size() && read.size() && write.size();
458 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
460 for( vector<string>::const_iterator i = mapCommands.begin();
461 i != mapCommands.end();
462 ++i )
464 // single names mean unmapping
465 if( i->find(',') == string::npos ) {
466 // unmap
467 cerr << "Unmapping: " << *i << endl;
468 ldif.Unmap(*i);
470 else {
471 cerr << "Mapping: " << *i << endl;
473 // map... extract ldif/read/write names
474 string ldifname, read, write;
475 if( SplitMap(*i, ldifname, read, write) ) {
476 if( !ldif.Map(ldifname, read, write) ) {
477 cerr << "Read/Write name unknown: " << *i << endl;
480 else {
481 cerr << "Invalid map format: " << *i << endl;
487 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
489 int read, write;
490 char comma;
491 istringstream iss(arg);
492 iss >> hex >> read >> comma >> write;
493 if( !iss )
494 return false;
495 epp->read = read;
496 epp->write = write;
497 return true;
500 int main(int argc, char *argv[])
502 INIT_I18N(PACKAGE);
504 cout.sync_with_stdio(true); // leave this on, since libusb uses
505 // stdio for debug messages
507 try {
509 uint32_t pin = 0;
510 bool list_only = false,
511 show_dbdb = false,
512 ldif_contacts = false,
513 data_dump = false,
514 vformat_mode = false,
515 reset_device = false,
516 list_contact_fields = false,
517 list_ldif_map = false,
518 epp_override = false,
519 threaded_sockets = true,
520 record_state_table = false,
521 clear_database = false,
522 null_parser = false,
523 bbackup_mode = false,
524 sort_records = false;
525 string ldifBaseDN, ldifDnAttr;
526 string filename;
527 string password;
528 string busname;
529 string devname;
530 string iconvCharset;
531 vector<string> dbNames, saveDbNames, mapCommands, clearDbNames;
532 vector<StateTableCommand> stCommands;
533 Usb::EndpointPair epOverride;
535 // process command line options
536 for(;;) {
537 int cmd = getopt(argc, argv, "a:b:B:c:C:d:D:e:f:hi:IlLm:MnN:p:P:r:R:Ss:tT:vVXzZ");
538 if( cmd == -1 )
539 break;
541 switch( cmd )
543 case 'a': // Clear Database
544 clear_database = true;
545 clearDbNames.push_back(string(optarg));
546 break;
548 case 'b': // Barry backup filename (tar.gz)
549 #ifdef __BARRY_BACKUP_MODE__
550 if( filename.size() == 0 ) {
551 filename = optarg;
552 bbackup_mode = true;
554 else {
555 cerr << "Do not use -f with -b\n";
556 return 1;
558 #else
559 cerr << "-b option not supported - no Barry "
560 "Backup library support available\n";
561 return 1;
562 #endif
563 break;
565 case 'B': // busname
566 busname = optarg;
567 break;
569 case 'c': // contacts to ldap ldif
570 ldif_contacts = true;
571 ldifBaseDN = optarg;
572 break;
574 case 'C': // DN Attribute for FQDN
575 ldifDnAttr = optarg;
576 break;
578 case 'd': // show dbname
579 dbNames.push_back(string(optarg));
580 break;
582 case 'D': // delete record
583 stCommands.push_back(
584 StateTableCommand('D', false, atoi(optarg)));
585 break;
587 case 'e': // endpoint override
588 if( !ParseEpOverride(optarg, &epOverride) ) {
589 Usage();
590 return 1;
592 epp_override = true;
593 break;
595 case 'f': // filename
596 #ifdef __BARRY_BOOST_MODE__
597 if( !bbackup_mode && filename.size() == 0 ) {
598 filename = optarg;
600 else {
601 cerr << "Do not use -f with -b\n";
602 return 1;
604 #else
605 cerr << "-f option not supported - no Boost "
606 "serialization support available\n";
607 return 1;
608 #endif
609 break;
611 case 'i': // international charset (iconv)
612 iconvCharset = optarg;
613 break;
615 case 'I': // sort before dump
616 sort_records = true;
617 break;
619 case 'l': // list only
620 list_only = true;
621 break;
623 case 'L': // List Contact field names
624 list_contact_fields = true;
625 break;
627 case 'm': // Map / Unmap
628 mapCommands.push_back(string(optarg));
629 break;
631 case 'M': // List LDIF map
632 list_ldif_map = true;
633 break;
635 case 'n': // use null parser
636 null_parser = true;
637 break;
639 case 'N': // Devname
640 devname = optarg;
641 break;
643 case 'p': // Blackberry PIN
644 pin = strtoul(optarg, NULL, 16);
645 break;
647 case 'P': // Device password
648 password = optarg;
649 break;
651 case 'r': // get specific record index
652 stCommands.push_back(
653 StateTableCommand('r', false, atoi(optarg)));
654 break;
656 case 'R': // same as 'r', and clears dirty
657 stCommands.push_back(
658 StateTableCommand('r', true, atoi(optarg)));
659 break;
661 case 's': // save dbname
662 saveDbNames.push_back(string(optarg));
663 break;
665 case 'S': // show supported databases
666 ShowParsers();
667 return 0;
669 case 't': // display database database
670 show_dbdb = true;
671 break;
673 case 'T': // show RecordStateTable
674 record_state_table = true;
675 dbNames.push_back(string(optarg));
676 break;
678 case 'v': // data dump on
679 data_dump = true;
680 break;
682 case 'V': // vformat MIME mode
683 #ifdef __BARRY_SYNC_MODE__
684 vformat_mode = true;
685 #else
686 cerr << "-V option not supported - no Sync "
687 "library support available\n";
688 return 1;
689 #endif
690 break;
692 case 'X': // reset device
693 reset_device = true;
694 break;
696 case 'z': // non-threaded sockets
697 threaded_sockets = false;
698 break;
700 case 'Z': // threaded socket router
701 threaded_sockets = true;
702 break;
704 case 'h': // help
705 default:
706 Usage();
707 return 0;
711 // Initialize the barry library. Must be called before
712 // anything else.
713 Barry::Init(data_dump);
714 if( data_dump ) {
715 int major, minor;
716 const char *Version = Barry::Version(major, minor);
717 cout << Version << endl;
720 // Create an IConverter object if needed
721 auto_ptr<IConverter> ic;
722 if( iconvCharset.size() ) {
723 ic.reset( new IConverter(iconvCharset.c_str(), true) );
726 // LDIF class... only needed if ldif output turned on
727 ContactLdif ldif(ldifBaseDN);
728 DoMapping(ldif, mapCommands);
729 if( ldifDnAttr.size() ) {
730 if( !ldif.SetDNAttr(ldifDnAttr) ) {
731 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
735 // Probe the USB bus for Blackberry devices and display.
736 // If user has specified a PIN, search for it in the
737 // available device list here as well
738 Barry::Probe probe(busname.c_str(), devname.c_str(),
739 epp_override ? &epOverride : 0);
740 int activeDevice = -1;
742 // show any errors during probe first
743 if( probe.GetFailCount() ) {
744 if( ldif_contacts )
745 cout << "# ";
746 cout << "Blackberry device errors with errors during probe:" << endl;
747 for( int i = 0; i < probe.GetFailCount(); i++ ) {
748 if( ldif_contacts )
749 cout << "# ";
750 cout << probe.GetFailMsg(i) << endl;
754 // show all successfully found devices
755 if( ldif_contacts )
756 cout << "# ";
757 cout << "Blackberry devices found:" << endl;
758 for( int i = 0; i < probe.GetCount(); i++ ) {
759 if( ldif_contacts )
760 cout << "# ";
761 if( data_dump )
762 probe.Get(i).DumpAll(cout);
763 else
764 cout << probe.Get(i);
765 cout << endl;
766 if( probe.Get(i).m_pin == pin )
767 activeDevice = i;
770 if( list_only )
771 return 0; // done
773 if( activeDevice == -1 ) {
774 if( pin == 0 ) {
775 // can we default to single device?
776 if( probe.GetCount() == 1 )
777 activeDevice = 0;
778 else {
779 cerr << "No device selected" << endl;
780 return 1;
783 else {
784 cerr << "PIN " << setbase(16) << pin
785 << " not found" << endl;
786 return 1;
790 if( ldif_contacts )
791 cout << "# ";
792 cout << "Using device (PIN): "
793 << probe.Get(activeDevice).m_pin.Str() << endl;
795 if( reset_device ) {
796 Usb::Device dev(probe.Get(activeDevice).m_dev);
797 dev.Reset();
798 return 0;
801 // Override device endpoints if user asks
802 Barry::ProbeResult device = probe.Get(activeDevice);
803 if( epp_override ) {
804 device.m_ep.read = epOverride.read;
805 device.m_ep.write = epOverride.write;
806 device.m_ep.type = 2; // FIXME - override this too?
807 cout << "Endpoint pair (read,write) overridden with: "
808 << hex
809 << (unsigned int) device.m_ep.read << ","
810 << (unsigned int) device.m_ep.write << endl;
814 // execute each mode that was turned on
818 // Dump current LDIF mapping
819 if( list_ldif_map ) {
820 cout << ldif << endl;
823 // Dump list of Contact field names
824 if( list_contact_fields ) {
825 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
826 cout.fill(' ');
827 cout << " " << left << setw(20) << n->name << ": "
828 << n->description << endl;
832 // Check if Desktop access is needed
833 if( !( show_dbdb ||
834 ldif_contacts ||
835 record_state_table ||
836 clear_database ||
837 stCommands.size() ||
838 dbNames.size() ||
839 saveDbNames.size() ) )
840 return 0; // done
843 // Create our controller object
845 // Order is important in the following auto_ptr<> objects,
846 // since Controller must get destroyed before router.
847 // Normally you'd pick one method, and not bother
848 // with auto_ptr<> and so the normal C++ constructor
849 // rules would guarantee this safety for you, but
850 // here we want the user to pick.
852 auto_ptr<SocketRoutingQueue> router;
853 auto_ptr<Barry::Controller> pcon;
854 if( threaded_sockets ) {
855 router.reset( new SocketRoutingQueue );
856 router->SpinoffSimpleReadThread();
857 pcon.reset( new Barry::Controller(device, *router) );
859 else {
860 pcon.reset( new Barry::Controller(device) );
863 Barry::Controller &con = *pcon;
864 Barry::Mode::Desktop desktop(con, *ic);
865 desktop.Open(password.c_str());
867 // Dump list of all databases to stdout
868 if( show_dbdb ) {
869 // open desktop mode socket
870 cout << desktop.GetDBDB() << endl;
873 // Dump list of contacts to an LDAP LDIF file
874 // This uses the Controller convenience templates
875 if( ldif_contacts ) {
876 // create a storage functor object that accepts
877 // Barry::Contact objects as input
878 Contact2Ldif storage(ldif);
880 // load all the Contact records into storage
881 desktop.LoadDatabaseByType<Barry::Contact>(storage);
884 // Dump record state table to stdout
885 if( record_state_table ) {
886 if( dbNames.size() == 0 ) {
887 cout << "No db names to process" << endl;
888 return 1;
891 vector<string>::iterator b = dbNames.begin();
892 for( ; b != dbNames.end(); b++ ) {
893 unsigned int id = desktop.GetDBID(*b);
894 RecordStateTable state;
895 desktop.GetRecordStateTable(id, state);
896 cout << "Record state table for: " << *b << endl;
897 cout << state;
899 return 0;
902 // Get Record mode overrides the default name mode
903 if( stCommands.size() ) {
904 if( dbNames.size() != 1 ) {
905 cout << "Must have 1 db name to process" << endl;
906 return 1;
909 unsigned int id = desktop.GetDBID(dbNames[0]);
910 shared_ptr<Parser> parse = GetParser(dbNames[0],filename,
911 null_parser, true, vformat_mode, bbackup_mode);
913 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
914 desktop.GetRecord(id, stCommands[i].index, *parse.get());
916 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
917 cout << "Clearing record's dirty flags..." << endl;
918 desktop.ClearDirty(id, stCommands[i].index);
921 if( stCommands[i].flag == 'D' ) {
922 desktop.DeleteRecord(id, stCommands[i].index);
926 return 0;
929 // Dump contents of selected databases to stdout, or
930 // to file if specified.
931 // This is retrieving data from the Blackberry.
932 if( dbNames.size() ) {
933 vector<string>::iterator b = dbNames.begin();
935 for( ; b != dbNames.end(); b++ ) {
936 shared_ptr<Parser> parse = GetParser(*b,
937 filename, null_parser, !sort_records,
938 vformat_mode, bbackup_mode);
939 unsigned int id = desktop.GetDBID(*b);
940 desktop.LoadDatabase(id, *parse.get());
944 // Clear databases
945 if( clear_database ) {
946 if( clearDbNames.size() == 0 ) {
947 cout << "No db names to erase" << endl;
948 return 1;
951 vector<string>::iterator b = clearDbNames.begin();
953 for( ; b != clearDbNames.end(); b++ ) {
954 unsigned int id = desktop.GetDBID(*b);
955 cout << "Deleting all records from " << (*b) << "..." << endl;
956 desktop.ClearDatabase(id);
959 return 0;
962 // Save contents of file to specified databases
963 // This is writing data to the Blackberry.
964 if( saveDbNames.size() ) {
965 vector<string>::iterator b = saveDbNames.begin();
967 for( ; b != saveDbNames.end(); b++ ) {
968 shared_ptr<Builder> build = GetBuilder(*b,
969 filename);
970 unsigned int id = desktop.GetDBID(*b);
971 desktop.SaveDatabase(id, *build);
976 catch( Usb::Error &ue ) {
977 std::cerr << "Usb::Error caught: " << ue.what() << endl;
978 return 1;
980 catch( Barry::Error &se ) {
981 std::cerr << "Barry::Error caught: " << se.what() << endl;
982 return 1;
984 catch( std::exception &e ) {
985 std::cerr << "std::exception caught: " << e.what() << endl;
986 return 1;
989 return 0;