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

Annotation of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Mon Aug 20 22:46:24 2001 UTC (22 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.5: +10 -12 lines
File MIME type: text/plain
Fixed a bug with DENY_LIMIT

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26