diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/group.c | 111 | ||||
-rw-r--r-- | toxcore/group.h | 46 |
2 files changed, 148 insertions, 9 deletions
diff --git a/toxcore/group.c b/toxcore/group.c index da56c572..6f0cfef3 100644 --- a/toxcore/group.c +++ b/toxcore/group.c | |||
@@ -431,6 +431,9 @@ static int addpeer(Group_Chats *g_c, int groupnumber, const uint8_t *real_pk, co | |||
431 | g_c->peer_namelistchange(g_c->m, groupnumber, g->numpeers - 1, CHAT_CHANGE_PEER_ADD, | 431 | g_c->peer_namelistchange(g_c->m, groupnumber, g->numpeers - 1, CHAT_CHANGE_PEER_ADD, |
432 | g_c->group_namelistchange_userdata); | 432 | g_c->group_namelistchange_userdata); |
433 | 433 | ||
434 | if (g_c->peer_on_join) | ||
435 | g_c->peer_on_join(g->object, groupnumber, g->numpeers - 1); | ||
436 | |||
434 | return (g->numpeers - 1); | 437 | return (g->numpeers - 1); |
435 | } | 438 | } |
436 | 439 | ||
@@ -460,25 +463,29 @@ static int delpeer(Group_Chats *g_c, int groupnumber, int peer_index) | |||
460 | Group_Peer *temp; | 463 | Group_Peer *temp; |
461 | --g->numpeers; | 464 | --g->numpeers; |
462 | 465 | ||
466 | void *peer_object = g->group[peer_index].object; | ||
467 | |||
463 | if (g->numpeers == 0) { | 468 | if (g->numpeers == 0) { |
464 | free(g->group); | 469 | free(g->group); |
465 | g->group = NULL; | 470 | g->group = NULL; |
466 | return 0; | 471 | } else { |
467 | } | 472 | if (g->numpeers != (uint32_t)peer_index) |
468 | 473 | memcpy(&g->group[peer_index], &g->group[g->numpeers], sizeof(Group_Peer)); | |
469 | if (g->numpeers != (uint32_t)peer_index) | ||
470 | memcpy(&g->group[peer_index], &g->group[g->numpeers], sizeof(Group_Peer)); | ||
471 | 474 | ||
472 | temp = realloc(g->group, sizeof(Group_Peer) * (g->numpeers)); | 475 | temp = realloc(g->group, sizeof(Group_Peer) * (g->numpeers)); |
473 | 476 | ||
474 | if (temp == NULL) | 477 | if (temp == NULL) |
475 | return -1; | 478 | return -1; |
476 | 479 | ||
477 | g->group = temp; | 480 | g->group = temp; |
481 | } | ||
478 | 482 | ||
479 | if (g_c->peer_namelistchange) | 483 | if (g_c->peer_namelistchange) |
480 | g_c->peer_namelistchange(g_c->m, groupnumber, peer_index, CHAT_CHANGE_PEER_DEL, g_c->group_namelistchange_userdata); | 484 | g_c->peer_namelistchange(g_c->m, groupnumber, peer_index, CHAT_CHANGE_PEER_DEL, g_c->group_namelistchange_userdata); |
481 | 485 | ||
486 | if (g_c->peer_on_leave) | ||
487 | g_c->peer_on_leave(g->object, groupnumber, peer_index, peer_object); | ||
488 | |||
482 | return 0; | 489 | return 0; |
483 | } | 490 | } |
484 | 491 | ||
@@ -950,6 +957,24 @@ void g_callback_group_namelistchange(Group_Chats *g_c, void (*function)(Messenge | |||
950 | g_c->group_namelistchange_userdata = userdata; | 957 | g_c->group_namelistchange_userdata = userdata; |
951 | } | 958 | } |
952 | 959 | ||
960 | /* Set a function to be called when a new peer joins a group chat. | ||
961 | * | ||
962 | * Function(void *group object (set with group_set_object), int groupnumber, int friendgroupnumber) | ||
963 | */ | ||
964 | void callback_groupchat_peer_new(const Group_Chats *g_c, void (*function)(void *, int, int)) | ||
965 | { | ||
966 | g_c->peer_on_join = function; | ||
967 | } | ||
968 | |||
969 | /* Set a function to be called when a peer leaves a group chat. | ||
970 | * | ||
971 | * Function(void *group object (set with group_set_object), int groupnumber, int friendgroupnumber, void *group peer object (set with group_peer_set_object)) | ||
972 | */ | ||
973 | void callback_groupchat_peer_delete(const Group_Chats *g_c, void (*function)(void *, int, int, void *)) | ||
974 | { | ||
975 | g_c->peer_on_leave = function; | ||
976 | } | ||
977 | |||
953 | static unsigned int send_message_group(const Group_Chats *g_c, int groupnumber, uint8_t message_id, const uint8_t *data, | 978 | static unsigned int send_message_group(const Group_Chats *g_c, int groupnumber, uint8_t message_id, const uint8_t *data, |
954 | uint16_t len); | 979 | uint16_t len); |
955 | 980 | ||
@@ -1785,6 +1810,74 @@ static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uin | |||
1785 | return 0; | 1810 | return 0; |
1786 | } | 1811 | } |
1787 | 1812 | ||
1813 | /* Set the object that is tied to the group chat. | ||
1814 | * | ||
1815 | * return 0 on success. | ||
1816 | * return -1 on failure | ||
1817 | */ | ||
1818 | int group_set_object(const Group_Chats *g_c, int groupnumber, void *object) | ||
1819 | { | ||
1820 | Group_c *g = get_group_c(g_c, groupnumber); | ||
1821 | |||
1822 | if (!g) | ||
1823 | return -1; | ||
1824 | |||
1825 | g->object = object; | ||
1826 | return 0; | ||
1827 | } | ||
1828 | |||
1829 | /* Set the object that is tied to the group peer. | ||
1830 | * | ||
1831 | * return 0 on success. | ||
1832 | * return -1 on failure | ||
1833 | */ | ||
1834 | int group_peer_set_object(const Group_Chats *g_c, int groupnumber, int peernumber, void *object) | ||
1835 | { | ||
1836 | Group_c *g = get_group_c(g_c, groupnumber); | ||
1837 | |||
1838 | if (!g) | ||
1839 | return -1; | ||
1840 | |||
1841 | if ((uint32_t)peernumber >= g->numpeers) | ||
1842 | return -1; | ||
1843 | |||
1844 | g->group[peernumber].object = object; | ||
1845 | return 0; | ||
1846 | } | ||
1847 | |||
1848 | /* Return the object tide to the group chat previously set by group_set_object. | ||
1849 | * | ||
1850 | * return NULL on failure. | ||
1851 | * return object on success. | ||
1852 | */ | ||
1853 | void *group_get_object(const Group_Chats *g_c, int groupnumber) | ||
1854 | { | ||
1855 | Group_c *g = get_group_c(g_c, groupnumber); | ||
1856 | |||
1857 | if (!g) | ||
1858 | return NULL; | ||
1859 | |||
1860 | return g->object; | ||
1861 | } | ||
1862 | |||
1863 | /* Return the object tide to the group chat peer previously set by group_peer_set_object. | ||
1864 | * | ||
1865 | * return NULL on failure. | ||
1866 | * return object on success. | ||
1867 | */ | ||
1868 | void *group_peer_get_object(const Group_Chats *g_c, int groupnumber, int peernumber) | ||
1869 | { | ||
1870 | Group_c *g = get_group_c(g_c, groupnumber); | ||
1871 | |||
1872 | if (!g) | ||
1873 | return NULL; | ||
1874 | |||
1875 | if ((uint32_t)peernumber >= g->numpeers) | ||
1876 | return NULL; | ||
1877 | |||
1878 | return g->group[peernumber].object; | ||
1879 | } | ||
1880 | |||
1788 | /* Interval in seconds to send ping messages */ | 1881 | /* Interval in seconds to send ping messages */ |
1789 | #define GROUP_PING_INTERVAL 30 | 1882 | #define GROUP_PING_INTERVAL 30 |
1790 | 1883 | ||
diff --git a/toxcore/group.h b/toxcore/group.h index e307bec9..41c7078e 100644 --- a/toxcore/group.h +++ b/toxcore/group.h | |||
@@ -49,6 +49,8 @@ typedef struct { | |||
49 | 49 | ||
50 | uint8_t recv_lossy[MAX_LOSSY_COUNT]; | 50 | uint8_t recv_lossy[MAX_LOSSY_COUNT]; |
51 | uint16_t bottom_lossy_number, top_lossy_number; | 51 | uint16_t bottom_lossy_number, top_lossy_number; |
52 | |||
53 | void *object; | ||
52 | } Group_Peer; | 54 | } Group_Peer; |
53 | 55 | ||
54 | #define DESIRED_CLOSE_CONNECTIONS 4 | 56 | #define DESIRED_CLOSE_CONNECTIONS 4 |
@@ -91,6 +93,8 @@ typedef struct { | |||
91 | uint64_t last_sent_ping; | 93 | uint64_t last_sent_ping; |
92 | 94 | ||
93 | int number_joined; /* friendcon_id of person that invited us to the chat. (-1 means none) */ | 95 | int number_joined; /* friendcon_id of person that invited us to the chat. (-1 means none) */ |
96 | |||
97 | void *object; | ||
94 | } Group_c; | 98 | } Group_c; |
95 | 99 | ||
96 | typedef struct { | 100 | typedef struct { |
@@ -109,6 +113,8 @@ typedef struct { | |||
109 | void (*peer_namelistchange)(Messenger *m, int, int, uint8_t, void *); | 113 | void (*peer_namelistchange)(Messenger *m, int, int, uint8_t, void *); |
110 | void *group_namelistchange_userdata; | 114 | void *group_namelistchange_userdata; |
111 | 115 | ||
116 | void (*peer_on_join)(void *, int, int); | ||
117 | void (*peer_on_leave)(void *, int, int, void *); | ||
112 | struct { | 118 | struct { |
113 | int (*function)(Messenger *m, int, int, const uint8_t *, uint16_t, void *); | 119 | int (*function)(Messenger *m, int, int, const uint8_t *, uint16_t, void *); |
114 | void *userdata; | 120 | void *userdata; |
@@ -252,6 +258,46 @@ uint32_t copy_chatlist(Group_Chats *g_c, int32_t *out_list, uint32_t list_size); | |||
252 | */ | 258 | */ |
253 | void send_name_all_groups(Group_Chats *g_c); | 259 | void send_name_all_groups(Group_Chats *g_c); |
254 | 260 | ||
261 | /* Set the object that is tied to the group chat. | ||
262 | * | ||
263 | * return 0 on success. | ||
264 | * return -1 on failure | ||
265 | */ | ||
266 | int group_set_object(const Group_Chats *g_c, int groupnumber, void *object); | ||
267 | |||
268 | /* Set the object that is tied to the group peer. | ||
269 | * | ||
270 | * return 0 on success. | ||
271 | * return -1 on failure | ||
272 | */ | ||
273 | int group_peer_set_object(const Group_Chats *g_c, int groupnumber, int peernumber, void *object); | ||
274 | |||
275 | /* Return the object tide to the group chat previously set by group_set_object. | ||
276 | * | ||
277 | * return NULL on failure. | ||
278 | * return object on success. | ||
279 | */ | ||
280 | void *group_get_object(const Group_Chats *g_c, int groupnumber); | ||
281 | |||
282 | /* Return the object tide to the group chat peer previously set by group_peer_set_object. | ||
283 | * | ||
284 | * return NULL on failure. | ||
285 | * return object on success. | ||
286 | */ | ||
287 | void *group_peer_get_object(const Group_Chats *g_c, int groupnumber, int peernumber); | ||
288 | |||
289 | /* Set a function to be called when a new peer joins a group chat. | ||
290 | * | ||
291 | * Function(void *group object (set with group_set_object), int groupnumber, int friendgroupnumber) | ||
292 | */ | ||
293 | void callback_groupchat_peer_new(const Group_Chats *g_c, void (*function)(void *, int, int)); | ||
294 | |||
295 | /* Set a function to be called when a peer leaves a group chat. | ||
296 | * | ||
297 | * Function(void *group object (set with group_set_object), int groupnumber, int friendgroupnumber, void *group peer object (set with group_peer_set_object)) | ||
298 | */ | ||
299 | void callback_groupchat_peer_delete(const Group_Chats *g_c, void (*function)(void *, int, int, void *)); | ||
300 | |||
255 | /* Create new groupchat instance. */ | 301 | /* Create new groupchat instance. */ |
256 | Group_Chats *new_groupchats(Messenger *m); | 302 | Group_Chats *new_groupchats(Messenger *m); |
257 | 303 | ||