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

Diff of /imapfilter/imap.c

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

revision 1.8 by lefcha, Fri Aug 24 17:42:49 2001 UTC revision 1.9 by lefcha, Sun Aug 26 01:18:24 2001 UTC
# Line 11  Line 11 
11  #include "log.h"  #include "log.h"
12    
13    
 static int tag = 0xA000;        /* Every IMAP command is prefixed with a  
                                    unique [:alnum:] string. */  
14  extern int sock;  extern int sock;
15  extern int dfilcnt, afilcnt;  extern account_t *accounts;
16  extern filter_entry **dfilters, **afilters;  extern filter_t *dfilters, *afilters;
 extern account_data **accounts;  
 extern int curacc;  
17  extern int options;  extern int options;
18  extern unsigned int dlimit, alimit;  extern unsigned int dlimit, alimit;
19    
20    static int tag = 0xA000;        /* Every IMAP command is prefixed with a
21                                       unique [:alnum:] string. */
22    
23    
24  /*  /*
25   * Sends to server data; a command.   * Sends to server data; a command.
26   */   */
27  int send_command(char *command)  int send_command(char *cmd)
28  {  {
29    
30  #ifdef DEBUG  #ifdef DEBUG
31      printf("debug: sending command: %s", command);      printf("debug: sending command: %s", cmd);
32  #endif  #endif
33    
34      if (write(sock, command, strlen(command)) == -1) {      if (write(sock, cmd, strlen(cmd)) == -1) {
35          error("imapfilter: error while sending command; %s",          error("imapfilter: error while sending command; %s",
36                strerror(errno));                strerror(errno));
37          return FAILURE;          return 1;
38      }      }
39    
40      return SUCCESS;      return 0;
41  }  }
42    
43    
44  /*  /*
45   * Calls send_command() and get_response().   * Calls send_command() and get_response().
46   */   */
47  int send_command_get_response(char *command)  int send_command_get_response(char *cmd)
48  {  {
49      char buf[RESPONSE_BUFFER_MAX];      char buf[RESPONSE_BUF];
50    
51      if (!send_command(command) && !get_response(buf))      if (!send_command(cmd) && !get_response(buf))
52          return SUCCESS;          return 0;
53      else      else
54          return FAILURE;          return 1;
55  }  }
56    
57    
# Line 62  int send_command_get_response(char *comm Line 61  int send_command_get_response(char *comm
61   */   */
62  int imap_noop(void)  int imap_noop(void)
63  {  {
64      char command[SMALL_COMMAND_MAX];      char cmd[SMALL_CMD];
65    
66      verbose("Client request:  NOOP\n");      verbose("Client request:  NOOP\n");
67    
68      snprintf(command, SMALL_COMMAND_MAX, "%X NOOP\r\n", tag++);      snprintf(cmd, SMALL_CMD, "%X NOOP\r\n", tag++);
69    
70      return send_command_get_response(command);      return send_command_get_response(cmd);
71    
72  }  }
73  #endif  #endif
# Line 79  int imap_noop(void) Line 78  int imap_noop(void)
78   */   */
79  int imap_logout(void)  int imap_logout(void)
80  {  {
81      char command[SMALL_COMMAND_MAX];      char cmd[SMALL_CMD];
82    
83      verbose("Client request:  LOGOUT\n");      verbose("Client request:  LOGOUT\n");
84    
85      snprintf(command, SMALL_COMMAND_MAX, "%X LOGOUT\r\n", tag++);      snprintf(cmd, SMALL_CMD, "%X LOGOUT\r\n", tag++);
86    
87      return send_command_get_response(command);      return send_command_get_response(cmd);
88  }  }
89    
90    
91  /*  /*
92   * IMAP LOGIN: identifies client to server.   * IMAP LOGIN: identifies client to server.
93   */   */
94  int imap_login(void)  int imap_login(account_t * ca)
95  {  {
96      char command[MEDIUM_COMMAND_MAX];      char cmd[MEDIUM_CMD];
97    
98      verbose("Client request:  LOGIN\n");      verbose("Client request:  LOGIN\n");
99    
100      snprintf(command, MEDIUM_COMMAND_MAX, "%X LOGIN \"%s\" \"%s\"\r\n",      snprintf(cmd, MEDIUM_CMD, "%X LOGIN \"%s\" \"%s\"\r\n",
101               tag++, accounts[curacc]->username,               tag++, ca->userid, ca->passwd);
              accounts[curacc]->password);  
102    
103      return send_command_get_response(command);      return send_command_get_response(cmd);
104  }  }
105    
106    
# Line 111  int imap_login(void) Line 109  int imap_login(void)
109   */   */
110  int imap_select(void)  int imap_select(void)
111  {  {
112      char command[SMALL_COMMAND_MAX];      char cmd[SMALL_CMD];
113    
114      verbose("Client request:  SELECT\n");      verbose("Client request:  SELECT\n");
115    
116      snprintf(command, SMALL_COMMAND_MAX, "%X SELECT INBOX\r\n", tag++);      snprintf(cmd, SMALL_CMD, "%X SELECT INBOX\r\n", tag++);
117    
118      if (!send_command(command) && !select_response())      if (!send_command(cmd) && !select_response())
119          return SUCCESS;          return 0;
120      else      else
121          return FAILURE;          return 1;
122  }  }
123    
124  /*  /*
125   * IMAP SEARCH: searches the mailbox for messages that match certain criteria.   * IMAP SEARCH: searches the mailbox for messages that match certain criteria.
126   */   */
127  int imap_search(char *results)  int imap_search(char *res)
128  {  {
129      char command[BIG_COMMAND_MAX];      char cmd[BIG_CMD];
130    
131      verbose("Client request:  SEARCH\n");      verbose("Client request:  SEARCH\n");
132    
133      generate_search_filters(command);      generate_search_filters(cmd);
134      if (send_command(command))      if (send_command(cmd))
135          return FAILURE;          return 1;
136      search_response(results);      search_response(res);
137    
138      verbose("Client request:  SEARCH\n");      verbose("Client request:  SEARCH\n");
139    
140      if (alimit && afilcnt) {      if (alimit && afilters) {
141          generate_search_limits(command);          generate_search_limits(cmd);
142          if (send_command(command))          if (send_command(cmd))
143              return FAILURE;              return 1;
144          search_response(results);          search_response(res);
145      }      }
146    
147      return SUCCESS;      return 0;
148  }  }
149    
150    
151  /*  /*
152   * IMAP FETCH: retrieves data associated with a message.   * IMAP FETCH: retrieves data associated with a message.
153   */   */
154  int imap_fetch(unsigned int m, char *results)  int imap_fetch(unsigned int msg, char *res)
155  {  {
156      char command[MEDIUM_COMMAND_MAX];      char cmd[MEDIUM_CMD];
157    
158      verbose("Client request:  FETCH\n");      verbose("Client request:  FETCH\n");
159    
160      snprintf(command, MEDIUM_COMMAND_MAX,      snprintf(cmd, MEDIUM_CMD,
161               "%X FETCH %d BODY[HEADER.FIELDS (\"DATE\" \"FROM\" \"SUBJECT\")]\r\n",               "%X FETCH %d BODY[HEADER.FIELDS (\"DATE\" \"FROM\" \"SUBJECT\")]\r\n",
162               tag++, m);               tag++, msg);
163    
164      if (!send_command(command) && !fetch_response(results))      if (!send_command(cmd) && !fetch_response(res))
165          return SUCCESS;          return 0;
166      else      else
167          return FAILURE;          return 1;
168  }  }
169    
170    
171  /*  /*
172   * IMAP STORE: alters data associated with a message.   * IMAP STORE: alters data associated with a message.
173   */   */
174  int imap_store(unsigned int m)  int imap_store(unsigned int msg)
175  {  {
176      char command[MEDIUM_COMMAND_MAX];      char cmd[MEDIUM_CMD];
177    
178      verbose("Client request:  STORE\n");      verbose("Client request:  STORE\n");
179    
180      snprintf(command, MEDIUM_COMMAND_MAX,      snprintf(cmd, MEDIUM_CMD,
181               "%X STORE %d +FLAGS \\Deleted\r\n", tag++, m);               "%X STORE %d +FLAGS \\Deleted\r\n", tag++, msg);
182    
183      return send_command_get_response(command);      return send_command_get_response(cmd);
184  }  }
185    
186    
# Line 191  int imap_store(unsigned int m) Line 189  int imap_store(unsigned int m)
189   */   */
190  int imap_expunge(void)  int imap_expunge(void)
191  {  {
192      char command[SMALL_COMMAND_MAX];      char cmd[SMALL_CMD];
193    
194      verbose("Client request:  EXPUNGE\n");      verbose("Client request:  EXPUNGE\n");
195    
196      snprintf(command, SMALL_COMMAND_MAX, "%X EXPUNGE\r\n", tag++);      snprintf(cmd, SMALL_CMD, "%X EXPUNGE\r\n", tag++);
197    
198      return send_command_get_response(command);      return send_command_get_response(cmd);
199  }  }
200    
201    
202  /*  /*
203   * Prepare according to the filters defined by the user the IMAP SEARCH   * Prepares according to the filters defined by the user the IMAP SEARCH
204   * command that will be sent.  This command will search for DENY, ALLOW,   * command that will be sent.  This command will search for DENY, ALLOW,
205   * and DENY_LIMIT filters.   * and DENY_LIMIT filters.
206   */   */
207  void generate_search_filters(char *command)  void generate_search_filters(char *cmd)
208  {  {
209      int len;      int len;
210    
211      snprintf(command, BIG_COMMAND_MAX, "%X SEARCH", tag++);      snprintf(cmd, BIG_CMD, "%X SEARCH", tag++);
   
212    
213      deny_filters(command);      deny_filters(cmd);
214    
215      if (dlimit) {      if (dlimit) {
216          len = strlen(command);          len = strlen(cmd);
217          snprintf(command + len, BIG_COMMAND_MAX - len, " LARGER %d",          snprintf(cmd + len, BIG_CMD - len, " LARGER %d", dlimit);
218                   dlimit);      } else if (!dfilters) {     /* If no DENY filters were defined, then
     } else if (!dfilcnt) {      /* If no DENY filters were defined, then  
219                                     deny all except the ALLOW filters. */                                     deny all except the ALLOW filters. */
220          len = strlen(command);          len = strlen(cmd);
221          snprintf(command + len, BIG_COMMAND_MAX - len, " ALL");          snprintf(cmd + len, BIG_CMD - len, " ALL");
222      }      }
223    
224      allow_filters(command, "NOT");      allow_filters(cmd, "NOT");
225    
226      len = strlen(command);      len = strlen(cmd);
227      snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n");      snprintf(cmd + len, BIG_CMD - len, "\r\n");
228  }  }
229    
230    
231  /*  /*
232   * Prepare according to the filters defined by the user the IMAP SEARCH   * Prepares according to the filters defined by the user the IMAP SEARCH
233   * command that will be sent.  This command will search for the ALLOW_LIMIT   * command that will be sent.  This command will search for the ALLOW_LIMIT
234   * filter.   * filter.
235   */   */
236  void generate_search_limits(char *command)  void generate_search_limits(char *cmd)
237  {  {
238      int len;      int len;
239    
240      snprintf(command, BIG_COMMAND_MAX, "%X SEARCH", tag++);      snprintf(cmd, BIG_CMD, "%X SEARCH", tag++);
241    
242      allow_filters(command, "OR");      allow_filters(cmd, "OR");
243    
244      len = strlen(command);      len = strlen(cmd);
245      snprintf(command + len, BIG_COMMAND_MAX - len, " LARGER %d", alimit);      snprintf(cmd + len, BIG_CMD - len, " LARGER %d", alimit);
246    
247      len = strlen(command);      len = strlen(cmd);
248      snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n");      snprintf(cmd + len, BIG_CMD - len, "\r\n");
249    
250  }  }
251    
# Line 257  void generate_search_limits(char *comman Line 253  void generate_search_limits(char *comman
253  /*  /*
254   * Adds to the IMAP SEARCH command all the DENY type filters.   * Adds to the IMAP SEARCH command all the DENY type filters.
255   */   */
256  void deny_filters(char *command)  void deny_filters(char *cmd)
257  {  {
     int f = 0;  
258      int len;      int len;
259        filter_t *cdf;
260    
261      if (dfilcnt > 0) {      if (dfilters) {
262          while (f < (dfilcnt - 1)) {          for (cdf = dfilters; cdf->next; cdf = cdf->next) {
263              len = strlen(command);              len = strlen(cmd);
264              snprintf(command + len, BIG_COMMAND_MAX - len,              snprintf(cmd + len, BIG_CMD - len,
265                       " OR %s%s \"%s\"",                       " OR %s%s \"%s\"",
266                       custom_header(dfilters[f]->custom),                       custom_header(cdf->custom), cdf->name, cdf->body);
                      dfilters[f]->name, dfilters[f]->body);  
             f++;  
267          }          }
268    
269          len = strlen(command);          len = strlen(cmd);
270          snprintf(command + len, BIG_COMMAND_MAX - len, " %s%s%s \"%s\"",          snprintf(cmd + len, BIG_CMD - len, " %s%s%s \"%s\"",
271                   (dlimit ? "OR " : ""),                   (dlimit ? "OR " : ""),
272                   custom_header(dfilters[f]->custom),                   custom_header(cdf->custom), cdf->name, cdf->body);
                  dfilters[f]->name, dfilters[f]->body);  
273      }      }
274  }  }
275    
# Line 284  void deny_filters(char *command) Line 277  void deny_filters(char *command)
277  /*  /*
278   * Adds to IMAP SEARCH command all the ALLOW type filters.   * Adds to IMAP SEARCH command all the ALLOW type filters.
279   */   */
280  void allow_filters(char *command, char *key)  void allow_filters(char *cmd, char *key)
281  {  {
     int f = 0;  
282      int len;      int len;
283        filter_t *caf;
284    
285      if (afilcnt > 0) {      if (afilters) {
286          while (f < (afilcnt - 1)) {          for (caf = afilters; caf->next; caf = caf->next) {
287              len = strlen(command);              len = strlen(cmd);
288              snprintf(command + len, BIG_COMMAND_MAX - len,              snprintf(cmd + len, BIG_CMD - len,
289                       " %s %s%s \"%s\"", key,                       " %s %s%s \"%s\"", key,
290                       custom_header(afilters[f]->custom),                       custom_header(caf->custom), caf->name, caf->body);
                      afilters[f]->name, afilters[f]->body);  
             f++;  
291          }          }
292    
293          len = strlen(command);          len = strlen(cmd);
294          snprintf(command + len, BIG_COMMAND_MAX - len, " %s%s%s \"%s\"",          snprintf(cmd + len, BIG_CMD - len, " %s%s%s \"%s\"",
295                   (!strncmp(key, "NOT", 3) ? "NOT " : ""),                   (!strncmp(key, "NOT", 3) ? "NOT " : ""),
296                   custom_header(afilters[f]->custom),                   custom_header(caf->custom), caf->name, caf->body);
                  afilters[f]->name, afilters[f]->body);  
297      }      }
298  }  }
299    
300    
301  /*  /*
302   * Convert the string with the UID's of the messages to be deleted to   * Converts the string with the UID's of the messages to be deleted to
303   * integers, send the appropriate command and maybe print some headers   * integers, sends the appropriate command and optionally prints some
304   * of the "to be deleted" message.   * headers of the "to be deleted" messages.
305   */   */
306  void delete_messages(char *unwanted)  void delete_messages(char *msgs)
307  {  {
308      unsigned long int messg;      unsigned long int m;
309      char headers[HEADERS_MAX];      char hdr[HEADERS_MAX];
310    
311      do {      do {
312          messg = strtoul(unwanted, &unwanted, 0);          m = strtoul(msgs, &msgs, 0);
313    
314          if (options & OPTION_SHOW_HEADERS) {          if (options & OPT_SHOW_HEADERS) {
315              imap_fetch(messg, headers);              imap_fetch(m, hdr);
316    
317              if (!(options & OPTION_DETAILS_QUITE))              if (!(options & OPT_DETAILS_QUITE))
318                  printf("Deleting message:\n%s", headers);                  printf("Deleting message:\n%s", hdr);
319    
320              if (!(options & OPTION_TEST_MODE))              if (!(options & OPT_TEST_MODE))
321                  log_info("%s", headers);                  log_info("%s", hdr);
322          }          }
323    
324          if (!(options & OPTION_TEST_MODE))          if (!(options & OPT_TEST_MODE))
325              imap_store(messg);              imap_store(m);
326    
327      } while (*unwanted);      } while (*msgs);
328  }  }

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26