svcb.c 10.4 KB
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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Library Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* sieve callback implementations */

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <mailutils/address.h>
#include <mailutils/envelope.h>
#include <mailutils/errno.h>
#include <mailutils/header.h>

#include "sv.h"

/** mailutils errno to sieve error code translation. */

/*
Note:

The sieve engine doesn't really care what you return from the callbacks, it's
either SIEVE_OK (== 0) or an error. This has the effect that if getheader()
fails due to, for example, an IMAP protocol error, it will be assumed the
header isn't present, which isn't true.

On the other hand, it makes this really simple mapping below possible.
*/

static int sieve_err(int rc)
{
  return rc;
}

/** message query callbacks **/

int
sv_getsize (void *mc, int *size)
{
  sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
  size_t sz = 0;

  m->rc = message_size (m->msg, &sz);

  if (!m->rc)
    {
      *size = sz;

      mu_debug_print (m->debug, SV_DEBUG_MSG_QUERY,
		      "msg uid %d getsize: %d\n", m->uid, *size);
    }

  return sieve_err (m->rc);
}

/*
A given header can occur multiple times, so we return a pointer to a null
terminated array of pointers to the values found for the named header.
*/
int
sv_getheader (void *mc, const char *name, const char ***body)
{
  sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;

  m->rc = 0;

  if (!m->cache_filled)
    {
      header_t h = 0;
      size_t i = 0;
      char *fn = 0;
      char *fv = 0;

      m->cache_filled = 1;

      message_get_header (m->msg, &h);

      header_get_field_count (h, &i);

      mu_debug_print (m->debug, SV_DEBUG_HDR_FILL,
		      "msg uid %d cache: filling with %d fields\n",
		      m->uid, i);

      for (; i > 0; i--)
	{
	  m->rc = header_aget_field_name (h, i, &fn);
	  if (m->rc)
	    break;
	  m->rc = header_aget_field_value (h, i, &fv);
	  if (m->rc)
	    break;

	  mu_debug_print (m->debug, SV_DEBUG_HDR_FILL,
			  "msg uid %d cache: %s: %s\n", m->uid, fn, fv);

	  m->rc = sv_field_cache_add (&m->cache, fn, fv);

	  if (m->rc == 0)
	    {
	      fv = 0;		/* owned by the cache */
	    }
	  else
	    break;

	  /* the cache doesn't want it, and we don't need it */
	  free (fn);
	  fn = 0;
	}
      free (fn);
      free (fv);
      if (m->rc)
	{
	  mu_debug_print (m->debug, SV_DEBUG_MSG_QUERY,
			  "msg uid %d cache: failed %s\n",
			  m->uid, sv_strerror (m->rc));
	  return sieve_err (m->rc);
	}
    }

  if ((m->rc = sv_field_cache_get (&m->cache, name, body)) == 0)
    {
      const char **b = *body;
      int i;
      for (i = 0; b[i]; i++)
	{
	  mu_debug_print (m->debug, SV_DEBUG_MSG_QUERY,
			  "msg uid %d getheader: %s: %s\n", m->uid, name,
			  b[i]);
	}
    }
  else
    mu_debug_print (m->debug, SV_DEBUG_MSG_QUERY,
		    "msg uid %d getheader: %s (not found)\n", m->uid, name);

  return sieve_err (m->rc);
}

/*
We need to do a little more work to deduce the envelope addresses, they
aren't necessarily stored in the mailbox.

CMU sieve cheesy sample implementation:

name will always be "to" or "from"

int
getenvelope (void *mc, const char *name, const char ***body)
{
  static const char *buf[2];

  if (buf[0] == NULL)
    {
      buf[0] = malloc (sizeof (char) * 256);
      buf[1] = NULL;
    }
  printf ("Envelope body of '%s'? ", head);
  scanf ("%s", buf[0]);
  body = buf;

  return SIEVE_OK;
}
*/

/** message action callbacks */

/*
The actions arguments are mostly callback data provided during the
setup of the intepreter object, script object, and the execution of
a script.

The args are:

void* ac; // action context, the member of the union Action.u associated
	  // with this kind of action.
void* ic, // from sieve_interp_alloc(, ic);
void* sc, // from sieve_script_parse(, , sc, );
void* mc, // from sieve_execute_script(, mc);
const char** errmsg // you can fill it in if you return failure
*/

static void
action_log (sv_msg_ctx_t * mc, const char *action, const char *fmt, ...)
{
  const char *script = mc->sc->file;
  message_t msg = mc->msg;
  sv_action_log_t log = mc->sc->ic->action_log;
  va_list ap;

  va_start (ap, fmt);

  if (log)
    log (script, msg, action, fmt, ap);

  va_end (ap);
}

int
sv_keep (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
  sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
  //sieve_keep_context_t * a = (sieve_keep_context_t *) ac;
  m->rc = 0;

  action_log (mc, "KEEP", "");

  m->rc = sv_mu_mark_deleted (m->msg, 0);

  return sieve_err (m->rc);
}

int
sv_fileinto (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
  sieve_fileinto_context_t *a = (sieve_fileinto_context_t *) ac;
  sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;

  m->rc = 0;

  action_log (mc, "FILEINTO", "delivering into %s", a->mailbox);

  if ((m->svflags & SV_FLAG_NO_ACTIONS) == 0)
    {
      m->rc = message_save_to_mailbox (m->msg, m->ticket, m->debug, a->mailbox);
      if (!m->rc)
	{
	  m->rc = sv_mu_mark_deleted (m->msg, 1);
	}
    }

  return sieve_err (m->rc);
}

