Blame view

python/libmu_py/registrar.c 3.16 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2
   Copyright (C) 2009-2012, 2014-2016 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14

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

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
15 16
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */
17

18
#include "libmu_py.h"
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

#define PY_MODULE "registrar"

struct format_record {
  char *name;
  mu_record_t *record;
};

static struct format_record format_table[] = {
  { "mbox", &mu_mbox_record },
  { "mh", &mu_mh_record },
  { "maildir", &mu_maildir_record },
  { "pop", &mu_pop_record },
  { "imap", &mu_imap_record },
#ifdef WITH_TLS
  { "pops", &mu_pops_record },
  { "imaps", &mu_imaps_record },
#endif /* WITH_TLS */
  { "sendmail", &mu_sendmail_record },
  { "smtp", &mu_smtp_record },
  { NULL, NULL },
};

static mu_record_t *
find_format (const struct format_record *table, const char *name)
{
  for (; table->name; table++)
    if (strcmp (table->name, name) == 0)
      break;
  return table->record;
}

static int
register_format (const char *name)
{
  int status = 0;

  if (!name)
    {
      struct format_record *table;
      for (table = format_table; table->name; table++)
	mu_registrar_record (*table->record);
    }
  else
    {
      mu_record_t *record = find_format (format_table, name);
      if (record)
	status = mu_registrar_record (*record);
      else
	status = EINVAL;
    }
  return status;
}

static int
set_default_format (const char *name)
{
  int status = 0;

  if (name)
    {
      mu_record_t *record = find_format (format_table, name);
      if (record)
	mu_registrar_set_default_record (*record);
      else
	status = EINVAL;
    }
  return status;
}

static PyObject *
api_registrar_register_format (PyObject *self, PyObject *args)
{
  int status;
  char *name = NULL;

  if (!PyArg_ParseTuple (args, "|s", &name))
    return NULL;

  status = register_format (name);
  return _ro (PyInt_FromLong (status));
}

static PyObject *
api_registrar_set_default_format (PyObject *self, PyObject *args)
{
  int status;
  char *name = NULL;

  if (!PyArg_ParseTuple (args, "s", &name))
    return NULL;

  status = set_default_format (name);
  return _ro (PyInt_FromLong (status));
}

static PyMethodDef methods[] = {
  { "register_format", (PyCFunction) api_registrar_register_format,
    METH_VARARGS,
    "Register desired mailutils formats. Any number of arguments "
    "can be given." },

  { "set_default_format", (PyCFunction) api_registrar_set_default_format,
    METH_VARARGS, "" },

  { NULL, NULL, 0, NULL }
};

void
128
_mu_py_attach_registrar ()
129
{
130
  _mu_py_attach_module (PY_MODULE, methods);
131 132 133 134

  mu_registrar_record (MU_DEFAULT_RECORD);
  mu_registrar_set_default_record (MU_DEFAULT_RECORD);
}