1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00
profanity/src/plugins/plugins.c

160 lines
3.8 KiB
C
Raw Normal View History

/*
2013-08-02 19:58:04 -04:00
* plugins.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>
2013-08-02 19:58:04 -04:00
#include "plugins/api.h"
2013-08-04 13:43:46 -04:00
#include "plugins/callbacks.h"
2013-08-02 19:48:22 -04:00
#include "plugins/plugins.h"
#include "ui/ui.h"
2013-08-02 18:36:47 -04:00
static GSList* _get_module_names(void);
2013-08-02 19:40:10 -04:00
static void _run_plugins(const char * const function, PyObject *p_args);
2013-08-02 18:36:47 -04:00
2013-08-02 19:22:00 -04:00
static GSList* plugins;
2013-08-04 11:42:25 -04:00
static void
_check_error(void)
{
if (PyErr_Occurred()) {
PyErr_Print();
PyErr_Clear();
}
}
void
2013-08-02 19:48:22 -04:00
plugins_init(void)
{
2013-08-02 18:52:16 -04:00
plugins = NULL;
PyObject *p_module;
2013-08-02 18:36:47 -04:00
GSList *module_names = _get_module_names();
Py_Initialize();
2013-08-04 11:42:25 -04:00
_check_error();
2013-08-02 19:58:04 -04:00
api_init();
2013-08-04 11:42:25 -04:00
_check_error();
2013-07-30 18:37:46 -04:00
2013-08-02 18:36:47 -04:00
// TODO change to use XDG spec
2013-08-02 21:21:43 -04:00
GString *path = g_string_new(Py_GetPath());
g_string_append(path, ":./plugins/");
PySys_SetPath(path->str);
2013-08-04 11:42:25 -04:00
_check_error();
2013-08-02 21:21:43 -04:00
g_string_free(path, TRUE);
2013-08-02 18:36:47 -04:00
if (module_names != NULL) {
cons_show("Loading plugins...");
GSList *module_name = module_names;
while (module_name != NULL) {
p_module = PyImport_ImportModule(module_name->data);
2013-08-04 11:42:25 -04:00
_check_error();
2013-08-02 18:36:47 -04:00
if (p_module != NULL) {
2013-08-02 18:52:16 -04:00
cons_show("Loaded plugin: %s", module_name->data);
plugins = g_slist_append(plugins, p_module);
2013-08-02 18:36:47 -04:00
}
module_name = g_slist_next(module_name);
}
2013-08-02 18:52:16 -04:00
PyObject *p_args = Py_BuildValue("ss", PACKAGE_VERSION, PACKAGE_STATUS);
_run_plugins("prof_init", p_args);
Py_XDECREF(p_args);
2013-08-04 11:42:25 -04:00
_check_error();
}
2013-07-24 19:54:31 -04:00
return;
}
void
plugins_on_start(void)
{
_run_plugins("prof_on_start", NULL);
_check_error();
}
2013-08-02 19:22:00 -04:00
void
2013-08-02 19:48:22 -04:00
plugins_shutdown(void)
2013-08-02 19:22:00 -04:00
{
Py_Finalize();
}
2013-08-02 19:58:04 -04:00
void
plugins_on_connect(void)
{
_run_plugins("prof_on_connect", NULL);
}
void
plugins_on_message(const char * const jid, const char * const message)
{
PyObject *p_args = Py_BuildValue("ss", jid, message);
_run_plugins("prof_on_message", p_args);
Py_XDECREF(p_args);
}
2013-08-02 18:36:47 -04:00
static GSList *
_get_module_names(void)
{
GSList *result = NULL;
// TODO change to use XDG
GDir *plugins_dir = g_dir_open("./plugins", 0, NULL);
if (plugins_dir != NULL) {
const gchar *file = g_dir_read_name(plugins_dir);
while (file != NULL) {
if (g_str_has_suffix(file, ".py")) {
gchar *module_name = g_strndup(file, strlen(file) - 3);
result = g_slist_append(result, module_name);
}
file = g_dir_read_name(plugins_dir);
}
g_dir_close(plugins_dir);
return result;
} else {
return NULL;
}
}
2013-08-02 18:52:16 -04:00
2013-08-02 19:40:10 -04:00
static void
_run_plugins(const char * const function, PyObject *p_args)
2013-08-02 19:22:00 -04:00
{
GSList *plugin = plugins;
2013-08-02 19:40:10 -04:00
PyObject *p_function;
2013-08-02 19:22:00 -04:00
while (plugin != NULL) {
2013-08-02 19:40:10 -04:00
PyObject *p_module = plugin->data;
2013-08-02 21:21:43 -04:00
if (PyObject_HasAttrString(p_module, function)) {
p_function = PyObject_GetAttrString(p_module, function);
2013-08-04 11:42:25 -04:00
_check_error();
2013-08-02 21:21:43 -04:00
if (p_function && PyCallable_Check(p_function)) {
PyObject_CallObject(p_function, p_args);
2013-08-04 11:42:25 -04:00
_check_error();
2013-08-02 21:21:43 -04:00
Py_XDECREF(p_function);
}
2013-08-02 19:22:00 -04:00
}
plugin = g_slist_next(plugin);
}
}