]>
Commit | Line | Data |
---|---|---|
f2561fda MM |
1 | /* |
2 | * git-imap-send - drops patches into an imap Drafts folder | |
3 | * derived from isync/mbsync - mailbox synchronizer | |
4 | * | |
5 | * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org> | |
6 | * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net> | |
7 | * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu> | |
8 | * Copyright (C) 2006 Mike McCormack | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 | */ | |
24 | ||
25 | #include "cache.h" | |
2fb3f6db | 26 | #include "exec_cmd.h" |
c94d2dd0 | 27 | #include "run-command.h" |
684ec6c6 RS |
28 | #ifdef NO_OPENSSL |
29 | typedef void *SSL; | |
ae9c606e HM |
30 | #else |
31 | #include <openssl/evp.h> | |
32 | #include <openssl/hmac.h> | |
684ec6c6 | 33 | #endif |
f2561fda | 34 | |
9f1ad541 | 35 | struct store_conf { |
f2561fda MM |
36 | char *name; |
37 | const char *path; /* should this be here? its interpretation is driver-specific */ | |
38 | char *map_inbox; | |
39 | char *trash; | |
40 | unsigned max_size; /* off_t is overkill */ | |
41 | unsigned trash_remote_new:1, trash_only_new:1; | |
9f1ad541 | 42 | }; |
f2561fda | 43 | |
f2561fda MM |
44 | /* For message->status */ |
45 | #define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */ | |
46 | #define M_DEAD (1<<1) /* expunged */ | |
47 | #define M_FLAGS (1<<2) /* flags fetched */ | |
48 | ||
9f1ad541 | 49 | struct message { |
f2561fda | 50 | struct message *next; |
f2561fda MM |
51 | size_t size; /* zero implies "not fetched" */ |
52 | int uid; | |
53 | unsigned char flags, status; | |
9f1ad541 | 54 | }; |
f2561fda | 55 | |
9f1ad541 JH |
56 | struct store { |
57 | struct store_conf *conf; /* foreign */ | |
f2561fda MM |
58 | |
59 | /* currently open mailbox */ | |
60 | const char *name; /* foreign! maybe preset? */ | |
61 | char *path; /* own */ | |
9f1ad541 | 62 | struct message *msgs; /* own */ |
f2561fda MM |
63 | int uidvalidity; |
64 | unsigned char opts; /* maybe preset? */ | |
65 | /* note that the following do _not_ reflect stats from msgs, but mailbox totals */ | |
66 | int count; /* # of messages */ | |
67 | int recent; /* # of recent messages - don't trust this beyond the initial read */ | |
9f1ad541 | 68 | }; |
f2561fda | 69 | |
9f1ad541 | 70 | struct msg_data { |
f2561fda MM |
71 | char *data; |
72 | int len; | |
73 | unsigned char flags; | |
9f1ad541 | 74 | }; |
f2561fda | 75 | |
9a2861e3 JN |
76 | static const char imap_send_usage[] = "git imap-send < <mbox>"; |
77 | ||
d23b1ecf | 78 | #undef DRV_OK |
f2561fda MM |
79 | #define DRV_OK 0 |
80 | #define DRV_MSG_BAD -1 | |
81 | #define DRV_BOX_BAD -2 | |
82 | #define DRV_STORE_BAD -3 | |
83 | ||
84 | static int Verbose, Quiet; | |
85 | ||
28bea9e5 | 86 | __attribute__((format (printf, 1, 2))) |
95c53908 | 87 | static void imap_info(const char *, ...); |
28bea9e5 | 88 | __attribute__((format (printf, 1, 2))) |
95c53908 | 89 | static void imap_warn(const char *, ...); |
f2561fda | 90 | |
95c53908 | 91 | static char *next_arg(char **); |
f2561fda | 92 | |
9f1ad541 | 93 | static void free_generic_messages(struct message *); |
f2561fda | 94 | |
28bea9e5 | 95 | __attribute__((format (printf, 3, 4))) |
95c53908 | 96 | static int nfsnprintf(char *buf, int blen, const char *fmt, ...); |
f2561fda | 97 | |
19247e55 PH |
98 | static int nfvasprintf(char **strp, const char *fmt, va_list ap) |
99 | { | |
100 | int len; | |
101 | char tmp[8192]; | |
102 | ||
103 | len = vsnprintf(tmp, sizeof(tmp), fmt, ap); | |
104 | if (len < 0) | |
d7530708 | 105 | die("Fatal: Out of memory"); |
19247e55 | 106 | if (len >= sizeof(tmp)) |
d7530708 | 107 | die("imap command overflow!"); |
19247e55 PH |
108 | *strp = xmemdupz(tmp, len); |
109 | return len; | |
110 | } | |
f2561fda | 111 | |
9f1ad541 | 112 | struct imap_server_conf { |
f2561fda MM |
113 | char *name; |
114 | char *tunnel; | |
115 | char *host; | |
116 | int port; | |
117 | char *user; | |
118 | char *pass; | |
684ec6c6 RS |
119 | int use_ssl; |
120 | int ssl_verify; | |
c64d84f1 | 121 | int use_html; |
ae9c606e HM |
122 | char *auth_method; |
123 | }; | |
124 | ||
125 | static struct imap_server_conf server = { | |
126 | NULL, /* name */ | |
127 | NULL, /* tunnel */ | |
128 | NULL, /* host */ | |
129 | 0, /* port */ | |
130 | NULL, /* user */ | |
131 | NULL, /* pass */ | |
132 | 0, /* use_ssl */ | |
133 | 1, /* ssl_verify */ | |
134 | 0, /* use_html */ | |
135 | NULL, /* auth_method */ | |
9f1ad541 | 136 | }; |
f2561fda | 137 | |
9f1ad541 JH |
138 | struct imap_store_conf { |
139 | struct store_conf gen; | |
140 | struct imap_server_conf *server; | |
f2561fda | 141 | unsigned use_namespace:1; |
9f1ad541 | 142 | }; |
f2561fda | 143 | |
9f1ad541 JH |
144 | #define NIL (void *)0x1 |
145 | #define LIST (void *)0x2 | |
f2561fda | 146 | |
9f1ad541 JH |
147 | struct imap_list { |
148 | struct imap_list *next, *child; | |
f2561fda MM |
149 | char *val; |
150 | int len; | |
9f1ad541 | 151 | }; |
f2561fda | 152 | |
9f1ad541 | 153 | struct imap_socket { |
7a7796e9 | 154 | int fd[2]; |
684ec6c6 | 155 | SSL *ssl; |
9f1ad541 | 156 | }; |
f2561fda | 157 | |
9f1ad541 JH |
158 | struct imap_buffer { |
159 | struct imap_socket sock; | |
f2561fda MM |
160 | int bytes; |
161 | int offset; | |
162 | char buf[1024]; | |
9f1ad541 | 163 | }; |
f2561fda MM |
164 | |
165 | struct imap_cmd; | |
166 | ||
9f1ad541 | 167 | struct imap { |
f2561fda | 168 | int uidnext; /* from SELECT responses */ |
9f1ad541 | 169 | struct imap_list *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */ |
f2561fda MM |
170 | unsigned caps, rcaps; /* CAPABILITY results */ |
171 | /* command queue */ | |
172 | int nexttag, num_in_progress, literal_pending; | |
173 | struct imap_cmd *in_progress, **in_progress_append; | |
9f1ad541 JH |
174 | struct imap_buffer buf; /* this is BIG, so put it last */ |
175 | }; | |
f2561fda | 176 | |
9f1ad541 JH |
177 | struct imap_store { |
178 | struct store gen; | |
f2561fda | 179 | int uidvalidity; |
9f1ad541 | 180 | struct imap *imap; |
f2561fda MM |
181 | const char *prefix; |
182 | unsigned /*currentnc:1,*/ trashnc:1; | |
9f1ad541 | 183 | }; |
f2561fda MM |
184 | |
185 | struct imap_cmd_cb { | |
9f1ad541 JH |
186 | int (*cont)(struct imap_store *ctx, struct imap_cmd *cmd, const char *prompt); |
187 | void (*done)(struct imap_store *ctx, struct imap_cmd *cmd, int response); | |
f2561fda MM |
188 | void *ctx; |
189 | char *data; | |
190 | int dlen; | |
191 | int uid; | |
192 | unsigned create:1, trycreate:1; | |
193 | }; | |
194 | ||
195 | struct imap_cmd { | |
196 | struct imap_cmd *next; | |
197 | struct imap_cmd_cb cb; | |
198 | char *cmd; | |
199 | int tag; | |
200 | }; | |
201 | ||
202 | #define CAP(cap) (imap->caps & (1 << (cap))) | |
203 | ||
204 | enum CAPABILITY { | |
205 | NOLOGIN = 0, | |
206 | UIDPLUS, | |
207 | LITERALPLUS, | |
208 | NAMESPACE, | |
684ec6c6 | 209 | STARTTLS, |
4b05548f | 210 | AUTH_CRAM_MD5 |
f2561fda MM |
211 | }; |
212 | ||
213 | static const char *cap_list[] = { | |
214 | "LOGINDISABLED", | |
215 | "UIDPLUS", | |
216 | "LITERAL+", | |
217 | "NAMESPACE", | |
684ec6c6 | 218 | "STARTTLS", |
ae9c606e | 219 | "AUTH=CRAM-MD5", |
f2561fda MM |
220 | }; |
221 | ||
222 | #define RESP_OK 0 | |
223 | #define RESP_NO 1 | |
224 | #define RESP_BAD 2 | |
225 | ||
9f1ad541 | 226 | static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd); |
f2561fda MM |
227 | |
228 | ||
229 | static const char *Flags[] = { | |
230 | "Draft", | |
231 | "Flagged", | |
232 | "Answered", | |
233 | "Seen", | |
234 | "Deleted", | |
235 | }; | |
236 | ||
684ec6c6 RS |
237 | #ifndef NO_OPENSSL |
238 | static void ssl_socket_perror(const char *func) | |
239 | { | |
2af202be | 240 | fprintf(stderr, "%s: %s\n", func, ERR_error_string(ERR_get_error(), NULL)); |
684ec6c6 RS |
241 | } |
242 | #endif | |
243 | ||
9f1ad541 | 244 | static void socket_perror(const char *func, struct imap_socket *sock, int ret) |
f2561fda | 245 | { |
684ec6c6 RS |
246 | #ifndef NO_OPENSSL |
247 | if (sock->ssl) { | |
248 | int sslerr = SSL_get_error(sock->ssl, ret); | |
249 | switch (sslerr) { | |
250 | case SSL_ERROR_NONE: | |
251 | break; | |
252 | case SSL_ERROR_SYSCALL: | |
253 | perror("SSL_connect"); | |
254 | break; | |
255 | default: | |
256 | ssl_socket_perror("SSL_connect"); | |
257 | break; | |
258 | } | |
259 | } else | |
260 | #endif | |
261 | { | |
262 | if (ret < 0) | |
263 | perror(func); | |
264 | else | |
265 | fprintf(stderr, "%s: unexpected EOF\n", func); | |
266 | } | |
267 | } | |
268 | ||
1e1fe529 | 269 | #ifdef NO_OPENSSL |
9f1ad541 | 270 | static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify) |
684ec6c6 | 271 | { |
684ec6c6 RS |
272 | fprintf(stderr, "SSL requested but SSL support not compiled in\n"); |
273 | return -1; | |
1e1fe529 JH |
274 | } |
275 | ||
1e380ddc | 276 | #else |
1e1fe529 JH |
277 | |
278 | static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify) | |
279 | { | |
1e380ddc VL |
280 | #if (OPENSSL_VERSION_NUMBER >= 0x10000000L) |
281 | const SSL_METHOD *meth; | |
684ec6c6 RS |
282 | #else |
283 | SSL_METHOD *meth; | |
1e380ddc | 284 | #endif |
684ec6c6 RS |
285 | SSL_CTX *ctx; |
286 | int ret; | |
287 | ||
288 | SSL_library_init(); | |
289 | SSL_load_error_strings(); | |
290 | ||
291 | if (use_tls_only) | |
292 | meth = TLSv1_method(); | |
f2561fda | 293 | else |
684ec6c6 RS |
294 | meth = SSLv23_method(); |
295 | ||
296 | if (!meth) { | |
297 | ssl_socket_perror("SSLv23_method"); | |
298 | return -1; | |
299 | } | |
300 | ||
301 | ctx = SSL_CTX_new(meth); | |
302 | ||
303 | if (verify) | |
304 | SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); | |
305 | ||
306 | if (!SSL_CTX_set_default_verify_paths(ctx)) { | |
307 | ssl_socket_perror("SSL_CTX_set_default_verify_paths"); | |
308 | return -1; | |
309 | } | |
310 | sock->ssl = SSL_new(ctx); | |
311 | if (!sock->ssl) { | |
312 | ssl_socket_perror("SSL_new"); | |
313 | return -1; | |
314 | } | |
7a7796e9 EFL |
315 | if (!SSL_set_rfd(sock->ssl, sock->fd[0])) { |
316 | ssl_socket_perror("SSL_set_rfd"); | |
317 | return -1; | |
318 | } | |
319 | if (!SSL_set_wfd(sock->ssl, sock->fd[1])) { | |
320 | ssl_socket_perror("SSL_set_wfd"); | |
684ec6c6 RS |
321 | return -1; |
322 | } | |
323 | ||
324 | ret = SSL_connect(sock->ssl); | |
325 | if (ret <= 0) { | |
326 | socket_perror("SSL_connect", sock, ret); | |
327 | return -1; | |
328 | } | |
329 | ||
330 | return 0; | |
f2561fda | 331 | } |
1e1fe529 | 332 | #endif |
f2561fda | 333 | |
9f1ad541 | 334 | static int socket_read(struct imap_socket *sock, char *buf, int len) |
f2561fda | 335 | { |
684ec6c6 RS |
336 | ssize_t n; |
337 | #ifndef NO_OPENSSL | |
338 | if (sock->ssl) | |
339 | n = SSL_read(sock->ssl, buf, len); | |
340 | else | |
341 | #endif | |
7a7796e9 | 342 | n = xread(sock->fd[0], buf, len); |
f2561fda | 343 | if (n <= 0) { |
95c53908 | 344 | socket_perror("read", sock, n); |
7a7796e9 EFL |
345 | close(sock->fd[0]); |
346 | close(sock->fd[1]); | |
347 | sock->fd[0] = sock->fd[1] = -1; | |
f2561fda MM |
348 | } |
349 | return n; | |
350 | } | |
351 | ||
9f1ad541 | 352 | static int socket_write(struct imap_socket *sock, const char *buf, int len) |
f2561fda | 353 | { |
684ec6c6 RS |
354 | int n; |
355 | #ifndef NO_OPENSSL | |
356 | if (sock->ssl) | |
357 | n = SSL_write(sock->ssl, buf, len); | |
358 | else | |
359 | #endif | |
7a7796e9 | 360 | n = write_in_full(sock->fd[1], buf, len); |
f2561fda | 361 | if (n != len) { |
95c53908 | 362 | socket_perror("write", sock, n); |
7a7796e9 EFL |
363 | close(sock->fd[0]); |
364 | close(sock->fd[1]); | |
365 | sock->fd[0] = sock->fd[1] = -1; | |
f2561fda MM |
366 | } |
367 | return n; | |
368 | } | |
369 | ||
9f1ad541 | 370 | static void socket_shutdown(struct imap_socket *sock) |
684ec6c6 RS |
371 | { |
372 | #ifndef NO_OPENSSL | |
373 | if (sock->ssl) { | |
374 | SSL_shutdown(sock->ssl); | |
375 | SSL_free(sock->ssl); | |
376 | } | |
377 | #endif | |
7a7796e9 EFL |
378 | close(sock->fd[0]); |
379 | close(sock->fd[1]); | |
684ec6c6 RS |
380 | } |
381 | ||
f2561fda | 382 | /* simple line buffering */ |
9f1ad541 | 383 | static int buffer_gets(struct imap_buffer *b, char **s) |
f2561fda MM |
384 | { |
385 | int n; | |
386 | int start = b->offset; | |
387 | ||
388 | *s = b->buf + start; | |
389 | ||
390 | for (;;) { | |
391 | /* make sure we have enough data to read the \r\n sequence */ | |
392 | if (b->offset + 1 >= b->bytes) { | |
393 | if (start) { | |
394 | /* shift down used bytes */ | |
395 | *s = b->buf; | |
396 | ||
95c53908 | 397 | assert(start <= b->bytes); |
f2561fda MM |
398 | n = b->bytes - start; |
399 | ||
400 | if (n) | |
173a9cbe | 401 | memmove(b->buf, b->buf + start, n); |
f2561fda MM |
402 | b->offset -= start; |
403 | b->bytes = n; | |
404 | start = 0; | |
405 | } | |
406 | ||
95c53908 RS |
407 | n = socket_read(&b->sock, b->buf + b->bytes, |
408 | sizeof(b->buf) - b->bytes); | |
f2561fda MM |
409 | |
410 | if (n <= 0) | |
411 | return -1; | |
412 | ||
413 | b->bytes += n; | |
414 | } | |
415 | ||
416 | if (b->buf[b->offset] == '\r') { | |
95c53908 | 417 | assert(b->offset + 1 < b->bytes); |
f2561fda MM |
418 | if (b->buf[b->offset + 1] == '\n') { |
419 | b->buf[b->offset] = 0; /* terminate the string */ | |
420 | b->offset += 2; /* next line */ | |
421 | if (Verbose) | |
95c53908 | 422 | puts(*s); |
f2561fda MM |
423 | return 0; |
424 | } | |
425 | } | |
426 | ||
427 | b->offset++; | |
428 | } | |
429 | /* not reached */ | |
430 | } | |
431 | ||
95c53908 | 432 | static void imap_info(const char *msg, ...) |
f2561fda MM |
433 | { |
434 | va_list va; | |
435 | ||
436 | if (!Quiet) { | |
95c53908 RS |
437 | va_start(va, msg); |
438 | vprintf(msg, va); | |
439 | va_end(va); | |
440 | fflush(stdout); | |
f2561fda MM |
441 | } |
442 | } | |
443 | ||
95c53908 | 444 | static void imap_warn(const char *msg, ...) |
f2561fda MM |
445 | { |
446 | va_list va; | |
447 | ||
448 | if (Quiet < 2) { | |
95c53908 RS |
449 | va_start(va, msg); |
450 | vfprintf(stderr, msg, va); | |
451 | va_end(va); | |
f2561fda MM |
452 | } |
453 | } | |
454 | ||
95c53908 | 455 | static char *next_arg(char **s) |
f2561fda MM |
456 | { |
457 | char *ret; | |
458 | ||
459 | if (!s || !*s) | |
5142db69 | 460 | return NULL; |
95c53908 | 461 | while (isspace((unsigned char) **s)) |
f2561fda MM |
462 | (*s)++; |
463 | if (!**s) { | |
5142db69 RS |
464 | *s = NULL; |
465 | return NULL; | |
f2561fda MM |
466 | } |
467 | if (**s == '"') { | |
468 | ++*s; | |
469 | ret = *s; | |
95c53908 | 470 | *s = strchr(*s, '"'); |
f2561fda MM |
471 | } else { |
472 | ret = *s; | |
95c53908 | 473 | while (**s && !isspace((unsigned char) **s)) |
f2561fda MM |
474 | (*s)++; |
475 | } | |
476 | if (*s) { | |
477 | if (**s) | |
478 | *(*s)++ = 0; | |
479 | if (!**s) | |
5142db69 | 480 | *s = NULL; |
f2561fda MM |
481 | } |
482 | return ret; | |
483 | } | |
484 | ||
9f1ad541 | 485 | static void free_generic_messages(struct message *msgs) |
f2561fda | 486 | { |
9f1ad541 | 487 | struct message *tmsg; |
f2561fda MM |
488 | |
489 | for (; msgs; msgs = tmsg) { | |
490 | tmsg = msgs->next; | |
95c53908 | 491 | free(msgs); |
f2561fda MM |
492 | } |
493 | } | |
494 | ||
95c53908 | 495 | static int nfsnprintf(char *buf, int blen, const char *fmt, ...) |
f2561fda MM |
496 | { |
497 | int ret; | |
498 | va_list va; | |
499 | ||
95c53908 RS |
500 | va_start(va, fmt); |
501 | if (blen <= 0 || (unsigned)(ret = vsnprintf(buf, blen, fmt, va)) >= (unsigned)blen) | |
d7530708 | 502 | die("Fatal: buffer too small. Please report a bug."); |
95c53908 | 503 | va_end(va); |
f2561fda MM |
504 | return ret; |
505 | } | |
506 | ||
9f1ad541 | 507 | static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx, |
95c53908 RS |
508 | struct imap_cmd_cb *cb, |
509 | const char *fmt, va_list ap) | |
f2561fda | 510 | { |
9f1ad541 | 511 | struct imap *imap = ctx->imap; |
f2561fda MM |
512 | struct imap_cmd *cmd; |
513 | int n, bufl; | |
514 | char buf[1024]; | |
515 | ||
95c53908 RS |
516 | cmd = xmalloc(sizeof(struct imap_cmd)); |
517 | nfvasprintf(&cmd->cmd, fmt, ap); | |
f2561fda MM |
518 | cmd->tag = ++imap->nexttag; |
519 | ||
520 | if (cb) | |
521 | cmd->cb = *cb; | |
522 | else | |
95c53908 | 523 | memset(&cmd->cb, 0, sizeof(cmd->cb)); |
f2561fda MM |
524 | |
525 | while (imap->literal_pending) | |
95c53908 | 526 | get_cmd_result(ctx, NULL); |
f2561fda | 527 | |
1702b138 ÆAB |
528 | if (!cmd->cb.data) |
529 | bufl = nfsnprintf(buf, sizeof(buf), "%d %s\r\n", cmd->tag, cmd->cmd); | |
530 | else | |
531 | bufl = nfsnprintf(buf, sizeof(buf), "%d %s{%d%s}\r\n", | |
532 | cmd->tag, cmd->cmd, cmd->cb.dlen, | |
533 | CAP(LITERALPLUS) ? "+" : ""); | |
534 | ||
f2561fda MM |
535 | if (Verbose) { |
536 | if (imap->num_in_progress) | |
95c53908 RS |
537 | printf("(%d in progress) ", imap->num_in_progress); |
538 | if (memcmp(cmd->cmd, "LOGIN", 5)) | |
539 | printf(">>> %s", buf); | |
f2561fda | 540 | else |
95c53908 | 541 | printf(">>> %d LOGIN <user> <pass>\n", cmd->tag); |
f2561fda | 542 | } |
95c53908 RS |
543 | if (socket_write(&imap->buf.sock, buf, bufl) != bufl) { |
544 | free(cmd->cmd); | |
545 | free(cmd); | |
8e0f7003 | 546 | if (cb) |
95c53908 | 547 | free(cb->data); |
f2561fda MM |
548 | return NULL; |
549 | } | |
550 | if (cmd->cb.data) { | |
551 | if (CAP(LITERALPLUS)) { | |
95c53908 RS |
552 | n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen); |
553 | free(cmd->cb.data); | |
f2561fda | 554 | if (n != cmd->cb.dlen || |
8e76bf3f | 555 | socket_write(&imap->buf.sock, "\r\n", 2) != 2) { |
95c53908 RS |
556 | free(cmd->cmd); |
557 | free(cmd); | |
f2561fda MM |
558 | return NULL; |
559 | } | |
5142db69 | 560 | cmd->cb.data = NULL; |
f2561fda MM |
561 | } else |
562 | imap->literal_pending = 1; | |
563 | } else if (cmd->cb.cont) | |
564 | imap->literal_pending = 1; | |
5142db69 | 565 | cmd->next = NULL; |
f2561fda MM |
566 | *imap->in_progress_append = cmd; |
567 | imap->in_progress_append = &cmd->next; | |
568 | imap->num_in_progress++; | |
569 | return cmd; | |
570 | } | |
571 | ||
28bea9e5 | 572 | __attribute__((format (printf, 3, 4))) |
9f1ad541 | 573 | static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx, |
95c53908 RS |
574 | struct imap_cmd_cb *cb, |
575 | const char *fmt, ...) | |
f2561fda MM |
576 | { |
577 | struct imap_cmd *ret; | |
578 | va_list ap; | |
579 | ||
95c53908 RS |
580 | va_start(ap, fmt); |
581 | ret = v_issue_imap_cmd(ctx, cb, fmt, ap); | |
582 | va_end(ap); | |
f2561fda MM |
583 | return ret; |
584 | } | |
585 | ||
28bea9e5 | 586 | __attribute__((format (printf, 3, 4))) |
9f1ad541 | 587 | static int imap_exec(struct imap_store *ctx, struct imap_cmd_cb *cb, |
95c53908 | 588 | const char *fmt, ...) |
f2561fda MM |
589 | { |
590 | va_list ap; | |
591 | struct imap_cmd *cmdp; | |
592 | ||
95c53908 RS |
593 | va_start(ap, fmt); |
594 | cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap); | |
595 | va_end(ap); | |
f2561fda MM |
596 | if (!cmdp) |
597 | return RESP_BAD; | |
598 | ||
95c53908 | 599 | return get_cmd_result(ctx, cmdp); |
f2561fda MM |
600 | } |
601 | ||
28bea9e5 | 602 | __attribute__((format (printf, 3, 4))) |
9f1ad541 | 603 | static int imap_exec_m(struct imap_store *ctx, struct imap_cmd_cb *cb, |
95c53908 | 604 | const char *fmt, ...) |
f2561fda MM |
605 | { |
606 | va_list ap; | |
607 | struct imap_cmd *cmdp; | |
608 | ||
95c53908 RS |
609 | va_start(ap, fmt); |
610 | cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap); | |
611 | va_end(ap); | |
f2561fda MM |
612 | if (!cmdp) |
613 | return DRV_STORE_BAD; | |
614 | ||
95c53908 | 615 | switch (get_cmd_result(ctx, cmdp)) { |
f2561fda MM |
616 | case RESP_BAD: return DRV_STORE_BAD; |
617 | case RESP_NO: return DRV_MSG_BAD; | |
618 | default: return DRV_OK; | |
619 | } | |
620 | } | |
621 | ||
9f1ad541 | 622 | static int is_atom(struct imap_list *list) |
f2561fda MM |
623 | { |
624 | return list && list->val && list->val != NIL && list->val != LIST; | |
625 | } | |
626 | ||
9f1ad541 | 627 | static int is_list(struct imap_list *list) |
f2561fda MM |
628 | { |
629 | return list && list->val == LIST; | |
630 | } | |
631 | ||
9f1ad541 | 632 | static void free_list(struct imap_list *list) |
f2561fda | 633 | { |
9f1ad541 | 634 | struct imap_list *tmp; |
f2561fda MM |
635 | |
636 | for (; list; list = tmp) { | |
637 | tmp = list->next; | |
95c53908 RS |
638 | if (is_list(list)) |
639 | free_list(list->child); | |
640 | else if (is_atom(list)) | |
641 | free(list->val); | |
642 | free(list); | |
f2561fda MM |
643 | } |
644 | } | |
645 | ||
9f1ad541 | 646 | static int parse_imap_list_l(struct imap *imap, char **sp, struct imap_list **curp, int level) |
f2561fda | 647 | { |
9f1ad541 | 648 | struct imap_list *cur; |
f2561fda MM |
649 | char *s = *sp, *p; |
650 | int n, bytes; | |
651 | ||
652 | for (;;) { | |
95c53908 | 653 | while (isspace((unsigned char)*s)) |
f2561fda MM |
654 | s++; |
655 | if (level && *s == ')') { | |
656 | s++; | |
657 | break; | |
658 | } | |
95c53908 | 659 | *curp = cur = xmalloc(sizeof(*cur)); |
f2561fda | 660 | curp = &cur->next; |
5142db69 | 661 | cur->val = NULL; /* for clean bail */ |
f2561fda MM |
662 | if (*s == '(') { |
663 | /* sublist */ | |
664 | s++; | |
665 | cur->val = LIST; | |
95c53908 | 666 | if (parse_imap_list_l(imap, &s, &cur->child, level + 1)) |
f2561fda MM |
667 | goto bail; |
668 | } else if (imap && *s == '{') { | |
669 | /* literal */ | |
95c53908 | 670 | bytes = cur->len = strtol(s + 1, &s, 10); |
f2561fda MM |
671 | if (*s != '}') |
672 | goto bail; | |
673 | ||
95c53908 | 674 | s = cur->val = xmalloc(cur->len); |
f2561fda MM |
675 | |
676 | /* dump whats left over in the input buffer */ | |
677 | n = imap->buf.bytes - imap->buf.offset; | |
678 | ||
679 | if (n > bytes) | |
680 | /* the entire message fit in the buffer */ | |
681 | n = bytes; | |
682 | ||
95c53908 | 683 | memcpy(s, imap->buf.buf + imap->buf.offset, n); |
f2561fda MM |
684 | s += n; |
685 | bytes -= n; | |
686 | ||
687 | /* mark that we used part of the buffer */ | |
688 | imap->buf.offset += n; | |
689 | ||
690 | /* now read the rest of the message */ | |
691 | while (bytes > 0) { | |
95c53908 | 692 | if ((n = socket_read(&imap->buf.sock, s, bytes)) <= 0) |
f2561fda MM |
693 | goto bail; |
694 | s += n; | |
695 | bytes -= n; | |
696 | } | |
697 | ||
95c53908 | 698 | if (buffer_gets(&imap->buf, &s)) |
f2561fda MM |
699 | goto bail; |
700 | } else if (*s == '"') { | |
701 | /* quoted string */ | |
702 | s++; | |
703 | p = s; | |
704 | for (; *s != '"'; s++) | |
705 | if (!*s) | |
706 | goto bail; | |
707 | cur->len = s - p; | |
708 | s++; | |
182af834 | 709 | cur->val = xmemdupz(p, cur->len); |
f2561fda MM |
710 | } else { |
711 | /* atom */ | |
712 | p = s; | |
95c53908 | 713 | for (; *s && !isspace((unsigned char)*s); s++) |
f2561fda MM |
714 | if (level && *s == ')') |
715 | break; | |
716 | cur->len = s - p; | |
9f1ad541 | 717 | if (cur->len == 3 && !memcmp("NIL", p, 3)) |
f2561fda | 718 | cur->val = NIL; |
9f1ad541 | 719 | else |
182af834 | 720 | cur->val = xmemdupz(p, cur->len); |
f2561fda MM |
721 | } |
722 | ||
723 | if (!level) | |
724 | break; | |
725 | if (!*s) | |
726 | goto bail; | |
727 | } | |
728 | *sp = s; | |
5142db69 | 729 | *curp = NULL; |
f2561fda MM |
730 | return 0; |
731 | ||
9f1ad541 | 732 | bail: |
5142db69 | 733 | *curp = NULL; |
f2561fda MM |
734 | return -1; |
735 | } | |
736 | ||
9f1ad541 | 737 | static struct imap_list *parse_imap_list(struct imap *imap, char **sp) |
f2561fda | 738 | { |
9f1ad541 | 739 | struct imap_list *head; |
f2561fda | 740 | |
95c53908 | 741 | if (!parse_imap_list_l(imap, sp, &head, 0)) |
f2561fda | 742 | return head; |
95c53908 | 743 | free_list(head); |
f2561fda MM |
744 | return NULL; |
745 | } | |
746 | ||
9f1ad541 | 747 | static struct imap_list *parse_list(char **sp) |
f2561fda | 748 | { |
95c53908 | 749 | return parse_imap_list(NULL, sp); |
f2561fda MM |
750 | } |
751 | ||
9f1ad541 | 752 | static void parse_capability(struct imap *imap, char *cmd) |
f2561fda MM |
753 | { |
754 | char *arg; | |
755 | unsigned i; | |
756 | ||
757 | imap->caps = 0x80000000; | |
95c53908 | 758 | while ((arg = next_arg(&cmd))) |
f2561fda | 759 | for (i = 0; i < ARRAY_SIZE(cap_list); i++) |
95c53908 | 760 | if (!strcmp(cap_list[i], arg)) |
f2561fda MM |
761 | imap->caps |= 1 << i; |
762 | imap->rcaps = imap->caps; | |
763 | } | |
764 | ||
9f1ad541 | 765 | static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb, |
95c53908 | 766 | char *s) |
f2561fda | 767 | { |
9f1ad541 | 768 | struct imap *imap = ctx->imap; |
f2561fda MM |
769 | char *arg, *p; |
770 | ||
771 | if (*s != '[') | |
772 | return RESP_OK; /* no response code */ | |
773 | s++; | |
95c53908 RS |
774 | if (!(p = strchr(s, ']'))) { |
775 | fprintf(stderr, "IMAP error: malformed response code\n"); | |
f2561fda MM |
776 | return RESP_BAD; |
777 | } | |
778 | *p++ = 0; | |
95c53908 RS |
779 | arg = next_arg(&s); |
780 | if (!strcmp("UIDVALIDITY", arg)) { | |
781 | if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg))) { | |
782 | fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n"); | |
f2561fda MM |
783 | return RESP_BAD; |
784 | } | |
95c53908 RS |
785 | } else if (!strcmp("UIDNEXT", arg)) { |
786 | if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) { | |
787 | fprintf(stderr, "IMAP error: malformed NEXTUID status\n"); | |
f2561fda MM |
788 | return RESP_BAD; |
789 | } | |
95c53908 RS |
790 | } else if (!strcmp("CAPABILITY", arg)) { |
791 | parse_capability(imap, s); | |
792 | } else if (!strcmp("ALERT", arg)) { | |
f2561fda MM |
793 | /* RFC2060 says that these messages MUST be displayed |
794 | * to the user | |
795 | */ | |
95c53908 RS |
796 | for (; isspace((unsigned char)*p); p++); |
797 | fprintf(stderr, "*** IMAP ALERT *** %s\n", p); | |
798 | } else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) { | |
799 | if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg)) || | |
9f1ad541 | 800 | !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) { |
95c53908 | 801 | fprintf(stderr, "IMAP error: malformed APPENDUID status\n"); |
f2561fda MM |
802 | return RESP_BAD; |
803 | } | |
804 | } | |
805 | return RESP_OK; | |
806 | } | |
807 | ||
9f1ad541 | 808 | static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd) |
f2561fda | 809 | { |
9f1ad541 | 810 | struct imap *imap = ctx->imap; |
f2561fda MM |
811 | struct imap_cmd *cmdp, **pcmdp, *ncmdp; |
812 | char *cmd, *arg, *arg1, *p; | |
813 | int n, resp, resp2, tag; | |
814 | ||
815 | for (;;) { | |
95c53908 | 816 | if (buffer_gets(&imap->buf, &cmd)) |
f2561fda MM |
817 | return RESP_BAD; |
818 | ||
95c53908 | 819 | arg = next_arg(&cmd); |
f2561fda | 820 | if (*arg == '*') { |
95c53908 | 821 | arg = next_arg(&cmd); |
f2561fda | 822 | if (!arg) { |
95c53908 | 823 | fprintf(stderr, "IMAP error: unable to parse untagged response\n"); |
f2561fda MM |
824 | return RESP_BAD; |
825 | } | |
826 | ||
95c53908 RS |
827 | if (!strcmp("NAMESPACE", arg)) { |
828 | imap->ns_personal = parse_list(&cmd); | |
829 | imap->ns_other = parse_list(&cmd); | |
830 | imap->ns_shared = parse_list(&cmd); | |
831 | } else if (!strcmp("OK", arg) || !strcmp("BAD", arg) || | |
832 | !strcmp("NO", arg) || !strcmp("BYE", arg)) { | |
833 | if ((resp = parse_response_code(ctx, NULL, cmd)) != RESP_OK) | |
f2561fda | 834 | return resp; |
95c53908 RS |
835 | } else if (!strcmp("CAPABILITY", arg)) |
836 | parse_capability(imap, cmd); | |
837 | else if ((arg1 = next_arg(&cmd))) { | |
838 | if (!strcmp("EXISTS", arg1)) | |
839 | ctx->gen.count = atoi(arg); | |
840 | else if (!strcmp("RECENT", arg1)) | |
841 | ctx->gen.recent = atoi(arg); | |
f2561fda | 842 | } else { |
95c53908 | 843 | fprintf(stderr, "IMAP error: unable to parse untagged response\n"); |
f2561fda MM |
844 | return RESP_BAD; |
845 | } | |
846 | } else if (!imap->in_progress) { | |
95c53908 | 847 | fprintf(stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : ""); |
f2561fda MM |
848 | return RESP_BAD; |
849 | } else if (*arg == '+') { | |
850 | /* This can happen only with the last command underway, as | |
851 | it enforces a round-trip. */ | |
852 | cmdp = (struct imap_cmd *)((char *)imap->in_progress_append - | |
853 | offsetof(struct imap_cmd, next)); | |
854 | if (cmdp->cb.data) { | |
95c53908 RS |
855 | n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen); |
856 | free(cmdp->cb.data); | |
5142db69 | 857 | cmdp->cb.data = NULL; |
f2561fda MM |
858 | if (n != (int)cmdp->cb.dlen) |
859 | return RESP_BAD; | |
860 | } else if (cmdp->cb.cont) { | |
95c53908 | 861 | if (cmdp->cb.cont(ctx, cmdp, cmd)) |
f2561fda MM |
862 | return RESP_BAD; |
863 | } else { | |
95c53908 | 864 | fprintf(stderr, "IMAP error: unexpected command continuation request\n"); |
f2561fda MM |
865 | return RESP_BAD; |
866 | } | |
95c53908 | 867 | if (socket_write(&imap->buf.sock, "\r\n", 2) != 2) |
f2561fda MM |
868 | return RESP_BAD; |
869 | if (!cmdp->cb.cont) | |
870 | imap->literal_pending = 0; | |
871 | if (!tcmd) | |
872 | return DRV_OK; | |
873 | } else { | |
95c53908 | 874 | tag = atoi(arg); |
f2561fda MM |
875 | for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next) |
876 | if (cmdp->tag == tag) | |
877 | goto gottag; | |
95c53908 | 878 | fprintf(stderr, "IMAP error: unexpected tag %s\n", arg); |
f2561fda | 879 | return RESP_BAD; |
9f1ad541 | 880 | gottag: |
f2561fda MM |
881 | if (!(*pcmdp = cmdp->next)) |
882 | imap->in_progress_append = pcmdp; | |
883 | imap->num_in_progress--; | |
884 | if (cmdp->cb.cont || cmdp->cb.data) | |
885 | imap->literal_pending = 0; | |
95c53908 RS |
886 | arg = next_arg(&cmd); |
887 | if (!strcmp("OK", arg)) | |
f2561fda MM |
888 | resp = DRV_OK; |
889 | else { | |
95c53908 RS |
890 | if (!strcmp("NO", arg)) { |
891 | if (cmdp->cb.create && cmd && (cmdp->cb.trycreate || !memcmp(cmd, "[TRYCREATE]", 11))) { /* SELECT, APPEND or UID COPY */ | |
892 | p = strchr(cmdp->cmd, '"'); | |
28bea9e5 | 893 | if (!issue_imap_cmd(ctx, NULL, "CREATE \"%.*s\"", (int)(strchr(p + 1, '"') - p + 1), p)) { |
f2561fda MM |
894 | resp = RESP_BAD; |
895 | goto normal; | |
896 | } | |
897 | /* not waiting here violates the spec, but a server that does not | |
898 | grok this nonetheless violates it too. */ | |
899 | cmdp->cb.create = 0; | |
95c53908 | 900 | if (!(ncmdp = issue_imap_cmd(ctx, &cmdp->cb, "%s", cmdp->cmd))) { |
f2561fda MM |
901 | resp = RESP_BAD; |
902 | goto normal; | |
903 | } | |
95c53908 RS |
904 | free(cmdp->cmd); |
905 | free(cmdp); | |
f2561fda MM |
906 | if (!tcmd) |
907 | return 0; /* ignored */ | |
908 | if (cmdp == tcmd) | |
909 | tcmd = ncmdp; | |
910 | continue; | |
911 | } | |
912 | resp = RESP_NO; | |
95c53908 | 913 | } else /*if (!strcmp("BAD", arg))*/ |
f2561fda | 914 | resp = RESP_BAD; |
95c53908 RS |
915 | fprintf(stderr, "IMAP command '%s' returned response (%s) - %s\n", |
916 | memcmp(cmdp->cmd, "LOGIN", 5) ? | |
f2561fda MM |
917 | cmdp->cmd : "LOGIN <user> <pass>", |
918 | arg, cmd ? cmd : ""); | |
919 | } | |
95c53908 | 920 | if ((resp2 = parse_response_code(ctx, &cmdp->cb, cmd)) > resp) |
f2561fda | 921 | resp = resp2; |
9f1ad541 | 922 | normal: |
f2561fda | 923 | if (cmdp->cb.done) |
95c53908 RS |
924 | cmdp->cb.done(ctx, cmdp, resp); |
925 | free(cmdp->cb.data); | |
926 | free(cmdp->cmd); | |
927 | free(cmdp); | |
f2561fda MM |
928 | if (!tcmd || tcmd == cmdp) |
929 | return resp; | |
930 | } | |
931 | } | |
932 | /* not reached */ | |
933 | } | |
934 | ||
9f1ad541 | 935 | static void imap_close_server(struct imap_store *ictx) |
f2561fda | 936 | { |
9f1ad541 | 937 | struct imap *imap = ictx->imap; |
f2561fda | 938 | |
7a7796e9 | 939 | if (imap->buf.sock.fd[0] != -1) { |
95c53908 RS |
940 | imap_exec(ictx, NULL, "LOGOUT"); |
941 | socket_shutdown(&imap->buf.sock); | |
f2561fda | 942 | } |
95c53908 RS |
943 | free_list(imap->ns_personal); |
944 | free_list(imap->ns_other); | |
945 | free_list(imap->ns_shared); | |
946 | free(imap); | |
f2561fda MM |
947 | } |
948 | ||
9f1ad541 | 949 | static void imap_close_store(struct store *ctx) |
f2561fda | 950 | { |
9f1ad541 | 951 | imap_close_server((struct imap_store *)ctx); |
95c53908 RS |
952 | free_generic_messages(ctx->msgs); |
953 | free(ctx); | |
f2561fda MM |
954 | } |
955 | ||
ae9c606e HM |
956 | #ifndef NO_OPENSSL |
957 | ||
958 | /* | |
959 | * hexchar() and cram() functions are based on the code from the isync | |
960 | * project (http://isync.sf.net/). | |
961 | */ | |
962 | static char hexchar(unsigned int b) | |
963 | { | |
964 | return b < 10 ? '0' + b : 'a' + (b - 10); | |
965 | } | |
966 | ||
967 | #define ENCODED_SIZE(n) (4*((n+2)/3)) | |
968 | static char *cram(const char *challenge_64, const char *user, const char *pass) | |
969 | { | |
970 | int i, resp_len, encoded_len, decoded_len; | |
971 | HMAC_CTX hmac; | |
972 | unsigned char hash[16]; | |
973 | char hex[33]; | |
974 | char *response, *response_64, *challenge; | |
975 | ||
976 | /* | |
977 | * length of challenge_64 (i.e. base-64 encoded string) is a good | |
978 | * enough upper bound for challenge (decoded result). | |
979 | */ | |
980 | encoded_len = strlen(challenge_64); | |
981 | challenge = xmalloc(encoded_len); | |
982 | decoded_len = EVP_DecodeBlock((unsigned char *)challenge, | |
983 | (unsigned char *)challenge_64, encoded_len); | |
984 | if (decoded_len < 0) | |
985 | die("invalid challenge %s", challenge_64); | |
986 | HMAC_Init(&hmac, (unsigned char *)pass, strlen(pass), EVP_md5()); | |
987 | HMAC_Update(&hmac, (unsigned char *)challenge, decoded_len); | |
988 | HMAC_Final(&hmac, hash, NULL); | |
989 | HMAC_CTX_cleanup(&hmac); | |
990 | ||
991 | hex[32] = 0; | |
992 | for (i = 0; i < 16; i++) { | |
993 | hex[2 * i] = hexchar((hash[i] >> 4) & 0xf); | |
994 | hex[2 * i + 1] = hexchar(hash[i] & 0xf); | |
995 | } | |
996 | ||
997 | /* response: "<user> <digest in hex>" */ | |
998 | resp_len = strlen(user) + 1 + strlen(hex) + 1; | |
999 | response = xmalloc(resp_len); | |
1000 | sprintf(response, "%s %s", user, hex); | |
1001 | ||
1002 | response_64 = xmalloc(ENCODED_SIZE(resp_len) + 1); | |
1003 | encoded_len = EVP_EncodeBlock((unsigned char *)response_64, | |
1004 | (unsigned char *)response, resp_len); | |
1005 | if (encoded_len < 0) | |
1006 | die("EVP_EncodeBlock error"); | |
1007 | response_64[encoded_len] = '\0'; | |
1008 | return (char *)response_64; | |
1009 | } | |
1010 | ||
1011 | #else | |
1012 | ||
1013 | static char *cram(const char *challenge_64, const char *user, const char *pass) | |
1014 | { | |
1015 | die("If you want to use CRAM-MD5 authenticate method, " | |
1016 | "you have to build git-imap-send with OpenSSL library."); | |
1017 | } | |
1018 | ||
1019 | #endif | |
1020 | ||
1021 | static int auth_cram_md5(struct imap_store *ctx, struct imap_cmd *cmd, const char *prompt) | |
1022 | { | |
1023 | int ret; | |
1024 | char *response; | |
1025 | ||
1026 | response = cram(prompt, server.user, server.pass); | |
1027 | ||
1028 | ret = socket_write(&ctx->imap->buf.sock, response, strlen(response)); | |
1029 | if (ret != strlen(response)) | |
1030 | return error("IMAP error: sending response failed\n"); | |
1031 | ||
1032 | free(response); | |
1033 | ||
1034 | return 0; | |
1035 | } | |
1036 | ||
9f1ad541 | 1037 | static struct store *imap_open_store(struct imap_server_conf *srvc) |
f2561fda | 1038 | { |
9f1ad541 JH |
1039 | struct imap_store *ctx; |
1040 | struct imap *imap; | |
f2561fda | 1041 | char *arg, *rsp; |
c94d2dd0 | 1042 | int s = -1, preauth; |
f2561fda | 1043 | |
95c53908 | 1044 | ctx = xcalloc(sizeof(*ctx), 1); |
f2561fda | 1045 | |
95c53908 | 1046 | ctx->imap = imap = xcalloc(sizeof(*imap), 1); |
7a7796e9 | 1047 | imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1; |
f2561fda MM |
1048 | imap->in_progress_append = &imap->in_progress; |
1049 | ||
1050 | /* open connection to IMAP server */ | |
1051 | ||
1052 | if (srvc->tunnel) { | |
ac0ba18d | 1053 | const char *argv[] = { srvc->tunnel, NULL }; |
c2e86add | 1054 | struct child_process tunnel = {NULL}; |
f2561fda | 1055 | |
c94d2dd0 | 1056 | imap_info("Starting tunnel '%s'... ", srvc->tunnel); |
f2561fda | 1057 | |
c94d2dd0 | 1058 | tunnel.argv = argv; |
ac0ba18d | 1059 | tunnel.use_shell = 1; |
c94d2dd0 EFL |
1060 | tunnel.in = -1; |
1061 | tunnel.out = -1; | |
1062 | if (start_command(&tunnel)) | |
1063 | die("cannot start proxy %s", argv[0]); | |
f2561fda | 1064 | |
c94d2dd0 EFL |
1065 | imap->buf.sock.fd[0] = tunnel.out; |
1066 | imap->buf.sock.fd[1] = tunnel.in; | |
f2561fda | 1067 | |
95c53908 | 1068 | imap_info("ok\n"); |
f2561fda | 1069 | } else { |
94ad2437 BK |
1070 | #ifndef NO_IPV6 |
1071 | struct addrinfo hints, *ai0, *ai; | |
1072 | int gai; | |
1073 | char portstr[6]; | |
1074 | ||
1702b138 | 1075 | snprintf(portstr, sizeof(portstr), "%d", srvc->port); |
94ad2437 BK |
1076 | |
1077 | memset(&hints, 0, sizeof(hints)); | |
1078 | hints.ai_socktype = SOCK_STREAM; | |
1079 | hints.ai_protocol = IPPROTO_TCP; | |
1080 | ||
1081 | imap_info("Resolving %s... ", srvc->host); | |
1082 | gai = getaddrinfo(srvc->host, portstr, &hints, &ai); | |
1083 | if (gai) { | |
1084 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai)); | |
1085 | goto bail; | |
1086 | } | |
1087 | imap_info("ok\n"); | |
1088 | ||
1089 | for (ai0 = ai; ai; ai = ai->ai_next) { | |
1090 | char addr[NI_MAXHOST]; | |
1091 | ||
1092 | s = socket(ai->ai_family, ai->ai_socktype, | |
1093 | ai->ai_protocol); | |
1094 | if (s < 0) | |
1095 | continue; | |
1096 | ||
1097 | getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, | |
1098 | sizeof(addr), NULL, 0, NI_NUMERICHOST); | |
1099 | imap_info("Connecting to [%s]:%s... ", addr, portstr); | |
1100 | ||
1101 | if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { | |
1102 | close(s); | |
1103 | s = -1; | |
1104 | perror("connect"); | |
1105 | continue; | |
1106 | } | |
1107 | ||
1108 | break; | |
1109 | } | |
1110 | freeaddrinfo(ai0); | |
1111 | #else /* NO_IPV6 */ | |
1112 | struct hostent *he; | |
1113 | struct sockaddr_in addr; | |
1114 | ||
95c53908 RS |
1115 | memset(&addr, 0, sizeof(addr)); |
1116 | addr.sin_port = htons(srvc->port); | |
f2561fda MM |
1117 | addr.sin_family = AF_INET; |
1118 | ||
95c53908 RS |
1119 | imap_info("Resolving %s... ", srvc->host); |
1120 | he = gethostbyname(srvc->host); | |
f2561fda | 1121 | if (!he) { |
95c53908 | 1122 | perror("gethostbyname"); |
f2561fda MM |
1123 | goto bail; |
1124 | } | |
95c53908 | 1125 | imap_info("ok\n"); |
f2561fda MM |
1126 | |
1127 | addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]); | |
1128 | ||
95c53908 | 1129 | s = socket(PF_INET, SOCK_STREAM, 0); |
f2561fda | 1130 | |
95c53908 RS |
1131 | imap_info("Connecting to %s:%hu... ", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); |
1132 | if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) { | |
1133 | close(s); | |
94ad2437 | 1134 | s = -1; |
95c53908 | 1135 | perror("connect"); |
94ad2437 BK |
1136 | } |
1137 | #endif | |
1138 | if (s < 0) { | |
1139 | fputs("Error: unable to connect to server.\n", stderr); | |
f2561fda MM |
1140 | goto bail; |
1141 | } | |
f2561fda | 1142 | |
7a7796e9 EFL |
1143 | imap->buf.sock.fd[0] = s; |
1144 | imap->buf.sock.fd[1] = dup(s); | |
f2561fda | 1145 | |
684ec6c6 RS |
1146 | if (srvc->use_ssl && |
1147 | ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) { | |
1148 | close(s); | |
1149 | goto bail; | |
1150 | } | |
95c53908 | 1151 | imap_info("ok\n"); |
f2561fda MM |
1152 | } |
1153 | ||
1154 | /* read the greeting string */ | |
95c53908 RS |
1155 | if (buffer_gets(&imap->buf, &rsp)) { |
1156 | fprintf(stderr, "IMAP error: no greeting response\n"); | |
f2561fda MM |
1157 | goto bail; |
1158 | } | |
95c53908 RS |
1159 | arg = next_arg(&rsp); |
1160 | if (!arg || *arg != '*' || (arg = next_arg(&rsp)) == NULL) { | |
1161 | fprintf(stderr, "IMAP error: invalid greeting response\n"); | |
f2561fda MM |
1162 | goto bail; |
1163 | } | |
1164 | preauth = 0; | |
95c53908 | 1165 | if (!strcmp("PREAUTH", arg)) |
f2561fda | 1166 | preauth = 1; |
95c53908 RS |
1167 | else if (strcmp("OK", arg) != 0) { |
1168 | fprintf(stderr, "IMAP error: unknown greeting response\n"); | |
f2561fda MM |
1169 | goto bail; |
1170 | } | |
95c53908 RS |
1171 | parse_response_code(ctx, NULL, rsp); |
1172 | if (!imap->caps && imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK) | |
f2561fda MM |
1173 | goto bail; |
1174 | ||
1175 | if (!preauth) { | |
684ec6c6 RS |
1176 | #ifndef NO_OPENSSL |
1177 | if (!srvc->use_ssl && CAP(STARTTLS)) { | |
d27da38a | 1178 | if (imap_exec(ctx, NULL, "STARTTLS") != RESP_OK) |
684ec6c6 RS |
1179 | goto bail; |
1180 | if (ssl_socket_connect(&imap->buf.sock, 1, | |
1181 | srvc->ssl_verify)) | |
1182 | goto bail; | |
1183 | /* capabilities may have changed, so get the new capabilities */ | |
d27da38a | 1184 | if (imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK) |
684ec6c6 RS |
1185 | goto bail; |
1186 | } | |
1187 | #endif | |
95c53908 | 1188 | imap_info("Logging in...\n"); |
f2561fda | 1189 | if (!srvc->user) { |
95c53908 | 1190 | fprintf(stderr, "Skipping server %s, no user\n", srvc->host); |
f2561fda MM |
1191 | goto bail; |
1192 | } | |
1193 | if (!srvc->pass) { | |
1194 | char prompt[80]; | |
95c53908 | 1195 | sprintf(prompt, "Password (%s@%s): ", srvc->user, srvc->host); |
f206063b | 1196 | arg = git_getpass(prompt); |
f2561fda | 1197 | if (!arg) { |
95c53908 RS |
1198 | perror("getpass"); |
1199 | exit(1); | |
f2561fda MM |
1200 | } |
1201 | if (!*arg) { | |
95c53908 | 1202 | fprintf(stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host); |
f2561fda MM |
1203 | goto bail; |
1204 | } | |
1205 | /* | |
1206 | * getpass() returns a pointer to a static buffer. make a copy | |
1207 | * for long term storage. | |
1208 | */ | |
95c53908 | 1209 | srvc->pass = xstrdup(arg); |
f2561fda MM |
1210 | } |
1211 | if (CAP(NOLOGIN)) { | |
95c53908 | 1212 | fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host); |
f2561fda MM |
1213 | goto bail; |
1214 | } | |
ae9c606e HM |
1215 | |
1216 | if (srvc->auth_method) { | |
1217 | struct imap_cmd_cb cb; | |
1218 | ||
1219 | if (!strcmp(srvc->auth_method, "CRAM-MD5")) { | |
1220 | if (!CAP(AUTH_CRAM_MD5)) { | |
1221 | fprintf(stderr, "You specified" | |
1222 | "CRAM-MD5 as authentication method, " | |
1223 | "but %s doesn't support it.\n", srvc->host); | |
1224 | goto bail; | |
1225 | } | |
1226 | /* CRAM-MD5 */ | |
1227 | ||
1228 | memset(&cb, 0, sizeof(cb)); | |
1229 | cb.cont = auth_cram_md5; | |
1230 | if (imap_exec(ctx, &cb, "AUTHENTICATE CRAM-MD5") != RESP_OK) { | |
1231 | fprintf(stderr, "IMAP error: AUTHENTICATE CRAM-MD5 failed\n"); | |
1232 | goto bail; | |
1233 | } | |
1234 | } else { | |
1235 | fprintf(stderr, "Unknown authentication method:%s\n", srvc->host); | |
1236 | goto bail; | |
1237 | } | |
1238 | } else { | |
10439d89 CW |
1239 | if (!imap->buf.sock.ssl) |
1240 | imap_warn("*** IMAP Warning *** Password is being " | |
1241 | "sent in the clear\n"); | |
ae9c606e HM |
1242 | if (imap_exec(ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass) != RESP_OK) { |
1243 | fprintf(stderr, "IMAP error: LOGIN failed\n"); | |
1244 | goto bail; | |
1245 | } | |
f2561fda MM |
1246 | } |
1247 | } /* !preauth */ | |
1248 | ||
1249 | ctx->prefix = ""; | |
1250 | ctx->trashnc = 1; | |
9f1ad541 | 1251 | return (struct store *)ctx; |
f2561fda | 1252 | |
9f1ad541 | 1253 | bail: |
95c53908 | 1254 | imap_close_store(&ctx->gen); |
5142db69 | 1255 | return NULL; |
f2561fda MM |
1256 | } |
1257 | ||
95c53908 | 1258 | static int imap_make_flags(int flags, char *buf) |
f2561fda MM |
1259 | { |
1260 | const char *s; | |
1261 | unsigned i, d; | |
1262 | ||
1263 | for (i = d = 0; i < ARRAY_SIZE(Flags); i++) | |
1264 | if (flags & (1 << i)) { | |
1265 | buf[d++] = ' '; | |
1266 | buf[d++] = '\\'; | |
1267 | for (s = Flags[i]; *s; s++) | |
1268 | buf[d++] = *s; | |
1269 | } | |
1270 | buf[0] = '('; | |
1271 | buf[d++] = ')'; | |
1272 | return d; | |
1273 | } | |
1274 | ||
67d17630 HM |
1275 | static void lf_to_crlf(struct msg_data *msg) |
1276 | { | |
1277 | char *new; | |
1278 | int i, j, lfnum = 0; | |
1279 | ||
1280 | if (msg->data[0] == '\n') | |
1281 | lfnum++; | |
1282 | for (i = 1; i < msg->len; i++) { | |
1283 | if (msg->data[i - 1] != '\r' && msg->data[i] == '\n') | |
1284 | lfnum++; | |
1285 | } | |
1286 | ||
1287 | new = xmalloc(msg->len + lfnum); | |
1288 | if (msg->data[0] == '\n') { | |
1289 | new[0] = '\r'; | |
1290 | new[1] = '\n'; | |
1291 | i = 1; | |
1292 | j = 2; | |
1293 | } else { | |
1294 | new[0] = msg->data[0]; | |
1295 | i = 1; | |
1296 | j = 1; | |
1297 | } | |
1298 | for ( ; i < msg->len; i++) { | |
1299 | if (msg->data[i] != '\n') { | |
1300 | new[j++] = msg->data[i]; | |
1301 | continue; | |
1302 | } | |
1303 | if (msg->data[i - 1] != '\r') | |
1304 | new[j++] = '\r'; | |
1305 | /* otherwise it already had CR before */ | |
1306 | new[j++] = '\n'; | |
1307 | } | |
1308 | msg->len += lfnum; | |
1309 | free(msg->data); | |
1310 | msg->data = new; | |
1311 | } | |
1312 | ||
3a7cba95 | 1313 | static int imap_store_msg(struct store *gctx, struct msg_data *data) |
f2561fda | 1314 | { |
9f1ad541 JH |
1315 | struct imap_store *ctx = (struct imap_store *)gctx; |
1316 | struct imap *imap = ctx->imap; | |
f2561fda | 1317 | struct imap_cmd_cb cb; |
f2561fda | 1318 | const char *prefix, *box; |
3a7cba95 JK |
1319 | int ret, d; |
1320 | char flagstr[128]; | |
f2561fda | 1321 | |
67d17630 | 1322 | lf_to_crlf(data); |
95c53908 | 1323 | memset(&cb, 0, sizeof(cb)); |
f2561fda | 1324 | |
3a7cba95 JK |
1325 | cb.dlen = data->len; |
1326 | cb.data = xmalloc(cb.dlen); | |
1327 | memcpy(cb.data, data->data, data->len); | |
f2561fda MM |
1328 | |
1329 | d = 0; | |
1330 | if (data->flags) { | |
95c53908 | 1331 | d = imap_make_flags(data->flags, flagstr); |
f2561fda MM |
1332 | flagstr[d++] = ' '; |
1333 | } | |
1334 | flagstr[d] = 0; | |
1335 | ||
3a7cba95 JK |
1336 | box = gctx->name; |
1337 | prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix; | |
1338 | cb.create = 0; | |
95c53908 | 1339 | ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr); |
f2561fda MM |
1340 | imap->caps = imap->rcaps; |
1341 | if (ret != DRV_OK) | |
1342 | return ret; | |
3a7cba95 | 1343 | gctx->count++; |
f2561fda MM |
1344 | |
1345 | return DRV_OK; | |
1346 | } | |
1347 | ||
c64d84f1 JW |
1348 | static void encode_html_chars(struct strbuf *p) |
1349 | { | |
1350 | int i; | |
1351 | for (i = 0; i < p->len; i++) { | |
1352 | if (p->buf[i] == '&') | |
1353 | strbuf_splice(p, i, 1, "&", 5); | |
1354 | if (p->buf[i] == '<') | |
1355 | strbuf_splice(p, i, 1, "<", 4); | |
1356 | if (p->buf[i] == '>') | |
1357 | strbuf_splice(p, i, 1, ">", 4); | |
1358 | if (p->buf[i] == '"') | |
1359 | strbuf_splice(p, i, 1, """, 6); | |
1360 | } | |
1361 | } | |
1362 | static void wrap_in_html(struct msg_data *msg) | |
1363 | { | |
1364 | struct strbuf buf = STRBUF_INIT; | |
1365 | struct strbuf **lines; | |
1366 | struct strbuf **p; | |
1367 | static char *content_type = "Content-Type: text/html;\n"; | |
1368 | static char *pre_open = "<pre>\n"; | |
1369 | static char *pre_close = "</pre>\n"; | |
1370 | int added_header = 0; | |
1371 | ||
1372 | strbuf_attach(&buf, msg->data, msg->len, msg->len); | |
1373 | lines = strbuf_split(&buf, '\n'); | |
1374 | strbuf_release(&buf); | |
1375 | for (p = lines; *p; p++) { | |
1376 | if (! added_header) { | |
1377 | if ((*p)->len == 1 && *((*p)->buf) == '\n') { | |
1378 | strbuf_addstr(&buf, content_type); | |
1379 | strbuf_addbuf(&buf, *p); | |
1380 | strbuf_addstr(&buf, pre_open); | |
1381 | added_header = 1; | |
1382 | continue; | |
1383 | } | |
1384 | } | |
1385 | else | |
1386 | encode_html_chars(*p); | |
1387 | strbuf_addbuf(&buf, *p); | |
1388 | } | |
1389 | strbuf_addstr(&buf, pre_close); | |
1390 | strbuf_list_free(lines); | |
1391 | msg->len = buf.len; | |
1392 | msg->data = strbuf_detach(&buf, NULL); | |
1393 | } | |
1394 | ||
f2561fda MM |
1395 | #define CHUNKSIZE 0x1000 |
1396 | ||
9f1ad541 | 1397 | static int read_message(FILE *f, struct msg_data *msg) |
f2561fda | 1398 | { |
f285a2d7 | 1399 | struct strbuf buf = STRBUF_INIT; |
f2561fda | 1400 | |
635d043f | 1401 | memset(msg, 0, sizeof(*msg)); |
635d043f PH |
1402 | |
1403 | do { | |
1404 | if (strbuf_fread(&buf, CHUNKSIZE, f) <= 0) | |
f2561fda | 1405 | break; |
635d043f PH |
1406 | } while (!feof(f)); |
1407 | ||
1408 | msg->len = buf.len; | |
b315c5c0 | 1409 | msg->data = strbuf_detach(&buf, NULL); |
f2561fda MM |
1410 | return msg->len; |
1411 | } | |
1412 | ||
9f1ad541 | 1413 | static int count_messages(struct msg_data *msg) |
f2561fda MM |
1414 | { |
1415 | int count = 0; | |
1416 | char *p = msg->data; | |
1417 | ||
1418 | while (1) { | |
1968d77d | 1419 | if (!prefixcmp(p, "From ")) { |
4916c8f9 RR |
1420 | p = strstr(p+5, "\nFrom: "); |
1421 | if (!p) break; | |
1422 | p = strstr(p+7, "\nDate: "); | |
1423 | if (!p) break; | |
1424 | p = strstr(p+7, "\nSubject: "); | |
1425 | if (!p) break; | |
1426 | p += 10; | |
f2561fda | 1427 | count++; |
f2561fda | 1428 | } |
95c53908 | 1429 | p = strstr(p+5, "\nFrom "); |
f2561fda MM |
1430 | if (!p) |
1431 | break; | |
1432 | p++; | |
1433 | } | |
1434 | return count; | |
1435 | } | |
1436 | ||
9f1ad541 | 1437 | static int split_msg(struct msg_data *all_msgs, struct msg_data *msg, int *ofs) |
f2561fda MM |
1438 | { |
1439 | char *p, *data; | |
1440 | ||
95c53908 | 1441 | memset(msg, 0, sizeof *msg); |
f2561fda MM |
1442 | if (*ofs >= all_msgs->len) |
1443 | return 0; | |
1444 | ||
95c53908 | 1445 | data = &all_msgs->data[*ofs]; |
f2561fda MM |
1446 | msg->len = all_msgs->len - *ofs; |
1447 | ||
1968d77d | 1448 | if (msg->len < 5 || prefixcmp(data, "From ")) |
f2561fda MM |
1449 | return 0; |
1450 | ||
95c53908 | 1451 | p = strchr(data, '\n'); |
e0b08307 MA |
1452 | if (p) { |
1453 | p = &p[1]; | |
1454 | msg->len -= p-data; | |
1455 | *ofs += p-data; | |
1456 | data = p; | |
1457 | } | |
1458 | ||
95c53908 | 1459 | p = strstr(data, "\nFrom "); |
f2561fda MM |
1460 | if (p) |
1461 | msg->len = &p[1] - data; | |
1462 | ||
182af834 | 1463 | msg->data = xmemdupz(data, msg->len); |
f2561fda | 1464 | *ofs += msg->len; |
a6080a0a | 1465 | return 1; |
f2561fda MM |
1466 | } |
1467 | ||
f2561fda MM |
1468 | static char *imap_folder; |
1469 | ||
95c53908 | 1470 | static int git_imap_config(const char *key, const char *val, void *cb) |
f2561fda MM |
1471 | { |
1472 | char imap_key[] = "imap."; | |
1473 | ||
95c53908 | 1474 | if (strncmp(key, imap_key, sizeof imap_key - 1)) |
f2561fda | 1475 | return 0; |
3c17c34a | 1476 | |
f2561fda MM |
1477 | key += sizeof imap_key - 1; |
1478 | ||
ace706e2 JH |
1479 | /* check booleans first, and barf on others */ |
1480 | if (!strcmp("sslverify", key)) | |
1481 | server.ssl_verify = git_config_bool(key, val); | |
1482 | else if (!strcmp("preformattedhtml", key)) | |
1483 | server.use_html = git_config_bool(key, val); | |
1484 | else if (!val) | |
1485 | return config_error_nonbool(key); | |
1486 | ||
95c53908 RS |
1487 | if (!strcmp("folder", key)) { |
1488 | imap_folder = xstrdup(val); | |
1489 | } else if (!strcmp("host", key)) { | |
684ec6c6 RS |
1490 | if (!prefixcmp(val, "imap:")) |
1491 | val += 5; | |
1492 | else if (!prefixcmp(val, "imaps:")) { | |
1493 | val += 6; | |
1494 | server.use_ssl = 1; | |
f2561fda | 1495 | } |
1968d77d | 1496 | if (!prefixcmp(val, "//")) |
f2561fda | 1497 | val += 2; |
95c53908 | 1498 | server.host = xstrdup(val); |
9f1ad541 | 1499 | } else if (!strcmp("user", key)) |
95c53908 RS |
1500 | server.user = xstrdup(val); |
1501 | else if (!strcmp("pass", key)) | |
1502 | server.pass = xstrdup(val); | |
1503 | else if (!strcmp("port", key)) | |
1504 | server.port = git_config_int(key, val); | |
1505 | else if (!strcmp("tunnel", key)) | |
1506 | server.tunnel = xstrdup(val); | |
ae9c606e HM |
1507 | else if (!strcmp("authmethod", key)) |
1508 | server.auth_method = xstrdup(val); | |
1509 | ||
f2561fda MM |
1510 | return 0; |
1511 | } | |
1512 | ||
95c53908 | 1513 | int main(int argc, char **argv) |
f2561fda | 1514 | { |
9f1ad541 JH |
1515 | struct msg_data all_msgs, msg; |
1516 | struct store *ctx = NULL; | |
f2561fda MM |
1517 | int ofs = 0; |
1518 | int r; | |
1519 | int total, n = 0; | |
a0406b94 | 1520 | int nongit_ok; |
f2561fda | 1521 | |
2fb3f6db SP |
1522 | git_extract_argv0_path(argv[0]); |
1523 | ||
9a2861e3 JN |
1524 | if (argc != 1) |
1525 | usage(imap_send_usage); | |
1526 | ||
a0406b94 | 1527 | setup_git_directory_gently(&nongit_ok); |
ef90d6d4 | 1528 | git_config(git_imap_config, NULL); |
f2561fda | 1529 | |
684ec6c6 RS |
1530 | if (!server.port) |
1531 | server.port = server.use_ssl ? 993 : 143; | |
1532 | ||
f2561fda | 1533 | if (!imap_folder) { |
95c53908 | 1534 | fprintf(stderr, "no imap store specified\n"); |
f2561fda MM |
1535 | return 1; |
1536 | } | |
5b67b8e2 | 1537 | if (!server.host) { |
34b5cd1f | 1538 | if (!server.tunnel) { |
95c53908 | 1539 | fprintf(stderr, "no imap host specified\n"); |
34b5cd1f JK |
1540 | return 1; |
1541 | } | |
1542 | server.host = "tunnel"; | |
5b67b8e2 | 1543 | } |
f2561fda MM |
1544 | |
1545 | /* read the messages */ | |
95c53908 | 1546 | if (!read_message(stdin, &all_msgs)) { |
9f1ad541 | 1547 | fprintf(stderr, "nothing to send\n"); |
f2561fda MM |
1548 | return 1; |
1549 | } | |
1550 | ||
95c53908 | 1551 | total = count_messages(&all_msgs); |
1cd88cc9 | 1552 | if (!total) { |
9f1ad541 | 1553 | fprintf(stderr, "no messages to send\n"); |
1cd88cc9 MM |
1554 | return 1; |
1555 | } | |
1556 | ||
f2561fda | 1557 | /* write it to the imap server */ |
95c53908 | 1558 | ctx = imap_open_store(&server); |
f2561fda | 1559 | if (!ctx) { |
9f1ad541 | 1560 | fprintf(stderr, "failed to open store\n"); |
f2561fda MM |
1561 | return 1; |
1562 | } | |
1563 | ||
9f1ad541 | 1564 | fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : ""); |
f2561fda MM |
1565 | ctx->name = imap_folder; |
1566 | while (1) { | |
1567 | unsigned percent = n * 100 / total; | |
95c53908 RS |
1568 | fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total); |
1569 | if (!split_msg(&all_msgs, &msg, &ofs)) | |
f2561fda | 1570 | break; |
c64d84f1 JW |
1571 | if (server.use_html) |
1572 | wrap_in_html(&msg); | |
3a7cba95 | 1573 | r = imap_store_msg(ctx, &msg); |
9f1ad541 JH |
1574 | if (r != DRV_OK) |
1575 | break; | |
f2561fda MM |
1576 | n++; |
1577 | } | |
95c53908 | 1578 | fprintf(stderr, "\n"); |
f2561fda | 1579 | |
95c53908 | 1580 | imap_close_store(ctx); |
f2561fda MM |
1581 | |
1582 | return 0; | |
1583 | } |