1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/*
* SMS Main
*
* Copyright (C) 2010 by Multi-Tech Systems
*
* Author: James Maki <jmaki@multitech.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#define __MAIN_FILE_C 1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <time.h>
#include "global.h"
#include "cmd_options.h"
#include "sms_utils.h"
#include "sms_send.h"
#include "sms_send_email.h"
#include "sms_list.h"
#include "sms_delete.h"
#include "xprintf.h"
#include "utils.h"
#include "atcmd.h"
#include "sms_config.h"
static int global_init(void)
{
memset(&Global, 0, sizeof(Global));
Global.user.name = NULL;
Global.user.email = NULL;
Global.core.verbose = false;
Global.core.interactive = true;
Global.core.sms_init = true;
Global.core.baud_rate = B115200;
Global.core.read_timeout = 5000;
Global.core.device = strdup(DEFAULT_DEVICE);
Global.core.msg_store_read = strdup("MT");
Global.core.msg_store_send = strdup("MT");
Global.core.msg_store_new = strdup("MT");
Global.core.pb_store = strdup("ME");
Global.core.editor = strdup("vi");
Global.core.edit_file = strdup("${HOME}/.smsmsg");
Global.smtp.server = NULL;
Global.smtp.port = 25;
Global.smtp.user = NULL;
Global.smtp.passwd = NULL;
Global.smtp.encryption = NULL;
Global.send_email.domain = NULL;
return 0;
}
struct sms_cmd {
const char *object;
int (*call)(int argc, char **argv);
};
static int pb_list(int argc, char **argv)
{
int fd;
int ret;
fd = sms_device_open();
if (fd < 0) {
return false;
}
ret = print_phonebook_list(fd, Global.core.pb_store);
sms_device_close(fd);
return ret;
}
static int sms_init(int argc, char **argv)
{
int fd;
Global.core.sms_init = true;
fd = sms_device_open();
if (fd < 0) {
return false;
}
sms_device_close(fd);
return true;
}
static struct sms_cmd top_cmds[] = {
{
.object = "send",
.call = sms_send,
},
#if HAVE_LIBESMTP
{
.object = "send-email",
.call = sms_send_email,
},
#endif
{
.object = "delete",
.call = sms_delete,
},
{
.object = "list",
.call = sms_list,
},
{
.object = "read",
.call = sms_list,
},
{
.object = "pb-list",
.call = pb_list,
},
{
.object = "sms-init",
.call = sms_init,
},
{
.object = NULL,
.call = NULL,
},
};
int sms_process_cmd(int argc, char **argv) {
int err;
const struct sms_cmd *cmd;
char *object;
if (argc <= 0) {
log_notice("object expected");
return false;
}
object = *argv;
cmd = top_cmds;
while (cmd->object) {
if (!tokcmp(object, cmd->object)) {
if (cmd->call) {
err = cmd->call(argc, argv);
return err;
} else {
log_notice("command does not have any actions");
return false;
}
} else {
cmd++;
}
}
log_notice("unknown object");
return false;
}
static void print_version(const char *name)
{
printf("%s (" PACKAGE ") " VERSION " (" __DATE__ " " __TIME__ ")\n", name);
printf("Copyright (C) 2010 by Multi-Tech Systems\n");
printf(
"This program is free software; you may redistribute it under the terms of\n"
"the GNU General Public License version 2 or (at your option) any later version.\n"
"This program has absolutely no warranty.\n");
}
static void usage(FILE *out)
{
fprintf(out, "Usage: sms [ OPTIONS ... ] { send | read | list | delete }\n");
fprintf(out, "where OPTIONS := { \n");
fprintf(out, " -d, --device <device> |\n");
fprintf(out, " -b, --baud-rate <rate> |\n");
fprintf(out, " -i, --interactive |\n");
fprintf(out, " -n, --non-interactive |\n");
fprintf(out, " --read-timeout <timeout> (default: 5000 milliseconds) |\n");
fprintf(out, " --verbose\n");
fprintf(out, " }\n");
fprintf(out, "\n");
}
static char *short_options = "+"
__CMD_OPT_DEVICE
__CMD_OPT_BAUD_RATE
__CMD_OPT_INTERACTIVE
__CMD_OPT_NON_INTERACTIVE
__CMD_OPT_VERBOSE;
static struct option long_options[] = {
{"device", 1, NULL, CMD_OPT_DEVICE},
{"baud-rate", 1, NULL, CMD_OPT_BAUD_RATE},
{"read-timeout", 1, NULL, CMD_OPT_READ_TIMEOUT},
{"msg-store-read", 1, NULL, CMD_OPT_MSG_STORE_READ},
{"msg-store-send", 1, NULL, CMD_OPT_MSG_STORE_SEND},
{"msg-store-new", 1, NULL, CMD_OPT_MSG_STORE_NEW},
{"pb-store", 1, NULL, CMD_OPT_PHONEBOOK_STORE},
{"interactive", 0, NULL, CMD_OPT_INTERACTIVE},
{"non-interactive", 0, NULL, CMD_OPT_NON_INTERACTIVE},
{"verbose", 0, NULL, CMD_OPT_VERBOSE},
{"version", 0, NULL, CMD_OPT_VERSION},
{"help", 0, NULL, CMD_OPT_HELP},
{0, 0, 0, 0},
};
int main(int argc, char *argv[])
{
int i;
int option_index;
int ret;
int err;
#if CONFIG_USE_SYSLOG
openlog("sms-utils", LOG_NDELAY, LOG_FACILITY);
# if DEBUG
setlogmask(LOG_UPTO(LOG_DEBUG));
# else
setlogmask(LOG_UPTO(LOG_INFO));
# endif
#endif
xprintf_init();
global_init();
err = sms_config_load();
if (err < 0) {
log_error("sms config load error");
exit(1);
}
while ((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) {
switch (i) {
case 0:
break;
case CMD_OPT_INTERACTIVE:
Global.core.interactive = true;
break;
case CMD_OPT_NON_INTERACTIVE:
Global.core.interactive = false;
break;
case CMD_OPT_VERBOSE:
Global.core.verbose = true;
break;
case CMD_OPT_DEVICE:
free(Global.core.device);
Global.core.device = strdup(optarg);
break;
case CMD_OPT_BAUD_RATE:
Global.core.baud_rate = atoi(optarg);
Global.core.baud_rate = value_to_baud(Global.core.baud_rate);
if (Global.core.baud_rate == (speed_t) -1) {
sms_send_help(stderr);
exit(1);
}
break;
case CMD_OPT_READ_TIMEOUT:
Global.core.read_timeout = atoi(optarg);
break;
case CMD_OPT_MSG_STORE_READ:
free(Global.core.msg_store_read);
Global.core.msg_store_read = strdup(optarg);
break;
case CMD_OPT_MSG_STORE_SEND:
free(Global.core.msg_store_send);
Global.core.msg_store_send = strdup(optarg);
break;
case CMD_OPT_MSG_STORE_NEW:
free(Global.core.msg_store_new);
Global.core.msg_store_new = strdup(optarg);
break;
case CMD_OPT_PHONEBOOK_STORE:
free(Global.core.pb_store);
Global.core.pb_store = strdup(optarg);
break;
case CMD_OPT_VERSION:
print_version("sms");
exit(0);
break;
case CMD_OPT_HELP:
usage(stdout);
exit(0);
break;
default:
usage(stderr);
exit(1);
}
}
if (optind >= argc) {
usage(stderr);
exit(1);
}
argc -= optind;
argv += optind;
optind = 1;
ret = sms_process_cmd(argc, argv);
return ret ? 0 : 1;
}
|