Skip to content

Commit 85474ef

Browse files
author
GrayHatter
committedMar 18, 2015
New callback for toxcore API messages
Fixed incoming in tox_callbacks.h And outgoing in tox.c
1 parent 9d2586b commit 85474ef

File tree

2 files changed

+28
-46
lines changed

2 files changed

+28
-46
lines changed
 

‎tox.c

+14-29
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void log_write(Tox *tox, int fid, const uint8_t *message, uint16_t length, _Bool
7070
} else {
7171
namelen = tox_friend_get_name_size(tox, fid, 0);
7272
tox_friend_get_name(tox, fid, name, 0);
73-
73+
7474
}
7575

7676
if (namelen > TOX_MAX_NAME_LENGTH) {
@@ -279,7 +279,6 @@ static void set_callbacks(Tox *tox)
279279
{
280280
tox_callback_friend_request(tox, callback_friend_request, NULL);
281281
tox_callback_friend_message(tox, callback_friend_message, NULL);
282-
tox_callback_friend_action(tox, callback_friend_action, NULL);
283282
tox_callback_friend_name(tox, callback_name_change, NULL);
284283
tox_callback_friend_status_message(tox, callback_status_message, NULL);
285284
tox_callback_friend_status(tox, callback_user_status, NULL);
@@ -789,46 +788,32 @@ static void tox_thread_message(Tox *tox, ToxAv *av, uint64_t time, uint8_t msg,
789788
break;
790789
}
791790

792-
case TOX_SENDMESSAGE: {
793-
/* param1: friend #
794-
* param2: message length
795-
* data: message
796-
*/
797-
798-
/* write message to friend to logfile */
799-
log_write(tox, param1, data, param2, 1, LOG_FILE_MSG_TYPE_TEXT);
800-
801-
void *p = data;
802-
while(param2 > TOX_MAX_MESSAGE_LENGTH) {
803-
uint16_t len = TOX_MAX_MESSAGE_LENGTH - utf8_unlen(p + TOX_MAX_MESSAGE_LENGTH);
804-
tox_friend_send_message(tox, param1, p, len, 0);
805-
param2 -= len;
806-
p += len;
807-
}
808-
809-
tox_friend_send_message(tox, param1, p, param2, 0);
810-
free(data);
811-
break;
812-
}
813-
791+
case TOX_SENDMESSAGE:
814792
case TOX_SENDACTION: {
815793
/* param1: friend #
816794
* param2: message length
817795
* data: message
818796
*/
819797

820-
/* write action/emote to friend to logfile */
821-
log_write(tox, param1, data, param2, 1, LOG_FILE_MSG_TYPE_ACTION);
822-
823798
void *p = data;
799+
TOX_MESSAGE_TYPE type;
800+
if(msg == TOX_SENDACTION){
801+
type = TOX_MESSAGE_TYPE_ACTION;
802+
} else {
803+
type = TOX_MESSAGE_TYPE_NORMAL;
804+
}
824805
while(param2 > TOX_MAX_MESSAGE_LENGTH) {
825806
uint16_t len = TOX_MAX_MESSAGE_LENGTH - utf8_unlen(p + TOX_MAX_MESSAGE_LENGTH);
826-
tox_friend_send_action(tox, param1, p, len, 0);
807+
tox_friend_send_message(tox, param1, type, p, len, 0);
827808
param2 -= len;
828809
p += len;
829810
}
811+
// Send last or only message
812+
tox_friend_send_message(tox, param1, type, p, param2, 0);
813+
814+
/* write message to friend to logfile */
815+
log_write(tox, param1, data, param2, 1, LOG_FILE_MSG_TYPE_TEXT);
830816

831-
tox_friend_send_action(tox, param1, p, param2, 0);
832817
free(data);
833818
break;
834819
}

‎tox_callbacks.h

+14-17
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,23 @@ static void callback_friend_request(Tox *UNUSED(tox), const uint8_t *id, const u
5555
postmessage(FRIEND_ACCEPT, (r < 0), (r < 0) ? 0 : r, data);*/
5656
}
5757

58-
static void callback_friend_message(Tox *tox, uint32_t fid, const uint8_t *message, size_t length, void *UNUSED(userdata))
59-
{
58+
static void callback_friend_message(Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type, const uint8_t *message, size_t length, void *UNUSED(userdata)){
6059
/* send message to UI */
61-
postmessage(FRIEND_MESSAGE, fid, 0, copy_message(message, length, MSG_TYPE_TEXT));
62-
63-
debug("Friend Message (%u): %.*s\n", fid, length, message);
60+
switch(type){
61+
case TOX_MESSAGE_TYPE_NORMAL:
62+
postmessage(FRIEND_MESSAGE, friend_number, 0, copy_message(message, length, MSG_TYPE_TEXT));
63+
debug("Friend(%u) Standard Message: %.*s\n", friend_number, length, message);
64+
break;
65+
case TOX_MESSAGE_TYPE_ACTION:
66+
postmessage(FRIEND_MESSAGE, friend_number, 0, copy_message(message, length, MSG_TYPE_ACTION_TEXT));
67+
debug("Friend(%u) Action Message: %.*s\n", friend_number, length, message);
68+
break;
69+
default:
70+
debug("Message from Friend(%u) of unsupported type: %.*s\n", friend_number, length, message);
71+
}
6472

6573
/* write message to logfile */
66-
log_write(tox, fid, message, length, 0, LOG_FILE_MSG_TYPE_TEXT);
67-
}
68-
69-
static void callback_friend_action(Tox *tox, uint32_t fid, const uint8_t *action, size_t length, void *UNUSED(userdata))
70-
{
71-
/* send action/emote to UI */
72-
postmessage(FRIEND_MESSAGE, fid, 0, copy_message(action, length, MSG_TYPE_ACTION_TEXT));
73-
74-
debug("Friend Action (%u): %.*s\n", fid, length, action);
75-
76-
/* write action/emote to logfile */
77-
log_write(tox, fid, action, length, 0, LOG_FILE_MSG_TYPE_ACTION);
74+
log_write(tox, friend_number, message, length, 0, LOG_FILE_MSG_TYPE_TEXT);
7875
}
7976

8077
static void callback_name_change(Tox *UNUSED(tox), uint32_t fid, const uint8_t *newname, size_t length, void *UNUSED(userdata))

0 commit comments

Comments
 (0)
Please sign in to comment.