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