Skip to content

Commit

Permalink
New callback for toxcore API messages
Browse files Browse the repository at this point in the history
Fixed incoming in tox_callbacks.h
And outgoing in tox.c
  • Loading branch information
GrayHatter committed Mar 18, 2015
1 parent 9d2586b commit 85474ef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 46 deletions.
43 changes: 14 additions & 29 deletions tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void log_write(Tox *tox, int fid, const uint8_t *message, uint16_t length, _Bool
} else {
namelen = tox_friend_get_name_size(tox, fid, 0);
tox_friend_get_name(tox, fid, name, 0);

}

if (namelen > TOX_MAX_NAME_LENGTH) {
Expand Down Expand Up @@ -279,7 +279,6 @@ static void set_callbacks(Tox *tox)
{
tox_callback_friend_request(tox, callback_friend_request, NULL);
tox_callback_friend_message(tox, callback_friend_message, NULL);
tox_callback_friend_action(tox, callback_friend_action, NULL);
tox_callback_friend_name(tox, callback_name_change, NULL);
tox_callback_friend_status_message(tox, callback_status_message, NULL);
tox_callback_friend_status(tox, callback_user_status, NULL);
Expand Down Expand Up @@ -789,46 +788,32 @@ static void tox_thread_message(Tox *tox, ToxAv *av, uint64_t time, uint8_t msg,
break;
}

case TOX_SENDMESSAGE: {
/* param1: friend #
* param2: message length
* data: message
*/

/* write message to friend to logfile */
log_write(tox, param1, data, param2, 1, LOG_FILE_MSG_TYPE_TEXT);

void *p = data;
while(param2 > TOX_MAX_MESSAGE_LENGTH) {
uint16_t len = TOX_MAX_MESSAGE_LENGTH - utf8_unlen(p + TOX_MAX_MESSAGE_LENGTH);
tox_friend_send_message(tox, param1, p, len, 0);
param2 -= len;
p += len;
}

tox_friend_send_message(tox, param1, p, param2, 0);
free(data);
break;
}

case TOX_SENDMESSAGE:
case TOX_SENDACTION: {
/* param1: friend #
* param2: message length
* data: message
*/

/* write action/emote to friend to logfile */
log_write(tox, param1, data, param2, 1, LOG_FILE_MSG_TYPE_ACTION);

void *p = data;
TOX_MESSAGE_TYPE type;
if(msg == TOX_SENDACTION){
type = TOX_MESSAGE_TYPE_ACTION;
} else {
type = TOX_MESSAGE_TYPE_NORMAL;
}
while(param2 > TOX_MAX_MESSAGE_LENGTH) {
uint16_t len = TOX_MAX_MESSAGE_LENGTH - utf8_unlen(p + TOX_MAX_MESSAGE_LENGTH);
tox_friend_send_action(tox, param1, p, len, 0);
tox_friend_send_message(tox, param1, type, p, len, 0);
param2 -= len;
p += len;
}
// Send last or only message
tox_friend_send_message(tox, param1, type, p, param2, 0);

/* write message to friend to logfile */
log_write(tox, param1, data, param2, 1, LOG_FILE_MSG_TYPE_TEXT);

tox_friend_send_action(tox, param1, p, param2, 0);
free(data);
break;
}
Expand Down
31 changes: 14 additions & 17 deletions tox_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,23 @@ static void callback_friend_request(Tox *UNUSED(tox), const uint8_t *id, const u
postmessage(FRIEND_ACCEPT, (r < 0), (r < 0) ? 0 : r, data);*/
}

static void callback_friend_message(Tox *tox, uint32_t fid, const uint8_t *message, size_t length, void *UNUSED(userdata))
{
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)){
/* 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);
switch(type){
case TOX_MESSAGE_TYPE_NORMAL:
postmessage(FRIEND_MESSAGE, friend_number, 0, copy_message(message, length, MSG_TYPE_TEXT));
debug("Friend(%u) Standard Message: %.*s\n", friend_number, length, message);
break;
case TOX_MESSAGE_TYPE_ACTION:
postmessage(FRIEND_MESSAGE, friend_number, 0, copy_message(message, length, MSG_TYPE_ACTION_TEXT));
debug("Friend(%u) Action Message: %.*s\n", friend_number, length, message);
break;
default:
debug("Message from Friend(%u) of unsupported type: %.*s\n", friend_number, 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, uint32_t fid, const uint8_t *action, size_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);
log_write(tox, friend_number, message, length, 0, LOG_FILE_MSG_TYPE_TEXT);
}

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

0 comments on commit 85474ef

Please sign in to comment.