tox-weechat/src/tox-weechat-chats.c

189 lines
5.2 KiB
C
Raw Normal View History

2014-09-02 16:47:08 +00:00
#include <stdlib.h>
#include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h>
#include "tox-weechat.h"
2014-09-07 01:04:42 +00:00
#include "tox-weechat-tox.h"
2014-09-02 16:47:08 +00:00
#include "tox-weechat-utils.h"
#include "tox-weechat-chats.h"
int tox_weechat_buffer_input_callback(void *data,
struct t_gui_buffer *buffer,
const char *input_data);
int tox_weechat_buffer_close_callback(void *data,
struct t_gui_buffer *buffer);
void
2014-09-07 01:04:42 +00:00
tox_weechat_chat_add(struct t_tox_weechat_identity *identity,
struct t_tox_weechat_chat *chat)
2014-09-02 16:47:08 +00:00
{
2014-09-07 01:04:42 +00:00
chat->identity = identity;
chat->prev_chat = identity->last_chat;
chat->next_chat = NULL;
if (identity->chats == NULL)
identity->chats = chat;
2014-09-02 16:47:08 +00:00
else
2014-09-07 01:04:42 +00:00
identity->last_chat->next_chat = chat;
2014-09-02 16:47:08 +00:00
2014-09-07 01:04:42 +00:00
identity->last_chat = chat;
2014-09-02 16:47:08 +00:00
}
void
2014-09-07 01:04:42 +00:00
tox_weechat_chat_remove(struct t_tox_weechat_chat *chat)
2014-09-02 16:47:08 +00:00
{
2014-09-07 01:04:42 +00:00
if (chat->prev_chat)
chat->prev_chat->next_chat = chat->next_chat;
if (chat->next_chat)
chat->next_chat->prev_chat = chat->prev_chat;
2014-09-02 16:47:08 +00:00
2014-09-07 01:04:42 +00:00
if (chat == chat->identity->chats)
chat->identity->chats = chat->next_chat;
if (chat == chat->identity->last_chat)
chat->identity->last_chat = chat->prev_chat;
2014-09-02 16:47:08 +00:00
free(chat);
}
void
2014-09-07 01:04:42 +00:00
tox_weechat_chat_refresh(struct t_tox_weechat_chat *chat)
2014-09-02 16:47:08 +00:00
{
2014-09-07 01:04:42 +00:00
char *name = tox_weechat_get_name_nt(chat->identity->tox,
chat->friend_number);
char *status_message = tox_weechat_get_status_message_nt(chat->identity->tox,
chat->friend_number);
2014-09-02 16:47:08 +00:00
weechat_buffer_set(chat->buffer, "short_name", name);
weechat_buffer_set(chat->buffer, "title", status_message);
free(name);
free(status_message);
}
2014-09-07 01:04:42 +00:00
struct t_tox_weechat_chat *
tox_weechat_friend_chat_new(struct t_tox_weechat_identity *identity,
int32_t friend_number)
2014-09-02 16:47:08 +00:00
{
2014-09-07 01:04:42 +00:00
struct t_tox_weechat_chat *chat = malloc(sizeof(*chat));
2014-09-02 16:47:08 +00:00
chat->friend_number = friend_number;
uint8_t client_id[TOX_CLIENT_ID_SIZE];
2014-09-07 01:04:42 +00:00
tox_get_client_id(identity->tox, friend_number, client_id);
2014-09-02 16:47:08 +00:00
2014-09-07 01:04:42 +00:00
// TODO: prepend identity name
char *buffer_name = malloc(TOX_CLIENT_ID_SIZE * 2 + 1);
tox_weechat_bin2hex(client_id, TOX_CLIENT_ID_SIZE, buffer_name);
2014-09-02 16:47:08 +00:00
chat->buffer = weechat_buffer_new(buffer_name,
tox_weechat_buffer_input_callback, chat,
tox_weechat_buffer_close_callback, chat);
tox_weechat_chat_refresh(chat);
2014-09-07 01:04:42 +00:00
tox_weechat_chat_add(identity, chat);
2014-09-02 16:47:08 +00:00
free(buffer_name);
return chat;
}
2014-09-07 01:04:42 +00:00
struct t_tox_weechat_chat *
tox_weechat_get_existing_friend_chat(struct t_tox_weechat_identity *identity,
int32_t friend_number)
2014-09-02 16:47:08 +00:00
{
2014-09-07 01:04:42 +00:00
for (struct t_tox_weechat_chat *chat = identity->chats;
2014-09-02 16:47:08 +00:00
chat;
2014-09-07 01:04:42 +00:00
chat = chat->next_chat)
2014-09-02 16:47:08 +00:00
{
if (chat->friend_number == friend_number)
return chat;
}
2014-09-02 18:15:08 +00:00
return NULL;
}
2014-09-07 01:04:42 +00:00
struct t_tox_weechat_chat *
tox_weechat_get_friend_chat(struct t_tox_weechat_identity *identity,
int32_t friend_number)
2014-09-02 18:15:08 +00:00
{
2014-09-07 01:04:42 +00:00
struct t_tox_weechat_chat *chat = tox_weechat_get_existing_friend_chat(identity, friend_number);
2014-09-02 18:15:08 +00:00
if (chat)
return chat;
else
2014-09-07 01:04:42 +00:00
return tox_weechat_friend_chat_new(identity, friend_number);
2014-09-02 16:47:08 +00:00
}
2014-09-07 01:04:42 +00:00
struct t_tox_weechat_chat *
2014-09-02 16:47:08 +00:00
tox_weechat_get_chat_for_buffer(struct t_gui_buffer *buffer)
{
2014-09-07 01:04:42 +00:00
for (struct t_tox_weechat_identity *identity = tox_weechat_identities;
identity;
identity = identity->next_identity)
2014-09-02 16:47:08 +00:00
{
2014-09-07 01:04:42 +00:00
for (struct t_tox_weechat_chat *chat = identity->chats;
chat;
chat = chat->next_chat)
{
if (chat->buffer == buffer)
return chat;
}
2014-09-02 16:47:08 +00:00
}
return NULL;
}
void
2014-09-07 01:04:42 +00:00
tox_weechat_chat_print_message(struct t_tox_weechat_chat *chat,
2014-09-02 16:47:08 +00:00
const char *sender,
const char *message)
{
weechat_printf(chat->buffer, "%s\t%s", sender, message);
}
void
2014-09-07 01:04:42 +00:00
tox_weechat_chat_print_action(struct t_tox_weechat_chat *chat,
2014-09-02 16:47:08 +00:00
const char *sender,
const char *message)
{
weechat_printf(chat->buffer,
"%s%s %s",
weechat_prefix("action"),
sender, message);
}
int
tox_weechat_buffer_input_callback(void *data,
struct t_gui_buffer *weechat_buffer,
const char *input_data)
{
2014-09-07 01:04:42 +00:00
struct t_tox_weechat_chat *chat = data;
2014-09-02 16:47:08 +00:00
2014-09-07 01:04:42 +00:00
tox_send_message(chat->identity->tox,
2014-09-02 16:47:08 +00:00
chat->friend_number,
(uint8_t *)input_data,
strlen(input_data));
2014-09-07 01:04:42 +00:00
char *name = tox_weechat_get_self_name_nt(chat->identity->tox);
2014-09-02 16:47:08 +00:00
tox_weechat_chat_print_message(chat, name, input_data);
free(name);
return WEECHAT_RC_OK;
}
int
tox_weechat_buffer_close_callback(void *data,
struct t_gui_buffer *weechat_buffer)
{
2014-09-07 01:04:42 +00:00
struct t_tox_weechat_chat *chat = data;
2014-09-02 16:47:08 +00:00
tox_weechat_chat_remove(chat);
return WEECHAT_RC_OK;
}