lib: fixed parsing of recurring VEVENTS: DAILY and interval support
[barry/progweb.git] / tools / btool.cc
blobc798722e81a9b539f95da43747cec26605be661a
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 #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 cerr
73 << "btool - Command line USB Blackberry Test Tool\n"
74 << " Copyright 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
75 << " Using: " << Version << "\n"
76 << " Compiled "
77 #ifdef __BTOOL_BOOST_MODE__
78 << "with"
79 #else
80 << "without"
81 #endif
82 << " Boost support\n"
83 << "\n"
84 << " -b file Filename to save or load a Barry Backup to (tar.gz)\n"
85 << " -B bus Specify which USB bus to search on\n"
86 << " -N dev Specify which system device, using system specific string\n"
87 << "\n"
88 << " -a db Erase / clear database 'db' FROM device, deleting all\n"
89 << " its records. Can be used multiple times to clear more\n"
90 << " than one DB.\n"
91 << " -c dn Convert address book database to LDIF format, using the\n"
92 << " specified baseDN\n"
93 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
94 << " Defaults to 'cn'\n"
95 << " -d db Load database 'db' FROM device and dump to screen\n"
96 << " Can be used multiple times to fetch more than one DB\n"
97 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
98 << " string separated by a comma, holding the read,write\n"
99 << " endpoint pair. Example: -e 83,5\n"
100 << " Note: Endpoints are specified in hex.\n"
101 << " You should never need to use this option.\n"
102 #ifdef __BTOOL_BOOST_MODE__
103 << " -f file Filename to save or load handheld data to/from\n"
104 #endif
105 << " -F sort Field name by which to sort the output. Note that the\n"
106 << " format of this field is special: 'DBName:field1,field2'\n"
107 << " with no spaces unless the spaces are part of the name.\n"
108 << " Can be used multiple times, to match your -d options.\n"
109 << " Example: -F 'Address Book:Company,LastName,FirstName'\n"
110 << " -h This help\n"
111 << " -i cs International charset for string conversions\n"
112 << " Valid values here are available with 'iconv --list'\n"
113 << " -I Sort records before output\n"
114 << " -l List devices\n"
115 << " -L List Contact field names\n"
116 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
117 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
118 << " Unmap: ldif name alone\n"
119 << " -M List current LDIF mapping\n"
120 << " -n Use null parser on all databases.\n"
121 << " -p pin PIN of device to talk with\n"
122 << " If only one device is plugged in, this flag is optional\n"
123 << " -P pass Simplistic method to specify device password\n"
124 << " -s db Save database 'db' TO device from data loaded from -f file\n"
125 << " -S Show list of supported database parsers. Use twice to\n"
126 << " display fields names as well.\n"
127 << " -t Show database database table\n"
128 << " -T db Show record state table for given database\n"
129 << " -v Dump protocol data during operation\n"
130 #ifdef __BARRY_SYNC_MODE__
131 << " -V Dump records using MIME vformats where possible\n"
132 #endif
133 << " -X Reset device\n"
134 << " -z Use non-threaded sockets\n"
135 << " -Z Use threaded socket router (default)\n"
136 << "\n"
137 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
138 << "\n"
139 << " -r # Record index number as seen in the -T state table.\n"
140 << " This overrides the default -d behaviour, and only\n"
141 << " downloads the one specified record, sending to stdout.\n"
142 << " -R # Same as -r, but also clears the record's dirty flags.\n"
143 << " -D # Record index number as seen in the -T state table,\n"
144 << " which indicates the record to delete. Used with the -d\n"
145 << " command to specify the database.\n"
146 << endl;
149 class Contact2Ldif
151 public:
152 Barry::ContactLdif &ldif;
154 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
156 void operator()(const Contact &rec)
158 ldif.DumpLdif(cout, rec);
162 template <class Record>
163 struct Store
165 std::vector<Record> records;
166 mutable typename std::vector<Record>::const_iterator rec_it;
167 std::string filename;
168 bool load;
169 bool immediate_display;
170 bool vformat_mode;
171 int from_device_count;
172 mutable int to_device_count;
174 Store(const string &filename, bool load, bool immediate_display,
175 bool vformat_mode)
176 : rec_it(records.end()),
177 filename(filename),
178 load(load),
179 immediate_display(immediate_display && !SortKeys.size()),
180 vformat_mode(vformat_mode),
181 from_device_count(0),
182 to_device_count(0)
184 #ifdef __BTOOL_BOOST_MODE__
185 if( load && filename.size() ) {
186 // filename is available, attempt to load
187 cout << "Loading: " << filename << endl;
188 string errmsg, dbName;
189 if( !LoadBoostFile(filename, records, dbName, errmsg) ) {
190 cerr << errmsg << endl;
192 cout << records.size()
193 << " records loaded from '"
194 << filename << "'" << endl;
195 sort(records.begin(), records.end());
196 rec_it = records.begin();
198 // debugging aid
199 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
200 for( ; beg != end; beg++ ) {
201 cout << (*beg) << endl;
204 #endif
207 ~Store()
209 if( !immediate_display ) {
210 // not dumped yet, sort then dump
211 if( SortKeys.size() && SortKeys.find(Record::GetDBName()) != SortKeys.end() ) {
212 sort(records.begin(), records.end(),
213 NamedFieldCmp<Record>(SortKeys[Record::GetDBName()]));
215 else {
216 sort(records.begin(), records.end());
218 DumpAll();
221 cout << "Store counted " << dec << from_device_count << " records read from device, and " << dec << to_device_count << " records written to device." << endl;
222 #ifdef __BTOOL_BOOST_MODE__
223 if( !load && filename.size() ) {
224 // filename is available, attempt to save
225 cout << "Saving: " << filename << endl;
226 string errmsg;
227 if( !SaveBoostFile(filename, records, errmsg) ) {
228 cerr << errmsg << endl;
230 cout << dec << records.size() << " records saved to '"
231 << filename << "'" << endl;
233 #endif
236 void DumpAll()
238 typename vector<Record>::const_iterator i = records.begin();
239 for( ; i != records.end(); ++i ) {
240 Dump(*i);
244 void Dump(const Record &rec)
246 if( vformat_mode ) {
247 #ifdef __BARRY_SYNC_MODE__
248 MimeDump<Record> md;
249 md.Dump(cout, rec);
250 #endif
252 else {
253 cout << rec << endl;
257 // storage operator
258 void operator()(const Record &rec)
260 from_device_count++;
261 if( immediate_display )
262 Dump(rec);
263 records.push_back(rec);
266 // retrieval operator
267 bool operator()(Record &rec, Builder &builder) const
269 if( rec_it == records.end() )
270 return false;
271 to_device_count++;
272 rec = *rec_it;
273 rec_it++;
274 return true;
278 shared_ptr<Parser> GetParser(const string &name,
279 const string &filename,
280 bool null_parser,
281 bool immediate_display,
282 bool vformat_mode,
283 bool bbackup_mode)
285 bool dnow = immediate_display;
286 bool vmode = vformat_mode;
288 if( null_parser ) {
289 // use null parser
290 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
292 else if( bbackup_mode ) {
293 #ifdef __BARRY_BACKUP_MODE__
294 // Only one backup file per run
295 static shared_ptr<Parser> backup;
296 if( !backup.get() ) {
297 backup.reset( new Backup(filename) );
299 return backup;
300 #else
301 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
302 #endif
304 // check for recognized database names
305 else if( name == Contact::GetDBName() ) {
306 return shared_ptr<Parser>(
307 new RecordParser<Contact, Store<Contact> > (
308 new Store<Contact>(filename, false, dnow, vmode)));
310 else if( name == Message::GetDBName() ) {
311 return shared_ptr<Parser>(
312 new RecordParser<Message, Store<Message> > (
313 new Store<Message>(filename, false, dnow, vmode)));
315 else if( name == Calendar::GetDBName() ) {
316 return shared_ptr<Parser>(
317 new RecordParser<Calendar, Store<Calendar> > (
318 new Store<Calendar>(filename, false, dnow, vmode)));
320 else if( name == CalendarAll::GetDBName() ) {
321 return shared_ptr<Parser>(
322 new RecordParser<CalendarAll, Store<CalendarAll> > (
323 new Store<CalendarAll>(filename, false, dnow, vmode)));
325 else if( name == CallLog::GetDBName() ) {
326 return shared_ptr<Parser>(
327 new RecordParser<CallLog, Store<CallLog> > (
328 new Store<CallLog>(filename, false, dnow, vmode)));
330 else if( name == Bookmark::GetDBName() ) {
331 return shared_ptr<Parser>(
332 new RecordParser<Bookmark, Store<Bookmark> > (
333 new Store<Bookmark>(filename, false, dnow, vmode)));
335 else if( name == ServiceBook::GetDBName() ) {
336 return shared_ptr<Parser>(
337 new RecordParser<ServiceBook, Store<ServiceBook> > (
338 new Store<ServiceBook>(filename, false, dnow, vmode)));
341 else if( name == Memo::GetDBName() ) {
342 return shared_ptr<Parser>(
343 new RecordParser<Memo, Store<Memo> > (
344 new Store<Memo>(filename, false, dnow, vmode)));
346 else if( name == Task::GetDBName() ) {
347 return shared_ptr<Parser>(
348 new RecordParser<Task, Store<Task> > (
349 new Store<Task>(filename, false, dnow, vmode)));
351 else if( name == PINMessage::GetDBName() ) {
352 return shared_ptr<Parser>(
353 new RecordParser<PINMessage, Store<PINMessage> > (
354 new Store<PINMessage>(filename, false, dnow, vmode)));
356 else if( name == SavedMessage::GetDBName() ) {
357 return shared_ptr<Parser>(
358 new RecordParser<SavedMessage, Store<SavedMessage> > (
359 new Store<SavedMessage>(filename, false, dnow, vmode)));
361 else if( name == Sms::GetDBName() ) {
362 return shared_ptr<Parser>(
363 new RecordParser<Sms, Store<Sms> > (
364 new Store<Sms>(filename, false, dnow, vmode)));
366 else if( name == Folder::GetDBName() ) {
367 return shared_ptr<Parser>(
368 new RecordParser<Folder, Store<Folder> > (
369 new Store<Folder>(filename, false, dnow, vmode)));
371 else if( name == TimeZone::GetDBName() ) {
372 return shared_ptr<Parser>(
373 new RecordParser<TimeZone, Store<TimeZone> > (
374 new Store<TimeZone>(filename, false, dnow, vmode)));
376 else if( name == HandheldAgent::GetDBName() ) {
377 return shared_ptr<Parser>(
378 new RecordParser<HandheldAgent, Store<HandheldAgent> > (
379 new Store<HandheldAgent>(filename, false, dnow, vmode)));
381 else {
382 // unknown database, use null parser
383 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
387 shared_ptr<Builder> GetBuilder(const string &name, const string &filename)
389 // check for recognized database names
390 if( name == Contact::GetDBName() ) {
391 return shared_ptr<Builder>(
392 new RecordBuilder<Contact, Store<Contact> > (
393 new Store<Contact>(filename, true, true, false)));
395 else if( name == Calendar::GetDBName() ) {
396 return shared_ptr<Builder>(
397 new RecordBuilder<Calendar, Store<Calendar> > (
398 new Store<Calendar>(filename, true, true, false)));
400 else if( name == CalendarAll::GetDBName() ) {
401 return shared_ptr<Builder>(
402 new RecordBuilder<CalendarAll, Store<CalendarAll> > (
403 new Store<CalendarAll>(filename, true, true, false)));
405 else if( name == Memo::GetDBName() ) {
406 return shared_ptr<Builder>(
407 new RecordBuilder<Memo, Store<Memo> > (
408 new Store<Memo>(filename, true, true, false)));
410 else if( name == Task::GetDBName() ) {
411 return shared_ptr<Builder>(
412 new RecordBuilder<Task, Store<Task> > (
413 new Store<Task>(filename, true, true, false)));
416 else if( name == "Messages" ) {
417 return shared_ptr<Parser>(
418 new RecordParser<Message, Store<Message> > (
419 new Store<Message>(filename, true, true, false)));
421 else if( name == "Service Book" ) {
422 return shared_ptr<Parser>(
423 new RecordParser<ServiceBook, Store<ServiceBook> > (
424 new Store<ServiceBook>(filename, true, true, false)));
427 else {
428 throw std::runtime_error("No Builder available for database");
432 struct StateTableCommand
434 char flag;
435 bool clear;
436 unsigned int index;
438 StateTableCommand(char f, bool c, unsigned int i)
439 : flag(f), clear(c), index(i) {}
442 bool SplitMap(const string &map, string &ldif, string &read, string &write)
444 string::size_type a = map.find(',');
445 if( a == string::npos )
446 return false;
448 string::size_type b = map.find(',', a+1);
449 if( b == string::npos )
450 return false;
452 ldif.assign(map, 0, a);
453 read.assign(map, a + 1, b - a - 1);
454 write.assign(map, b + 1, map.size() - b - 1);
456 return ldif.size() && read.size() && write.size();
459 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
461 for( vector<string>::const_iterator i = mapCommands.begin();
462 i != mapCommands.end();
463 ++i )
465 // single names mean unmapping
466 if( i->find(',') == string::npos ) {
467 // unmap
468 cerr << "Unmapping: " << *i << endl;
469 ldif.Unmap(*i);
471 else {
472 cerr << "Mapping: " << *i << endl;
474 // map... extract ldif/read/write names
475 string ldifname, read, write;
476 if( SplitMap(*i, ldifname, read, write) ) {
477 if( !ldif.Map(ldifname, read, write) ) {
478 cerr << "Read/Write name unknown: " << *i << endl;
481 else {
482 cerr << "Invalid map format: " << *i << endl;
488 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
490 int read, write;
491 char comma;
492 istringstream iss(arg);
493 iss >> hex >> read >> comma >> write;
494 if( !iss )
495 return false;
496 epp->read = read;
497 epp->write = write;
498 return true;
501 void ParseSortKey(const std::string &key)
503 istringstream iss(key);
504 string db, spec;
505 getline(iss, db, ':');
506 getline(iss, spec, ':');
508 if( db.size() && spec.size() )
509 SortKeys[db] = spec;
512 int main(int argc, char *argv[])
514 INIT_I18N(PACKAGE);
516 cout.sync_with_stdio(true); // leave this on, since libusb uses
517 // stdio for debug messages
519 try {
521 uint32_t pin = 0;
522 bool list_only = false,
523 show_dbdb = false,
524 ldif_contacts = false,
525 data_dump = false,
526 vformat_mode = false,
527 reset_device = false,
528 list_contact_fields = false,
529 list_ldif_map = false,
530 epp_override = false,
531 threaded_sockets = true,
532 record_state_table = false,
533 clear_database = false,
534 null_parser = false,
535 bbackup_mode = false,
536 sort_records = false,
537 show_parsers = false,
538 show_fields = false;
539 string ldifBaseDN, ldifDnAttr;
540 string filename;
541 string password;
542 string busname;
543 string devname;
544 string iconvCharset;
545 vector<string> dbNames, saveDbNames, mapCommands, clearDbNames;
546 vector<StateTableCommand> stCommands;
547 Usb::EndpointPair epOverride;
549 // process command line options
550 for(;;) {
551 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");
552 if( cmd == -1 )
553 break;
555 switch( cmd )
557 case 'a': // Clear Database
558 clear_database = true;
559 clearDbNames.push_back(string(optarg));
560 break;
562 case 'b': // Barry backup filename (tar.gz)
563 #ifdef __BARRY_BACKUP_MODE__
564 if( filename.size() == 0 ) {
565 filename = optarg;
566 bbackup_mode = true;
568 else {
569 cerr << "Do not use -f with -b\n";
570 return 1;
572 #else
573 cerr << "-b option not supported - no Barry "
574 "Backup library support available\n";
575 return 1;
576 #endif
577 break;
579 case 'B': // busname
580 busname = optarg;
581 break;
583 case 'c': // contacts to ldap ldif
584 ldif_contacts = true;
585 ldifBaseDN = optarg;
586 break;
588 case 'C': // DN Attribute for FQDN
589 ldifDnAttr = optarg;
590 break;
592 case 'd': // show dbname
593 dbNames.push_back(string(optarg));
594 break;
596 case 'D': // delete record
597 stCommands.push_back(
598 StateTableCommand('D', false, atoi(optarg)));
599 break;
601 case 'e': // endpoint override
602 if( !ParseEpOverride(optarg, &epOverride) ) {
603 Usage();
604 return 1;
606 epp_override = true;
607 break;
609 case 'f': // filename
610 #ifdef __BTOOL_BOOST_MODE__
611 if( !bbackup_mode && filename.size() == 0 ) {
612 filename = optarg;
614 else {
615 cerr << "Do not use -f with -b\n";
616 return 1;
618 #else
619 cerr << "-f option not supported - no Boost "
620 "serialization support available\n";
621 return 1;
622 #endif
623 break;
625 case 'F': // sort key
626 ParseSortKey(optarg);
627 break;
629 case 'i': // international charset (iconv)
630 iconvCharset = optarg;
631 break;
633 case 'I': // sort before dump
634 sort_records = true;
635 break;
637 case 'l': // list only
638 list_only = true;
639 break;
641 case 'L': // List Contact field names
642 list_contact_fields = true;
643 break;
645 case 'm': // Map / Unmap
646 mapCommands.push_back(string(optarg));
647 break;
649 case 'M': // List LDIF map
650 list_ldif_map = true;
651 break;
653 case 'n': // use null parser
654 null_parser = true;
655 break;
657 case 'N': // Devname
658 devname = optarg;
659 break;
661 case 'p': // Blackberry PIN
662 pin = strtoul(optarg, NULL, 16);
663 break;
665 case 'P': // Device password
666 password = optarg;
667 break;
669 case 'r': // get specific record index
670 stCommands.push_back(
671 StateTableCommand('r', false, atoi(optarg)));
672 break;
674 case 'R': // same as 'r', and clears dirty
675 stCommands.push_back(
676 StateTableCommand('r', true, atoi(optarg)));
677 break;
679 case 's': // save dbname
680 saveDbNames.push_back(string(optarg));
681 break;
683 case 'S': // show supported databases
684 if( show_parsers )
685 show_fields = true;
686 else
687 show_parsers = true;
688 break;
690 case 't': // display database database
691 show_dbdb = true;
692 break;
694 case 'T': // show RecordStateTable
695 record_state_table = true;
696 dbNames.push_back(string(optarg));
697 break;
699 case 'v': // data dump on
700 data_dump = true;
701 break;
703 case 'V': // vformat MIME mode
704 #ifdef __BARRY_SYNC_MODE__
705 vformat_mode = true;
706 #else
707 cerr << "-V option not supported - no Sync "
708 "library support available\n";
709 return 1;
710 #endif
711 break;
713 case 'X': // reset device
714 reset_device = true;
715 break;
717 case 'z': // non-threaded sockets
718 threaded_sockets = false;
719 break;
721 case 'Z': // threaded socket router
722 threaded_sockets = true;
723 break;
725 case 'h': // help
726 default:
727 Usage();
728 return 0;
732 if( show_parsers ) {
733 ShowParsers(show_fields, true);
734 ShowBuilders();
735 return 0;
738 // Initialize the barry library. Must be called before
739 // anything else.
740 Barry::Init(data_dump);
741 if( data_dump ) {
742 int logical, major, minor;
743 const char *Version = Barry::Version(logical, major, minor);
744 cout << Version << endl;
747 // Create an IConverter object if needed
748 auto_ptr<IConverter> ic;
749 if( iconvCharset.size() ) {
750 ic.reset( new IConverter(iconvCharset.c_str(), true) );
753 // LDIF class... only needed if ldif output turned on
754 ContactLdif ldif(ldifBaseDN);
755 DoMapping(ldif, mapCommands);
756 if( ldifDnAttr.size() ) {
757 if( !ldif.SetDNAttr(ldifDnAttr) ) {
758 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
762 // Probe the USB bus for Blackberry devices and display.
763 // If user has specified a PIN, search for it in the
764 // available device list here as well
765 Barry::Probe probe(busname.c_str(), devname.c_str(),
766 epp_override ? &epOverride : 0);
767 int activeDevice = -1;
769 // show any errors during probe first
770 if( probe.GetFailCount() ) {
771 if( ldif_contacts )
772 cout << "# ";
773 cout << "Blackberry device errors with errors during probe:" << endl;
774 for( int i = 0; i < probe.GetFailCount(); i++ ) {
775 if( ldif_contacts )
776 cout << "# ";
777 cout << probe.GetFailMsg(i) << endl;
781 // show all successfully found devices
782 if( ldif_contacts )
783 cout << "# ";
784 cout << "Blackberry devices found:" << endl;
785 for( int i = 0; i < probe.GetCount(); i++ ) {
786 if( ldif_contacts )
787 cout << "# ";
788 if( data_dump )
789 probe.Get(i).DumpAll(cout);
790 else
791 cout << probe.Get(i);
792 cout << endl;
793 if( probe.Get(i).m_pin == pin )
794 activeDevice = i;
797 if( list_only )
798 return 0; // done
800 if( activeDevice == -1 ) {
801 if( pin == 0 ) {
802 // can we default to single device?
803 if( probe.GetCount() == 1 )
804 activeDevice = 0;
805 else {
806 cerr << "No device selected" << endl;
807 return 1;
810 else {
811 cerr << "PIN " << setbase(16) << pin
812 << " not found" << endl;
813 return 1;
817 if( ldif_contacts )
818 cout << "# ";
819 cout << "Using device (PIN): "
820 << probe.Get(activeDevice).m_pin.Str() << endl;
822 if( reset_device ) {
823 Usb::Device dev(probe.Get(activeDevice).m_dev);
824 dev.Reset();
825 return 0;
828 // Override device endpoints if user asks
829 Barry::ProbeResult device = probe.Get(activeDevice);
830 if( epp_override ) {
831 device.m_ep.read = epOverride.read;
832 device.m_ep.write = epOverride.write;
833 // FIXME - override this too?
834 device.m_ep.type = Usb::EndpointDescriptor::BulkType;
835 cout << "Endpoint pair (read,write) overridden with: "
836 << hex
837 << (unsigned int) device.m_ep.read << ","
838 << (unsigned int) device.m_ep.write << endl;
842 // execute each mode that was turned on
846 // Dump current LDIF mapping
847 if( list_ldif_map ) {
848 cout << ldif << endl;
851 // Dump list of Contact field names
852 if( list_contact_fields ) {
853 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
854 cout.fill(' ');
855 cout << " " << left << setw(20) << n->name << ": "
856 << n->description << endl;
860 // Check if Desktop access is needed
861 if( !( show_dbdb ||
862 ldif_contacts ||
863 record_state_table ||
864 clear_database ||
865 stCommands.size() ||
866 dbNames.size() ||
867 saveDbNames.size() ) )
868 return 0; // done
871 // Create our controller object
873 // Order is important in the following auto_ptr<> objects,
874 // since Controller must get destroyed before router.
875 // Normally you'd pick one method, and not bother
876 // with auto_ptr<> and so the normal C++ constructor
877 // rules would guarantee this safety for you, but
878 // here we want the user to pick.
880 auto_ptr<SocketRoutingQueue> router;
881 if( threaded_sockets ) {
882 router.reset( new SocketRoutingQueue );
883 router->SpinoffSimpleReadThread();
886 DesktopConnector connector(password.c_str(),
887 iconvCharset, device, router.get());
888 if( !connector.Connect() ) {
889 // bad password (default action is not to prompt)
890 cerr << connector.GetBadPassword().what() << endl;
891 return 1;
894 Barry::Mode::Desktop &desktop = connector.GetDesktop();
896 // Dump list of all databases to stdout
897 if( show_dbdb ) {
898 // open desktop mode socket
899 cout << desktop.GetDBDB() << endl;
902 // Dump list of contacts to an LDAP LDIF file
903 // This uses the Controller convenience templates
904 if( ldif_contacts ) {
905 // create a storage functor object that accepts
906 // Barry::Contact objects as input
907 Contact2Ldif storage(ldif);
909 // load all the Contact records into storage
910 desktop.LoadDatabaseByType<Barry::Contact>(storage);
913 // Dump record state table to stdout
914 if( record_state_table ) {
915 if( dbNames.size() == 0 ) {
916 cout << "No db names to process" << endl;
917 return 1;
920 vector<string>::iterator b = dbNames.begin();
921 for( ; b != dbNames.end(); b++ ) {
922 unsigned int id = desktop.GetDBID(*b);
923 RecordStateTable state;
924 desktop.GetRecordStateTable(id, state);
925 cout << "Record state table for: " << *b << endl;
926 cout << state;
928 return 0;
931 // Get Record mode overrides the default name mode
932 if( stCommands.size() ) {
933 if( dbNames.size() != 1 ) {
934 cout << "Must have 1 db name to process" << endl;
935 return 1;
938 unsigned int id = desktop.GetDBID(dbNames[0]);
939 shared_ptr<Parser> parse = GetParser(dbNames[0],filename,
940 null_parser, true, vformat_mode, bbackup_mode);
942 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
943 desktop.GetRecord(id, stCommands[i].index, *parse.get());
945 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
946 cout << "Clearing record's dirty flags..." << endl;
947 desktop.ClearDirty(id, stCommands[i].index);
950 if( stCommands[i].flag == 'D' ) {
951 desktop.DeleteRecord(id, stCommands[i].index);
955 return 0;
958 // Dump contents of selected databases to stdout, or
959 // to file if specified.
960 // This is retrieving data from the Blackberry.
961 if( dbNames.size() ) {
962 vector<string>::iterator b = dbNames.begin();
964 for( ; b != dbNames.end(); b++ ) {
965 shared_ptr<Parser> parse = GetParser(*b,
966 filename, null_parser, !sort_records,
967 vformat_mode, bbackup_mode);
968 unsigned int id = desktop.GetDBID(*b);
969 desktop.LoadDatabase(id, *parse.get());
973 // Clear databases
974 if( clear_database ) {
975 if( clearDbNames.size() == 0 ) {
976 cout << "No db names to erase" << endl;
977 return 1;
980 vector<string>::iterator b = clearDbNames.begin();
982 for( ; b != clearDbNames.end(); b++ ) {
983 unsigned int id = desktop.GetDBID(*b);
984 cout << "Deleting all records from " << (*b) << "..." << endl;
985 desktop.ClearDatabase(id);
988 return 0;
991 // Save contents of file to specified databases
992 // This is writing data to the Blackberry.
993 if( saveDbNames.size() ) {
994 vector<string>::iterator b = saveDbNames.begin();
996 for( ; b != saveDbNames.end(); b++ ) {
997 shared_ptr<Builder> build = GetBuilder(*b,
998 filename);
999 unsigned int id = desktop.GetDBID(*b);
1000 desktop.SaveDatabase(id, *build);
1005 catch( Usb::Error &ue ) {
1006 std::cerr << "Usb::Error caught: " << ue.what() << endl;
1007 return 1;
1009 catch( Barry::Error &se ) {
1010 std::cerr << "Barry::Error caught: " << se.what() << endl;
1011 return 1;
1013 catch( std::exception &e ) {
1014 std::cerr << "std::exception caught: " << e.what() << endl;
1015 return 1;
1018 return 0;