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