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

Contents of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.37 - (show annotations)
Sun Jul 27 17:39:45 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.36: +50 -50 lines
File MIME type: text/plain
New structure to hold information about connection's socket, ssl socket, capabilities, namespace.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26