diff --git a/toxcore/group.c b/toxcore/group.c index 550c1b57..c158a91b 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -513,7 +513,7 @@ static int setnick(Group_Chats *g_c, int groupnumber, int peer_index, const uint return 0; } -static int settitle(Group_Chats *g_c, int groupnumber, int peer_index, const uint8_t *title, uint16_t title_len) +static int settitle(Group_Chats *g_c, int groupnumber, int peer_index, const uint8_t *title, uint8_t title_len) { if (title_len > MAX_NAME_LENGTH || title_len == 0) return -1; @@ -998,10 +998,10 @@ void g_callback_group_namelistchange(Group_Chats *g_c, void (*function)(Messenge /* Set callback funciton for title changes. * - * Function(Group_Chats *g_c, int groupnumber, int friendgroupnumber, uint8_t * title, uint16_t length, void *userdata) + * Function(Group_Chats *g_c, int groupnumber, int friendgroupnumber, uint8_t * title, uint8_t length, void *userdata) * if friendgroupnumber == -1, then author is unknown (e.g. initial joining the group) */ -void g_callback_group_title(Group_Chats *g_c, void (*function)(Messenger *m, int, int, const uint8_t *, uint16_t, +void g_callback_group_title(Group_Chats *g_c, void (*function)(Messenger *m, int, int, const uint8_t *, uint8_t, void *), void *userdata) { g_c->title_callback = function; @@ -1143,12 +1143,24 @@ static int group_name_send(const Group_Chats *g_c, int groupnumber, const uint8_ * return 0 on success * return -1 on failure */ -int group_title_send(const Group_Chats *g_c, int groupnumber, const uint8_t *title, uint16_t length) +int group_title_send(const Group_Chats *g_c, int groupnumber, const uint8_t *title, uint8_t title_len) { - if (length > MAX_NAME_LENGTH) + if (title_len > MAX_NAME_LENGTH || title_len == 0) return -1; - if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_TITLE_ID, title, length)) + Group_c *g = get_group_c(g_c, groupnumber); + + if (!g) + return -1; + + /* same as already set? */ + if (g->title_len == title_len && !memcmp(g->title, title, title_len)) + return 0; + + memcpy(g->title, title, title_len); + g->title_len = title_len; + + if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_TITLE_ID, title, title_len)) return 0; else return -1; diff --git a/toxcore/group.h b/toxcore/group.h index 891100d4..8a82a5ab 100644 --- a/toxcore/group.h +++ b/toxcore/group.h @@ -124,7 +124,7 @@ typedef struct { void *action_callback_userdata; void (*peer_namelistchange)(Messenger *m, int, int, uint8_t, void *); void *group_namelistchange_userdata; - void (*title_callback)(Messenger *m, int, int, const uint8_t *, uint16_t, void *); + void (*title_callback)(Messenger *m, int, int, const uint8_t *, uint8_t, void *); void *title_callback_userdata; struct { diff --git a/toxcore/tox.c b/toxcore/tox.c index 9e9a5057..2f578971 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -584,6 +584,18 @@ void tox_callback_group_action(Tox *tox, void (*function)(Messenger *tox, int, i g_callback_group_action(m->group_chat_object, function, userdata); } +/* Set callback function for title changes. + * + * Function(Tox *tox, int groupnumber, int peernumber, uint8_t * title, uint8_t length, void *userdata) + * if peernumber == -1, then author is unknown (e.g. initial joining the group) + */ +void tox_callback_group_title(Tox *tox, void (*function)(Tox *tox, int, int, const uint8_t *, uint16_t, + void *), void *userdata) +{ + Messenger *m = tox; + g_callback_group_title(m->group_chat_object, function, userdata); +} + /* Set callback function for peer name list changes. * * It gets called every time the name list changes(new peer/name, deleted peer) @@ -671,6 +683,16 @@ int tox_group_action_send(Tox *tox, int groupnumber, const uint8_t *action, uint return group_action_send(m->group_chat_object, groupnumber, action, length); } +/* set the group's title, limited to MAX_NAME_LENGTH + * return 0 on success + * return -1 on failure + */ +int tox_group_set_title(Tox *tox, int groupnumber, const uint8_t *title, uint8_t length) +{ + Messenger *m = tox; + return group_title_send(m->group_chat_object, groupnumber, title, length); +} + /* Check if the current peernumber corresponds to ours. * * return 1 if the peernumber corresponds to ours. diff --git a/toxcore/tox.h b/toxcore/tox.h index 4f93e608..40ab6c72 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -452,6 +452,14 @@ void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, int, c void tox_callback_group_action(Tox *tox, void (*function)(Tox *tox, int, int, const uint8_t *, uint16_t, void *), void *userdata); +/* Set callback function for title changes. + * + * Function(Tox *tox, int groupnumber, int peernumber, uint8_t * title, uint8_t length, void *userdata) + * if peernumber == -1, then author is unknown (e.g. initial joining the group) + */ +void tox_callback_group_title(Tox *tox, void (*function)(Tox *tox, int, int, const uint8_t *, uint16_t, + void *), void *userdata); + /* Set callback function for peer name list changes. * * It gets called every time the name list changes(new peer/name, deleted peer) @@ -514,6 +522,12 @@ int tox_group_message_send(Tox *tox, int groupnumber, const uint8_t *message, ui */ int tox_group_action_send(Tox *tox, int groupnumber, const uint8_t *action, uint16_t length); +/* set the group's title, limited to MAX_NAME_LENGTH + * return 0 on success + * return -1 on failure + */ +int tox_group_set_title(Tox *tox, int groupnumber, const uint8_t *title, uint8_t length); + /* Check if the current peernumber corresponds to ours. * * return 1 if the peernumber corresponds to ours.