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

Diff of /imapfilter/imap.c

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

revision 1.12 by lefcha, Tue Aug 28 22:42:06 2001 UTC revision 1.13 by lefcha, Mon Sep 10 23:43:29 2001 UTC
# Line 6  Line 6 
6    
7  #include "config.h"  #include "config.h"
8  #include "imapfilter.h"  #include "imapfilter.h"
9  #include "imap.h"  #include "data.h"
 #include "connect.h"  
 #include "log.h"  
10    
11    
12  extern int sock;  extern int sock;
 extern account_t *accounts;  
 extern filter_t *dfilters, *afilters;  
 extern unsigned int options;  
 extern unsigned int dlimit, alimit;  
   
 static int tag = 0xA000;        /* Every IMAP command is prefixed with a  
                                    unique [:alnum:] string. */  
13    
14    static unsigned int tag = 0xF00000;     /* Every IMAP command is prefixed
15                                               with a unique [:alnum:] string. */
16    
17  /*  /*
18   * Sends to server data; a command.   * Send to server data; a command.
19   */   */
20  int send_command(char *cmd)  int send_command(char *cmd)
21  {  {
# Line 53  int imap_noop(void) Line 46  int imap_noop(void)
46    
47      snprintf(cmd, SMALL_CMD, "%X NOOP\r\n", tag++);      snprintf(cmd, SMALL_CMD, "%X NOOP\r\n", tag++);
48    
49      if (!send_command(cmd) && !clear_stream())      return send_command(cmd);
         return 0;  
     else  
         return 1;  
50  }  }
51  #endif  #endif
52    
# Line 72  int imap_logout(void) Line 62  int imap_logout(void)
62    
63      snprintf(cmd, SMALL_CMD, "%X LOGOUT\r\n", tag++);      snprintf(cmd, SMALL_CMD, "%X LOGOUT\r\n", tag++);
64    
65      if (!send_command(cmd) && !clear_stream())      return send_command(cmd);
         return 0;  
     else  
         return 1;  
