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