1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/* $Id$ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "fdm.h"
#include "deliver.h"
#include "fetch.h"
/*
* This file is a bit of a mishmash, so that we can use some bits from
* the IMAP fetching code.
*
* All needs to be straightened out sometime.
*/
int deliver_imap_deliver(struct deliver_ctx *, struct actitem *);
void deliver_imap_desc(struct actitem *, char *, size_t);
int deliver_imap_poll(struct account *, struct io *);
int deliver_imap_pollto(int (*)(struct account *, struct fetch_ctx *),
struct account *, struct io *, struct fetch_ctx *);
int deliver_imap_waitokay(struct account *, struct fetch_ctx *,
struct io *, char **);
int deliver_imap_waitcontinue(struct account *, struct fetch_ctx *,
struct io *, char **);
int deliver_imap_waitappend(struct account *, struct fetch_ctx *,
struct io *, char **);
struct deliver deliver_imap = {
"imap",
DELIVER_ASUSER,
deliver_imap_deliver,
deliver_imap_desc
};
/* Poll for data from/to server. */
int
deliver_imap_poll(struct account *a, struct io *io)
{
char *cause;
switch (io_poll(io, conf.timeout, &cause)) {
case 0:
log_warnx("%s: connection unexpectedly closed", a->name);
return (1);
case -1:
log_warnx("%s: %s", a->name, cause);
xfree(cause);
return (1);
}
return (0);
}
/* Poll through the IMAP fetch states until a particular one is reached. */
int
deliver_imap_pollto(int (*state)(struct account *, struct fetch_ctx *),
struct account *a, struct io *io, struct fetch_ctx *fctx)
{
while (state == NULL || fctx->state != state) {
switch (fctx->state(a, fctx)) {
case FETCH_AGAIN:
continue;
case FETCH_ERROR:
return (1);
case FETCH_EXIT:
return (0);
}
if (deliver_imap_poll(a, io) != 0)
return (1);
}
return (0);
}
/* Wait for okay. */
int
deliver_imap_waitokay(struct account *a, struct fetch_ctx *fctx, struct io *io,
char **line)
{
do {
if (deliver_imap_poll(a, io) != 0)
return (1);
if (imap_getln(a, fctx, IMAP_TAGGED, line) != 0)
return (1);
} while (*line == NULL);
if (!imap_okay(*line)) {
imap_bad(a, *line);
return (1);
}
return (0);
}
/* Wait for continuation. */
int
deliver_imap_waitcontinue(struct account *a, struct fetch_ctx *fctx,
struct io *io, char **line)
{
do {
if (deliver_imap_poll(a, io) != 0)
return (1);
if (imap_getln(a, fctx, IMAP_CONTINUE, line) != 0)
return (1);
} while (*line == NULL);
return (0);
}
/* Wait for append response. */
int
deliver_imap_waitappend(struct account *a, struct fetch_ctx *fctx,
struct io *io, char **line)
{
struct fetch_imap_data *data = a->data;
int tag;
for (;;) {
if (deliver_imap_poll(a, io) != 0) {
line = NULL;
return (IMAP_TAG_ERROR);
}
if (data->getln(a, fctx, line) != 0) {
line = NULL;
return (IMAP_TAG_ERROR);
}
if (*line == NULL)
continue;
tag = imap_tag(*line);
if (tag != IMAP_TAG_NONE)
break;
}
if (tag == IMAP_TAG_CONTINUE)
return (IMAP_TAG_CONTINUE);
if (tag != data->tag)
return (IMAP_TAG_ERROR);
return (tag);
}
int
deliver_imap_deliver(struct deliver_ctx *dctx, struct actitem *ti)
{
struct account *a = dctx->account;
struct mail *m = dctx->mail;
struct deliver_imap_data *data = ti->data;
struct io *io;
struct fetch_ctx fctx;
struct fetch_imap_data fdata;
char *cause, *folder, *ptr, *line;
size_t len, maillen;
u_int total, body;
/* Connect to the IMAP server. */
io = connectproxy(&data->server,
conf.verify_certs, conf.proxy, IO_CRLF, conf.timeout, &cause);
if (io == NULL) {
log_warnx("%s: %s", a->name, cause);
xfree(cause);
return (DELIVER_FAILURE);
}
if (conf.debug > 3 && !conf.syslog)
io->dup_fd = STDOUT_FILENO;
/* Work out the folder name. */
folder = replacestr(&data->folder, m->tags, m, &m->rml);
if (folder == NULL || *folder == '\0') {
log_warnx("%s: empty folder", a->name);
goto error;
}
/* Fake up the fetch context for the fetch code. */
memset(&fdata, 0, sizeof fdata);
fdata.user = data->user;
fdata.pass = data->pass;
fdata.nocrammd5 = data->nocrammd5;
fdata.nologin = data->nologin;
fdata.noplain = data->noplain;
fdata.oauthbearer = data->oauthbearer;
fdata.xoauth2 = data->xoauth2;
memcpy(&fdata.server, &data->server, sizeof fdata.server);
fdata.io = io;
fdata.only = FETCH_ONLY_ALL;
a->data = &fdata;
fetch_imap_state_init(a, &fctx);
fctx.state = imap_state_connected;
fctx.llen = IO_LINESIZE;
fctx.lbuf = xmalloc(fctx.llen);
/* Use the fetch code until the select1 state is reached. */
if (deliver_imap_pollto(imap_state_select1, a, io, &fctx) != 0)
goto error;
retry:
/*
* Determine the mail size, not forgetting lines are CRLF
* terminated. The Google IMAP server is written strangely, so send
* the size as if every CRLF was a CR if the server has XYZZY.
*/
count_lines(m, &total, &body);
maillen = m->size + total - 2;
if (fdata.capa & IMAP_CAPA_XYZZY) {
log_debug2("%s: adjusting size: actual %zu", a->name, maillen);
maillen = m->size;
}
/* Send an append command. */
if (imap_not_clean(folder)) {
if (imap_putln(a, "%u APPEND {%zu}", ++fdata.tag,
strlen(folder)) != 0)
goto error;
} else {
if (imap_putln(a, "%u APPEND \"%s\" {%zu}", ++fdata.tag, folder,
maillen) != 0)
goto error;
goto data;
}
switch (deliver_imap_waitappend(a, &fctx, io, &line)) {
case IMAP_TAG_ERROR:
if (line != NULL)
imap_invalid(a, line);
goto error;
case IMAP_TAG_CONTINUE:
break;
default:
if (imap_no(line) && strstr(line, "[TRYCREATE]") != NULL)
goto try_create;
imap_invalid(a, line);
goto error;
}
if (fdata.capa & IMAP_CAPA_NOSPACE) {
if (imap_putln(a, "%s{%zu}", folder, maillen) != 0)
goto error;
} else {
if (imap_putln(a, "%s {%zu}", folder, maillen) != 0)
goto error;
}
data:
switch (deliver_imap_waitappend(a, &fctx, io, &line)) {
case IMAP_TAG_ERROR:
if (line != NULL)
imap_invalid(a, line);
goto error;
case IMAP_TAG_CONTINUE:
break;
default:
if (imap_no(line) && strstr(line, "[TRYCREATE]") != NULL)
goto try_create;
imap_invalid(a, line);
goto error;
}
/* Send the mail data. */
line_init(m, &ptr, &len);
while (ptr != NULL) {
if (len > 1)
io_write(io, ptr, len - 1);
io_writeline(io, NULL);
/* Update if necessary. */
if (io_update(io, conf.timeout, &cause) != 1) {
log_warnx("%s: %s", a->name, cause);
xfree(cause);
goto error;
}
line_next(m, &ptr, &len);
}
/* Wait for an okay from the server. */
switch (deliver_imap_waitappend(a, &fctx, io, &line)) {
case IMAP_TAG_ERROR:
case IMAP_TAG_CONTINUE:
if (line != NULL)
imap_invalid(a, line);
goto error;
default:
if (imap_okay(line))
break;
if (strstr(line, "[TRYCREATE]") != NULL)
goto try_create;
imap_invalid(a, line);
goto error;
}
if (imap_putln(a, "%u LOGOUT", ++fdata.tag) != 0)
goto error;
if (deliver_imap_waitokay(a, &fctx, io, &line) != 0)
goto error;
xfree(fctx.lbuf);
xfree(folder);
fdata.disconnect(a);
return (DELIVER_SUCCESS);
try_create:
/* Try to create the folder. */
if (imap_putln(a, "%u CREATE {%zu}", ++fdata.tag, strlen(folder)) != 0)
goto error;
if (deliver_imap_waitcontinue(a, &fctx, io, &line) != 0)
goto error;
if (imap_putln(a, "%s", folder) != 0)
goto error;
if (deliver_imap_waitokay(a, &fctx, io, &line) != 0)
goto error;
goto retry;
error:
io_writeline(io, "QUIT");
io_flush(io, conf.timeout, NULL);
xfree(fctx.lbuf);
if (folder != NULL)
xfree(folder);
fdata.disconnect(a);
return (DELIVER_FAILURE);
}
void
deliver_imap_desc(struct actitem *ti, char *buf, size_t len)
{
struct deliver_imap_data *data = ti->data;
xsnprintf(buf, len, "imap%s server \"%s\" port %s folder \"%s\"",
data->server.ssl ? "s" : "", data->server.host, data->server.port,
data->folder.str);
}
|