pop3: report error responses
[pop3.git] / pop3.c
blobe4f85944680adc34088b6520dada2a92ce1ae72a
1 /*
2 * A NEAT POP3 MAIL CLIENT
4 * Copyright (C) 2010-2017 Ali Gholami Rudi
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <ctype.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <netdb.h>
22 #include <signal.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include "conf.h"
30 #include "uidl.h"
31 #include "conn.h"
33 #define BUFFSIZE (1 << 12)
34 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
35 #define MIN(a, b) ((a) < (b) ? (a) : (b))
37 static struct mailinfo {
38 char name[1 << 4];
39 char id[1 << 6];
40 int size;
41 } mails[MAXMAILS];
42 static int nmails;
43 static struct uidl *uidl;
45 static char buf[BUFFSIZE];
46 static int buf_len;
47 static int buf_pos;
48 static struct conn *conn;
49 static char *mailbuf;
51 static int pop3_read(void)
53 if (buf_pos == buf_len) {
54 buf_len = conn_read(conn, buf, sizeof(buf));
55 buf_pos = 0;
57 return buf_pos < buf_len ? (unsigned char) buf[buf_pos++] : -1;
60 /* read a line from the server */
61 static int pop3_get(char *dst, int len)
63 int i = 0;
64 int c;
65 while (i < len - 1) {
66 c = pop3_read();
67 if (c < 0)
68 return -1;
69 dst[i++] = c;
70 if (c == '\n')
71 break;
73 dst[i] = '\0';
74 LOG(dst);
75 return i;
78 /* read a pop3 response line */
79 static int pop3_res(char *dst, int len)
81 pop3_get(dst, len);
82 if (dst[0] != '+')
83 printf("%s", dst);
84 return dst[0] != '+';
87 /* send a pop3 command */
88 static void pop3_cmd(char *cmd, ...)
90 static char buf[512];
91 va_list ap;
92 va_start(ap, cmd);
93 vsnprintf(buf, sizeof(buf), cmd, ap);
94 va_end(ap);
95 conn_write(conn, buf, strlen(buf));
96 LOG(buf);
99 static int pop3_iseoc(char *line, int len)
101 return len >= 2 && len <= 3 && line[0] == '.' &&
102 (line[1] == '\r' || line[1] == '\n');
105 static int pop3_login(char *user, char *pass)
107 char line[BUFFSIZE];
108 pop3_cmd("USER %s\r\n", user);
109 if (pop3_res(line, sizeof(line)))
110 return 1;
111 pop3_cmd("PASS %s\r\n", pass);
112 if (pop3_res(line, sizeof(line)))
113 return 1;
114 return 0;
117 static int pop3_stat(void)
119 char line[BUFFSIZE];
120 int len;
121 pop3_cmd("STAT\r\n");
122 if (pop3_res(line, sizeof(line)))
123 return 1;
124 printf("%s", line);
125 pop3_cmd("LIST\r\n");
126 if (pop3_res(line, sizeof(line)))
127 return 1;
128 while ((len = pop3_get(line, sizeof(line))) >= 0) {
129 struct mailinfo *mail;
130 if (pop3_iseoc(line, len))
131 break;
132 mail = &mails[nmails++];
133 sscanf(line, "%s %d", mail->name, &mail->size);
135 return 0;
138 static int pop3_uidl(void)
140 char line[BUFFSIZE];
141 char name[128];
142 int len;
143 int i = 0;
144 pop3_cmd("UIDL\r\n");
145 if (pop3_res(line, sizeof(line)))
146 return 1;
147 while ((len = pop3_get(line, sizeof(line))) > 0 && !pop3_iseoc(line, len))
148 sscanf(line, "%s %s", name, mails[i++].id);
149 return 0;
152 static void pop3_retr(int i)
154 pop3_cmd("RETR %s\r\n", mails[i].name);
157 static char *mail_dst(char *hdr, int len)
159 int i;
160 hdr[len] = '\0';
161 for (i = 0; i < ARRAY_SIZE(filters); i++)
162 if (!strncmp(filters[i].hdr, hdr, strlen(filters[i].hdr)) &&
163 strstr(hdr, filters[i].val))
164 return filters[i].dst;
165 return NULL;
168 static int xwrite(int fd, char *buf, int len)
170 int nw = 0;
171 while (nw < len) {
172 int ret = write(fd, buf + nw, len - nw);
173 if (ret == -1 && (errno == EAGAIN || errno == EINTR))
174 continue;
175 if (ret < 0)
176 break;
177 nw += ret;
179 return nw;
182 static int mail_write(char *dst, char *mail, int len)
184 int fd = open(dst, O_WRONLY | O_APPEND | O_CREAT, 0600);
185 if (fd < 0)
186 return 1;
187 if (xwrite(fd, mail, len) != len)
188 return 1;
189 close(fd);
190 return 0;
193 static int mail_from_(char *s)
195 char date[128];
196 time_t t;
197 time(&t);
198 strftime(date, sizeof(date), "%a %b %d %H:%M:%S %Y", localtime(&t));
199 return sprintf(s, "From %s %s\n", getenv("USER") ? getenv("USER") : "root", date);
202 static int pop3_lonefrom_(char *s)
204 while (*s == '>')
205 s++;
206 return !strncmp("From ", s, 5);
209 static int fetch_one(int i)
211 char line[BUFFSIZE];
212 char *s = mailbuf;
213 char *dst = NULL;
214 int hdr = 1;
215 int len, ret;
216 if (pop3_res(line, sizeof(line)))
217 return 1;
218 printf("%s", mails[i].name);
219 s += mail_from_(s);
220 while (1) {
221 len = pop3_get(line, sizeof(line));
222 if (len <= 0) /* end of stream or error */
223 return 1;
224 if (pop3_iseoc(line, len))
225 break;
226 if (len > 1 && line[len - 2] == '\r')
227 line[len-- - 2] = '\n';
228 if (line[0] == '\n')
229 hdr = 0;
230 if (hdr && !dst)
231 dst = mail_dst(line, len);
232 if (pop3_lonefrom_(line))
233 *s++ = '>';
234 memcpy(s, line, len);
235 s += len;
237 *s++ = '\n';
238 if (!dst)
239 dst = SPOOL;
240 ret = mail_write(dst, mailbuf, s - mailbuf);
241 printf(" -> %s%s\n", dst, ret ? " [failed]" : "");
242 return ret;
245 static void pop3_del(int i)
247 pop3_cmd("DELE %s\r\n", mails[i].name);
250 static int size_ok(int i)
252 return mails[i].size + 100 < MAXSIZE;
255 static int uidl_new(int i)
257 return !uidl || !uidl_find(uidl, mails[i].id);
260 static int fetch_mails(int beg, int end, int del)
262 char line[BUFFSIZE];
263 int i;
264 for (i = beg; i < end; i++)
265 if (size_ok(i) && uidl_new(i))
266 pop3_retr(i);
267 for (i = beg; i < end; i++) {
268 if (size_ok(i) && uidl_new(i)) {
269 if (fetch_one(i))
270 return 1;
271 if (uidl)
272 uidl_add(uidl, mails[i].id);
275 if (del) {
276 for (i = beg; i < end; i++)
277 if ((!uidl && size_ok(i)) || (uidl && !uidl_new(i)))
278 pop3_del(i);
279 for (i = beg; i < end; i++)
280 if ((!uidl && size_ok(i)) || (uidl && !uidl_new(i)))
281 pop3_get(line, sizeof(line));
283 return 0;
286 static int fetch(struct account *account)
288 char line[BUFFSIZE];
289 int batch;
290 int failed = 0;
291 int i;
292 nmails = 0;
293 conn = conn_connect(account->server, account->port);
294 if (!conn)
295 return 1;
296 if (account->stls) {
297 if (pop3_res(line, sizeof(line)))
298 return 1;
299 pop3_cmd("STLS\r\n");
300 if (pop3_res(line, sizeof(line)))
301 return 1;
303 if (conn_tls(conn, account->cert)) {
304 conn_close(conn);
305 return 1;
307 buf_pos = 0;
308 buf_len = 0;
309 if (account->uidl)
310 uidl = uidl_read(account->uidl);
311 printf("fetching %s@%s\n", account->user, account->server);
312 if (!account->stls)
313 if (pop3_res(line, sizeof(line)))
314 return 1;
315 if (pop3_login(account->user, account->pass))
316 return 1;
317 if (pop3_stat())
318 return 1;
319 if (account->uidl)
320 if (pop3_uidl())
321 return 1;
322 batch = account->nopipe ? 1 : nmails;
323 for (i = 0; i < nmails; i += batch)
324 if ((failed = fetch_mails(i, MIN(nmails, i + batch), account->del)))
325 break;
326 if (!failed) {
327 pop3_cmd("QUIT\r\n");
328 pop3_get(line, sizeof(line));
330 conn_close(conn);
331 if (uidl)
332 uidl_save(uidl);
333 uidl = NULL;
334 return failed;
337 static void sigint(int sig)
339 if (uidl)
340 uidl_save(uidl);
341 exit(1);
344 int main(int argc, char *argv[])
346 int i;
347 signal(SIGINT, sigint);
348 mailbuf = malloc(MAXSIZE);
349 for (i = 0; i < ARRAY_SIZE(accounts); i++)
350 fetch(&accounts[i]);
351 free(mailbuf);
352 return 0;