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