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