Debian Codegraph @

apps/gsmsmsstore.cc

raw symbols SCIP
1 // *************************************************************************
2 // * GSM TA/ME library
3 // *
4 // * File: gsmsmsstore.cc
5 // *
6 // * Purpose: SMS store management program
7 // *
8 // * Author: Peter Hofmann ([email protected])
9 // *
10 // * Created: 4.8.1999
11 // *************************************************************************
12
13 #ifdef HAVE_CONFIG_H
14 #include <gsm_config.h>
15 #endif
16 #include <gsmlib/gsm_nls.h>
17 #include <string>
18 #include <ctype.h>
19 #ifdef WIN32
20 #include <gsmlib/gsm_win32_serial.h>
21 #else
22 #include <gsmlib/gsm_unix_serial.h>
23 #include <unistd.h>
24 #endif
25 #if defined(HAVE_GETOPT_LONG) || defined(WIN32)
26 #include <getopt.h>
27 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <gsmlib/gsm_me_ta.h>
32 #include <gsmlib/gsm_util.h>
33 #include <gsmlib/gsm_sorted_sms_store.h>
34 #include <iostream>
35
36 #ifdef HAVE_GETOPT_LONG
37 static struct option longOpts[] =
38 {
39 {"xonxoff", no_argument, (int*)NULL, 'X'},
40 {"init", required_argument, (int*)NULL, 'I'},
41 {"store", required_argument, (int*)NULL, 't'},
42 {"erase", no_argument, (int*)NULL, 'e'},
43 {"add", no_argument, (int*)NULL, 'a'},
44 {"list", no_argument, (int*)NULL, 'l'},
45 {"destination", required_argument, (int*)NULL, 'd'},
46 {"source", required_argument, (int*)NULL, 's'},
47 {"baudrate", required_argument, (int*)NULL, 'b'},
48 {"sca", required_argument, (int*)NULL, 'C'},
49 {"copy", no_argument, (int*)NULL, 'c'},
50 {"delete", no_argument, (int*)NULL, 'x'},
51 {"backup", no_argument, (int*)NULL, 'k'},
52 {"help", no_argument, (int*)NULL, 'h'},
53 {"version", no_argument, (int*)NULL, 'v'},
54 {"verbose", no_argument, (int*)NULL, 'V'},
55 {(char*)NULL, 0, (int*)NULL, 0}
56 };
57 #else
58 #define getopt_long(argc, argv, options, longopts, indexptr) \
59 getopt(argc, argv, options)
60 #endif
61
62 bool verbose = false; // true if --verbose option given
63
64 // type of operation to perform
65
66 enum Operation {CopyOp = 'c', BackupOp = 'k', DeleteOp = 'x',
67 AddOp = 'a', ListOp = 'l', NoOp = 0};
68
69 // aux function, insert entry only if not already present in dest
70
71 void backup(gsmlib::SortedSMSStoreRef destStore, gsmlib::SMSStoreEntry &entry)
72 {
73 // the following only works because we know that the default sort order
74 // is by date
75 assert(destStore->sortOrder() == gsmlib::ByDate);
76
77 gsmlib::Timestamp date = entry.message()->serviceCentreTimestamp();
78 std::pair<gsmlib::SortedSMSStore::iterator, gsmlib::SortedSMSStore::iterator> range =
79 destStore->equal_range(date);
80
81 for (gsmlib::SortedSMSStore::iterator j = range.first;
82 j != range.second; ++j)
83 if (entry == *j)
84 // do nothing if the entry is already present in the destination
85 return;
86
87 if (verbose)
88 std::cout << gsmlib::stringPrintf(_("inserting entry #%d from source into destination"),
89 entry.index()) << std::endl
90 << entry.message()->toString();
91 destStore->insert(entry); // insert
92 }
93
94 // aux function, throw exception if operation != NoOp
95
96 void checkNoOp(Operation operation, int opt)
97 {
98 if (operation != NoOp)
99 throw gsmlib::GsmException(gsmlib::stringPrintf(_("incompatible options '%c' and '%c'"),
100 (char)operation, (char)opt),
101 gsmlib::ParameterError);
102 }
103
104 // *** main program
105
106 int main(int argc, char *argv[])
107 {
108 try
109 {
110 // handle command line options
111 std::string destination;
112 std::string source;
113 std::string baudrate;
114 std::string storeName;
115 char operation = NoOp;
116 gsmlib::SortedSMSStoreRef sourceStore, destStore;
117 bool useIndices = false; // use indices in delete, copy, backup op
118 std::string initString = gsmlib::DEFAULT_INIT_STRING;
119 bool swHandshake = false;
120 // service centre address (set on command line)
121 std::string serviceCentreAddress;
122 gsmlib::Ref<gsmlib::MeTa> sourceMeTa, destMeTa;
123
124 int opt;
125 int dummy;
126 while((opt = getopt_long(argc, argv, "I:t:s:d:b:cxlakhvVXC:",
127 longOpts, &dummy))
128 != -1)
129 switch (opt)
130 {
131 case 'C':
132 serviceCentreAddress = optarg;
133 break;
134 case 'X':
135 swHandshake = true;
136 break;
137 case 'I':
138 initString = optarg;
139 break;
140 case 'V':
141 verbose = true;
142 break;
143 case 't':
144 storeName = optarg;
145 break;
146 case 'd':
147 destination = optarg;
148 break;
149 case 's':
150 source = optarg;
151 break;
152 case 'b':
153 baudrate = optarg;
154 break;
155 case 'c':
156 checkNoOp((Operation)operation, opt);
157 operation = CopyOp;
158 break;
159 case 'x':
160 checkNoOp((Operation)operation, opt);
161 operation = DeleteOp;
162 break;
163 case 'l':
164 checkNoOp((Operation)operation, opt);
165 operation = ListOp;
166 break;
167 case 'a':
168 checkNoOp((Operation)operation, opt);
169 operation = AddOp;
170 break;
171 case 'k':
172 checkNoOp((Operation)operation, opt);
173 operation = BackupOp;
174 break;
175 case 'v':
176 std::cerr << argv[0] << gsmlib::stringPrintf(_(": version %s [compiled %s]"),
177 VERSION, __DATE__) << std::endl;
178 exit(0);
179 break;
180 case 'h':
181 std::cerr << argv[0] << _(": [-a][-b baudrate][-c][-C sca]"
182 "[-d device or file]\n"
183 " [-h][-I init string][-k][-l]"
184 "[-s device or file]"
185 "[-t SMS store name]\n [-v][-V][-x][-X]"
186 "{indices}|[phonenumber text]") << std::endl
187 << std::endl
188 << _(" -a, --add add new SMS submit message\n"
189 " (phonenumber and text) to destination")
190 << std::endl
191 << _(" -b, --baudrate baudrate to use for device "
192 "(default: 38400)")
193 << std::endl
194 << _(" -c, --copy copy source entries to destination\n"
195 " (if indices are given, "
196 "copy only these entries)") << std::endl
197 << _(" -C, --sca SMS service centre address") << std::endl
198 << _(" -d, --destination sets the destination device to\n"
199 " connect to, or the file to write to")
200 << std::endl
201 << _(" -h, --help prints this message") << std::endl
202 << _(" -I, --init device AT init sequence") << std::endl
203 << _(" -k, --backup backup new entries to destination\n"
204 " (if indices are given, "
205 "copy only these entries)") << std::endl
206 << _(" -l, --list list source to stdout") << std::endl
207 << _(" -s, --source sets the source device to connect to,\n"
208 " or the file to read") << std::endl
209 << _(" -t, --store name of SMS store to use") << std::endl
210 << _(" -v, --version prints version and exits") << std::endl
211 << _(" -V, --verbose print detailed progress messages")
212 << std::endl
213 << _(" -x, --delete delete entries denoted by indices")
214 << std::endl
215 << _(" -X, --xonxoff switch on software handshake") << std::endl
216 << std::endl;
217 exit(0);
218 break;
219 case '?':
220 throw gsmlib::GsmException(_("unknown option"), gsmlib::ParameterError);
221 break;
222 }
223
224 // check if parameters are complete
225 if (operation == NoOp)
226 throw gsmlib::GsmException(_("no operation option given"), gsmlib::ParameterError);
227 if (operation == BackupOp || operation == CopyOp)
228 if (destination.length() == 0 || source.length() == 0)
229 throw gsmlib::GsmException(_("both source and destination required"),
230 gsmlib::ParameterError);
231 if (operation == ListOp)
232 {
233 if (destination.length() != 0)
234 throw gsmlib::GsmException(_("destination must not be given"), gsmlib::ParameterError);
235 if (source.length() == 0)
236 throw gsmlib::GsmException(_("source required"), gsmlib::ParameterError);
237 }
238 if (operation == AddOp || operation == DeleteOp)
239 {
240 if (source.length() != 0)
241 throw gsmlib::GsmException(_("source must not be given"), gsmlib::ParameterError);
242 if (destination.length() == 0)
243 throw gsmlib::GsmException(_("destination required"), gsmlib::ParameterError);
244 }
245 if (operation == CopyOp || operation == DeleteOp || operation == BackupOp)
246 {
247 // check if all indices are numbers
248 for (int i = optind; i < argc; ++i)
249 for (char *pp = argv[i]; *pp != 0; ++pp)
250 if (! isdigit(*pp))
251 throw gsmlib::GsmException(gsmlib::stringPrintf(_("expected number, got '%s'"),
252 argv[i]), gsmlib::ParameterError);
253 useIndices = optind != argc;
254 }
255 else if (operation == AddOp)
256 {
257 if (optind + 2 < argc)
258 throw gsmlib::GsmException(_("more than two parameters given"),
259 gsmlib::ParameterError);
260 if (optind + 2 > argc)
261 throw gsmlib::GsmException(_("not enough parameters given"),
262 gsmlib::ParameterError);
263 }
264 else
265 if (optind != argc)
266 throw gsmlib::GsmException(_("unexpected parameters"), gsmlib::ParameterError);
267
268 // start accessing source store or file if required by operation
269 if (operation == CopyOp || operation == BackupOp || operation == ListOp)
270 {
271 if (source == "-")
272 sourceStore = new gsmlib::SortedSMSStore(true);
273 else if (gsmlib::isFile(source))
274 sourceStore = new gsmlib::SortedSMSStore(source);
275 else
276 {
277 if (storeName == "")
278 throw gsmlib::GsmException(_("store name must be given"), gsmlib::ParameterError);
279
280 sourceMeTa = new gsmlib::MeTa(new
281 #ifdef WIN32
282 gsmlib::Win32SerialPort
283 #else
284 gsmlib::UnixSerialPort
285 #endif
286 (source,
287 baudrate == "" ? gsmlib::DEFAULT_BAUD_RATE :
288 gsmlib::baudRateStrToSpeed(baudrate), initString,
289 swHandshake));
290 sourceStore = new gsmlib::SortedSMSStore(sourceMeTa->getSMSStore(storeName));
291 }
292 }
293
294 // make sure destination file exists if specified
295 // Use isFile() for its exception-throwing properties, and discard
296 // return value cos we don't care (yet) whether it's a device or a
297 // regular file.
298 if (destination != "")
299 gsmlib::isFile(destination);
300
301 // start accessing destination store or file
302 if (operation == CopyOp || operation == BackupOp || operation == AddOp ||
303 operation == DeleteOp)
304 {
305 if (destination == "-")
306 destStore = new gsmlib::SortedSMSStore(false);
307 else if (gsmlib::isFile(destination))
308 destStore = new gsmlib::SortedSMSStore(destination);
309 else
310 {
311 if (storeName == "")
312 throw gsmlib::GsmException(_("store name must be given"), gsmlib::ParameterError);
313
314 destMeTa = new gsmlib::MeTa(new
315 #ifdef WIN32
316 gsmlib::Win32SerialPort
317 #else
318 gsmlib::UnixSerialPort
319 #endif
320 (destination,
321 baudrate == "" ? gsmlib::DEFAULT_BAUD_RATE :
322 gsmlib::baudRateStrToSpeed(baudrate), initString,
323 swHandshake));
324 destStore = new gsmlib::SortedSMSStore(destMeTa->getSMSStore(storeName));
325 }
326 }
327
328 // now do the actual work
329 switch (operation)
330 {
331 case BackupOp:
332 {
333 sourceStore->setSortOrder(gsmlib::ByIndex); // needed in loop
334
335 if (useIndices)
336 for (int i = optind; i < argc; ++i)
337 {
338 gsmlib::SortedSMSStore::iterator j = sourceStore->find(atoi(argv[i]));
339 if (j == sourceStore->end())
340 throw gsmlib::GsmException(gsmlib::stringPrintf(_("no index '%s' in source"),
341 argv[i]), gsmlib::ParameterError);
342 backup(destStore, *j);
343 }
344 else
345 for (gsmlib::SortedSMSStore::iterator i = sourceStore->begin();
346 i != sourceStore->end(); ++i)
347 backup(destStore, *i);
348 break;
349 }
350 case CopyOp:
351 {
352 destStore->clear();
353 if (! useIndices) // copy all entries
354 {
355 for (gsmlib::SortedSMSStore::iterator i = sourceStore->begin();
356 i != sourceStore->end(); ++i)
357 {
358 if (verbose)
359 std::cout << gsmlib::stringPrintf(_("inserting entry #%d from source "
360 "into destination"), i->index()) << std::endl
361 << i->message()->toString();
362 destStore->insert(*i);
363 }
364 }
365 else // copy indexed entries
366 {
367 sourceStore->setSortOrder(gsmlib::ByIndex); // needed in loop
368
369 for (int i = optind; i < argc; ++i)
370 {
371 gsmlib::SortedSMSStore::iterator j = sourceStore->find(atoi(argv[i]));
372 if (j == sourceStore->end())
373 throw gsmlib::GsmException(gsmlib::stringPrintf(_("no index '%s' in source"),
374 argv[i]), gsmlib::ParameterError);
375 if (verbose)
376 std::cout << gsmlib::stringPrintf(_("inserting entry #%d from source into "
377 "destination"), j->index()) << std::endl
378 << j->message()->toString();
379 destStore->insert(*j);
380 }
381 }
382 break;
383 }
384 case ListOp:
385 {
386 for (gsmlib::SortedSMSStore::iterator i = sourceStore->begin();
387 i != sourceStore->end(); ++i)
388 std::cout << gsmlib::stringPrintf(_("index #%d"), i->index()) << std::endl
389 << i->message()->toString();
390 break;
391 }
392 case AddOp:
393 {
394 gsmlib::SMSMessageRef sms = new gsmlib::SMSSubmitMessage(argv[optind + 1], argv[optind]);
395 // set service centre address in new submit PDU if requested by user
396 if (serviceCentreAddress != "")
397 {
398 gsmlib::Address sca(serviceCentreAddress);
399 sms->setServiceCentreAddress(sca);
400 }
401 if (verbose)
402 std::cout << _("inserting new entry into destination") << std::endl
403 << sms->toString();
404 destStore->insert(sms);
405 break;
406 }
407 case DeleteOp:
408 {
409 destStore->setSortOrder(gsmlib::ByIndex);
410 for (int i = optind; i < argc; ++i)
411 {
412 int index = atoi(argv[i]);
413 if (verbose)
414 {
415 gsmlib::SortedSMSStore::iterator e = destStore->find(index);
416 if (e != destStore->end())
417 std::cout << gsmlib::stringPrintf(_("deleting entry #%d from destination"),
418 index) << std::endl
419 << e->message()->toString();
420 }
421 if (destStore->erase(index) != 1)
422 throw gsmlib::GsmException(gsmlib::stringPrintf(_("no index '%s' in destination"),
423 argv[i]), gsmlib::ParameterError);
424 }
425 break;
426 }
427 }
428 }
429 catch (gsmlib::GsmException &ge)
430 {
431 std::cerr << argv[0] << _("[ERROR]: ") << ge.what() << std::endl;
432 return 1;
433 }
434 return 0;
435 }