master
Håvard Pettersson 10 years ago
parent 8fdcc5173f
commit ca6a80f4cb

@ -8,6 +8,10 @@ add_library(tox SHARED
src/tox-weechat.c
src/tox-weechat-tox.c
src/tox-weechat-utils.c
src/tox-weechat-commands.c
src/tox-weechat-gui.c
src/tox-weechat-chats.c
src/tox-weechat-friend-requests.c
)
# remove lib prefix

@ -0,0 +1,168 @@
#include <stdlib.h>
#include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h>
#include "tox-weechat.h"
#include "tox-weechat-utils.h"
#include "tox-weechat-chats.h"
struct t_tox_chat *tox_weechat_chats_head = NULL;
struct t_tox_chat *tox_weechat_chats_tail = NULL;
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
tox_weechat_chat_add(struct t_tox_chat *chat)
{
if (tox_weechat_chats_head == NULL)
{
tox_weechat_chats_head = tox_weechat_chats_tail = chat;
chat->prev = chat->next = NULL;
}
else
{
tox_weechat_chats_tail->next = chat;
chat->prev = tox_weechat_chats_tail;
chat->next = NULL;
tox_weechat_chats_tail = chat;
}
}
void
tox_weechat_chat_remove(struct t_tox_chat *chat)
{
if (chat->prev)
chat->prev->next = chat->next;
if (chat->next)
chat->next->prev = chat->prev;
if (chat == tox_weechat_chats_head)
tox_weechat_chats_head = chat->next;
if (chat == tox_weechat_chats_tail)
tox_weechat_chats_tail = chat->prev;
free(chat);
}
void
tox_weechat_chat_refresh(struct t_tox_chat *chat)
{
char *name = tox_weechat_get_name_nt(chat->friend_number);
char *status_message = tox_weechat_get_status_message_nt(chat->friend_number);
weechat_buffer_set(chat->buffer, "short_name", name);
weechat_buffer_set(chat->buffer, "title", status_message);
free(name);
free(status_message);
}
struct t_tox_chat *
tox_weechat_friend_chat_new(int32_t friend_number)
{
struct t_tox_chat *chat = malloc(sizeof(*chat));
chat->friend_number = friend_number;
uint8_t client_id[TOX_CLIENT_ID_SIZE];
tox_get_client_id(tox, friend_number, client_id);
char *buffer_name = tox_weechat_bin2hex(client_id, TOX_CLIENT_ID_SIZE);
chat->buffer = weechat_buffer_new(buffer_name,
tox_weechat_buffer_input_callback, chat,
tox_weechat_buffer_close_callback, chat);
tox_weechat_chat_refresh(chat);
tox_weechat_chat_add(chat);
free(buffer_name);
return chat;
}
struct t_tox_chat *
tox_weechat_get_friend_chat(int32_t friend_number)
{
for (struct t_tox_chat *chat = tox_weechat_chats_head;
chat;
chat = chat->next)
{
if (chat->friend_number == friend_number)
return chat;
}
return tox_weechat_friend_chat_new(friend_number);
}
struct t_tox_chat *
tox_weechat_get_chat_for_buffer(struct t_gui_buffer *buffer)
{
for (struct t_tox_chat *chat = tox_weechat_chats_head;
chat;
chat = chat->next)
{
if (chat->buffer == buffer)
return chat;
}
return NULL;
}
void
tox_weechat_chat_print_message(struct t_tox_chat *chat,
const char *sender,
const char *message)
{
weechat_printf(chat->buffer, "%s\t%s", sender, message);
}
void
tox_weechat_chat_print_action(struct t_tox_chat *chat,
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)
{
struct t_tox_chat *chat = data;
tox_send_message(tox,
chat->friend_number,
(uint8_t *)input_data,
strlen(input_data));
char *name = tox_weechat_get_self_name_nt();
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)
{
struct t_tox_chat *chat = data;
tox_weechat_chat_remove(chat);
return WEECHAT_RC_OK;
}

@ -0,0 +1,29 @@
#ifndef TOX_WEECHAT_CHATS_H
#define TOX_WEECHAT_CHATS_H
#include <stdint.h>
#include <tox/tox.h>
struct t_tox_chat
{
struct t_gui_buffer *buffer;
int32_t friend_number;
// linked list pointers
struct t_tox_chat *next;
struct t_tox_chat *prev;
};
struct t_tox_chat *tox_weechat_get_friend_chat(int32_t friend_number);
struct t_tox_chat *tox_weechat_get_chat_for_buffer(struct t_gui_buffer *target_buffer);
void tox_weechat_chat_print_message(struct t_tox_chat *chat,
const char *sender,
const char *message);
void tox_weechat_chat_print_action(struct t_tox_chat *chat,
const char *sender,
const char *message);
#endif // TOX_WEECHAT_CHATS_H

