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

Annotation of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Sun Aug 12 16:58:21 2001 UTC (22 years, 7 months ago) by lefcha
Branch: MAIN
Changes since 1.3: +35 -27 lines
File MIME type: text/plain
Fix bug with no filters in config file, and introduce inverted DENY filters functionality

1 lefcha 1.1 #include <stdio.h>
2     #include <string.h>
3     #include <stdlib.h>
4     #include <unistd.h>
5     #include <errno.h>
6    
7     #include "config.h"
8     #include "imapfilter.h"
9     #include "imap.h"
10     #include "connect.h"
11     #include "log.h"
12    
13    
14     static int tag = 0xA000; /* Every IMAP command is prefixed with a
15     unique [:alnum:] string. */
16     extern int sock;
17 lefcha 1.2 extern int dfcnt, afcnt;
18     extern filter_entry **dfilters, **afilters;
19 lefcha 1.1 extern account_data account;
20     extern int options;
21    
22    
23     /*
24     * Sends to server data; a command.
25     */
26     int send_command(char *command)
27     {
28    
29     #ifdef DEBUG
30     printf("debug: sending command: %s", command);
31     #endif
32    
33     if (write(sock, command, strlen(command)) == -1) {
34     error("imapfilter: error while sending command; %s",
35     strerror(errno));
36     return FAILURE;
37     }
38    
39     return SUCCESS;
40     }
41    
42    
43     /*
44     * Calls send_command() and get_response().
45     */
46     int send_command_get_response(char *command)
47     {
48     char buf[RESPONSE_BUFFER_MAX];
49    
50     if (!send_command(command)) {
51     get_response(buf);
52     return SUCCESS;
53     } else
54     return FAILURE;
55     }
56    
57    
58     #ifdef DEBUG
59     /*
60     * IMAP NOOP: does nothing always succeeds.
61     */
62     int imap_noop(void)
63     {
64     char command[SMALL_COMMAND_MAX];
65    
66     verbose("Client request: NOOP\n");
67    
68     snprintf(command, SMALL_COMMAND_MAX, "%X NOOP\r\n", tag++);
69    
70     return send_command_get_response(command);
71    
72     }
73     #endif
74    
75    
76     /*
77     * IMAP LOGOUT: informs server that client is done.
78     */
79     int imap_logout(void)
80     {
81     char command[SMALL_COMMAND_MAX];
82    
83     verbose("Client request: LOGOUT\n");
84    
85     snprintf(command, SMALL_COMMAND_MAX, "%X LOGOUT\r\n", tag++);
86    
87     return send_command_get_response(command);
88     }
89    
90    
91     /*
92     * IMAP LOGIN: identifies client to server.
93     */
94     int imap_login(void)
95     {
96     char command[MEDIUM_COMMAND_MAX];
97    
98     verbose("Client request: LOGIN\n");
99    
100     snprintf(command, MEDIUM_COMMAND_MAX, "%X LOGIN \"%s\" \"%s\"\r\n",
101     tag++, account.username, account.password);
102    
103     return send_command_get_response(command);
104     }
105    
106    
107     /*
108     * IMAP SELECT: selects a mailbox in which messages can be accessed.
109     */
110     int imap_select(void)
111     {
112     char command[SMALL_COMMAND_MAX];
113    
114     verbose("Client request: SELECT\n");
115    
116     snprintf(command, SMALL_COMMAND_MAX, "%X SELECT INBOX\r\n", tag++);
117    
118     return send_command_get_response(command);
119     }
120    
121     /*
122     * IMAP SEARCH: searches the mailbox for messages that match certain criteria.
123     */
124     int imap_search(void)
125     {
126     char command[BIG_COMMAND_MAX];
127    
128     verbose("Client request: SEARCH\n");
129    
130     generate_search_filters(command);
131    
132     return send_command(command);
133     }
134    
135    
136     /*
137     * IMAP FETCH: retrieves data associated with a message.
138     */
139     int imap_fetch(unsigned int m, char *results)
140     {
141     char command[MEDIUM_COMMAND_MAX];
142    
143     verbose("Client request: FETCH\n");
144    
145     snprintf(command, MEDIUM_COMMAND_MAX,
146     "%X FETCH %d BODY[HEADER.FIELDS (\"DATE\" \"FROM\" \"SUBJECT\")]\r\n",
147     tag++, m);
148    
149     if (!send_command(command)) {
150     fetch_response(results);
151     return SUCCESS;
152     } else
153     return FAILURE;
154     }
155    
156    
157     /*
158     * IMAP STORE: alters data associated with a message.
159     */
160     int imap_store(unsigned int m)
161     {
162     char command[MEDIUM_COMMAND_MAX];
163    
164     verbose("Client request: STORE\n");
165    
166     snprintf(command, MEDIUM_COMMAND_MAX,
167     "%X STORE %d +FLAGS \\Deleted\r\n", tag++, m);
168    
169     return send_command_get_response(command);
170     }
171    
172    
173     /*
174     * IMAP EXPUNGE: premanently removes any messages with the \Deleted flag set.
175     */
176     int imap_expunge(void)
177     {
178     char command[SMALL_COMMAND_MAX];
179    
180     verbose("Client request: EXPUNGE\n");
181    
182     snprintf(command, SMALL_COMMAND_MAX, "%X EXPUNGE\r\n", tag++);
183    
184     return send_command_get_response(command);
185     }
186    
187    
188     /*
189     * Prepare according to the filters defined by the user the IMAP SEARCH
190     * command that will be sent.
191     */
192     void generate_search_filters(char *command)
193     {
194     int f = 0;
195     int len;
196    
197 lefcha 1.4 snprintf(command, BIG_COMMAND_MAX, "%X SEARCH", tag++);
198 lefcha 1.1
199 lefcha 1.4 /* Go through the DENY type filters... */
200 lefcha 1.2
201 lefcha 1.4 if (dfcnt > 0) {
202 lefcha 1.2
203 lefcha 1.4 while (f < (dfcnt - 1)) {
204     len = strlen(command);
205     snprintf(command + len, BIG_COMMAND_MAX - len,
206     " OR %s%s \"%s\"",
207     (dfilters[f]->custom ? "HEADER " : ""),
208     dfilters[f]->name, dfilters[f]->body);
209     f++;
210     }
211    
212     len = strlen(command);
213     snprintf(command + len, BIG_COMMAND_MAX - len, " %s%s \"%s\"",
214     (dfilters[f]->custom ? "HEADER " : ""),
215     dfilters[f]->name, dfilters[f]->body);
216     } else {
217     len = strlen(command);
218     snprintf(command + len, BIG_COMMAND_MAX - len, " ALL");
219     }
220    
221     /* ... and then continue to the ALLOW entries. */
222    
223     f = 0;
224    
225     while (f < afcnt) {
226     len = strlen(command);
227     snprintf(command + len, BIG_COMMAND_MAX - len,
228     " NOT %s%s \"%s\"",
229     (afilters[f]->custom ? "HEADER " : ""),
230     afilters[f]->name, afilters[f]->body);
231     f++;
232     }
233 lefcha 1.2
234     len = strlen(command);
235 lefcha 1.4 snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n");
236 lefcha 1.1 }
237    
238    
239     /*
240     * Convert the string with the UID's of the messages to be deleted to
241     * integers, send the appropriate command and maybe print some headers
242     * of the "to be deleted" message.
243     */
244     void delete_messages(char *unwanted)
245     {
246     unsigned long int messg;
247     char headers[HEADERS_MAX];
248    
249     do {
250     messg = strtoul(unwanted, &unwanted, 0);
251    
252 lefcha 1.3 if (options & OPTION_SHOW_HEADERS) {
253     imap_fetch(messg, headers);
254 lefcha 1.1
255 lefcha 1.3 if (!(options & OPTION_DETAILS_QUITE))
256     printf("Deleting message:\n%s", headers);
257    
258     if (!(options & OPTION_TEST_MODE))
259     log_info("%s", headers);
260 lefcha 1.1 }
261    
262 lefcha 1.3 if (!(options & OPTION_TEST_MODE))
263 lefcha 1.1 imap_store(messg);
264 lefcha 1.4
265 lefcha 1.1 } while (*unwanted);
266     }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26