forked from notsecure/uTox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tox_callbacks.h
267 lines (205 loc) · 8.7 KB
/
tox_callbacks.h
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
static void* copy_message(const uint8_t *str, uint16_t length, uint8_t msg_type)
{
length = utf8_validate(str, length);
MESSAGE *msg = malloc(sizeof(MESSAGE) + length);
msg->author = 0;
msg->msg_type = msg_type;
msg->length = length;
memcpy(msg->msg, str, length);
return msg;
}
static void* copy_groupmessage(Tox *tox, const uint8_t *str, uint16_t length, uint8_t msg_type, int gid, int pid)
{
uint8_t name[TOX_MAX_NAME_LENGTH];
int namelen = tox_group_peername(tox, gid, pid, name);
if(namelen == 0 || namelen == -1) {
memcpy(name, "<unknown>", 9);
namelen = 9;
}
length = utf8_validate(str, length);
namelen = utf8_validate(name, namelen);
MESSAGE *msg = malloc(sizeof(MESSAGE) + 1 + length + namelen);
msg->author = 0;
msg->msg_type = msg_type;
msg->length = length;
memcpy(msg->msg, str, length);
msg->msg[length] = (char_t)namelen;
memcpy(&msg->msg[length] + 1, name, namelen);
return msg;
}
static void callback_friend_request(Tox *UNUSED(tox), const uint8_t *id, const uint8_t *msg, uint16_t length, void *UNUSED(userdata))
{
length = utf8_validate(msg, length);
FRIENDREQ *req = malloc(sizeof(FRIENDREQ) + length);
req->length = length;
memcpy(req->id, id, sizeof(req->id));
memcpy(req->msg, msg, length);
postmessage(FRIEND_REQUEST, 0, 0, req);
/*int r = tox_add_friend_norequest(tox, id);
void *data = malloc(TOX_FRIEND_ADDRESS_SIZE);
memcpy(data, id, TOX_FRIEND_ADDRESS_SIZE);
postmessage(FRIEND_ACCEPT, (r < 0), (r < 0) ? 0 : r, data);*/
}
static void callback_friend_message(Tox *tox, int fid, const uint8_t *message, uint16_t length, void *UNUSED(userdata))
{
/* send message to UI */
postmessage(FRIEND_MESSAGE, fid, 0, copy_message(message, length, MSG_TYPE_TEXT));
debug("Friend Message (%u): %.*s\n", fid, length, message);
/* write message to logfile */
log_write(tox, fid, message, length, 0, LOG_FILE_MSG_TYPE_TEXT);
}
static void callback_friend_action(Tox *tox, int fid, const uint8_t *action, uint16_t length, void *UNUSED(userdata))
{
/* send action/emote to UI */
postmessage(FRIEND_MESSAGE, fid, 0, copy_message(action, length, MSG_TYPE_ACTION_TEXT));
debug("Friend Action (%u): %.*s\n", fid, length, action);
/* write action/emote to logfile */
log_write(tox, fid, action, length, 0, LOG_FILE_MSG_TYPE_ACTION);
}
static void callback_name_change(Tox *UNUSED(tox), int fid, const uint8_t *newname, uint16_t length, void *UNUSED(userdata))
{
length = utf8_validate(newname, length);
void *data = malloc(length);
memcpy(data, newname, length);
postmessage(FRIEND_NAME, fid, length, data);
debug("Friend Name (%u): %.*s\n", fid, length, newname);
}
static void callback_status_message(Tox *UNUSED(tox), int fid, const uint8_t *newstatus, uint16_t length, void *UNUSED(userdata))
{
length = utf8_validate(newstatus, length);
void *data = malloc(length);
memcpy(data, newstatus, length);
postmessage(FRIEND_STATUS_MESSAGE, fid, length, data);
debug("Friend Status Message (%u): %.*s\n", fid, length, newstatus);
}
static void callback_user_status(Tox *UNUSED(tox), int fid, uint8_t status, void *UNUSED(userdata))
{
postmessage(FRIEND_STATUS, fid, status, NULL);
debug("Friend Userstatus (%u): %u\n", fid, status);
}
static void callback_typing_change(Tox *UNUSED(tox), int fid, uint8_t is_typing, void *UNUSED(userdata))
{
postmessage(FRIEND_TYPING, fid, is_typing, NULL);
debug("Friend Typing (%u): %u\n", fid, is_typing);
}
static void callback_read_receipt(Tox *UNUSED(tox), int fid, uint32_t receipt, void *UNUSED(userdata))
{
//postmessage(FRIEND_RECEIPT, fid, receipt);
debug("Friend Receipt (%u): %u\n", fid, receipt);
}
static void callback_connection_status(Tox *tox, int fid, uint8_t status, void *UNUSED(userdata))
{
FRIEND *f = &friend[fid];
int i;
postmessage(FRIEND_ONLINE, fid, status, NULL);
if(!status) {
/* break all file transfers */
for(i = 0; i != countof(f->incoming); i++) {
if(f->incoming[i].status) {
f->incoming[i].status = FT_BROKE;
postmessage(FRIEND_FILE_IN_STATUS, fid, i, (void*)FILE_BROKEN);
}
}
for(i = 0; i != countof(f->outgoing); i++) {
if(f->outgoing[i].status) {
f->outgoing[i].status = FT_BROKE;
postmessage(FRIEND_FILE_OUT_STATUS, fid, i, (void*)FILE_BROKEN);
}
}
} else {
/* resume any broken file transfers */
for(i = 0; i != countof(f->incoming); i++) {
if(f->incoming[i].status == FT_BROKE) {
tox_file_send_control(tox, fid, 1, i, TOX_FILECONTROL_RESUME_BROKEN, (void*)&f->incoming[i].bytes, sizeof(uint64_t));
}
}
/* request avatar info (in case it changed) */
tox_request_avatar_info(tox, fid);
}
debug("Friend Online/Offline (%u): %u\n", fid, status);
}
void callback_avatar_info(Tox *tox, int fid, uint8_t format, uint8_t *hash, void *UNUSED(userdata))
{
FRIEND *f = &friend[fid];
if (format != TOX_AVATAR_FORMAT_NONE) {
if (!friend_has_avatar(f) || memcmp(f->avatar.hash, hash, TOX_HASH_LENGTH) != 0) { // check if avatar has changed
memcpy(f->avatar.hash, hash, TOX_HASH_LENGTH); // set hash pre-emptively so we don't request data twice
char_t hash_string[TOX_HASH_LENGTH * 2];
hash_to_string(hash_string, hash);
debug("Friend Avatar Hash (%u): %.*s\n", fid, (int)sizeof(hash_string), hash_string);
tox_request_avatar_data(tox, fid);
}
} else if (friend_has_avatar(f)) {
postmessage(FRIEND_UNSETAVATAR, fid, 0, NULL); // unset avatar if we had one
}
}
void callback_avatar_data(Tox *tox, int fid, uint8_t format, uint8_t *hash, uint8_t *data, uint32_t datalen, void *UNUSED(userdata))
{
FRIEND *f = &friend[fid];
if (memcmp(f->avatar.hash, hash, TOX_HASH_LENGTH) == 0) { // same hash as in last avatar_info
uint8_t *data_out = malloc(datalen);
memcpy(data_out, data, datalen);
postmessage(FRIEND_SETAVATAR, fid, datalen, data_out);
}
}
void callback_av_group_audio(Tox *tox, int groupnumber, int peernumber, const int16_t *pcm, unsigned int samples,
uint8_t channels, unsigned int sample_rate, void *userdata);
static void callback_group_invite(Tox *tox, int fid, uint8_t type, const uint8_t *data, uint16_t length, void *UNUSED(userdata))
{
int gid = -1;
if (type == TOX_GROUPCHAT_TYPE_TEXT) {
gid = tox_join_groupchat(tox, fid, data, length);
} else if (type == TOX_GROUPCHAT_TYPE_AV) {
gid = toxav_join_av_groupchat(tox, fid, data, length, &callback_av_group_audio, NULL);
}
if(gid != -1) {
postmessage(GROUP_ADD, gid, 0, tox);
}
debug("Group Invite (%i,f:%i) type %u\n", gid, fid, type);
}
static void callback_group_message(Tox *tox, int gid, int pid, const uint8_t *message, uint16_t length, void *UNUSED(userdata))
{
postmessage(GROUP_MESSAGE, gid, 0, copy_groupmessage(tox, message, length, MSG_TYPE_TEXT, gid, pid));
debug("Group Message (%u, %u): %.*s\n", gid, pid, length, message);
}
static void callback_group_action(Tox *tox, int gid, int pid, const uint8_t *action, uint16_t length, void *UNUSED(userdata))
{
postmessage(GROUP_MESSAGE, gid, 0, copy_groupmessage(tox, action, length, MSG_TYPE_ACTION_TEXT, gid, pid));
debug("Group Action (%u, %u): %.*s\n", gid, pid, length, action);
}
static void callback_group_namelist_change(Tox *tox, int gid, int pid, uint8_t change, void *UNUSED(userdata))
{
switch(change) {
case TOX_CHAT_CHANGE_PEER_ADD: {
postmessage(GROUP_PEER_ADD, gid, pid, tox);
break;
}
case TOX_CHAT_CHANGE_PEER_DEL: {
postmessage(GROUP_PEER_DEL, gid, pid, tox);
break;
}
case TOX_CHAT_CHANGE_PEER_NAME: {
uint8_t name[TOX_MAX_NAME_LENGTH];
int len = tox_group_peername(tox, gid, pid, name);
len = utf8_validate(name, len);
uint8_t *data = malloc(len + 1);
data[0] = len;
memcpy(data + 1, name, len);
postmessage(GROUP_PEER_NAME, gid, pid, data);
break;
}
}
debug("Group Namelist Change (%u, %u): %u\n", gid, pid, change);
}
static void callback_group_title(Tox *tox, int gid, int pid, const uint8_t *title, uint8_t length, void *UNUSED(userdata))
{
length = utf8_validate(title, length);
if (!length)
return;
uint8_t *copy_title = malloc(length);
if (!copy_title)
return;
memcpy(copy_title, title, length);
postmessage(GROUP_TITLE, gid, length, copy_title);
debug("Group Title (%u, %u): %.*s\n", gid, pid, length, title);
}