@ -0,0 +1,221 @@
#include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h>
#include "tox-weechat.h"
#include "tox-weechat-utils.h"
#include "tox-weechat-chats.h"
#include "tox-weechat-friend-requests.h"
#include "tox-weechat-commands.h"
int
tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
// /friend [list]
if (argc == 1 || (argc == 2 && weechat_strcasecmp(argv[1], "list") == 0))
{
weechat_printf(tox_main_buffer, "TODO: friend list");
return WEECHAT_RC_OK;
}
if (argc == 3 && (weechat_strcasecmp(argv[1], "add") == 0))
{
weechat_printf(tox_main_buffer, "TODO: friend add");
return WEECHAT_RC_OK;
}
// /friend accept
if (argc == 3 &&
(weechat_strcasecmp(argv[1], "accept") == 0
|| weechat_strcasecmp(argv[1], "decline")))
{
int accept = weechat_strcasecmp(argv[1], "accept") == 0;
struct t_tox_friend_request *request;
if (weechat_strcasecmp(argv[2], "all") == 0)
{
int count = 0;
while ((request = tox_weechat_friend_request_with_num(1)) != NULL)
{
if (accept)
tox_weechat_accept_friend_request(request);
else
tox_weechat_decline_friend_request(request);
++count;
}
weechat_printf(tox_main_buffer,
"%s%s %d friend requests.",
weechat_prefix("network"),
accept ? "Accepted" : "Declined",
count);
return WEECHAT_RC_OK;
}
else
{
unsigned int num = atoi(argv[1]);
if (num == 0 || (request = tox_weechat_friend_request_with_num(num)) == NULL)
{
weechat_printf(tox_main_buffer,
"%sInvalid friend request ID.",
weechat_prefix("error"));
return WEECHAT_RC_OK;
}
if (accept)
tox_weechat_accept_friend_request(request);
else
tox_weechat_decline_friend_request(request);
char *hex_id = tox_weechat_bin2hex(request->address, TOX_CLIENT_ID_SIZE);
weechat_printf(tox_main_buffer,
"%s%s friend request from %s.",
weechat_prefix("network"),
accept ? "Accepted" : "Declined",
hex_id);
free(hex_id);
return WEECHAT_RC_OK;
}
}
if (argc == 2 && weechat_strcasecmp(argv[1], "requests") == 0)
{
struct t_tox_friend_request *request = tox_weechat_first_friend_request();
if (request == NULL)
{
weechat_printf(tox_main_buffer,
"%sNo pending friend requests :(",
weechat_prefix("network"));
}
else
{
weechat_printf(tox_main_buffer,
"%sPending friend requests:",
weechat_prefix("network"));
unsigned int num = 1;
while (request)
{
char *hex_address = tox_weechat_bin2hex(request->address,
TOX_CLIENT_ID_SIZE);
weechat_printf(tox_main_buffer,
"%s[%d] Address: %s\n"
"[%d] Message: %s",
weechat_prefix("network"),
num, hex_address,
num, request->message);
free(hex_address);
request = request->next;
++num;
}
}
return WEECHAT_RC_OK;
}
return WEECHAT_RC_ERROR;
}
int
tox_weechat_cmd_myaddress(void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
tox_get_address(tox, address);
char *address_str = tox_weechat_bin2hex(address, TOX_FRIEND_ADDRESS_SIZE);
weechat_printf(tox_main_buffer,
"%sYour Tox address: %s",
weechat_prefix("network"),
address_str);
free(address_str);
return WEECHAT_RC_OK;
}
int
tox_weechat_cmd_me(void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
struct t_tox_chat *chat = tox_weechat_get_chat_for_buffer(buffer);
tox_send_message(tox,
chat->friend_number,
(uint8_t *)argv_eol[1],
strlen(argv_eol[1]));
char *name = tox_weechat_get_self_name_nt();
tox_weechat_chat_print_action(chat, name, argv_eol[1]);
free(name);
return WEECHAT_RC_OK;
}
int
tox_weechat_cmd_name(void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
char *message = argv_eol[1];
int result = tox_set_name(tox, (uint8_t *)message, strlen(message));
if (result == -1)
{
weechat_printf(tox_main_buffer,
"%s%s",
weechat_prefix("error"),
"Could not change name.");
return WEECHAT_RC_ERROR;
}
weechat_bar_item_update("input_prompt");
return WEECHAT_RC_OK;
}
void
tox_weechat_commands_init()
{
weechat_hook_command("friend",
"manage friends",
"list"
" || add <address>"
" || requests"
" || accept <number>|all"
" || decline <number>|all",
" list: list all friends\n"
" add: add a friend by their public Tox address\n"
"requests: list friend requests\n"
" accept: accept friend requests\n"
" decline: decline friend requests\n",
NULL, tox_weechat_cmd_friend, NULL);
weechat_hook_command("me",
"send an action to the current chat",
"<message>",
"message: message to send",
NULL, tox_weechat_cmd_me, NULL);
weechat_hook_command("myaddress",
"get your Tox address to give to friends",
"", "",
NULL, tox_weechat_cmd_myaddress, NULL);
weechat_hook_command("name",
"change your Tox name",
"<name>",
"name: your new name",
NULL, tox_weechat_cmd_name, NULL);
}

