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
|
/*
* relay-api.c - API protocol for relay to client
*
* Copyright (C) 2023-2024 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat 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.
*
* WeeChat 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 WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <errno.h>
#include <arpa/inet.h>
#include "../../weechat-plugin.h"
#include "../relay.h"
#include "../relay-client.h"
#include "../relay-config.h"
#include "../relay-http.h"
#include "../relay-raw.h"
#include "../relay-raw.h"
#include "relay-api.h"
#include "relay-api-protocol.h"
/*
* Returns value of "colors" URL parameter, an enum with one of these values:
* - RELAY_API_COLORS_ANSI (default)
* - RELAY_API_COLORS_WEECHAT
* - RELAY_API_COLORS_STRIP
*/
enum t_relay_api_colors
relay_api_search_colors (const char *colors)
{
if (!colors)
return RELAY_API_COLORS_ANSI;
if (strcmp (colors, "weechat") == 0)
return RELAY_API_COLORS_WEECHAT;
if (strcmp (colors, "strip") == 0)
return RELAY_API_COLORS_STRIP;
return RELAY_API_COLORS_ANSI;
}
/*
* Hooks signals for a client.
*/
void
relay_api_hook_signals (struct t_relay_client *client)
{
if (!RELAY_API_DATA(client, hook_signal_buffer))
{
RELAY_API_DATA(client, hook_signal_buffer) =
weechat_hook_signal ("buffer_*",
&relay_api_protocol_signal_buffer_cb,
client, NULL);
}
if (RELAY_API_DATA(client, sync_nicks))
{
if (!RELAY_API_DATA(client, hook_hsignal_nicklist))
{
RELAY_API_DATA(client, hook_hsignal_nicklist) =
weechat_hook_hsignal ("nicklist_*",
&relay_api_protocol_hsignal_nicklist_cb,
client, NULL);
}
}
else
{
if (RELAY_API_DATA(client, hook_hsignal_nicklist))
{
weechat_unhook (RELAY_API_DATA(client, hook_hsignal_nicklist));
RELAY_API_DATA(client, hook_hsignal_nicklist) = NULL;
}
}
if (!RELAY_API_DATA(client, hook_signal_upgrade))
{
RELAY_API_DATA(client, hook_signal_upgrade) =
weechat_hook_signal ("upgrade*",
&relay_api_protocol_signal_upgrade_cb,
client, NULL);
}
}
/*
* Unhooks signals for a client.
*/
void
relay_api_unhook_signals (struct t_relay_client *client)
{
if (RELAY_API_DATA(client, hook_signal_buffer))
{
weechat_unhook (RELAY_API_DATA(client, hook_signal_buffer));
RELAY_API_DATA(client, hook_signal_buffer) = NULL;
}
if (RELAY_API_DATA(client, hook_hsignal_nicklist))
{
weechat_unhook (RELAY_API_DATA(client, hook_hsignal_nicklist));
RELAY_API_DATA(client, hook_hsignal_nicklist) = NULL;
}
if (RELAY_API_DATA(client, hook_signal_upgrade))
{
weechat_unhook (RELAY_API_DATA(client, hook_signal_upgrade));
RELAY_API_DATA(client, hook_signal_upgrade) = NULL;
}
}
/*
* Reads HTTP request from a client.
*/
void
relay_api_recv_http (struct t_relay_client *client,
struct t_relay_http_request *request)
{
relay_api_protocol_recv_http (client, request);
}
/*
* Reads JSON string from a client.
*/
void
relay_api_recv_json (struct t_relay_client *client, const char *json)
{
relay_api_protocol_recv_json (client, json);
}
/*
* Closes connection with a client.
*/
void
relay_api_close_connection (struct t_relay_client *client)
{
/*
* IMPORTANT: if changes are made in this function or sub-functions called,
* please also update the function relay_api_add_to_infolist:
* when the flag force_disconnected_state is set to 1 we simulate
* a disconnected state for client in infolist (used on /upgrade -save)
*/
relay_api_unhook_signals (client);
}
/*
* Initializes relay data specific to API protocol.
*/
void
relay_api_alloc (struct t_relay_client *client)
{
client->protocol_data = malloc (sizeof (struct t_relay_api_data));
if (!client->protocol_data)
return;
RELAY_API_DATA(client, hook_signal_buffer) = NULL;
RELAY_API_DATA(client, hook_hsignal_nicklist) = NULL;
RELAY_API_DATA(client, hook_signal_upgrade) = NULL;
RELAY_API_DATA(client, sync_enabled) = 0;
RELAY_API_DATA(client, sync_nicks) = 0;
RELAY_API_DATA(client, sync_colors) = RELAY_API_COLORS_ANSI;
}
/*
* Initializes relay data specific to API protocol with an infolist.
*
* This is called after /upgrade.
*/
void
relay_api_alloc_with_infolist (struct t_relay_client *client,
struct t_infolist *infolist)
{
client->protocol_data = malloc (sizeof (struct t_relay_api_data));
if (!client->protocol_data)
return;
RELAY_API_DATA(client, hook_signal_buffer) = NULL;
RELAY_API_DATA(client, hook_hsignal_nicklist) = NULL;
RELAY_API_DATA(client, hook_signal_upgrade) = NULL;
RELAY_API_DATA(client, sync_enabled) = weechat_infolist_integer (
infolist, "sync_enabled");
RELAY_API_DATA(client, sync_nicks) = weechat_infolist_integer (
infolist, "sync_nicks");
RELAY_API_DATA(client, sync_colors) = weechat_infolist_integer (
infolist, "sync_colors");
if (!RELAY_CLIENT_HAS_ENDED(client) && RELAY_API_DATA(client, sync_enabled))
relay_api_hook_signals (client);
}
/*
* Returns the client initial status: it is always "waiting_auth" for API
* protocol because we always expect the "init" command, even without any
* password.
*/
enum t_relay_status
relay_api_get_initial_status (struct t_relay_client *client)
{
/* make C compiler happy */
(void) client;
return RELAY_STATUS_WAITING_AUTH;
}
/*
* Frees relay data specific to API protocol.
*/
void
relay_api_free (struct t_relay_client *client)
{
if (!client)
return;
if (client->protocol_data)
{
if (RELAY_API_DATA(client, hook_signal_buffer))
weechat_unhook (RELAY_API_DATA(client, hook_signal_buffer));
if (RELAY_API_DATA(client, hook_hsignal_nicklist))
weechat_unhook (RELAY_API_DATA(client, hook_hsignal_nicklist));
if (RELAY_API_DATA(client, hook_signal_upgrade))
weechat_unhook (RELAY_API_DATA(client, hook_signal_upgrade));
free (client->protocol_data);
client->protocol_data = NULL;
}
}
/*
* Adds client API data in an infolist.
*
* If force_disconnected_state == 1, the infolist contains the client
* in a disconnected state (but the client is unchanged, still connected if it
* was).
*
* Returns:
* 1: OK
* 0: error
*/
int
relay_api_add_to_infolist (struct t_infolist_item *item,
struct t_relay_client *client,
int force_disconnected_state)
{
if (!item || !client)
return 0;
/* parameter not used today, it may be in future */
(void) force_disconnected_state;
if (!weechat_infolist_new_var_pointer (item, "hook_signal_buffer", RELAY_API_DATA(client, hook_signal_buffer)))
return 0;
if (!weechat_infolist_new_var_pointer (item, "hook_hsignal_nicklist", RELAY_API_DATA(client, hook_hsignal_nicklist)))
return 0;
if (!weechat_infolist_new_var_pointer (item, "hook_signal_upgrade", RELAY_API_DATA(client, hook_signal_upgrade)))
return 0;
if (!weechat_infolist_new_var_integer (item, "sync_enabled", RELAY_API_DATA(client, sync_enabled)))
return 0;
if (!weechat_infolist_new_var_integer (item, "sync_nicks", RELAY_API_DATA(client, sync_nicks)))
return 0;
if (!weechat_infolist_new_var_integer (item, "sync_colors", RELAY_API_DATA(client, sync_colors)))
return 0;
return 1;
}
/*
* Prints client API data in WeeChat log file (usually for crash dump).
*/
void
relay_api_print_log (struct t_relay_client *client)
{
if (client->protocol_data)
{
weechat_log_printf (" hook_signal_buffer. . . : 0x%lx", RELAY_API_DATA(client, hook_signal_buffer));
weechat_log_printf (" hook_hsignal_nicklist . : 0x%lx", RELAY_API_DATA(client, hook_hsignal_nicklist));
weechat_log_printf (" hook_signal_upgrade . . : 0x%lx", RELAY_API_DATA(client, hook_signal_upgrade));
weechat_log_printf (" sync_enabled. . . . . . : %d", RELAY_API_DATA(client, sync_enabled));
weechat_log_printf (" sync_nicks. . . . . . . : %d", RELAY_API_DATA(client, sync_nicks));
weechat_log_printf (" sync_colors . . . . . . : %d", RELAY_API_DATA(client, sync_colors));
}
}
|