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