@ -0,0 +1,9 @@
#ifndef TOX_WEECHAT_COMMANDS_H
#define TOX_WEECHAT_COMMANDS_H
/**
* Register command callbacks.
*/
void tox_weechat_commands_init();
#endif // TOX_WEECHAT_COMMANDS_H

@ -0,0 +1,149 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h>
#include "tox-weechat.h"
#include "tox-weechat-utils.h"
#include "tox-weechat-friend-requests.h"
#define MAX_FRIEND_REQUESTS 250
struct t_tox_friend_request *tox_weechat_friend_requests_head = NULL;
struct t_tox_friend_request *tox_weechat_friend_requests_tail = NULL;
unsigned int friend_request_count = 0;
void
tox_weechat_friend_request_free(struct t_tox_friend_request *request)
{
free(request->message);
free(request);
}
void
tox_weechat_friend_request_add(struct t_tox_friend_request *request)
{
if (tox_weechat_friend_requests_head == NULL)
{
tox_weechat_friend_requests_head = tox_weechat_friend_requests_tail = request;
request->prev = request->next = NULL;
friend_request_count = 1;
}
else
{
tox_weechat_friend_requests_tail->next = request;
request->prev = tox_weechat_friend_requests_tail;
request->next = NULL;
tox_weechat_friend_requests_tail = request;
++friend_request_count;
}
}
void
tox_weechat_friend_request_remove(struct t_tox_friend_request *request)
{
if (request->prev)
request->prev->next = request->next;
if (request->next)
request->next->prev = request->prev;
if (request == tox_weechat_friend_requests_head)
tox_weechat_friend_requests_head = request->next;
if (request == tox_weechat_friend_requests_tail)
tox_weechat_friend_requests_tail = request->prev;
tox_weechat_friend_request_free(request);
}
struct t_tox_friend_request *
tox_weechat_first_friend_request()
{
return tox_weechat_friend_requests_head;
}
unsigned int
tox_weechat_friend_request_count()
{
return friend_request_count;
}
void
tox_weechat_accept_friend_request(struct t_tox_friend_request *request)
{
tox_add_friend_norequest(tox, request->address);
tox_weechat_friend_request_remove(request);
}
void
tox_weechat_decline_friend_request(struct t_tox_friend_request *request)
{
tox_weechat_friend_request_remove(request);
}
struct t_tox_friend_request *
tox_weechat_friend_request_with_num(unsigned int num)
{
if (num < 1 || num > friend_request_count) return NULL;
unsigned int i = 1;
struct t_tox_friend_request *request = tox_weechat_friend_requests_head;
while (i != num && request->next)
{
request = request->next;
++i;
}
return request;
}
void
tox_weechat_callback_friend_request(Tox *tox,
const uint8_t *public_key,
const uint8_t *message,
uint16_t length,
void *userdata)
{
if (friend_request_count >= MAX_FRIEND_REQUESTS)
{
weechat_printf(tox_main_buffer,
"%sReceived a friend request, but your friend request "
"list is full!",
weechat_prefix("warning"));
return;
}
struct t_tox_friend_request *request = malloc(sizeof(*request));
memcpy(request->address, public_key, TOX_CLIENT_ID_SIZE);
request->message = tox_weechat_null_terminate(message, length);
tox_weechat_friend_request_add(request);
char *hex_address = tox_weechat_bin2hex(request->address,
TOX_CLIENT_ID_SIZE);
weechat_printf(tox_main_buffer,
"%sReceived a friend request from %s: \"%s\"",
weechat_prefix("network"),
hex_address,
request->message);
}
void
tox_weechat_friend_requests_init()
{
tox_callback_friend_request(tox,
tox_weechat_callback_friend_request,
NULL);
}
void
tox_weechat_friend_requests_free()
{
// TODO: persist requests
while (tox_weechat_friend_requests_head)
tox_weechat_friend_request_remove(tox_weechat_friend_requests_head);
}

