summaryrefslogtreecommitdiff
path: root/src/sms_send.c
blob: 72b5a137a552ba25e7e8b176f017dbb102a0506a (plain)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * SMS Send
 *
 * 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
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "global.h"
#include "utils.h"
#include "cmd_options.h"
#include "sms_utils.h"
#include "atcmd.h"
#include "sms_send.h"
#include "pdu_encode.h"
#include "phonebook.h"

enum {
	SEND_MODE_CMGW_CMSS,
	SEND_MODE_CMGS,
};

struct send_options {
	char *file;
	int alphabet;
	const char *smsc_addr;
	int send_mode;
};

static int auto_fill_smsc(int fd, struct sms_addr *addr)
{
	char buf[ATCMD_LINE_SIZE];
	char *save;
	char *token;
	char *addr_str;
	int type;
	int tmp;

	atcmd_writeline(fd, "AT+CSCA?");
	tmp = atcmd_expect_line(fd, buf, sizeof(buf), "+CSCA: ");
	if (tmp <= 0) {
		if (*buf) {
			log_debug("expected +CPMS: but got %s", buf);
		} else {
			log_debug("expected +CPMS");
		}
		return -1;
	}

	save = buf;
	token = atcmd_response_brk(&save);
	if (!token) {
		log_debug("response tokenizing failed");
		return -1;
	}

	token = atcmd_value_tok(&save);
	if (!token) {
		log_debug("addr token not present");
		return -1;
	}
	addr_str = token;

	token = atcmd_value_tok(&save);
	if (!token) {
		log_debug("addr-len token not present");
		return -1;
	}
	type = atoi(token);

	tmp = atcmd_expect_line(fd, buf, sizeof(buf), "OK");
	if (tmp <= 0) {
		if (*buf) {
			log_debug("expected OK but got %s", buf);
		} else {
			log_debug("expected OK");
		}
		return -1;
	}

	return sms_addr_fill(addr, addr_str, type);
}

static int fill_addr(struct phonebook_list_info *list_info,
		struct sms_addr *addr, const char *addr_str)
{
	struct phonebook_entry *entry;
	int type;
	int tmp;

	type = sms_addr_type_infer(addr_str);
	if (type != SMS_ADDR_TEXT) {
		goto fill;
	}

	log_debug("text addr found: searching phonebook for match");

	if (Global.core.interactive) {
		entry = phonebook_search_names_i(&list_info->entry_list, addr_str);
		if (!entry) {
			return -1;
		}
	} else {
		entry = phonebook_find_by_name(&list_info->entry_list, addr_str);
		if (!entry) {
			log_notice("%s not found in phonebook", addr_str);
			return -1;
		}
	}
	addr_str = entry->addr;

fill:

	tmp = sms_addr_fill(addr, addr_str, SMS_ADDR_UNSPEC);
	if (tmp < 0) {
		log_debug("pdu fill failed with addr %s", addr_str);
		return -1;
	}

	return 0;
}

static int fill_user_data(struct send_options *options, struct pdu_info *pdu)
{
	int fd;
	int tmp;

	if (options->file) {
		fd = open(options->file, O_RDONLY);
		if (fd < 0) {
			log_error("failed to open %s for reading", options->file);
			return -1;
		}
	} else if (Global.core.interactive && Global.core.edit_file && Global.core.editor) {
		tmp = systemf("%s %s", Global.core.editor, Global.core.edit_file);
		if (tmp < 0 || !WIFEXITED(tmp) || WEXITSTATUS(tmp)) {
			log_error("edit failed");
			return -1;
		}

		char *path = shell_path_expand(Global.core.edit_file);
		if (!path) {
			return -1;
		}

		fd = open(path, O_RDONLY);
		if (fd < 0) {
			log_error("failed to open %s for reading", path);
			free(path);
			return -1;
		}
		free(path);
	} else {
		fd = fileno(stdin);
	}

	tmp = pdu_user_data_read(fd, pdu);

	close(fd);

	if (tmp < 0) {
		log_error("read user data failed");
		return -1;
	}

	return 0;
}

