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 sock; |
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(char *cmd) |
21 |
{ |
22 |
|
23 |
#ifdef DEBUG |
24 |
printf("debug: sending command: %s", cmd); |
25 |
#endif |
26 |
|
27 |
if (write(sock, cmd, strlen(cmd)) == -1) |
28 |
fatal(ERROR_NETWORK, "imapfilter: error while sending command; %s", |
29 |
strerror(errno)); |
30 |
|
31 |
return tag++; |
32 |
} |
33 |
|
34 |
|
35 |
#ifdef DEBUG |
36 |
/* |
37 |
* IMAP NOOP: does nothing always succeeds. |
38 |
*/ |
39 |
int imap_noop(void) |
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(cmd); |
48 |
} |
49 |
#endif |
50 |
|
51 |
|
52 |
/* |
53 |
* IMAP CAPABILITY: requests listing of capabilities that the server supports |
54 |
*/ |
55 |
int imap_capability(void) |
56 |
{ |
57 |
char cmd[SMALL_CMD]; |
58 |
|
59 |
verbose("Client request: CAPABILITY\n"); |
60 |
|
61 |
snprintf(cmd, SMALL_CMD, "%X CAPABILITY\r\n", tag); |
62 |
|
63 |
return send_command(cmd); |
64 |
} |
65 |
|
66 |
|
67 |
/* |
68 |
* IMAP LOGOUT: informs server that client is done. |
69 |
*/ |
70 |
int imap_logout(void) |
71 |
{ |
72 |
char cmd[SMALL_CMD]; |
73 |
|
74 |
verbose("Client request: LOGOUT\n"); |
75 |
|
76 |
snprintf(cmd, SMALL_CMD, "%X LOGOUT\r\n", tag); |
77 |
|
78 |
return send_command(cmd); |
79 |
} |
80 |
|
81 |
|
82 |
/* |
83 |
* IMAP LOGIN: identifies client to server. |
84 |
*/ |
85 |
int imap_login(char *user, char *pass) |
86 |
{ |
87 |
char cmd[MEDIUM_CMD]; |
88 |
|
89 |
verbose("Client request: LOGIN\n"); |
90 |
|
91 |
snprintf(cmd, MEDIUM_CMD, "%X LOGIN \"%s\" \"%s\"\r\n", tag, user, |
92 |
pass); |
93 |
|
94 |
return send_command(cmd); |
95 |
} |
96 |
|
97 |
|
98 |
/* |
99 |
* IMAP EXAMINE: access a mailbox in READ-ONLY mode. |
100 |
* |
101 |
int imap_examine(char *mbox) |
102 |
{ |
103 |
char cmd[MEDIUM_CMD]; |
104 |
|
105 |
verbose("Client request: EXAMINE %s\n", mbox); |
106 |
|
107 |
snprintf(cmd, MEDIUM_CMD, "%X EXAMINE \"%s\"\r\n", tag, mbox); |
108 |
|
109 |
return send_command(cmd); |
110 |
}*/ |
111 |
|
112 |
|
113 |
/* |
114 |
* IMAP SELECT: access a mailbox in READ-WRITE mode. |
115 |
*/ |
116 |
int imap_select(char *mbox) |
117 |
{ |
118 |
char cmd[SMALL_CMD]; |
119 |
|
120 |
verbose("Client request: SELECT\n"); |
121 |
|
122 |
snprintf(cmd, SMALL_CMD, "%X SELECT \"%s\"\r\n", tag, mbox); |
123 |
|
124 |
return send_command(cmd); |
125 |
} |
126 |
|
127 |
|
128 |
/* |
129 |
* IMAP STATUS: requests status of the indicated mailbox. |
130 |
*/ |
131 |
int imap_status(char *mbox, char *items) |
132 |
{ |
133 |
char cmd[MEDIUM_CMD]; |
134 |
|
135 |
verbose("Client request: STATUS\n"); |
136 |
|
137 |
snprintf(cmd, MEDIUM_CMD, "%X STATUS %s (%s)\r\n", tag, mbox, items); |
138 |
|
139 |
return send_command(cmd); |
140 |
} |
141 |
|
142 |
|
143 |
/* |
144 |
* IMAP CREATE: create mailbox. |
145 |
*/ |
146 |
int imap_create(char *mbox) |
147 |
{ |
148 |
char cmd[MEDIUM_CMD]; |
149 |
|
150 |
verbose("Client request: CREATE\n"); |
151 |
|
152 |
snprintf(cmd, MEDIUM_CMD, "%X CREATE %s\r\n", tag, mbox); |
153 |
|
154 |
return send_command(cmd); |
155 |
} |
156 |
|
157 |
|
158 |
|
159 |
/* |
160 |
* IMAP SEARCH: searches the mailbox for messages that match certain criteria. |
161 |
*/ |
162 |
int imap_search(char *search) |
163 |
{ |
164 |
char cmd[BIG_CMD]; |
165 |
|
166 |
verbose("Client request: SEARCH\n"); |
167 |
|
168 |
snprintf(cmd, BIG_CMD, "%X SEARCH %s\r\n", tag, search); |
169 |
|
170 |
return send_command(cmd); |
171 |
} |
172 |
|
173 |
|
174 |
/* |
175 |
* IMAP FETCH: retrieves data associated with a message. |
176 |
*/ |
177 |
int imap_fetch(char *mesg, char *headers, int peek) |
178 |
{ |
179 |
char cmd[MEDIUM_CMD]; |
180 |
|
181 |
verbose("Client request: FETCH\n"); |
182 |
|
183 |
snprintf(cmd, MEDIUM_CMD, |
184 |
"%X FETCH %s BODY%s[HEADER.FIELDS (%s)]\r\n", tag, mesg, |
185 |
(peek ? ".PEEK" : ""), headers); |
186 |
|
187 |
return send_command(cmd); |
188 |
} |
189 |
|
190 |
|
191 |
/* |
192 |
* IMAP STORE: alters data associated with a message. |
193 |
*/ |
194 |
int imap_store(char *mesg, char *flags) |
195 |
{ |
196 |
char cmd[MEDIUM_CMD]; |
197 |
|
198 |
verbose("Client request: STORE\n"); |
199 |
|
200 |
snprintf(cmd, MEDIUM_CMD, "%X STORE %s +FLAGS.SILENT (%s)\r\n", tag, mesg, |
201 |
flags); |
202 |
|
203 |
return send_command(cmd); |
204 |
} |
205 |
|
206 |
|
207 |
/* |
208 |
* IMAP COPY: copy messages to mailbox. |
209 |
*/ |
210 |
int imap_copy(char *mesg, char *mbox) |
211 |
{ |
212 |
char cmd[MEDIUM_CMD]; |
213 |
|
214 |
verbose("Client request: COPY\n"); |
215 |
|
216 |
snprintf(cmd, SMALL_CMD, "%X COPY %s \"%s\"\r\n", tag, mesg, mbox); |
217 |
|
218 |
return send_command(cmd); |
219 |
} |
220 |
|
221 |
|
222 |
/* |
223 |
* IMAP CLOSE: delete messages and return to authenticated state. |
224 |
*/ |
225 |
int imap_close(void) |
226 |
{ |
227 |
char cmd[SMALL_CMD]; |
228 |
|
229 |
verbose("Client request: CLOSE\n"); |
230 |
|
231 |
snprintf(cmd, SMALL_CMD, "%X CLOSE\r\n", tag); |
232 |
|
233 |
return send_command(cmd); |
234 |
} |
235 |
|
236 |
|
237 |
|
238 |
/* |
239 |
* IMAP EXPUNGE: permanently removes any messages with the \Deleted flag set. |
240 |
*/ |
241 |
int imap_expunge(void) |
242 |
{ |
243 |
char cmd[SMALL_CMD]; |
244 |
|
245 |
verbose("Client request: EXPUNGE\n"); |
246 |
|
247 |
snprintf(cmd, SMALL_CMD, "%X EXPUNGE\r\n", tag); |
248 |
|
249 |
return send_command(cmd); |
250 |
} |