@ -0,0 +1,30 @@
#ifndef TOX_WEECHAT_FRIEND_REQUESTS_H
#define TOX_WEECHAT_FRIEND_REQUESTS_H
#include <stdint.h>
#include <tox/tox.h>
struct t_tox_friend_request
{
// public address of friend request
uint8_t address[TOX_CLIENT_ID_SIZE];
// message sent with request
char *message;
// linked list pointers
struct t_tox_friend_request *next;
struct t_tox_friend_request *prev;
};
struct t_tox_friend_request *tox_weechat_first_friend_request();
void tox_weechat_accept_friend_request(struct t_tox_friend_request *request);
void tox_weechat_decline_friend_request(struct t_tox_friend_request *request);
struct t_tox_friend_request *tox_weechat_friend_request_with_num(unsigned int num);
void tox_weechat_friend_requests_init();
void tox_weechat_friend_requests_free();
#endif // TOX_WEECHAT_FRIEND_REQUESTS_H

@ -0,0 +1,24 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h>
#include "tox-weechat.h"
#include "tox-weechat-utils.h"
char *bar_item_input_prompt(void *data,
struct t_gui_bar_item *item,
struct t_gui_window *window,
struct t_gui_buffer *buffer,
struct t_hashtable *extra_info)
{
return tox_weechat_get_self_name_nt();
}
void tox_weechat_gui_init()
{
weechat_bar_item_new("input_prompt", bar_item_input_prompt, NULL);
}

@ -0,0 +1,6 @@
#ifndef TOX_WEECHAT_GUI_H
#define TOX_WEECHAT_GUI_H
void tox_weechat_gui_init();
#endif // TOX_WEECHAT_GUI_H

@ -9,6 +9,7 @@
#include "tox-weechat.h"
#include "tox-weechat-utils.h"
#include "tox-weechat-chats.h"
#include "tox-weechat-tox.h"
@ -71,9 +72,6 @@ tox_weechat_load_file(Tox *tox, char *path)
return -1;
}
/**
* Continous timer keeping Tox running.
*/
int
tox_weechat_do_timer_cb(void *data,
int remaining_calls)
@ -84,6 +82,42 @@ tox_weechat_do_timer_cb(void *data,
return WEECHAT_RC_OK;
}
void
tox_weechat_friend_message_callback(Tox *tox,
int32_t friend_number,
const uint8_t *message,
uint16_t length,
void *data)
{
struct t_tox_chat *chat = tox_weechat_get_friend_chat(friend_number);
char *name = tox_weechat_get_name_nt(friend_number);
char *message_nt = tox_weechat_null_terminate(message, length);
tox_weechat_chat_print_message(chat, name, message_nt);
free(name);
free(message_nt);
}
void
tox_weechat_friend_action_callback(Tox *tox,
int32_t friend_number,
const uint8_t *message,
uint16_t length,
void *data)
{
struct t_tox_chat *chat = tox_weechat_get_friend_chat(friend_number);
char *name = tox_weechat_get_name_nt(friend_number);
char *message_nt = tox_weechat_null_terminate(message, length);
tox_weechat_chat_print_action(chat, name, message_nt);
free(name);
free(message_nt);
}
void
tox_weechat_tox_init()
{
@ -105,10 +139,13 @@ tox_weechat_tox_init()
// start tox_do loop
tox_weechat_do_timer_cb(NULL, 0);
tox_callback_friend_message(tox, tox_weechat_friend_message_callback, NULL);
tox_callback_friend_action(tox, tox_weechat_friend_action_callback, NULL);
}
void
tox_weechat_tox_end()
tox_weechat_tox_free()
{
// save Tox data to a buffer
uint32_t size = tox_size(tox);

@ -3,8 +3,6 @@
#include <tox/tox.h>
extern Tox *tox;
/**
* Initialize the Tox object, bootstrap the DHT and start working.
*/
@ -13,6 +11,6 @@ void tox_weechat_tox_init();
/**
* Dump Tox to file and de-initialize.
*/
void tox_weechat_tox_end();
void tox_weechat_tox_free();
#endif // TOX_WEECHAT_TOX_H

@ -3,6 +3,10 @@
#include <string.h>
#include <stdio.h>
#include <tox/tox.h>
#include "tox-weechat.h"
#include "tox-weechat-utils.h"
uint8_t *
@ -22,7 +26,7 @@ tox_weechat_hex2bin(const char *hex)
}
char *
weechat_tox_bin2hex(const uint8_t *bin, size_t size)
tox_weechat_bin2hex(const uint8_t *bin, size_t size)
{
char *hex = malloc(size * 2 + 1);
char *position = hex;
@ -37,3 +41,42 @@ weechat_tox_bin2hex(const uint8_t *bin, size_t size)
return hex;
}
char *
tox_weechat_null_terminate(const uint8_t *str, size_t length)
{
char *str_null = malloc(length + 1);
memcpy(str_null, str, length);
str_null[length] = 0;
return str_null;
}
char *
tox_weechat_get_name_nt(int32_t friend_number)
{
size_t length = tox_get_name_size(tox, friend_number);
uint8_t name[length];
tox_get_name(tox, friend_number, name);
return tox_weechat_null_terminate(name, length);
}
char *
tox_weechat_get_status_message_nt(int32_t friend_number)
{
size_t length = tox_get_status_message_size(tox, friend_number);
uint8_t message[length];
tox_get_status_message(tox, friend_number, message, length);
return tox_weechat_null_terminate(message, length);
}
char *
tox_weechat_get_self_name_nt()
{
size_t length = tox_get_self_name_size(tox);
uint8_t name[length];
tox_get_self_name(tox, name);
return tox_weechat_null_terminate(name, length);
}

