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

Annotation of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations)
Fri Aug 24 17:42:49 2001 UTC (22 years, 7 months ago) by lefcha
Branch: MAIN
Changes since 1.7: +8 -7 lines
File MIME type: text/plain
Printing of an info message and other small changes

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.7 extern int dfilcnt, afilcnt;
18 lefcha 1.2 extern filter_entry **dfilters, **afilters;
19 lefcha 1.7 extern account_data **accounts;
20     extern int curacc;
21 lefcha 1.1 extern int options;
22 lefcha 1.5 extern unsigned int dlimit, alimit;
23 lefcha 1.1
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 lefcha 1.8 if (!send_command(command) && !get_response(buf))
53 lefcha 1.1 return SUCCESS;
54 lefcha 1.8 else
55 lefcha 1.1 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 lefcha 1.7 tag++, accounts[curacc]->username,
103     accounts[curacc]->password);
104 lefcha 1.1
105     return send_command_get_response(command);
106     }
107    
108    
109     /*
110     * IMAP SELECT: selects a mailbox in which messages can be accessed.
111     */
112     int imap_select(void)
113     {
114     char command[SMALL_COMMAND_MAX];
115    
116     verbose("Client request: SELECT\n");
117    
118     snprintf(command, SMALL_COMMAND_MAX, "%X SELECT INBOX\r\n", tag++);
119    
120 lefcha 1.8 if (!send_command(command) && !select_response())
121     return SUCCESS;
122     else
123     return FAILURE;
124 lefcha 1.1 }
125    
126     /*
127     * IMAP SEARCH: searches the mailbox for messages that match certain criteria.
128     */
129 lefcha 1.5 int imap_search(char *results)
130 lefcha 1.1 {
131     char command[BIG_COMMAND_MAX];
132    
133     verbose("Client request: SEARCH\n");
134    
135     generate_search_filters(command);
136 lefcha 1.5 if (send_command(command))
137     return FAILURE;
138     search_response(results);
139    
140     verbose("Client request: SEARCH\n");
141 lefcha 1.1
142 lefcha 1.7 if (alimit && afilcnt) {
143 lefcha 1.5 generate_search_limits(command);
144     if (send_command(command))
145     return FAILURE;
146     search_response(results);
147     }
148    
149     return SUCCESS;
150 lefcha 1.1 }
151    
152    
153     /*
154     * IMAP FETCH: retrieves data associated with a message.
155     */
156     int imap_fetch(unsigned int m, char *results)
157     {
158     char command[MEDIUM_COMMAND_MAX];
159    
160     verbose("Client request: FETCH\n");
161    
162     snprintf(command, MEDIUM_COMMAND_MAX,
163     "%X FETCH %d BODY[HEADER.FIELDS (\"DATE\" \"FROM\" \"SUBJECT\")]\r\n",
164     tag++, m);
165    
166 lefcha 1.8 if (!send_command(command) && !fetch_response(results))
167 lefcha 1.1 return SUCCESS;
168 lefcha 1.8 else
169 lefcha 1.1 return FAILURE;
170     }
171    
172    
173     /*
174     * IMAP STORE: alters data associated with a message.
175     */
176     int imap_store(unsigned int m)
177     {
178     char command[MEDIUM_COMMAND_MAX];
179    
180     verbose("Client request: STORE\n");
181    
182     snprintf(command, MEDIUM_COMMAND_MAX,
183     "%X STORE %d +FLAGS \\Deleted\r\n", tag++, m);
184    
185     return send_command_get_response(command);
186     }
187    
188    
189     /*
190     * IMAP EXPUNGE: premanently removes any messages with the \Deleted flag set.
191     */
192     int imap_expunge(void)
193     {
194     char command[SMALL_COMMAND_MAX];
195    
196     verbose("Client request: EXPUNGE\n");
197    
198     snprintf(command, SMALL_COMMAND_MAX, "%X EXPUNGE\r\n", tag++);
199    
200     return send_command_get_response(command);
201     }
202    
203    
204     /*
205     * Prepare according to the filters defined by the user the IMAP SEARCH
206 lefcha 1.5 * command that will be sent. This command will search for DENY, ALLOW,
207     * and DENY_LIMIT filters.
208 lefcha 1.1 */
209     void generate_search_filters(char *command)
210     {
211     int len;
212    
213 lefcha 1.5 snprintf(command, BIG_COMMAND_MAX, "%X SEARCH", tag++);
214    
215    
216     deny_filters(command);
217    
218     if (dlimit) {
219     len = strlen(command);
220     snprintf(command + len, BIG_COMMAND_MAX - len, " LARGER %d",
221     dlimit);
222 lefcha 1.7 } else if (!dfilcnt) { /* If no DENY filters were defined, then
223 lefcha 1.5 deny all except the ALLOW filters. */
224 lefcha 1.7 len = strlen(command);
225     snprintf(command + len, BIG_COMMAND_MAX - len, " ALL");
226     }
227 lefcha 1.5
228     allow_filters(command, "NOT");
229    
230     len = strlen(command);
231     snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n");
232     }
233    
234    
235     /*
236     * Prepare according to the filters defined by the user the IMAP SEARCH
237     * command that will be sent. This command will search for the ALLOW_LIMIT
238     * filter.
239     */
240     void generate_search_limits(char *command)
241     {
242     int len;
243    
244     snprintf(command, BIG_COMMAND_MAX, "%X SEARCH", tag++);
245    
246     allow_filters(command, "OR");
247 lefcha 1.1
248 lefcha 1.5 len = strlen(command);
249 lefcha 1.6 snprintf(command + len, BIG_COMMAND_MAX - len, " LARGER %d", alimit);
250    
251 lefcha 1.5 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.7 if (dfilcnt > 0) {
266     while (f < (dfilcnt - 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.7 if (afilcnt > 0) {
293     while (f < (afilcnt - 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 lefcha 1.6 (!strncmp(key, "NOT", 3) ? "NOT " : ""),
305     custom_header(afilters[f]->custom),
306     afilters[f]->name, afilters[f]->body);
307 lefcha 1.5 }
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