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

Contents of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.45 - (show annotations)
Sat Feb 14 19:14:43 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
Changes since 1.44: +20 -14 lines
File MIME type: text/plain
Indentation.

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 the
96 * 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 set of
213 * "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,
272 mbox, items);
273
274 return send_command(conn, obuf.data, NULL);
275 }
276
277
278 /*
279 * IMAP CREATE: create mailbox.
280 */
281 int
282 imap_create(connection_t * conn, char *mbox)
283 {
284
285 buffer_reset(&obuf);
286 buffer_check(&obuf, strlen("CREATE") + strlen(mbox) + 14);
287
288 snprintf(obuf.data, obuf.size, "%04X CREATE \"%s\"\r\n", tag, mbox);
289
290 return send_command(conn, obuf.data, NULL);
291 }
292
293
294 /*
295 * IMAP SEARCH: searches the mailbox for messages that match certain criteria.
296 */
297 int
298 imap_search(connection_t * conn, char *charset, char *search)
299 {
300
301 buffer_reset(&obuf);
302 buffer_check(&obuf, strlen("SEARCH CHARSET") + strlen(charset) +
303 strlen(search) + 15);
304
305 if (*charset)
306 snprintf(obuf.data, obuf.size,
307 "%04X SEARCH CHARSET \"%s\" %s\r\n", tag, charset, search);
308 else
309 snprintf(obuf.data, obuf.size, "%04X SEARCH %s\r\n", tag,
310 search);
311
312 return send_command(conn, obuf.data, NULL);
313 }
314
315
316 /*
317 * IMAP FETCH: retrieves data associated with a message.
318 */
319 int
320 imap_fetch(connection_t * conn, char *mesg, char *items)
321 {
322
323 buffer_reset(&obuf);
324 buffer_check(&obuf,
325 strlen("FETCH") + strlen(mesg) + strlen(items) + 14);
326
327 snprintf(obuf.data, obuf.size, "%04X FETCH %s %s\r\n", tag, mesg,
328 items);
329
330 return send_command(conn, obuf.data, NULL);
331 }
332
333
334 /*
335 * IMAP STORE: alters data associated with a message.
336 */
337 int
338 imap_store(connection_t * conn, char *mesg, unsigned int mode, char *flags)
339 {
340
341 buffer_reset(&obuf);
342 buffer_check(&obuf, strlen("STORE") + strlen(mesg) +
343 strlen("FLAGS.SILENT") + strlen(flags) + 18);
344
345 snprintf(obuf.data, obuf.size, "%04X STORE %s %sFLAGS.SILENT (%s)\r\n",
346 tag, mesg, (mode == ACTION_FLAG_REPLACE ? "" :
347 mode == ACTION_FLAG_ADD ? "+" : "-"), flags);
348
349 return send_command(conn, obuf.data, NULL);
350 }
351
352
353 /*
354 * IMAP COPY: copy messages to mailbox.
355 */
356 int
357 imap_copy(connection_t * conn, char *mesg, char *mbox)
358 {
359
360 buffer_reset(&obuf);
361 buffer_check(&obuf, strlen("COPY") + strlen(mesg) + strlen(mbox) + 16);
362
363 snprintf(obuf.data, obuf.size, "%04X COPY %s \"%s\"\r\n", tag, mesg,
364 mbox);
365
366 return send_command(conn, obuf.data, NULL);
367 }
368
369
370 /*
371 * IMAP APPEND: append message to the end of a mailbox.
372 */
373 int
374 imap_append(connection_t * conn, char *mbox, char *flags, char *date, unsigned int size)
375 {
376
377 buffer_reset(&obuf);
378 buffer_check(&obuf, strlen("APPEND") + strlen(mbox) + strlen(flags) +
379 strlen(date) + strlen(ultostr(size, 10)) + 24);
380
381 snprintf(obuf.data, obuf.size,
382 "%04X APPEND \"%s\" (%s) \"%s\" {%d}\r\n", tag, mbox, flags, date,
383 size);
384
385 return send_command(conn, obuf.data, NULL);
386 }
387
388
389
390 /*
391 * IMAP CLOSE: delete messages and return to authenticated state.
392 */
393 int
394 imap_close(connection_t * conn)
395 {
396
397 buffer_reset(&obuf);
398 buffer_check(&obuf, strlen("CLOSE") + 12);
399
400 snprintf(obuf.data, obuf.size, "%04X CLOSE\r\n", tag);
401
402 return send_command(conn, obuf.data, NULL);
403 }
404
405
406 /*
407 * IMAP EXPUNGE: permanently removes any messages with the \Deleted flag set.
408 */
409 int
410 imap_expunge(connection_t * conn)
411 {
412
413 buffer_reset(&obuf);
414 buffer_check(&obuf, strlen("EXPUNGE") + 12);
415
416 snprintf(obuf.data, obuf.size, "%04X EXPUNGE\r\n", tag);
417
418 return send_command(conn, obuf.data, NULL);
419 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26