1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-06-23 21:45:30 +00:00
profanity/src/plugins/c_api.c

426 lines
11 KiB
C
Raw Normal View History

/*
* c_api.c
*
2016-02-14 23:12:01 +00:00
* Copyright (C) 2012 - 2016 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
2016-07-24 00:14:49 +00:00
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
*/
#include <stdlib.h>
2016-07-24 00:19:59 +00:00
#include <string.h>
#include <glib.h>
#include "log.h"
#include "plugins/api.h"
#include "plugins/c_api.h"
#include "plugins/callbacks.h"
#include "plugins/profapi.h"
typedef struct command_wrapper_t {
void(*func)(char **args);
} CommandWrapper;
typedef struct timed_wrapper_t {
void(*func)(void);
} TimedWrapper;
typedef struct window_wrapper_t {
void(*func)(char *tag, char *line);
} WindowWrapper;
2016-06-22 23:26:50 +00:00
static char* _c_plugin_name(const char *filename);
static void
c_api_cons_alert(void)
{
api_cons_alert();
}
static int
c_api_cons_show(const char * const message)
{
return api_cons_show(message);
}
2016-02-21 23:58:13 +00:00
static int
c_api_cons_show_themed(const char *const group, const char *const item, const char *const def, const char *const message)
{
return api_cons_show_themed(group, item, def, message);
}
static int
c_api_cons_bad_cmd_usage(const char *const cmd)
{
return api_cons_bad_cmd_usage(cmd);
}
static void
c_api_register_command(const char *filename, const char *command_name, int min_args, int max_args,
char **synopsis, const char *description, char *arguments[][2], char **examples,
2016-02-18 01:20:17 +00:00
void(*callback)(char **args))
{
2016-06-22 23:26:50 +00:00
char *plugin_name = _c_plugin_name(filename);
2016-06-22 23:36:26 +00:00
log_debug("Register command %s for %s", command_name, plugin_name);
2016-06-22 23:26:50 +00:00
CommandWrapper *wrapper = malloc(sizeof(CommandWrapper));
wrapper->func = callback;
2016-06-30 21:54:46 +00:00
api_register_command(plugin_name, command_name, min_args, max_args, synopsis,
2016-07-03 23:41:29 +00:00
description, arguments, examples, wrapper, c_command_callback, free);
free(plugin_name);
}
static void
2016-06-22 23:36:26 +00:00
c_api_register_timed(const char *filename, void(*callback)(void), int interval_seconds)
{
2016-06-22 23:36:26 +00:00
char *plugin_name = _c_plugin_name(filename);
log_debug("Register timed for %s", plugin_name);
TimedWrapper *wrapper = malloc(sizeof(TimedWrapper));
wrapper->func = callback;
2016-07-03 23:41:29 +00:00
api_register_timed(plugin_name, wrapper, interval_seconds, c_timed_callback, free);
free(plugin_name);
}
static void
2016-06-22 23:44:52 +00:00
c_api_completer_add(const char *filename, const char *key, char **items)
{
2016-06-22 23:44:52 +00:00
char *plugin_name = _c_plugin_name(filename);
log_debug("Autocomplete add %s for %s", key, plugin_name);
2016-06-30 21:54:46 +00:00
api_completer_add(plugin_name, key, items);
2016-07-03 23:41:29 +00:00
free(plugin_name);
}
2016-04-07 20:25:12 +00:00
static void
c_api_completer_remove(const char *filename, const char *key, char **items)
2016-04-07 20:25:12 +00:00
{
char *plugin_name = _c_plugin_name(filename);
log_debug("Autocomplete remove %s for %s", key, plugin_name);
api_completer_remove(plugin_name, key, items);
free(plugin_name);
2016-04-07 20:25:12 +00:00
}
2016-04-07 22:25:47 +00:00
static void
c_api_completer_clear(const char *filename, const char *key)
2016-04-07 22:25:47 +00:00
{
char *plugin_name = _c_plugin_name(filename);
log_debug("Autocomplete clear %s for %s", key, plugin_name);
api_completer_clear(plugin_name, key);
free(plugin_name);
2016-04-07 22:25:47 +00:00
}
static void
c_api_filepath_completer_add(const char *filename, const char *prefix)
{
char *plugin_name = _c_plugin_name(filename);
log_debug("Filepath autocomplete added '%s' for %s", prefix, plugin_name);
api_filepath_completer_add(plugin_name, prefix);
free(plugin_name);
}
static void
c_api_notify(const char *message, int timeout_ms, const char *category)
{
api_notify(message, category, timeout_ms);
}
static void
c_api_send_line(char *line)
{
api_send_line(line);
}
static char *
c_api_get_current_recipient(void)
{
return api_get_current_recipient();
}
static char *
c_api_get_current_muc(void)
{
return api_get_current_muc();
}
static int
2016-04-15 21:24:50 +00:00
c_api_current_win_is_console(void)
{
return api_current_win_is_console();
}
2016-04-15 21:24:50 +00:00
static char*
c_api_get_current_nick(void)
{
return api_get_current_nick();
}
static char**
c_api_get_current_occupants(void)
{
return api_get_current_occupants();
}
2016-11-06 23:01:16 +00:00
static char*
c_api_get_room_nick(const char *barejid)
{
return api_get_room_nick(barejid);
}
static void
c_api_log_debug(const char *message)
{
api_log_debug(message);
}
static void
c_api_log_info(const char *message)
{
api_log_info(message);
}
static void
c_api_log_warning(const char *message)
{
api_log_warning(message);
}
static void
c_api_log_error(const char *message)
{
api_log_error(message);
}
2016-02-21 23:58:13 +00:00
static int
c_api_win_exists(char *tag)
{
return api_win_exists(tag);
}
2016-02-21 23:58:13 +00:00
static void
2016-07-02 23:48:22 +00:00
c_api_win_create(const char *filename, char *tag, void(*callback)(char *tag, char *line))
{
2016-07-02 23:48:22 +00:00
char *plugin_name = _c_plugin_name(filename);
WindowWrapper *wrapper = malloc(sizeof(WindowWrapper));
wrapper->func = callback;
2016-07-03 23:41:29 +00:00
api_win_create(plugin_name, tag, wrapper, c_window_callback, free);
free(plugin_name);
}
2016-02-21 23:58:13 +00:00
static int
c_api_win_focus(char *tag)
{
return api_win_focus(tag);
}
2016-02-21 23:58:13 +00:00
static int
c_api_win_show(char *tag, char *line)
{
return api_win_show(tag, line);
}
2016-02-21 23:58:13 +00:00
static int
c_api_win_show_themed(char *tag, char *group, char *key, char *def, char *line)
{
2016-02-21 23:58:13 +00:00
return api_win_show_themed(tag, group, key, def, line);
}
2016-03-23 22:57:03 +00:00
static int
c_api_send_stanza(char *stanza)
{
return api_send_stanza(stanza);
}
2016-03-25 22:06:24 +00:00
static int
2016-08-06 23:16:57 +00:00
c_api_settings_boolean_get(char *group, char *key, int def)
2016-03-25 22:06:24 +00:00
{
2016-08-06 23:16:57 +00:00
return api_settings_boolean_get(group, key, def);
2016-03-25 22:06:24 +00:00
}
static void
2016-08-06 23:16:57 +00:00
c_api_settings_boolean_set(char *group, char *key, int value)
2016-03-25 22:06:24 +00:00
{
2016-08-06 23:16:57 +00:00
api_settings_boolean_set(group, key, value);
2016-03-25 22:06:24 +00:00
}
2016-03-26 01:48:42 +00:00
static char*
2016-08-06 23:16:57 +00:00
c_api_settings_string_get(char *group, char *key, char *def)
2016-03-26 01:48:42 +00:00
{
2016-08-06 23:16:57 +00:00
return api_settings_string_get(group, key, def);
2016-03-26 01:48:42 +00:00
}
static void
2016-08-06 23:16:57 +00:00
c_api_settings_string_set(char *group, char *key, char *value)
2016-03-26 01:48:42 +00:00
{
2016-08-06 23:16:57 +00:00
api_settings_string_set(group, key, value);
2016-03-26 01:48:42 +00:00
}
2016-08-01 20:34:38 +00:00
static char**
2016-08-06 23:16:57 +00:00
c_api_settings_string_list_get(char *group, char *key)
2016-08-01 20:34:38 +00:00
{
2016-08-06 23:16:57 +00:00
return api_settings_string_list_get(group, key);
2016-08-01 20:34:38 +00:00
}
static void
c_api_settings_string_list_add(char *group, char *key, char* value)
{
api_settings_string_list_add(group, key, value);
}
static int
c_api_settings_string_list_remove(char *group, char *key, char *value)
{
return api_settings_string_list_remove(group, key, value);
}
static int
2016-08-06 23:16:57 +00:00
c_api_settings_string_list_clear(char *group, char *key)
2016-08-01 20:34:38 +00:00
{
2016-08-06 23:16:57 +00:00
return api_settings_string_list_clear(group, key);
2016-08-01 20:34:38 +00:00
}
2016-03-26 02:19:30 +00:00
static int
2016-08-06 23:16:57 +00:00
c_api_settings_int_get(char *group, char *key, int def)
2016-03-26 02:19:30 +00:00
{
2016-08-06 23:16:57 +00:00
return api_settings_int_get(group, key, def);
2016-03-26 02:19:30 +00:00
}
static void
2016-08-06 23:16:57 +00:00
c_api_settings_int_set(char *group, char *key, int value)
2016-03-26 02:19:30 +00:00
{
2016-08-06 23:16:57 +00:00
api_settings_int_set(group, key, value);
2016-03-26 02:19:30 +00:00
}
static void
c_api_incoming_message(char *barejid, char *resource, char *message)
{
api_incoming_message(barejid, resource, message);
}
static void
c_api_disco_add_feature(const char *filename, char *feature)
{
char *plugin_name = _c_plugin_name(filename);
api_disco_add_feature(plugin_name, feature);
free(plugin_name);
}
void
c_command_callback(PluginCommand *command, gchar **args)
{
CommandWrapper *wrapper = command->callback;
void(*f)(gchar **args) = wrapper->func;
f(args);
}
void
c_timed_callback(PluginTimedFunction *timed_function)
{
TimedWrapper *wrapper = timed_function->callback;
void(*f)(void) = wrapper->func;
f();
}
void
c_window_callback(PluginWindowCallback *window_callback, char *tag, char *line)
{
WindowWrapper *wrapper = window_callback->callback;
void(*f)(char *tag, char *line) = wrapper->func;
f(tag, line);
}
void
c_api_init(void)
{
prof_cons_alert = c_api_cons_alert;
prof_cons_show = c_api_cons_show;
2016-02-21 23:58:13 +00:00
prof_cons_show_themed = c_api_cons_show_themed;
prof_cons_bad_cmd_usage = c_api_cons_bad_cmd_usage;
2016-06-22 23:26:50 +00:00
_prof_register_command = c_api_register_command;
2016-06-22 23:36:26 +00:00
_prof_register_timed = c_api_register_timed;
2016-06-22 23:44:52 +00:00
_prof_completer_add = c_api_completer_add;
_prof_completer_remove = c_api_completer_remove;
_prof_completer_clear = c_api_completer_clear;
_prof_filepath_completer_add = c_api_filepath_completer_add;
2016-07-02 23:48:22 +00:00
_prof_win_create = c_api_win_create;
prof_notify = c_api_notify;
prof_send_line = c_api_send_line;
prof_get_current_recipient = c_api_get_current_recipient;
prof_get_current_muc = c_api_get_current_muc;
prof_current_win_is_console = c_api_current_win_is_console;
2016-04-15 21:24:50 +00:00
prof_get_current_nick = c_api_get_current_nick;
prof_get_current_occupants = c_api_get_current_occupants;
2016-11-06 23:01:16 +00:00
prof_get_room_nick = c_api_get_room_nick;
prof_log_debug = c_api_log_debug;
prof_log_info = c_api_log_info;
prof_log_warning = c_api_log_warning;
prof_log_error = c_api_log_error;
prof_win_exists = c_api_win_exists;
prof_win_focus = c_api_win_focus;
prof_win_show = c_api_win_show;
2016-02-21 23:58:13 +00:00
prof_win_show_themed = c_api_win_show_themed;
2016-03-23 22:57:03 +00:00
prof_send_stanza = c_api_send_stanza;
2016-08-06 23:16:57 +00:00
prof_settings_boolean_get = c_api_settings_boolean_get;
prof_settings_boolean_set = c_api_settings_boolean_set;
prof_settings_string_get = c_api_settings_string_get;
prof_settings_string_set = c_api_settings_string_set;
prof_settings_int_get = c_api_settings_int_get;
prof_settings_int_set = c_api_settings_int_set;
prof_settings_string_list_get = c_api_settings_string_list_get;
2016-08-01 20:34:38 +00:00
prof_settings_string_list_add = c_api_settings_string_list_add;
prof_settings_string_list_remove = c_api_settings_string_list_remove;
2016-08-06 23:16:57 +00:00
prof_settings_string_list_clear = c_api_settings_string_list_clear;
prof_incoming_message = c_api_incoming_message;
_prof_disco_add_feature = c_api_disco_add_feature;
}
2016-06-22 23:26:50 +00:00
static char *
_c_plugin_name(const char *filename)
{
GString *plugin_name_str = g_string_new("");
2016-07-27 20:32:44 +00:00
gchar *name = g_strndup(filename, strlen(filename)-1);
2016-06-22 23:26:50 +00:00
g_string_append(plugin_name_str, name);
2016-07-27 20:32:44 +00:00
g_free(name);
2016-06-22 23:26:50 +00:00
g_string_append(plugin_name_str, "so");
char *result = plugin_name_str->str;
g_string_free(plugin_name_str, FALSE);
return result;
}