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

Added plugins commands

This commit is contained in:
James Booth 2013-08-03 02:21:43 +01:00
parent dded9e954f
commit 5464d5079b
6 changed files with 146 additions and 8 deletions

View File

@ -23,7 +23,8 @@ profanity_SOURCES = \
src/config/preferences.c src/config/preferences.h \
src/config/theme.c src/config/theme.h \
src/plugins/plugins.h src/plugins/plugins.c \
src/plugins/api.h src/plugins/api.c
src/plugins/api.h src/plugins/api.c \
src/plugins/command.h src/plugins/command.c
TESTS = tests/testsuite
check_PROGRAMS = tests/testsuite
@ -52,6 +53,7 @@ tests_testsuite_SOURCES = \
src/config/theme.c src/config/theme.h \
src/plugins/plugins.h src/plugins/plugins.c \
src/plugins/api.h src/plugins/api.c \
src/plugins/command.h src/plugins/command.c \
tests/test_roster.c tests/test_common.c tests/test_history.c \
tests/test_autocomplete.c tests/testsuite.c tests/test_parser.c \
tests/test_jid.c

15
plugins/whoami.py Normal file
View File

@ -0,0 +1,15 @@
import prof
import getpass
# hooks
def prof_init(version, status):
prof.register_command("/whoami", 0, 0, "/whoami", "Call shell whoami command.", "Call shell whoami command.", cmd_whoami)
# local functions
def cmd_whoami():
me = getpass.getuser()
prof.cons_show("")
prof.cons_show(me)
prof.cons_show("")

View File

@ -22,23 +22,56 @@
#include <Python.h>
#include "plugins/command.h"
#include "ui/ui.h"
// API functions
static PyObject*
api_cons_show(PyObject *self, PyObject *args)
{
const char *message = NULL;
if (!PyArg_ParseTuple(args, "s", &message)) {
return NULL;
}
cons_show("%s", message);
return Py_BuildValue("");
}
static PyObject*
api_register_command(PyObject *self, PyObject *args)
{
const char *command_name = NULL;
int min_args = 0;
int max_args = 0;
const char *usage = NULL;
const char *short_help = NULL;
const char *long_help = NULL;
PyObject *p_callback = NULL;
if (!PyArg_ParseTuple(args, "siisssO", &command_name, &min_args, &max_args, &usage, &short_help, &long_help, &p_callback)) {
return NULL;
}
if (p_callback && PyCallable_Check(p_callback)) {
PluginCommand *command = malloc(sizeof(PluginCommand));
command->command_name = command_name;
command->min_args = min_args;
command->max_args = max_args;
command->usage = usage;
command->short_help = short_help;
command->long_help = long_help;
command->p_callback = p_callback;
add_command(command);
}
return Py_BuildValue("");
}
static PyMethodDef apiMethods[] = {
{ "cons_show", api_cons_show, METH_VARARGS, "Print a line to the console." },
{ "register_command", api_register_command, METH_VARARGS, "Register a command." },
{ NULL, NULL, 0, NULL }
};

44
src/plugins/command.c Normal file
View File

@ -0,0 +1,44 @@
/*
* command.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 <glib.h>
// API functions
static GSList *p_commands = NULL;
typedef struct p_command {
const char *command_name;
int min_args;
int max_args;
const char *usage;
const char *short_help;
const char *long_help;
PyObject *p_callback;
} PluginCommand;
void
add_command(PluginCommand *command)
{
p_commands = g_slist_append(p_commands, command);
}

35
src/plugins/command.h Normal file
View File

@ -0,0 +1,35 @@
/*
* command.h
*
* 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>
typedef struct p_command {
const char *command_name;
int min_args;
int max_args;
const char *usage;
const char *short_help;
const char *long_help;
PyObject *p_callback;
} PluginCommand;
void add_command(PluginCommand *command);

View File

@ -44,7 +44,10 @@ plugins_init(void)
api_init();
// TODO change to use XDG spec
PySys_SetPath("$PYTHONPATH:./plugins/");
GString *path = g_string_new(Py_GetPath());
g_string_append(path, ":./plugins/");
PySys_SetPath(path->str);
g_string_free(path, TRUE);
if (module_names != NULL) {
cons_show("Loading plugins...");
@ -65,6 +68,10 @@ plugins_init(void)
_init();
_on_start();
if (PyErr_Occurred()) {
PyErr_Print();
PyErr_Clear();
}
}
return;
}
@ -127,10 +134,12 @@ _run_plugins(const char * const function, PyObject *p_args)
while (plugin != NULL) {
PyObject *p_module = plugin->data;
p_function = PyObject_GetAttrString(p_module, function);
if (p_function && PyCallable_Check(p_function)) {
PyObject_CallObject(p_function, p_args);
Py_XDECREF(p_function);
if (PyObject_HasAttrString(p_module, function)) {
p_function = PyObject_GetAttrString(p_module, function);
if (p_function && PyCallable_Check(p_function)) {
PyObject_CallObject(p_function, p_args);
Py_XDECREF(p_function);
}
}
plugin = g_slist_next(plugin);