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