1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
|
|
#include <unistd.h> |
3 |
|
|
#include <stdlib.h> |
4 |
lefcha |
1.4 |
#include <errno.h> |
5 |
lefcha |
1.1 |
#include <string.h> |
6 |
|
|
#include <fcntl.h> |
7 |
|
|
#include <sys/time.h> |
8 |
|
|
#include <sys/types.h> |
9 |
|
|
#include <regex.h> |
10 |
|
|
|
11 |
|
|
|
12 |
|
|
#include "config.h" |
13 |
|
|
#include "imapfilter.h" |
14 |
|
|
|
15 |
|
|
|
16 |
|
|
extern int sock; |
17 |
|
|
extern unsigned int options; |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
/* |
21 |
lefcha |
1.3 |
* Read data the server sent. |
22 |
lefcha |
1.1 |
*/ |
23 |
|
|
int receive_response(char *buf) |
24 |
|
|
{ |
25 |
|
|
int flags; |
26 |
|
|
fd_set fds; |
27 |
|
|
struct timeval tv; |
28 |
|
|
|
29 |
|
|
memset(buf, 0, RESPONSE_BUF); |
30 |
|
|
|
31 |
lefcha |
1.2 |
tv.tv_sec = 20; |
32 |
lefcha |
1.1 |
tv.tv_usec = 0; |
33 |
|
|
|
34 |
|
|
flags = fcntl(sock, F_GETFL, 0); |
35 |
|
|
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
36 |
|
|
|
37 |
|
|
FD_ZERO(&fds); |
38 |
|
|
FD_SET(sock, &fds); |
39 |
|
|
|
40 |
|
|
if ((select(sock + 1, &fds, NULL, NULL, &tv) > 0) |
41 |
|
|
&& FD_ISSET(sock, &fds)) { |
42 |
|
|
if (read(sock, buf, RESPONSE_BUF - 1) != -1) { |
43 |
|
|
#ifdef DEBUG |
44 |
|
|
printf("\n%s\n", buf); |
45 |
|
|
#endif |
46 |
|
|
fcntl(sock, F_SETFL, flags); |
47 |
|
|
|
48 |
|
|
return analyze_response(buf); |
49 |
|
|
} |
50 |
|
|
} |
51 |
lefcha |
1.6 |
fcntl(sock, F_SETFL, flags); |
52 |
lefcha |
1.1 |
|
53 |
lefcha |
1.4 |
fatal(ERROR_NETWORK, "imapfilter: waiting input from socket; %s\n", |
54 |
|
|
strerror(errno)); |
55 |
lefcha |
1.6 |
|
56 |
lefcha |
1.4 |
return ERROR_NETWORK; |
57 |
lefcha |
1.1 |
} |
58 |
|
|
|
59 |
|
|
|
60 |
|
|
/* |
61 |
|
|
* Get server response to client's request. |
62 |
|
|
*/ |
63 |
lefcha |
1.4 |
int server_response(char *cmd) |
64 |
lefcha |
1.1 |
{ |
65 |
lefcha |
1.4 |
int r; |
66 |
lefcha |
1.1 |
char buf[RESPONSE_BUF]; |
67 |
lefcha |
1.6 |
|
68 |
lefcha |
1.4 |
do |
69 |
|
|
r = receive_response(buf); |
70 |
|
|
while (*cmd && !strstr(buf, cmd)); |
71 |
lefcha |
1.6 |
|
72 |
lefcha |
1.4 |
return r; |
73 |
lefcha |
1.1 |
} |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
/* |
77 |
lefcha |
1.3 |
* Process the data that server sent due to IMAP STATUS client request. |
78 |
lefcha |
1.1 |
*/ |
79 |
|
|
int status_response(void) |
80 |
|
|
{ |
81 |
|
|
int r; |
82 |
|
|
char buf[RESPONSE_BUF]; |
83 |
|
|
unsigned int exist, recent, unseen; |
84 |
|
|
|
85 |
|
|
exist = recent = unseen = 0; |
86 |
|
|
|
87 |
|
|
do |
88 |
|
|
r = receive_response(buf); |
89 |
|
|
while (!strstr(buf, "STATUS")); |
90 |
|
|
|
91 |
|
|
sscanf(buf, "* STATUS %*s (MESSAGES %d RECENT %d UNSEEN %d)", &exist, |
92 |
|
|
&recent, &unseen); |
93 |
|
|
|
94 |
lefcha |
1.8 |
if (!exist) { |
95 |
|
|
info("No messages "); |
96 |
|
|
return -2; |
97 |
|
|
} |
98 |
|
|
info("%d message%s, %d recent, %d unseen, ", exist, plural(exist), recent, |
99 |
lefcha |
1.1 |
unseen); |
100 |
|
|
|
101 |
|
|
return r; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
|
105 |
|
|
/* |
106 |
lefcha |
1.3 |
* Process the data that server sent due to IMAP SEARCH client request. |
107 |
lefcha |
1.1 |
*/ |
108 |
|
|
int search_response(char *mesgs) |
109 |
|
|
{ |
110 |
|
|
int r; |
111 |
|
|
char buf[RESPONSE_BUF]; |
112 |
|
|
|
113 |
|
|
do |
114 |
|
|
r = receive_response(buf); |
115 |
|
|
while (!strstr(buf, "SEARCH")); |
116 |
|
|
|
117 |
|
|
sscanf(buf, "* SEARCH %511[0-9 ]", mesgs); |
118 |
|
|
|
119 |
|
|
return r; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
|
123 |
|
|
/* |
124 |
lefcha |
1.3 |
* Process the data that server sent due to IMAP FETCH client request. |
125 |
lefcha |
1.1 |
*/ |
126 |
lefcha |
1.4 |
int fetch_response(void) |
127 |
lefcha |
1.1 |
{ |
128 |
|
|
int r, s, i; |
129 |
|
|
char buf[RESPONSE_BUF]; |
130 |
|
|
char *pos; |
131 |
lefcha |
1.4 |
char headers[HEADERS_BUF]; |
132 |
lefcha |
1.6 |
|
133 |
lefcha |
1.4 |
headers[0] = 0; |
134 |
lefcha |
1.6 |
|
135 |
lefcha |
1.1 |
do |
136 |
|
|
r = receive_response(buf); |
137 |
|
|
while (!strstr(buf, "FETCH")); |
138 |
lefcha |
1.6 |
|
139 |
lefcha |
1.4 |
pos = buf; |
140 |
lefcha |
1.1 |
|
141 |
lefcha |
1.6 |
while ((pos = strchr(pos, '{'))) { |
142 |
lefcha |
1.4 |
s = atoi(pos + 1); |
143 |
|
|
pos = strchr(pos, '}'); |
144 |
lefcha |
1.8 |
|
145 |
lefcha |
1.4 |
for (i = 0; i < HEADERS_BUF - 1 && i < s - 2; i++) |
146 |
|
|
headers[i] = *(pos + 3 + i); |
147 |
lefcha |
1.6 |
|
148 |
lefcha |
1.4 |
headers[i] = 0; |
149 |
lefcha |
1.6 |
|
150 |
lefcha |
1.7 |
if (*headers) { |
151 |
|
|
if (options & OPTION_HEADERS) |
152 |
|
|
info("%s\n", headers); |
153 |
lefcha |
1.6 |
log_info(LOG_WRITE, headers); |
154 |
lefcha |
1.7 |
} else |
155 |
lefcha |
1.6 |
log_info(LOG_WRITE, NULL); |
156 |
lefcha |
1.4 |
} |
157 |
lefcha |
1.6 |
|
158 |
lefcha |
1.1 |
return r; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
|
162 |
|
|
/* |
163 |
lefcha |
1.3 |
* Process the data that server sent due to IMAP COPY client request. |
164 |
lefcha |
1.1 |
*/ |
165 |
|
|
int copy_response(void) |
166 |
|
|
{ |
167 |
|
|
int r; |
168 |
|
|
char buf[RESPONSE_BUF]; |
169 |
|
|
|
170 |
|
|
do |
171 |
|
|
r = receive_response(buf); |
172 |
|
|
while (!strstr(buf, "COPY")); |
173 |
|
|
|
174 |
|
|
if (r == 1 && strstr(buf, "[TRYCREATE]")) |
175 |
|
|
return 1; |
176 |
|
|
|
177 |
|
|
return 0; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
|
181 |
|
|
/* |
182 |
lefcha |
1.3 |
* Check if response of server to client's request was succesfully |
183 |
lefcha |
1.1 |
* delivered or there was some kind of error. |
184 |
|
|
*/ |
185 |
|
|
int analyze_response(char *buf) |
186 |
|
|
{ |
187 |
lefcha |
1.4 |
int r = 0; |
188 |
lefcha |
1.1 |
regex_t creg; |
189 |
|
|
regmatch_t match[3]; |
190 |
lefcha |
1.4 |
const char *reg = "[[:xdigit:]]{6,6} ((OK|NO|BAD) [[:print:]]+)\r\n"; |
191 |
lefcha |
1.1 |
char result[RESULT_BUF]; |
192 |
|
|
|
193 |
lefcha |
1.4 |
result[0] = 0; |
194 |
|
|
|
195 |
lefcha |
1.1 |
regcomp(&creg, reg, REG_EXTENDED); |
196 |
lefcha |
1.6 |
|
197 |
lefcha |
1.1 |
if (!regexec(&creg, buf, 3, match, 0)) { |
198 |
lefcha |
1.4 |
strncat(result, buf + match[1].rm_so, |
199 |
|
|
min(match[1].rm_eo - match[1].rm_so, RESULT_BUF - 1)); |
200 |
lefcha |
1.1 |
|
201 |
|
|
if (!strncmp(buf + match[2].rm_so, "NO", 2)) |
202 |
|
|
r = 1; |
203 |
|
|
else if (!strncmp(buf + match[2].rm_so, "BAD", 3)) |
204 |
|
|
r = -1; |
205 |
lefcha |
1.6 |
|
206 |
lefcha |
1.1 |
verbose("Server response: %s\n", result); |
207 |
|
|
} |
208 |
lefcha |
1.6 |
regfree(&creg); |
209 |
lefcha |
1.1 |
|
210 |
|
|
return r; |
211 |
|
|
} |