Blame view

maidag/script.c 3.42 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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2002, 2005,
   2007 Free Software Foundation, Inc.

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

   GNU Mailutils 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#include "maidag.h"

#ifdef WITH_GUILE
#include <mailutils/guile.h>

int debug_guile;

SCM mda_catch_body (void *data, mu_mailbox_t mbox);
SCM mda_catch_handler (void *unused, SCM tag, SCM throw_args);
int mda_next (void *data, mu_mailbox_t mbox);
int mda_exit (void *data, mu_mailbox_t mbox);
int mda_init (void *data);

int
prog_mda (struct mda_data *data)
{
  char *x_argv[2];
  mu_guimb_param_t param;
  
  x_argv[0] = "maidag";
  x_argv[1] = NULL;

  param.debug_guile = debug_guile;
  param.mbox = data->mbox;
  param.user_name = NULL;
  param.init = mda_init;
  param.catch_body = mda_catch_body;
  param.catch_handler = mda_catch_handler;
  param.next = mda_next;
  param.exit = mda_exit;
  param.data = data;
  
  mu_process_mailbox (1, x_argv, &param);
  return EX_UNAVAILABLE;
}

int
mda_init (void *data)
{
  struct mda_data *md = data;
  md->progfile = mu_expand_path_pattern (md->progfile_pattern, md->argv[0]);
  return 0;
}

static void
mda_switch_to_user (struct mda_data *md)
{
  struct mu_auth_data *auth = NULL;
  
  if (md && *md->argv != NULL)
    auth = mu_get_auth_by_name (*md->argv);

  if (auth)
    {
      switch_user_id (auth, 1);
      chdir (auth->dir);
      mu_auth_data_free (auth);
    }
  else
    {
      switch_user_id (auth, 0);
      chdir ("/");
    }
}

SCM
mda_catch_body (void *data, mu_mailbox_t mbox)
{
  struct mda_data *md = data;
  mu_message_t mesg = NULL;
  mu_attribute_t attr = NULL;

  if (access (md->progfile, R_OK))
    {
      if (debug_level > 2)
95
	mu_diag_output (MU_DIAG_DEBUG, _("Access to %s failed: %m"), md->progfile);
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
    }
  else
    {
      mda_switch_to_user (md);
      scm_primitive_load (scm_makfrom0str (md->progfile));
    }
  
  mu_mailbox_get_message (mbox, 1, &mesg);
  mu_message_get_attribute (mesg, &attr);
  if (mu_attribute_is_deleted (attr))
    return SCM_BOOL_F;

  mda_switch_to_user (NULL);
  mda (md->mbox, md->argv[0]);
  return SCM_BOOL_F;
}

SCM
mda_catch_handler (void *data, SCM tag, SCM throw_args)
{
  exit_code = EX_TEMPFAIL;
  return scm_handle_by_message_noexit ("mail.local", tag, throw_args);
}

int
mda_next (void *data, mu_mailbox_t mbox)
{
  struct mda_data *md = data;
  mu_message_t mesg = NULL;
  mu_attribute_t attr = NULL;

  md->argv++;
  if (*md->argv == NULL)
    return 0;
  if (md->progfile)
    free (md->progfile);
  md->progfile = mu_expand_path_pattern (md->progfile_pattern, *md->argv);

  mu_mailbox_get_message (mbox, 1, &mesg);
  mu_message_get_attribute (mesg, &attr);
  mu_attribute_unset_deleted (attr);
  
  return md->progfile != NULL;
}

int
mda_exit (void *data, mu_mailbox_t mbox)
{
  return exit_code;
}

#endif