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

Contents of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (show annotations)
Fri Aug 24 17:42:49 2001 UTC (22 years, 8 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 #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 extern int dfilcnt, afilcnt;
18 extern filter_entry **dfilters, **afilters;
19 extern account_data **accounts;
20 extern int curacc;
21 extern int options;
22 extern unsigned int dlimit, alimit;
23
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 if (!send_command(command) && !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++, accounts[curacc]->username,
103 accounts[curacc]->password);
104
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 if (!send_command(command) && !select_response())
121 return SUCCESS;
122 else
123 return FAILURE;
124 }
125
126 /*
127 * IMAP SEARCH: searches the mailbox for messages that match certain criteria.
128 */
129 int imap_search(char *results)
130 {
131 char command[BIG_COMMAND_MAX];
132
133 verbose("Client request: SEARCH\n");
134
135 generate_search_filters(command);
136 if (send_command(command))
137 return FAILURE;
138 search_response(results);
139
140 verbose("Client request: SEARCH\n");
141
142 if (alimit && afilcnt) {
143 generate_search_limits(command);
144 if (send_command(command))
145 return FAILURE;
146 search_response(results);
147 }
148
149 return SUCCESS;
150 }
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 if (!send_command(command) && !fetch_response(results))
167 return SUCCESS;
168 else
169 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 * command that will be sent. This command will search for DENY, ALLOW,
207 * and DENY_LIMIT filters.
208 */
209 void generate_search_filters(char *command)
210 {
211 int len;
212
213 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 } else if (!dfilcnt) { /* If no DENY filters were defined, then
223 deny all except the ALLOW filters. */
224 len = strlen(command);
225 snprintf(command + len, BIG_COMMAND_MAX - len, " ALL");
226 }
227
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
248 len = strlen(command);
249 snprintf(command + len, BIG_COMMAND_MAX - len, " LARGER %d", alimit);
250
251 len = strlen(command);
252 snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n");
253
254 }
255
256
257 /*
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
265 if (dfilcnt > 0) {
266 while (f < (dfilcnt - 1)) {
267 len = strlen(command);
268 snprintf(command + len, BIG_COMMAND_MAX - len,
269 " OR %s%s \"%s\"",
270 custom_header(dfilters[f]->custom),
271 dfilters[f]->name, dfilters[f]->body);
272 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
283
284 /*
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
292 if (afilcnt > 0) {
293 while (f < (afilcnt - 1)) {
294 len = strlen(command);
295 snprintf(command + len, BIG_COMMAND_MAX - len,
296 " %s %s%s \"%s\"", key,
297 custom_header(afilters[f]->custom),
298 afilters[f]->name, afilters[f]->body);
299 f++;
300 }
301
302 len = strlen(command);
303 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 }
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 if (options & OPTION_SHOW_HEADERS) {
325 imap_fetch(messg, headers);
326
327 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 }
333
334 if (!(options & OPTION_TEST_MODE))
335 imap_store(messg);
336
337 } while (*unwanted);
338 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26