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

Annotation of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.36 - (hide annotations)
Sun Jul 27 15:54:49 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.35: +101 -73 lines
File MIME type: text/plain
Use new *_buffer() routines for input/output buffers.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26