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