This repository was archived by the owner on Jan 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtwc-chat.c
More file actions
483 lines (431 loc) · 14.2 KB
/
twc-chat.c
File metadata and controls
483 lines (431 loc) · 14.2 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* Copyright (c) 2018 Håvard Pettersson <mail@haavard.me>
*
* This file is part of Tox-WeeChat.
*
* Tox-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.
*
* Tox-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 Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#include "twc-config.h"
#include "twc-list.h"
#include "twc-message-queue.h"
#include "twc-profile.h"
#include "twc-utils.h"
#include "twc.h"
#include "twc-chat.h"
const char *twc_tag_unsent_message = "tox_unsent";
const char *twc_tag_sent_message = "tox_sent";
const char *twc_tag_received_message = "tox_received";
int
twc_chat_buffer_input_callback(const void *pointer, void *data,
struct t_gui_buffer *weechat_buffer,
const char *input_data);
int
twc_chat_buffer_close_callback(const void *pointer, void *data,
struct t_gui_buffer *weechat_buffer);
/**
* Create a new chat.
*/
struct t_twc_chat *
twc_chat_new(struct t_twc_profile *profile, const char *name)
{
struct t_twc_chat *chat = malloc(sizeof(struct t_twc_chat));
if (!chat)
return NULL;
chat->profile = profile;
chat->friend_number = chat->group_number = -1;
chat->nicks = NULL;
chat->ids = NULL;
chat->completion = NULL;
chat->last_search = NULL;
chat->prev_comp = NULL;
size_t full_name_size = strlen(profile->name) + 1 + strlen(name) + 1;
char *full_name = malloc(full_name_size);
snprintf(full_name, full_name_size, "%s/%s", profile->name, name);
chat->buffer = weechat_buffer_search("tox", full_name);
if (!(chat->buffer))
{
chat->buffer = weechat_buffer_new(
full_name, twc_chat_buffer_input_callback, chat, NULL,
twc_chat_buffer_close_callback, chat, NULL);
}
else
{
weechat_buffer_set_pointer(chat->buffer, "input_callback",
twc_chat_buffer_input_callback);
weechat_buffer_set_pointer(chat->buffer, "input_callback_pointer",
chat);
weechat_buffer_set_pointer(chat->buffer, "close_callback",
twc_chat_buffer_close_callback);
weechat_buffer_set_pointer(chat->buffer, "close_callback_pointer",
chat);
}
free(full_name);
if (!(chat->buffer))
{
free(chat);
return NULL;
}
/* set correct logging state for buffer */
bool log = TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_LOGGING);
twc_set_buffer_logging(chat->buffer, log);
twc_chat_queue_refresh(chat);
twc_list_item_new_data_add(profile->chats, chat);
return chat;
}
/**
* Create a new friend chat.
*/
struct t_twc_chat *
twc_chat_new_friend(struct t_twc_profile *profile, int32_t friend_number)
{
uint8_t client_id[TOX_PUBLIC_KEY_SIZE];
TOX_ERR_FRIEND_GET_PUBLIC_KEY err;
tox_friend_get_public_key(profile->tox, friend_number, client_id, &err);
if (err != TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK)
return NULL;
char buffer_name[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(client_id, TOX_PUBLIC_KEY_SIZE, buffer_name);
struct t_twc_chat *chat = twc_chat_new(profile, buffer_name);
if (chat)
chat->friend_number = friend_number;
return chat;
}
/**
* Create a new group chat.
*/
struct t_twc_chat *
twc_chat_new_group(struct t_twc_profile *profile, int32_t group_number)
{
char buffer_name[32];
sprintf(buffer_name, "group_chat_%" PRId32, group_number);
struct t_twc_chat *chat = twc_chat_new(profile, buffer_name);
if (chat)
{
chat->group_number = group_number;
chat->nicklist_group =
weechat_nicklist_add_group(chat->buffer, NULL, NULL, NULL, true);
chat->nicks = weechat_list_new();
chat->ids = weechat_list_new();
chat->completion = weechat_list_new();
weechat_buffer_set(chat->buffer, "nicklist", "1");
}
return chat;
}
/**
* Refresh a chat. Updates buffer short_name and title.
*/
void
twc_chat_refresh(const struct t_twc_chat *chat)
{
char *name = NULL;
char *title = NULL;
bool rc = false;
TOX_ERR_CONFERENCE_TITLE err = TOX_ERR_CONFERENCE_TITLE_OK;
if (chat->friend_number >= 0)
{
name = twc_get_name_nt(chat->profile->tox, chat->friend_number);
title =
twc_get_status_message_nt(chat->profile->tox, chat->friend_number);
}
else if (chat->group_number >= 0)
{
char group_name[TOX_MAX_NAME_LENGTH + 1] = {0};
int len = tox_conference_get_title_size(chat->profile->tox,
chat->group_number, &err);
if ((err == TOX_ERR_CONFERENCE_TITLE_OK) &&
(len <= TOX_MAX_NAME_LENGTH))
rc =
tox_conference_get_title(chat->profile->tox, chat->group_number,
(uint8_t *)group_name, &err);
if (!rc)
sprintf(group_name, "Group Chat %" PRId32, chat->group_number);
name = title = strdup((char *)group_name);
}
weechat_buffer_set(chat->buffer, "short_name", name);
weechat_buffer_set(chat->buffer, "title", title);
if (name)
free(name);
if (title && title != name)
free(title);
}
/**
* Callback for twc_chat_queue_refresh. Simply calls twc_chat_refresh.
*/
int
twc_chat_refresh_timer_callback(const void *pointer, void *data, int remaining)
{
twc_chat_refresh(pointer);
return WEECHAT_RC_OK;
}
/**
* Queue a refresh of the buffer in 1ms (i.e. the next event loop tick). Done
* this way to allow data to update before refreshing interface.
*/
void
twc_chat_queue_refresh(struct t_twc_chat *chat)
{
weechat_hook_timer(1, 0, 1, twc_chat_refresh_timer_callback, chat, NULL);
}
/**
* Find an existing chat object for a friend, and if not found, optionally
* create a new one.
*/
struct t_twc_chat *
twc_chat_search_friend(struct t_twc_profile *profile, int32_t friend_number,
bool create_new)
{
size_t index;
struct t_twc_list_item *item;
twc_list_foreach (profile->chats, index, item)
{
if (item->chat->friend_number == friend_number)
return item->chat;
}
if (create_new)
return twc_chat_new_friend(profile, friend_number);
return NULL;
}
/**
* Find an existing chat object for a group, and if not found, optionally
* create a new one.
*/
struct t_twc_chat *
twc_chat_search_group(struct t_twc_profile *profile, int32_t group_number,
bool create_new)
{
size_t index;
struct t_twc_list_item *item;
twc_list_foreach (profile->chats, index, item)
{
if (item->chat->group_number == group_number)
return item->chat;
}
if (create_new)
return twc_chat_new_group(profile, group_number);
return NULL;
}
/**
* Find the chat object associated with a buffer, if it exists.
*/
struct t_twc_chat *
twc_chat_search_buffer(struct t_gui_buffer *buffer)
{
size_t profile_index;
struct t_twc_list_item *profile_item;
twc_list_foreach (twc_profiles, profile_index, profile_item)
{
size_t chat_index;
struct t_twc_list_item *chat_item;
twc_list_foreach (profile_item->profile->chats, chat_index, chat_item)
{
if (chat_item->chat->buffer == buffer)
{
return chat_item->chat;
}
}
}
return NULL;
}
/**
* Set a prefix to a nickname in the nicklist of a chat.
*/
void
twc_chat_update_prefix(struct t_twc_chat *chat, const char *id,
const char *prefix, const char *prefix_color)
{
struct t_gui_nick_group *ptr_group = NULL;
struct t_gui_nick *ptr_nick = NULL;
weechat_nicklist_get_next_item(chat->buffer, &ptr_group, &ptr_nick);
while (ptr_group || ptr_nick)
{
if (ptr_nick)
{
const char *name_field = weechat_nicklist_nick_get_string(
chat->buffer, ptr_nick, "name");
size_t short_id_length =
weechat_config_integer(twc_config_short_id_size);
if (!weechat_strncasecmp(id, name_field + sizeof(char),
short_id_length))
{
weechat_nicklist_nick_set(chat->buffer, ptr_nick, "prefix",
prefix);
weechat_nicklist_nick_set(chat->buffer, ptr_nick,
"prefix_color", prefix_color);
weechat_nicklist_nick_set(chat->buffer, ptr_nick, "color",
"default");
return;
}
}
weechat_nicklist_get_next_item(chat->buffer, &ptr_group, &ptr_nick);
}
}
/**
* Update prefix for a certain nickname structure pointer.
*/
void
twc_chat_update_prefix_by_nick(struct t_gui_buffer *buffer,
struct t_gui_nick *nick, const char *prefix,
const char *prefix_color)
{
weechat_nicklist_nick_set(buffer, nick, "prefix", prefix);
weechat_nicklist_nick_set(buffer, nick, "prefix_color", prefix_color);
weechat_nicklist_nick_set(buffer, nick, "color", "default");
}
/**
* Print a chat message to a chat's buffer.
*/
void
twc_chat_print_message(struct t_twc_chat *chat, const char *tags,
const char *color, const char *sender,
const char *message, TOX_MESSAGE_TYPE message_type)
{
switch (message_type)
{
case TOX_MESSAGE_TYPE_NORMAL:
weechat_printf_date_tags(chat->buffer, 0, tags, "%s%s%s\t%s", color,
sender, weechat_color("reset"), message);
break;
case TOX_MESSAGE_TYPE_ACTION:
weechat_printf_date_tags(chat->buffer, 0, tags, "%s%s%s%s %s",
weechat_prefix("action"), color, sender,
weechat_color("reset"), message);
break;
}
}
/**
* Send a message to the recipient(s) of a chat.
*/
void
twc_chat_send_message(struct t_twc_chat *chat, const char *message,
TOX_MESSAGE_TYPE message_type)
{
TOX_ERR_CONFERENCE_SEND_MESSAGE err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK;
if (chat->friend_number >= 0)
{
twc_message_queue_add_friend_message(chat->profile, chat->friend_number,
message, message_type);
char *name = twc_get_self_name_nt(chat->profile->tox);
twc_chat_print_message(chat, "notify_message",
weechat_color("chat_nick_self"), name, message,
message_type);
free(name);
}
else if (chat->group_number >= 0)
{
int len = strlen(message);
while (len > 0)
{
int fit_len = twc_fit_utf8(message, TWC_MAX_GROUP_MESSAGE_LENGTH);
err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK;
tox_conference_send_message(chat->profile->tox, chat->group_number,
message_type, (uint8_t *)message,
fit_len, &err);
if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK)
break;
message += fit_len;
len -= fit_len;
}
if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK)
{
weechat_printf(
chat->buffer, "%s%sFailed to send message with error %d%s",
weechat_prefix("error"), weechat_color("chat_highlight"), err,
weechat_color("reset"));
}
}
}
/**
* Callback for a buffer receiving user input.
*/
int
twc_chat_buffer_input_callback(const void *pointer, void *data,
struct t_gui_buffer *weechat_buffer,
const char *input_data)
{
/* TODO: don't strip the const */
struct t_twc_chat *chat = (void *)pointer;
twc_chat_send_message(chat, input_data, TOX_MESSAGE_TYPE_NORMAL);
return WEECHAT_RC_OK;
}
/**
* Callback for a buffer being closed.
*/
int
twc_chat_buffer_close_callback(const void *pointer, void *data,
struct t_gui_buffer *weechat_buffer)
{
/* TODO: don't strip the const */
TOX_ERR_CONFERENCE_DELETE err = TOX_ERR_CONFERENCE_DELETE_OK;
struct t_twc_chat *chat = (void *)pointer;
if (chat->profile->tox && chat->group_number >= 0)
{
tox_conference_delete(chat->profile->tox, chat->group_number, &err);
if (err != TOX_ERR_CONFERENCE_DELETE_OK)
{
weechat_printf(
chat->profile->buffer,
"%swarning: failed to leave group chat with error %d",
weechat_prefix("error"), err);
}
}
twc_list_remove_with_data(chat->profile->chats, chat);
twc_chat_free(chat);
return WEECHAT_RC_OK;
}
/**
* Free a chat object.
*/
void
twc_chat_free(struct t_twc_chat *chat)
{
weechat_nicklist_remove_all(chat->buffer);
if (chat->nicks)
{
weechat_list_remove_all(chat->nicks);
weechat_list_free(chat->nicks);
}
if (chat->ids)
{
weechat_list_remove_all(chat->ids);
weechat_list_free(chat->ids);
}
if (chat->completion)
{
weechat_list_remove_all(chat->completion);
weechat_list_free(chat->completion);
}
free(chat->last_search);
free(chat->prev_comp);
free(chat);
}
/**
* Free all chats connected to a profile.
*/
void
twc_chat_free_list(struct t_twc_list *list)
{
struct t_twc_chat *chat;
while ((chat = twc_list_pop(list)))
{
weechat_buffer_set_pointer(chat->buffer, "close_callback", NULL);
weechat_buffer_close(chat->buffer);
twc_chat_free(chat);
}
free(list);
}