1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-02-02 15:08:15 -05:00
profanity/src/api/api.c

82 lines
2.2 KiB
C
Raw Normal View History

/*
* api.c
*
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity 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 of the License, or
* (at your option) any later version.
*
* Profanity 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 Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <Python.h>
#include "ui/ui.h"
2013-07-25 00:54:31 +01:00
// API
static PyObject*
api_cons_show(PyObject *self, PyObject *args)
{
2013-07-25 01:13:41 +01:00
const char *message = NULL;
if (!PyArg_ParseTuple(args, "s", &message)) {
return NULL;
}
cons_show("%s", message);
2013-07-25 00:54:31 +01:00
return NULL;
}
static PyMethodDef apiMethods[] = {
{ "cons_show", api_cons_show, METH_VARARGS, "Print a line to the console." },
{ NULL, NULL, 0, NULL }
};
void
api_init(void)
{
2013-07-30 23:37:46 +01:00
PyObject *pName, *pModule, *pProfInit, *pProfOnStart, *pArgs;
Py_Initialize();
2013-07-30 23:42:46 +01:00
PySys_SetPath("$PYTHONPATH:./plugins/");
2013-07-25 00:54:31 +01:00
Py_InitModule("prof", apiMethods);
pName = PyString_FromString("helloworld");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
2013-07-30 23:37:46 +01:00
pProfInit = PyObject_GetAttrString(pModule, "prof_init");
if (pProfInit && PyCallable_Check(pProfInit)) {
pArgs = Py_BuildValue("ss", PACKAGE_VERSION, PACKAGE_STATUS);
PyObject_CallObject(pProfInit, pArgs);
2013-07-30 23:42:46 +01:00
Py_XDECREF(pArgs);
}
2013-07-30 23:37:46 +01:00
Py_XDECREF(pProfInit);
2013-07-30 23:37:46 +01:00
pProfOnStart = PyObject_GetAttrString(pModule, "prof_on_start");
if (pProfOnStart && PyCallable_Check(pProfOnStart)) {
PyObject_CallObject(pProfOnStart, NULL);
}
2013-07-30 23:37:46 +01:00
Py_XDECREF(pProfOnStart);
Py_DECREF(pModule);
}
else {
cons_show("Failed to load plugin");
return ;
}
Py_Finalize();
2013-07-25 00:54:31 +01:00
return;
}