diff options
Diffstat (limited to 'toxcore/group.c')
-rw-r--r-- | toxcore/group.c | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/toxcore/group.c b/toxcore/group.c index 19d7ffa5..da56c572 100644 --- a/toxcore/group.c +++ b/toxcore/group.c | |||
@@ -925,6 +925,19 @@ void g_callback_group_action(Group_Chats *g_c, void (*function)(Messenger *m, in | |||
925 | g_c->action_callback_userdata = userdata; | 925 | g_c->action_callback_userdata = userdata; |
926 | } | 926 | } |
927 | 927 | ||
928 | /* Set handlers for custom lossy packets. | ||
929 | * NOTE: Handler must return 0 if packet is to be relayed, -1 if the packet should not be relayed. | ||
930 | * | ||
931 | * return -1 on failure. | ||
932 | * return 0 on success. | ||
933 | */ | ||
934 | void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, int (*function)(Messenger *m, int, int, | ||
935 | const uint8_t *, uint16_t, void *), void *userdata) | ||
936 | { | ||
937 | g_c->lossy_packethandlers[byte].function = function; | ||
938 | g_c->lossy_packethandlers[byte].userdata = userdata; | ||
939 | } | ||
940 | |||
928 | /* Set callback function for peer name list changes. | 941 | /* Set callback function for peer name list changes. |
929 | * | 942 | * |
930 | * It gets called every time the name list changes(new peer/name, deleted peer) | 943 | * It gets called every time the name list changes(new peer/name, deleted peer) |
@@ -1466,6 +1479,34 @@ int group_action_send(const Group_Chats *g_c, int groupnumber, const uint8_t *ac | |||
1466 | } | 1479 | } |
1467 | } | 1480 | } |
1468 | 1481 | ||
1482 | /* High level function to send custom lossy packets. | ||
1483 | * | ||
1484 | * return -1 on failure. | ||
1485 | * return 0 on success. | ||
1486 | */ | ||
1487 | int send_group_lossy_packet(const Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length) | ||
1488 | { | ||
1489 | //TODO: length check here? | ||
1490 | Group_c *g = get_group_c(g_c, groupnumber); | ||
1491 | |||
1492 | if (!g) | ||
1493 | return -1; | ||
1494 | |||
1495 | uint8_t packet[sizeof(uint16_t) * 2 + length]; | ||
1496 | uint16_t peer_number = htons(g->peer_number); | ||
1497 | memcpy(packet, &peer_number, sizeof(uint16_t)); | ||
1498 | uint16_t message_num = htons(g->lossy_message_number); | ||
1499 | memcpy(packet + sizeof(uint16_t), &message_num, sizeof(uint16_t)); | ||
1500 | memcpy(packet + sizeof(uint16_t) * 2, data, length); | ||
1501 | |||
1502 | if (send_lossy_all_close(g_c, groupnumber, packet, sizeof(packet), -1) == 0) { | ||
1503 | return -1; | ||
1504 | } | ||
1505 | |||
1506 | ++g->lossy_message_number; | ||
1507 | return 0; | ||
1508 | } | ||
1509 | |||
1469 | static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length, | 1510 | static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length, |
1470 | int close_index) | 1511 | int close_index) |
1471 | { | 1512 | { |
@@ -1730,9 +1771,13 @@ static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uin | |||
1730 | ++lossy_data; | 1771 | ++lossy_data; |
1731 | --lossy_length; | 1772 | --lossy_length; |
1732 | 1773 | ||
1733 | switch (message_id) { | 1774 | if (g_c->lossy_packethandlers[message_id].function) { |
1734 | 1775 | if (g_c->lossy_packethandlers[message_id].function(g_c->m, groupnumber, index, lossy_data, lossy_length, | |
1735 | 1776 | g_c->lossy_packethandlers[message_id].userdata) == -1) { | |
1777 | return -1; | ||
1778 | } | ||
1779 | } else { | ||
1780 | return -1; | ||
1736 | } | 1781 | } |
1737 | 1782 | ||
1738 | send_lossy_all_close(g_c, groupnumber, data + 1 + sizeof(uint16_t), length - (1 + sizeof(uint16_t)), | 1783 | send_lossy_all_close(g_c, groupnumber, data + 1 + sizeof(uint16_t), length - (1 + sizeof(uint16_t)), |