1 |
#include <stdio.h> |
#include <stdio.h> |
2 |
#include <unistd.h> |
#include <unistd.h> |
3 |
#include <stdlib.h> |
#include <stdlib.h> |
4 |
|
#include <errno.h> |
5 |
#include <string.h> |
#include <string.h> |
6 |
#include <fcntl.h> |
#include <fcntl.h> |
7 |
#include <sys/time.h> |
#include <sys/time.h> |
50 |
} |
} |
51 |
|
|
52 |
fcntl(sock, F_SETFL, flags); |
fcntl(sock, F_SETFL, flags); |
53 |
|
|
54 |
return 2; |
fatal(ERROR_NETWORK, "imapfilter: waiting input from socket; %s\n", |
55 |
|
strerror(errno)); |
56 |
|
|
57 |
|
return ERROR_NETWORK; |
58 |
} |
} |
59 |
|
|
60 |
|
|
61 |
/* |
/* |
62 |
* Get server response to client's request. |
* Get server response to client's request. |
63 |
*/ |
*/ |
64 |
int server_response(void) |
int server_response(char *cmd) |
65 |
{ |
{ |
66 |
|
int r; |
67 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
68 |
|
|
69 |
return receive_response(buf); |
do |
70 |
|
r = receive_response(buf); |
71 |
|
while (*cmd && !strstr(buf, cmd)); |
72 |
|
|
73 |
|
return r; |
74 |
} |
} |
75 |
|
|
76 |
|
|
120 |
/* |
/* |
121 |
* Process the data that server sent due to IMAP FETCH client request. |
* Process the data that server sent due to IMAP FETCH client request. |
122 |
*/ |
*/ |
123 |
int fetch_response(char *headers) |
int fetch_response(void) |
124 |
{ |
{ |
125 |
int r, s, i; |
int r, s, i; |
126 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
127 |
char *pos; |
char *pos; |
128 |
|
char headers[HEADERS_BUF]; |
129 |
|
|
130 |
|
headers[0] = 0; |
131 |
|
|
132 |
do |
do |
133 |
r = receive_response(buf); |
r = receive_response(buf); |
134 |
while (!strstr(buf, "FETCH")); |
while (!strstr(buf, "FETCH")); |
135 |
|
|
136 |
|
pos = buf; |
137 |
|
|
138 |
pos = strchr(buf, '{'); |
while((pos = strchr(pos, '{'))) { |
139 |
|
s = atoi(pos + 1); |
140 |
s = atoi(pos + 1); |
pos = strchr(pos, '}'); |
141 |
|
|
142 |
pos = strchr(pos, '}'); |
for (i = 0; i < HEADERS_BUF - 1 && i < s - 2; i++) |
143 |
|
headers[i] = *(pos + 3 + i); |
144 |
for (i = 0; i < HEADERS_BUF && i < s - 2; i++) |
|
145 |
headers[i] = *(pos + 3 + i); |
headers[i] = 0; |
146 |
|
|
147 |
headers[i] = 0; |
info("\n%s", headers); |
148 |
|
log_info(0, headers); |
149 |
|
} |
150 |
|
|
151 |
return r; |
return r; |
152 |
} |
} |
153 |
|
|
177 |
*/ |
*/ |
178 |
int analyze_response(char *buf) |
int analyze_response(char *buf) |
179 |
{ |
{ |
180 |
int s, r = 0; |
int r = 0; |
181 |
regex_t creg; |
regex_t creg; |
182 |
regmatch_t match[3]; |
regmatch_t match[3]; |
183 |
const char *reg = "[[:xdigit:]]{6,6} ((OK|NO|BAD) [[:print:]]*)\r\n"; |
const char *reg = "[[:xdigit:]]{6,6} ((OK|NO|BAD) [[:print:]]+)\r\n"; |
184 |
char result[RESULT_BUF]; |
char result[RESULT_BUF]; |
185 |
|
|
186 |
|
result[0] = 0; |
187 |
|
|
188 |
regcomp(&creg, reg, REG_EXTENDED); |
regcomp(&creg, reg, REG_EXTENDED); |
189 |
|
|
190 |
if (!regexec(&creg, buf, 3, match, 0)) { |
if (!regexec(&creg, buf, 3, match, 0)) { |
191 |
s = min(match[1].rm_eo - match[1].rm_so, RESULT_BUF - 1); |
strncat(result, buf + match[1].rm_so, |
192 |
strncpy(result, buf + match[1].rm_so, s); |
min(match[1].rm_eo - match[1].rm_so, RESULT_BUF - 1)); |
|
result[s] = 0; |
|
193 |
|
|
194 |
if (!strncmp(buf + match[2].rm_so, "NO", 2)) |
if (!strncmp(buf + match[2].rm_so, "NO", 2)) |
195 |
r = 1; |
r = 1; |