Blame view

python/libmu_py/mailcap.c 9.73 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

#define PY_MODULE  "mailcap"
#define PY_CSNAME1 "MailcapType"
#define PY_CSNAME2 "MailcapEntryType"

static PyObject *
_repr1 (PyObject *self)
{
  char buf[80];
  sprintf (buf, "<" PY_MODULE "." PY_CSNAME1 " instance at %p>", self);
  return PyString_FromString (buf);
}

static PyTypeObject PyMailcapType = {
  PyObject_HEAD_INIT(NULL)
  0,                         /* ob_size */
  PY_MODULE "." PY_CSNAME1,  /* tp_name */
  sizeof (PyMailcap),        /* tp_basicsize */
  0,                         /* tp_itemsize */
38
  (destructor)_py_dealloc,   /* tp_dealloc */
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  0,                         /* tp_print */
  0,                         /* tp_getattr; __getattr__ */
  0,                         /* tp_setattr; __setattr__ */
  0,                         /* tp_compare; __cmp__ */
  _repr1,                    /* tp_repr; __repr__ */
  0,                         /* tp_as_number */
  0,                         /* tp_as_sequence */
  0,                         /* tp_as_mapping */
  0,                         /* tp_hash; __hash__ */
  0,                         /* tp_call; __call__ */
  _repr1,                    /* tp_str; __str__ */
  0,                         /* tp_getattro */
  0,                         /* tp_setattro */
  0,                         /* tp_as_buffer */
  Py_TPFLAGS_DEFAULT,        /* tp_flags */
  "",                        /* tp_doc */
55 56 57 58 59 60
  0,                         /* tp_traverse */
  0,                         /* tp_clear */
  0,                         /* tp_richcompare */
  0,                         /* tp_weaklistoffset */
  0,                         /* tp_iter */
  0,                         /* tp_iternext */
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
  0,                         /* tp_methods */
  0,                         /* tp_members */
  0,                         /* tp_getset */
  0,                         /* tp_base */
  0,                         /* tp_dict */
  0,                         /* tp_descr_get */
  0,                         /* tp_descr_set */
  0,                         /* tp_dictoffset */
  0,                         /* tp_init */
  0,                         /* tp_alloc */
  0,                         /* tp_new */
};

PyMailcap *
PyMailcap_NEW ()
{
  return (PyMailcap *)PyObject_NEW (PyMailcap, &PyMailcapType);
}

static PyObject *
_repr2 (PyObject *self)
{
  char buf[80];
  sprintf (buf, "<" PY_MODULE "." PY_CSNAME2 " instance at %p>", self);
  return PyString_FromString (buf);
}

static PyTypeObject PyMailcapEntryType = {
  PyObject_HEAD_INIT(NULL)
  0,                         /* ob_size */
  PY_MODULE "." PY_CSNAME2,  /* tp_name */
  sizeof (PyMailcapEntry),   /* tp_basicsize */
  0,                         /* tp_itemsize */
94
  (destructor)_py_dealloc,   /* tp_dealloc */
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  0,                         /* tp_print */
  0,                         /* tp_getattr; __getattr__ */
  0,                         /* tp_setattr; __setattr__ */
  0,                         /* tp_compare; __cmp__ */
  _repr2,                    /* tp_repr; __repr__ */
  0,                         /* tp_as_number */
  0,                         /* tp_as_sequence */
  0,                         /* tp_as_mapping */
  0,                         /* tp_hash; __hash__ */
  0,                         /* tp_call; __call__ */
  _repr2,                    /* tp_str; __str__ */
  0,                         /* tp_getattro */
  0,                         /* tp_setattro */
  0,                         /* tp_as_buffer */
  Py_TPFLAGS_DEFAULT,        /* tp_flags */
  "",                        /* tp_doc */
111 112 113 114 115 116
  0,                         /* tp_traverse */
  0,                         /* tp_clear */
  0,                         /* tp_richcompare */
  0,                         /* tp_weaklistoffset */
  0,                         /* tp_iter */
  0,                         /* tp_iternext */
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
  0,                         /* tp_methods */
  0,                         /* tp_members */
  0,                         /* tp_getset */
  0,                         /* tp_base */
  0,                         /* tp_dict */
  0,                         /* tp_descr_get */
  0,                         /* tp_descr_set */
  0,                         /* tp_dictoffset */
  0,                         /* tp_init */
  0,                         /* tp_alloc */
  0,                         /* tp_new */
};

PyMailcapEntry *
PyMailcapEntry_NEW ()
{
  return (PyMailcapEntry *)PyObject_NEW (PyMailcapEntry,
					 &PyMailcapEntryType);
}


static PyObject *
api_mailcap_create (PyObject *self, PyObject *args)
{
  int status;
  PyMailcap *py_mc;
  PyStream *py_stm;

  if (!PyArg_ParseTuple (args, "O!O", &PyMailcapType, &py_mc, &py_stm))
    return NULL;

  if (!PyStream_Check ((PyObject *)py_stm))
    {
      PyErr_SetString (PyExc_TypeError, py_stm->ob_type->tp_name);
      return NULL;
    }

  status = mu_mailcap_create (&py_mc->mc, py_stm->stm);
  return _ro (PyInt_FromLong (status));
}

static PyObject *
api_mailcap_destroy (PyObject *self, PyObject *args)
{
  PyMailcap *py_mc;

  if (!PyArg_ParseTuple (args, "O!", &PyMailcapType, &py_mc))
    return NULL;

  mu_mailcap_destroy (&py_mc->mc);
  return _ro (Py_None);
}