@ -1,6 +1,7 @@
#ifndef TOX_WEECHAT_UTILS_H
#define TOX_WEECHAT_UTILS_H
#include <stdlib.h>
#include <stdint.h>
/**
@ -11,6 +12,28 @@ uint8_t *tox_weechat_hex2bin(const char *hex);
/**
* Convert a binary address to a null-terminated hex string. Must be freed.
*/
char *tox_weechat_bin2hex(const char *hex);
char *tox_weechat_bin2hex(const uint8_t *bin, size_t size);
/**
* Return a new string that is a null-terminated version of the argument. Must
* be freed.
*/
char *tox_weechat_null_terminate(const uint8_t *str, size_t length);
/**
* Get the name of a friend as a null-terminated string. Must be freed.
*/
char *tox_weechat_get_name_nt(int32_t friend_number);
/**
* Get the status message of a friend as a null-terminated string. Must be
* freed.
*/
char *tox_weechat_get_status_message_nt(int32_t friend_number);
/**
* Get the name of the user as a null-terminated string. Must be freed.
*/
char *tox_weechat_get_self_name_nt();
#endif // TOX_WEECHAT_UTILS_H

@ -3,6 +3,9 @@
#include <weechat/weechat-plugin.h>
#include "tox-weechat-tox.h"
#include "tox-weechat-commands.h"
#include "tox-weechat-gui.h"
#include "tox-weechat-friend-requests.h"
#include "tox-weechat.h"
@ -13,13 +16,18 @@ WEECHAT_PLUGIN_VERSION("0.1");
WEECHAT_PLUGIN_LICENSE("GPL3");
struct t_weechat_plugin *weechat_plugin = NULL;
struct t_gui_buffer *tox_main_buffer = NULL;
int
weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[])
{
weechat_plugin = plugin;
tox_main_buffer = weechat_buffer_new("tox", NULL, NULL, NULL, NULL);
tox_weechat_tox_init();
tox_weechat_commands_init();
tox_weechat_gui_init();
tox_weechat_friend_requests_init();
return WEECHAT_RC_OK;
}
@ -27,5 +35,8 @@ weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[])
int
weechat_plugin_end(struct t_weechat_plugin *plugin)
{
tox_weechat_tox_free();
tox_weechat_friend_requests_free();
return WEECHAT_RC_OK;
}

@ -1,6 +1,11 @@
#ifndef TOX_WEECHAT_H
#define TOX_WEECHAT_H
#include <tox/tox.h>
extern struct t_weechat_plugin *weechat_plugin;
extern Tox *tox;
extern struct t_gui_buffer *tox_main_buffer;
#endif // TOX_WEECHAT_H

Loading…
Cancel
Save