1 |
#include <stdio.h> |
#include <stdio.h> |
2 |
#include <unistd.h> |
#include <unistd.h> |
3 |
#include <stdlib.h> |
#include <stdlib.h> |
4 |
|
#include <ctype.h> |
5 |
#include <errno.h> |
#include <errno.h> |
6 |
#include <string.h> |
#include <string.h> |
7 |
#include <fcntl.h> |
#include <fcntl.h> |
61 |
/* |
/* |
62 |
* Get server response to client's request. |
* Get server response to client's request. |
63 |
*/ |
*/ |
64 |
int server_response(char *cmd) |
int server_response(unsigned int tag) |
65 |
{ |
{ |
66 |
int r; |
int r; |
67 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
68 |
|
|
69 |
do |
do |
70 |
r = receive_response(buf); |
r = receive_response(buf); |
71 |
while (*cmd && !strstr(buf, cmd)); |
while (tag && !strcasestr(buf, ultostr(tag, 16))); |
72 |
|
|
73 |
return r; |
return r; |
74 |
} |
} |
75 |
|
|
76 |
|
|
77 |
/* |
/* |
78 |
|
* Process the greeting that server sends during connection. |
79 |
|
*/ |
80 |
|
int greeting_response(void) |
81 |
|
{ |
82 |
|
char buf[RESPONSE_BUF]; |
83 |
|
|
84 |
|
receive_response(buf); |
85 |
|
|
86 |
|
if (strcasestr(buf, "BYE")) |
87 |
|
return RESPONSE_BYE; |
88 |
|
|
89 |
|
return RESPONSE_OK; |
90 |
|
} |
91 |
|
|
92 |
|
/* |
93 |
|
* Process the data that server sent due to IMAP CAPABILITY client request. |
94 |
|
*/ |
95 |
|
int capability_response(unsigned int tag) |
96 |
|
{ |
97 |
|
int r; |
98 |
|
char buf[RESPONSE_BUF]; |
99 |
|
|
100 |
|
do |
101 |
|
r = receive_response(buf); |
102 |
|
while (!strcasestr(buf, ultostr(tag, 16))); |
103 |
|
|
104 |
|
if (!strcasestr(buf, "IMAP4rev1")) { |
105 |
|
error("imapfilter: server does not support IMAP4rev1 protocol\n"); |
106 |
|
return -2; |
107 |
|
} |
108 |
|
return r; |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
/* |
113 |
* Process the data that server sent due to IMAP STATUS client request. |
* Process the data that server sent due to IMAP STATUS client request. |
114 |
*/ |
*/ |
115 |
int status_response(void) |
int status_response(unsigned int tag) |
116 |
{ |
{ |
117 |
int r; |
int r; |
118 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
119 |
unsigned int exist, recent, unseen; |
unsigned int exist, recent, unseen; |
120 |
|
char *c; |
121 |
|
|
122 |
exist = recent = unseen = 0; |
exist = recent = unseen = 0; |
123 |
|
|
124 |
do |
do |
125 |
r = receive_response(buf); |
r = receive_response(buf); |
126 |
while (!strstr(buf, "STATUS")); |
while (!strcasestr(buf, ultostr(tag, 16))); |
|
|
|
|
sscanf(buf, "* STATUS %*s (MESSAGES %d RECENT %d UNSEEN %d)", &exist, |
|
|
&recent, &unseen); |
|
127 |
|
|
128 |
|
if ((c = strcasestr(buf, "MESSAGES"))) { |
129 |
|
c += 9; |
130 |
|
exist = strtoul(c, NULL, 10); |
131 |
|
} |
132 |
|
if ((c = strcasestr(buf, "RECENT"))) { |
133 |
|
c += 7; |
134 |
|
recent = strtoul(c, NULL, 10); |
135 |
|
} |
136 |
|
if ((c = strcasestr(buf, "UNSEEN"))) { |
137 |
|
c += 7; |
138 |
|
unseen = strtoul(c, NULL, 10); |
139 |
|
} |
140 |
if (!exist) { |
if (!exist) { |
141 |
info("No messages "); |
info("No messages "); |
142 |
return -2; |
return -2; |
143 |
} |
} |
144 |
info("%d message%s, %d recent, %d unseen, ", exist, plural(exist), recent, |
info("%d message%s, %d recent, %d unseen, ", exist, plural(exist), |
145 |
unseen); |
recent, unseen); |
146 |
|
|
147 |
|
return r; |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
/* |
152 |
|
* Process the data that server sent due to IMAP SELECT client request. |
153 |
|
*/ |
154 |
|
int select_response(unsigned int tag) |
155 |
|
{ |
156 |
|
int r; |
157 |
|
char buf[RESPONSE_BUF]; |
158 |
|
|
159 |
|
do |
160 |
|
r = receive_response(buf); |
161 |
|
while (!strcasestr(buf, ultostr(tag, 16))); |
162 |
|
|
163 |
|
if (strcasestr(buf, "[READ-ONLY]")) |
164 |
|
return RESPONSE_READONLY; |
165 |
|
|
166 |
return r; |
return r; |
167 |
} |
} |
170 |
/* |
/* |
171 |
* Process the data that server sent due to IMAP SEARCH client request. |
* Process the data that server sent due to IMAP SEARCH client request. |
172 |
*/ |
*/ |
173 |
int search_response(char *mesgs) |
int search_response(unsigned int tag, char *mesgs) |
174 |
{ |
{ |
175 |
int r; |
int r; |
176 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
177 |
|
char *c, *m = mesgs; |
178 |
|
|
179 |
do |
do |
180 |
r = receive_response(buf); |
r = receive_response(buf); |
181 |
while (!strstr(buf, "SEARCH")); |
while (!strcasestr(buf, ultostr(tag, 16))); |
182 |
|
|
183 |
sscanf(buf, "* SEARCH %511[0-9 ]", mesgs); |
if ((c = strcasestr(buf, "SEARCH "))) { |
184 |
|
c += 7; |
185 |
|
while (mesgs - m < 512 && (isdigit(*c) || *c == ' ')) |
186 |
|
*(m++) = *(c++); |
187 |
|
*m = 0; |
188 |
|
} |
189 |
|
|
190 |
return r; |
return r; |
191 |
} |
} |
194 |
/* |
/* |
195 |
* Process the data that server sent due to IMAP FETCH client request. |
* Process the data that server sent due to IMAP FETCH client request. |
196 |
*/ |
*/ |
197 |
int fetch_response(void) |
int fetch_response(unsigned int tag) |
198 |
{ |
{ |
199 |
int r, s, i; |
int r, s, i; |
200 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
205 |
|
|
206 |
do |
do |
207 |
r = receive_response(buf); |
r = receive_response(buf); |
208 |
while (!strstr(buf, "FETCH")); |
while (!strcasestr(buf, ultostr(tag, 16))); |
209 |
|
|
210 |
pos = buf; |
pos = buf; |
211 |
|
|
233 |
/* |
/* |
234 |
* Process the data that server sent due to IMAP COPY client request. |
* Process the data that server sent due to IMAP COPY client request. |
235 |
*/ |
*/ |
236 |
int copy_response(void) |
int copy_response(unsigned int tag) |
237 |
{ |
{ |
238 |
int r; |
int r; |
239 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
240 |
|
|
241 |
do |
do |
242 |
r = receive_response(buf); |
r = receive_response(buf); |
243 |
while (!strstr(buf, "COPY")); |
while (!strcasestr(buf, ultostr(tag, 16))); |
244 |
|
|
245 |
if (r == 1 && strstr(buf, "[TRYCREATE]")) |
if (r == RESPONSE_NO && strcasestr(buf, "[TRYCREATE]")) |
246 |
return 1; |
return RESPONSE_TRYCREATE; |
247 |
|
|
248 |
return 0; |
return RESPONSE_OK; |
249 |
} |
} |
250 |
|
|
251 |
|
|
255 |
*/ |
*/ |
256 |
int analyze_response(char *buf) |
int analyze_response(char *buf) |
257 |
{ |
{ |
258 |
int r = 0; |
int r = RESPONSE_OK; |
259 |
regex_t creg; |
regex_t creg; |
260 |
regmatch_t match[3]; |
regmatch_t match[3]; |
261 |
const char *reg = "[[:xdigit:]]{6,6} ((OK|NO|BAD) [[:print:]]+)\r\n"; |
const char *reg = "[[:xdigit:]]{6,6} ((OK|NO|BAD)[[:print:]]*)\r\n"; |
262 |
char result[RESULT_BUF]; |
char result[RESULT_BUF]; |
263 |
|
|
264 |
result[0] = 0; |
result[0] = 0; |
265 |
|
|
266 |
regcomp(&creg, reg, REG_EXTENDED); |
regcomp(&creg, reg, REG_EXTENDED | REG_ICASE); |
267 |
|
|
268 |
if (!regexec(&creg, buf, 3, match, 0)) { |
if (!regexec(&creg, buf, 3, match, 0)) { |
269 |
strncat(result, buf + match[1].rm_so, |
strncat(result, buf + match[1].rm_so, |
270 |
min(match[1].rm_eo - match[1].rm_so, RESULT_BUF - 1)); |
min(match[1].rm_eo - match[1].rm_so, RESULT_BUF - 1)); |
271 |
|
|
272 |
if (!strncmp(buf + match[2].rm_so, "NO", 2)) |
if (!strncasecmp(buf + match[2].rm_so, "NO", 2)) |
273 |
r = 1; |
r = RESPONSE_NO; |
274 |
else if (!strncmp(buf + match[2].rm_so, "BAD", 3)) |
else if (!strncasecmp(buf + match[2].rm_so, "BAD", 3)) |
275 |
r = -1; |
r = RESPONSE_BAD; |
276 |
|
|
277 |
verbose("Server response: %s\n", result); |
verbose("Server response: %s\n", result); |
278 |
} |
} |