tox-weechat/src/twc-chat.c

424 lines
12 KiB
C
Raw Normal View History

2014-09-28 03:29:34 +02:00
/*
2017-02-04 01:00:25 +01:00
* Copyright (c) 2017 Håvard Pettersson <mail@haavard.me>
2014-09-28 03:29:34 +02:00
*
* 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 <http://www.gnu.org/licenses/>.
*/
2014-10-04 20:57:29 +02:00
#include <stdio.h>
#include <string.h>
2014-09-28 03:29:34 +02:00
#include <weechat/weechat-plugin.h>
#include <tox/tox.h>
#include "twc.h"
2014-09-28 05:55:18 +02:00
#include "twc-list.h"
2014-09-28 03:29:34 +02:00
#include "twc-profile.h"
#include "twc-message-queue.h"
#include "twc-utils.h"
#include "twc-chat.h"
const char *twc_tag_unsent_message = "tox_unsent";
const char *twc_tag_sent_message = "tox_sent";
const char *twc_tag_received_message = "tox_received";
int
twc_chat_buffer_input_callback(const void *pointer,
void *data,
2014-11-19 07:12:41 +01:00
struct t_gui_buffer *weechat_buffer,
const char *input_data);
2014-09-28 03:29:34 +02:00
int
twc_chat_buffer_close_callback(const void *pointer,
void *data,
2014-11-19 07:12:41 +01:00
struct t_gui_buffer *weechat_buffer);
2014-09-28 03:29:34 +02:00
/**
2014-10-04 20:57:29 +02:00
* Create a new chat.
2014-09-28 03:29:34 +02:00
*/
struct t_twc_chat *
2014-11-19 07:12:41 +01:00
twc_chat_new(struct t_twc_profile *profile, const char *name)
2014-09-28 03:29:34 +02:00
{
struct t_twc_chat *chat = malloc(sizeof(struct t_twc_chat));
if (!chat)
return NULL;
chat->profile = profile;
2014-10-04 20:57:29 +02:00
chat->friend_number = chat->group_number = -1;
2014-10-05 05:22:59 +02:00
chat->nicks = NULL;
2014-09-28 03:29:34 +02:00
size_t full_name_size = strlen(profile->name) + 1 + strlen(name) + 1;
char *full_name = malloc(full_name_size);
snprintf(full_name, full_name_size, "%s/%s", profile->name, name);
chat->buffer = weechat_buffer_search("tox", full_name);
if (!(chat->buffer))
{
chat->buffer = weechat_buffer_new(full_name,
twc_chat_buffer_input_callback, chat, NULL,
twc_chat_buffer_close_callback, chat, NULL);
}
else
{
weechat_buffer_set_pointer(chat->buffer,
"input_callback",
twc_chat_buffer_input_callback);
weechat_buffer_set_pointer(chat->buffer, "input_callback_pointer", chat);
weechat_buffer_set_pointer(chat->buffer,
"close_callback",
twc_chat_buffer_close_callback);
weechat_buffer_set_pointer(chat->buffer, "close_callback_pointer", chat);
}
2016-12-22 13:35:08 +01:00
free(full_name);
2014-09-28 03:29:34 +02:00
if (!(chat->buffer))
{
free(chat);
return NULL;
}
twc_chat_queue_refresh(chat);
twc_list_item_new_data_add(profile->chats, chat);
return chat;
}
2014-10-04 20:57:29 +02:00
/**
* Create a new friend chat.
*/
struct t_twc_chat *
2014-11-19 07:12:41 +01:00
twc_chat_new_friend(struct t_twc_profile *profile, int32_t friend_number)
2014-10-04 20:57:29 +02:00
{
uint8_t client_id[TOX_PUBLIC_KEY_SIZE];
TOX_ERR_FRIEND_GET_PUBLIC_KEY err;
tox_friend_get_public_key(profile->tox, friend_number, client_id, &err);
if (err != TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK)
return NULL;
2014-10-04 20:57:29 +02:00
char buffer_name[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(client_id, TOX_PUBLIC_KEY_SIZE, buffer_name);
2014-10-04 20:57:29 +02:00
2014-10-05 05:22:59 +02:00
struct t_twc_chat *chat = twc_chat_new(profile, buffer_name);
2014-10-04 20:57:29 +02:00
if (chat)
chat->friend_number = friend_number;
return chat;
}
/**
2014-10-05 05:22:59 +02:00
* Create a new group chat.
2014-10-04 20:57:29 +02:00
*/
struct t_twc_chat *
2014-11-19 07:12:41 +01:00
twc_chat_new_group(struct t_twc_profile *profile, int32_t group_number)
2014-10-04 20:57:29 +02:00
{
char buffer_name[32];
sprintf(buffer_name, "group_chat_%d", group_number);
2014-10-05 05:22:59 +02:00
struct t_twc_chat *chat = twc_chat_new(profile, buffer_name);
2014-10-04 20:57:29 +02:00
if (chat)
{
2014-10-04 20:57:29 +02:00
chat->group_number = group_number;
chat->nicklist_group = weechat_nicklist_add_group(chat->buffer, NULL,
NULL, NULL, true);
2017-01-03 13:41:35 +01:00
chat->nicks = weechat_list_new();
2014-10-05 05:22:59 +02:00
weechat_buffer_set(chat->buffer, "nicklist", "1");
}
2014-10-05 05:22:59 +02:00
2014-10-04 20:57:29 +02:00
return chat;
}
2014-09-28 03:29:34 +02:00
/**
* Refresh a chat. Updates buffer short_name and title.
*/
void
twc_chat_refresh(const struct t_twc_chat *chat)
2014-09-28 03:29:34 +02:00
{
2014-10-04 20:57:29 +02:00
char *name = NULL;
char *title = NULL;
2016-12-22 13:35:08 +01:00
bool rc = false;
TOX_ERR_CONFERENCE_TITLE err = TOX_ERR_CONFERENCE_TITLE_OK;
2014-10-04 20:57:29 +02:00
if (chat->friend_number >= 0)
{
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)
{
char group_name[TOX_MAX_NAME_LENGTH + 1] = {0};
2016-12-22 13:35:08 +01:00
int len = tox_conference_get_title_size(chat->profile->tox, chat->group_number,
&err);
if ((err == TOX_ERR_CONFERENCE_TITLE_OK) && (len <= TOX_MAX_NAME_LENGTH))
rc = tox_conference_get_title(chat->profile->tox, chat->group_number,
(uint8_t *)group_name, &err);
if (!rc)
sprintf(group_name, "Group Chat %d", chat->group_number);
name = title = strdup((char *)group_name);
2014-10-04 20:57:29 +02:00
}
2014-09-28 03:29:34 +02:00
weechat_buffer_set(chat->buffer, "short_name", name);
2014-10-04 20:57:29 +02:00
weechat_buffer_set(chat->buffer, "title", title);
2014-09-28 03:29:34 +02:00
2014-11-18 16:03:49 +01:00
if (name)
free(name);
if (title && title != name)
free(title);
2014-09-28 03:29:34 +02:00
}
/**
* Callback for twc_chat_queue_refresh. Simply calls twc_chat_refresh.
*/
int
twc_chat_refresh_timer_callback(const void *pointer, void *data, int remaining)
2014-09-28 03:29:34 +02:00
{
twc_chat_refresh(pointer);
2014-09-28 03:29:34 +02:00
return WEECHAT_RC_OK;
}
/**
* Queue a refresh of the buffer in 1ms (i.e. the next event loop tick). Done
* this way to allow data to update before refreshing interface.
*/
void
twc_chat_queue_refresh(struct t_twc_chat *chat)
{
weechat_hook_timer(1, 0, 1,
twc_chat_refresh_timer_callback, chat, NULL);
2014-09-28 03:29:34 +02:00
}
/**
* Find an existing chat object for a friend, and if not found, optionally
* create a new one.
*/
struct t_twc_chat *
twc_chat_search_friend(struct t_twc_profile *profile,
2014-11-19 07:12:41 +01:00
int32_t friend_number, bool create_new)
2014-09-28 03:29:34 +02:00
{
size_t index;
struct t_twc_list_item *item;
twc_list_foreach(profile->chats, index, item)
{
if (item->chat->friend_number == friend_number)
return item->chat;
}
if (create_new)
return twc_chat_new_friend(profile, friend_number);
2014-10-04 20:57:29 +02:00
return NULL;
}
/**
2016-05-11 08:41:51 +02:00
* Find an existing chat object for a group, and if not found, optionally
2014-10-04 20:57:29 +02:00
* create a new one.
*/
struct t_twc_chat *
twc_chat_search_group(struct t_twc_profile *profile,
2014-11-19 07:12:41 +01:00
int32_t group_number, bool create_new)
2014-10-04 20:57:29 +02:00
{
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;
2014-09-28 03:29:34 +02:00
}
2014-10-04 20:57:29 +02:00
if (create_new)
2014-10-04 23:13:49 +02:00
return twc_chat_new_group(profile, group_number);
2014-10-04 20:57:29 +02:00
2014-09-28 03:29:34 +02:00
return NULL;
}
/**
* Find the chat object associated with a buffer, if it exists.
*/
struct t_twc_chat *
twc_chat_search_buffer(struct t_gui_buffer *buffer)
{
size_t profile_index;
struct t_twc_list_item *profile_item;
twc_list_foreach(twc_profiles, profile_index, profile_item)
{
size_t chat_index;
struct t_twc_list_item *chat_item;
twc_list_foreach(profile_item->profile->chats, chat_index, chat_item)
{
if (chat_item->chat->buffer == buffer)
{
return chat_item->chat;
}
}
}
return NULL;
}
/**
* Print a chat message to a chat's buffer.
*/
void
twc_chat_print_message(struct t_twc_chat *chat,
const char *tags,
2016-05-10 11:29:50 +02:00
const char *color,
2014-09-28 03:29:34 +02:00
const char *sender,
const char *message,
2016-12-22 13:35:08 +01:00
TOX_MESSAGE_TYPE message_type)
2014-09-28 03:29:34 +02:00
{
switch (message_type)
{
2016-12-22 13:35:08 +01:00
case TOX_MESSAGE_TYPE_NORMAL:
weechat_printf_date_tags(chat->buffer, 0, tags,
2016-05-10 11:29:50 +02:00
"%s%s%s\t%s",
color, sender,
weechat_color("reset"), message);
2014-09-28 03:29:34 +02:00
break;
2016-12-22 13:35:08 +01:00
case TOX_MESSAGE_TYPE_ACTION:
weechat_printf_date_tags(chat->buffer, 0, tags,
2016-05-10 11:55:06 +02:00
"%s%s%s%s %s",
2014-09-28 03:29:34 +02:00
weechat_prefix("action"),
2016-05-10 11:29:50 +02:00
color, sender,
weechat_color("reset"), message);
2014-09-28 03:29:34 +02:00
break;
}
}
/**
* Send a message to the recipient(s) of a chat.
*/
void
2014-11-19 07:12:41 +01:00
twc_chat_send_message(struct t_twc_chat *chat, const char *message,
2016-12-22 13:35:08 +01:00
TOX_MESSAGE_TYPE message_type)
2014-09-28 03:29:34 +02:00
{
2016-12-22 13:35:08 +01:00
TOX_ERR_CONFERENCE_SEND_MESSAGE err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK;
if (chat->friend_number >= 0)
{
twc_message_queue_add_friend_message(chat->profile,
chat->friend_number,
message, message_type);
2014-10-05 02:16:43 +02:00
char *name = twc_get_self_name_nt(chat->profile->tox);
2016-12-05 00:44:28 +01:00
twc_chat_print_message(chat, "notify_message",
2016-05-10 11:29:50 +02:00
weechat_color("chat_nick_self"), name,
message, message_type);
2014-10-05 02:16:43 +02:00
free(name);
}
else if (chat->group_number >= 0)
{
2016-12-22 13:35:08 +01:00
int len = strlen(message);
while (len > 0)
2016-12-03 14:05:59 +01:00
{
2016-12-22 13:35:08 +01:00
int fit_len = twc_fit_utf8(message, TWC_MAX_GROUP_MESSAGE_LENGTH);
err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK;
tox_conference_send_message(chat->profile->tox, chat->group_number,
message_type, (uint8_t *)message, fit_len,
&err);
if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK)
break;
message += fit_len;
len -= fit_len;
2016-12-03 14:05:59 +01:00
}
2016-12-22 13:35:08 +01:00
if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK)
2016-12-03 14:05:59 +01:00
{
weechat_printf(chat->buffer,
2016-12-22 13:35:08 +01:00
"%s%sFailed to send message with error %d%s",
2016-12-03 14:05:59 +01:00
weechat_prefix("error"),
weechat_color("chat_highlight"),
2016-12-22 13:35:08 +01:00
err,
2016-12-03 14:05:59 +01:00
weechat_color("reset"));
}
}
2014-09-28 03:29:34 +02:00
}
/**
* Callback for a buffer receiving user input.
*/
int
twc_chat_buffer_input_callback(const void *pointer, void *data,
struct t_gui_buffer *weechat_buffer,
2014-09-28 03:29:34 +02:00
const char *input_data)
{
/* TODO: don't strip the const */
struct t_twc_chat *chat = (void *)pointer;
2016-12-22 13:35:08 +01:00
twc_chat_send_message(chat, input_data, TOX_MESSAGE_TYPE_NORMAL);
2014-09-28 03:29:34 +02:00
return WEECHAT_RC_OK;
}
/**
* Callback for a buffer being closed.
*/
int
twc_chat_buffer_close_callback(const void *pointer, void *data,
struct t_gui_buffer *weechat_buffer)
2014-09-28 03:29:34 +02:00
{
/* TODO: don't strip the const */
2016-12-22 13:35:08 +01:00
TOX_ERR_CONFERENCE_DELETE err = TOX_ERR_CONFERENCE_DELETE_OK;
struct t_twc_chat *chat = (void *)pointer;
2014-09-28 03:29:34 +02:00
if (chat->profile->tox && chat->group_number >= 0)
{
2016-12-22 13:35:08 +01:00
tox_conference_delete(chat->profile->tox, chat->group_number, &err);
if (err != TOX_ERR_CONFERENCE_DELETE_OK)
{
weechat_printf(chat->profile->buffer,
2016-12-22 13:35:08 +01:00
"%swarning: failed to leave group chat with error %d",
weechat_prefix("error"), err);
}
}
2014-09-28 03:29:34 +02:00
twc_list_remove_with_data(chat->profile->chats, chat);
2014-10-05 05:22:59 +02:00
twc_chat_free(chat);
2014-09-28 03:29:34 +02:00
return WEECHAT_RC_OK;
}
2014-10-05 05:22:59 +02:00
/**
* Free a chat object.
*/
void
twc_chat_free(struct t_twc_chat *chat)
{
weechat_nicklist_remove_all(chat->buffer);
2014-10-05 05:22:59 +02:00
if (chat->nicks)
2017-01-03 13:41:35 +01:00
{
weechat_list_remove_all(chat->nicks);
weechat_list_free(chat->nicks);
}
2014-10-05 05:22:59 +02:00
free(chat);
}
2014-09-28 03:29:34 +02:00
/**
* Free all chats connected to a profile.
*/
void
2014-10-04 21:42:30 +02:00
twc_chat_free_list(struct t_twc_list *list)
2014-09-28 03:29:34 +02:00
{
struct t_twc_chat *chat;
2014-10-04 21:42:30 +02:00
while ((chat = twc_list_pop(list)))
2014-09-28 03:29:34 +02:00
{
weechat_buffer_set_pointer(chat->buffer, "close_callback", NULL);
weechat_buffer_close(chat->buffer);
2014-10-05 05:22:59 +02:00
twc_chat_free(chat);
2014-09-28 03:29:34 +02:00
}
2014-10-04 21:42:30 +02:00
free(list);
2014-09-28 03:29:34 +02:00
}