1 |
#include <stdio.h> |
2 |
#include <errno.h> |
3 |
#include <unistd.h> |
4 |
#include <sys/types.h> |
5 |
#include <sys/time.h> |
6 |
#include <sys/socket.h> |
7 |
#include <fcntl.h> |
8 |
#include <netdb.h> |
9 |
#include <netinet/in.h> |
10 |
#include <string.h> |
11 |
#include <regex.h> |
12 |
|
13 |
#include "config.h" |
14 |
#include "imapfilter.h" |
15 |
#include "connect.h" |
16 |
#include "log.h" |
17 |
#include "imap.h" |
18 |
|
19 |
|
20 |
extern account_t *accounts; |
21 |
extern unsigned int options; |
22 |
|
23 |
int sock; |
24 |
|
25 |
|
26 |
/* |
27 |
* Connects to mailserver. |
28 |
*/ |
29 |
int init_connection(account_t * ca) |
30 |
{ |
31 |
struct sockaddr_in name; |
32 |
struct hostent *hostinfo; |
33 |
|
34 |
memset((char *) &name, 0, sizeof(struct sockaddr_in)); |
35 |
|
36 |
sock = socket(PF_INET, SOCK_STREAM, 0); |
37 |
if (sock < 0) { |
38 |
error("imapfilter: create socket; %s\n", strerror(errno)); |
39 |
return 1; |
40 |
} |
41 |
|
42 |
if (!(hostinfo = gethostbyname(ca->server))) { |
43 |
error("imapfilter: get network host entry; %s\n", strerror(errno)); |
44 |
close_connection(); |
45 |
return 1; |
46 |
} |
47 |
|
48 |
name.sin_family = AF_INET; |
49 |
name.sin_port = htons(ca->port); |
50 |
name.sin_addr = *(struct in_addr *) hostinfo->h_addr; |
51 |
|
52 |
if (connect(sock, (struct sockaddr *) &name, sizeof(struct sockaddr))) { |
53 |
error("imapfilter: initiating connection; %s\n", strerror(errno)); |
54 |
close_connection(); |
55 |
return 1; |
56 |
} |
57 |
|
58 |
info("Connected to %s as %s.\n", hostinfo->h_name, ca->userid); |
59 |
|
60 |
return 0; |
61 |
} |
62 |
|
63 |
|
64 |
/* |
65 |
* Disconnects from mailserver. |
66 |
*/ |
67 |
int close_connection(void) |
68 |
{ |
69 |
if (close(sock)) { |
70 |
error("imapfilter: closing socket; %s\n", strerror(errno)); |
71 |
return 1; |
72 |
} else |
73 |
return 0; |
74 |
} |
75 |
|
76 |
|
77 |
/* |
78 |
* Reads data the server sent. |
79 |
*/ |
80 |
int recv_response(char *buf) |
81 |
{ |
82 |
int flags; |
83 |
fd_set rfds; |
84 |
struct timeval tv; |
85 |
|
86 |
memset(buf, 0, RESPONSE_BUF); |
87 |
|
88 |
tv.tv_sec = 10; |
89 |
tv.tv_usec = 0; |
90 |
|
91 |
flags = fcntl(sock, F_GETFL, 0); |
92 |
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
93 |
|
94 |
FD_ZERO(&rfds); |
95 |
FD_SET(sock, &rfds); |
96 |
|
97 |
if ((select(sock + 1, &rfds, NULL, NULL, &tv) > 0) |
98 |
&& FD_ISSET(sock, &rfds)) { |
99 |
if (read(sock, buf, RESPONSE_BUF - 1) != -1) { |
100 |
#ifdef DEBUG |
101 |
printf("\n%s\n", buf); |
102 |
#endif |
103 |
if (options & OPT_DETAILS_VERBOSE) |
104 |
analyze_response(buf); |
105 |
|
106 |
fcntl(sock, F_SETFL, flags); |
107 |
return 0; |
108 |
} |
109 |
} |
110 |
|
111 |
fcntl(sock, F_SETFL, flags); |
112 |
|
113 |
return 1; |
114 |
} |
115 |
|
116 |
|
117 |
/* |
118 |
* Clears stream from any data server sent. |
119 |
*/ |
120 |
int clear_stream(void) |
121 |
{ |
122 |
char buf[RESPONSE_BUF]; |
123 |
|
124 |
return recv_response(buf); |
125 |
} |
126 |
|
127 |
|
128 |
/* |
129 |
* Processes the data that server sent due to IMAP STATUS client request. |
130 |
*/ |
131 |
int status_response(void) |
132 |
{ |
133 |
int r, s; |
134 |
char buf[RESPONSE_BUF]; |
135 |
char exist[STATUS_BUF], recent[STATUS_BUF], unseen[STATUS_BUF]; |
136 |
regex_t creg; |
137 |
regmatch_t match[4]; |
138 |
const char *reg = |
139 |
"\\* STATUS INBOX \\(MESSAGES ([[:digit:]]+) RECENT ([[:digit:]]+) UNSEEN ([[:digit:]]+)\\)"; |
140 |
|
141 |
exist[0] = recent[0] = unseen[0] = 0; |
142 |
|
143 |
r = recv_response(buf); |
144 |
|
145 |
regcomp(&creg, reg, REG_EXTENDED); |
146 |
|
147 |
if (!regexec(&creg, buf, 4, match, 0)) { |
148 |
s = min(match[1].rm_eo - match[1].rm_so, STATUS_BUF - 1); |
149 |
strncpy(exist, buf + match[1].rm_so, s); |
150 |
exist[s] = 0; |
151 |
|
152 |
s = min(match[2].rm_eo - match[2].rm_so, STATUS_BUF - 1); |
153 |
strncpy(recent, buf + match[2].rm_so, s); |
154 |
recent[s] = 0; |
155 |
|
156 |
s = min(match[3].rm_eo - match[3].rm_so, STATUS_BUF - 1); |
157 |
strncpy(unseen, buf + match[3].rm_so, s); |
158 |
unseen[s] = 0; |
159 |
|
160 |
info("%s messages exist, %s recent, %s unseen.\n", exist, recent, |
161 |
unseen); |
162 |
} |
163 |
|
164 |
return r; |
165 |
} |
166 |
|
167 |
|
168 |
/* |
169 |
* Processes the data that server sent due to IMAP SEARCH client request. |
170 |
*/ |
171 |
int search_response(char *res) |
172 |
{ |
173 |
int r, s, len; |
174 |
char buf[RESPONSE_BUF]; |
175 |
regex_t creg; |
176 |
regmatch_t match[2]; |
177 |
const char *reg = "\\* SEARCH([[:digit:] ]*)"; |
178 |
|
179 |
len = strlen(res); |
180 |
|
181 |
r = recv_response(buf); |
182 |
|
183 |
regcomp(&creg, reg, REG_EXTENDED); |
184 |
|
185 |
if (!regexec(&creg, buf, 2, match, 0)) { |
186 |
s = min(match[1].rm_eo - match[1].rm_so, SEARCH_BUF - 1); |
187 |
strncpy(res + len, buf + match[1].rm_so, s); |
188 |
res[s + len] = 0; |
189 |
} |
190 |
return r; |
191 |
} |
192 |
|
193 |
|
194 |
/* |
195 |
* Processes the data that server sent due to IMAP FETCH client request. |
196 |
*/ |
197 |
int fetch_response(char *res) |
198 |
{ |
199 |
int r, i, s, len = 0; |
200 |
char buf[RESPONSE_BUF]; |
201 |
regex_t creg[3]; |
202 |
regmatch_t match[1]; |
203 |
const char *reg[3] = { |
204 |
"Date: [[:print:]]*", |
205 |
"From: [[:print:]]*", |
206 |
"Subject: [[:print:]]*" |
207 |
}; |
208 |
|
209 |
r = recv_response(buf); |
210 |
|
211 |
for (i = 0; i < 3; i++) { |
212 |
regcomp(&creg[i], reg[i], REG_EXTENDED); |
213 |
|
214 |
if (!regexec(&creg[i], buf, 1, match, 0)) { |
215 |
s = min(match[0].rm_eo - match[0].rm_so, |
216 |
HEADERS_MAX - len - 1); |
217 |
strncpy(res + len, buf + match[0].rm_so, s); |
218 |
res[s + len] = '\n'; |
219 |
res[s + len + 1] = 0; |
220 |
len = strlen(res); |
221 |
} |
222 |
} |
223 |
|
224 |
return r; |
225 |
} |
226 |
|
227 |
|
228 |
/* |
229 |
* Checks if response of server to client's request was succesfully |
230 |
* delivered or there was some kind of error. |
231 |
*/ |
232 |
void analyze_response(char *buf) |
233 |
{ |
234 |
int s; |
235 |
regex_t creg; |
236 |
regmatch_t match[2]; |
237 |
const char *reg = "[[:xdigit:]]{4,4} ((OK|NO|BAD) [[:print:]]*)"; |
238 |
char res[RESULTS_BUF]; |
239 |
|
240 |
regcomp(&creg, reg, REG_EXTENDED); |
241 |
|
242 |
if (!regexec(&creg, buf, 2, match, 0)) { |
243 |
s = min(match[1].rm_eo - match[1].rm_so, RESULTS_BUF - 1); |
244 |
strncpy(res, buf + match[1].rm_so, s); |
245 |
res[s] = 0; |
246 |
printf("Server response: %s\n", res); |
247 |
} |
248 |
} |