pop3: include a carriage return after commands
[pop3.git] / pop3.c
blob7a521282b28e03b0265029dac9ec405aa7a7ae17
1 /*
2 * pop3 - a simple pop3 mail client
4 * Copyright (C) 2010-2012 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
7 */
8 #include <ctype.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <netdb.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17 #include <unistd.h>
18 #include "config.h"
19 #include "uidl.h"
20 #include "conn.h"
22 #define BUFFSIZE (1 << 12)
23 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
24 #define MIN(a, b) ((a) < (b) ? (a) : (b))
26 static struct mailinfo {
27 char name[1 << 4];
28 char id[1 << 5];
29 int size;
30 } mails[MAXMAILS];
31 static int nmails;
32 static struct uidl *uidl;
34 static char buf[BUFFSIZE];
35 static char *buf_cur;
36 static char *buf_end;
37 static struct conn *conn;
39 static void print(char *buf, int len)
41 write(STDOUT_FILENO, buf, len);
44 static int reply_line(char *dst, int len)
46 int nr = 0;
47 char *nl;
48 while (nr < len) {
49 int ml;
50 if (!buf_cur || buf_cur >= buf_end) {
51 int buf_len = conn_read(conn, buf, sizeof(buf));
52 if (buf_len <= 0)
53 return -1;
54 DPRINT(buf, buf_len);
55 buf_cur = buf;
56 buf_end = buf + buf_len;
58 ml = MIN(buf_end - buf_cur, len - nr);
59 if ((nl = memchr(buf_cur, '\n', ml))) {
60 nl++;
61 memcpy(dst + nr, buf_cur, nl - buf_cur);
62 nr += nl - buf_cur;
63 buf_cur = nl;
64 return nr;
66 memcpy(dst + nr, buf_cur, ml);
67 nr += ml;
68 buf_cur += ml;
70 return nr;
73 static void send_cmd(char *cmd)
75 conn_write(conn, cmd, strlen(cmd));
76 DPRINT(cmd, strlen(cmd));
79 static int is_eoc(char *line, int len)
81 return len < 4 && line[0] == '.' &&
82 (line[1] == '\r' || line[1] == '\n');
85 static char *putmem(char *dst, char *src, int len)
87 memcpy(dst, src, len);
88 return dst + len;
91 static char *cutword(char *dst, char *s)
93 while (*s && isspace(*s))
94 s++;
95 while (*s && !isspace(*s))
96 *dst++ = *s++;
97 *dst = '\0';
98 return s;
101 static char *putstr(char *dst, char *src)
103 int len = strchr(src, '\0') - src;
104 memcpy(dst, src, len + 1);
105 return dst + len;
108 static void login(char *user, char *pass)
110 char line[BUFFSIZE];
111 int len;
112 sprintf(line, "USER %s\r\n", user);
113 send_cmd(line);
114 len = reply_line(line, sizeof(line));
115 sprintf(line, "PASS %s\r\n", pass);
116 send_cmd(line);
117 len = reply_line(line, sizeof(line));
120 static void mail_stat(void)
122 char line[BUFFSIZE];
123 int len;
124 send_cmd("STAT\r\n");
125 len = reply_line(line, sizeof(line));
126 print(line, len);
127 send_cmd("LIST\r\n");
128 len = reply_line(line, sizeof(line));
129 while ((len = reply_line(line, sizeof(line))) != -1) {
130 struct mailinfo *mail;
131 char *s = line;
132 if (is_eoc(line, len))
133 break;
134 mail = &mails[nmails++];
135 s = cutword(mail->name, s);
136 mail->size = atoi(s);
140 static void mail_uidl(void)
142 char line[BUFFSIZE];
143 int len;
144 int i = 0;
145 send_cmd("UIDL\r\n");
146 len = reply_line(line, sizeof(line));
147 while ((len = reply_line(line, sizeof(line))) != -1 &&
148 !is_eoc(line, len)) {
149 char name[100];
150 char *s = line;
151 s = cutword(name, s);
152 s = cutword(mails[i++].id, s);
156 static void req_mail(int i)
158 char cmd[100];
159 sprintf(cmd, "RETR %s\r\n", mails[i].name);
160 send_cmd(cmd);
163 static char *mail_dst(char *line, int len)
165 int i;
166 line[len] = '\0';
167 for (i = 0; i < ARRAY_SIZE(filters); i++) {
168 char *hdr = filters[i].hdr;
169 if (!strncmp(hdr, line, strlen(hdr)) &&
170 strstr(line, filters[i].val))
171 return filters[i].dst;
173 return NULL;
176 static int xwrite(int fd, char *buf, int len)
178 int nw = 0;
179 while (nw < len) {
180 int ret = write(fd, buf + nw, len - nw);
181 if (ret == -1 && (errno == EAGAIN || errno == EINTR))
182 continue;
183 if (ret < 0)
184 break;
185 nw += ret;
187 return nw;
190 static int mail_write(char *dst, char *mail, int len)
192 int fd = open(dst, O_WRONLY | O_APPEND | O_CREAT, 0600);
193 if (fd == -1)
194 return -1;
195 if (xwrite(fd, mail, len) != len)
196 return -1;
197 close(fd);
198 return 0;
201 static char *put_from_(char *s)
203 time_t t;
204 time(&t);
205 s = putstr(s, "From ");
206 s = putstr(s, getenv("USER") ? getenv("USER") : "root");
207 s += strftime(s, MAXSIZE, " %a %b %d %H:%M:%S %Y\n", localtime(&t));
208 return s;
211 static int lone_from(char *s)
213 while (*s == '>')
214 s++;
215 return !strncmp("From ", s, 5);
218 static int ret_mail(int i)
220 char mail[MAXSIZE];
221 char line[BUFFSIZE];
222 char *s = mail;
223 int len = reply_line(line, sizeof(line));
224 char *dst = NULL;
225 int hdr = 1;
226 int ret;
227 print(mails[i].name, strlen(mails[i].name));
228 s = put_from_(s);
229 while ((len = reply_line(line, sizeof(line))) != -1) {
230 if (is_eoc(line, len))
231 break;
232 if (len > 1 && line[len - 2] == '\r')
233 line[len-- - 2] = '\n';
234 if (line[0] == '\n')
235 hdr = 0;
236 if (hdr && !dst)
237 dst = mail_dst(line, len);
238 if (lone_from(line))
239 *s++ = '>';
240 s = putmem(s, line, len);
242 *s++ = '\n';
243 if (!dst)
244 dst = SPOOL;
245 ret = mail_write(dst, mail, s - mail);
246 sprintf(line, " -> %s\n", dst);
247 print(line, strlen(line));
248 return ret;
251 static void del_mail(int i)
253 char cmd[100];
254 sprintf(cmd, "DELE %s\r\n", mails[i].name);
255 send_cmd(cmd);
258 static int size_ok(int i)
260 return mails[i].size + 100 < MAXSIZE;
263 static int uidl_new(int i)
265 return !uidl || !uidl_find(uidl, mails[i].id);
268 static int ret_mails(int beg, int end, int del)
270 char line[BUFFSIZE];
271 int i;
272 for (i = beg; i < end; i++)
273 if (size_ok(i) && uidl_new(i))
274 req_mail(i);
275 for (i = beg; i < end; i++) {
276 if (size_ok(i) && uidl_new(i)) {
277 if (ret_mail(i) == -1)
278 return -1;
279 if (uidl)
280 uidl_add(uidl, mails[i].id);
283 if (del) {
284 for (i = beg; i < end; i++)
285 if ((!uidl && size_ok(i)) || (uidl && !uidl_new(i)))
286 del_mail(i);
287 for (i = beg; i < end; i++)
288 if ((!uidl && size_ok(i)) || (uidl && !uidl_new(i)))
289 reply_line(line, sizeof(line));
291 return 0;
294 static int fetch(struct account *account)
296 char line[BUFFSIZE];
297 int len;
298 int batch;
299 int i;
300 nmails = 0;
301 conn = conn_connect(account->server, account->port, account->cert);
302 if (!conn)
303 return -1;
304 buf_cur = buf;
305 buf_end = buf;
306 if (account->uidl)
307 uidl = uidl_read(account->uidl);
308 sprintf(line, "fetching %s@%s\n", account->user, account->server);
309 print(line, strlen(line));
310 len = reply_line(line, sizeof(line));
311 login(account->user, account->pass);
312 mail_stat();
313 if (account->uidl)
314 mail_uidl();
315 batch = account->nopipe ? 1 : nmails;
316 for (i = 0; i < nmails; i += batch)
317 ret_mails(i, MIN(nmails, i + batch), account->del);
318 send_cmd("QUIT\r\n");
319 len = reply_line(line, sizeof(line));
320 conn_close(conn);
321 if (uidl)
322 uidl_save(uidl);
323 uidl = NULL;
324 return 0;
327 static void sigint(int sig)
329 if (uidl)
330 uidl_save(uidl);
331 exit(1);
334 int main(int argc, char *argv[])
336 int i;
337 signal(SIGINT, sigint);
338 for (i = 0; i < ARRAY_SIZE(accounts); i++) {
339 if (fetch(&accounts[i]) == -1)
340 return 1;
342 return 0;