Barry debian version 0.18.5-1
[barry.git] / tools / btool.cc
blob7933a6b611c793efb8b5ebaa176625ba63c5b261
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
7 Copyright (C) 2005-2013, 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 #endif
42 #ifdef __BARRY_BACKUP_MODE__
43 #include <barry/barrybackup.h>
44 #endif
46 #include <iomanip>
47 #include <iostream>
48 #include <fstream>
49 #include <sstream>
50 #include <vector>
51 #include <string>
52 #include <algorithm>
53 #include <stdlib.h>
54 #include <tr1/memory>
55 #include "i18n.h"
56 #include "util.h"
57 #include "boostwrap.h"
59 #include "barrygetopt.h"
61 using namespace std;
62 using namespace std::tr1;
63 using namespace Barry;
65 std::map<std::string, std::string> SortKeys;
67 void Usage()
69 int logical, major, minor;
70 const char *Version = Barry::Version(logical, major, minor);
72 #ifdef __BTOOL_BOOST_MODE__
73 string boost_mode = _("Compiled with Boost support");
74 string boost_opt = _(" -f file Filename to save or load handheld data to/from");
75 #else
76 string boost_mode = _("Compiled without Boost support");
77 string boost_opt;
78 #endif
80 #ifdef __BARRY_SYNC_MODE__
81 string sync_mode = _(" -V Dump records using MIME vformats where possible");
82 #else
83 string sync_mode;
84 #endif
86 cerr << string_vprintf(
87 _("btool - Command line USB Blackberry Test Tool\n"
88 " Copyright 2005-2013, Net Direct Inc. (http://www.netdirect.ca/)\n"
89 " Using: %s\n"
90 " %s\n"
91 "\n"
92 " -b file Filename to save or load a Barry Backup to (tar.gz)\n"
93 " -B bus Specify which USB bus to search on\n"
94 " -N dev Specify which system device, using system specific string\n"
95 "\n"
96 " -a db Erase / clear database 'db' FROM device, deleting all\n"
97 " its records. Can be used multiple times to clear more\n"
98 " than one DB.\n"
99 " -c dn Convert address book database to LDIF format, using the\n"
100 " specified baseDN\n"
101 " -C dnattr LDIF attribute name to use when building the FQDN\n"
102 " Defaults to 'cn'\n"
103 " -d db Load database 'db' FROM device and dump to screen\n"
104 " Can be used multiple times to fetch more than one DB\n"
105 " -e epp Override endpoint pair detection. 'epp' is a single\n"
106 " string separated by a comma, holding the read,write\n"
107 " endpoint pair. Example: -e 83,5\n"
108 " Note: Endpoints are specified in hex.\n"
109 " You should never need to use this option.\n"
110 "%s\n"
111 " -F sort Field name by which to sort the output. Note that the\n"
112 " format of this field is special: 'DBName:field1,field2'\n"
113 " with no spaces unless the spaces are part of the name.\n"
114 " Can be used multiple times, to match your -d options.\n"
115 " Example: -F 'Address Book:Company,LastName,FirstName'\n"
116 " -h This help\n"
117 " -i cs International charset for string conversions\n"
118 " Valid values here are available with 'iconv --list'\n"
119 " -I Sort records before output\n"
120 " -l List devices\n"
121 " -L List Contact field names\n"
122 " -m Map LDIF name to Contact field / Unmap LDIF name\n"
123 " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
124 " Unmap: ldif name alone\n"
125 " -M List current LDIF mapping\n"
126 " -n Use null parser on all databases.\n"
127 " -p pin PIN of device to talk with\n"
128 " If only one device is plugged in, this flag is optional\n"
129 " -P pass Simplistic method to specify device password\n"
130 " -s db Save database 'db' TO device from data loaded from -f file\n"
131 " -S Show list of supported database parsers. Use twice to\n"
132 " display fields names as well.\n"
133 " -t Show database database table\n"
134 " -T db Show record state table for given database\n"
135 " -v Dump protocol data during operation\n"
136 "%s\n"
137 " -X Reset device\n"
138 " -z Use non-threaded sockets\n"
139 " -Z Use threaded socket router (default)\n"
140 "\n"
141 " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
142 "\n"
143 " -r # Record index number as seen in the -T state table.\n"
144 " This overrides the default -d behaviour, and only\n"
145 " downloads the one specified record, sending to stdout.\n"
146 " -R # Same as -r, but also clears the record's dirty flags.\n"
147 " -D # Record index number as seen in the -T state table,\n"
148 " which indicates the record to delete. Used with the -d\n"
149 " command to specify the database.\n"),
150 Version,
151 boost_mode.c_str(),
152 boost_opt.c_str(),
153 sync_mode.c_str()
155 << endl;
158 class Contact2Ldif
160 public:
161 Barry::ContactLdif &ldif;
163 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
165 void operator()(const Contact &rec)
167 ldif.DumpLdif(cout, rec);
171 template <class Record>
172 struct Store
174 std::vector<Record> records;
175 mutable typename std::vector<Record>::const_iterator rec_it;
176 std::string filename;
177 bool load;
178 bool immediate_display;
179 bool vformat_mode;
180 int from_device_count;
181 mutable int to_device_count;
183 Store(const string &filename, bool load, bool immediate_display,
184 bool vformat_mode)
185 : rec_it(records.end()),
186 filename(filename),
187 load(load),
188 immediate_display(immediate_display && !SortKeys.size()),
189 vformat_mode(vformat_mode),
190 from_device_count(0),
191 to_device_count(0)
193 #ifdef __BTOOL_BOOST_MODE__
194 if( load && filename.size() ) {
195 // filename is available, attempt to load
196 cout << _("Loading: ") << filename << endl;
197 string errmsg, dbName;
198 if( !LoadBoostFile(filename, records, dbName, errmsg) ) {
199 cerr << errmsg << endl;
201 cout << records.size()
202 << _(" records loaded from '")
203 << filename << "'" << endl;
204 sort(records.begin(), records.end());
205 rec_it = records.begin();
207 // debugging aid
208 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
209 for( ; beg != end; beg++ ) {
210 cout << (*beg) << endl;
213 #endif
216 ~Store()
218 if( !immediate_display ) {
219 // not dumped yet, sort then dump
220 if( SortKeys.size() && SortKeys.find(Record::GetDBName()) != SortKeys.end() ) {
221 sort(records.begin(), records.end(),
222 NamedFieldCmp<Record>(SortKeys[Record::GetDBName()]));
224 else {
225 sort(records.begin(), records.end());
227 DumpAll();
230 cout << string_vprintf(_("Store counted %d records read from device, and %d records written to device."), from_device_count, to_device_count) << endl;
231 #ifdef __BTOOL_BOOST_MODE__
232 if( !load && filename.size() ) {
233 // filename is available, attempt to save
234 cout << _("Saving: ") << filename << endl;
235 string errmsg;
236 if( !SaveBoostFile(filename, records, errmsg) ) {
237 cerr << errmsg << endl;
239 cout << dec << records.size() << _(" records saved to '")
240 << filename << "'" << endl;
242 #endif
245 void DumpAll()
247 typename vector<Record>::const_iterator i = records.begin();
248 for( ; i != records.end(); ++i ) {
249 Dump(*i);
253 void Dump(const Record &rec)
255 if( vformat_mode ) {
256 #ifdef __BARRY_SYNC_MODE__
257 MimeDump<Record> md;
258 md.Dump(cout, rec);
259 #endif
261 else {
262 cout << rec << endl;
266 // storage operator
267 void operator()(const Record &rec)
269 from_device_count++;
270 if( immediate_display )
271 Dump(rec);
272 records.push_back(rec);
275 // retrieval operator
276 bool operator()(Record &rec, Builder &builder) const
278 if( rec_it == records.end() )
279 return false;
280 to_device_count++;
281 rec = *rec_it;
282 rec_it++;
283 return true;
287 shared_ptr<Parser> GetParser(const string &name,
288 const string &filename,
289 bool null_parser,
290 bool immediate_display,
291 bool vformat_mode,
292 bool bbackup_mode)
294 bool dnow = immediate_display;
295 bool vmode = vformat_mode;
297 if( null_parser ) {
298 // use null parser
299 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
301 else if( bbackup_mode ) {
302 #ifdef __BARRY_BACKUP_MODE__
303 // Only one backup file per run
304 static shared_ptr<Parser> backup;
305 if( !backup.get() ) {
306 backup.reset( new Backup(filename) );
308 return backup;
309 #else
310 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
311 #endif
313 // check for recognized database names
314 else if( name == Contact::GetDBName() ) {
315 return shared_ptr<Parser>(
316 new RecordParser<Contact, Store<Contact> > (
317 new Store<Contact>(filename, false, dnow, vmode)));
319 else if( name == Message::GetDBName() ) {
320 return shared_ptr<Parser>(
321 new RecordParser<Message, Store<Message> > (
322 new Store<Message>(filename, false, dnow, vmode)));
324 else if( name == Calendar::GetDBName() ) {
325 return shared_ptr<Parser>(
326 new RecordParser<Calendar, Store<Calendar> > (
327 new Store<Calendar>(filename, false, dnow, vmode)));
329 else if( name == CalendarAll::GetDBName() ) {
330 return shared_ptr<Parser>(
331 new RecordParser<CalendarAll, Store<CalendarAll> > (
332 new Store<CalendarAll>(filename, false, dnow, vmode)));
334 else if( name == CallLog::GetDBName() ) {
335 return shared_ptr<Parser>(
336 new RecordParser<CallLog, Store<CallLog> > (
337 new Store<CallLog>(filename, false, dnow, vmode)));
339 else if( name == Bookmark::GetDBName() ) {
340 return shared_ptr<Parser>(
341 new RecordParser<Bookmark, Store<Bookmark> > (
342 new Store<Bookmark>(filename, false, dnow, vmode)));
344 else if( name == ServiceBook::GetDBName() ) {
345 return shared_ptr<Parser>(
346 new RecordParser<ServiceBook, Store<ServiceBook> > (
347 new Store<ServiceBook>(filename, false, dnow, vmode)));
350 else if( name == Memo::GetDBName() ) {
351 return shared_ptr<Parser>(
352 new RecordParser<Memo, Store<Memo> > (
353 new Store<Memo>(filename, false, dnow, vmode)));
355 else if( name == Task::GetDBName() ) {
356 return shared_ptr<Parser>(
357 new RecordParser<Task, Store<Task> > (
358 new Store<Task>(filename, false, dnow, vmode)));
360 else if( name == PINMessage::GetDBName() ) {
361 return shared_ptr<Parser>(
362 new RecordParser<PINMessage, Store<PINMessage> > (
363 new Store<PINMessage>(filename, false, dnow, vmode)));
365 else if( name == SavedMessage::GetDBName() ) {
366 return shared_ptr<Parser>(
367 new RecordParser<SavedMessage, Store<SavedMessage> > (
368 new Store<SavedMessage>(filename, false, dnow, vmode)));
370 else if( name == Sms::GetDBName() ) {
371 return shared_ptr<Parser>(
372 new RecordParser<Sms, Store<Sms> > (
373 new Store<Sms>(filename, false, dnow, vmode)));
375 else if( name == Folder::GetDBName() ) {
376 return shared_ptr<Parser>(
377 new RecordParser<Folder, Store<Folder> > (
378 new Store<Folder>(filename, false, dnow, vmode)));
380 else if( name == TimeZone::GetDBName() ) {
381 return shared_ptr<Parser>(
382 new RecordParser<TimeZone, Store<TimeZone> > (
383 new Store<TimeZone>(filename, false, dnow, vmode)));
385 else if( name == HandheldAgent::GetDBName() ) {
386 return shared_ptr<Parser>(
387 new RecordParser<HandheldAgent, Store<HandheldAgent> > (
388 new Store<HandheldAgent>(filename, false, dnow, vmode)));
390 else {
391 // unknown database, use null parser
392 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
396 shared_ptr<Builder> GetBuilder(const string &name, const string &filename)
398 // check for recognized database names
399 if( name == Contact::GetDBName() ) {
400 return shared_ptr<Builder>(
401 new RecordBuilder<Contact, Store<Contact> > (
402 new Store<Contact>(filename, true, true, false)));
404 else if( name == Calendar::GetDBName() ) {
405 return shared_ptr<Builder>(
406 new RecordBuilder<Calendar, Store<Calendar> > (
407 new Store<Calendar>(filename, true, true, false)));
409 else if( name == CalendarAll::GetDBName() ) {
410 return shared_ptr<Builder>(
411 new RecordBuilder<CalendarAll, Store<CalendarAll> > (
412 new Store<CalendarAll>(filename, true, true, false)));
414 else if( name == Memo::GetDBName() ) {
415 return shared_ptr<Builder>(
416 new RecordBuilder<Memo, Store<Memo> > (
417 new Store<Memo>(filename, true, true, false)));
419 else if( name == Task::GetDBName() ) {
420 return shared_ptr<Builder>(
421 new RecordBuilder<Task, Store<Task> > (
422 new Store<Task>(filename, true, true, false)));
425 else if( name == "Messages" ) {
426 return shared_ptr<Parser>(
427 new RecordParser<Message, Store<Message> > (
428 new Store<Message>(filename, true, true, false)));
430 else if( name == "Service Book" ) {
431 return shared_ptr<Parser>(
432 new RecordParser<ServiceBook, Store<ServiceBook> > (
433 new Store<ServiceBook>(filename, true, true, false)));
436 else {
437 throw std::runtime_error(_("No Builder available for database"));
441 struct StateTableCommand
443 char flag;
444 bool clear;
445 unsigned int index;
447 StateTableCommand(char f, bool c, unsigned int i)
448 : flag(f), clear(c), index(i) {}
451 bool SplitMap(const string &map, string &ldif, string &read, string &write)
453 string::size_type a = map.find(',');
454 if( a == string::npos )
455 return false;
457 string::size_type b = map.find(',', a+1);
458 if( b == string::npos )
459 return false;
461 ldif.assign(map, 0, a);
462 read.assign(map, a + 1, b - a - 1);
463 write.assign(map, b + 1, map.size() - b - 1);
465 return ldif.size() && read.size() && write.size();
468 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
470 for( vector<string>::const_iterator i = mapCommands.begin();
471 i != mapCommands.end();
472 ++i )
474 // single names mean unmapping
475 if( i->find(',') == string::npos ) {
476 // unmap
477 cerr << _("Unmapping: ") << *i << endl;
478 ldif.Unmap(*i);
480 else {
481 cerr << _("Mapping: ") << *i << endl;
483 // map... extract ldif/read/write names
484 string ldifname, read, write;
485 if( SplitMap(*i, ldifname, read, write) ) {
486 if( !ldif.Map(ldifname, read, write) ) {
487 cerr << _("Read/Write name unknown: ") << *i << endl;
490 else {
491 cerr << _("Invalid map format: ") << *i << endl;
497 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
499 int read, write;
500 char comma;
501 istringstream iss(arg);
502 iss >> hex >> read >> comma >> write;
503 if( !iss )
504 return false;
505 epp->read = read;
506 epp->write = write;
507 return true;
510 void ParseSortKey(const std::string &key)
512 istringstream iss(key);
513 string db, spec;
514 getline(iss, db, ':');
515 getline(iss, spec, ':');
517 if( db.size() && spec.size() )
518 SortKeys[db] = spec;
521 int main(int argc, char *argv[])
523 INIT_I18N(PACKAGE);
525 cout.sync_with_stdio(true); // leave this on, since libusb uses
526 // stdio for debug messages
528 try {
530 uint32_t pin = 0;
531 bool list_only = false,
532 show_dbdb = false,
533 ldif_contacts = false,
534 data_dump = false,
535 vformat_mode = false,
536 reset_device = false,
537 list_contact_fields = false,
538 list_ldif_map = false,
539 epp_override = false,
540 threaded_sockets = true,
541 record_state_table = false,
542 clear_database = false,
543 null_parser = false,
544 bbackup_mode = false,
545 sort_records = false,
546 show_parsers = false,
547 show_fields = false;
548 string ldifBaseDN, ldifDnAttr;
549 string filename;
550 string password;
551 string busname;
552 string devname;
553 string iconvCharset;
554 vector<string> dbNames, saveDbNames, mapCommands, clearDbNames;
555 vector<StateTableCommand> stCommands;
556 Usb::EndpointPair epOverride;
558 // process command line options
559 for(;;) {
560 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");
561 if( cmd == -1 )
562 break;
564 switch( cmd )
566 case 'a': // Clear Database
567 clear_database = true;
568 clearDbNames.push_back(string(optarg));
569 break;
571 case 'b': // Barry backup filename (tar.gz)
572 #ifdef __BARRY_BACKUP_MODE__
573 if( filename.size() == 0 ) {
574 filename = optarg;
575 bbackup_mode = true;
577 else {
578 cerr << "Do not use -f with -b\n";
579 return 1;
581 #else
582 cerr << _("-b option not supported - no Barry Backup library support available\n");
583 return 1;
584 #endif
585 break;
587 case 'B': // busname
588 busname = optarg;
589 break;
591 case 'c': // contacts to ldap ldif
592 ldif_contacts = true;
593 ldifBaseDN = optarg;
594 break;
596 case 'C': // DN Attribute for FQDN
597 ldifDnAttr = optarg;
598 break;
600 case 'd': // show dbname
601 dbNames.push_back(string(optarg));
602 break;
604 case 'D': // delete record
605 stCommands.push_back(
606 StateTableCommand('D', false, atoi(optarg)));
607 break;
609 case 'e': // endpoint override
610 if( !ParseEpOverride(optarg, &epOverride) ) {
611 Usage();
612 return 1;
614 epp_override = true;
615 break;
617 case 'f': // filename
618 #ifdef __BTOOL_BOOST_MODE__
619 if( !bbackup_mode && filename.size() == 0 ) {
620 filename = optarg;
622 else {
623 cerr << "Do not use -f with -b\n";
624 return 1;
626 #else
627 cerr << _("-f option not supported - no Boost serialization support available\n");
628 return 1;
629 #endif
630 break;
632 case 'F': // sort key
633 ParseSortKey(optarg);
634 break;
636 case 'i': // international charset (iconv)
637 iconvCharset = optarg;
638 break;
640 case 'I': // sort before dump
641 sort_records = true;
642 break;
644 case 'l': // list only
645 list_only = true;
646 break;
648 case 'L': // List Contact field names
649 list_contact_fields = true;
650 break;
652 case 'm': // Map / Unmap
653 mapCommands.push_back(string(optarg));
654 break;
656 case 'M': // List LDIF map
657 list_ldif_map = true;
658 break;
660 case 'n': // use null parser
661 null_parser = true;
662 break;
664 case 'N': // Devname
665 devname = optarg;
666 break;
668 case 'p': // Blackberry PIN
669 pin = strtoul(optarg, NULL, 16);
670 break;
672 case 'P': // Device password
673 password = optarg;
674 break;
676 case 'r': // get specific record index
677 stCommands.push_back(
678 StateTableCommand('r', false, atoi(optarg)));
679 break;
681 case 'R': // same as 'r', and clears dirty
682 stCommands.push_back(
683 StateTableCommand('r', true, atoi(optarg)));
684 break;
686 case 's': // save dbname
687 saveDbNames.push_back(string(optarg));
688 break;
690 case 'S': // show supported databases
691 if( show_parsers )
692 show_fields = true;
693 else
694 show_parsers = true;
695 break;
697 case 't': // display database database
698 show_dbdb = true;
699 break;
701 case 'T': // show RecordStateTable
702 record_state_table = true;
703 dbNames.push_back(string(optarg));
704 break;
706 case 'v': // data dump on
707 data_dump = true;
708 break;
710 case 'V': // vformat MIME mode
711 #ifdef __BARRY_SYNC_MODE__
712 vformat_mode = true;
713 #else
714 cerr << _("-V option not supported - no Sync library support available\n");
715 return 1;
716 #endif
717 break;
719 case 'X': // reset device
720 reset_device = true;
721 break;
723 case 'z': // non-threaded sockets
724 threaded_sockets = false;
725 break;
727 case 'Z': // threaded socket router
728 threaded_sockets = true;
729 break;
731 case 'h': // help
732 default:
733 Usage();
734 return 0;
738 if( show_parsers ) {
739 ShowParsers(show_fields, true);
740 ShowBuilders();
741 return 0;
744 // Initialize the barry library. Must be called before
745 // anything else.
746 Barry::Init(data_dump);
747 if( data_dump ) {
748 int logical, major, minor;
749 const char *Version = Barry::Version(logical, major, minor);
750 cout << Version << endl;
753 // Create an IConverter object if needed
754 auto_ptr<IConverter> ic;
755 if( iconvCharset.size() ) {
756 ic.reset( new IConverter(iconvCharset.c_str(), true) );
759 // LDIF class... only needed if ldif output turned on
760 ContactLdif ldif(ldifBaseDN);
761 DoMapping(ldif, mapCommands);
762 if( ldifDnAttr.size() ) {
763 if( !ldif.SetDNAttr(ldifDnAttr) ) {
764 cerr << _("Unable to set DN Attr: ") << ldifDnAttr << endl;
768 // Probe the USB bus for Blackberry devices and display.
769 // If user has specified a PIN, search for it in the
770 // available device list here as well
771 Barry::Probe probe(busname.c_str(), devname.c_str(),
772 epp_override ? &epOverride : 0);
773 int activeDevice = -1;
775 // show any errors during probe first
776 if( probe.GetFailCount() ) {
777 if( ldif_contacts )
778 cout << "# ";
779 cout << _("Blackberry device errors with errors during probe:") << endl;
780 for( int i = 0; i < probe.GetFailCount(); i++ ) {
781 if( ldif_contacts )
782 cout << "# ";
783 cout << probe.GetFailMsg(i) << endl;
787 // show all successfully found devices
788 if( ldif_contacts )
789 cout << "# ";
790 cout << _("Blackberry devices found:") << endl;
791 for( int i = 0; i < probe.GetCount(); i++ ) {
792 if( ldif_contacts )
793 cout << "# ";
794 if( data_dump )
795 probe.Get(i).DumpAll(cout);
796 else
797 cout << probe.Get(i);
798 cout << endl;
799 if( probe.Get(i).m_pin == pin )
800 activeDevice = i;
803 if( list_only )
804 return 0; // done
806 if( activeDevice == -1 ) {
807 if( pin == 0 ) {
808 // can we default to single device?
809 if( probe.GetCount() == 1 )
810 activeDevice = 0;
811 else {
812 cerr << _("No device selected") << endl;
813 return 1;
816 else {
817 cerr << _("PIN not found: ")
818 << setbase(16) << pin << endl;
819 return 1;
823 if( ldif_contacts )
824 cout << "# ";
825 cout << _("Using device (PIN): ")
826 << probe.Get(activeDevice).m_pin.Str() << endl;
828 if( reset_device ) {
829 Usb::Device dev(probe.Get(activeDevice).m_dev);
830 dev.Reset();
831 return 0;
834 // Override device endpoints if user asks
835 Barry::ProbeResult device = probe.Get(activeDevice);
836 if( epp_override ) {
837 device.m_ep.read = epOverride.read;
838 device.m_ep.write = epOverride.write;
839 // FIXME - override this too?
840 device.m_ep.type = Usb::EndpointDescriptor::BulkType;
841 cout << _("Endpoint pair (read,write) overridden with: ")
842 << hex
843 << (unsigned int) device.m_ep.read << ","
844 << (unsigned int) device.m_ep.write << endl;
848 // execute each mode that was turned on
852 // Dump current LDIF mapping
853 if( list_ldif_map ) {
854 cout << ldif << endl;
857 // Dump list of Contact field names
858 if( list_contact_fields ) {
859 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
860 cout.fill(' ');
861 cout << " " << left << setw(20) << n->name << ": "
862 << n->description << endl;
866 // Check if Desktop access is needed
867 if( !( show_dbdb ||
868 ldif_contacts ||
869 record_state_table ||
870 clear_database ||
871 stCommands.size() ||
872 dbNames.size() ||
873 saveDbNames.size() ) )
874 return 0; // done
877 // Create our controller object
879 // Order is important in the following auto_ptr<> objects,
880 // since Controller must get destroyed before router.
881 // Normally you'd pick one method, and not bother
882 // with auto_ptr<> and so the normal C++ constructor
883 // rules would guarantee this safety for you, but
884 // here we want the user to pick.
886 auto_ptr<SocketRoutingQueue> router;
887 if( threaded_sockets ) {
888 router.reset( new SocketRoutingQueue );
889 router->SpinoffSimpleReadThread();
892 DesktopConnector connector(password.c_str(),
893 iconvCharset, device, router.get());
894 if( !connector.Connect() ) {
895 // bad password (default action is not to prompt)
896 cerr << connector.GetBadPassword().what() << endl;
897 return 1;
900 Barry::Mode::Desktop &desktop = connector.GetDesktop();
902 // Dump list of all databases to stdout
903 if( show_dbdb ) {
904 // open desktop mode socket
905 cout << desktop.GetDBDB() << endl;
908 // Dump list of contacts to an LDAP LDIF file
909 // This uses the Controller convenience templates
910 if( ldif_contacts ) {
911 // create a storage functor object that accepts
912 // Barry::Contact objects as input
913 Contact2Ldif storage(ldif);
915 // load all the Contact records into storage
916 desktop.LoadDatabaseByType<Barry::Contact>(storage);
919 // Dump record state table to stdout
920 if( record_state_table ) {
921 if( dbNames.size() == 0 ) {
922 cout << _("No db names to process") << endl;
923 return 1;
926 vector<string>::iterator b = dbNames.begin();
927 for( ; b != dbNames.end(); b++ ) {
928 unsigned int id = desktop.GetDBID(*b);
929 RecordStateTable state;
930 desktop.GetRecordStateTable(id, state);
931 cout << _("Record state table for: ") << *b << endl;
932 cout << state;
934 return 0;
937 // Get Record mode overrides the default name mode
938 if( stCommands.size() ) {
939 if( dbNames.size() != 1 ) {
940 cout << _("Must have 1 db name to process") << endl;
941 return 1;
944 unsigned int id = desktop.GetDBID(dbNames[0]);
945 shared_ptr<Parser> parse = GetParser(dbNames[0],filename,
946 null_parser, true, vformat_mode, bbackup_mode);
948 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
949 desktop.GetRecord(id, stCommands[i].index, *parse.get());
951 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
952 cout << _("Clearing record's dirty flags...") << endl;
953 desktop.ClearDirty(id, stCommands[i].index);
956 if( stCommands[i].flag == 'D' ) {
957 desktop.DeleteRecord(id, stCommands[i].index);
961 return 0;
964 // Dump contents of selected databases to stdout, or
965 // to file if specified.
966 // This is retrieving data from the Blackberry.
967 if( dbNames.size() ) {
968 vector<string>::iterator b = dbNames.begin();
970 for( ; b != dbNames.end(); b++ ) {
971 shared_ptr<Parser> parse = GetParser(*b,
972 filename, null_parser, !sort_records,
973 vformat_mode, bbackup_mode);
974 unsigned int id = desktop.GetDBID(*b);
975 desktop.LoadDatabase(id, *parse.get());
979 // Clear databases
980 if( clear_database ) {
981 if( clearDbNames.size() == 0 ) {
982 cout << _("No db names to erase") << endl;
983 return 1;
986 vector<string>::iterator b = clearDbNames.begin();
988 for( ; b != clearDbNames.end(); b++ ) {
989 unsigned int id = desktop.GetDBID(*b);
990 cout << _("Deleting all records from ") << (*b) << "..." << endl;
991 desktop.ClearDatabase(id);
994 return 0;
997 // Save contents of file to specified databases
998 // This is writing data to the Blackberry.
999 if( saveDbNames.size() ) {
1000 vector<string>::iterator b = saveDbNames.begin();
1002 for( ; b != saveDbNames.end(); b++ ) {
1003 shared_ptr<Builder> build = GetBuilder(*b,
1004 filename);
1005 unsigned int id = desktop.GetDBID(*b);
1006 desktop.SaveDatabase(id, *build);
1011 catch( Usb::Error &ue ) {
1012 std::cerr << _("Usb::Error caught: ") << ue.what() << endl;
1013 return 1;
1015 catch( Barry::Error &se ) {
1016 std::cerr << _("Barry::Error caught: ") << se.what() << endl;
1017 return 1;
1019 catch( std::exception &e ) {
1020 std::cerr << _("std::exception caught: ") << e.what() << endl;
1021 return 1;
1024 return 0;