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