forked from bsx/weechat-silc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
silc-plugin.c
100 lines (79 loc) · 3.15 KB
/
silc-plugin.c
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
#include "silc.h"
#include "silcclient.h"
#include "weechat-plugin.h"
#include "silc-plugin.h"
#include "silc-operations.h"
#include "silc-commands.h"
#include "silc-config.h"
#include "silc-keys.h"
#include "silc-connections.h"
#include "silc-bar.h"
WEECHAT_PLUGIN_NAME(SILC_PLUGIN_NAME);
WEECHAT_PLUGIN_DESCRIPTION(SILC_PLUGIN_DESCRIPTION);
WEECHAT_PLUGIN_AUTHOR(SILC_PLUGIN_AUTHOR);
WEECHAT_PLUGIN_VERSION(SILC_PLUGIN_VERSION);
WEECHAT_PLUGIN_LICENSE(SILC_PLUGIN_LICENSE);
/* ===== SILC callbacks ===== */
void silc_running(SilcClient client, void *application) {
weechat_log_printf("SILC initialized");
silc_plugin->running = 1;
}
void silc_stopped(SilcClient client, void *application) {
weechat_log_printf("SILC stopped");
silc_plugin->running = 0;
silc_client_free(silc_plugin->client);
silc_free(silc_plugin);
}
/* ===== weechat plugin interface ===== */
// this is the simulation of a main loop that makes the silc client do its work
int timer_silc(void *data, int remaining_calls) {
silc_client_run_one(silc_plugin->client);
return WEECHAT_RC_OK;
}
int weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[]) {
weechat_plugin = plugin;
SilcClientParams params;
if (silc_plugin_config_init() < 0) {
weechat_log_printf("could not initialize SILC plugin config");
return WEECHAT_RC_ERROR;
}
if (silc_plugin_config_read() < 0) {
weechat_log_printf("could not read SILC plugin config file");
return WEECHAT_RC_ERROR;
}
memset(¶ms, 0, sizeof(params));
params.threads = TRUE;
silc_plugin = silc_calloc(1, sizeof(*silc_plugin));
if (!silc_plugin) {
weechat_log_printf("could not allocate plugin context");
return WEECHAT_RC_ERROR;
}
weechat_log_printf("SILC plugin context allocated");
silc_plugin->client = silc_client_alloc(&ops, ¶ms, silc_plugin, NULL);
if (!silc_plugin->client) {
weechat_log_printf("could not allocate SILC client");
return WEECHAT_RC_ERROR;
}
weechat_log_printf("SILC client allocated");
if (!silc_client_init(silc_plugin->client, weechat_config_string(option_default_username),
silc_net_localhost(), weechat_config_string(option_default_realname), silc_running, silc_plugin)) {
weechat_log_printf("could not initialize SILC client");
return WEECHAT_RC_ERROR;
}
// tick the client once to complete the initialization
silc_client_run_one(silc_plugin->client);
weechat_log_printf("SILC client initialized");
silc_plugin_get_keypair("weechat", "", 1, &silc_plugin->public_key, &silc_plugin->private_key);
server_list = malloc(sizeof(struct SilcPluginServer));
memset(server_list, 0, sizeof(struct SilcPluginServer));
weechat_hook_command("silc", "This is the SILC plugin", "", "", NULL, &command_silc, NULL);
weechat_hook_timer(50, 0, 0, &timer_silc, NULL);
silc_bar_init();
return WEECHAT_RC_OK;
}
int weechat_plugin_end(struct t_weechat_plugin *plugin) {
silc_client_stop(silc_plugin->client, silc_stopped, NULL);
silc_plugin_config_write();
silc_plugin_config_free();
return WEECHAT_RC_OK;
}