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.
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
{
43 static struct uidl
*uidl
;
45 static char buf
[BUFFSIZE
];
48 static struct conn
*conn
;
51 static int pop3_read(void)
53 if (buf_pos
== buf_len
) {
54 buf_len
= conn_read(conn
, buf
, sizeof(buf
));
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
)
78 /* read a pop3 response line */
79 static int pop3_res(char *dst
, int len
)
87 /* send a pop3 command */
88 static void pop3_cmd(char *cmd
, ...)
93 vsnprintf(buf
, sizeof(buf
), cmd
, ap
);
95 conn_write(conn
, buf
, strlen(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
)
108 pop3_cmd("USER %s\r\n", user
);
109 if (pop3_res(line
, sizeof(line
)))
111 pop3_cmd("PASS %s\r\n", pass
);
112 if (pop3_res(line
, sizeof(line
)))
117 static int pop3_stat(void)
121 pop3_cmd("STAT\r\n");
122 if (pop3_res(line
, sizeof(line
)))
125 pop3_cmd("LIST\r\n");
126 if (pop3_res(line
, sizeof(line
)))
128 while ((len
= pop3_get(line
, sizeof(line
))) >= 0) {
129 struct mailinfo
*mail
;
130 if (pop3_iseoc(line
, len
))
132 mail
= &mails
[nmails
++];
133 sscanf(line
, "%s %d", mail
->name
, &mail
->size
);
138 static int pop3_uidl(void)
144 pop3_cmd("UIDL\r\n");
145 if (pop3_res(line
, sizeof(line
)))
147 while ((len
= pop3_get(line
, sizeof(line
))) > 0 && !pop3_iseoc(line
, len
))
148 sscanf(line
, "%s %s", name
, mails
[i
++].id
);
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
)
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
;
168 static int xwrite(int fd
, char *buf
, int len
)
172 int ret
= write(fd
, buf
+ nw
, len
- nw
);
173 if (ret
== -1 && (errno
== EAGAIN
|| errno
== EINTR
))
182 static int mail_write(char *dst
, char *mail
, int len
)
184 int fd
= open(dst
, O_WRONLY
| O_APPEND
| O_CREAT
, 0600);
187 if (xwrite(fd
, mail
, len
) != len
)
193 static int mail_from_(char *s
)
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
)
206 return !strncmp("From ", s
, 5);
209 static int fetch_one(int i
)
216 if (pop3_res(line
, sizeof(line
)))
218 printf("%s", mails
[i
].name
);
222 len
= pop3_get(line
, sizeof(line
));
223 if (len
<= 0) /* end of stream or error */
225 if (pop3_iseoc(line
, len
))
227 if (len
> 1 && line
[len
- 2] == '\r')
228 line
[len
-- - 2] = '\n';
232 dst
= mail_dst(line
, len
);
233 if (pop3_lonefrom_(line
))
235 memcpy(s
, line
, len
);
241 ret
= mail_write(dst
, mailbuf
, s
- mailbuf
);
242 printf(" -> %s%s\n", dst
, ret
? " [failed]" : "");
246 static void pop3_del(int i
)
248 pop3_cmd("DELE %s\r\n", mails
[i
].name
);
251 static int size_ok(int i
)
253 return mails
[i
].size
+ 100 < MAXSIZE
;
256 static int uidl_new(int i
)
258 return !uidl
|| !uidl_find(uidl
, mails
[i
].id
);
261 static int fetch_mails(int beg
, int end
, int del
)
265 for (i
= beg
; i
< end
; i
++)
266 if (size_ok(i
) && uidl_new(i
))
268 for (i
= beg
; i
< end
; i
++) {
269 if (size_ok(i
) && uidl_new(i
)) {
273 uidl_add(uidl
, mails
[i
].id
);
277 for (i
= beg
; i
< end
; i
++)
278 if ((!uidl
&& size_ok(i
)) || (uidl
&& !uidl_new(i
)))
280 for (i
= beg
; i
< end
; i
++)
281 if ((!uidl
&& size_ok(i
)) || (uidl
&& !uidl_new(i
)))
282 pop3_get(line
, sizeof(line
));
287 static int fetch(struct account
*account
)
294 conn
= conn_connect(account
->server
, account
->port
);
298 if (pop3_res(line
, sizeof(line
)))
300 pop3_cmd("STLS\r\n");
301 if (pop3_res(line
, sizeof(line
)))
304 if (conn_tls(conn
, account
->cert
)) {
311 uidl
= uidl_read(account
->uidl
);
312 printf("fetching %s@%s\n", account
->user
, account
->server
);
314 if (pop3_res(line
, sizeof(line
)))
316 if (pop3_login(account
->user
, account
->pass
))
323 batch
= account
->nopipe
? 1 : nmails
;
324 for (i
= 0; i
< nmails
; i
+= batch
)
325 if ((failed
= fetch_mails(i
, MIN(nmails
, i
+ batch
), account
->del
)))
328 pop3_cmd("QUIT\r\n");
329 pop3_get(line
, sizeof(line
));
338 static void sigint(int sig
)
345 int main(int argc
, char *argv
[])
348 signal(SIGINT
, sigint
);
349 mailbuf
= malloc(MAXSIZE
);
350 for (i
= 0; i
< ARRAY_SIZE(accounts
); i
++)