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

Annotation of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Mon Aug 20 21:40:57 2001 UTC (22 years, 7 months ago) by lefcha
Branch: MAIN
Changes since 1.4: +99 -27 lines
File MIME type: text/plain
Added message size limits support

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     }
220    
221     if (!dfcnt) { /* 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 lefcha 1.1
247 lefcha 1.5 len = strlen(command);
248     snprintf(command + len, BIG_COMMAND_MAX - len,
249     " LARGER %d", alimit);
250    
251     len = strlen(command);
252     snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n");
253    
254     }
255 lefcha 1.2
256    
257 lefcha 1.5 /*
258     * Adds to the IMAP SEARCH command all the DENY type filters.
259     */
260     void deny_filters(char *command)
261     {
262     int f = 0;
263     int len;
264 lefcha 1.4
265 lefcha 1.5 if (dfcnt > 0) {
266     while (f < (dfcnt - 1)) {
267 lefcha 1.4 len = strlen(command);
268 lefcha 1.5 snprintf(command + len, BIG_COMMAND_MAX - len,
269     " OR %s%s \"%s\"",
270     custom_header(dfilters[f]->custom),
271 lefcha 1.4 dfilters[f]->name, dfilters[f]->body);
272 lefcha 1.5 f++;
273     }
274    
275     len = strlen(command);
276     snprintf(command + len, BIG_COMMAND_MAX - len, " %s%s%s \"%s\"",
277     (dlimit ? "OR " : ""),
278     custom_header(dfilters[f]->custom),
279     dfilters[f]->name, dfilters[f]->body);
280     }
281     }
282 lefcha 1.4
283    
284 lefcha 1.5 /*
285     * Adds to IMAP SEARCH command all the ALLOW type filters.
286     */
287     void allow_filters(char *command, char *key)
288     {
289     int f = 0;
290     int len;
291 lefcha 1.4
292 lefcha 1.5 if (afcnt > 0) {
293     while (f < (afcnt - 1)) {
294 lefcha 1.4 len = strlen(command);
295     snprintf(command + len, BIG_COMMAND_MAX - len,
296 lefcha 1.5 " %s %s%s \"%s\"", key,
297     custom_header(afilters[f]->custom),
298 lefcha 1.4 afilters[f]->name, afilters[f]->body);
299     f++;
300     }
301 lefcha 1.2
302     len = strlen(command);
303 lefcha 1.5 snprintf(command + len, BIG_COMMAND_MAX - len, " %s%s%s \"%s\"",
304     (!strncmp(key, "NOT", 3) ? "NOT " : ""),
305     custom_header(afilters[f]->custom),
306     afilters[f]->name, afilters[f]->body);
307     }
308 lefcha 1.1 }
309    
310    
311     /*
312     * Convert the string with the UID's of the messages to be deleted to
313     * integers, send the appropriate command and maybe print some headers
314     * of the "to be deleted" message.
315     */
316     void delete_messages(char *unwanted)
317     {
318     unsigned long int messg;
319     char headers[HEADERS_MAX];
320    
321     do {
322     messg = strtoul(unwanted, &unwanted, 0);
323    
324 lefcha 1.3 if (options & OPTION_SHOW_HEADERS) {
325     imap_fetch(messg, headers);
326 lefcha 1.1
327 lefcha 1.3 if (!(options & OPTION_DETAILS_QUITE))
328     printf("Deleting message:\n%s", headers);
329    
330     if (!(options & OPTION_TEST_MODE))
331     log_info("%s", headers);
332 lefcha 1.1 }
333    
334 lefcha 1.3 if (!(options & OPTION_TEST_MODE))
335 lefcha 1.1 imap_store(messg);
336 lefcha 1.4
337 lefcha 1.1 } while (*unwanted);
338     }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26