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