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