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 |
debug("sending command (%s):\n\n%s\n", |
28 |
(conn == &connpri ? "primary" : "auxiliary"), cmd); |
29 |
verbose("%s: %s", (conn == &connpri ? "C" : "c"), 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); |
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); |
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); |
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); |
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); |
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); |
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 |
n = strlen("LOGIN") + strlen(user) + strlen(pass) + 18; |
165 |
sbuf = (char *)smalloc(n); |
166 |
|
167 |
snprintf(sbuf, n, "%04X LOGIN \"%s\" \"%s\"\r\n", tag, user, pass); |
168 |
|
169 |
r = send_command(conn, sbuf); |
170 |
|
171 |
sfree(sbuf); |
172 |
|
173 |
return r; |
174 |
} |
175 |
|
176 |
|
177 |
/* |
178 |
* IMAP LIST: returns a subset of names from the complete set of names |
179 |
* available to the client. |
180 |
* |
181 |
int imap_list(conn_t *conn, char *refer, char *mbox) |
182 |
{ |
183 |
int r; |
184 |
|
185 |
reset_buffer(&obuf); |
186 |
check_buffer(&obuf, strlen("LIST") + strlen(refer) + strlen(mbox) + 18); |
187 |
|
188 |
snprintf(obuf.data, obuf.size, "%04X LIST \"%s\" \"%s\"\r\n", tag, |
189 |
refer, mbox); |
190 |
|
191 |
r = send_command(conn, obuf.data); |
192 |
|
193 |
return r; |
194 |
}*/ |
195 |
|
196 |
|
197 |
/* |
198 |
* IMAP SUBSCRIBE: adds the specified mailbox name to the server's |
199 |
* set of "active" or "subscribed" mailboxes. |
200 |
*/ |
201 |
int |
202 |
imap_subscribe(conn_t * conn, char *mbox) |
203 |
{ |
204 |
reset_buffer(&obuf); |
205 |
check_buffer(&obuf, strlen("SUBSCRIBE") + strlen(mbox) + 15); |
206 |
|
207 |
snprintf(obuf.data, obuf.size, "%04X SUBSCRIBE \"%s\"\r\n", tag, mbox); |
208 |
|
209 |
return send_command(conn, obuf.data); |
210 |
} |
211 |
|
212 |
|
213 |
/* |
214 |
* IMAP EXAMINE: access a mailbox in READ-ONLY mode. |
215 |
* |
216 |
int |
217 |
imap_examine(conn_t *conn, char *mbox) |
218 |
{ |
219 |
reset_buffer(&obuf); |
220 |
check_buffer(&obuf, strlen("EXAMINE") + strlen(mbox) + 15); |
221 |
|
222 |
snprintf(obuf.data, MEDIUM_CMD, "%04X EXAMINE \"%s\"\r\n", tag, mbox); |
223 |
|
224 |
return send_command(conn, obuf.data); |
225 |
}*/ |
226 |
|
227 |
|
228 |
/* |
229 |
* IMAP SELECT: access a mailbox in READ-WRITE mode. |
230 |
*/ |
231 |
int |
232 |
imap_select(conn_t * conn, char *mbox) |
233 |
{ |
234 |
reset_buffer(&obuf); |
235 |
check_buffer(&obuf, strlen("SELECT") + strlen(mbox) + 15); |
236 |
|
237 |
snprintf(obuf.data, obuf.size, "%04X SELECT \"%s\"\r\n", tag, mbox); |
238 |
|
239 |
return send_command(conn, obuf.data); |
240 |
} |
241 |
|
242 |
|
243 |
/* |
244 |
* IMAP STATUS: requests status of the indicated mailbox. |
245 |
*/ |
246 |
int |
247 |
imap_status(conn_t * conn, char *mbox, char *items) |
248 |
{ |
249 |
reset_buffer(&obuf); |
250 |
check_buffer(&obuf, strlen("STATUS") + strlen(mbox) + |
251 |
strlen(items) + 18); |
252 |
|
253 |
snprintf(obuf.data, obuf.size, "%04X STATUS \"%s\" (%s)\r\n", tag, mbox, items); |
254 |
|
255 |
return send_command(conn, obuf.data); |
256 |
} |
257 |
|
258 |
|
259 |
/* |
260 |
* IMAP CREATE: create mailbox. |
261 |
*/ |
262 |
int |
263 |
imap_create(conn_t * conn, char *mbox) |
264 |
{ |
265 |
reset_buffer(&obuf); |
266 |
check_buffer(&obuf, strlen("CREATE") + strlen(mbox) + 14); |
267 |
|
268 |
snprintf(obuf.data, obuf.size, "%04X CREATE \"%s\"\r\n", tag, mbox); |
269 |
|
270 |
return send_command(conn, obuf.data); |
271 |
} |
272 |
|
273 |
|
274 |
/* |
275 |
* IMAP SEARCH: searches the mailbox for messages that match certain criteria. |
276 |
*/ |
277 |
int |
278 |
imap_search(conn_t * conn, char *charset, char *search) |
279 |
{ |
280 |
reset_buffer(&obuf); |
281 |
check_buffer(&obuf, strlen("SEARCH CHARSET") + strlen(charset) + |
282 |
strlen(search) + 15); |
283 |
|
284 |
if (*charset) |
285 |
snprintf(obuf.data, obuf.size, "%04X SEARCH CHARSET \"%s\" %s\r\n", |
286 |
tag, charset, search); |
287 |
else |
288 |
snprintf(obuf.data, obuf.size, "%04X SEARCH %s\r\n", tag, search); |
289 |
|
290 |
return send_command(conn, obuf.data); |
291 |
} |
292 |
|
293 |
|
294 |
/* |
295 |
* IMAP FETCH: retrieves data associated with a message. |
296 |
*/ |
297 |
int |
298 |
imap_fetch(conn_t * conn, char *mesg, char *items) |
299 |
{ |
300 |
reset_buffer(&obuf); |
301 |
check_buffer(&obuf, strlen("FETCH") + strlen(mesg) + strlen(items) + 14); |
302 |
|
303 |
snprintf(obuf.data, obuf.size, "%04X FETCH %s %s\r\n", tag, mesg, items); |
304 |
|
305 |
return send_command(conn, obuf.data); |
306 |
} |
307 |
|
308 |
|
309 |
/* |
310 |
* IMAP STORE: alters data associated with a message. |
311 |
*/ |
312 |
int |
313 |
imap_store(conn_t * conn, char *mesg, unsigned int mode, char *flags) |
314 |
{ |
315 |
reset_buffer(&obuf); |
316 |
check_buffer(&obuf, strlen("STORE") + strlen(mesg) + |
317 |
strlen("FLAGS.SILENT") + strlen(flags) + 18); |
318 |
|
319 |
snprintf(obuf.data, obuf.size, "%04X STORE %s %sFLAGS.SILENT (%s)\r\n", |
320 |
tag, mesg, (mode == STORE_FLAG_REPLACE ? "" : |
321 |
mode == STORE_FLAG_ADD ? "+" : "-"), flags); |
322 |
|
323 |
return send_command(conn, obuf.data); |
324 |
} |
325 |
|
326 |
|
327 |
/* |
328 |
* IMAP COPY: copy messages to mailbox. |
329 |
*/ |
330 |
int |
331 |
imap_copy(conn_t * conn, char *mesg, char *mbox) |
332 |
{ |
333 |
reset_buffer(&obuf); |
334 |
check_buffer(&obuf, strlen("COPY") + strlen(mesg) + strlen(mbox) + 16); |
335 |
|
336 |
snprintf(obuf.data, obuf.size, "%04X COPY %s \"%s\"\r\n", tag, mesg, mbox); |
337 |
|
338 |
return send_command(conn, obuf.data); |
339 |
} |
340 |
|
341 |
|
342 |
/* |
343 |
* IMAP APPEND: append message to the end of a mailbox. |
344 |
*/ |
345 |
int |
346 |
imap_append(conn_t * conn, char *mbox, char *flags, char *date, unsigned int size) |
347 |
{ |
348 |
reset_buffer(&obuf); |
349 |
check_buffer(&obuf, strlen("APPEND") + strlen(mbox) + strlen(flags) + |
350 |
strlen(date) + strlen(ultostr(size, 10)) + 24); |
351 |
|
352 |
snprintf(obuf.data, obuf.size, "%04X APPEND \"%s\" (%s) \"%s\" {%d}\r\n", |
353 |
tag, mbox, flags, date, size); |
354 |
|
355 |
return send_command(conn, obuf.data); |
356 |
} |
357 |
|
358 |
|
359 |
|
360 |
/* |
361 |
* IMAP CLOSE: delete messages and return to authenticated state. |
362 |
*/ |
363 |
int |
364 |
imap_close(conn_t * conn) |
365 |
{ |
366 |
reset_buffer(&obuf); |
367 |
check_buffer(&obuf, strlen("CLOSE") + 12); |
368 |
|
369 |
snprintf(obuf.data, obuf.size, "%04X CLOSE\r\n", tag); |
370 |
|
371 |
return send_command(conn, obuf.data); |
372 |
} |
373 |
|
374 |
|
375 |
/* |
376 |
* IMAP EXPUNGE: permanently removes any messages with the \Deleted flag set. |
377 |
*/ |
378 |
int |
379 |
imap_expunge(conn_t * conn) |
380 |
{ |
381 |
reset_buffer(&obuf); |
382 |
check_buffer(&obuf, strlen("EXPUNGE") + 12); |
383 |
|
384 |
snprintf(obuf.data, obuf.size, "%04X EXPUNGE\r\n", tag); |
385 |
|
386 |
return send_command(conn, obuf.data); |
387 |
} |