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

Annotation of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26