mirror of
https://github.com/Tha14/toxic.git
synced 2025-06-29 06:56:44 +02:00
Finalized and documented the Python scripting interface.
This commit is contained in:
72
src/api.c
72
src/api.c
@ -1,7 +1,7 @@
|
||||
/* api.c
|
||||
*
|
||||
*
|
||||
* Copyright (C) 2017 Toxic All Rights Reserved.
|
||||
* Copyright (C) 2017 Jakob Kreuze <jakob@memeware.net>
|
||||
*
|
||||
* This file is part of Toxic.
|
||||
*
|
||||
@ -20,6 +20,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <dirent.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <tox/tox.h>
|
||||
@ -27,7 +28,10 @@
|
||||
#include "execute.h"
|
||||
#include "friendlist.h"
|
||||
#include "line_info.h"
|
||||
#include "message_queue.h"
|
||||
#include "misc_tools.h"
|
||||
#include "python_api.h"
|
||||
#include "settings.h"
|
||||
#include "windows.h"
|
||||
|
||||
Tox *user_tox;
|
||||
@ -35,12 +39,13 @@ static WINDOW *cur_window;
|
||||
static ToxWindow *self_window;
|
||||
|
||||
extern FriendsList Friends;
|
||||
extern struct user_settings *user_settings;
|
||||
|
||||
void api_display(const char * const msg)
|
||||
{
|
||||
if (msg == NULL)
|
||||
return;
|
||||
|
||||
self_window = get_active_window();
|
||||
line_info_add(self_window, NULL, NULL, NULL, SYS_MSG, 0, 0, msg);
|
||||
}
|
||||
|
||||
@ -56,6 +61,7 @@ char *api_get_nick(void)
|
||||
if (name == NULL)
|
||||
return NULL;
|
||||
tox_self_get_name(user_tox, name);
|
||||
name[len] = '\0';
|
||||
return (char *) name;
|
||||
}
|
||||
|
||||
@ -74,12 +80,45 @@ char *api_get_status_message(void)
|
||||
return (char *) status;
|
||||
}
|
||||
|
||||
void api_send(const char *msg)
|
||||
{
|
||||
if (msg == NULL || self_window->chatwin->cqueue == NULL)
|
||||
return;
|
||||
char *name = api_get_nick();
|
||||
char timefrmt[TIME_STR_SIZE];
|
||||
get_time_str(timefrmt, sizeof(timefrmt));
|
||||
self_window = get_active_window();
|
||||
line_info_add(self_window, timefrmt, name, NULL, OUT_MSG, 0, 0, "%s", msg);
|
||||
free(name);
|
||||
cqueue_add(self_window->chatwin->cqueue, msg, strlen(msg), OUT_MSG,
|
||||
self_window->chatwin->hst->line_end->id + 1);
|
||||
}
|
||||
|
||||
void api_execute(const char *input, int mode)
|
||||
{
|
||||
self_window = get_active_window();
|
||||
execute(cur_window, self_window, user_tox, input, mode);
|
||||
}
|
||||
|
||||
/* TODO: Register command */
|
||||
int do_plugin_command(int num_args, char (*args)[MAX_STR_SIZE])
|
||||
{
|
||||
return do_python_command(num_args, args);
|
||||
}
|
||||
|
||||
int num_registered_handlers(void)
|
||||
{
|
||||
return python_num_registered_handlers();
|
||||
}
|
||||
|
||||
int help_max_width(void)
|
||||
{
|
||||
return python_help_max_width();
|
||||
}
|
||||
|
||||
void draw_handler_help(WINDOW *win)
|
||||
{
|
||||
python_draw_handler_help(win);
|
||||
}
|
||||
|
||||
void cmd_run(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
||||
{
|
||||
@ -107,3 +146,30 @@ void cmd_run(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
|
||||
run_python(fp, argv[1]);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void invoke_autoruns(WINDOW *window, ToxWindow *self)
|
||||
{
|
||||
struct dirent *dir;
|
||||
char abspath_buf[PATH_MAX + 1];
|
||||
size_t path_len;
|
||||
DIR *d = opendir(user_settings->autorun_path);
|
||||
FILE *fp;
|
||||
if (d == NULL)
|
||||
return;
|
||||
|
||||
cur_window = window;
|
||||
self_window = self;
|
||||
|
||||
while ((dir = readdir(d)) != NULL) {
|
||||
path_len = strlen(dir->d_name);
|
||||
if (!strcmp(dir->d_name + path_len - 3, ".py")) {
|
||||
snprintf(abspath_buf, PATH_MAX + 1, "%s%s", user_settings->autorun_path, dir->d_name);
|
||||
fp = fopen(abspath_buf, "r");
|
||||
if (fp == NULL)
|
||||
continue;
|
||||
run_python(fp, abspath_buf);
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
Reference in New Issue
Block a user