66  }  }
67    
68    
69  /*  /*
70   * IMAP LOGIN: identifies client to server.   * IMAP LOGIN: identifies client to server.
71   */   */
72  int imap_login(account_t * ca)  int imap_login(char *user, char *pass)
73  {  {
74      char cmd[MEDIUM_CMD];      char cmd[MEDIUM_CMD];
75    
76      verbose("Client request:  LOGIN\n");      verbose("Client request:  LOGIN\n");
77    
78      snprintf(cmd, MEDIUM_CMD, "%X LOGIN \"%s\" \"%s\"\r\n",      snprintf(cmd, MEDIUM_CMD, "%X LOGIN \"%s\" \"%s\"\r\n", tag++, user,
79               tag++, ca->userid, ca->passwd);               pass);
80    
81      if (!send_command(cmd) && !clear_stream())      return send_command(cmd);
         return 0;  
     else  
         return 1;  
82  }  }
83    
84    
85  /*  /*
86   * IMAP SELECT: selects a mailbox in which messages can be accessed.   * IMAP EXAMINE: access a mailbox in READ-ONLY mode.
87   */   */
88  int imap_select(void)  int imap_examine(char *mbox)
89  {  {
90      char cmd[SMALL_CMD];      char cmd[MEDIUM_CMD];
91    
92      verbose("Client request:  SELECT\n");      verbose("Client request: EXAMINE %s\n", mbox);
93    
94      snprintf(cmd, SMALL_CMD, "%X SELECT INBOX\r\n", tag++);      snprintf(cmd, MEDIUM_CMD, "%X EXAMINE \"%s\"\r\n", tag++, mbox);
95    
96      if (!send_command(cmd) && !clear_stream())      return send_command(cmd);
         return 0;  
     else  
         return 1;  
97  }  }
98    
99    
100    
101  /*  /*
102   * IMAP STATUS: requests status of the indicated mailbox.   * IMAP SELECT: access a mailbox in READ-WRITE mode.
103   */   */
104  int imap_status(void)  int imap_select(char *mbox)
105  {  {
106      char cmd[MEDIUM_CMD];      char cmd[SMALL_CMD];
107    
108      verbose("Client request: STATUS\n");      verbose("Client request:  SELECT\n");
109    
110      snprintf(cmd, MEDIUM_CMD,      snprintf(cmd, SMALL_CMD, "%X SELECT \"%s\"\r\n", tag++, mbox);
              "%X STATUS INBOX (MESSAGES RECENT UNSEEN)\r\n", tag++);  
111    
112      if (!send_command(cmd) && !status_response())      return send_command(cmd);
         return 0;  
     else  
         return 1;  
113  }  }
114    
115    
116  /*  /*
117   * IMAP SEARCH: searches the mailbox for messages that match certain criteria.   * IMAP STATUS: requests status of the indicated mailbox.
118   */   */
119  int imap_search(char *res)  int imap_status(char *mbox, char *items)
120  {  {
121      char cmd[BIG_CMD];      char cmd[MEDIUM_CMD];
   
     verbose("Client request:  SEARCH\n");  
   
     gen_search_filters(cmd);  
     if (send_command(cmd) || search_response(res))  
         return 1;  
122    
123      verbose("Client request:  SEARCH\n");      verbose("Client request: STATUS\n");
124    
125      if (alimit && afilters) {      snprintf(cmd, MEDIUM_CMD, "%X STATUS %s (%s)\r\n", tag++, mbox, items);
         gen_search_limits(cmd);  
         if (send_command(cmd) || search_response(res))  
             return 1;  
     }  
126    
127      return 0;      return send_command(cmd);
128  }  }
129    
130    
131  /*  /*
132   * IMAP FETCH: retrieves data associated with a message.   * IMAP CREATE: create mailbox.
133   */   */
134  int imap_fetch(unsigned int msg, char *res)  int imap_create(char *mbox)
135  {  {
136      char cmd[MEDIUM_CMD];      char cmd[MEDIUM_CMD];
137    
138      verbose("Client request:  FETCH\n");      verbose("Client request: CREATE\n");
139    
140      snprintf(cmd, MEDIUM_CMD,      snprintf(cmd, MEDIUM_CMD, "%X CREATE %s\r\n", tag++, mbox);
              "%X FETCH %d BODY[HEADER.FIELDS (\"DATE\" \"FROM\" \"SUBJECT\")]\r\n",  
              tag++, msg);  
141    
142      if (!send_command(cmd) && !fetch_response(res))      return send_command(cmd);
         return 0;  
     else  
         return 1;  
143  }  }
144    
145    
146    
147  /*  /*
148   * IMAP STORE: alters data associated with a message.   * IMAP SEARCH: searches the mailbox for messages that match certain criteria.
149   */   */
150  int imap_store(unsigned int msg)  int imap_search(char *search)
151  {  {
152      char cmd[MEDIUM_CMD];      char cmd[BIG_CMD];
153    
154      verbose("Client request:  STORE\n");      verbose("Client request: SEARCH\n");
155    
156      snprintf(cmd, MEDIUM_CMD,      snprintf(cmd, BIG_CMD, "%X SEARCH %s\r\n", tag++, search);
              "%X STORE %d +FLAGS \\Deleted\r\n", tag++, msg);  
157    
158      if (!send_command(cmd) && !clear_stream())      return send_command(cmd);
         return 0;  
     else  
         return 1;  
159  }  }
160    
161    
162  /*  /*
163   * IMAP EXPUNGE: premanently removes any messages with the \Deleted flag set.   * IMAP FETCH: retrieves data associated with a message.
164   */   */
165  int imap_expunge(void)  int imap_fetch(char *mesg, char *headers)
166  {  {
167      char cmd[SMALL_CMD];      char cmd[MEDIUM_CMD];
168    
169      verbose("Client request:  EXPUNGE\n");      verbose("Client request:  FETCH\n");
170    
171      snprintf(cmd, SMALL_CMD, "%X EXPUNGE\r\n", tag++);      snprintf(cmd, MEDIUM_CMD,
172                 "%X FETCH %s BODY[HEADER.FIELDS (%s)]\r\n", tag++, mesg,
173                 headers);
174    
175      if (!send_command(cmd) && !expunge_response())      return send_command(cmd);
         return 0;  
     else  
         return 1;  
176  }  }
177    
178    
179  /*  /*
180   * Prepares according to the filters defined by the user the IMAP SEARCH   * IMAP STORE: alters data associated with a message.
  * command that will be sent.  This command will search for DENY, ALLOW,  
  * and DENY_LIMIT filters.  
181   */   */
182  void gen_search_filters(char *cmd)  int imap_store(char *mesg, char *flags)
183  {  {
184      int len;      char cmd[MEDIUM_CMD];
   
     snprintf(cmd, BIG_CMD, "%X SEARCH", tag++);  
   
     gen_deny_filters(cmd);  
185    
186      if (dlimit) {      verbose("Client request:  STORE\n");
         len = strlen(cmd);  
         snprintf(cmd + len, BIG_CMD - len, " LARGER %d", dlimit);  
     } else if (!dfilters) {     /* If no DENY filters were defined, then  
                                    deny all except the ALLOW filters. */  
         len = strlen(cmd);  
         snprintf(cmd + len, BIG_CMD - len, " ALL");  
     }  
187    
188      gen_allow_filters(cmd, "NOT");      snprintf(cmd, MEDIUM_CMD, "%X STORE %s +FLAGS (%s)\r\n", tag++, mesg,
189                 flags);
190    
191      if (options & OPT_UNSEEN_ONLY) {      return send_command(cmd);
         len = strlen(cmd);  
         snprintf(cmd + len, BIG_CMD - len, " UNSEEN");  
     }  
   
     len = strlen(cmd);  
     snprintf(cmd + len, BIG_CMD - len, "\r\n");  
192  }  }
193    
194    
195  /*  /*
196   * Prepares according to the filters defined by the user the IMAP SEARCH   * IMAP COPY: copy messages to mailbox.
  * command that will be sent.  This command will search for the ALLOW_LIMIT  
  * filter.  
197   */   */
198  void gen_search_limits(char *cmd)  int imap_copy(char *mesg, char *mbox)
199  {  {
200      int len;      char cmd[MEDIUM_CMD];
   
     snprintf(cmd, BIG_CMD, "%X SEARCH", tag++);  
   
     gen_allow_filters(cmd, "OR");  
   
     len = strlen(cmd);  
     snprintf(cmd + len, BIG_CMD - len, " LARGER %d", alimit);  
201    
202      if (options & OPT_UNSEEN_ONLY) {      verbose("Client request: COPY\n");
         len = strlen(cmd);  
         snprintf(cmd + len, BIG_CMD - len, " UNSEEN");  
     }  
203    
204      len = strlen(cmd);      snprintf(cmd, SMALL_CMD, "%X COPY %s \"%s\"\r\n", tag++, mesg, mbox);
     snprintf(cmd + len, BIG_CMD - len, "\r\n");  
205    
206        return send_command(cmd);
207  }  }
208    
209    
210  /*  /*
211   * Adds to the IMAP SEARCH command all the DENY type filters.   * IMAP CLOSE: delete messages and return to authenticated state.
212   */   */
213  void gen_deny_filters(char *cmd)  int imap_close(void)
214  {  {
215      int len;      char cmd[SMALL_CMD];
     filter_t *cdf;  
   
     if (dfilters) {  
         for (cdf = dfilters; cdf->next; cdf = cdf->next) {  
             len = strlen(cmd);  
             snprintf(cmd + len, BIG_CMD - len,  
                      " OR %s%s \"%s\"",  
                      custom_header(cdf->custom), cdf->name, cdf->body);  
         }  
   
         len = strlen(cmd);  
         snprintf(cmd + len, BIG_CMD - len, " %s%s%s \"%s\"",  
                  (dlimit ? "OR " : ""),  
                  custom_header(cdf->custom), cdf->name, cdf->body);  
     }  
 }  
216    
217        verbose("Client request: CLOSE\n");
218    
219  /*      snprintf(cmd, SMALL_CMD, "%X CLOSE\r\n", tag++);
  * Adds to IMAP SEARCH command all the ALLOW type filters.  
  */  
 void gen_allow_filters(char *cmd, char *key)  
 {  
     int len;  
     filter_t *caf;  
220    
221      if (afilters) {      return send_command(cmd);
         for (caf = afilters; caf->next; caf = caf->next) {  
             len = strlen(cmd);  
             snprintf(cmd + len, BIG_CMD - len,  
                      " %s %s%s \"%s\"", key,  
                      custom_header(caf->custom), caf->name, caf->body);  
         }  
   
         len = strlen(cmd);  
         snprintf(cmd + len, BIG_CMD - len, " %s%s%s \"%s\"",  
                  (!strncmp(key, "NOT", 3) ? "NOT " : ""),  
                  custom_header(caf->custom), caf->name, caf->body);  
     }  
222  }  }
223    
224    
225    
226  /*  /*
227   * Converts the string with the UID's of the messages to be deleted to   * IMAP EXPUNGE: permanently removes any messages with the \Deleted flag set.
  * integers, sends the appropriate command and optionally prints some  
  * headers of the "to be deleted" messages.  
228   */   */
229  void del_messages(char *msgs)  int imap_expunge(void)
230  {  {
231      unsigned long int m;      char cmd[SMALL_CMD];
     char hdr[HEADERS_MAX];  
   
     do {  
         m = strtoul(msgs, &msgs, 0);  
   
         if (options & OPT_SHOW_HEADERS) {  
             imap_fetch(m, hdr);  
   
             if (!(options & OPT_DETAILS_QUITE))  
                 printf("Deleting message:\n%s", hdr);  
232    
233              if (!(options & OPT_TEST_MODE))      verbose("Client request: EXPUNGE\n");
                 log_info("%s", hdr);  
         }  
234    
235          if (!(options & OPT_TEST_MODE))      snprintf(cmd, SMALL_CMD, "%X EXPUNGE\r\n", tag++);
             imap_store(m);  
236    
237      } while (*msgs);      return send_command(cmd);
238  }  }

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.13

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26