1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2009-2021 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General
15 Public License along with this library. If not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #include "libmu_py.h"
19
20 #define PY_MODULE "body"
21 #define PY_CSNAME "BodyType"
22
23 static PyObject *
_repr(PyObject * self)24 _repr (PyObject *self)
25 {
26 char buf[80];
27 sprintf (buf, "<" PY_MODULE "." PY_CSNAME " instance at %p>", self);
28 return PyUnicode_FromString (buf);
29 }
30
31 static PyTypeObject PyBodyType = {
32 .ob_base = { PyObject_HEAD_INIT(NULL) },
33 .tp_name = PY_MODULE "." PY_CSNAME,
34 .tp_basicsize = sizeof (PyBody),
35 .tp_dealloc = (destructor)_py_dealloc,
36 .tp_repr = _repr,
37 .tp_str = _repr,
38 .tp_flags = Py_TPFLAGS_DEFAULT,
39 .tp_doc = "",
40 };
41
42 PyBody *
PyBody_NEW()43 PyBody_NEW ()
44 {
45 return (PyBody *)PyObject_NEW (PyBody, &PyBodyType);
46 }
47
48 static PyObject *
api_body_size(PyObject * self,PyObject * args)49 api_body_size (PyObject *self, PyObject *args)
50 {
51 int status;
52 size_t size;
53 PyBody *py_body;
54
55 if (!PyArg_ParseTuple (args, "O!", &PyBodyType, &py_body))
56 return NULL;
57
58 status = mu_body_size (py_body->body, &size);
59 return status_object (status, PyLong_FromSize_t (size));
60 }
61
62 static PyObject *
api_body_lines(PyObject * self,PyObject * args)63 api_body_lines (PyObject *self, PyObject *args)
64 {
65 int status;
66 size_t lines;
67 PyBody *py_body;
68
69 if (!PyArg_ParseTuple (args, "O!", &PyBodyType, &py_body))
70 return NULL;
71
72 status = mu_body_lines (py_body->body, &lines);
73 return status_object (status, PyLong_FromSize_t (lines));
74 }
75
76 static PyObject *
api_body_get_stream(PyObject * self,PyObject * args)77 api_body_get_stream (PyObject *self, PyObject *args)
78 {
79 int status;
80 PyBody *py_body;
81 PyStream *py_stm = PyStream_NEW ();
82
83 if (!PyArg_ParseTuple (args, "O!", &PyBodyType, &py_body))
84 return NULL;
85
86 Py_INCREF (py_stm);
87
88 status = mu_body_get_streamref (py_body->body, &py_stm->stm);
89 return status_object (status, (PyObject *)py_stm);
90 }
91
92 static PyMethodDef methods[] = {
93 { "size", (PyCFunction) api_body_size, METH_VARARGS,
94 "Retrieve 'body' size." },
95
96 { "lines", (PyCFunction) api_body_lines, METH_VARARGS,
97 "Retrieve 'body' number of lines." },
98
99 { "get_stream", (PyCFunction) api_body_get_stream, METH_VARARGS,
100 "" },
101
102 { NULL, NULL, 0, NULL }
103 };
104
105 static struct PyModuleDef moduledef = {
106 PyModuleDef_HEAD_INIT,
107 PY_MODULE,
108 NULL,
109 -1,
110 methods
111 };
112
113 int
mu_py_init_body(void)114 mu_py_init_body (void)
115 {
116 PyBodyType.tp_new = PyType_GenericNew;
117 return PyType_Ready (&PyBodyType);
118 }
119
120 void
_mu_py_attach_body(void)121 _mu_py_attach_body (void)
122 {
123 PyObject *m;
124 if ((m = _mu_py_attach_module (&moduledef)))
125 {
126 Py_INCREF (&PyBodyType);
127 PyModule_AddObject (m, PY_CSNAME, (PyObject *)&PyBodyType);
128 }
129 }
130