/[imapfilter]/imapfilter/imap.c
ViewVC logotype

Contents of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Thu Aug 23 19:13:15 2001 UTC (22 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.6: +14 -13 lines
File MIME type: text/plain
Added support for more than one accounts

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 dfilcnt, afilcnt;
18 extern filter_entry **dfilters, **afilters;
19 extern account_data **accounts;
20 extern int curacc;
21 extern int options;
22 extern unsigned int dlimit, alimit;
23
24
25 /*
26 * Sends to server data; a command.
27 */
28 int send_command(char *command)
29 {
30
31 #ifdef DEBUG
32 printf("debug: sending command: %s", command);
33 #endif
34
35 if (write(sock, command, strlen(command)) == -1) {
36 error("imapfilter: error while sending command; %s",
37 strerror(errno));
38 return FAILURE;
39 }
40
41 return SUCCESS;
42 }
43
44
45 /*
46 * Calls send_command() and get_response().
47 */
48 int send_command_get_response(char *command)
49 {
50 char buf[RESPONSE_BUFFER_MAX];
51
52 if (!send_command(command)) {
53 get_response(buf);
54 return SUCCESS;
55 } else
56 return FAILURE;
57 }
58
59
60 #ifdef DEBUG
61 /*
62 * IMAP NOOP: does nothing always succeeds.
63 */
64 int imap_noop(void)
65 {
66 char command[SMALL_COMMAND_MAX];
67
68 verbose("Client request: NOOP\n");
69
70 snprintf(command, SMALL_COMMAND_MAX, "%X NOOP\r\n", tag++);
71
72 return send_command_get_response(command);
73
74 }
75 #endif
76
77
78 /*
79 * IMAP LOGOUT: informs server that client is done.
80 */
81 int imap_logout(void)
82 {
83 char command[SMALL_COMMAND_MAX];
84
85 verbose("Client request: LOGOUT\n");
86
87 snprintf(command, SMALL_COMMAND_MAX, "%X LOGOUT\r\n", tag++);
88
89 return send_command_get_response(command);
90 }
91
92
93 /*
94 * IMAP LOGIN: identifies client to server.
95 */
96 int imap_login(void)
97 {
98 char command[MEDIUM_COMMAND_MAX];
99
100 verbose("Client request: LOGIN\n");
101
102 snprintf(command, MEDIUM_COMMAND_MAX, "%X LOGIN \"%s\" \"%s\"\r\n",
103 tag++, accounts[curacc]->username,
104 accounts[curacc]->password);
105
106 return send_command_get_response(command);
107 }
108
109
110 /*
111 * IMAP SELECT: selects a mailbox in which messages can be accessed.
112 */
113 int imap_select(void)
114 {
115 char command[SMALL_COMMAND_MAX];
116
117 verbose("Client request: SELECT\n");
118
119 snprintf(command, SMALL_COMMAND_MAX, "%X SELECT INBOX\r\n", tag++);
120
121 return send_command_get_response(command);
122 }
123
124 /*
125 * IMAP SEARCH: searches the mailbox for messages that match certain criteria.
126 */
127 int imap_search(char *results)
128 {
129 char command[BIG_COMMAND_MAX];
130
131 verbose("Client request: SEARCH\n");
132
133 generate_search_filters(command);
134 if (send_command(command))
135 return FAILURE;
136 search_response(results);
137
138 verbose("Client request: SEARCH\n");
139
140 if (alimit && afilcnt) {
141 generate_search_limits(command);
142 if (send_command(command))
143 return FAILURE;
144 search_response(results);
145 }
146
147 return SUCCESS;
148 }
149
150
151 /*
152 * IMAP FETCH: retrieves data associated with a message.
153 */
154 int imap_fetch(unsigned int m, char *results)
155 {
156 char command[MEDIUM_COMMAND_MAX];
157
158 verbose("Client request: FETCH\n");
159
160 snprintf(command, MEDIUM_COMMAND_MAX,
161 "%X FETCH %d BODY[HEADER.FIELDS (\"DATE\" \"FROM\" \"SUBJECT\")]\r\n",
162 tag++, m);
163
164 if (!send_command(command)) {
165 fetch_response(results);
166 return SUCCESS;
167 } else
168 return FAILURE;
169 }
170
171
172 /*
173 * IMAP STORE: alters data associated with a message.
174 */
175 int imap_store(unsigned int m)
176 {
177 char command[MEDIUM_COMMAND_MAX];
178
179 verbose("Client request: STORE\n");
180
181 snprintf(command, MEDIUM_COMMAND_MAX,
182 "%X STORE %d +FLAGS \\Deleted\r\n", tag++, m);
183
184 return send_command_get_response(command);
185 }
186
187
188 /*
189 * IMAP EXPUNGE: premanently removes any messages with the \Deleted flag set.
190 */
191 int imap_expunge(void)
192 {
193 char command[SMALL_COMMAND_MAX];
194
195 verbose("Client request: EXPUNGE\n");
196
197 snprintf(command, SMALL_COMMAND_MAX, "%X EXPUNGE\r\n", tag++);
198
199 return send_command_get_response(command);
200 }
201
202
203 /*
204 * Prepare according to the filters defined by the user the IMAP SEARCH
205 * command that will be sent. This command will search for DENY, ALLOW,
206 * and DENY_LIMIT filters.
207 */
208 void generate_search_filters(char *command)
209 {
210 int len;
211
212 snprintf(command, BIG_COMMAND_MAX, "%X SEARCH", tag++);
213
214
215 deny_filters(command);
216
217 if (dlimit) {
218 len = strlen(command);
219 snprintf(command + len, BIG_COMMAND_MAX - len, " LARGER %d",
220 dlimit);
221 } else if (!dfilcnt) { /* If no DENY filters were defined, then
222 deny all except the ALLOW filters. */
223 len = strlen(command);
224 snprintf(command + len, BIG_COMMAND_MAX - len, " ALL");
225 }
226
227 allow_filters(command, "NOT");
228
229 len = strlen(command);
230 snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n");
231 }
232
233
234 /*
235 * Prepare according to the filters defined by the user the IMAP SEARCH
236 * command that will be sent. This command will search for the ALLOW_LIMIT
237 * filter.
238 */
239 void generate_search_limits(char *command)
240 {
241 int len;
242
243 snprintf(command, BIG_COMMAND_MAX, "%X SEARCH", tag++);
244
245 allow_filters(command, "OR");
246
247 len = strlen(command);
248 snprintf(command + len, BIG_COMMAND_MAX - len, " LARGER %d", alimit);
249
250 len = strlen(command);
251 snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n");
252
253 }
254
255
256 /*
257 * Adds to the IMAP SEARCH command all the DENY type filters.
258 */
259 void deny_filters(char *command)
260 {
261 int f = 0;
262 int len;
263
264 if (dfilcnt > 0) {
265 while (f < (dfilcnt - 1)) {
266 len = strlen(command);
267 snprintf(command + len, BIG_COMMAND_MAX - len,
268 " OR %s%s \"%s\"",
269 custom_header(dfilters[f]->custom),
270 dfilters[f]->name, dfilters[f]->body);
271 f++;
272 }
273
274 len = strlen(command);
275 snprintf(command + len, BIG_COMMAND_MAX - len, " %s%s%s \"%s\"",
276 (dlimit ? "OR " : ""),
277 custom_header(dfilters[f]->custom),
278 dfilters[f]->name, dfilters[f]->body);
279 }
280 }
281
282
283 /*
284 * Adds to IMAP SEARCH command all the ALLOW type filters.
285 */
286 void allow_filters(char *command, char *key)
287 {
288 int f = 0;
289 int len;
290
291 if (afilcnt > 0) {
292 while (f < (afilcnt - 1)) {
293 len = strlen(command);
294 snprintf(command + len, BIG_COMMAND_MAX - len,
295 " %s %s%s \"%s\"", key,
296 custom_header(afilters[f]->custom),
297 afilters[f]->name, afilters[f]->body);
298 f++;
299 }
300
301 len = strlen(command);
302 snprintf(command + len, BIG_COMMAND_MAX - len, " %s%s%s \"%s\"",
303 (!strncmp(key, "NOT", 3) ? "NOT " : ""),
304 custom_header(afilters[f]->custom),
305 afilters[f]->name, afilters[f]->body);
306 }
307 }
308
309
310 /*
311 * Convert the string with the UID's of the messages to be deleted to
312 * integers, send the appropriate command and maybe print some headers
313 * of the "to be deleted" message.
314 */
315 void delete_messages(char *unwanted)
316 {
317 unsigned long int messg;
318 char headers[HEADERS_MAX];
319
320 do {
321 messg = strtoul(unwanted, &unwanted, 0);
322
323 if (options & OPTION_SHOW_HEADERS) {
324 imap_fetch(messg, headers);
325
326 if (!(options & OPTION_DETAILS_QUITE))
327 printf("Deleting message:\n%s", headers);
328
329 if (!(options & OPTION_TEST_MODE))
330 log_info("%s", headers);
331 }
332
333 if (!(options & OPTION_TEST_MODE))
334 imap_store(messg);
335
336 } while (*unwanted);
337 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26