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

Contents of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (show annotations)
Sun Aug 26 19:54:54 2001 UTC (22 years, 7 months ago) by lefcha
Branch: MAIN
Changes since 1.10: +47 -36 lines
File MIME type: text/plain
Delete an unneeded fuction and small changes.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26