pop3: increase the default message-id length
[pop3.git] / pop3.c
blob5aadbb4720662ba471e8aa54e458e4a45a8c9df6
1 /*
2 * pop3 - a simple pop3 mail client
4 * Copyright (C) 2010-2013 Ali Gholami Rudi
6 * This program is released under the modified BSD license.
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))
25 #define PRINT(s, l) (write(1, (s), (l)))
27 static struct mailinfo {
28 char name[1 << 4];
29 char id[1 << 6];
30 int size;
31 } mails[MAXMAILS];
32 static int nmails;
33 static struct uidl *uidl;
35 static char buf[BUFFSIZE];
36 static int buf_len;
37 static int buf_pos;
38 static struct conn *conn;
39 static char *mailbuf;
41 static int pop3_read(void)
43 if (buf_pos == buf_len) {
44 buf_len = conn_read(conn, buf, sizeof(buf));
45 buf_pos = 0;
47 return buf_pos < buf_len ? (unsigned char) buf[buf_pos++] : -1;
50 static int pop3_line(char *dst, int len)
52 int i = 0;
53 int c;
54 while (i < len) {
55 c = pop3_read();
56 if (c < 0)
57 return -1;
58 dst[i++] = c;
59 if (c == '\n')
60 break;
62 DPRINT(dst, i);
63 return i;
66 static void send_cmd(char *cmd)
68 conn_write(conn, cmd, strlen(cmd));
69 DPRINT(cmd, strlen(cmd));
72 static int is_eoc(char *line, int len)
74 return len >= 2 && len <= 3 && line[0] == '.' &&
75 (line[1] == '\r' || line[1] == '\n');
78 static char *cutword(char *dst, char *s)
80 while (*s && isspace(*s))
81 s++;
82 while (*s && !isspace(*s))
83 *dst++ = *s++;
84 *dst = '\0';
85 return s;
88 static void login(char *user, char *pass)
90 char line[BUFFSIZE];
91 sprintf(line, "USER %s\r\n", user);
92 send_cmd(line);
93 pop3_line(line, sizeof(line));
94 sprintf(line, "PASS %s\r\n", pass);
95 send_cmd(line);
96 pop3_line(line, sizeof(line));
99 static void mail_stat(void)
101 char line[BUFFSIZE];
102 int len;
103 send_cmd("STAT\r\n");
104 len = pop3_line(line, sizeof(line));
105 PRINT(line, len);
106 send_cmd("LIST\r\n");
107 len = pop3_line(line, sizeof(line));
108 while ((len = pop3_line(line, sizeof(line))) != -1) {
109 struct mailinfo *mail;
110 char *s = line;
111 if (is_eoc(line, len))
112 break;
113 mail = &mails[nmails++];
114 s = cutword(mail->name, s);
115 mail->size = atoi(s);
119 static void mail_uidl(void)
121 char line[BUFFSIZE];
122 char name[100];
123 int len;
124 int i = 0;
125 send_cmd("UIDL\r\n");
126 len = pop3_line(line, sizeof(line));
127 while ((len = pop3_line(line, sizeof(line))) > 0 && !is_eoc(line, len)) {
128 char *s = line;
129 s = cutword(name, s);
130 s = cutword(mails[i++].id, s);
134 static void req_mail(int i)
136 char cmd[100];
137 sprintf(cmd, "RETR %s\r\n", mails[i].name);
138 send_cmd(cmd);
141 static char *mail_dst(char *hdr, int len)
143 int i;
144 hdr[len] = '\0';
145 for (i = 0; i < ARRAY_SIZE(filters); i++)
146 if (!strncmp(filters[i].hdr, hdr, strlen(filters[i].hdr)) &&
147 strstr(hdr, filters[i].val))
148 return filters[i].dst;
149 return NULL;
152 static int xwrite(int fd, char *buf, int len)
154 int nw = 0;
155 while (nw < len) {
156 int ret = write(fd, buf + nw, len - nw);
157 if (ret == -1 && (errno == EAGAIN || errno == EINTR))
158 continue;
159 if (ret < 0)
160 break;
161 nw += ret;
163 return nw;
166 static int mail_write(char *dst, char *mail, int len)
168 int fd = open(dst, O_WRONLY | O_APPEND | O_CREAT, 0600);
169 if (fd == -1)
170 return -1;
171 if (xwrite(fd, mail, len) != len)
172 return -1;
173 close(fd);
174 return 0;
177 static char *putstr(char *dst, char *src)
179 int len = strchr(src, '\0') - src;
180 memcpy(dst, src, len + 1);
181 return dst + len;
184 static char *put_from_(char *s)
186 time_t t;
187 time(&t);
188 s = putstr(s, "From ");
189 s = putstr(s, getenv("USER") ? getenv("USER") : "root");
190 s += strftime(s, MAXSIZE, " %a %b %d %H:%M:%S %Y\n", localtime(&t));
191 return s;
194 static int lone_from(char *s)
196 while (*s == '>')
197 s++;
198 return !strncmp("From ", s, 5);
201 static int ret_mail(int i)
203 char line[BUFFSIZE];
204 char *s = mailbuf;
205 int len = pop3_line(line, sizeof(line));
206 char *dst = NULL;
207 int hdr = 1;
208 int ret;
209 PRINT(mails[i].name, strlen(mails[i].name));
210 s = put_from_(s);
211 while (1) {
212 len = pop3_line(line, sizeof(line));
213 if (len <= 0) /* end of stream or error */
214 return -1;
215 if (is_eoc(line, len))
216 break;
217 if (len > 1 && line[len - 2] == '\r')
218 line[len-- - 2] = '\n';
219 if (line[0] == '\n')
220 hdr = 0;
221 if (hdr && !dst)
222 dst = mail_dst(line, len);
223 if (lone_from(line))
224 *s++ = '>';
225 memcpy(s, line, len);
226 s += len;
228 *s++ = '\n';
229 if (!dst)
230 dst = SPOOL;
231 ret = mail_write(dst, mailbuf, s - mailbuf);
232 sprintf(line, " -> %s\n", dst);
233 PRINT(line, strlen(line));
234 return ret;
237 static void del_mail(int i)
239 char cmd[100];
240 sprintf(cmd, "DELE %s\r\n", mails[i].name);
241 send_cmd(cmd);
244 static int size_ok(int i)
246 return mails[i].size + 100 < MAXSIZE;
249 static int uidl_new(int i)
251 return !uidl || !uidl_find(uidl, mails[i].id);
254 static int ret_mails(int beg, int end, int del)
256 char line[BUFFSIZE];
257 int i;
258 for (i = beg; i < end; i++)
259 if (size_ok(i) && uidl_new(i))
260 req_mail(i);
261 for (i = beg; i < end; i++) {
262 if (size_ok(i) && uidl_new(i)) {
263 if (ret_mail(i) == -1)
264 return -1;
265 if (uidl)
266 uidl_add(uidl, mails[i].id);
269 if (del) {
270 for (i = beg; i < end; i++)
271 if ((!uidl && size_ok(i)) || (uidl && !uidl_new(i)))
272 del_mail(i);
273 for (i = beg; i < end; i++)
274 if ((!uidl && size_ok(i)) || (uidl && !uidl_new(i)))
275 pop3_line(line, sizeof(line));
277 return 0;
280 static int fetch(struct account *account)
282 char line[BUFFSIZE];
283 int batch;
284 int i;
285 nmails = 0;
286 conn = conn_connect(account->server, account->port, account->cert);
287 if (!conn)
288 return -1;
289 buf_pos = 0;
290 buf_len = 0;
291 if (account->uidl)
292 uidl = uidl_read(account->uidl);
293 sprintf(line, "fetching %s@%s\n", account->user, account->server);
294 PRINT(line, strlen(line));
295 pop3_line(line, sizeof(line));
296 login(account->user, account->pass);
297 mail_stat();
298 if (account->uidl)
299 mail_uidl();
300 batch = account->nopipe ? 1 : nmails;
301 for (i = 0; i < nmails; i += batch)
302 ret_mails(i, MIN(nmails, i + batch), account->del);
303 send_cmd("QUIT\r\n");
304 pop3_line(line, sizeof(line));
305 conn_close(conn);
306 if (uidl)
307 uidl_save(uidl);
308 uidl = NULL;
309 return 0;
312 static void sigint(int sig)
314 if (uidl)
315 uidl_save(uidl);
316 exit(1);
319 int main(int argc, char *argv[])
321 int i;
322 signal(SIGINT, sigint);
323 mailbuf = malloc(MAXSIZE);
324 for (i = 0; i < ARRAY_SIZE(accounts); i++)
325 if (fetch(&accounts[i]) == -1)
326 return 1;
327 free(mailbuf);
328 return 0;