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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "imapfilter.h"
#include "session.h"
#include "buffer.h"
extern options opts;
buffer nbuf; /* Namespace buffer. */
int create_mailbox(session * ssn, const char *mbox);
const char *apply_namespace(const char *mbox, char *prefix, char delim);
/*
* Connect to the server, login to the IMAP server, get it's capabilities, get
* the namespace of the mailboxes.
*/
int
request_login(const char *server, const char *user, const char *pass,
unsigned int port, const char *ssl)
{
int r, rg;
session *s;
if ((s = session_find(server, user)))
return RESPONSE_OK;
s = session_new();
s->server = xstrdup(server);
s->username = xstrdup(user);
if (ssl && strncasecmp(ssl, "tls1", 4) &&
strncasecmp(ssl, "ssl3", 4) && strncasecmp(ssl, "ssl2", 4))
ssl = NULL;
if (!port) {
if (!ssl)
port = 143;
else
port = 993;
}
if (open_connection(s, server, port, ssl) == -1)
goto fail;
if ((rg = response_greeting(s)) == -1)
goto fail;
if (opts.debug > 0)
if (response_generic(s, imap_noop(s)) == -1)
goto fail;
if (response_capability(s, imap_capability(s)) == -1)
goto fail;
#ifndef NO_SSLTLS
if (!ssl && s->capabilities & CAPABILITY_STARTTLS &&
get_option_boolean("starttls"))
switch (response_generic(s, imap_starttls(s))) {
case RESPONSE_OK:
if (open_secure_connection(s, "tls1") == -1)
goto fail;
if (response_capability(s, imap_capability(s)) == -1)
goto fail;
break;
case -1:
goto fail;
break;
}
#endif
r = RESPONSE_NONE;
if (rg != RESPONSE_PREAUTH) {
#ifndef NO_CRAMMD5
if (s->capabilities & CAPABILITY_CRAMMD5 &&
get_option_boolean("crammd5")) {
if ((r = auth_cram_md5(s, user, pass)) == -1)
goto fail;
}
#endif
if (r != RESPONSE_OK && (r = response_generic(s, imap_login(s, user,
pass))) == -1)
goto fail;
if (r == RESPONSE_NO) {
error("username %s or password rejected at %s\n",
user, server);
goto fail;
}
}
if (s->capabilities & CAPABILITY_NAMESPACE &&
get_option_boolean("namespace")) {
if (response_namespace(s, imap_namespace(s)) == -1)
goto fail;
}
return r;
fail:
close_connection(s);
session_destroy(s);
return -1;
}
/*
* Logout from the IMAP server and disconnect from the server.
*/
int
request_logout(const char *server, const char *user)
{
int r;
session *s;
if (!(s = session_find(server, user)))
return -1;
r = response_generic(s, imap_logout(s));
close_connection(s);
session_destroy(s);
return r;
}
/*
* Get mailbox's status.
*/
int
request_status(const char *server, const char *user, const char *mbox,
unsigned int *exists, unsigned int *recent, unsigned int *unseen)
{
int r;
session *s;
if (!(s = session_find(server, user)))
return -1;
if (s->protocol == PROTOCOL_IMAP4REV1) {
if ((r = response_status(s, imap_status(s,
apply_namespace(mbox, s->ns.prefix, s->ns.delim),
"MESSAGES RECENT UNSEEN"), exists, recent, unseen)) == -1)
goto fail;
} else {
if ((r = response_examine(s, imap_examine(s,
apply_namespace(mbox, s->ns.prefix, s->ns.delim)), exists,
recent, unseen)) == -1)
goto fail;
}
return r;
fail:
close_connection(s);
session_destroy(s);
return -1;
}
/*
* Open mailbox in read-write mode.
*/
int
request_select(const char *server, const char *user, const char *mbox)
{
int r;
session *s;
if (!(s = session_find(server, user)))
return -1;
if ((r = response_select(s, imap_select(s,
apply_namespace(mbox, s->ns.prefix, s->ns.delim)))) == -1)
goto fail;
return r;
fail:
close_connection(s);
session_destroy(s);
return -1;
}
/*
* Close examined/selected mailbox.
*/
int
request_close(const char *server, const char *user)
{
int r;
session *s;
if (!(s = session_find(server, user)))
return -1;
if ((r = response_generic(s, imap_close(s))) == -1)
goto fail;
return r;
fail:
close_connection(s);
session_destroy(s);
return -1;
}
/*
* Search selected mailbox according to the supplied search criteria.
*/
int
request_search(const char *server, const char *user, const char *criteria,
char **mesgs)
{
int r;
session *s;
if (!(s = session_find(server, user)))
return -1;
if ((r = response_search(s, imap_search(s, get_option_boolean("uid"),
get_option_string("charset"), criteria), mesgs)) == -1)
goto fail;
return r;
fail:
close_connection(s);
session_destroy(s);
return -1;
}
/*
* Fetch the specified information or data of the messages.
*/
int
request_fetch(const char *server, const char *user, const char *mesg,
const char *items, char **fetch)
{
int r;
session *s;
if (!(s = session_find(server, user)))
return -1;
if ((r = response_fetch(s, imap_fetch(s, get_option_boolean("uid"),
mesg, items), fetch)) == -1)
goto fail;
return r;
fail:
close_connection(s);
session_destroy(s);
return -1;
}
/*
* Add, remove or replace the specified flags of the messages.
*/
int
request_store(const char *server, const char *user, const char *mesg,
const char *mode, const char *flags)
{
int r;
session *s;
if (!(s = session_find(server, user)))
return -1;
if ((r = response_generic(s, imap_store(s, get_option_boolean("uid"),
mesg, mode, flags))) == -1)
goto fail;
if (xstrcasestr(flags, "\\Deleted") && get_option_boolean("expunge"))
if (response_generic(s, imap_expunge(s)) == -1)
goto fail;
return r;
fail:
close_connection(s);
session_destroy(s);
return -1;
}
/*
* Copy the specified messages to another mailbox.
*/
int
request_copy(const char *server, const char *user, const char *mesg,
const char *mbox)
{
int r;
session *s;
if (!(s = session_find(server, user)))
return -1;
do {
switch (r = response_generic(s, imap_copy(s,
get_option_boolean("uid"), mesg, apply_namespace(mbox,
s->ns.prefix, s->ns.delim)))) {
case RESPONSE_TRYCREATE:
if (create_mailbox(s, mbox) == -1)
goto fail;
break;
case -1:
goto fail;
break;
}
} while (r == RESPONSE_TRYCREATE);
return r;
fail:
close_connection(s);
session_destroy(s);
return -1;
}
/*
* Append supplied message to the specified mailbox.
*/
int
request_append(const char *server, const char *user, const char *mbox,
const char *mesg, const char *flags, const char *date)
{
int r;
int t;
session *s;
if (!(s = session_find(server, user)))
return -1;
do {
if ((t = imap_append(s, apply_namespace(mbox, s->ns.prefix,
s->ns.delim), flags, date, strlen(mesg))) == -1)
goto fail;
if ((r = response_generic(s, t)) == -1)
goto fail;
switch (r) {
case RESPONSE_CONTINUE:
if (imap_continuation(s, mesg) == -1)
goto fail;
if ((r = response_generic(s, t)) == -1)
goto fail;
break;
case RESPONSE_TRYCREATE:
if (create_mailbox(s, mbox) == -1)
goto fail;
break;
case -1:
goto fail;
break;
}
} while (r == RESPONSE_TRYCREATE);
return r;
fail:
close_connection(s);
session_destroy(s);
return -1;
}
/*
* Auxiliary function to create a mailbox.
*/
int
create_mailbox(session * ssn, const char *mbox)
{
int r;
if ((r = response_generic(ssn, imap_create(ssn,
apply_namespace(mbox, ssn->ns.prefix, ssn->ns.delim)))) == -1)
return -1;
if (get_option_boolean("subscribe"))
if (response_generic(ssn, imap_subscribe(ssn,
apply_namespace(mbox, ssn->ns.prefix,
ssn->ns.delim))) == -1)
return -1;
return r;
}
/*
* Convert the names of personal mailboxes using the namespace specified
* by the mail server.
*/
const char *
apply_namespace(const char *mbox, char *prefix, char delim)
{
int n;
char *c;
if ((prefix == NULL && delim == '\0') ||
(prefix == NULL && delim == '/') ||
!strcasecmp(mbox, "INBOX"))
return mbox;
buffer_reset(&nbuf);
n = snprintf(nbuf.data, nbuf.size + 1, "%s%s", (prefix ? prefix : ""),
mbox);
if (n > (int)nbuf.size) {
buffer_check(&nbuf, n);
snprintf(nbuf.data, nbuf.size + 1, "%s%s",
(prefix ? prefix : ""), mbox);
}
c = nbuf.data;
while ((c = strchr(c, '/')))
*(c++) = delim;
debug("mailbox: '%s'\n", nbuf.data);
return nbuf.data;
}
|