about summary refs log tree commit diff stats
path: root/src/plugins/profapi.c
blob: 77a1e26ab90cdcec9e4ab28d3eef75c2af0b2e90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * prof_api.c
 * vim: expandtab:ts=4:sts=4:sw=4
 *
 * Copyright (C) 2012 - 2019 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
 * 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 "plugins/profapi.h"
#include "plugins/callbacks.h"

void (*prof_cons_alert)(void) = NULL;
int (*prof_cons_show)(const char * const message) = NULL;
int (*prof_cons_show_themed)(const char *const group, const char *const item, const char *const def, const char *const message) = NULL;
int (*prof_cons_bad_cmd_usage)(const char *const cmd) = NULL;

void (*_prof_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,
    CMD_CB callback) = NULL;

void (*_prof_register_timed)(const char *filename, TIMED_CB callback, int interval_seconds) = NULL;

void (*_prof_completer_add)(const char *filename, const char *key, char **items) = NULL;
void (*_prof_completer_remove)(const char *filename, const char *key, char **items) = NULL;
void (*_prof_completer_clear)(const char *filename, const char *key) = NULL;
void (*_prof_filepath_completer_add)(const char *filename, const char *prefix) = NULL;

void (*prof_notify)(const char *message, int timeout_ms, const char *category) = NULL;

void (*prof_send_line)(char *line) = NULL;

char* (*prof_get_current_recipient)(void) = NULL;
char* (*prof_get_current_muc)(void) = NULL;
int (*prof_current_win_is_console)(void) = NULL;
char* (*prof_get_current_nick)(void) = NULL;
char** (*prof_get_current_occupants)(void) = NULL;

char* (*prof_get_room_nick)(const char *barejid) = NULL;

void (*prof_log_debug)(const char *message) = NULL;
void (*prof_log_info)(const char *message) = NULL;
void (*prof_log_warning)(const char *message) = NULL;
void (*prof_log_error)(const char *message) = NULL;

void (*_prof_win_create)(const char *filename, PROF_WIN_TAG win, WINDOW_CB input_handler) = NULL;
int (*prof_win_exists)(PROF_WIN_TAG win) = NULL;
int (*prof_win_focus)(PROF_WIN_TAG win) = NULL;
int (*prof_win_show)(PROF_WIN_TAG win, char *line) = NULL;
int (*prof_win_show_themed)(PROF_WIN_TAG tag, char *group, char *key, char *def, char *line) = NULL;

int (*prof_send_stanza)(char *stanza) = NULL;

int (*prof_settings_boolean_get)(char *group, char *key, int def) = NULL;
void (*prof_settings_boolean_set)(char *group, char *key, int value) = NULL;
char* (*prof_settings_string_get)(char *group, char *key, char *def) = NULL;
void (*prof_settings_string_set)(char *group, char *key, char *value) = NULL;
int (*prof_settings_int_get)(char *group, char *key, int def) = NULL;
void (*prof_settings_int_set)(char *group, char *key, int value) = NULL;
char** (*prof_settings_string_list_get)(char *group, char *key) = NULL;
void (*prof_settings_string_list_add)(char *group, char *key, char *value) = NULL;
int (*prof_settings_string_list_remove)(char *group, char *key, char *value) = NULL;
int (*prof_settings_string_list_clear)(char *group, char *key) = NULL;

void (*prof_incoming_message)(char *barejid, char *resource, char *message) = NULL;

void (*_prof_disco_add_feature)(const char *filename, char *feature) = NULL;

void (*prof_encryption_reset)(const char *barejid) = NULL;

int (*prof_chat_set_titlebar_enctext)(const char *barejid, const char *enctext) = NULL;
int (*prof_chat_unset_titlebar_enctext)(const char *barejid) = NULL;
int (*prof_chat_set_incoming_char)(const char *barejid, const char *ch) = NULL;
int (*prof_chat_unset_incoming_char)(const char *barejid) = NULL;
int (*prof_chat_set_outgoing_char)(const char *barejid, const char *ch) = NULL;
int (*prof_chat_unset_outgoing_char)(const char *barejid) = NULL;
int (*prof_room_set_titlebar_enctext)(const char *roomjid, const char *enctext) = NULL;
int (*prof_room_unset_titlebar_enctext)(const char *roomjid) = NULL;
int (*prof_room_set_message_char)(const char *roomjid, const char *ch) = NULL;
int (*prof_room_unset_message_char)(const char *roomjid) = NULL;

int (*prof_chat_show)(const char *const barejid, const char *const message) = NULL;
int (*prof_chat_show_themed)(const char *const barejid, const char *const group, const char *const item, const char *const def,
    const char *const ch, const char *const message) = NULL;

int (*prof_room_show)(const char *const roomjid, const char *const message) = NULL;
int (*prof_room_show_themed)(const char *const roomjid, const char *const group, const char *const item, const char *const def,
    const char *const ch, const char *const message) = NULL;
