1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00
profanity/src/plugins/api.c

463 lines
12 KiB
C
Raw Normal View History

/*
* api.c
*
2016-02-14 18:12:01 -05: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-23 20:14:49 -04: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>
#include <string.h>
#include <assert.h>
#include <glib.h>
2016-07-24 10:43:51 -04:00
#include "profanity.h"
#include "log.h"
2016-07-24 10:43:51 -04:00
#include "common.h"
#include "config/theme.h"
#include "command/cmd_defs.h"
#include "event/server_events.h"
#include "event/client_events.h"
#include "plugins/callbacks.h"
#include "plugins/autocompleters.h"
2016-02-21 18:58:13 -05:00
#include "plugins/themes.h"
2016-03-25 18:06:24 -04:00
#include "plugins/settings.h"
#include "plugins/disco.h"
#include "ui/ui.h"
2016-07-24 10:14:46 -04:00
#include "ui/window_list.h"
void
api_cons_alert(void)
{
cons_alert();
}
int
api_cons_show(const char * const message)
{
if (message == NULL) {
log_warning("%s", "prof_cons_show failed, message is NULL");
return 0;
}
char *parsed = str_replace(message, "\r\n", "\n");
cons_show("%s", parsed);
free(parsed);
return 1;
}
2016-02-21 18:58:13 -05:00
int
api_cons_show_themed(const char *const group, const char *const key, const char *const def, const char *const message)
{
if (message == NULL) {
log_warning("%s", "prof_cons_show_themed failed, message is NULL");
return 0;
}
char *parsed = str_replace(message, "\r\n", "\n");
theme_item_t themeitem = plugin_themes_get(group, key, def);
ProfWin *console = wins_get_console();
win_print(console, '-', 0, NULL, 0, themeitem, "", parsed);
free(parsed);
return 1;
}
int
api_cons_bad_cmd_usage(const char *const cmd)
{
if (cmd == NULL) {
log_warning("%s", "prof_cons_bad_cmd_usage failed, cmd is NULL");
return 0;
}
cons_bad_cmd_usage(cmd);
return 1;
}
void
2016-06-30 17:54:46 -04:00
api_register_command(const char *const plugin_name, const char *command_name, int min_args, int max_args,
2016-07-03 19:41:29 -04:00
const char **synopsis, const char *description, const char *arguments[][2], const char **examples,
void *callback, void(*callback_exec)(PluginCommand *command, gchar **args), void(*callback_destroy)(void *callback))
{
PluginCommand *command = malloc(sizeof(PluginCommand));
2016-07-03 19:41:29 -04:00
command->command_name = strdup(command_name);
command->min_args = min_args;
command->max_args = max_args;
command->callback = callback;
command->callback_exec = callback_exec;
2016-07-03 19:41:29 -04:00
command->callback_destroy = callback_destroy;
2016-02-17 20:20:17 -05:00
CommandHelp *help = malloc(sizeof(CommandHelp));
2016-07-03 19:41:29 -04:00
help->tags[0] = NULL;
2016-02-17 20:20:17 -05:00
int i = 0;
for (i = 0; synopsis[i] != NULL; i++) {
help->synopsis[i] = strdup(synopsis[i]);
}
help->synopsis[i] = NULL;
help->desc = strdup(description);
for (i = 0; arguments[i][0] != NULL; i++) {
help->args[i][0] = strdup(arguments[i][0]);
help->args[i][1] = strdup(arguments[i][1]);
}
help->args[i][0] = NULL;
help->args[i][1] = NULL;
for (i = 0; examples[i] != NULL; i++) {
help->examples[i] = strdup(examples[i]);
}
help->examples[i] = NULL;
command->help = help;
2016-07-03 19:41:29 -04:00
callbacks_add_command(plugin_name, command);
}
void
2016-06-30 17:54:46 -04:00
api_register_timed(const char *const plugin_name, void *callback, int interval_seconds,
2016-07-03 19:41:29 -04:00
void (*callback_exec)(PluginTimedFunction *timed_function), void(*callback_destroy)(void *callback))
{
PluginTimedFunction *timed_function = malloc(sizeof(PluginTimedFunction));
timed_function->callback = callback;
timed_function->callback_exec = callback_exec;
2016-07-03 19:41:29 -04:00
timed_function->callback_destroy = callback_destroy;
timed_function->interval_seconds = interval_seconds;
timed_function->timer = g_timer_new();
callbacks_add_timed(plugin_name, timed_function);
}
void
2016-06-30 17:54:46 -04:00
api_completer_add(const char *const plugin_name, const char *key, char **items)
{
2016-07-05 16:46:36 -04:00
autocompleters_add(plugin_name, key, items);
}
2016-04-07 16:25:12 -04:00
void
api_completer_remove(const char *const plugin_name, const char *key, char **items)
2016-04-07 16:25:12 -04:00
{
2016-07-05 16:46:36 -04:00
autocompleters_remove(plugin_name, key, items);
2016-04-07 16:25:12 -04:00
}
2016-04-07 18:25:47 -04:00
void
api_completer_clear(const char *const plugin_name, const char *key)
2016-04-07 18:25:47 -04:00
{
2016-07-05 16:46:36 -04:00
autocompleters_clear(plugin_name, key);
2016-04-07 18:25:47 -04:00
}
void
api_notify(const char *message, const char *category, int timeout_ms)
{
notify(message, timeout_ms, category);
}
void
api_send_line(char *line)
{
ProfWin *current = wins_get_current();
cmd_process_input(current, line);
}
char *
api_get_current_recipient(void)
{
ProfWin *current = wins_get_current();
if (current->type == WIN_CHAT) {
ProfChatWin *chatwin = (ProfChatWin*)current;
assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
return chatwin->barejid;
} else {
return NULL;
}
}
char *
api_get_current_muc(void)
{
ProfWin *current = wins_get_current();
if (current->type == WIN_MUC) {
ProfMucWin *mucwin = (ProfMucWin*)current;
assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
return mucwin->roomjid;
} else {
return NULL;
}
}
2016-04-15 17:24:50 -04:00
char *
api_get_current_nick(void)
{
ProfWin *current = wins_get_current();
if (current->type == WIN_MUC) {
ProfMucWin *mucwin = (ProfMucWin*)current;
assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
return muc_nick(mucwin->roomjid);
} else {
return NULL;
}
}
char**
api_get_current_occupants(void)
{
ProfWin *current = wins_get_current();
if (current->type == WIN_MUC) {
ProfMucWin *mucwin = (ProfMucWin*)current;
assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
GList *occupants_list = muc_roster(mucwin->roomjid);
char **result = malloc((g_list_length(occupants_list) + 1) * sizeof(char*));
GList *curr = occupants_list;
int i = 0;
while (curr) {
Occupant *occupant = curr->data;
result[i++] = strdup(occupant->nick);
curr = g_list_next(curr);
}
result[i] = NULL;
return result;
} else {
return NULL;
}
}
int
api_current_win_is_console(void)
{
ProfWin *current = wins_get_current();
if (current && current->type == WIN_CONSOLE) {
return 1;
} else {
return 0;
}
}
void
api_log_debug(const char *message)
{
log_debug("%s", message);
}
void
api_log_info(const char *message)
{
log_info("%s", message);
}
void
api_log_warning(const char *message)
{
log_warning("%s", message);
}
void
api_log_error(const char *message)
{
log_error("%s", message);
}
int
api_win_exists(const char *tag)
{
return (wins_get_plugin(tag) != NULL);
}
void
api_win_create(
2016-07-02 19:48:22 -04:00
const char *const plugin_name,
const char *tag,
void *callback,
void(*callback_exec)(PluginWindowCallback *window_callback, const char *tag, const char * const line),
void(*callback_destroy)(void *callback))
{
2016-07-11 20:51:27 -04:00
if (callbacks_win_exists(plugin_name, tag)) {
2016-07-12 19:25:45 -04:00
if (callback_destroy) {
callback_destroy(callback);
}
2016-07-11 20:51:27 -04:00
return;
}
PluginWindowCallback *window = malloc(sizeof(PluginWindowCallback));
window->callback = callback;
window->callback_exec = callback_exec;
window->callback_destroy = callback_destroy;
callbacks_add_window_handler(plugin_name, tag, window);
2016-07-11 21:16:12 -04:00
wins_new_plugin(plugin_name, tag);
// set status bar active
ProfPluginWin *pluginwin = wins_get_plugin(tag);
int num = wins_get_num((ProfWin*)pluginwin);
status_bar_active(num);
}
int
api_win_focus(const char *tag)
{
if (tag == NULL) {
log_warning("%s", "prof_win_focus failed, tag is NULL");
return 0;
}
ProfPluginWin *pluginwin = wins_get_plugin(tag);
if (pluginwin == NULL) {
log_warning("prof_win_focus failed, no window with tag: %s", tag);
return 0;
}
ui_focus_win((ProfWin*)pluginwin);
return 1;
}
int
api_win_show(const char *tag, const char *line)
{
if (tag == NULL) {
log_warning("%s", "prof_win_show failed, tag is NULL");
return 0;
}
if (line == NULL) {
log_warning("%s", "prof_win_show failed, line is NULL");
return 0;
}
ProfPluginWin *pluginwin = wins_get_plugin(tag);
if (pluginwin == NULL) {
log_warning("prof_win_show failed, no window with tag: %s", tag);
return 0;
}
ProfWin *window = (ProfWin*)pluginwin;
win_print(window, '!', 0, NULL, 0, 0, "", line);
return 1;
}
int
2016-02-21 18:58:13 -05:00
api_win_show_themed(const char *tag, const char *const group, const char *const key, const char *const def, const char *line)
{
if (tag == NULL) {
2016-02-21 18:58:13 -05:00
log_warning("%s", "prof_win_show_themed failed, tag is NULL");
return 0;
}
if (line == NULL) {
2016-02-21 18:58:13 -05:00
log_warning("%s", "prof_win_show_themed failed, line is NULL");
return 0;
}
ProfPluginWin *pluginwin = wins_get_plugin(tag);
if (pluginwin == NULL) {
2016-02-21 18:58:13 -05:00
log_warning("prof_win_show_themed failed, no window with tag: %s", tag);
return 0;
}
2016-02-21 18:58:13 -05:00
theme_item_t themeitem = plugin_themes_get(group, key, def);
ProfWin *window = (ProfWin*)pluginwin;
2016-02-21 18:58:13 -05:00
win_print(window, '!', 0, NULL, 0, themeitem, "", line);
return 1;
}
2016-03-23 18:57:03 -04:00
int
api_send_stanza(const char *const stanza)
{
2016-05-05 20:51:58 -04:00
return connection_send_stanza(stanza);
2016-03-23 18:57:03 -04:00
}
2016-03-25 18:06:24 -04:00
gboolean
api_settings_get_boolean(const char *const group, const char *const key, gboolean def)
{
return plugin_settings_get_boolean(group, key, def);
}
void
api_settings_set_boolean(const char *const group, const char *const key, gboolean value)
{
plugin_settings_set_boolean(group, key, value);
}
2016-03-25 21:48:42 -04:00
char*
api_settings_get_string(const char *const group, const char *const key, const char *const def)
{
return plugin_settings_get_string(group, key, def);
}
void
api_settings_set_string(const char *const group, const char *const key, const char *const value)
{
plugin_settings_set_string(group, key, value);
}
2016-03-25 22:19:30 -04:00
int
api_settings_get_int(const char *const group, const char *const key, int def)
{
return plugin_settings_get_int(group, key, def);
}
void
api_settings_set_int(const char *const group, const char *const key, int value)
{
plugin_settings_set_int(group, key, value);
}
void
api_incoming_message(const char *const barejid, const char *const resource, const char *const message)
{
sv_ev_incoming_message((char*)barejid, (char*)resource, (char*)message, NULL, NULL);
// TODO handle all states
sv_ev_activity((char*)barejid, (char*)resource, FALSE);
}
void
api_disco_add_feature(char *feature)
{
if (feature == NULL) {
return;
}
disco_add_feature(feature);
caps_reset_ver();
// resend presence to update server's disco info data for this client
2016-05-05 18:51:49 -04:00
if (connection_get_status() == JABBER_CONNECTED) {
2016-05-05 19:53:03 -04:00
resource_presence_t last_presence = accounts_get_last_presence(session_get_account_name());
2016-05-05 18:51:49 -04:00
cl_ev_presence_send(last_presence, connection_get_presence_msg(), 0);
}
}