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