about summary refs log tree commit diff stats
path: root/src/plugins/c_api.c
blob: 32719febbfb71c6b00cd2350ca15f440a32467d9 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * c_api.c
 *
 * 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
 * along with Profanity.  If not, see <http://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 <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;

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);
}

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,
    const char **synopsis, const char *description, const char *arguments[][2], const char **examples,
    void(*callback)(char **args))
{
    char *plugin_name = _c_plugin_name(filename);
    log_debug("Register command %s for %s", command_name, plugin_name);

    CommandWrapper *wrapper = malloc(sizeof(CommandWrapper));
    wrapper->func = callback;
    api_register_command(plugin_name, command_name, min_args, max_args, synopsis,
        description, arguments, examples, wrapper, c_command_callback, free);

    free(plugin_name);
}

static void
c_api_register_timed(const char *filename, void(*callback)(void), int interval_seconds)
{
    char *plugin_name = _c_plugin_name(filename);
    log_debug("Register timed for %s", plugin_name);

    TimedWrapper *wrapper = malloc(sizeof(TimedWrapper));
    wrapper->func = callback;
    api_register_timed(plugin_name, wrapper, interval_seconds, c_timed_callback, free);

    free(plugin_name);
}

static void
c_api_completer_add(const char *filename, const char *key, char **items)
{
    char *plugin_name = _c_plugin_name(filename);
    log_debug("Autocomplete add %s for %s", key, plugin_name);

    api_completer_add(plugin_name, key, items);

    free(plugin_name);
}

static void
c_api_completer_remove(const char *key, char **items)
{
    api_completer_remove(key, items);
}

static void
c_api_completer_clear(const char *key)
{
    api_completer_clear(key);
}

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
c_api_current_win_is_console(void)
{
    return api_current_win_is_console();
}

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();
}

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);
}

static int
c_api_win_exists(char *tag)
{
    return api_win_exists(tag);
}

static void
c_api_win_create(const char *filename, char *tag, void(*callback)(char *tag, char *line))
{
    char *plugin_name = _c_plugin_name(filename);
    log_debug("Win create %s for %s", tag, plugin_name);

    WindowWrapper *wrapper = malloc(sizeof(WindowWrapper));
    wrapper->func = callback;
    api_win_create(plugin_name, tag, wrapper, c_window_callback, free);

    free(plugin_name);
}

static int
c_api_win_focus(char *tag)
{
    return api_win_focus(tag);
}

static int
c_api_win_show(char *tag, char *line)
{
    return api_win_show(tag, line);
}

static int
c_api_win_show_themed(char *tag, char *group, char *key, char *def, char *line)
{
    return api_win_show_themed(tag, group, key, def, line);
}

static int
c_api_send_stanza(char *stanza)
{
    return api_send_stanza(stanza);
}

static int
c_api_settings_get_boolean(char *group, char *key, int def)
{
    return api_settings_get_boolean(group, key, def);
}

static void
c_api_settings_set_boolean(char *group, char *key, int value)
{
    api_settings_set_boolean(group, key, value);
}

static char*
c_api_settings_get_string(char *group, char *key, char *def)
{
    return api_settings_get_string(group, key, def);
}

static void
c_api_settings_set_string(char *group, char *key, char *value)
{
    api_settings_set_string(group, key, value);
}

static int
c_api_settings_get_int(char *group, char *key, int def)
{
    return api_settings_get_int(group, key, def);
}

static void
c_api_settings_set_int(char *group, char *key, int value)
{
    api_settings_set_int(group, key, value);
}

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(char *feature)
{
    api_disco_add_feature(feature);
}

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;
    prof_cons_show_themed = c_api_cons_show_themed;
    prof_cons_bad_cmd_usage = c_api_cons_bad_cmd_usage;
    _prof_register_command = c_api_register_command;
    _prof_register_timed = c_api_register_timed;
    _prof_completer_add = c_api_completer_add;
    _prof_win_create = c_api_win_create;
    prof_completer_remove = c_api_completer_remove;
    prof_completer_clear = c_api_completer_clear;
    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;
    prof_get_current_nick = c_api_get_current_nick;
    prof_get_current_occupants = c_api_get_current_occupants;
    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;
    prof_win_show_themed = c_api_win_show_themed;
    prof_send_stanza = c_api_send_stanza;
    prof_settings_get_boolean = c_api_settings_get_boolean;
    prof_settings_set_boolean = c_api_settings_set_boolean;
    prof_settings_get_string = c_api_settings_get_string;
    prof_settings_set_string = c_api_settings_set_string;
    prof_settings_get_int = c_api_settings_get_int;
    prof_settings_set_int = c_api_settings_set_int;
    prof_incoming_message = c_api_incoming_message;
    prof_disco_add_feature = c_api_disco_add_feature;
}

static char *
_c_plugin_name(const char *filename)
{
    GString *plugin_name_str = g_string_new("");
    char *name = strndup(filename, strlen(filename)-1);
    g_string_append(plugin_name_str, name);
    free(name);
    g_string_append(plugin_name_str, "so");
    char *result = plugin_name_str->str;
    g_string_free(plugin_name_str, FALSE);

    return result;
}