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

Contents of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sat Aug 11 15:52:52 2001 UTC (22 years, 8 months ago) by lefcha
Branch: MAIN
Branch point for: lefcha
File MIME type: text/plain
Initial revision

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 extern int fcnt;
18 extern filter_entry **filters;
19 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 snprintf(command, BIG_COMMAND_MAX, "%X SEARCH ", tag++);
198
199 while (f < (fcnt - 1)) {
200 len = strlen(command);
201 snprintf(command + len, BIG_COMMAND_MAX - len, "OR %s%s \"%s\" ",
202 (filters[f]->explicit ? "HEADER " : ""), filters[f]->name,
203 filters[f]->body);
204 f++;
205 }
206
207 len = strlen(command);
208 snprintf(command + len, BIG_COMMAND_MAX - len, "%s%s \"%s\"\r\n",
209 (filters[f]->explicit ? "HEADER " : ""), filters[f]->name,
210 filters[f]->body);
211 }
212
213
214 /*
215 * Convert the string with the UID's of the messages to be deleted to
216 * integers, send the appropriate command and maybe print some headers
217 * of the "to be deleted" message.
218 */
219 void delete_messages(char *unwanted)
220 {
221 unsigned long int messg;
222 char headers[HEADERS_MAX];
223
224 do {
225 messg = strtoul(unwanted, &unwanted, 0);
226
227 imap_fetch(messg, headers);
228
229 if (!(options & OPTION_DETAILS_QUITE)) {
230 printf("Deleting message:\n%s\n", headers);
231 }
232
233 if (!(options & OPTION_TEST_MODE)) {
234 log_info("%s\n", headers);
235 imap_store(messg);
236 }
237 } while (*unwanted);
238 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26