Reduced minimum password retry level from 6 to 3
[barry/pauldeden.git] / tools / upldif.cc
blobda541dab228448c8c16b116cee3a6cf488730a7e
1 ///
2 /// \file upldif.cc
3 /// LDIF contact uploader
4 ///
6 /*
7 Copyright (C) 2006-2008, 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 #include <iomanip>
24 #include <iostream>
25 #include <fstream>
26 #include <vector>
27 #include <string>
28 #include <getopt.h>
31 using namespace std;
32 using namespace Barry;
34 void Usage()
36 cerr
37 << "upldif - Command line LDIF uploader\n"
38 << " Copyright 2006-2008, Net Direct Inc. (http://www.netdirect.ca/)\n\n"
39 << " -p pin PIN of device to talk with\n"
40 << " If only one device plugged in, this flag is optional\n"
41 << " -u Do the upload. If not specified, only dumps parsed\n"
42 << " LDIF data to stdout.\n"
43 << " -v Dump protocol data during operation\n"
44 << " -h This help output\n"
45 << endl;
48 template <class Record>
49 struct Store
51 std::vector<Record> records;
52 mutable typename std::vector<Record>::const_iterator rec_it;
53 int count;
55 Barry::ContactLdif ldif;
57 // Store constructor -- reads LDIF records from the given
58 // stream object and stores them in memory.
59 Store(std::istream &is)
60 : count(0),
61 ldif("")
63 Record rec;
64 while( is ) {
65 if( ldif.ReadLdif(is, rec) ) {
66 count++;
67 records.push_back(rec);
71 rec_it = records.begin();
74 ~Store()
76 cout << "Store counted " << dec << count << " records." << endl;
79 // Retrieval operator -- called by Barry during the upload
80 // process to get the next object
81 bool operator()(Record &rec, unsigned int databaseId) const
83 if( rec_it == records.end() )
84 return false;
85 rec = *rec_it;
86 rec_it++;
87 return true;
90 // For easy data display and debugging.
91 void Dump(std::ostream &os) const
93 typename std::vector<Record>::const_iterator b = records.begin();
94 for( ; b != records.end(); ++b ) {
95 os << *b << endl;
100 template <class Record>
101 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
103 store.Dump(os);
104 return os;
107 int main(int argc, char *argv[])
109 cout.sync_with_stdio(true); // leave this on, since libusb uses
110 // stdio for debug messages
112 try {
114 uint32_t pin = 0;
115 bool data_dump = false,
116 do_upload = false;
118 // process command line options
119 for(;;) {
120 int cmd = getopt(argc, argv, "hp:uv");
121 if( cmd == -1 )
122 break;
124 switch( cmd )
126 case 'p': // Blackberry PIN
127 pin = strtoul(optarg, NULL, 16);
128 break;
130 case 'u': // do upload
131 do_upload = true;
132 break;
134 case 'v': // data dump on
135 data_dump = true;
136 break;
138 case 'h': // help
139 default:
140 Usage();
141 return 0;
145 // Read all contacts from stdin
146 Store<Contact> contactStore(cin);
148 // Only dump to stdout if not uploading to device
149 if( !do_upload ) {
150 cout << contactStore << endl;
151 return 0;
154 // Initialize the barry library. Must be called before
155 // anything else.
156 Barry::Init(data_dump);
158 // Probe the USB bus for Blackberry devices
159 // If user has specified a PIN, search for it
160 Barry::Probe probe;
161 int activeDevice = probe.FindActive(pin);
162 if( activeDevice == -1 ) {
163 cerr << "Device not found, or not specified" << endl;
164 return 1;
167 // Create our controller object
168 Barry::Controller con(probe.Get(activeDevice));
170 // make sure we're in desktop mode
171 Barry::Mode::Desktop desktop(con);
172 desktop.Open();
174 // upload all records to device
175 desktop.SaveDatabaseByType<Barry::Contact>(contactStore);
178 catch( Usb::Error &ue) {
179 std::cerr << "Usb::Error caught: " << ue.what() << endl;
181 catch( Barry::Error &se ) {
182 std::cerr << "Barry::Error caught: " << se.what() << endl;
184 catch( std::exception &e ) {
185 std::cerr << "std::exception caught: " << e.what() << endl;
186 return 1;
189 return 0;