/[imapfilter]/imapfilter/response.c
ViewVC logotype

Contents of /imapfilter/response.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations)
Mon Oct 1 11:48:51 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.4: +1 -1 lines
File MIME type: text/plain
Added some log records.

1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #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 * Read data the server sent.
22 */
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 tv.tv_sec = 20;
32 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
52 fcntl(sock, F_SETFL, flags);
53
54 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.
63 */
64 int server_response(char *cmd)
65 {
66 int r;
67 char buf[RESPONSE_BUF];
68
69 do
70 r = receive_response(buf);
71 while (*cmd && !strstr(buf, cmd));
72
73 return r;
74 }
75
76
77 /*
78 * Process the data that server sent due to IMAP STATUS client request.
79 */
80 int status_response(void)
81 {
82 int r;
83 char buf[RESPONSE_BUF];
84 unsigned int exist, recent, unseen;
85
86 exist = recent = unseen = 0;
87
88 do
89 r = receive_response(buf);
90 while (!strstr(buf, "STATUS"));
91
92 sscanf(buf, "* STATUS %*s (MESSAGES %d RECENT %d UNSEEN %d)", &exist,
93 &recent, &unseen);
94
95 info("%d messages exist, %d recent, %d unseen,", exist, recent,
96 unseen);
97
98 return r;
99 }
100
101
102 /*
103 * Process the data that server sent due to IMAP SEARCH client request.
104 */
105 int search_response(char *mesgs)
106 {
107 int r;
108 char buf[RESPONSE_BUF];
109
110 do
111 r = receive_response(buf);
112 while (!strstr(buf, "SEARCH"));
113
114 sscanf(buf, "* SEARCH %511[0-9 ]", mesgs);
115
116 return r;
117 }
118
119
120 /*
121 * Process the data that server sent due to IMAP FETCH client request.
122 */
123 int fetch_response(void)
124 {
125 int r, s, i;
126 char buf[RESPONSE_BUF];
127 char *pos;
128 char headers[HEADERS_BUF];
129
130 headers[0] = 0;
131
132 do
133 r = receive_response(buf);
134 while (!strstr(buf, "FETCH"));
135
136 pos = buf;
137
138 while((pos = strchr(pos, '{'))) {
139 s = atoi(pos + 1);
140 pos = strchr(pos, '}');
141
142 for (i = 0; i < HEADERS_BUF - 1 && i < s - 2; i++)
143 headers[i] = *(pos + 3 + i);
144
145 headers[i] = 0;
146
147 info("\n%s", headers);
148 log_info(LOG_WRITE, headers);
149 }
150
151 return r;
152 }
153
154
155 /*
156 * Process the data that server sent due to IMAP COPY client request.
157 */
158 int copy_response(void)
159 {
160 int r;
161 char buf[RESPONSE_BUF];
162
163 do
164 r = receive_response(buf);
165 while (!strstr(buf, "COPY"));
166
167 if (r == 1 && strstr(buf, "[TRYCREATE]"))
168 return 1;
169
170 return 0;
171 }
172
173
174 /*
175 * Check if response of server to client's request was succesfully
176 * delivered or there was some kind of error.
177 */
178 int analyze_response(char *buf)
179 {
180 int r = 0;
181 regex_t creg;
182 regmatch_t match[3];
183 const char *reg = "[[:xdigit:]]{6,6} ((OK|NO|BAD) [[:print:]]+)\r\n";
184 char result[RESULT_BUF];
185
186 result[0] = 0;
187
188 regcomp(&creg, reg, REG_EXTENDED);
189
190 if (!regexec(&creg, buf, 3, match, 0)) {
191 strncat(result, buf + match[1].rm_so,
192 min(match[1].rm_eo - match[1].rm_so, RESULT_BUF - 1));
193
194 if (!strncmp(buf + match[2].rm_so, "NO", 2))
195 r = 1;
196 else if (!strncmp(buf + match[2].rm_so, "BAD", 3))
197 r = -1;
198
199 verbose("Server response: %s\n", result);
200 }
201
202 regfree(&creg);
203
204 return r;
205 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26