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

Annotation of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations)
Tue Aug 28 22:42:06 2001 UTC (22 years, 7 months ago) by lefcha
Branch: MAIN
Changes since 1.11: +1 -1 lines
File MIME type: text/plain
Info message about deleted mails.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26