1 /* vi: set sw=4 ts=4: */
3 * popmaildir: a simple yet powerful POP3 client
4 * Delivers contents of remote mailboxes to local Maildir
6 * Inspired by original utility by Nikola Vladov
8 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
10 * Licensed under GPLv2, see file LICENSE in this tarball for details.
15 static void pop3_checkr(const char *fmt, const char *param, char **ret)
17 const char *msg = command(fmt, param);
18 char *answer = xmalloc_fgetline(stdin);
19 if (answer && '+' == answer[0]) {
24 memmove(answer, answer + 4, strlen(answer) - 4);
30 bb_error_msg_and_die("%s failed: %s", msg, answer);
33 static void pop3_check(const char *fmt, const char *param)
35 pop3_checkr(fmt, param, NULL);
38 int popmaildir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
39 int popmaildir_main(int argc UNUSED_PARAM, char **argv)
46 #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
49 unsigned opt_nlines = 0;
52 OPT_b = 1 << 0, // -b binary mode. Ignored
53 OPT_d = 1 << 1, // -d,-dd,-ddd debug. Ignored
54 OPT_m = 1 << 2, // -m show used memory. Ignored
55 OPT_V = 1 << 3, // -V version. Ignored
56 OPT_c = 1 << 4, // -c use tcpclient. Ignored
57 OPT_a = 1 << 5, // -a use APOP protocol
58 OPT_s = 1 << 6, // -s skip authorization
59 OPT_T = 1 << 7, // -T get messages with TOP instead with RETR
60 OPT_k = 1 << 8, // -k keep retrieved messages on the server
61 OPT_t = 1 << 9, // -t90 set timeout to 90 sec
62 OPT_R = 1 << 10, // -R20000 remove old messages on the server >= 20000 bytes (requires -k). Ignored
63 OPT_Z = 1 << 11, // -Z11-23 remove messages from 11 to 23 (dangerous). Ignored
64 OPT_L = 1 << 12, // -L50000 not retrieve new messages >= 50000 bytes. Ignored
65 OPT_H = 1 << 13, // -H30 type first 30 lines of a message; (-L12000 -H30). Ignored
66 OPT_M = 1 << 14, // -M\"program arg1 arg2 ...\"; deliver by program. Treated like -F
67 OPT_F = 1 << 15, // -F\"program arg1 arg2 ...\"; filter by program. Treated like -M
70 // init global variables
74 opt_complementary = "-1:dd:t+:R+:L+:H+";
76 "bdmVcasTkt:" "R:Z:L:H:" IF_FEATURE_POPMAILDIR_DELIVERY("M:F:"),
77 &timeout, NULL, NULL, NULL, &opt_nlines
78 IF_FEATURE_POPMAILDIR_DELIVERY(, &delivery, &delivery) // we treat -M and -F the same
85 get_cred_or_die(STDIN_FILENO);
90 // launch connect helper, if any
92 launch_helper((const char **)argv);
94 // get server greeting
95 pop3_checkr(NULL, NULL, &buf);
97 // authenticate (if no -s given)
98 if (!(opts & OPT_s)) {
99 // server supports APOP and we want it?
100 if ('<' == buf[0] && (opts & OPT_a)) {
101 union { // save a bit of stack
103 char hex[16 * 2 + 1];
105 uint32_t res[16 / 4];
107 char *s = strchr(buf, '>');
110 // get md5 sum of "<stamp>password" string
112 md5_hash(buf, strlen(buf), &md5.ctx);
113 md5_hash(G.pass, strlen(G.pass), &md5.ctx);
114 md5_end(res, &md5.ctx);
115 *bin2hex(md5.hex, (char*)res, 16) = '\0';
117 s = xasprintf("%s %s", G.user, md5.hex);
118 pop3_check("APOP %s", s);
121 // server ignores APOP -> use simple text authentication
124 pop3_check("USER %s", G.user);
126 pop3_check("PASS %s", G.pass);
130 // get mailbox statistics
131 pop3_checkr("STAT", NULL, &buf);
133 // prepare message filename suffix
134 hostname = safe_gethostname();
137 // get messages counter
138 // NOTE: we don't use xatou(buf) since buf is "nmsg nbytes"
139 // we only need nmsg and atoi is just exactly what we need
140 // if atoi fails to convert buf into number it returns 0
141 // in this case the following loop simply will not be executed
145 // loop through messages
146 retr = (opts & OPT_T) ? xasprintf("TOP %%u %u", opt_nlines) : "RETR %u";
147 for (; nmsg; nmsg--) {
153 #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
156 // generate unique filename
157 filename = xasprintf("tmp/%llu.%u.%s",
158 monotonic_us(), (unsigned)pid, hostname);
160 // retrieve message in ./tmp/ unless filter is specified
161 pop3_check(retr, (const char *)(ptrdiff_t)nmsg);
163 #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
164 // delivery helper ordered? -> setup pipe
165 if (opts & (OPT_F|OPT_M)) {
166 // helper will have $FILENAME set to filename
167 xsetenv("FILENAME", filename);
168 fp = popen(delivery, "w");
169 unsetenv("FILENAME");
171 bb_perror_msg("delivery helper");
176 // create and open file filename
177 fp = xfopen_for_write(filename);
179 // copy stdin to fp (either filename or delivery helper)
180 while ((answer = xmalloc_fgets_str(stdin, "\r\n")) != NULL) {
182 if ('.' == answer[0]) {
183 if ('.' == answer[1])
185 else if ('\r' == answer[1] && '\n' == answer[2] && '\0' == answer[3])
188 //*strchrnul(s, '\r') = '\n';
193 #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
194 // analyse delivery status
195 if (opts & (OPT_F|OPT_M)) {
197 if (99 == rc) // 99 means bail out
199 // if (rc) // !0 means skip to the next message
201 // // 0 means continue
208 // delete message from server
210 pop3_check("DELE %u", (const char*)(ptrdiff_t)nmsg);
212 // atomically move message to ./new/
213 target = xstrdup(filename);
214 strncpy(target, "new", 3);
215 // ... or just stop receiving on failure
216 if (rename_or_warn(filename, target))
220 #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
227 pop3_check("QUIT", NULL);
229 if (ENABLE_FEATURE_CLEAN_UP) {