summaryrefslogtreecommitdiff
path: root/src/sms_delete.c
blob: b45546e8e94c9c269b15719c835e3a7068603956 (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
/*
 * SMS Delete Messages
 *
 * 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 "log.h"
#include "utils.h"
#include "cmd_options.h"
#include "sms_utils.h"
#include "sms_delete.h"
#include "sms_list.h"
#include "atcmd.h"

static int do_delete(int fd, int argc, char **argv)
{
	int tmp;
	char *arg;
	struct msg_list_info list_info;

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

	if (argc < 1) {
		log_error("msg-status expected");
		return false;
	}
	arg = *argv;
	argc--; argv++;

	if (!strcmp(arg, "unread")) {
		list_info.status = SMS_MSG_REC_UNREAD;
		list_info.cmd_type = CMD_TYPE_CMGL;
	} else if (!strcmp(arg, "read")) {
		list_info.status = SMS_MSG_REC_READ;
		list_info.cmd_type = CMD_TYPE_CMGL;
	} else if (!strcmp(arg, "unsent")) {
		list_info.status = SMS_MSG_STO_UNSENT;
		list_info.cmd_type = CMD_TYPE_CMGL;
	} else if (!strcmp(arg, "sent")) {
		list_info.status = SMS_MSG_STO_SENT;
		list_info.cmd_type = CMD_TYPE_CMGL;
	} else if (!strcmp(arg, "all")) {
		list_info.status = SMS_MSG_ALL;
		list_info.cmd_type = CMD_TYPE_CMGL;
	} else if (!strcmp(arg, "index")) {
		if (argc < 1) {
			log_error("index expected");
			return false;
		}
		arg = *argv;
		argc--; argv++;

		list_info.index = atoi(arg);

		list_info.status = SMS_MSG_ALL;
		list_info.cmd_type = CMD_TYPE_CMGR;
	} else {
		log_error("invalid msg-status %s", arg);
		return false;
	}

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

	tmp = sms_list_get(fd, &list_info);
	if (tmp < 0) {
		log_error("failed to get msg list");
		return false;
	}

	struct sms_msg *msg;
	int failed = 0;

	list_for_each_entry(msg, &list_info.msg_list, list) {
		if (Global.core.verbose) {
			printf("deleting message at index %d", msg->index);
		}

		tmp = atcmd_plus_cmgd_write(fd, msg->index);
		if (tmp < 0) {
			printf("deleting message at index %d failed\n", msg->index);
			failed++;
			continue;
		}
	}

	sms_list_free(&list_info);

	return failed ? false : true;
}

static char *short_options = "";
static struct option long_options[] = {
	{NULL, 0, NULL, 0},
};

void sms_delete_help(FILE *out) {
	fprintf(out, "usage: delete STATUS [ ARGUMENTS ]\n");
	fprintf(out, "where  STATUS := { \n");
	fprintf(out, "         index <index>\n");
	fprintf(out, "         unread\n");
	fprintf(out, "         read\n");
	fprintf(out, "         unsent\n");
	fprintf(out, "         sent\n");
	fprintf(out, "         all\n");
	fprintf(out, "       }\n");
	fprintf(out, "\n");
}

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

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

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

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

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

	ret = do_delete(fd, argc, argv);

	sms_device_close(fd);

	return ret;
}