int
sv_redirect (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
  sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
  sieve_redirect_context_t *a = (sieve_redirect_context_t *) ac;
  char *fromaddr = 0;
  address_t to = 0;
  address_t from = 0;

  action_log (mc, "REDIRECT", "to %s", a->addr);

  if (!m->mailer)
    {
      m->rc = ENOSYS;
      mu_debug_print (m->debug, MU_DEBUG_ERROR, "%s\n",
	  "redirect - requires a mailer");
      return SIEVE_FAIL;
    }

  if ((m->rc = address_create (&to, a->addr)))
    {
      mu_debug_print (m->debug, MU_DEBUG_ERROR,
		      "redirect - parsing to '%s' failed: [%d] %s\n",
		      a->addr, m->rc, mu_errstring (m->rc));
      goto end;
    }

  {
    envelope_t envelope = 0;
    size_t sz = 0;
    if ((m->rc = message_get_envelope (m->msg, &envelope)))
      goto end;

    if ((m->rc = envelope_sender (envelope, NULL, 0, &sz)))
      goto end;

    if (!(fromaddr = malloc (sz + 1)))
      {
	m->rc = ENOMEM;
	goto end;
      }
    if ((m->rc = envelope_sender (envelope, fromaddr, sz + 1, NULL)))
      goto end;
  }

  if ((m->rc = address_create (&from, fromaddr)))
  {
    mu_debug_print (m->debug, MU_DEBUG_ERROR,
		    "redirect - parsing from '%s' failed: [%d] %s\n",
		    a->addr, m->rc, mu_errstring (m->rc));
    goto end;
  }

  if ((m->rc = mailer_open (m->mailer, 0)))
  {
    mu_debug_print (m->debug, MU_DEBUG_ERROR,
		    "redirect - opening mailer '%s' failed: [%d] %s\n",
		    m->mailer, m->rc, mu_errstring (m->rc));
    goto end;
  }

  if ((m->rc = mailer_send_message (m->mailer, m->msg, from, to)))
  {
    mu_debug_print (m->debug, MU_DEBUG_ERROR,
		    "redirect - send from '%s' to '%s' failed: [%d] %s\n",
		    fromaddr, a->addr, m->rc, mu_errstring (m->rc));
    goto end;
  }

end:

  if (fromaddr)
    free (fromaddr);
  mailer_close (m->mailer);
  address_destroy (&to);
  address_destroy (&from);

  return sieve_err (m->rc);
}

int
sv_discard (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
  sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
//sv_interp_ctx_t *i = (sv_interp_ctx_t *) ic;

  m->rc = 0;

  action_log (mc, "DISCARD", "marking as deleted");

  if ((m->svflags & SV_FLAG_NO_ACTIONS) == 0)
    {
      m->rc = sv_mu_mark_deleted (m->msg, 1);
      mu_debug_print (m->debug, MU_DEBUG_ERROR,
		      "discard - deleting failed: [%d] %s\n",
		      m->rc, mu_errstring (m->rc));
    }

  return sieve_err (m->rc);
}

int
sv_reject (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
  sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;

  m->rc = ENOSYS;

  action_log (mc, "REJECT", "");

  return sieve_err (m->rc);
}

#if 0
int
sv_notify (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
  sv_print_action ("NOTIFY", ac, ic, sc, mc);

  return SIEVE_OK;
}
#endif

#if 0
int
sv_autorespond (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
  return SIEVE_FAIL;
}

int
sv_send_response (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
  return SIEVE_FAIL;
}

sieve_vacation_t vacation = {
  0,				/* min response */
  0,				/* max response */
  &sv_autorespond,		/* autorespond() */
  &sv_send_response		/* send_response() */
};

char *markflags[] = { "\\flagged" };
sieve_imapflags_t mark = { markflags, 1 };
#endif

/* sieve error callbacks */

int
sv_parse_error (int lineno, const char *msg, void *ic, void *sc)
{
  sv_interp_ctx_t *i = (sv_interp_ctx_t *) ic;
  sv_script_ctx_t *s = (sv_script_ctx_t *) sc;

  if (i->parse_error)
    i->parse_error (s->file, lineno, msg);

  return SIEVE_OK;
}

int
sv_execute_error (const char *msg, void *ic, void *sc, void *mc)
{
  sv_interp_ctx_t *i = (sv_interp_ctx_t *) ic;
  sv_script_ctx_t *s = (sv_script_ctx_t *) sc;
  sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;

  if (i->execute_error)
    i->execute_error (s->file, m->msg, m->rc, msg);

  return SIEVE_OK;
}

/* register supported callbacks */

int
sv_register_callbacks (sieve_interp_t * i)
{
  int rc = 0;

  /* These 4 callbacks are mandatory. */
  if (!rc)
    rc = sieve_register_size (i, &sv_getsize);
  if (!rc)
    rc = sieve_register_header (i, &sv_getheader);
  if (!rc)
    rc = sieve_register_redirect (i, &sv_redirect);
  if (!rc)
    rc = sieve_register_keep (i, &sv_keep);

#if 0
  if (!rc)
    rc = sieve_register_envelope (i, &sv_getenvelope);
#endif
  if (!rc)
    rc = sieve_register_discard (i, &sv_discard);
#if 0
  if (!rc)
    rc = sieve_register_reject (i, &sv_reject);
#endif
  /* The "fileinto" extension. */
  if (!rc)
    rc = sieve_register_fileinto (i, &sv_fileinto);
#if 0
  if (!rc)
    rc = sieve_register_vacation (i, &sv_vacation);
#endif
#if 0
  if (!rc)
    rc = sieve_register_imapflags (i, &mark);
#endif
#if 0
  if (!rc)
    rc = sieve_register_notify (i, &sv_notify);
#endif
  if (!rc)
    rc = sieve_register_parse_error (i, &sv_parse_error);
  if (!rc)
    rc = sieve_register_execute_error (i, &sv_execute_error);
  return rc;
}