Added group chat code.

This commit is contained in:
Håvard Pettersson 2014-10-04 20:57:29 +02:00
parent 225e576d57
commit 4c6e739527
2 changed files with 91 additions and 21 deletions

View File

@ -17,6 +17,9 @@
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>. * along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdio.h>
#include <string.h>
#include <weechat/weechat-plugin.h> #include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
@ -40,29 +43,22 @@ int
twc_chat_buffer_close_callback(void *data, twc_chat_buffer_close_callback(void *data,
struct t_gui_buffer *weechat_buffer); struct t_gui_buffer *weechat_buffer);
/** /**
* Create a new friend chat. * Create a new chat.
*/ */
struct t_twc_chat * struct t_twc_chat *
twc_chat_new_friend(struct t_twc_profile *profile, twc_chat_create(struct t_twc_profile *profile,
int32_t friend_number) const char *name)
{ {
struct t_twc_chat *chat = malloc(sizeof(struct t_twc_chat)); struct t_twc_chat *chat = malloc(sizeof(struct t_twc_chat));
if (!chat) if (!chat)
return NULL; return NULL;
chat->friend_number = friend_number;
chat->profile = profile; chat->profile = profile;
chat->friend_number = chat->group_number = -1;
uint8_t client_id[TOX_CLIENT_ID_SIZE];
tox_get_client_id(profile->tox, friend_number, client_id);
// TODO: prepend profile name // TODO: prepend profile name
char buffer_name[TOX_CLIENT_ID_SIZE * 2 + 1]; chat->buffer = weechat_buffer_new(name,
twc_bin2hex(client_id, TOX_CLIENT_ID_SIZE, buffer_name);
chat->buffer = weechat_buffer_new(buffer_name,
twc_chat_buffer_input_callback, chat, twc_chat_buffer_input_callback, chat,
twc_chat_buffer_close_callback, chat); twc_chat_buffer_close_callback, chat);
@ -78,22 +74,70 @@ twc_chat_new_friend(struct t_twc_profile *profile,
return chat; return chat;
} }
/**
* Create a new friend chat.
*/
struct t_twc_chat *
twc_chat_new_friend(struct t_twc_profile *profile,
int32_t friend_number)
{
uint8_t client_id[TOX_CLIENT_ID_SIZE];
tox_get_client_id(profile->tox, friend_number, client_id);
char buffer_name[TOX_CLIENT_ID_SIZE * 2 + 1];
twc_bin2hex(client_id, TOX_CLIENT_ID_SIZE, buffer_name);
struct t_twc_chat *chat = twc_chat_create(profile, buffer_name);
if (chat)
chat->friend_number = friend_number;
return chat;
}
/**
* Create a new friend chat.
*/
struct t_twc_chat *
twc_chat_new_group(struct t_twc_profile *profile,
int32_t group_number)
{
char buffer_name[32];
sprintf(buffer_name, "group_chat_%d", group_number);
struct t_twc_chat *chat = twc_chat_create(profile, buffer_name);
if (chat)
chat->group_number = group_number;
return chat;
}
/** /**
* Refresh a chat. Updates buffer short_name and title. * Refresh a chat. Updates buffer short_name and title.
*/ */
void void
twc_chat_refresh(struct t_twc_chat *chat) twc_chat_refresh(struct t_twc_chat *chat)
{ {
char *name = twc_get_name_nt(chat->profile->tox, char *name = NULL;
chat->friend_number); char *title = NULL;
char *status_message = twc_get_status_message_nt(chat->profile->tox, if (chat->friend_number >= 0)
chat->friend_number); {
name = twc_get_name_nt(chat->profile->tox,
chat->friend_number);
title = twc_get_status_message_nt(chat->profile->tox,
chat->friend_number);
}
else if (chat->group_number >= 0)
{
name = malloc(sizeof(char) * 32);
sprintf(name, "Group Chat %d", chat->group_number);
title = strdup(name);
}
weechat_buffer_set(chat->buffer, "short_name", name); weechat_buffer_set(chat->buffer, "short_name", name);
weechat_buffer_set(chat->buffer, "title", status_message); weechat_buffer_set(chat->buffer, "title", title);
free(name); free(name);
free(status_message); free(title);
} }
/** /**
@ -136,10 +180,31 @@ twc_chat_search_friend(struct t_twc_profile *profile,
} }
if (create_new) if (create_new)
{
return twc_chat_new_friend(profile, friend_number); return twc_chat_new_friend(profile, friend_number);
return NULL;
}
/**
* Find an existing chat object for a friend, and if not found, optionally
* create a new one.
*/
struct t_twc_chat *
twc_chat_search_group(struct t_twc_profile *profile,
int32_t group_number,
bool create_new)
{
size_t index;
struct t_twc_list_item *item;
twc_list_foreach(profile->chats, index, item)
{
if (item->chat->group_number == group_number)
return item->chat;
} }
if (create_new)
return twc_chat_new_friend(profile, group_number);
return NULL; return NULL;
} }
@ -200,6 +265,7 @@ twc_chat_send_message(struct t_twc_chat *chat,
const char *message, const char *message,
enum TWC_MESSAGE_TYPE message_type) enum TWC_MESSAGE_TYPE message_type)
{ {
// TODO: fix for group messages
twc_message_queue_add_friend_message(chat->profile, twc_message_queue_add_friend_message(chat->profile,
chat->friend_number, chat->friend_number,
message, message_type); message, message_type);
@ -233,7 +299,6 @@ twc_chat_buffer_close_callback(void *data,
struct t_twc_chat *chat = data; struct t_twc_chat *chat = data;
twc_list_remove_with_data(chat->profile->chats, chat); twc_list_remove_with_data(chat->profile->chats, chat);
free(chat); free(chat);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;

View File

@ -39,7 +39,7 @@ struct t_twc_chat
struct t_gui_buffer *buffer; struct t_gui_buffer *buffer;
int32_t friend_number; int32_t friend_number;
int32_t group_number;
}; };
struct t_twc_chat * struct t_twc_chat *
@ -47,6 +47,11 @@ twc_chat_search_friend(struct t_twc_profile *profile,
int32_t friend_number, int32_t friend_number,
bool create_new); bool create_new);
struct t_twc_chat *
twc_chat_search_group(struct t_twc_profile *profile,
int32_t group_number,
bool create_new);
struct t_twc_chat * struct t_twc_chat *
twc_chat_search_buffer(struct t_gui_buffer *target_buffer); twc_chat_search_buffer(struct t_gui_buffer *target_buffer);