2017-05-16 02:17:23 +02:00
|
|
|
/* api.c
|
|
|
|
*
|
|
|
|
*
|
2017-05-17 02:31:23 +02:00
|
|
|
* Copyright (C) 2017 Jakob Kreuze <jakob@memeware.net>
|
2017-05-16 02:17:23 +02:00
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic 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.
|
|
|
|
*
|
|
|
|
* Toxic 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 Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
#include <dirent.h>
|
2017-05-16 04:12:28 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <tox/tox.h>
|
|
|
|
|
|
|
|
#include "execute.h"
|
|
|
|
#include "friendlist.h"
|
2017-05-16 02:17:23 +02:00
|
|
|
#include "line_info.h"
|
2017-05-17 02:31:23 +02:00
|
|
|
#include "message_queue.h"
|
|
|
|
#include "misc_tools.h"
|
|
|
|
#include "settings.h"
|
2017-05-23 02:47:56 +02:00
|
|
|
#include "toxic_strings.h"
|
2017-05-16 02:17:23 +02:00
|
|
|
#include "windows.h"
|
|
|
|
|
2017-06-07 01:03:40 +02:00
|
|
|
#ifdef PYTHON
|
|
|
|
#include "python_api.h"
|
|
|
|
|
2017-05-16 04:12:28 +02:00
|
|
|
Tox *user_tox;
|
|
|
|
static WINDOW *cur_window;
|
|
|
|
static ToxWindow *self_window;
|
|
|
|
|
|
|
|
extern FriendsList Friends;
|
2017-05-17 02:31:23 +02:00
|
|
|
extern struct user_settings *user_settings;
|
2017-05-16 02:17:23 +02:00
|
|
|
|
2017-05-17 05:19:39 +02:00
|
|
|
void api_display(const char *const msg)
|
2017-05-16 02:17:23 +02:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (msg == NULL) {
|
2017-05-16 02:17:23 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
self_window = get_active_window();
|
2017-05-16 04:12:28 +02:00
|
|
|
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);
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (name == NULL) {
|
2017-05-16 04:12:28 +02:00
|
|
|
return NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2017-05-16 04:12:28 +02:00
|
|
|
tox_self_get_name(user_tox, name);
|
2017-05-17 02:31:23 +02:00
|
|
|
name[len] = '\0';
|
2017-05-16 04:12:28 +02:00
|
|
|
return (char *) name;
|
|
|
|
}
|
|
|
|
|
2018-10-10 05:06:57 +02:00
|
|
|
Tox_User_Status api_get_status(void)
|
2017-05-16 04:12:28 +02:00
|
|
|
{
|
|
|
|
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);
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (status == NULL) {
|
2017-05-16 04:12:28 +02:00
|
|
|
return NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2017-05-16 04:12:28 +02:00
|
|
|
tox_self_get_status_message(user_tox, status);
|
2017-05-17 14:37:05 +02:00
|
|
|
status[len] = '\0';
|
2017-05-16 04:12:28 +02:00
|
|
|
return (char *) status;
|
|
|
|
}
|
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
void api_send(const char *msg)
|
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (msg == NULL || self_window->chatwin->cqueue == NULL) {
|
2017-05-17 02:31:23 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
char *name = api_get_nick();
|
|
|
|
char timefrmt[TIME_STR_SIZE];
|
2017-05-19 21:35:31 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (name == NULL) {
|
2017-05-19 21:35:31 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2017-05-19 21:35:31 +02:00
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
self_window = get_active_window();
|
2017-05-17 14:37:05 +02:00
|
|
|
get_time_str(timefrmt, sizeof(timefrmt));
|
|
|
|
|
2017-05-23 02:47:56 +02:00
|
|
|
strncpy((char *) self_window->chatwin->line, msg, sizeof(self_window->chatwin->line));
|
|
|
|
add_line_to_hist(self_window->chatwin);
|
2017-06-01 22:46:12 +02:00
|
|
|
int id = line_info_add(self_window, timefrmt, name, NULL, OUT_MSG, 0, 0, "%s", msg);
|
|
|
|
cqueue_add(self_window->chatwin->cqueue, msg, strlen(msg), OUT_MSG, id);
|
2017-05-17 14:37:05 +02:00
|
|
|
free(name);
|
2017-05-17 02:31:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-16 04:12:28 +02:00
|
|
|
void api_execute(const char *input, int mode)
|
|
|
|
{
|
2017-05-17 02:31:23 +02:00
|
|
|
self_window = get_active_window();
|
2017-05-16 04:12:28 +02:00
|
|
|
execute(cur_window, self_window, user_tox, input, mode);
|
2017-05-16 02:17:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
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);
|
|
|
|
}
|
2017-05-16 04:12:28 +02:00
|
|
|
|
2017-05-16 02:17:23 +02:00
|
|
|
void cmd_run(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
const char *error_str;
|
|
|
|
|
2017-05-16 04:12:28 +02:00
|
|
|
cur_window = window;
|
|
|
|
self_window = self;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (argc != 1) {
|
|
|
|
if (argc < 1) {
|
2018-10-08 06:47:51 +02:00
|
|
|
error_str = "Path must be specified.";
|
2018-07-18 17:33:16 +02:00
|
|
|
} else {
|
2018-10-08 06:47:51 +02:00
|
|
|
error_str = "Only one argument allowed.";
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2017-05-16 02:17:23 +02:00
|
|
|
|
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, error_str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(argv[1], "r");
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (fp == NULL) {
|
2018-10-08 06:47:51 +02:00
|
|
|
error_str = "Path does not exist.";
|
2017-05-16 02:17:23 +02:00
|
|
|
|
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, error_str);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2017-05-16 02:17:23 +02:00
|
|
|
run_python(fp, argv[1]);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2017-05-17 02:31:23 +02:00
|
|
|
|
|
|
|
void invoke_autoruns(WINDOW *window, ToxWindow *self)
|
|
|
|
{
|
2020-11-19 20:21:45 +01:00
|
|
|
char abspath_buf[PATH_MAX + 256];
|
|
|
|
char err_buf[PATH_MAX + 128];
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (user_settings->autorun_path[0] == '\0') {
|
2017-05-24 01:12:53 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2017-05-24 01:12:53 +02:00
|
|
|
|
2020-11-19 20:21:45 +01:00
|
|
|
DIR *d = opendir(user_settings->autorun_path);
|
2017-05-24 01:12:53 +02:00
|
|
|
|
2017-05-19 03:37:28 +02:00
|
|
|
if (d == NULL) {
|
2020-11-19 20:21:45 +01:00
|
|
|
snprintf(err_buf, sizeof(err_buf), "Autorun path does not exist: %s", user_settings->autorun_path);
|
2017-05-19 03:37:28 +02:00
|
|
|
api_display(err_buf);
|
2017-05-17 02:31:23 +02:00
|
|
|
return;
|
2017-05-19 03:37:28 +02:00
|
|
|
}
|
2017-05-17 02:31:23 +02:00
|
|
|
|
2020-11-19 20:21:45 +01:00
|
|
|
struct dirent *dir = NULL;
|
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
cur_window = window;
|
2020-11-19 20:21:45 +01:00
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
self_window = self;
|
|
|
|
|
|
|
|
while ((dir = readdir(d)) != NULL) {
|
2020-11-19 20:21:45 +01:00
|
|
|
size_t path_len = strlen(dir->d_name);
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
if (!strcmp(dir->d_name + path_len - 3, ".py")) {
|
2020-11-19 20:21:45 +01:00
|
|
|
snprintf(abspath_buf, sizeof(abspath_buf), "%s%s", user_settings->autorun_path, dir->d_name);
|
|
|
|
FILE *fp = fopen(abspath_buf, "r");
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2017-05-19 03:37:28 +02:00
|
|
|
if (fp == NULL) {
|
2020-11-19 20:21:45 +01:00
|
|
|
snprintf(err_buf, sizeof(err_buf), "Invalid path: %s", abspath_buf);
|
2017-05-19 03:37:28 +02:00
|
|
|
api_display(err_buf);
|
2017-05-17 02:31:23 +02:00
|
|
|
continue;
|
2017-05-19 03:37:28 +02:00
|
|
|
}
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
run_python(fp, abspath_buf);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 05:19:39 +02:00
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
closedir(d);
|
|
|
|
}
|
2018-02-26 08:34:38 +01:00
|
|
|
#endif /* PYTHON */
|