1 |
#include <stdio.h> |
2 |
#include <string.h> |
3 |
#include <stdlib.h> |
4 |
#include <unistd.h> |
5 |
#include <errno.h> |
6 |
|
7 |
#include "config.h" |
8 |
#include "imapfilter.h" |
9 |
#include "imap.h" |
10 |
#include "connect.h" |
11 |
#include "log.h" |
12 |
|
13 |
|
14 |
static int tag = 0xA000; /* Every IMAP command is prefixed with a |
15 |
unique [:alnum:] string. */ |
16 |
extern int sock; |
17 |
extern int dfcnt, afcnt; |
18 |
extern filter_entry **dfilters, **afilters; |
19 |
extern account_data account; |
20 |
extern int options; |
21 |
|
22 |
|
23 |
/* |
24 |
* Sends to server data; a command. |
25 |
*/ |
26 |
int send_command(char *command) |
27 |
{ |
28 |
|
29 |
#ifdef DEBUG |
30 |
printf("debug: sending command: %s", command); |
31 |
#endif |
32 |
|
33 |
if (write(sock, command, strlen(command)) == -1) { |
34 |
error("imapfilter: error while sending command; %s", |
35 |
strerror(errno)); |
36 |
return FAILURE; |
37 |
} |
38 |
|
39 |
return SUCCESS; |
40 |
} |
41 |
|
42 |
|
43 |
/* |
44 |
* Calls send_command() and get_response(). |
45 |
*/ |
46 |
int send_command_get_response(char *command) |
47 |
{ |
48 |
char buf[RESPONSE_BUFFER_MAX]; |
49 |
|
50 |
if (!send_command(command)) { |
51 |
get_response(buf); |
52 |
return SUCCESS; |
53 |
} else |
54 |
return FAILURE; |
55 |
} |
56 |
|
57 |
|
58 |
#ifdef DEBUG |
59 |
/* |
60 |
* IMAP NOOP: does nothing always succeeds. |
61 |
*/ |
62 |
int imap_noop(void) |
63 |
{ |
64 |
char command[SMALL_COMMAND_MAX]; |
65 |
|
66 |
verbose("Client request: NOOP\n"); |
67 |
|
68 |
snprintf(command, SMALL_COMMAND_MAX, "%X NOOP\r\n", tag++); |
69 |
|
70 |
return send_command_get_response(command); |
71 |
|
72 |
} |
73 |
#endif |
74 |
|
75 |
|
76 |
/* |
77 |
* IMAP LOGOUT: informs server that client is done. |
78 |
*/ |
79 |
int imap_logout(void) |
80 |
{ |
81 |
char command[SMALL_COMMAND_MAX]; |
82 |
|
83 |
verbose("Client request: LOGOUT\n"); |
84 |
|
85 |
snprintf(command, SMALL_COMMAND_MAX, "%X LOGOUT\r\n", tag++); |
86 |
|
87 |
return send_command_get_response(command); |
88 |
} |
89 |
|
90 |
|
91 |
/* |
92 |
* IMAP LOGIN: identifies client to server. |
93 |
*/ |
94 |
int imap_login(void) |
95 |
{ |
96 |
char command[MEDIUM_COMMAND_MAX]; |
97 |
|
98 |
verbose("Client request: LOGIN\n"); |
99 |
|
100 |
snprintf(command, MEDIUM_COMMAND_MAX, "%X LOGIN \"%s\" \"%s\"\r\n", |
101 |
tag++, account.username, account.password); |
102 |
|
103 |
return send_command_get_response(command); |
104 |
} |
105 |
|
106 |
|
107 |
/* |
108 |
* IMAP SELECT: selects a mailbox in which messages can be accessed. |
109 |
*/ |
110 |
int imap_select(void) |
111 |
{ |
112 |
char command[SMALL_COMMAND_MAX]; |
113 |
|
114 |
verbose("Client request: SELECT\n"); |
115 |
|
116 |
snprintf(command, SMALL_COMMAND_MAX, "%X SELECT INBOX\r\n", tag++); |
117 |
|
118 |
return send_command_get_response(command); |
119 |
} |
120 |
|
121 |
/* |
122 |
* IMAP SEARCH: searches the mailbox for messages that match certain criteria. |
123 |
*/ |
124 |
int imap_search(void) |
125 |
{ |
126 |
char command[BIG_COMMAND_MAX]; |
127 |
|
128 |
verbose("Client request: SEARCH\n"); |
129 |
|
130 |
generate_search_filters(command); |
131 |
|
132 |
return send_command(command); |
133 |
} |
134 |
|
135 |
|
136 |
/* |
137 |
* IMAP FETCH: retrieves data associated with a message. |
138 |
*/ |
139 |
int imap_fetch(unsigned int m, char *results) |
140 |
{ |
141 |
char command[MEDIUM_COMMAND_MAX]; |
142 |
|
143 |
verbose("Client request: FETCH\n"); |
144 |
|
145 |
snprintf(command, MEDIUM_COMMAND_MAX, |
146 |
"%X FETCH %d BODY[HEADER.FIELDS (\"DATE\" \"FROM\" \"SUBJECT\")]\r\n", |
147 |
tag++, m); |
148 |
|
149 |
if (!send_command(command)) { |
150 |
fetch_response(results); |
151 |
return SUCCESS; |
152 |
} else |
153 |
return FAILURE; |
154 |
} |
155 |
|
156 |
|
157 |
/* |
158 |
* IMAP STORE: alters data associated with a message. |
159 |
*/ |
160 |
int imap_store(unsigned int m) |
161 |
{ |
162 |
char command[MEDIUM_COMMAND_MAX]; |
163 |
|
164 |
verbose("Client request: STORE\n"); |
165 |
|
166 |
snprintf(command, MEDIUM_COMMAND_MAX, |
167 |
"%X STORE %d +FLAGS \\Deleted\r\n", tag++, m); |
168 |
|
169 |
return send_command_get_response(command); |
170 |
} |
171 |
|
172 |
|
173 |
/* |
174 |
* IMAP EXPUNGE: premanently removes any messages with the \Deleted flag set. |
175 |
*/ |
176 |
int imap_expunge(void) |
177 |
{ |
178 |
char command[SMALL_COMMAND_MAX]; |
179 |
|
180 |
verbose("Client request: EXPUNGE\n"); |
181 |
|
182 |
snprintf(command, SMALL_COMMAND_MAX, "%X EXPUNGE\r\n", tag++); |
183 |
|
184 |
return send_command_get_response(command); |
185 |
} |
186 |
|
187 |
|
188 |
/* |
189 |
* Prepare according to the filters defined by the user the IMAP SEARCH |
190 |
* command that will be sent. |
191 |
*/ |
192 |
void generate_search_filters(char *command) |
193 |
{ |
194 |
int f = 0; |
195 |
int len; |
196 |
|
197 |
snprintf(command, BIG_COMMAND_MAX, "%X SEARCH ", tag++); |
198 |
|
199 |
/* Go through the DENY type filters... */ |
200 |
|
201 |
while (f < (dfcnt - 1)) { |
202 |
len = strlen(command); |
203 |
snprintf(command + len, BIG_COMMAND_MAX - len, "OR %s%s \"%s\" ", |
204 |
(dfilters[f]->custom ? "HEADER " : ""), dfilters[f]->name, |
205 |
dfilters[f]->body); |
206 |
f++; |
207 |
} |
208 |
|
209 |
len = strlen(command); |
210 |
snprintf(command + len, BIG_COMMAND_MAX - len, "%s%s \"%s\"", |
211 |
(dfilters[f]->custom ? "HEADER " : ""), dfilters[f]->name, |
212 |
dfilters[f]->body); |
213 |
|
214 |
/* ... and then continue to the ALLOW entries. */ |
215 |
|
216 |
f = 0; |
217 |
|
218 |
while (f < afcnt) { |
219 |
len = strlen(command); |
220 |
snprintf(command + len, BIG_COMMAND_MAX - len, " NOT %s%s \"%s\"", |
221 |
(afilters[f]->custom ? "HEADER " : ""), afilters[f]->name, |
222 |
afilters[f]->body); |
223 |
f++; |
224 |
} |
225 |
|
226 |
len = strlen(command); |
227 |
snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n"); |
228 |
} |
229 |
|
230 |
|
231 |
/* |
232 |
* Convert the string with the UID's of the messages to be deleted to |
233 |
* integers, send the appropriate command and maybe print some headers |
234 |
* of the "to be deleted" message. |
235 |
*/ |
236 |
void delete_messages(char *unwanted) |
237 |
{ |
238 |
unsigned long int messg; |
239 |
char headers[HEADERS_MAX]; |
240 |
|
241 |
do { |
242 |
messg = strtoul(unwanted, &unwanted, 0); |
243 |
|
244 |
imap_fetch(messg, headers); |
245 |
|
246 |
if (!(options & OPTION_DETAILS_QUITE)) { |
247 |
printf("Deleting message:\n%s\n", headers); |
248 |
} |
249 |
|
250 |
if (!(options & OPTION_TEST_MODE)) { |
251 |
log_info("%s\n", headers); |
252 |
imap_store(messg); |
253 |
} |
254 |
} while (*unwanted); |
255 |
} |