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

Contents of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.38.2.2 - (show annotations)
Tue Jan 20 01:49:44 2004 UTC (20 years, 2 months ago) by lefcha
Branch: release-0_9-patches
Changes since 1.38.2.1: +5 -5 lines
File MIME type: text/plain
Try to preserve message flags and internal date when appending.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26