3 |
|
|
4 |
#include "config.h" |
#include "config.h" |
5 |
#include "imapfilter.h" |
#include "imapfilter.h" |
6 |
|
#include "filter.h" |
7 |
#include "buffer.h" |
#include "buffer.h" |
8 |
|
|
9 |
|
|
10 |
extern opts_t opts; |
extern options_t opts; |
11 |
extern conn_t connpri, connaux; |
extern connection_t connpri, connaux; |
12 |
|
|
13 |
buffer_t obuf; /* Output buffer. */ |
buffer_t obuf; /* Output buffer. */ |
14 |
|
|
16 |
* a unique [:alnum:] string. */ |
* a unique [:alnum:] string. */ |
17 |
|
|
18 |
|
|
19 |
unsigned int send_command(conn_t * conn, char *cmd, char *alt); |
unsigned int send_command(connection_t * conn, char *cmd, char *alt); |
20 |
void send_command_cont(conn_t * conn, char *cmd); |
void send_command_cont(connection_t * conn, char *cmd); |
21 |
|
|
22 |
|
|
23 |
/* |
/* |
24 |
* Send to server data; a command. |
* Send to server data; a command. |
25 |
*/ |
*/ |
26 |
unsigned int |
unsigned int |
27 |
send_command(conn_t * conn, char *cmd, char *alt) |
send_command(connection_t * conn, char *cmd, char *alt) |
28 |
{ |
{ |
29 |
|
|
30 |
debug("sending command (%s):\n\n%s\n", |
debug("sending command (%s):\n\n%s\n", |
47 |
* Send to server data: a continuation command. |
* Send to server data: a continuation command. |
48 |
*/ |
*/ |
49 |
void |
void |
50 |
send_command_cont(conn_t * conn, char *cmd) |
send_command_cont(connection_t * conn, char *cmd) |
51 |
{ |
{ |
52 |
|
|
53 |
debug("sending command (%s):\n\n%s\r\n\n", |
debug("sending command (%s):\n\n%s\r\n\n", |
63 |
* IMAP NOOP: does nothing always succeeds. |
* IMAP NOOP: does nothing always succeeds. |
64 |
*/ |
*/ |
65 |
int |
int |
66 |
imap_noop(conn_t * conn) |
imap_noop(connection_t * conn) |
67 |
{ |
{ |
68 |
|
|
69 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
70 |
check_buffer(&obuf, strlen("NOOP") + 12); |
buffer_check(&obuf, strlen("NOOP") + 12); |
71 |
|
|
72 |
snprintf(obuf.data, obuf.size, "%04X NOOP\r\n", tag); |
snprintf(obuf.data, obuf.size, "%04X NOOP\r\n", tag); |
73 |
|
|
79 |
* IMAP CAPABILITY: requests listing of capabilities that the server supports. |
* IMAP CAPABILITY: requests listing of capabilities that the server supports. |
80 |
*/ |
*/ |
81 |
int |
int |
82 |
imap_capability(conn_t * conn) |
imap_capability(connection_t * conn) |
83 |
{ |
{ |
84 |
|
|
85 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
86 |
check_buffer(&obuf, strlen("CAPABILITY") + 12); |
buffer_check(&obuf, strlen("CAPABILITY") + 12); |
87 |
|
|
88 |
snprintf(obuf.data, obuf.size, "%04X CAPABILITY\r\n", tag); |
snprintf(obuf.data, obuf.size, "%04X CAPABILITY\r\n", tag); |
89 |
|
|
96 |
* the server for mailboxes (RFC 2342). |
* the server for mailboxes (RFC 2342). |
97 |
*/ |
*/ |
98 |
int |
int |
99 |
imap_namespace(conn_t * conn) |
imap_namespace(connection_t * conn) |
100 |
{ |
{ |
101 |
|
|
102 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
103 |
check_buffer(&obuf, strlen("NAMESPACE") + 12); |
buffer_check(&obuf, strlen("NAMESPACE") + 12); |
104 |
|
|
105 |
snprintf(obuf.data, obuf.size, "%04X NAMESPACE\r\n", tag); |
snprintf(obuf.data, obuf.size, "%04X NAMESPACE\r\n", tag); |
106 |
|
|
112 |
* IMAP LOGOUT: informs server that client is done. |
* IMAP LOGOUT: informs server that client is done. |
113 |
*/ |
*/ |
114 |
int |
int |
115 |
imap_logout(conn_t * conn) |
imap_logout(connection_t * conn) |
116 |
{ |
{ |
117 |
|
|
118 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
119 |
check_buffer(&obuf, strlen("LOGOUT") + 12); |
buffer_check(&obuf, strlen("LOGOUT") + 12); |
120 |
|
|
121 |
snprintf(obuf.data, obuf.size, "%04X LOGOUT\r\n", tag); |
snprintf(obuf.data, obuf.size, "%04X LOGOUT\r\n", tag); |
122 |
|
|
128 |
* IMAP STARTTLS: begin TLS negotiation. |
* IMAP STARTTLS: begin TLS negotiation. |
129 |
*/ |
*/ |
130 |
int |
int |
131 |
imap_starttls(conn_t * conn) |
imap_starttls(connection_t * conn) |
132 |
{ |
{ |
133 |
|
|
134 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
135 |
check_buffer(&obuf, strlen("STARTTLS") + 12); |
buffer_check(&obuf, strlen("STARTTLS") + 12); |
136 |
|
|
137 |
snprintf(obuf.data, obuf.size, "%04X STARTTLS\r\n", tag); |
snprintf(obuf.data, obuf.size, "%04X STARTTLS\r\n", tag); |
138 |
|
|
146 |
* authentication protocol exchange. |
* authentication protocol exchange. |
147 |
*/ |
*/ |
148 |
int |
int |
149 |
imap_authenticate(conn_t * conn, char *auth, int cont) |
imap_authenticate(connection_t * conn, char *auth, int cont) |
150 |
{ |
{ |
151 |
|
|
152 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
153 |
check_buffer(&obuf, strlen("AUTHENTICATE") + 12); |
buffer_check(&obuf, strlen("AUTHENTICATE") + 12); |
154 |
|
|
155 |
if (!cont) { |
if (!cont) { |
156 |
snprintf(obuf.data, obuf.size, "%04X AUTHENTICATE %s\r\n", |
snprintf(obuf.data, obuf.size, "%04X AUTHENTICATE %s\r\n", |
168 |
* IMAP LOGIN: identifies client to server. |
* IMAP LOGIN: identifies client to server. |
169 |
*/ |
*/ |
170 |
int |
int |
171 |
imap_login(conn_t * conn, char *user, char *pass) |
imap_login(connection_t * conn, char *user, char *pass) |
172 |
{ |
{ |
173 |
int r, n; |
int r, n; |
174 |
char *sbuf; |
char *sbuf; |
179 |
snprintf(sbuf, n, "%04X LOGIN \"%s\" \"%s\"\r\n", tag, user, pass); |
snprintf(sbuf, n, "%04X LOGIN \"%s\" \"%s\"\r\n", tag, user, pass); |
180 |
|
|
181 |
/* Alternate command with password shrouded for safe printing. */ |
/* Alternate command with password shrouded for safe printing. */ |
182 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
183 |
check_buffer(&obuf, strlen("LOGIN") + strlen(user) + 18); |
buffer_check(&obuf, strlen("LOGIN") + strlen(user) + 18); |
184 |
snprintf(obuf.data, obuf.size, "%04X LOGIN \"%s\" *\r\n", tag, user); |
snprintf(obuf.data, obuf.size, "%04X LOGIN \"%s\" *\r\n", tag, user); |
185 |
|
|
186 |
r = send_command(conn, sbuf, obuf.data); |
r = send_command(conn, sbuf, obuf.data); |
195 |
* IMAP LIST: returns a subset of names from the complete set of names |
* IMAP LIST: returns a subset of names from the complete set of names |
196 |
* available to the client. |
* available to the client. |
197 |
* |
* |
198 |
int imap_list(conn_t *conn, char *refer, char *mbox) |
int imap_list(connection_t *conn, char *refer, char *mbox) |
199 |
{ |
{ |
200 |
|
|
201 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
202 |
check_buffer(&obuf, strlen("LIST") + strlen(refer) + strlen(mbox) + 18); |
buffer_check(&obuf, strlen("LIST") + strlen(refer) + strlen(mbox) + 18); |
203 |
|
|
204 |
snprintf(obuf.data, obuf.size, "%04X LIST \"%s\" \"%s\"\r\n", tag, |
snprintf(obuf.data, obuf.size, "%04X LIST \"%s\" \"%s\"\r\n", tag, |
205 |
refer, mbox); |
refer, mbox); |
213 |
* set of "active" or "subscribed" mailboxes. |
* set of "active" or "subscribed" mailboxes. |
214 |
*/ |
*/ |
215 |
int |
int |
216 |
imap_subscribe(conn_t * conn, char *mbox) |
imap_subscribe(connection_t * conn, char *mbox) |
217 |
{ |
{ |
218 |
|
|
219 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
220 |
check_buffer(&obuf, strlen("SUBSCRIBE") + strlen(mbox) + 15); |
buffer_check(&obuf, strlen("SUBSCRIBE") + strlen(mbox) + 15); |
221 |
|
|
222 |
snprintf(obuf.data, obuf.size, "%04X SUBSCRIBE \"%s\"\r\n", tag, mbox); |
snprintf(obuf.data, obuf.size, "%04X SUBSCRIBE \"%s\"\r\n", tag, mbox); |
223 |
|
|
229 |
* IMAP EXAMINE: access a mailbox in READ-ONLY mode. |
* IMAP EXAMINE: access a mailbox in READ-ONLY mode. |
230 |
* |
* |
231 |
int |
int |
232 |
imap_examine(conn_t *conn, char *mbox) |
imap_examine(connection_t *conn, char *mbox) |
233 |
{ |
{ |
234 |
|
|
235 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
236 |
check_buffer(&obuf, strlen("EXAMINE") + strlen(mbox) + 15); |
buffer_check(&obuf, strlen("EXAMINE") + strlen(mbox) + 15); |
237 |
|
|
238 |
snprintf(obuf.data, MEDIUM_CMD, "%04X EXAMINE \"%s\"\r\n", tag, mbox); |
snprintf(obuf.data, MEDIUM_CMD, "%04X EXAMINE \"%s\"\r\n", tag, mbox); |
239 |
|
|
245 |
* IMAP SELECT: access a mailbox in READ-WRITE mode. |
* IMAP SELECT: access a mailbox in READ-WRITE mode. |
246 |
*/ |
*/ |
247 |
int |
int |
248 |
imap_select(conn_t * conn, char *mbox) |
imap_select(connection_t * conn, char *mbox) |
249 |
{ |
{ |
250 |
|
|
251 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
252 |
check_buffer(&obuf, strlen("SELECT") + strlen(mbox) + 15); |
buffer_check(&obuf, strlen("SELECT") + strlen(mbox) + 15); |
253 |
|
|
254 |
snprintf(obuf.data, obuf.size, "%04X SELECT \"%s\"\r\n", tag, mbox); |
snprintf(obuf.data, obuf.size, "%04X SELECT \"%s\"\r\n", tag, mbox); |
255 |
|
|
261 |
* IMAP STATUS: requests status of the indicated mailbox. |
* IMAP STATUS: requests status of the indicated mailbox. |
262 |
*/ |
*/ |
263 |
int |
int |
264 |
imap_status(conn_t * conn, char *mbox, char *items) |
imap_status(connection_t * conn, char *mbox, char *items) |
265 |
{ |
{ |
266 |
|
|
267 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
268 |
check_buffer(&obuf, strlen("STATUS") + strlen(mbox) + |
buffer_check(&obuf, strlen("STATUS") + strlen(mbox) + |
269 |
strlen(items) + 18); |
strlen(items) + 18); |
270 |
|
|
271 |
snprintf(obuf.data, obuf.size, "%04X STATUS \"%s\" (%s)\r\n", tag, mbox, items); |
snprintf(obuf.data, obuf.size, "%04X STATUS \"%s\" (%s)\r\n", tag, mbox, items); |
278 |
* IMAP CREATE: create mailbox. |
* IMAP CREATE: create mailbox. |
279 |
*/ |
*/ |
280 |
int |
int |
281 |
imap_create(conn_t * conn, char *mbox) |
imap_create(connection_t * conn, char *mbox) |
282 |
{ |
{ |
283 |
|
|
284 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
285 |
check_buffer(&obuf, strlen("CREATE") + strlen(mbox) + 14); |
buffer_check(&obuf, strlen("CREATE") + strlen(mbox) + 14); |
286 |
|
|
287 |
snprintf(obuf.data, obuf.size, "%04X CREATE \"%s\"\r\n", tag, mbox); |
snprintf(obuf.data, obuf.size, "%04X CREATE \"%s\"\r\n", tag, mbox); |
288 |
|
|
294 |
* IMAP SEARCH: searches the mailbox for messages that match certain criteria. |
* IMAP SEARCH: searches the mailbox for messages that match certain criteria. |
295 |
*/ |
*/ |
296 |
int |
int |
297 |
imap_search(conn_t * conn, char *charset, char *search) |
imap_search(connection_t * conn, char *charset, char *search) |
298 |
{ |
{ |
299 |
|
|
300 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
301 |
check_buffer(&obuf, strlen("SEARCH CHARSET") + strlen(charset) + |
buffer_check(&obuf, strlen("SEARCH CHARSET") + strlen(charset) + |
302 |
strlen(search) + 15); |
strlen(search) + 15); |
303 |
|
|
304 |
if (*charset) |
if (*charset) |
315 |
* IMAP FETCH: retrieves data associated with a message. |
* IMAP FETCH: retrieves data associated with a message. |
316 |
*/ |
*/ |
317 |
int |
int |
318 |
imap_fetch(conn_t * conn, char *mesg, char *items) |
imap_fetch(connection_t * conn, char *mesg, char *items) |
319 |
{ |
{ |
320 |
|
|
321 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
322 |
check_buffer(&obuf, strlen("FETCH") + strlen(mesg) + strlen(items) + 14); |
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); |
snprintf(obuf.data, obuf.size, "%04X FETCH %s %s\r\n", tag, mesg, items); |
325 |
|
|
331 |
* IMAP STORE: alters data associated with a message. |
* IMAP STORE: alters data associated with a message. |
332 |
*/ |
*/ |
333 |
int |
int |
334 |
imap_store(conn_t * conn, char *mesg, unsigned int mode, char *flags) |
imap_store(connection_t * conn, char *mesg, unsigned int mode, char *flags) |
335 |
{ |
{ |
336 |
|
|
337 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
338 |
check_buffer(&obuf, strlen("STORE") + strlen(mesg) + |
buffer_check(&obuf, strlen("STORE") + strlen(mesg) + |
339 |
strlen("FLAGS.SILENT") + strlen(flags) + 18); |
strlen("FLAGS.SILENT") + strlen(flags) + 18); |
340 |
|
|
341 |
snprintf(obuf.data, obuf.size, "%04X STORE %s %sFLAGS.SILENT (%s)\r\n", |
snprintf(obuf.data, obuf.size, "%04X STORE %s %sFLAGS.SILENT (%s)\r\n", |
342 |
tag, mesg, (mode == STORE_FLAG_REPLACE ? "" : |
tag, mesg, (mode == ACTION_FLAG_REPLACE ? "" : |
343 |
mode == STORE_FLAG_ADD ? "+" : "-"), flags); |
mode == ACTION_FLAG_ADD ? "+" : "-"), flags); |
344 |
|
|
345 |
return send_command(conn, obuf.data, NULL); |
return send_command(conn, obuf.data, NULL); |
346 |
} |
} |
350 |
* IMAP COPY: copy messages to mailbox. |
* IMAP COPY: copy messages to mailbox. |
351 |
*/ |
*/ |
352 |
int |
int |
353 |
imap_copy(conn_t * conn, char *mesg, char *mbox) |
imap_copy(connection_t * conn, char *mesg, char *mbox) |
354 |
{ |
{ |
355 |
|
|
356 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
357 |
check_buffer(&obuf, strlen("COPY") + strlen(mesg) + strlen(mbox) + 16); |
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); |
snprintf(obuf.data, obuf.size, "%04X COPY %s \"%s\"\r\n", tag, mesg, mbox); |
360 |
|
|
366 |
* IMAP APPEND: append message to the end of a mailbox. |
* IMAP APPEND: append message to the end of a mailbox. |
367 |
*/ |
*/ |
368 |
int |
int |
369 |
imap_append(conn_t * conn, char *mbox, char *flags, char *date, unsigned int size) |
imap_append(connection_t * conn, char *mbox, char *flags, char *date, unsigned int size) |
370 |
{ |
{ |
371 |
|
|
372 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
373 |
check_buffer(&obuf, strlen("APPEND") + strlen(mbox) + strlen(flags) + |
buffer_check(&obuf, strlen("APPEND") + strlen(mbox) + strlen(flags) + |
374 |
strlen(date) + strlen(ultostr(size, 10)) + 24); |
strlen(date) + strlen(ultostr(size, 10)) + 24); |
375 |
|
|
376 |
snprintf(obuf.data, obuf.size, "%04X APPEND \"%s\" (%s) \"%s\" {%d}\r\n", |
snprintf(obuf.data, obuf.size, "%04X APPEND \"%s\" (%s) \"%s\" {%d}\r\n", |
385 |
* IMAP CLOSE: delete messages and return to authenticated state. |
* IMAP CLOSE: delete messages and return to authenticated state. |
386 |
*/ |
*/ |
387 |
int |
int |
388 |
imap_close(conn_t * conn) |
imap_close(connection_t * conn) |
389 |
{ |
{ |
390 |
|
|
391 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
392 |
check_buffer(&obuf, strlen("CLOSE") + 12); |
buffer_check(&obuf, strlen("CLOSE") + 12); |
393 |
|
|
394 |
snprintf(obuf.data, obuf.size, "%04X CLOSE\r\n", tag); |
snprintf(obuf.data, obuf.size, "%04X CLOSE\r\n", tag); |
395 |
|
|
401 |
* IMAP EXPUNGE: permanently removes any messages with the \Deleted flag set. |
* IMAP EXPUNGE: permanently removes any messages with the \Deleted flag set. |
402 |
*/ |
*/ |
403 |
int |
int |
404 |
imap_expunge(conn_t * conn) |
imap_expunge(connection_t * conn) |
405 |
{ |
{ |
406 |
|
|
407 |
reset_buffer(&obuf); |
buffer_reset(&obuf); |
408 |
check_buffer(&obuf, strlen("EXPUNGE") + 12); |
buffer_check(&obuf, strlen("EXPUNGE") + 12); |
409 |
|
|
410 |
snprintf(obuf.data, obuf.size, "%04X EXPUNGE\r\n", tag); |
snprintf(obuf.data, obuf.size, "%04X EXPUNGE\r\n", tag); |
411 |
|
|