static PyObject *
api_mailcap_entries_count (PyObject *self, PyObject *args)
{
  int status;
  size_t count = 0;
  PyMailcap *py_mc;

  if (!PyArg_ParseTuple (args, "O!", &PyMailcapType, &py_mc))
    return NULL;

  status = mu_mailcap_entries_count (py_mc->mc, &count);
181
  return status_object (status, PyInt_FromSize_t (count));
182 183 184 185 186
}

static PyObject *
api_mailcap_get_entry (PyObject *self, PyObject *args)
{
187 188
  int status;
  Py_ssize_t i;
189 190 191
  PyMailcap *py_mc;
  PyMailcapEntry *py_entry = PyMailcapEntry_NEW ();

192
  if (!PyArg_ParseTuple (args, "O!n", &PyMailcapType, &py_mc, &i))
193
    return NULL;
194
  ASSERT_INDEX_RANGE (i, "mailcap");
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  status = mu_mailcap_get_entry (py_mc->mc, i, &py_entry->entry);

  Py_INCREF (py_entry);
  return status_object (status, (PyObject *)py_entry);
}

static PyObject *
api_mailcap_entry_fields_count (PyObject *self, PyObject *args)
{
  int status;
  size_t count;
  PyMailcapEntry *py_entry;

  if (!PyArg_ParseTuple (args, "O!", &PyMailcapEntryType, &py_entry))
    return NULL;

  status = mu_mailcap_entry_fields_count (py_entry->entry, &count);
212
  return status_object (status, PyInt_FromSize_t (count));
213 214 215 216 217
}

static PyObject *
api_mailcap_entry_get_field (PyObject *self, PyObject *args)
{
218 219
  int status;
  Py_ssize_t i;
220 221 222
  char buf[256];
  PyMailcapEntry *py_entry;

223
  if (!PyArg_ParseTuple (args, "O!n", &PyMailcapEntryType, &py_entry, &i))
224
    return NULL;
225
  ASSERT_INDEX_RANGE (i, "mailcap");
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
  status = mu_mailcap_entry_get_field (py_entry->entry, i, buf,
				       sizeof (buf), NULL);
  return status_object (status, PyString_FromString (buf));
}

static PyObject *
api_mailcap_entry_get_typefield (PyObject *self, PyObject *args)
{
  int status;
  char buf[256];
  PyMailcapEntry *py_entry;

  if (!PyArg_ParseTuple (args, "O!", &PyMailcapEntryType, &py_entry))
    return NULL;

  status = mu_mailcap_entry_get_typefield (py_entry->entry, buf,
					   sizeof (buf), NULL);
  return status_object (status, PyString_FromString (buf));
}

static PyObject *
api_mailcap_entry_get_viewcommand (PyObject *self, PyObject *args)
{
  int status;
  char buf[256];
  PyMailcapEntry *py_entry;

  if (!PyArg_ParseTuple (args, "O!", &PyMailcapEntryType, &py_entry))
    return NULL;

  status = mu_mailcap_entry_get_viewcommand (py_entry->entry, buf,
					     sizeof (buf), NULL);
  return status_object (status, PyString_FromString (buf));
}

static PyMethodDef methods[] = {
  { "create", (PyCFunction) api_mailcap_create, METH_VARARGS,
Wojciech Polak authored
263
    "Allocate, parse the buffer from the 'stream' and initializes 'mailcap'." },
264 265 266 267 268 269 270 271

  { "destroy", (PyCFunction) api_mailcap_destroy, METH_VARARGS,
    "Release any resources from the mailcap object." },

  { "entries_count", (PyCFunction) api_mailcap_entries_count, METH_VARARGS,
    "Return the number of entries found in the mailcap." },

  { "get_entry", (PyCFunction) api_mailcap_get_entry, METH_VARARGS,
Wojciech Polak authored
272
    "Return in 'entry' the mailcap entry of 'no'." },
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

  { "entry_fields_count", (PyCFunction) api_mailcap_entry_fields_count,
    METH_VARARGS,
    "Return the number of fields found in the entry." },

  { "entry_get_field", (PyCFunction) api_mailcap_entry_get_field,
    METH_VARARGS,
    "" },

  { "entry_get_typefield", (PyCFunction) api_mailcap_entry_get_typefield,
    METH_VARARGS,
    "" },

  { "entry_get_viewcommand",
    (PyCFunction) api_mailcap_entry_get_viewcommand, METH_VARARGS,
    "" },

  { NULL, NULL, 0, NULL }
};

293
int
294
mu_py_init_mailcap (void)
295 296 297
{
  PyMailcapType.tp_new = PyType_GenericNew;
  PyMailcapEntryType.tp_new = PyType_GenericNew;
298 299 300 301 302
  if (PyType_Ready (&PyMailcapType) < 0)
    return -1;
  if (PyType_Ready (&PyMailcapEntryType) < 0)
    return -1;
  return 0;
303
}
304

305 306 307 308 309
void
_mu_py_attach_mailcap (void)
{
  PyObject *m;
  if ((m = _mu_py_attach_module (PY_MODULE, methods)))
310 311 312 313 314 315 316
    {
      Py_INCREF (&PyMailcapType);
      Py_INCREF (&PyMailcapEntryType);
      PyModule_AddObject (m, PY_CSNAME1, (PyObject *)&PyMailcapType);
      PyModule_AddObject (m, PY_CSNAME2, (PyObject *)&PyMailcapEntryType);
    }
}