1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 00:43:01 +01:00

Updated code style

This commit is contained in:
jakob 2017-05-16 23:19:39 -04:00
parent b3ed8bc35c
commit 7d3d129624
3 changed files with 63 additions and 2 deletions

View File

@ -41,10 +41,11 @@ static ToxWindow *self_window;
extern FriendsList Friends; extern FriendsList Friends;
extern struct user_settings *user_settings; extern struct user_settings *user_settings;
void api_display(const char * const msg) void api_display(const char *const msg)
{ {
if (msg == NULL) if (msg == NULL)
return; return;
self_window = get_active_window(); self_window = get_active_window();
line_info_add(self_window, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); line_info_add(self_window, NULL, NULL, NULL, SYS_MSG, 0, 0, msg);
} }
@ -58,8 +59,10 @@ char *api_get_nick(void)
{ {
size_t len = tox_self_get_name_size(user_tox); size_t len = tox_self_get_name_size(user_tox);
uint8_t *name = malloc(len + 1); uint8_t *name = malloc(len + 1);
if (name == NULL) if (name == NULL)
return NULL; return NULL;
tox_self_get_name(user_tox, name); tox_self_get_name(user_tox, name);
name[len] = '\0'; name[len] = '\0';
return (char *) name; return (char *) name;
@ -74,8 +77,10 @@ char *api_get_status_message(void)
{ {
size_t len = tox_self_get_status_message_size(user_tox); size_t len = tox_self_get_status_message_size(user_tox);
uint8_t *status = malloc(len + 1); uint8_t *status = malloc(len + 1);
if (status == NULL) if (status == NULL)
return NULL; return NULL;
tox_self_get_status_message(user_tox, status); tox_self_get_status_message(user_tox, status);
return (char *) status; return (char *) status;
} }
@ -84,6 +89,7 @@ void api_send(const char *msg)
{ {
if (msg == NULL || self_window->chatwin->cqueue == NULL) if (msg == NULL || self_window->chatwin->cqueue == NULL)
return; return;
char *name = api_get_nick(); char *name = api_get_nick();
char timefrmt[TIME_STR_SIZE]; char timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt, sizeof(timefrmt)); get_time_str(timefrmt, sizeof(timefrmt));
@ -137,12 +143,14 @@ void cmd_run(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
} }
fp = fopen(argv[1], "r"); fp = fopen(argv[1], "r");
if ( fp == NULL ) { if ( fp == NULL ) {
error_str = "Path does not exist!"; error_str = "Path does not exist!";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, error_str); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, error_str);
return; return;
} }
run_python(fp, argv[1]); run_python(fp, argv[1]);
fclose(fp); fclose(fp);
} }
@ -154,6 +162,7 @@ void invoke_autoruns(WINDOW *window, ToxWindow *self)
size_t path_len; size_t path_len;
DIR *d = opendir(user_settings->autorun_path); DIR *d = opendir(user_settings->autorun_path);
FILE *fp; FILE *fp;
if (d == NULL) if (d == NULL)
return; return;
@ -162,14 +171,18 @@ void invoke_autoruns(WINDOW *window, ToxWindow *self)
while ((dir = readdir(d)) != NULL) { while ((dir = readdir(d)) != NULL) {
path_len = strlen(dir->d_name); path_len = strlen(dir->d_name);
if (!strcmp(dir->d_name + path_len - 3, ".py")) { if (!strcmp(dir->d_name + path_len - 3, ".py")) {
snprintf(abspath_buf, PATH_MAX + 1, "%s%s", user_settings->autorun_path, dir->d_name); snprintf(abspath_buf, PATH_MAX + 1, "%s%s", user_settings->autorun_path, dir->d_name);
fp = fopen(abspath_buf, "r"); fp = fopen(abspath_buf, "r");
if (fp == NULL) if (fp == NULL)
continue; continue;
run_python(fp, abspath_buf); run_python(fp, abspath_buf);
fclose(fp); fclose(fp);
} }
} }
closedir(d); closedir(d);
} }