static int do_send(int fd, struct send_options *options, int argc, char **argv)
{
	char buf[PDU_BUFFER_SIZE];
	struct pdu_info pdu;
	struct phonebook_list_info list_info;
	int failed = 0;
	int mem_index = 0;
	int tmp;
	int i;

	if (argc < 1) {
		log_error("at least one recipient addr is required");
		return false;
	}

	memset(&pdu, 0, sizeof(pdu));

	pdu.type.msg_type = PDU_MTI_SUBMIT;
	pdu.type.validity_period_format = PDU_VPF_RELATIVE;
	pdu.validity_period = PDU_VPF_RELATIVE_2DAYS;
	pdu.data_coding.general.alphabet = options->alphabet;

	if (options->smsc_addr) {
		tmp = sms_addr_fill(&pdu.smsc_addr, options->smsc_addr, SMS_ADDR_UNSPEC);
		if (tmp < 0) {
			return false;
		}
	}

	tmp = fill_user_data(options, &pdu);
	if (tmp < 0) {
		log_error("read user data failed");
		return false;
	}

	if (Global.core.verbose) {
		printf("preparing for send...\n");
	}

	if (options->send_mode == SEND_MODE_CMGW_CMSS) {
		if (Global.core.sms_mode == SMS_PDU_MODE) {
			tmp = pdu_encode(buf, sizeof(buf), &pdu);
			if (tmp < 0) {
				log_error("pdu encode failed");
				return false;
			}
		}

		if (Global.core.verbose) {
			printf("writing message to memory\n");
		}

		if (Global.core.sms_mode == SMS_TEXT_MODE) {
			mem_index = atcmd_plus_cmgw_write_text(fd, NULL,
				SMS_ADDR_UNSPEC, NULL, pdu.user_data, pdu.user_data_len);
		} else {
			mem_index = atcmd_plus_cmgw_write(fd, buf, pdu.msg_len);
		}
		if (mem_index < 0) {
			log_error("write message to memory failed");
			return false;
		}
	}

	memset(&list_info, 0, sizeof(list_info));

	if (Global.core.pb_store) {
		strncpy(list_info.store_select, Global.core.pb_store, STORE_NAME_LEN);
	}
	tmp = phonebook_list_get(fd, &list_info);
	if (tmp < 0) {
		log_warning("failed to get phonebook list");
		memset(&list_info, 0, sizeof(list_info));
		INIT_LIST_HEAD(&list_info.entry_list);
	}

	for (i = 0; i < argc; i++) {
		if (Global.core.verbose) {
			printf("sending message to %s\n", argv[i]);
		}

		tmp = fill_addr(&list_info, &pdu.addr, argv[i]);
		if (tmp < 0) {
			printf("sending message to %s failed\n", argv[i]);
			failed++;
			continue;
		}

		if (options->send_mode == SEND_MODE_CMGW_CMSS) {
			tmp = atcmd_plus_cmss_write(fd, mem_index,
					pdu.addr.addr, SMS_ADDR_UNSPEC);
			if (tmp < 0) {
				printf("sending message to %s failed\n", argv[i]);
				failed++;
				continue;
			}
		} else {
			if (Global.core.sms_mode == SMS_PDU_MODE) {
				tmp = pdu_encode(buf, sizeof(buf), &pdu);
				if (tmp < 0) {
					printf("sending message to %s failed\n", argv[i]);
					failed++;
					continue;
				}
			}

			if (Global.core.sms_mode == SMS_TEXT_MODE) {
				tmp = atcmd_plus_cmgs_write_text(fd, pdu.addr.addr,
						pdu.user_data, pdu.user_data_len);
			} else {
				tmp = atcmd_plus_cmgs_write(fd, buf, pdu.msg_len);
			}
			if (tmp < 0) {
				printf("sending message to %s failed\n", argv[i]);
				failed++;
				continue;
			}
		}
	}

	if (!failed && Global.core.interactive && Global.core.edit_file) {
		systemf("rm -f %s", Global.core.edit_file);
	}

	phonebook_list_free(&list_info);

	return failed ? false : true;
}

static char *short_options = __CMD_OPT_FILE;
static struct option long_options[] = {
	{"alphabet", 1, NULL, CMD_OPT_ALPHABET},
	{"file", 1, NULL, CMD_OPT_FILE},
	{"smsc-addr", 1, NULL, CMD_OPT_SMSC_ADDR},
	{"send-mode", 1, NULL, CMD_OPT_SEND_MODE},
	{NULL, 0, NULL, 0},
};

void sms_send_help(FILE *out) {
	fprintf(out, "usage: send [ OPTIONS ... ] <number>\n");
	fprintf(out, "where  OPTIONS := { \n");
	fprintf(out, "         --alphabet { seven-bit | eight-bit } |\n");
	fprintf(out, "         -f, --file <input-file> |\n");
	fprintf(out, "         --smsc-addr <smsc-addr> |\n");
	fprintf(out, "         --send-mode { cmgw-cmss | cmgs } \n");
	fprintf(out, "       }\n");
	fprintf(out, "\n");
}

int sms_send(int argc, char **argv)
{
	int i;
	int option_index;
	int ret;
	int fd;

	struct send_options options;
	options.alphabet = PDU_ALPHABET_DEFAULT;
	options.file = NULL;
	options.send_mode = SEND_MODE_CMGS;
	options.smsc_addr = NULL;

	while ((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) {
		switch (i) {
		case 0:
			break;

		case CMD_OPT_ALPHABET:
			if (!strcmp(optarg, "seven-bit")) {
				options.alphabet = PDU_ALPHABET_DEFAULT;
			} else if (!strcmp(optarg, "eight-bit")) {
				options.alphabet = PDU_ALPHABET_EIGHT;
			} else {
				sms_send_help(stderr);
				return false;
			}
			break;

		case CMD_OPT_FILE:
			options.file = optarg;
			break;

		case CMD_OPT_SMSC_ADDR:
			options.smsc_addr = optarg;
			break;

		case CMD_OPT_SEND_MODE:
			if (!strcmp(optarg, "cmgw-cmss")) {
				options.send_mode = SEND_MODE_CMGW_CMSS;
			} else if (!strcmp(optarg, "cmgs")) {
				options.send_mode = SEND_MODE_CMGS;
			} else {
				sms_send_help(stderr);
				return false;
			}
			break;

		default:
			sms_send_help(stderr);
			return false;
		}
	}

	if (optind >= argc) {
		sms_send_help(stderr);
		return false;
	}
	argc -= optind;
	argv += optind;

	fd = sms_device_open();
	if (fd < 0) {
		return false;
	}

	ret = do_send(fd, &options, argc, argv);

	sms_device_close(fd);

	return ret;
}