1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-14 08:13:02 +01:00

Python interface for getting the user's nickname/status and executing commands

This commit is contained in:
jakob 2017-05-15 22:12:28 -04:00
parent 0e13a1f1bc
commit 90210daca7
6 changed files with 120 additions and 12 deletions

View File

@ -6,11 +6,8 @@ PYTHON_OBJ = api.o python_api.o
# Check if we can build Python scripting support
CHECK_PYTHON_LIBS = $(shell $(PKG_CONFIG) --exists $(PYTHON_LIBS) || echo -n "error")
ifneq ($(CHECK_PYTHON_LIBS), error)
# LIBS += $(PYTHON_LIBS)
# Unwise hacks... You will pay for this.
LDFLAGS += $(shell python3-config --ldflags)
CFLAGS += $(PYTHON_CFLAGS) $(shell python3-config --cflags)
CFLAGS += $(PYTHON_CFLAGS) $(shell python3-config --includes)
OBJ += $(PYTHON_OBJ)
else ifneq ($(MAKECMDGOALS), clean)
MISSING_AUDIO_LIBS = $(shell for lib in $(PYTHON_LIBS) ; do if ! $(PKG_CONFIG) --exists $$lib ; then echo $$lib ; fi ; done)

View File

@ -20,25 +20,75 @@
*
*/
#include <stdint.h>
#include <tox/tox.h>
#include "execute.h"
#include "friendlist.h"
#include "line_info.h"
#include "python_api.h"
#include "windows.h"
extern ToxWindow *prompt;
Tox *user_tox;
static WINDOW *cur_window;
static ToxWindow *self_window;
extern FriendsList Friends;
void api_display(const char * const msg)
{
if (msg == NULL)
return;
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, msg);
line_info_add(self_window, NULL, NULL, NULL, SYS_MSG, 0, 0, msg);
}
FriendsList api_get_friendslist(void)
{
return Friends;
}
char *api_get_nick(void)
{
size_t len = tox_self_get_name_size(user_tox);
uint8_t *name = malloc(len + 1);
if (name == NULL)
return NULL;
tox_self_get_name(user_tox, name);
return (char *) name;
}
TOX_USER_STATUS api_get_status(void)
{
return tox_self_get_status(user_tox);
}
char *api_get_status_message(void)
{
size_t len = tox_self_get_status_message_size(user_tox);
uint8_t *status = malloc(len + 1);
if (status == NULL)
return NULL;
tox_self_get_status_message(user_tox, status);
return (char *) status;
}
void api_execute(const char *input, int mode)
{
execute(cur_window, self_window, user_tox, input, mode);
}
/* TODO: Register command */
void cmd_run(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
FILE *fp;
const char *error_str;
cur_window = window;
self_window = self;
if ( argc != 1 ) {
if ( argc < 1 ) error_str = "Path must be specified!";
else error_str = "Only one argument allowed!";

View File

@ -23,8 +23,14 @@
#ifndef API_H
#define API_H
#include "friendlist.h"
#include "windows.h"
void api_display(const char * const msg);
FriendsList api_get_friendslist(void);
char *api_get_nick(void);
TOX_USER_STATUS api_get_status(void);
char *api_get_status_message(void);
void api_execute(const char *input, int mode);
#endif /* #define API_H */

View File

@ -24,19 +24,71 @@
#include "api.h"
extern Tox *user_tox;
static PyObject *python_api_display(PyObject *self, PyObject *args)
{
const char *msg;
if (!PyArg_ParseTuple(args, "s", &msg))
return NULL;
api_display(msg);
return Py_None;
}
Py_RETURN_NONE;
static PyObject *python_api_get_nick(PyObject *self, PyObject *args)
{
char *name;
PyObject *ret;
if (!PyArg_ParseTuple(args, ""))
return NULL;
name = api_get_nick();
if (name == NULL)
return NULL;
ret = Py_BuildValue("s", name);
free(name);
return ret;
}
static PyObject *python_api_get_status(PyObject *self, PyObject *args)
{
TOX_USER_STATUS status;
PyObject *ret;
if (!PyArg_ParseTuple(args, ""))
return NULL;
status = api_get_status();
ret = Py_BuildValue("i", status);
return ret;
}
static PyObject *python_api_get_status_message(PyObject *self, PyObject *args)
{
char *status;
PyObject *ret;
if (!PyArg_ParseTuple(args, ""))
return NULL;
status = api_get_status_message();
if (status == NULL)
return NULL;
ret = Py_BuildValue("s", status);
free(status);
return ret;
}
static PyObject *python_api_execute(PyObject *self, PyObject *args)
{
int mode;
const char *command;
if (!PyArg_ParseTuple(args, "si", &command, &mode))
return NULL;
api_execute(command, mode);
return Py_None;
}
static PyMethodDef ToxicApiMethods[] = {
{"display", python_api_display, METH_VARARGS, "Display a message to the primary prompt"},
{"get_nick", python_api_get_nick, METH_VARARGS, "Return the user's current nickname"},
{"get_status_message", python_api_get_status_message, METH_VARARGS, "Return the user's current status message"},
{"execute", python_api_execute, METH_VARARGS, "Execute a command like `/nick`"},
{NULL, NULL, 0, NULL},
};
@ -58,11 +110,14 @@ void terminate_python(void)
Py_FinalizeEx();
}
void init_python(void)
void init_python(Tox *m)
{
wchar_t *program = Py_DecodeLocale("toxic", NULL);
user_tox = m;
PyImport_AppendInittab("toxic_api", PyInit_toxic_api);
/* TODO: Set Python program name. */
Py_SetProgramName(program);
Py_Initialize();
PyMem_RawFree(program);
}
void run_python(FILE *fp, char *path)

View File

@ -27,7 +27,7 @@
PyMODINIT_FUNC PyInit_toxic_api(void);
void terminate_python(void);
void init_python(void);
void init_python(Tox *m);
void run_python(FILE *fp, char *path);
#endif /* #define PYTHON_API_H */

View File

@ -1221,7 +1221,7 @@ int main(int argc, char **argv)
#ifdef PYTHON
init_python();
init_python(m);
#endif /* PYTHON */