View File

@ -26,7 +26,7 @@
#include "friendlist.h" #include "friendlist.h"
#include "windows.h" #include "windows.h"
void api_display(const char * const msg); void api_display(const char *const msg);
FriendsList api_get_friendslist(void); FriendsList api_get_friendslist(void);
char *api_get_nick(void); char *api_get_nick(void);
TOX_USER_STATUS api_get_status(void); TOX_USER_STATUS api_get_status(void);

View File

@ -36,8 +36,10 @@ struct python_registered_func {
static PyObject *python_api_display(PyObject *self, PyObject *args) static PyObject *python_api_display(PyObject *self, PyObject *args)
{ {
const char *msg; const char *msg;
if (!PyArg_ParseTuple(args, "s", &msg)) if (!PyArg_ParseTuple(args, "s", &msg))
return NULL; return NULL;
api_display(msg); api_display(msg);
return Py_None; return Py_None;
} }
@ -46,11 +48,15 @@ static PyObject *python_api_get_nick(PyObject *self, PyObject *args)
{ {
char *name; char *name;
PyObject *ret; PyObject *ret;
if (!PyArg_ParseTuple(args, "")) if (!PyArg_ParseTuple(args, ""))
return NULL; return NULL;
name = api_get_nick(); name = api_get_nick();
if (name == NULL) if (name == NULL)
return NULL; return NULL;
ret = Py_BuildValue("s", name); ret = Py_BuildValue("s", name);
free(name); free(name);
return ret; return ret;
@ -60,8 +66,10 @@ static PyObject *python_api_get_status(PyObject *self, PyObject *args)
{ {
TOX_USER_STATUS status; TOX_USER_STATUS status;
PyObject *ret; PyObject *ret;
if (!PyArg_ParseTuple(args, "")) if (!PyArg_ParseTuple(args, ""))
return NULL; return NULL;
status = api_get_status(); status = api_get_status();
ret = Py_BuildValue("i", status); ret = Py_BuildValue("i", status);
return ret; return ret;
@ -71,11 +79,15 @@ static PyObject *python_api_get_status_message(PyObject *self, PyObject *args)
{ {
char *status; char *status;
PyObject *ret; PyObject *ret;
if (!PyArg_ParseTuple(args, "")) if (!PyArg_ParseTuple(args, ""))
return NULL; return NULL;
status = api_get_status_message(); status = api_get_status_message();
if (status == NULL) if (status == NULL)
return NULL; return NULL;
ret = Py_BuildValue("s", status); ret = Py_BuildValue("s", status);
free(status); free(status);
return ret; return ret;
@ -87,25 +99,32 @@ static PyObject *python_api_get_all_friends(PyObject *self, PyObject *args)
FriendsList friends; FriendsList friends;
PyObject *cur, *ret; PyObject *cur, *ret;
char pubkey_buf[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char pubkey_buf[TOX_PUBLIC_KEY_SIZE * 2 + 1];
if (!PyArg_ParseTuple(args, "")) if (!PyArg_ParseTuple(args, ""))
return NULL; return NULL;
friends = api_get_friendslist(); friends = api_get_friendslist();
ret = PyList_New(0); ret = PyList_New(0);
for (i = 0; i < friends.num_friends; i++) { for (i = 0; i < friends.num_friends; i++) {
for (ii = 0; ii < TOX_PUBLIC_KEY_SIZE; ii++) for (ii = 0; ii < TOX_PUBLIC_KEY_SIZE; ii++)
snprintf(pubkey_buf + ii * 2, 3, "%02X", friends.list[i].pub_key[ii] & 0xff); snprintf(pubkey_buf + ii * 2, 3, "%02X", friends.list[i].pub_key[ii] & 0xff);
pubkey_buf[TOX_PUBLIC_KEY_SIZE * 2] = '\0'; pubkey_buf[TOX_PUBLIC_KEY_SIZE * 2] = '\0';
cur = Py_BuildValue("(s,s)", friends.list[i].name, pubkey_buf); cur = Py_BuildValue("(s,s)", friends.list[i].name, pubkey_buf);
PyList_Append(ret, cur); PyList_Append(ret, cur);
} }
return ret; return ret;
} }
static PyObject *python_api_send(PyObject *self, PyObject *args) static PyObject *python_api_send(PyObject *self, PyObject *args)
{ {
const char *msg; const char *msg;
if (!PyArg_ParseTuple(args, "s", &msg)) if (!PyArg_ParseTuple(args, "s", &msg))
return NULL; return NULL;
api_send(msg); api_send(msg);
return Py_None; return Py_None;
} }
@ -114,8 +133,10 @@ static PyObject *python_api_execute(PyObject *self, PyObject *args)
{ {
int mode; int mode;
const char *command; const char *command;
if (!PyArg_ParseTuple(args, "si", &command, &mode)) if (!PyArg_ParseTuple(args, "si", &command, &mode))
return NULL; return NULL;
api_execute(command, mode); api_execute(command, mode);
return Py_None; return Py_None;
} }
@ -126,16 +147,20 @@ static PyObject *python_api_register(PyObject *self, PyObject *args)
size_t command_len, help_len; size_t command_len, help_len;
const char *command, *help; const char *command, *help;
PyObject *callback; PyObject *callback;
if (!PyArg_ParseTuple(args, "ssO:register_command", &command, &help, &callback)) if (!PyArg_ParseTuple(args, "ssO:register_command", &command, &help, &callback))
return NULL; return NULL;
if (!PyCallable_Check(callback)) { if (!PyCallable_Check(callback)) {
PyErr_SetString(PyExc_TypeError, "Parameter must be callable"); PyErr_SetString(PyExc_TypeError, "Parameter must be callable");
return NULL; return NULL;
} }
if (command[0] != '/') { if (command[0] != '/') {
PyErr_SetString(PyExc_TypeError, "Command must be prefixed with a '/'"); PyErr_SetString(PyExc_TypeError, "Command must be prefixed with a '/'");
return NULL; return NULL;
} }
for (cur = &python_commands; ; cur = cur->next) { for (cur = &python_commands; ; cur = cur->next) {
if (cur->name != NULL && !strcmp(command, cur->name)) { if (cur->name != NULL && !strcmp(command, cur->name)) {
Py_XDECREF(cur->callback); Py_XDECREF(cur->callback);
@ -143,26 +168,34 @@ static PyObject *python_api_register(PyObject *self, PyObject *args)
cur->callback = callback; cur->callback = callback;
break; break;
} }
if (cur->next == NULL) { if (cur->next == NULL) {
Py_XINCREF(callback); Py_XINCREF(callback);
cur->next = malloc(sizeof(struct python_registered_func)); cur->next = malloc(sizeof(struct python_registered_func));
if (cur->next == NULL) if (cur->next == NULL)
return PyErr_NoMemory(); return PyErr_NoMemory();
command_len = strlen(command); command_len = strlen(command);
cur->next->name = malloc(command_len + 1); cur->next->name = malloc(command_len + 1);
if (cur->next->name == NULL) if (cur->next->name == NULL)
return PyErr_NoMemory(); return PyErr_NoMemory();
strncpy(cur->next->name, command, command_len + 1); strncpy(cur->next->name, command, command_len + 1);
help_len = strlen(help); help_len = strlen(help);
cur->next->help = malloc(help_len + 1); cur->next->help = malloc(help_len + 1);
if (cur->next->help == NULL) if (cur->next->help == NULL)
return PyErr_NoMemory(); return PyErr_NoMemory();
strncpy(cur->next->help, help, help_len + 1); strncpy(cur->next->help, help, help_len + 1);
cur->next->callback = callback; cur->next->callback = callback;
cur->next->next = NULL; cur->next->next = NULL;
break; break;
} }
} }
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
@ -195,14 +228,17 @@ PyMODINIT_FUNC PyInit_toxic_api(void)
void terminate_python(void) void terminate_python(void)
{ {
struct python_registered_func *cur, *old; struct python_registered_func *cur, *old;
if (python_commands.name != NULL) if (python_commands.name != NULL)
free(python_commands.name); free(python_commands.name);
for (cur = python_commands.next; cur != NULL;) { for (cur = python_commands.next; cur != NULL;) {
old = cur; old = cur;
cur = cur->next; cur = cur->next;
free(old->name); free(old->name);
free(old); free(old);
} }
Py_FinalizeEx(); Py_FinalizeEx();
} }
@ -226,19 +262,26 @@ int do_python_command(int num_args, char (*args)[MAX_STR_SIZE])
int i; int i;
PyObject *callback_args, *args_strings; PyObject *callback_args, *args_strings;
struct python_registered_func *cur; struct python_registered_func *cur;
for (cur = &python_commands; cur != NULL; cur = cur->next) { for (cur = &python_commands; cur != NULL; cur = cur->next) {
if (cur->name == NULL) if (cur->name == NULL)
continue; continue;
if (!strcmp(args[0], cur->name)) { if (!strcmp(args[0], cur->name)) {
args_strings = PyList_New(0); args_strings = PyList_New(0);
for (i = 1; i < num_args; i++) for (i = 1; i < num_args; i++)
PyList_Append(args_strings, Py_BuildValue("s", args[i])); PyList_Append(args_strings, Py_BuildValue("s", args[i]));
callback_args = PyTuple_Pack(1, args_strings); callback_args = PyTuple_Pack(1, args_strings);
if (PyObject_CallObject(cur->callback, callback_args) == NULL) if (PyObject_CallObject(cur->callback, callback_args) == NULL)
api_display("Exception raised in callback function"); api_display("Exception raised in callback function");
return 0; return 0;
} }
} }
return 1; return 1;
} }
@ -246,10 +289,12 @@ int python_num_registered_handlers(void)
{ {
int n = 0; int n = 0;
struct python_registered_func *cur; struct python_registered_func *cur;
for (cur = &python_commands; cur != NULL; cur = cur->next) { for (cur = &python_commands; cur != NULL; cur = cur->next) {
if (cur->name != NULL) if (cur->name != NULL)
n++; n++;
} }
return n; return n;
} }
@ -258,12 +303,14 @@ int python_help_max_width(void)
size_t tmp; size_t tmp;
int max = 0; int max = 0;
struct python_registered_func *cur; struct python_registered_func *cur;
for (cur = &python_commands; cur != NULL; cur = cur->next) { for (cur = &python_commands; cur != NULL; cur = cur->next) {
if (cur->name != NULL) { if (cur->name != NULL) {
tmp = strlen(cur->help); tmp = strlen(cur->help);
max = tmp > max ? tmp : max; max = tmp > max ? tmp : max;
} }
} }
max = max > 50 ? 50 : max; max = max > 50 ? 50 : max;
return 37 + max; return 37 + max;
} }
@ -271,6 +318,7 @@ int python_help_max_width(void)
void python_draw_handler_help(WINDOW *win) void python_draw_handler_help(WINDOW *win)
{ {
struct python_registered_func *cur; struct python_registered_func *cur;
for (cur = &python_commands; cur != NULL; cur = cur->next) { for (cur = &python_commands; cur != NULL; cur = cur->next) {
if (cur->name != NULL) if (cur->name != NULL)
wprintw(win, " %-29s: %.50s\n", cur->name, cur->help); wprintw(win, " %-29s: %.50s\n", cur->name, cur->help);