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

Diff of /imapfilter/response.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.38 by lefcha, Sun Jul 27 15:54:49 2003 UTC revision 1.39 by lefcha, Sun Jul 27 17:39:45 2003 UTC
# Line 13  Line 13 
13  #include "imapfilter.h"  #include "imapfilter.h"
14    
15    
16  extern int sockpri;  extern conn_t connpri, connaux;
 extern unsigned int capspri, capsaux;  
17  extern jmp_buf acctloop;  extern jmp_buf acctloop;
18    
19  buffer_t ibuf;                  /* Input buffer. */  buffer_t ibuf;                  /* Input buffer. */
# Line 24  buffer_t ibuf;                 /* Input buffer. */ Line 23  buffer_t ibuf;                 /* Input buffer. */
23   * Read one packet of data that the server sent.   * Read one packet of data that the server sent.
24   */   */
25  void  void
26  receive_response(int *sock, char *buf)  receive_response(conn_t * conn, char *buf)
27  {  {
28          if (socket_read(sock, buf) == ERROR_NETWORK)          if (socket_read(conn, buf) == ERROR_NETWORK)
29                  longjmp(acctloop, -1);                  longjmp(acctloop, -1);
30    
31  #ifdef DEBUG  #ifdef DEBUG
32          fprintf(stderr, "debug: getting response (%s):\n\n%s\n",          fprintf(stderr, "debug: getting response (%s):\n\n%s\n",
33              (sock == &sockpri ? "primary" : "auxiliary"), buf);              (conn == &connpri ? "primary" : "auxiliary"), buf);
34  #endif  #endif
35    
36  }  }
# Line 41  receive_response(int *sock, char *buf) Line 40  receive_response(int *sock, char *buf)
40   * Get server response to client's request.   * Get server response to client's request.
41   */   */
42  int  int
43  server_response(int *sock, unsigned int tag)  server_response(conn_t * conn, unsigned int tag)
44  {  {
45          reset_buffer(&ibuf);          reset_buffer(&ibuf);
46    
47          do {          do {
48                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
49                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
50                  bye_response(ibuf.data);                  bye_response(ibuf.data);
51          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));
52    
53          return analyze_response(sock, ibuf.data);          return analyze_response(conn, ibuf.data);
54  }  }
55    
56    
# Line 70  bye_response(char *buf) Line 69  bye_response(char *buf)
69   * Process the greeting that server sends during connection.   * Process the greeting that server sends during connection.
70   */   */
71  int  int
72  greeting_response(int *sock)  greeting_response(conn_t * conn)
73  {  {
74          reset_buffer(&ibuf);          reset_buffer(&ibuf);
75    
76          receive_response(sock, ibuf.data);          receive_response(conn, ibuf.data);
77    
78          verbose("%s: %s", (sock == &sockpri ? "S" : "s"), ibuf);          verbose("%s: %s", (conn == &connpri ? "S" : "s"), ibuf);
79    
80          bye_response(ibuf.data);          bye_response(ibuf.data);
81    
# Line 91  greeting_response(int *sock) Line 90  greeting_response(int *sock)
90   * Process the data that server sent due to IMAP LOGOUT client request.   * Process the data that server sent due to IMAP LOGOUT client request.
91   */   */
92  int  int
93  logout_response(int *sock, unsigned int tag)  logout_response(conn_t * conn, unsigned int tag)
94  {  {
95          reset_buffer(&ibuf);          reset_buffer(&ibuf);
96    
97          do {          do {
98                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
99                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
100          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));
101    
102          return analyze_response(sock, ibuf.data);          return analyze_response(conn, ibuf.data);
103  }  }
104    
105    
# Line 108  logout_response(int *sock, unsigned int Line 107  logout_response(int *sock, unsigned int
107   * Process the data that server sent due to IMAP CAPABILITY client request.   * Process the data that server sent due to IMAP CAPABILITY client request.
108   */   */
109  int  int
110  capability_response(int *sock, unsigned int tag)  capability_response(conn_t * conn, unsigned int tag)
111  {  {
         unsigned int *caps;  
   
         caps = (sock == &sockpri ? &capspri : &capsaux);  
   
112          reset_buffer(&ibuf);          reset_buffer(&ibuf);
113    
114          do {          do {
115                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
116                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
117                  bye_response(ibuf.data);                  bye_response(ibuf.data);
118          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));
119    
# Line 127  capability_response(int *sock, unsigned Line 122  capability_response(int *sock, unsigned
122                  return -2;                  return -2;
123          }          }
124          if (strcasestr(ibuf.data, "NAMESPACE"))          if (strcasestr(ibuf.data, "NAMESPACE"))
125                  *caps |= CAPABILITY_NAMESPACE;                  conn->caps |= CAPABILITY_NAMESPACE;
126  #ifdef CRAM_MD5  #ifdef CRAM_MD5
127          if (strcasestr(ibuf.data, "AUTH=CRAM-MD5"))          if (strcasestr(ibuf.data, "AUTH=CRAM-MD5"))
128                  *caps |= CAPABILITY_AUTH_CRAM_MD5;                  conn->caps |= CAPABILITY_AUTH_CRAM_MD5;
129  #endif  #endif
130  #ifdef SSL_TLS  #ifdef SSL_TLS
131          if (strcasestr(ibuf.data, "STARTTLS"))          if (strcasestr(ibuf.data, "STARTTLS"))
132                  *caps |= CAPABILITY_STARTTLS;                  conn->caps |= CAPABILITY_STARTTLS;
133  #endif  #endif
134    
135    
136          return analyze_response(sock, ibuf.data);          return analyze_response(conn, ibuf.data);
137  }  }
138    
139    
# Line 147  capability_response(int *sock, unsigned Line 142  capability_response(int *sock, unsigned
142   * Process the data that server sent due to IMAP AUTHENTICATE client request.   * Process the data that server sent due to IMAP AUTHENTICATE client request.
143   */   */
144  int  int
145  authenticate_response(int *sock, unsigned int tag, unsigned char **cont)  authenticate_response(conn_t * conn, unsigned int tag, unsigned char **cont)
146  {  {
147          int i;          int i;
148          char *c;          char *c;
# Line 156  authenticate_response(int *sock, unsigne Line 151  authenticate_response(int *sock, unsigne
151    
152          do {          do {
153                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
154                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
155                  bye_response(ibuf.data);                  bye_response(ibuf.data);
156          } while (strlen(ibuf.data) == RESPONSE_BUF &&          } while (strlen(ibuf.data) == RESPONSE_BUF &&
157              !strcasestr(ibuf.data, ultostr(tag, 16)));              !strcasestr(ibuf.data, ultostr(tag, 16)));
# Line 169  authenticate_response(int *sock, unsigne Line 164  authenticate_response(int *sock, unsigne
164    
165                  *c = '\0';                  *c = '\0';
166          }          }
167          return analyze_response(sock, ibuf.data);          return analyze_response(conn, ibuf.data);
168  }  }
169    
170  #endif  #endif
# Line 179  authenticate_response(int *sock, unsigne Line 174  authenticate_response(int *sock, unsigne
174   * Process the data that server sent due to IMAP NAMESPACE client request.   * Process the data that server sent due to IMAP NAMESPACE client request.
175   */   */
176  int  int
177  namespace_response(int *sock, unsigned int tag, namesp_t * nsp)  namespace_response(conn_t * conn, unsigned int tag)
178  {  {
179          char *c, *d;          char *c, *d;
180    
# Line 187  namespace_response(int *sock, unsigned i Line 182  namespace_response(int *sock, unsigned i
182    
183          do {          do {
184                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
185                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
186                  bye_response(ibuf.data);                  bye_response(ibuf.data);
187          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));
188    
# Line 197  namespace_response(int *sock, unsigned i Line 192  namespace_response(int *sock, unsigned i
192                          c = strchr(c, '"') + 1;                          c = strchr(c, '"') + 1;
193                          d = strchr(c, '"') + 1;                          d = strchr(c, '"') + 1;
194    
195                          strncat(nsp->prefix, c, d - c - 1);                          strncat(conn->nsp.prefix, c, d - c - 1);
196                          nsp->delim = *(strchr(d, '"') + 1);                          conn->nsp.delim = *(strchr(d, '"') + 1);
197                  }                  }
198          }          }
199  #ifdef DEBUG  #ifdef DEBUG
200          fprintf(stderr, "debug: namespace (%s): '%s' '%c'\n",          fprintf(stderr, "debug: namespace (%s): '%s' '%c'\n",
201              (sock == &sockpri ? "primary" : "auxiliary"), nsp->prefix,              (conn == &connpri ? "primary" : "auxiliary"), conn->nsp.prefix,
202              nsp->delim);              conn->nsp.delim);
203  #endif  #endif
204          return analyze_response(sock, ibuf.data);          return analyze_response(conn, ibuf.data);
205  }  }
206    
207    
# Line 214  namespace_response(int *sock, unsigned i Line 209  namespace_response(int *sock, unsigned i
209   * Process the data that server sent due to IMAP STATUS client request.   * Process the data that server sent due to IMAP STATUS client request.
210   */   */
211  int  int
212  status_response(int *sock, unsigned int tag, char *mbox)  status_response(conn_t * conn, unsigned int tag, char *mbox)
213  {  {
214          int r;          int r;
215          unsigned int exist, recent, unseen;          unsigned int exist, recent, unseen;
# Line 226  status_response(int *sock, unsigned int Line 221  status_response(int *sock, unsigned int
221    
222          do {          do {
223                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
224                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
225                  bye_response(ibuf.data);                  bye_response(ibuf.data);
226          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));
227    
228          r = analyze_response(sock, ibuf.data);          r = analyze_response(conn, ibuf.data);
229    
230          if (r == RESPONSE_NO)          if (r == RESPONSE_NO)
231                  return -2;                  return -2;
# Line 262  status_response(int *sock, unsigned int Line 257  status_response(int *sock, unsigned int
257   * Process the data that server sent due to IMAP SELECT client request.   * Process the data that server sent due to IMAP SELECT client request.
258   */   */
259  int  int
260  select_response(int *sock, unsigned int tag)  select_response(conn_t * conn, unsigned int tag)
261  {  {
262          reset_buffer(&ibuf);          reset_buffer(&ibuf);
263    
264          do {          do {
265                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
266                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
267                  bye_response(ibuf.data);                  bye_response(ibuf.data);
268          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));
269    
270          if (strcasestr(ibuf.data, "[READ-ONLY]"))          if (strcasestr(ibuf.data, "[READ-ONLY]"))
271                  return RESPONSE_READONLY;                  return RESPONSE_READONLY;
272    
273          return analyze_response(sock, ibuf.data);          return analyze_response(conn, ibuf.data);
274  }  }
275    
276    
# Line 283  select_response(int *sock, unsigned int Line 278  select_response(int *sock, unsigned int
278   * Process the data that server sent due to IMAP SEARCH client request.   * Process the data that server sent due to IMAP SEARCH client request.
279   */   */
280  int  int
281  search_response(int *sock, unsigned int tag, char **mesgs)  search_response(conn_t * conn, unsigned int tag, char **mesgs)
282  {  {
283          char *c, *m;          char *c, *m;
284          unsigned int blen;          unsigned int blen;
# Line 292  search_response(int *sock, unsigned int Line 287  search_response(int *sock, unsigned int
287    
288          do {          do {
289                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
290                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
291                  bye_response(ibuf.data);                  bye_response(ibuf.data);
292          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));
293    
# Line 308  search_response(int *sock, unsigned int Line 303  search_response(int *sock, unsigned int
303    
304                  *m = 0;                  *m = 0;
305          }          }
306          return analyze_response(sock, ibuf.data);          return analyze_response(conn, ibuf.data);
307  }  }
308    
309    
# Line 316  search_response(int *sock, unsigned int Line 311  search_response(int *sock, unsigned int
311   * Process the data that server sent due to IMAP FETCH client request.   * Process the data that server sent due to IMAP FETCH client request.
312   */   */
313  int  int
314  fetch_response(int *sock, unsigned int tag, int reset, char *fetch)  fetch_response(conn_t * conn, unsigned int tag, int reset, char *fetch)
315  {  {
316          unsigned int i;          unsigned int i;
317          static unsigned int s;          static unsigned int s;
# Line 332  fetch_response(int *sock, unsigned int t Line 327  fetch_response(int *sock, unsigned int t
327    
328          do {          do {
329                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
330                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
331                  bye_response(ibuf.data);                  bye_response(ibuf.data);
332          } while (strlen(ibuf.data) < RESPONSE_BUF &&          } while (strlen(ibuf.data) < RESPONSE_BUF &&
333              !strcasestr(ibuf.data, ultostr(tag, 16)));              !strcasestr(ibuf.data, ultostr(tag, 16)));
# Line 356  fetch_response(int *sock, unsigned int t Line 351  fetch_response(int *sock, unsigned int t
351    
352          fetch[i] = '\0';          fetch[i] = '\0';
353    
354          return analyze_response(sock, ibuf.data);          return analyze_response(conn, ibuf.data);
355  }  }
356    
357    
# Line 365  fetch_response(int *sock, unsigned int t Line 360  fetch_response(int *sock, unsigned int t
360   * request.   * request.
361   */   */
362  int  int
363  fetchsize_response(int *sock, unsigned int *size, unsigned int tag)  fetchsize_response(conn_t * conn, unsigned int *size, unsigned int tag)
364  {  {
365          char *c;          char *c;
366    
# Line 375  fetchsize_response(int *sock, unsigned i Line 370  fetchsize_response(int *sock, unsigned i
370    
371          do {          do {
372                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
373                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
374                  bye_response(ibuf.data);                  bye_response(ibuf.data);
375          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));
376    
# Line 383  fetchsize_response(int *sock, unsigned i Line 378  fetchsize_response(int *sock, unsigned i
378                  c += 19;                  c += 19;
379                  *size = strtoul(c, NULL, 10);                  *size = strtoul(c, NULL, 10);
380          }          }
381          return analyze_response(sock, ibuf.data);          return analyze_response(conn, ibuf.data);
382  }  }
383    
384    
# Line 391  fetchsize_response(int *sock, unsigned i Line 386  fetchsize_response(int *sock, unsigned i
386   * Process the data that server sent due to IMAP APPEND client request.   * Process the data that server sent due to IMAP APPEND client request.
387   */   */
388  int  int
389  append_response(int *sock, unsigned int tag)  append_response(conn_t * conn, unsigned int tag)
390  {  {
391          int r;          int r;
392    
# Line 401  append_response(int *sock, unsigned int Line 396  append_response(int *sock, unsigned int
396    
397          do {          do {
398                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
399                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
400                  bye_response(ibuf.data);                  bye_response(ibuf.data);
401          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));
402    
403          if ((r = analyze_response(sock, ibuf.data)) == RESPONSE_NO &&          if ((r = analyze_response(conn, ibuf.data)) == RESPONSE_NO &&
404              strcasestr(ibuf.data, "[TRYCREATE]"))              strcasestr(ibuf.data, "[TRYCREATE]"))
405                  return RESPONSE_TRYCREATE;                  return RESPONSE_TRYCREATE;
406    
# Line 417  append_response(int *sock, unsigned int Line 412  append_response(int *sock, unsigned int
412   * Process the data that server sent due to IMAP COPY client request.   * Process the data that server sent due to IMAP COPY client request.
413   */   */
414  int  int
415  copy_response(int *sock, unsigned int tag)  copy_response(conn_t * conn, unsigned int tag)
416  {  {
417          int r;          int r;
418    
# Line 427  copy_response(int *sock, unsigned int ta Line 422  copy_response(int *sock, unsigned int ta
422    
423          do {          do {
424                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);                  check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF);
425                  receive_response(sock, ibuf.data + strlen(ibuf.data));                  receive_response(conn, ibuf.data + strlen(ibuf.data));
426                  bye_response(ibuf.data);                  bye_response(ibuf.data);
427          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));          } while (!strcasestr(ibuf.data, ultostr(tag, 16)));
428    
429          if ((r = analyze_response(sock, ibuf.data)) == RESPONSE_NO &&          if ((r = analyze_response(conn, ibuf.data)) == RESPONSE_NO &&
430              strcasestr(ibuf.data, "[TRYCREATE]"))              strcasestr(ibuf.data, "[TRYCREATE]"))
431                  return RESPONSE_TRYCREATE;                  return RESPONSE_TRYCREATE;
432    
# Line 444  copy_response(int *sock, unsigned int ta Line 439  copy_response(int *sock, unsigned int ta
439   * delivered or there was some kind of error.   * delivered or there was some kind of error.
440   */   */
441  int  int
442  analyze_response(int *sock, char *buf)  analyze_response(conn_t * conn, char *buf)
443  {  {
444          int r;          int r;
445          regex_t creg;          regex_t creg;
# Line 467  analyze_response(int *sock, char *buf) Line 462  analyze_response(int *sock, char *buf)
462                  else if (!strncasecmp(buf + match[2].rm_so, "BAD", 3))                  else if (!strncasecmp(buf + match[2].rm_so, "BAD", 3))
463                          r = RESPONSE_BAD;                          r = RESPONSE_BAD;
464    
465                  verbose("%s: %s", (sock == &sockpri ? "S" : "s"),                  verbose("%s: %s", (conn == &connpri ? "S" : "s"),
466                      buf + match[0].rm_so);                      buf + match[0].rm_so);
467          } else          } else
468                  r = RESPONSE_NONE;                  r = RESPONSE_NONE;

Legend:
Removed from v.1.38  
changed lines
  Added in v.1.39

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26