From f81176881fcc4361276dff6008becf40f3dacca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Sat, 4 Oct 2014 21:42:30 +0200 Subject: [PATCH] Wrote group chat invite code. --- CMakeLists.txt | 1 + src/twc-chat.c | 6 +-- src/twc-chat.h | 2 +- src/twc-group-invite.c | 110 +++++++++++++++++++++++++++++++++++++++++ src/twc-group-invite.h | 64 ++++++++++++++++++++++++ src/twc-list.h | 1 + src/twc-profile.c | 5 +- src/twc-profile.h | 1 + 8 files changed, 185 insertions(+), 5 deletions(-) create mode 100644 src/twc-group-invite.c create mode 100644 src/twc-group-invite.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 411a54f..0b3c909 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ add_library(tox MODULE src/twc-config.c src/twc-friend-request.c src/twc-gui.c + src/twc-group-invite.c src/twc-list.c src/twc-message-queue.c src/twc-profile.c diff --git a/src/twc-chat.c b/src/twc-chat.c index ee2752e..ad9bef5 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -308,16 +308,16 @@ twc_chat_buffer_close_callback(void *data, * Free all chats connected to a profile. */ void -twc_chat_free_profile(struct t_twc_profile *profile) +twc_chat_free_list(struct t_twc_list *list) { struct t_twc_chat *chat; - while ((chat = twc_list_pop(profile->chats))) + while ((chat = twc_list_pop(list))) { weechat_buffer_set_pointer(chat->buffer, "close_callback", NULL); weechat_buffer_close(chat->buffer); free(chat); } - free(profile->chats); + free(list); } diff --git a/src/twc-chat.h b/src/twc-chat.h index f64fe4b..bbe3091 100644 --- a/src/twc-chat.h +++ b/src/twc-chat.h @@ -71,7 +71,7 @@ void twc_chat_queue_refresh(struct t_twc_chat *chat); void -twc_chat_free_profile(struct t_twc_profile *profile); +twc_chat_free_list(struct t_twc_list *list); #endif // TOX_WEECHAT_CHAT_H diff --git a/src/twc-group-invite.c b/src/twc-group-invite.c new file mode 100644 index 0000000..f57f136 --- /dev/null +++ b/src/twc-group-invite.c @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2014 Håvard Pettersson + * + * This file is part of Tox-WeeChat. + * + * Tox-WeeChat is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox-WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox-WeeChat. If not, see . + */ + +#include + +#include +#include + +#include "twc.h" +#include "twc-list.h" +#include "twc-profile.h" +#include "twc-utils.h" + +#include "twc-group-invite.h" + +/** + * Add a new group invite to a profile. + * + * Returns 0 on success, -1 on error. + */ +int +twc_group_chat_invite_add(struct t_twc_profile *profile, + int32_t friend_number, + const uint8_t *data, + size_t size) +{ + // create a new invite object + struct t_twc_group_chat_invite *invite + = malloc(sizeof(struct t_twc_group_chat_invite)); + if (!invite) + return -1; + + invite->profile = profile; + invite->data = data; + invite->data_size = size; + + return 0; +} + +/** + * Accept a group chat invite. Remove and free the invite. + */ +void +twc_group_chat_invite_accept(struct t_twc_group_chat_invite *invite) +{ + tox_join_groupchat(invite->profile->tox, + invite->friend_number, + invite->data, + invite->data_size); + twc_group_chat_invite_remove(invite); +} + +/** + * Remove and free a group chat invite. + */ +void +twc_group_chat_invite_remove(struct t_twc_group_chat_invite *invite) +{ + twc_list_remove_with_data(invite->profile->group_chat_invites, invite); + twc_group_chat_invite_free(invite); +} + +/** + * Get group chat invite with a given index. + */ +struct t_twc_group_chat_invite * +twc_group_chat_invite_with_index(struct t_twc_profile *profile, + int64_t index) +{ + return twc_list_get(profile->group_chat_invites, index)->group_chat_invite; +} + +/** + * Free a group chat invite. + */ +void +twc_group_chat_invite_free(struct t_twc_group_chat_invite *invite) +{ + free(invite); +} + +/** + * Free a list of group chat invites. + */ +void +twc_group_chat_invite_free_list(struct t_twc_list *list) +{ + struct t_twc_group_chat_invite *invite; + while ((invite = twc_list_pop(list))) + twc_group_chat_invite_free(invite); + + free(list); +} + diff --git a/src/twc-group-invite.h b/src/twc-group-invite.h new file mode 100644 index 0000000..ba6d1b0 --- /dev/null +++ b/src/twc-group-invite.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2014 Håvard Pettersson + * + * This file is part of Tox-WeeChat. + * + * Tox-WeeChat is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox-WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox-WeeChat. If not, see . + */ + +#ifndef TOX_WEECHAT_GROUP_INVITE_H +#define TOX_WEECHAT_GROUP_INVITE_H + +#include + +#include + +struct t_twc_list; + +/** + * Represents a group chat invite. + */ +struct t_twc_group_chat_invite +{ + struct t_twc_profile *profile; + + int32_t friend_number; + const uint8_t *data; + size_t data_size; +}; + +int +twc_group_chat_invite_add(struct t_twc_profile *profile, + int32_t friend_number, + const uint8_t *data, + size_t size); + +void +twc_group_chat_invite_accept(struct t_twc_group_chat_invite *invite); + +void +twc_group_chat_invite_remove(struct t_twc_group_chat_invite *invite); + +struct t_twc_group_chat_invite * +twc_group_chat_invite_with_index(struct t_twc_profile *profile, + int64_t index); + +void +twc_group_chat_invite_free(struct t_twc_group_chat_invite *invite); + +void +twc_group_chat_invite_free_list(struct t_twc_list *list); + +#endif // TOX_WEECHAT_GROUP_INVITE_H + diff --git a/src/twc-list.h b/src/twc-list.h index 5d09192..80e0ae7 100644 --- a/src/twc-list.h +++ b/src/twc-list.h @@ -40,6 +40,7 @@ struct t_twc_list_item void *data; struct t_twc_profile *profile; struct t_twc_friend_request *friend_request; + struct t_twc_group_chat_invite *group_chat_invite; struct t_twc_chat *chat; struct t_twc_queued_message *queued_message; }; diff --git a/src/twc-profile.c b/src/twc-profile.c index 607b3b0..1c8859c 100644 --- a/src/twc-profile.c +++ b/src/twc-profile.c @@ -30,6 +30,7 @@ #include "twc-bootstrap.h" #include "twc-config.h" #include "twc-friend-request.h" +#include "twc-group-invite.h" #include "twc-message-queue.h" #include "twc-chat.h" #include "twc-tox-callbacks.h" @@ -177,6 +178,7 @@ twc_profile_new(const char *name) profile->tox_online = false; profile->chats = twc_list_new(); + profile->group_chat_invites = twc_list_new(); profile->message_queues = weechat_hashtable_new(32, WEECHAT_HASHTABLE_INTEGER, WEECHAT_HASHTABLE_POINTER, @@ -432,7 +434,8 @@ twc_profile_free(struct t_twc_profile *profile) } // free things - twc_chat_free_profile(profile); + twc_chat_free_list(profile->chats); + twc_group_chat_invite_free_list(profile->group_chat_invites); twc_message_queue_free_profile(profile); free(profile->name); free(profile); diff --git a/src/twc-profile.h b/src/twc-profile.h index b186817..f09c219 100644 --- a/src/twc-profile.h +++ b/src/twc-profile.h @@ -47,6 +47,7 @@ struct t_twc_profile struct t_hook *tox_do_timer; struct t_twc_list *chats; + struct t_twc_list *group_chat_invites; struct t_hashtable *message_queues; };