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

Diff of /imapfilter/response.c

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

revision 1.8 by lefcha, Thu Oct 4 15:48:34 2001 UTC revision 1.9 by lefcha, Mon Oct 8 08:52:49 2001 UTC
# Line 1  Line 1 
1  #include <stdio.h>  #include <stdio.h>
2  #include <unistd.h>  #include <unistd.h>
3  #include <stdlib.h>  #include <stdlib.h>
4    #include <ctype.h>
5  #include <errno.h>  #include <errno.h>
6  #include <string.h>  #include <string.h>
7  #include <fcntl.h>  #include <fcntl.h>
# Line 60  int receive_response(char *buf) Line 61  int receive_response(char *buf)
61  /*  /*
62   * Get server response to client's request.   * Get server response to client's request.
63   */   */
64  int server_response(char *cmd)  int server_response(unsigned int tag)
65  {  {
66      int r;      int r;
67      char buf[RESPONSE_BUF];      char buf[RESPONSE_BUF];
68    
69      do      do
70          r = receive_response(buf);          r = receive_response(buf);
71      while (*cmd && !strstr(buf, cmd));      while (tag && !strcasestr(buf, ultostr(tag, 16)));
72    
73      return r;      return r;
74  }  }
75    
76    
77  /*  /*
78     * Process the greeting that server sends during connection.
79     */
80    int greeting_response(void)
81    {
82        char buf[RESPONSE_BUF];
83    
84        receive_response(buf);
85    
86        if (strcasestr(buf, "BYE"))
87            return RESPONSE_BYE;
88    
89        return RESPONSE_OK;
90    }
91    
92    /*
93     * Process the data that server sent due to IMAP CAPABILITY client request.
94     */
95    int capability_response(unsigned int tag)
96    {
97        int r;
98        char buf[RESPONSE_BUF];
99    
100        do
101            r = receive_response(buf);
102        while (!strcasestr(buf, ultostr(tag, 16)));
103    
104        if (!strcasestr(buf, "IMAP4rev1")) {
105            error("imapfilter: server does not support IMAP4rev1 protocol\n");
106            return -2;
107        }
108        return r;
109    }
110    
111    
112    /*
113   * Process the data that server sent due to IMAP STATUS client request.   * Process the data that server sent due to IMAP STATUS client request.
114   */   */
115  int status_response(void)  int status_response(unsigned int tag)
116  {  {
117      int r;      int r;
118      char buf[RESPONSE_BUF];      char buf[RESPONSE_BUF];
119      unsigned int exist, recent, unseen;      unsigned int exist, recent, unseen;
120        char *c;
121    
122      exist = recent = unseen = 0;      exist = recent = unseen = 0;
123    
124      do      do
125          r = receive_response(buf);          r = receive_response(buf);
126      while (!strstr(buf, "STATUS"));      while (!strcasestr(buf, ultostr(tag, 16)));
   
     sscanf(buf, "* STATUS %*s (MESSAGES %d RECENT %d UNSEEN %d)", &exist,  
            &recent, &unseen);  
127    
128        if ((c = strcasestr(buf, "MESSAGES"))) {
129            c += 9;
130            exist = strtoul(c, NULL, 10);
131        }
132        if ((c = strcasestr(buf, "RECENT"))) {
133            c += 7;
134            recent = strtoul(c, NULL, 10);
135        }
136        if ((c = strcasestr(buf, "UNSEEN"))) {
137            c += 7;
138            unseen = strtoul(c, NULL, 10);
139        }
140      if (!exist) {      if (!exist) {
141          info("No messages ");          info("No messages ");
142          return -2;          return -2;
143      }      }
144      info("%d message%s, %d recent, %d unseen, ", exist, plural(exist), recent,      info("%d message%s, %d recent, %d unseen, ", exist, plural(exist),
145           unseen);           recent, unseen);
146    
147        return r;
148    }
149    
150    
151    /*
152     * Process the data that server sent due to IMAP SELECT client request.
153     */
154    int select_response(unsigned int tag)
155    {
156        int r;
157        char buf[RESPONSE_BUF];
158    
159        do
160            r = receive_response(buf);
161        while (!strcasestr(buf, ultostr(tag, 16)));
162    
163        if (strcasestr(buf, "[READ-ONLY]"))
164            return RESPONSE_READONLY;
165    
166      return r;      return r;
167  }  }
# Line 105  int status_response(void) Line 170  int status_response(void)
170  /*  /*
171   * Process the data that server sent due to IMAP SEARCH client request.   * Process the data that server sent due to IMAP SEARCH client request.
172   */   */
173  int search_response(char *mesgs)  int search_response(unsigned int tag, char *mesgs)
174  {  {
175      int r;      int r;
176      char buf[RESPONSE_BUF];      char buf[RESPONSE_BUF];
177        char *c, *m = mesgs;
178    
179      do      do
180          r = receive_response(buf);          r = receive_response(buf);
181      while (!strstr(buf, "SEARCH"));      while (!strcasestr(buf, ultostr(tag, 16)));
182    
183      sscanf(buf, "* SEARCH %511[0-9 ]", mesgs);      if ((c = strcasestr(buf, "SEARCH "))) {
184            c += 7;
185            while (mesgs - m < 512 && (isdigit(*c) || *c == ' '))
186                *(m++) = *(c++);
187            *m = 0;
188        }
189    
190      return r;      return r;
191  }  }
# Line 123  int search_response(char *mesgs) Line 194  int search_response(char *mesgs)
194  /*  /*
195   * Process the data that server sent due to IMAP FETCH client request.   * Process the data that server sent due to IMAP FETCH client request.
196   */   */
197  int fetch_response(void)  int fetch_response(unsigned int tag)
198  {  {
199      int r, s, i;      int r, s, i;
200      char buf[RESPONSE_BUF];      char buf[RESPONSE_BUF];
# Line 134  int fetch_response(void) Line 205  int fetch_response(void)
205    
206      do      do
207          r = receive_response(buf);          r = receive_response(buf);
208      while (!strstr(buf, "FETCH"));      while (!strcasestr(buf, ultostr(tag, 16)));
209    
210      pos = buf;      pos = buf;
211    
# Line 162  int fetch_response(void) Line 233  int fetch_response(void)
233  /*  /*
234   * Process the data that server sent due to IMAP COPY client request.   * Process the data that server sent due to IMAP COPY client request.
235   */   */
236  int copy_response(void)  int copy_response(unsigned int tag)
237  {  {
238      int r;      int r;
239      char buf[RESPONSE_BUF];      char buf[RESPONSE_BUF];
240    
241      do      do
242          r = receive_response(buf);          r = receive_response(buf);
243      while (!strstr(buf, "COPY"));      while (!strcasestr(buf, ultostr(tag, 16)));
244    
245      if (r == 1 && strstr(buf, "[TRYCREATE]"))      if (r == RESPONSE_NO && strcasestr(buf, "[TRYCREATE]"))
246          return 1;          return RESPONSE_TRYCREATE;
247    
248      return 0;      return RESPONSE_OK;
249  }  }
250    
251    
# Line 184  int copy_response(void) Line 255  int copy_response(void)
255   */   */
256  int analyze_response(char *buf)  int analyze_response(char *buf)
257  {  {
258      int r = 0;      int r = RESPONSE_OK;
259      regex_t creg;      regex_t creg;
260      regmatch_t match[3];      regmatch_t match[3];
261      const char *reg = "[[:xdigit:]]{6,6} ((OK|NO|BAD) [[:print:]]+)\r\n";      const char *reg = "[[:xdigit:]]{6,6} ((OK|NO|BAD)[[:print:]]*)\r\n";
262      char result[RESULT_BUF];      char result[RESULT_BUF];
263    
264      result[0] = 0;      result[0] = 0;
265    
266      regcomp(&creg, reg, REG_EXTENDED);      regcomp(&creg, reg, REG_EXTENDED | REG_ICASE);
267    
268      if (!regexec(&creg, buf, 3, match, 0)) {      if (!regexec(&creg, buf, 3, match, 0)) {
269          strncat(result, buf + match[1].rm_so,          strncat(result, buf + match[1].rm_so,
270                  min(match[1].rm_eo - match[1].rm_so, RESULT_BUF - 1));                  min(match[1].rm_eo - match[1].rm_so, RESULT_BUF - 1));
271    
272          if (!strncmp(buf + match[2].rm_so, "NO", 2))          if (!strncasecmp(buf + match[2].rm_so, "NO", 2))
273              r = 1;              r = RESPONSE_NO;
274          else if (!strncmp(buf + match[2].rm_so, "BAD", 3))          else if (!strncasecmp(buf + match[2].rm_so, "BAD", 3))
275              r = -1;              r = RESPONSE_BAD;
276    
277          verbose("Server response: %s\n", result);          verbose("Server response: %s\n", result);
278      }      }

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.9

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26