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

Contents of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.44 - (show annotations)
Fri Feb 13 12:17:16 2004 UTC (20 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.43: +69 -68 lines
File MIME type: text/plain
Stylistic changes.

1 #include <stdio.h>
2 #include <string.h>
3
4 #include "config.h"
5 #include "imapfilter.h"
6 #include "filter.h"
7 #include "buffer.h"
8
9
10 extern options_t opts;
11 extern connection_t connpri, connaux;
12
13 buffer_t obuf; /* Output buffer. */
14
15 static unsigned int tag = 0x1000; /* Every IMAP command is prefixed with
16 * a unique [:alnum:] string. */
17
18
19 unsigned int send_command(connection_t * conn, char *cmd, char *alt);
20 void send_command_cont(connection_t * conn, char *cmd);
21
22
23 /*
24 * Send to server data; a command.
25 */
26 unsigned int
27 send_command(connection_t * conn, char *cmd, char *alt)
28 {
29
30 debug("sending command (%s):\n\n%s\n",
31 (conn == &connpri ? "primary" : "auxiliary"),
32 (opts.debug == 1 && alt ? alt : cmd));
33
34 verbose("%s: %s", (conn == &connpri ? "C" : "c"), (alt ? alt : cmd));
35
36 socket_write(conn, cmd);
37
38 if (tag == 0xFFFF)
39 tag = 0x0FFF;
40
41 return tag++;
42 }
43
44
45 #ifdef CRAM_MD5
46 /*
47 * Send to server data: a continuation command.
48 */
49 void
50 send_command_cont(connection_t * conn, char *cmd)
51 {
52
53 debug("sending command (%s):\n\n%s\r\n\n",
54 (conn == &connpri ? "primary" : "auxiliary"), cmd);
55
56 socket_write(conn, cmd);
57 socket_write(conn, "\r\n");
58 }
59 #endif
60
61
62 /*
63 * IMAP NOOP: does nothing always succeeds.
64 */
65 int
66 imap_noop(connection_t * conn)
67 {
68
69 buffer_reset(&obuf);
70 buffer_check(&obuf, strlen("NOOP") + 12);
71
72 snprintf(obuf.data, obuf.size, "%04X NOOP\r\n", tag);
73
74 return send_command(conn, obuf.data, NULL);
75 }
76
77
78 /*
79 * IMAP CAPABILITY: requests listing of capabilities that the server supports.
80 */
81 int
82 imap_capability(connection_t * conn)
83 {
84
85 buffer_reset(&obuf);
86 buffer_check(&obuf, strlen("CAPABILITY") + 12);
87
88 snprintf(obuf.data, obuf.size, "%04X CAPABILITY\r\n", tag);
89
90 return send_command(conn, obuf.data, NULL);
91 }
92
93
94 /*
95 * IMAP NAMESPACE: discovers the prefix and delimeter of namespaces used by
96 * the server for mailboxes (RFC 2342).
97 */
98 int
99 imap_namespace(connection_t * conn)
100 {
101
102 buffer_reset(&obuf);
103 buffer_check(&obuf, strlen("NAMESPACE") + 12);
104
105 snprintf(obuf.data, obuf.size, "%04X NAMESPACE\r\n", tag);
106
107 return send_command(conn, obuf.data, NULL);
108 }
109
110
111 /*
112 * IMAP LOGOUT: informs server that client is done.
113 */
114 int
115 imap_logout(connection_t * conn)
116 {
117
118 buffer_reset(&obuf);
119 buffer_check(&obuf, strlen("LOGOUT") + 12);
120
121 snprintf(obuf.data, obuf.size, "%04X LOGOUT\r\n", tag);
122
123 return send_command(conn, obuf.data, NULL);
124 }
125
126
127 /*
128 * IMAP STARTTLS: begin TLS negotiation.
129 */
130 int
131 imap_starttls(connection_t * conn)
132 {
133
134 buffer_reset(&obuf);
135 buffer_check(&obuf, strlen("STARTTLS") + 12);
136
137 snprintf(obuf.data, obuf.size, "%04X STARTTLS\r\n", tag);
138
139 return send_command(conn, obuf.data, NULL);
140 }
141
142
143 #ifdef CRAM_MD5
144 /*
145 * IMAP AUTHENTICATE: indicates authentication mechanism and performs an
146 * authentication protocol exchange.
147 */
148 int
149 imap_authenticate(connection_t * conn, char *auth, int cont)
150 {
151
152 buffer_reset(&obuf);
153 buffer_check(&obuf, strlen("AUTHENTICATE") + 12);
154
155 if (!cont) {
156 snprintf(obuf.data, obuf.size, "%04X AUTHENTICATE %s\r\n",
157 tag, auth);
158 return send_command(conn, obuf.data, NULL);
159 } else {
160 send_command_cont(conn, auth);
161 return 0;
162 }
163 }
164 #endif
165
166
167 /*
168 * IMAP LOGIN: identifies client to server.
169 */
170 int
171 imap_login(connection_t * conn, char *user, char *pass)
172 {
173 int r, n;
174 char *sbuf;
175
176 /* Command to send to server. */
177 n = strlen("LOGIN") + strlen(user) + strlen(pass) + 18;
178 sbuf = (char *)smalloc(n);
179 snprintf(sbuf, n, "%04X LOGIN \"%s\" \"%s\"\r\n", tag, user, pass);
180
181 /* Alternate command with password shrouded for safe printing. */
182 buffer_reset(&obuf);
183 buffer_check(&obuf, strlen("LOGIN") + strlen(user) + 18);
184 snprintf(obuf.data, obuf.size, "%04X LOGIN \"%s\" *\r\n", tag, user);
185
186 r = send_command(conn, sbuf, obuf.data);
187
188 sfree(sbuf);
189
190 return r;
191 }
192
193
194 /*
195 * IMAP LIST: returns a subset of names from the complete set of names
196 * available to the client.
197 *
198 int imap_list(connection_t *conn, char *refer, char *mbox)
199 {
200
201 buffer_reset(&obuf);
202 buffer_check(&obuf, strlen("LIST") + strlen(refer) + strlen(mbox) + 18);
203
204 snprintf(obuf.data, obuf.size, "%04X LIST \"%s\" \"%s\"\r\n", tag,
205 refer, mbox);
206
207 return send_command(conn, obuf.data, NULL);
208 }*/
209
210
211 /*
212 * IMAP SUBSCRIBE: adds the specified mailbox name to the server's
213 * set of "active" or "subscribed" mailboxes.
214 */
215 int
216 imap_subscribe(connection_t * conn, char *mbox)
217 {
218
219 buffer_reset(&obuf);
220 buffer_check(&obuf, strlen("SUBSCRIBE") + strlen(mbox) + 15);
221
222 snprintf(obuf.data, obuf.size, "%04X SUBSCRIBE \"%s\"\r\n", tag, mbox);
223
224 return send_command(conn, obuf.data, NULL);
225 }
226
227
228 /*
229 * IMAP EXAMINE: access a mailbox in READ-ONLY mode.
230 *
231 int
232 imap_examine(connection_t *conn, char *mbox)
233 {
234
235 buffer_reset(&obuf);
236 buffer_check(&obuf, strlen("EXAMINE") + strlen(mbox) + 15);
237
238 snprintf(obuf.data, MEDIUM_CMD, "%04X EXAMINE \"%s\"\r\n", tag, mbox);
239
240 return send_command(conn, obuf.data, NULL);
241 }*/
242
243
244 /*
245 * IMAP SELECT: access a mailbox in READ-WRITE mode.
246 */
247 int
248 imap_select(connection_t * conn, char *mbox)
249 {
250
251 buffer_reset(&obuf);
252 buffer_check(&obuf, strlen("SELECT") + strlen(mbox) + 15);
253
254 snprintf(obuf.data, obuf.size, "%04X SELECT \"%s\"\r\n", tag, mbox);
255
256 return send_command(conn, obuf.data, NULL);
257 }
258
259
260 /*
261 * IMAP STATUS: requests status of the indicated mailbox.
262 */
263 int
264 imap_status(connection_t * conn, char *mbox, char *items)
265 {
266
267 buffer_reset(&obuf);
268 buffer_check(&obuf, strlen("STATUS") + strlen(mbox) +
269 strlen(items) + 18);
270
271 snprintf(obuf.data, obuf.size, "%04X STATUS \"%s\" (%s)\r\n", tag, mbox, items);
272
273 return send_command(conn, obuf.data, NULL);
274 }
275
276
277 /*
278 * IMAP CREATE: create mailbox.
279 */
280 int
281 imap_create(connection_t * conn, char *mbox)
282 {
283
284 buffer_reset(&obuf);
285 buffer_check(&obuf, strlen("CREATE") + strlen(mbox) + 14);
286
287 snprintf(obuf.data, obuf.size, "%04X CREATE \"%s\"\r\n", tag, mbox);
288
289 return send_command(conn, obuf.data, NULL);
290 }
291
292
293 /*
294 * IMAP SEARCH: searches the mailbox for messages that match certain criteria.
295 */
296 int
297 imap_search(connection_t * conn, char *charset, char *search)
298 {
299
300 buffer_reset(&obuf);
301 buffer_check(&obuf, strlen("SEARCH CHARSET") + strlen(charset) +
302 strlen(search) + 15);
303
304 if (*charset)
305 snprintf(obuf.data, obuf.size, "%04X SEARCH CHARSET \"%s\" %s\r\n",
306 tag, charset, search);
307 else
308 snprintf(obuf.data, obuf.size, "%04X SEARCH %s\r\n", tag, search);
309
310 return send_command(conn, obuf.data, NULL);
311 }
312
313
314 /*
315 * IMAP FETCH: retrieves data associated with a message.
316 */
317 int
318 imap_fetch(connection_t * conn, char *mesg, char *items)
319 {
320
321 buffer_reset(&obuf);
322 buffer_check(&obuf, strlen("FETCH") + strlen(mesg) + strlen(items) + 14);
323
324 snprintf(obuf.data, obuf.size, "%04X FETCH %s %s\r\n", tag, mesg, items);
325
326 return send_command(conn, obuf.data, NULL);
327 }
328
329
330 /*
331 * IMAP STORE: alters data associated with a message.
332 */
333 int
334 imap_store(connection_t * conn, char *mesg, unsigned int mode, char *flags)
335 {
336
337 buffer_reset(&obuf);
338 buffer_check(&obuf, strlen("STORE") + strlen(mesg) +
339 strlen("FLAGS.SILENT") + strlen(flags) + 18);
340
341 snprintf(obuf.data, obuf.size, "%04X STORE %s %sFLAGS.SILENT (%s)\r\n",
342 tag, mesg, (mode == ACTION_FLAG_REPLACE ? "" :
343 mode == ACTION_FLAG_ADD ? "+" : "-"), flags);
344
345 return send_command(conn, obuf.data, NULL);
346 }
347
348
349 /*
350 * IMAP COPY: copy messages to mailbox.
351 */
352 int
353 imap_copy(connection_t * conn, char *mesg, char *mbox)
354 {
355
356 buffer_reset(&obuf);
357 buffer_check(&obuf, strlen("COPY") + strlen(mesg) + strlen(mbox) + 16);
358
359 snprintf(obuf.data, obuf.size, "%04X COPY %s \"%s\"\r\n", tag, mesg, mbox);
360
361 return send_command(conn, obuf.data, NULL);
362 }
363
364
365 /*
366 * IMAP APPEND: append message to the end of a mailbox.
367 */
368 int
369 imap_append(connection_t * conn, char *mbox, char *flags, char *date, unsigned int size)
370 {
371
372 buffer_reset(&obuf);
373 buffer_check(&obuf, strlen("APPEND") + strlen(mbox) + strlen(flags) +
374 strlen(date) + strlen(ultostr(size, 10)) + 24);
375
376 snprintf(obuf.data, obuf.size, "%04X APPEND \"%s\" (%s) \"%s\" {%d}\r\n",
377 tag, mbox, flags, date, size);
378
379 return send_command(conn, obuf.data, NULL);
380 }
381
382
383
384 /*
385 * IMAP CLOSE: delete messages and return to authenticated state.
386 */
387 int
388 imap_close(connection_t * conn)
389 {
390
391 buffer_reset(&obuf);
392 buffer_check(&obuf, strlen("CLOSE") + 12);
393
394 snprintf(obuf.data, obuf.size, "%04X CLOSE\r\n", tag);
395
396 return send_command(conn, obuf.data, NULL);
397 }
398
399
400 /*
401 * IMAP EXPUNGE: permanently removes any messages with the \Deleted flag set.
402 */
403 int
404 imap_expunge(connection_t * conn)
405 {
406
407 buffer_reset(&obuf);
408 buffer_check(&obuf, strlen("EXPUNGE") + 12);
409
410 snprintf(obuf.data, obuf.size, "%04X EXPUNGE\r\n", tag);
411
412 return send_command(conn, obuf.data, NULL);
413 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26