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 |
#ifdef CRAM_MD5 |
115 |
/* |
116 |
* IMAP AUTHENTICATE: indicates authentication mechanism and performs an |
117 |
* authentication protocol exchange. |
118 |
*/ |
119 |
int |
120 |
imap_authenticate(int *sock, char *auth, int cont) |
121 |
{ |
122 |
char cmd[MEDIUM_CMD]; |
123 |
|
124 |
if (!cont) { |
125 |
snprintf(cmd, MEDIUM_CMD, "%X AUTHENTICATE %s\r\n", tag, auth); |
126 |
return send_command(sock, cmd); |
127 |
} else { |
128 |
send_command_cont(sock, auth); |
129 |
return 0; |
130 |
} |
131 |
} |
132 |
|
133 |
#endif |
134 |
|
135 |
|
136 |
/* |
137 |
* IMAP LOGIN: identifies client to server. |
138 |
*/ |
139 |
int |
140 |
imap_login(int *sock, char *user, char *pass) |
141 |
{ |
142 |
int r; |
143 |
char *cmd; |
144 |
|
145 |
cmd = (char *)smalloc(MEDIUM_CMD); |
146 |
|
147 |
snprintf(cmd, MEDIUM_CMD, "%X LOGIN \"%s\" \"%s\"\r\n", tag, user, |
148 |
pass); |
149 |
|
150 |
r = send_command(sock, cmd); |
151 |
|
152 |
sfree(cmd); |
153 |
|
154 |
return r; |
155 |
} |
156 |
|
157 |
|
158 |
/* |
159 |
* IMAP LIST: returns a subset of names from the complete set of names |
160 |
* available to the client. |
161 |
* |
162 |
int imap_list(int *sock, char *refer, char *mbox) |
163 |
{ |
164 |
int r; |
165 |
char cmd[BIG_CMD]; |
166 |
|
167 |
snprintf(cmd, MEDIUM_CMD, "%X LIST \"%s\" \"%s\"\r\n", tag, refer, |
168 |
mbox); |
169 |
|
170 |
r = send_command(sock, cmd); |
171 |
|
172 |
return r; |
173 |
}*/ |
174 |
|
175 |
|
176 |
/* |
177 |
* IMAP SUBSCRIBE: adds the specified mailbox name to the server's |
178 |
* set of "active" or "subscribed" mailboxes. |
179 |
*/ |
180 |
int |
181 |
imap_subscribe(int *sock, char *mbox) |
182 |
{ |
183 |
char cmd[MEDIUM_CMD]; |
184 |
|
185 |
snprintf(cmd, MEDIUM_CMD, "%X SUBSCRIBE \"%s\"\r\n", tag, mbox); |
186 |
|
187 |
return send_command(sock, cmd); |
188 |
} |
189 |
|
190 |
|
191 |
/* |
192 |
* IMAP EXAMINE: access a mailbox in READ-ONLY mode. |
193 |
* |
194 |
int |
195 |
imap_examine(int *sock, char *mbox) |
196 |
{ |
197 |
char cmd[MEDIUM_CMD]; |
198 |
|
199 |
snprintf(cmd, MEDIUM_CMD, "%X EXAMINE \"%s\"\r\n", tag, mbox); |
200 |
|
201 |
return send_command(sock, cmd); |
202 |
}*/ |
203 |
|
204 |
|
205 |
/* |
206 |
* IMAP SELECT: access a mailbox in READ-WRITE mode. |
207 |
*/ |
208 |
int |
209 |
imap_select(int *sock, char *mbox) |
210 |
{ |
211 |
char cmd[SMALL_CMD]; |
212 |
|
213 |
snprintf(cmd, SMALL_CMD, "%X SELECT \"%s\"\r\n", tag, mbox); |
214 |
|
215 |
return send_command(sock, cmd); |
216 |
} |
217 |
|
218 |
|
219 |
/* |
220 |
* IMAP STATUS: requests status of the indicated mailbox. |
221 |
*/ |
222 |
int |
223 |
imap_status(int *sock, char *mbox, char *items) |
224 |
{ |
225 |
char cmd[MEDIUM_CMD]; |
226 |
|
227 |
snprintf(cmd, MEDIUM_CMD, "%X STATUS \"%s\" (%s)\r\n", tag, mbox, items); |
228 |
|
229 |
return send_command(sock, cmd); |
230 |
} |
231 |
|
232 |
|
233 |
/* |
234 |
* IMAP CREATE: create mailbox. |
235 |
*/ |
236 |
int |
237 |
imap_create(int *sock, char *mbox) |
238 |
{ |
239 |
char cmd[MEDIUM_CMD]; |
240 |
|
241 |
snprintf(cmd, MEDIUM_CMD, "%X CREATE \"%s\"\r\n", tag, mbox); |
242 |
|
243 |
return send_command(sock, cmd); |
244 |
} |
245 |
|
246 |
|
247 |
/* |
248 |
* IMAP SEARCH: searches the mailbox for messages that match certain criteria. |
249 |
*/ |
250 |
int |
251 |
imap_search(int *sock, char *charset, char *search) |
252 |
{ |
253 |
char cmd[BIG_CMD]; |
254 |
|
255 |
if (*charset) |
256 |
snprintf(cmd, BIG_CMD, "%X SEARCH CHARSET \"%s\" %s\r\n", tag, |
257 |
charset, search); |
258 |
else |
259 |
snprintf(cmd, BIG_CMD, "%X SEARCH %s\r\n", tag, search); |
260 |
|
261 |
return send_command(sock, cmd); |
262 |
} |
263 |
|
264 |
|
265 |
/* |
266 |
* IMAP FETCH: retrieves data associated with a message. |
267 |
*/ |
268 |
int |
269 |
imap_fetch(int *sock, char *mesg, char *items) |
270 |
{ |
271 |
char cmd[MEDIUM_CMD]; |
272 |
|
273 |
snprintf(cmd, MEDIUM_CMD, "%X FETCH %s %s\r\n", tag, mesg, items); |
274 |
|
275 |
return send_command(sock, cmd); |
276 |
} |
277 |
|
278 |
|
279 |
/* |
280 |
* IMAP STORE: alters data associated with a message. |
281 |
*/ |
282 |
int |
283 |
imap_store(int *sock, char *mesg, unsigned int mode, char *flags) |
284 |
{ |
285 |
char cmd[MEDIUM_CMD]; |
286 |
|
287 |
snprintf(cmd, MEDIUM_CMD, "%X STORE %s %sFLAGS.SILENT (%s)\r\n", tag, |
288 |
mesg, (mode == STORE_FLAG_REPLACE ? "" : |
289 |
mode == STORE_FLAG_ADD ? "+" : "-"), flags); |
290 |
|
291 |
return send_command(sock, cmd); |
292 |
} |
293 |
|
294 |
|
295 |
/* |
296 |
* IMAP COPY: copy messages to mailbox. |
297 |
*/ |
298 |
int |
299 |
imap_copy(int *sock, char *mesg, char *mbox) |
300 |
{ |
301 |
char cmd[MEDIUM_CMD]; |
302 |
|
303 |
snprintf(cmd, MEDIUM_CMD, "%X COPY %s \"%s\"\r\n", tag, mesg, mbox); |
304 |
|
305 |
return send_command(sock, cmd); |
306 |
} |
307 |
|
308 |
|
309 |
/* |
310 |
* IMAP APPEND: append message to the end of a mailbox. |
311 |
*/ |
312 |
int |
313 |
imap_append(int *sock, char *mbox, unsigned int size) |
314 |
{ |
315 |
char cmd[MEDIUM_CMD]; |
316 |
|
317 |
snprintf(cmd, MEDIUM_CMD, "%X APPEND \"%s\" {%d}\r\n", tag, mbox, size); |
318 |
|
319 |
return send_command(sock, cmd); |
320 |
} |
321 |
|
322 |
|
323 |
|
324 |
/* |
325 |
* IMAP CLOSE: delete messages and return to authenticated state. |
326 |
*/ |
327 |
int |
328 |
imap_close(int *sock) |
329 |
{ |
330 |
char cmd[SMALL_CMD]; |
331 |
|
332 |
snprintf(cmd, SMALL_CMD, "%X CLOSE\r\n", tag); |
333 |
|
334 |
return send_command(sock, cmd); |
335 |
} |
336 |
|
337 |
|
338 |
/* |
339 |
* IMAP EXPUNGE: permanently removes any messages with the \Deleted flag set. |
340 |
*/ |
341 |
int |
342 |
imap_expunge(int *sock) |
343 |
{ |
344 |
char cmd[SMALL_CMD]; |
345 |
|
346 |
snprintf(cmd, SMALL_CMD, "%X EXPUNGE\r\n", tag); |
347 |
|
348 |
return send_command(sock, cmd); |
349 |
} |