debian: bumped Standards-Version to 3.9.3
[barry/progweb.git] / tools / btool.cc
blob5684fbdae28910b13f6d3235b703827b44184254
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
7 Copyright (C) 2005-2012, 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.
23 // This define is used in barry/barry.h to signal inclusion of Boost
24 // serialization headers. It is intended to be used by applications,
25 // so we shouldn't mess with it.
27 // But all actual Boost related code is now stuffed into util.cc, safely
28 // locked away from other code. So we don't need the Boost headers, we
29 // just need a flag for our own functionality. So translate this define
30 // into our own, and undef to skip the Boost headers, and the compile speed
31 // slowdown that it creates.
33 #ifdef __BARRY_BOOST_MODE__
34 #define __BTOOL_BOOST_MODE__
35 #endif
36 #undef __BARRY_BOOST_MODE__
38 #include <barry/barry.h>
39 #ifdef __BARRY_SYNC_MODE__
40 #include <barry/barrysync.h>
41 #include "mimedump.h"
42 #endif
43 #ifdef __BARRY_BACKUP_MODE__
44 #include <barry/barrybackup.h>
45 #endif
47 #include <iomanip>
48 #include <iostream>
49 #include <fstream>
50 #include <sstream>
51 #include <vector>
52 #include <string>
53 #include <algorithm>
54 #include <stdlib.h>
55 #include <tr1/memory>
56 #include "i18n.h"
57 #include "util.h"
58 #include "boostwrap.h"
60 #include "barrygetopt.h"
62 using namespace std;
63 using namespace std::tr1;
64 using namespace Barry;
66 std::map<std::string, std::string> SortKeys;
68 void Usage()
70 int logical, major, minor;
71 const char *Version = Barry::Version(logical, major, minor);
73 cerr
74 << "btool - Command line USB Blackberry Test Tool\n"
75 << " Copyright 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
76 << " Using: " << Version << "\n"
77 << " Compiled "
78 #ifdef __BTOOL_BOOST_MODE__
79 << "with"
80 #else
81 << "without"
82 #endif
83 << " Boost support\n"
84 << "\n"
85 << " -b file Filename to save or load a Barry Backup to (tar.gz)\n"
86 << " -B bus Specify which USB bus to search on\n"
87 << " -N dev Specify which system device, using system specific string\n"
88 << "\n"
89 << " -a db Erase / clear database 'db' FROM device, deleting all\n"
90 << " its records. Can be used multiple times to clear more\n"
91 << " than one DB.\n"
92 << " -c dn Convert address book database to LDIF format, using the\n"
93 << " specified baseDN\n"
94 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
95 << " Defaults to 'cn'\n"
96 << " -d db Load database 'db' FROM device and dump to screen\n"
97 << " Can be used multiple times to fetch more than one DB\n"
98 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
99 << " string separated by a comma, holding the read,write\n"
100 << " endpoint pair. Example: -e 83,5\n"
101 << " Note: Endpoints are specified in hex.\n"
102 << " You should never need to use this option.\n"
103 #ifdef __BTOOL_BOOST_MODE__
104 << " -f file Filename to save or load handheld data to/from\n"
105 #endif
106 << " -F sort Field name by which to sort the output. Note that the\n"
107 << " format of this field is special: 'DBName:field1,field2'\n"
108 << " with no spaces unless the spaces are part of the name.\n"
109 << " Can be used multiple times, to match your -d options.\n"
110 << " Example: -F 'Address Book:Company,LastName,FirstName'\n"
111 << " -h This help\n"
112 << " -i cs International charset for string conversions\n"
113 << " Valid values here are available with 'iconv --list'\n"
114 << " -I Sort records before output\n"
115 << " -l List devices\n"
116 << " -L List Contact field names\n"
117 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
118 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
119 << " Unmap: ldif name alone\n"
120 << " -M List current LDIF mapping\n"
121 << " -n Use null parser on all databases.\n"
122 << " -p pin PIN of device to talk with\n"
123 << " If only one device is plugged in, this flag is optional\n"
124 << " -P pass Simplistic method to specify device password\n"
125 << " -s db Save database 'db' TO device from data loaded from -f file\n"
126 << " -S Show list of supported database parsers. Use twice to\n"
127 << " display fields names as well.\n"
128 << " -t Show database database table\n"
129 << " -T db Show record state table for given database\n"
130 << " -v Dump protocol data during operation\n"
131 #ifdef __BARRY_SYNC_MODE__
132 << " -V Dump records using MIME vformats where possible\n"
133 #endif
134 << " -X Reset device\n"
135 << " -z Use non-threaded sockets\n"
136 << " -Z Use threaded socket router (default)\n"
137 << "\n"
138 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
139 << "\n"
140 << " -r # Record index number as seen in the -T state table.\n"
141 << " This overrides the default -d behaviour, and only\n"
142 << " downloads the one specified record, sending to stdout.\n"
143 << " -R # Same as -r, but also clears the record's dirty flags.\n"
144 << " -D # Record index number as seen in the -T state table,\n"
145 << " which indicates the record to delete. Used with the -d\n"
146 << " command to specify the database.\n"
147 << endl;
150 class Contact2Ldif
152 public:
153 Barry::ContactLdif &ldif;
155 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
157 void operator()(const Contact &rec)
159 ldif.DumpLdif(cout, rec);
163 template <class Record>
164 struct Store
166 std::vector<Record> records;
167 mutable typename std::vector<Record>::const_iterator rec_it;
168 std::string filename;
169 bool load;
170 bool immediate_display;
171 bool vformat_mode;
172 int from_device_count;
173 mutable int to_device_count;
175 Store(const string &filename, bool load, bool immediate_display,
176 bool vformat_mode)
177 : rec_it(records.end()),
178 filename(filename),
179 load(load),
180 immediate_display(immediate_display && !SortKeys.size()),
181 vformat_mode(vformat_mode),
182 from_device_count(0),
183 to_device_count(0)
185 #ifdef __BTOOL_BOOST_MODE__
186 if( load && filename.size() ) {
187 // filename is available, attempt to load
188 cout << "Loading: " << filename << endl;
189 string errmsg, dbName;
190 if( !LoadBoostFile(filename, records, dbName, errmsg) ) {
191 cerr << errmsg << endl;
193 cout << records.size()
194 << " records loaded from '"
195 << filename << "'" << endl;
196 sort(records.begin(), records.end());
197 rec_it = records.begin();
199 // debugging aid
200 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
201 for( ; beg != end; beg++ ) {
202 cout << (*beg) << endl;
205 #endif
208 ~Store()
210 if( !immediate_display ) {
211 // not dumped yet, sort then dump
212 if( SortKeys.size() && SortKeys.find(Record::GetDBName()) != SortKeys.end() ) {
213 sort(records.begin(), records.end(),
214 NamedFieldCmp<Record>(SortKeys[Record::GetDBName()]));
216 else {
217 sort(records.begin(), records.end());
219 DumpAll();
222 cout << "Store counted " << dec << from_device_count << " records read from device, and " << dec << to_device_count << " records written to device." << endl;
223 #ifdef __BTOOL_BOOST_MODE__
224 if( !load && filename.size() ) {
225 // filename is available, attempt to save
226 cout << "Saving: " << filename << endl;
227 string errmsg;
228 if( !SaveBoostFile(filename, records, errmsg) ) {
229 cerr << errmsg << endl;
231 cout << dec << records.size() << " records saved to '"
232 << filename << "'" << endl;
234 #endif
237 void DumpAll()
239 typename vector<Record>::const_iterator i = records.begin();
240 for( ; i != records.end(); ++i ) {
241 Dump(*i);
245 void Dump(const Record &rec)
247 if( vformat_mode ) {
248 #ifdef __BARRY_SYNC_MODE__
249 MimeDump<Record> md;
250 md.Dump(cout, rec);
251 #endif
253 else {
254 cout << rec << endl;
258 // storage operator
259 void operator()(const Record &rec)
261 from_device_count++;
262 if( immediate_display )
263 Dump(rec);
264 records.push_back(rec);
267 // retrieval operator
268 bool operator()(Record &rec, Builder &builder) const
270 if( rec_it == records.end() )
271 return false;
272 to_device_count++;
273 rec = *rec_it;
274 rec_it++;
275 return true;
279 shared_ptr<Parser> GetParser(const string &name,
280 const string &filename,
281 bool null_parser,
282 bool immediate_display,
283 bool vformat_mode,
284 bool bbackup_mode)
286 bool dnow = immediate_display;
287 bool vmode = vformat_mode;
289 if( null_parser ) {
290 // use null parser
291 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
293 else if( bbackup_mode ) {
294 #ifdef __BARRY_BACKUP_MODE__
295 // Only one backup file per run
296 static shared_ptr<Parser> backup;
297 if( !backup.get() ) {
298 backup.reset( new Backup(filename) );
300 return backup;
301 #else
302 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
303 #endif
305 // check for recognized database names
306 else if( name == Contact::GetDBName() ) {
307 return shared_ptr<Parser>(
308 new RecordParser<Contact, Store<Contact> > (
309 new Store<Contact>(filename, false, dnow, vmode)));
311 else if( name == Message::GetDBName() ) {
312 return shared_ptr<Parser>(
313 new RecordParser<Message, Store<Message> > (
314 new Store<Message>(filename, false, dnow, vmode)));
316 else if( name == Calendar::GetDBName() ) {
317 return shared_ptr<Parser>(
318 new RecordParser<Calendar, Store<Calendar> > (
319 new Store<Calendar>(filename, false, dnow, vmode)));
321 else if( name == CalendarAll::GetDBName() ) {
322 return shared_ptr<Parser>(
323 new RecordParser<CalendarAll, Store<CalendarAll> > (
324 new Store<CalendarAll>(filename, false, dnow, vmode)));
326 else if( name == CallLog::GetDBName() ) {
327 return shared_ptr<Parser>(
328 new RecordParser<CallLog, Store<CallLog> > (
329 new Store<CallLog>(filename, false, dnow, vmode)));
331 else if( name == Bookmark::GetDBName() ) {
332 return shared_ptr<Parser>(
333 new RecordParser<Bookmark, Store<Bookmark> > (
334 new Store<Bookmark>(filename, false, dnow, vmode)));
336 else if( name == ServiceBook::GetDBName() ) {
337 return shared_ptr<Parser>(
338 new RecordParser<ServiceBook, Store<ServiceBook> > (
339 new Store<ServiceBook>(filename, false, dnow, vmode)));
342 else if( name == Memo::GetDBName() ) {
343 return shared_ptr<Parser>(
344 new RecordParser<Memo, Store<Memo> > (
345 new Store<Memo>(filename, false, dnow, vmode)));
347 else if( name == Task::GetDBName() ) {
348 return shared_ptr<Parser>(
349 new RecordParser<Task, Store<Task> > (
350 new Store<Task>(filename, false, dnow, vmode)));
352 else if( name == PINMessage::GetDBName() ) {
353 return shared_ptr<Parser>(
354 new RecordParser<PINMessage, Store<PINMessage> > (
355 new Store<PINMessage>(filename, false, dnow, vmode)));
357 else if( name == SavedMessage::GetDBName() ) {
358 return shared_ptr<Parser>(
359 new RecordParser<SavedMessage, Store<SavedMessage> > (
360 new Store<SavedMessage>(filename, false, dnow, vmode)));
362 else if( name == Sms::GetDBName() ) {
363 return shared_ptr<Parser>(
364 new RecordParser<Sms, Store<Sms> > (
365 new Store<Sms>(filename, false, dnow, vmode)));
367 else if( name == Folder::GetDBName() ) {
368 return shared_ptr<Parser>(
369 new RecordParser<Folder, Store<Folder> > (
370 new Store<Folder>(filename, false, dnow, vmode)));
372 else if( name == TimeZone::GetDBName() ) {
373 return shared_ptr<Parser>(
374 new RecordParser<TimeZone, Store<TimeZone> > (
375 new Store<TimeZone>(filename, false, dnow, vmode)));
377 else if( name == HandheldAgent::GetDBName() ) {
378 return shared_ptr<Parser>(
379 new RecordParser<HandheldAgent, Store<HandheldAgent> > (
380 new Store<HandheldAgent>(filename, false, dnow, vmode)));
382 else {
383 // unknown database, use null parser
384 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
388 shared_ptr<Builder> GetBuilder(const string &name, const string &filename)
390 // check for recognized database names
391 if( name == Contact::GetDBName() ) {
392 return shared_ptr<Builder>(
393 new RecordBuilder<Contact, Store<Contact> > (
394 new Store<Contact>(filename, true, true, false)));
396 else if( name == Calendar::GetDBName() ) {
397 return shared_ptr<Builder>(
398 new RecordBuilder<Calendar, Store<Calendar> > (
399 new Store<Calendar>(filename, true, true, false)));
401 else if( name == CalendarAll::GetDBName() ) {
402 return shared_ptr<Builder>(
403 new RecordBuilder<CalendarAll, Store<CalendarAll> > (
404 new Store<CalendarAll>(filename, true, true, false)));
406 else if( name == Memo::GetDBName() ) {
407 return shared_ptr<Builder>(
408 new RecordBuilder<Memo, Store<Memo> > (
409 new Store<Memo>(filename, true, true, false)));
411 else if( name == Task::GetDBName() ) {
412 return shared_ptr<Builder>(
413 new RecordBuilder<Task, Store<Task> > (
414 new Store<Task>(filename, true, true, false)));
417 else if( name == "Messages" ) {
418 return shared_ptr<Parser>(
419 new RecordParser<Message, Store<Message> > (
420 new Store<Message>(filename, true, true, false)));
422 else if( name == "Service Book" ) {
423 return shared_ptr<Parser>(
424 new RecordParser<ServiceBook, Store<ServiceBook> > (
425 new Store<ServiceBook>(filename, true, true, false)));
428 else {
429 throw std::runtime_error("No Builder available for database");
433 struct StateTableCommand
435 char flag;
436 bool clear;
437 unsigned int index;
439 StateTableCommand(char f, bool c, unsigned int i)
440 : flag(f), clear(c), index(i) {}
443 bool SplitMap(const string &map, string &ldif, string &read, string &write)
445 string::size_type a = map.find(',');
446 if( a == string::npos )
447 return false;
449 string::size_type b = map.find(',', a+1);
450 if( b == string::npos )
451 return false;
453 ldif.assign(map, 0, a);
454 read.assign(map, a + 1, b - a - 1);
455 write.assign(map, b + 1, map.size() - b - 1);
457 return ldif.size() && read.size() && write.size();
460 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
462 for( vector<string>::const_iterator i = mapCommands.begin();
463 i != mapCommands.end();
464 ++i )
466 // single names mean unmapping
467 if( i->find(',') == string::npos ) {
468 // unmap
469 cerr << "Unmapping: " << *i << endl;
470 ldif.Unmap(*i);
472 else {
473 cerr << "Mapping: " << *i << endl;
475 // map... extract ldif/read/write names
476 string ldifname, read, write;
477 if( SplitMap(*i, ldifname, read, write) ) {
478 if( !ldif.Map(ldifname, read, write) ) {
479 cerr << "Read/Write name unknown: " << *i << endl;
482 else {
483 cerr << "Invalid map format: " << *i << endl;
489 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
491 int read, write;
492 char comma;
493 istringstream iss(arg);
494 iss >> hex >> read >> comma >> write;
495 if( !iss )
496 return false;
497 epp->read = read;
498 epp->write = write;
499 return true;
502 void ParseSortKey(const std::string &key)
504 istringstream iss(key);
505 string db, spec;
506 getline(iss, db, ':');
507 getline(iss, spec, ':');
509 if( db.size() && spec.size() )
510 SortKeys[db] = spec;
513 int main(int argc, char *argv[])
515 INIT_I18N(PACKAGE);
517 cout.sync_with_stdio(true); // leave this on, since libusb uses
518 // stdio for debug messages
520 try {
522 uint32_t pin = 0;
523 bool list_only = false,
524 show_dbdb = false,
525 ldif_contacts = false,
526 data_dump = false,
527 vformat_mode = false,
528 reset_device = false,
529 list_contact_fields = false,
530 list_ldif_map = false,
531 epp_override = false,
532 threaded_sockets = true,
533 record_state_table = false,
534 clear_database = false,
535 null_parser = false,
536 bbackup_mode = false,
537 sort_records = false,
538 show_parsers = false,
539 show_fields = false;
540 string ldifBaseDN, ldifDnAttr;
541 string filename;
542 string password;
543 string busname;
544 string devname;
545 string iconvCharset;
546 vector<string> dbNames, saveDbNames, mapCommands, clearDbNames;
547 vector<StateTableCommand> stCommands;
548 Usb::EndpointPair epOverride;
550 // process command line options
551 for(;;) {
552 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");
553 if( cmd == -1 )
554 break;
556 switch( cmd )
558 case 'a': // Clear Database
559 clear_database = true;
560 clearDbNames.push_back(string(optarg));
561 break;
563 case 'b': // Barry backup filename (tar.gz)
564 #ifdef __BARRY_BACKUP_MODE__
565 if( filename.size() == 0 ) {
566 filename = optarg;
567 bbackup_mode = true;
569 else {
570 cerr << "Do not use -f with -b\n";
571 return 1;
573 #else
574 cerr << "-b option not supported - no Barry "
575 "Backup library support available\n";
576 return 1;
577 #endif
578 break;
580 case 'B': // busname
581 busname = optarg;
582 break;
584 case 'c': // contacts to ldap ldif
585 ldif_contacts = true;
586 ldifBaseDN = optarg;
587 break;
589 case 'C': // DN Attribute for FQDN
590 ldifDnAttr = optarg;
591 break;
593 case 'd': // show dbname
594 dbNames.push_back(string(optarg));
595 break;
597 case 'D': // delete record
598 stCommands.push_back(
599 StateTableCommand('D', false, atoi(optarg)));
600 break;
602 case 'e': // endpoint override
603 if( !ParseEpOverride(optarg, &epOverride) ) {
604 Usage();
605 return 1;
607 epp_override = true;
608 break;
610 case 'f': // filename
611 #ifdef __BTOOL_BOOST_MODE__
612 if( !bbackup_mode && filename.size() == 0 ) {
613 filename = optarg;
615 else {
616 cerr << "Do not use -f with -b\n";
617 return 1;
619 #else
620 cerr << "-f option not supported - no Boost "
621 "serialization support available\n";
622 return 1;
623 #endif
624 break;
626 case 'F': // sort key
627 ParseSortKey(optarg);
628 break;
630 case 'i': // international charset (iconv)
631 iconvCharset = optarg;
632 break;
634 case 'I': // sort before dump
635 sort_records = true;
636 break;
638 case 'l': // list only
639 list_only = true;
640 break;
642 case 'L': // List Contact field names
643 list_contact_fields = true;
644 break;
646 case 'm': // Map / Unmap
647 mapCommands.push_back(string(optarg));
648 break;
650 case 'M': // List LDIF map
651 list_ldif_map = true;
652 break;
654 case 'n': // use null parser
655 null_parser = true;
656 break;
658 case 'N': // Devname
659 devname = optarg;
660 break;
662 case 'p': // Blackberry PIN
663 pin = strtoul(optarg, NULL, 16);
664 break;
666 case 'P': // Device password
667 password = optarg;
668 break;
670 case 'r': // get specific record index
671 stCommands.push_back(
672 StateTableCommand('r', false, atoi(optarg)));
673 break;
675 case 'R': // same as 'r', and clears dirty
676 stCommands.push_back(
677 StateTableCommand('r', true, atoi(optarg)));
678 break;
680 case 's': // save dbname
681 saveDbNames.push_back(string(optarg));
682 break;
684 case 'S': // show supported databases
685 if( show_parsers )
686 show_fields = true;
687 else
688 show_parsers = true;
689 break;
691 case 't': // display database database
692 show_dbdb = true;
693 break;
695 case 'T': // show RecordStateTable
696 record_state_table = true;
697 dbNames.push_back(string(optarg));
698 break;
700 case 'v': // data dump on
701 data_dump = true;
702 break;
704 case 'V': // vformat MIME mode
705 #ifdef __BARRY_SYNC_MODE__
706 vformat_mode = true;
707 #else
708 cerr << "-V option not supported - no Sync "
709 "library support available\n";
710 return 1;
711 #endif
712 break;
714 case 'X': // reset device
715 reset_device = true;
716 break;
718 case 'z': // non-threaded sockets
719 threaded_sockets = false;
720 break;
722 case 'Z': // threaded socket router
723 threaded_sockets = true;
724 break;
726 case 'h': // help
727 default:
728 Usage();
729 return 0;
733 if( show_parsers ) {
734 ShowParsers(show_fields, true);
735 ShowBuilders();
736 return 0;
739 // Initialize the barry library. Must be called before
740 // anything else.
741 Barry::Init(data_dump);
742 if( data_dump ) {
743 int logical, major, minor;
744 const char *Version = Barry::Version(logical, major, minor);
745 cout << Version << endl;
748 // Create an IConverter object if needed
749 auto_ptr<IConverter> ic;
750 if( iconvCharset.size() ) {
751 ic.reset( new IConverter(iconvCharset.c_str(), true) );
754 // LDIF class... only needed if ldif output turned on
755 ContactLdif ldif(ldifBaseDN);
756 DoMapping(ldif, mapCommands);
757 if( ldifDnAttr.size() ) {
758 if( !ldif.SetDNAttr(ldifDnAttr) ) {
759 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
763 // Probe the USB bus for Blackberry devices and display.
764 // If user has specified a PIN, search for it in the
765 // available device list here as well
766 Barry::Probe probe(busname.c_str(), devname.c_str(),
767 epp_override ? &epOverride : 0);
768 int activeDevice = -1;
770 // show any errors during probe first
771 if( probe.GetFailCount() ) {
772 if( ldif_contacts )
773 cout << "# ";
774 cout << "Blackberry device errors with errors during probe:" << endl;
775 for( int i = 0; i < probe.GetFailCount(); i++ ) {
776 if( ldif_contacts )
777 cout << "# ";
778 cout << probe.GetFailMsg(i) << endl;
782 // show all successfully found devices
783 if( ldif_contacts )
784 cout << "# ";
785 cout << "Blackberry devices found:" << endl;
786 for( int i = 0; i < probe.GetCount(); i++ ) {
787 if( ldif_contacts )
788 cout << "# ";
789 if( data_dump )
790 probe.Get(i).DumpAll(cout);
791 else
792 cout << probe.Get(i);
793 cout << endl;
794 if( probe.Get(i).m_pin == pin )
795 activeDevice = i;
798 if( list_only )
799 return 0; // done
801 if( activeDevice == -1 ) {
802 if( pin == 0 ) {
803 // can we default to single device?
804 if( probe.GetCount() == 1 )
805 activeDevice = 0;
806 else {
807 cerr << "No device selected" << endl;
808 return 1;
811 else {
812 cerr << "PIN " << setbase(16) << pin
813 << " not found" << endl;
814 return 1;
818 if( ldif_contacts )
819 cout << "# ";
820 cout << "Using device (PIN): "
821 << probe.Get(activeDevice).m_pin.Str() << endl;
823 if( reset_device ) {
824 Usb::Device dev(probe.Get(activeDevice).m_dev);
825 dev.Reset();
826 return 0;
829 // Override device endpoints if user asks
830 Barry::ProbeResult device = probe.Get(activeDevice);
831 if( epp_override ) {
832 device.m_ep.read = epOverride.read;
833 device.m_ep.write = epOverride.write;
834 // FIXME - override this too?
835 device.m_ep.type = Usb::EndpointDescriptor::BulkType;
836 cout << "Endpoint pair (read,write) overridden with: "
837 << hex
838 << (unsigned int) device.m_ep.read << ","
839 << (unsigned int) device.m_ep.write << endl;
843 // execute each mode that was turned on
847 // Dump current LDIF mapping
848 if( list_ldif_map ) {
849 cout << ldif << endl;
852 // Dump list of Contact field names
853 if( list_contact_fields ) {
854 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
855 cout.fill(' ');
856 cout << " " << left << setw(20) << n->name << ": "
857 << n->description << endl;
861 // Check if Desktop access is needed
862 if( !( show_dbdb ||
863 ldif_contacts ||
864 record_state_table ||
865 clear_database ||
866 stCommands.size() ||
867 dbNames.size() ||
868 saveDbNames.size() ) )
869 return 0; // done
872 // Create our controller object
874 // Order is important in the following auto_ptr<> objects,
875 // since Controller must get destroyed before router.
876 // Normally you'd pick one method, and not bother
877 // with auto_ptr<> and so the normal C++ constructor
878 // rules would guarantee this safety for you, but
879 // here we want the user to pick.
881 auto_ptr<SocketRoutingQueue> router;
882 if( threaded_sockets ) {
883 router.reset( new SocketRoutingQueue );
884 router->SpinoffSimpleReadThread();
887 DesktopConnector connector(password.c_str(),
888 iconvCharset, device, router.get());
889 if( !connector.Connect() ) {
890 // bad password (default action is not to prompt)
891 cerr << connector.GetBadPassword().what() << endl;
892 return 1;
895 Barry::Mode::Desktop &desktop = connector.GetDesktop();
897 // Dump list of all databases to stdout
898 if( show_dbdb ) {
899 // open desktop mode socket
900 cout << desktop.GetDBDB() << endl;
903 // Dump list of contacts to an LDAP LDIF file
904 // This uses the Controller convenience templates
905 if( ldif_contacts ) {
906 // create a storage functor object that accepts
907 // Barry::Contact objects as input
908 Contact2Ldif storage(ldif);
910 // load all the Contact records into storage
911 desktop.LoadDatabaseByType<Barry::Contact>(storage);
914 // Dump record state table to stdout
915 if( record_state_table ) {
916 if( dbNames.size() == 0 ) {
917 cout << "No db names to process" << endl;
918 return 1;
921 vector<string>::iterator b = dbNames.begin();
922 for( ; b != dbNames.end(); b++ ) {
923 unsigned int id = desktop.GetDBID(*b);
924 RecordStateTable state;
925 desktop.GetRecordStateTable(id, state);
926 cout << "Record state table for: " << *b << endl;
927 cout << state;
929 return 0;
932 // Get Record mode overrides the default name mode
933 if( stCommands.size() ) {
934 if( dbNames.size() != 1 ) {
935 cout << "Must have 1 db name to process" << endl;
936 return 1;
939 unsigned int id = desktop.GetDBID(dbNames[0]);
940 shared_ptr<Parser> parse = GetParser(dbNames[0],filename,
941 null_parser, true, vformat_mode, bbackup_mode);
943 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
944 desktop.GetRecord(id, stCommands[i].index, *parse.get());
946 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
947 cout << "Clearing record's dirty flags..." << endl;
948 desktop.ClearDirty(id, stCommands[i].index);
951 if( stCommands[i].flag == 'D' ) {
952 desktop.DeleteRecord(id, stCommands[i].index);
956 return 0;
959 // Dump contents of selected databases to stdout, or
960 // to file if specified.
961 // This is retrieving data from the Blackberry.
962 if( dbNames.size() ) {
963 vector<string>::iterator b = dbNames.begin();
965 for( ; b != dbNames.end(); b++ ) {
966 shared_ptr<Parser> parse = GetParser(*b,
967 filename, null_parser, !sort_records,
968 vformat_mode, bbackup_mode);
969 unsigned int id = desktop.GetDBID(*b);
970 desktop.LoadDatabase(id, *parse.get());
974 // Clear databases
975 if( clear_database ) {
976 if( clearDbNames.size() == 0 ) {
977 cout << "No db names to erase" << endl;
978 return 1;
981 vector<string>::iterator b = clearDbNames.begin();
983 for( ; b != clearDbNames.end(); b++ ) {
984 unsigned int id = desktop.GetDBID(*b);
985 cout << "Deleting all records from " << (*b) << "..." << endl;
986 desktop.ClearDatabase(id);
989 return 0;
992 // Save contents of file to specified databases
993 // This is writing data to the Blackberry.
994 if( saveDbNames.size() ) {
995 vector<string>::iterator b = saveDbNames.begin();
997 for( ; b != saveDbNames.end(); b++ ) {
998 shared_ptr<Builder> build = GetBuilder(*b,
999 filename);
1000 unsigned int id = desktop.GetDBID(*b);
1001 desktop.SaveDatabase(id, *build);
1006 catch( Usb::Error &ue ) {
1007 std::cerr << "Usb::Error caught: " << ue.what() << endl;
1008 return 1;
1010 catch( Barry::Error &se ) {
1011 std::cerr << "Barry::Error caught: " << se.what() << endl;
1012 return 1;
1014 catch( std::exception &e ) {
1015 std::cerr << "std::exception caught: " << e.what() << endl;
1016 return 1;
1019 return 0;