lass="w"> width) { Display_column = 0; if (Display_row < height-1) ++Display_row; } } break; } :(before "End Primitive Recipe Declarations") CURSOR_POSITION_ON_DISPLAY, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "cursor-position-on-display", CURSOR_POSITION_ON_DISPLAY); :(before "End Primitive Recipe Checks") case CURSOR_POSITION_ON_DISPLAY: { break; } :(before "End Primitive Recipe Implementations") case CURSOR_POSITION_ON_DISPLAY: { CHECK_SCREEN; products.resize(2); products.at(0).push_back(Display_row); products.at(1).push_back(Display_column); break; } :(before "End Primitive Recipe Declarations") MOVE_CURSOR_ON_DISPLAY, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "move-cursor-on-display", MOVE_CURSOR_ON_DISPLAY); :(before "End Primitive Recipe Checks") case MOVE_CURSOR_ON_DISPLAY: { if (SIZE(inst.ingredients) != 2) { raise << maybe(get(Recipe, r).name) << "'move-cursor-on-display' requires two ingredients, but got '" << to_original_string(inst) << "'\n" << end(); break; } if (!is_mu_number(inst.ingredients.at(0))) { raise << maybe(get(Recipe, r).name) << "first ingredient of 'move-cursor-on-display' should be a row number, but got '" << inst.ingredients.at(0).original_string << "'\n" << end(); break; } if (!is_mu_number(inst.ingredients.at(1))) { raise << maybe(get(Recipe, r).name) << "second ingredient of 'move-cursor-on-display' should be a column number, but got '" << inst.ingredients.at(1).original_string << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case MOVE_CURSOR_ON_DISPLAY: { CHECK_SCREEN; Display_row = ingredients.at(0).at(0); Display_column = ingredients.at(1).at(0); tb_set_cursor(Display_column, Display_row); break; } :(before "End Primitive Recipe Declarations") MOVE_CURSOR_DOWN_ON_DISPLAY, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "move-cursor-down-on-display", MOVE_CURSOR_DOWN_ON_DISPLAY); :(before "End Primitive Recipe Checks") case MOVE_CURSOR_DOWN_ON_DISPLAY: { break; } :(before "End Primitive Recipe Implementations") case MOVE_CURSOR_DOWN_ON_DISPLAY: { CHECK_SCREEN; int h=tb_height(); int height = (h >= 0) ? h : 0; if (Display_row < height-1) { ++Display_row; tb_set_cursor(Display_column, Display_row); } break; } :(before "End Primitive Recipe Declarations") MOVE_CURSOR_UP_ON_DISPLAY, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "move-cursor-up-on-display", MOVE_CURSOR_UP_ON_DISPLAY); :(before "End Primitive Recipe Checks") case MOVE_CURSOR_UP_ON_DISPLAY: { break; } :(before "End Primitive Recipe Implementations") case MOVE_CURSOR_UP_ON_DISPLAY: { CHECK_SCREEN; if (Display_row > 0) { --Display_row; tb_set_cursor(Display_column, Display_row); } break; } :(before "End Primitive Recipe Declarations") MOVE_CURSOR_RIGHT_ON_DISPLAY, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "move-cursor-right-on-display", MOVE_CURSOR_RIGHT_ON_DISPLAY); :(before "End Primitive Recipe Checks") case MOVE_CURSOR_RIGHT_ON_DISPLAY: { break; } :(before "End Primitive Recipe Implementations") case MOVE_CURSOR_RIGHT_ON_DISPLAY: { CHECK_SCREEN; int w=tb_width(); int width = (w >= 0) ? w : 0; if (Display_column < width-1) { ++Display_column; tb_set_cursor(Display_column, Display_row); } break; } :(before "End Primitive Recipe Declarations") MOVE_CURSOR_LEFT_ON_DISPLAY, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "move-cursor-left-on-display", MOVE_CURSOR_LEFT_ON_DISPLAY); :(before "End Primitive Recipe Checks") case MOVE_CURSOR_LEFT_ON_DISPLAY: { break; } :(before "End Primitive Recipe Implementations") case MOVE_CURSOR_LEFT_ON_DISPLAY: { CHECK_SCREEN; if (Display_column > 0) { --Display_column; tb_set_cursor(Display_column, Display_row); } break; } //: as a convenience, make $print mostly work in console mode :(before "End $print 10/newline Special-cases") else if (tb_is_active()) { move_cursor_to_start_of_next_line_on_display(); } :(code) void move_cursor_to_start_of_next_line_on_display() { if (Display_row < tb_height()-1) ++Display_row; else Display_row = 0; Display_column = 0; tb_set_cursor(Display_column, Display_row); } :(before "End Primitive Recipe Declarations") DISPLAY_WIDTH, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "display-width", DISPLAY_WIDTH); :(before "End Primitive Recipe Checks") case DISPLAY_WIDTH: { break; } :(before "End Primitive Recipe Implementations") case DISPLAY_WIDTH: { CHECK_SCREEN; products.resize(1); products.at(0).push_back(tb_width()); break; } :(before "End Primitive Recipe Declarations") DISPLAY_HEIGHT, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "display-height", DISPLAY_HEIGHT); :(before "End Primitive Recipe Checks") case DISPLAY_HEIGHT: { break; } :(before "End Primitive Recipe Implementations") case DISPLAY_HEIGHT: { CHECK_SCREEN; products.resize(1); products.at(0).push_back(tb_height()); break; } //:: Keyboard/mouse management :(before "End Primitive Recipe Declarations") WAIT_FOR_SOME_INTERACTION, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "wait-for-some-interaction", WAIT_FOR_SOME_INTERACTION); :(before "End Primitive Recipe Checks") case WAIT_FOR_SOME_INTERACTION: { break; } :(before "End Primitive Recipe Implementations") case WAIT_FOR_SOME_INTERACTION: { CHECK_SCREEN; tb_event event; tb_poll_event(&event); break; } :(before "End Primitive Recipe Declarations") CHECK_FOR_INTERACTION, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "check-for-interaction", CHECK_FOR_INTERACTION); :(before "End Primitive Recipe Checks") case CHECK_FOR_INTERACTION: { break; } :(before "End Primitive Recipe Implementations") case CHECK_FOR_INTERACTION: { CHECK_CONSOLE; products.resize(2); // result and status tb_event event; int event_type = tb_peek_event(&event, 5/*ms*/); if (event_type == TB_EVENT_KEY && event.ch) { products.at(0).push_back(/*text event*/0); products.at(0).push_back(event.ch); products.at(0).push_back(0); products.at(0).push_back(0); products.at(1).push_back(/*found*/true); break; } // treat keys within ascii as unicode characters if (event_type == TB_EVENT_KEY && event.key < 0xff) { products.at(0).push_back(/*text event*/0); if (event.key == TB_KEY_CTRL_C) exit(1); if (event.key == TB_KEY_BACKSPACE2) event.key = TB_KEY_BACKSPACE; if (event.key == TB_KEY_CARRIAGE_RETURN) event.key = TB_KEY_NEWLINE; products.at(0).push_back(event.key); products.at(0).push_back(0); products.at(0).push_back(0); products.at(1).push_back(/*found*/true); break; } // keys outside ascii aren't unicode characters but arbitrary termbox inventions if (event_type == TB_EVENT_KEY) { products.at(0).push_back(/*keycode event*/1); products.at(0).push_back(event.key); products.at(0).push_back(0); products.at(0).push_back(0); products.at(1).push_back(/*found*/true); break; } if (event_type == TB_EVENT_MOUSE) { products.at(0).push_back(/*touch event*/2); products.at(0).push_back(event.key); // which button, etc. products.at(0).push_back(event.y); // row products.at(0).push_back(event.x); // column products.at(1).push_back(/*found*/true); break; } if (event_type == TB_EVENT_RESIZE) { products.at(0).push_back(/*resize event*/3); products.at(0).push_back(event.w); // width products.at(0).push_back(event.h); // height products.at(0).push_back(0); products.at(1).push_back(/*found*/true); break; } assert(event_type == 0); products.at(0).push_back(0); products.at(0).push_back(0); products.at(0).push_back(0); products.at(0).push_back(0); products.at(1).push_back(/*found*/false); break; } :(before "End Primitive Recipe Declarations") INTERACTIONS_LEFT, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "interactions-left?", INTERACTIONS_LEFT); :(before "End Primitive Recipe Checks") case INTERACTIONS_LEFT: { break; } :(before "End Primitive Recipe Implementations") case INTERACTIONS_LEFT: { CHECK_CONSOLE; products.resize(1); products.at(0).push_back(tb_event_ready()); break; } //: hacks to make text-mode apps more responsive under Unix :(before "End Primitive Recipe Declarations") CLEAR_LINE_ON_DISPLAY, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "clear-line-on-display", CLEAR_LINE_ON_DISPLAY); :(before "End Primitive Recipe Checks") case CLEAR_LINE_ON_DISPLAY: { break; } :(before "End Primitive Recipe Implementations") case CLEAR_LINE_ON_DISPLAY: { CHECK_SCREEN; int width = tb_width(); for (int x = Display_column; x < width; ++x) tb_print(' ', TB_WHITE, TB_BLACK); tb_set_cursor(Display_column, Display_row); break; } :(before "End Primitive Recipe Declarations") CLEAR_DISPLAY_FROM, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "clear-display-from", CLEAR_DISPLAY_FROM); :(before "End Primitive Recipe Checks") case CLEAR_DISPLAY_FROM: { break; } :(before "End Primitive Recipe Implementations") case CLEAR_DISPLAY_FROM: { CHECK_SCREEN; // todo: error checking int row = ingredients.at(0).at(0); int column = ingredients.at(1).at(0); int left = ingredients.at(2).at(0); int right = ingredients.at(3).at(0); int height=tb_height(); for (/*nada*/; row < height; ++row, column=left) { // start column from left in every inner loop except first tb_set_cursor(column, row); for (/*nada*/; column <= right; ++column) tb_print(' ', TB_WHITE, TB_BLACK); } tb_set_cursor(Display_column, Display_row); break; }