More support for multiple identities.

This commit is contained in:
Håvard Pettersson 2014-09-07 03:04:42 +02:00
parent 76d07ed4f6
commit b60ac98a3e
14 changed files with 529 additions and 344 deletions

View File

@ -5,13 +5,11 @@
#include <tox/tox.h> #include <tox/tox.h>
#include "tox-weechat.h" #include "tox-weechat.h"
#include "tox-weechat-tox.h"
#include "tox-weechat-utils.h" #include "tox-weechat-utils.h"
#include "tox-weechat-chats.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, int tox_weechat_buffer_input_callback(void *data,
struct t_gui_buffer *buffer, struct t_gui_buffer *buffer,
const char *input_data); const char *input_data);
@ -20,44 +18,45 @@ int tox_weechat_buffer_close_callback(void *data,
struct t_gui_buffer *buffer); struct t_gui_buffer *buffer);
void void
tox_weechat_chat_add(struct t_tox_chat *chat) tox_weechat_chat_add(struct t_tox_weechat_identity *identity,
struct t_tox_weechat_chat *chat)
{ {
if (tox_weechat_chats_head == NULL) chat->identity = identity;
{
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; chat->prev_chat = identity->last_chat;
} chat->next_chat = NULL;
if (identity->chats == NULL)
identity->chats = chat;
else
identity->last_chat->next_chat = chat;
identity->last_chat = chat;
} }
void void
tox_weechat_chat_remove(struct t_tox_chat *chat) tox_weechat_chat_remove(struct t_tox_weechat_chat *chat)
{ {
if (chat->prev) if (chat->prev_chat)
chat->prev->next = chat->next; chat->prev_chat->next_chat = chat->next_chat;
if (chat->next) if (chat->next_chat)
chat->next->prev = chat->prev; chat->next_chat->prev_chat = chat->prev_chat;
if (chat == tox_weechat_chats_head) if (chat == chat->identity->chats)
tox_weechat_chats_head = chat->next; chat->identity->chats = chat->next_chat;
if (chat == tox_weechat_chats_tail) if (chat == chat->identity->last_chat)
tox_weechat_chats_tail = chat->prev; chat->identity->last_chat = chat->prev_chat;
free(chat); free(chat);
} }
void void
tox_weechat_chat_refresh(struct t_tox_chat *chat) tox_weechat_chat_refresh(struct t_tox_weechat_chat *chat)
{ {
char *name = tox_weechat_get_name_nt(chat->friend_number); char *name = tox_weechat_get_name_nt(chat->identity->tox,
char *status_message = tox_weechat_get_status_message_nt(chat->friend_number); chat->friend_number);
char *status_message = tox_weechat_get_status_message_nt(chat->identity->tox,
chat->friend_number);
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", status_message);
@ -66,40 +65,38 @@ tox_weechat_chat_refresh(struct t_tox_chat *chat)
free(status_message); free(status_message);
} }
struct t_tox_chat * struct t_tox_weechat_chat *
tox_weechat_friend_chat_new(int32_t friend_number) tox_weechat_friend_chat_new(struct t_tox_weechat_identity *identity,
int32_t friend_number)
{ {
struct t_tox_chat *chat = malloc(sizeof(*chat)); struct t_tox_weechat_chat *chat = malloc(sizeof(*chat));
chat->friend_number = friend_number; chat->friend_number = friend_number;
uint8_t client_id[TOX_CLIENT_ID_SIZE]; uint8_t client_id[TOX_CLIENT_ID_SIZE];
tox_get_client_id(tox, friend_number, client_id); tox_get_client_id(identity->tox, friend_number, client_id);
char *buffer_name = tox_weechat_bin2hex(client_id, TOX_CLIENT_ID_SIZE); // 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);
chat->buffer = weechat_buffer_new(buffer_name, chat->buffer = weechat_buffer_new(buffer_name,
tox_weechat_buffer_input_callback, chat, tox_weechat_buffer_input_callback, chat,
tox_weechat_buffer_close_callback, chat); tox_weechat_buffer_close_callback, chat);
tox_weechat_chat_refresh(chat); tox_weechat_chat_refresh(chat);
tox_weechat_chat_add(chat); tox_weechat_chat_add(identity, chat);
free(buffer_name); free(buffer_name);
return chat; return chat;
} }
struct t_tox_chat * struct t_tox_weechat_chat *
tox_weechat_get_first_chat() tox_weechat_get_existing_friend_chat(struct t_tox_weechat_identity *identity,
int32_t friend_number)
{ {
return tox_weechat_chats_head; for (struct t_tox_weechat_chat *chat = identity->chats;
}
struct t_tox_chat *
tox_weechat_get_existing_friend_chat(int32_t friend_number)
{
for (struct t_tox_chat *chat = tox_weechat_chats_head;
chat; chat;
chat = chat->next) chat = chat->next_chat)
{ {
if (chat->friend_number == friend_number) if (chat->friend_number == friend_number)
return chat; return chat;
@ -108,33 +105,39 @@ tox_weechat_get_existing_friend_chat(int32_t friend_number)
return NULL; return NULL;
} }
struct t_tox_chat * struct t_tox_weechat_chat *
tox_weechat_get_friend_chat(int32_t friend_number) tox_weechat_get_friend_chat(struct t_tox_weechat_identity *identity,
int32_t friend_number)
{ {
struct t_tox_chat *chat = tox_weechat_get_existing_friend_chat(friend_number); struct t_tox_weechat_chat *chat = tox_weechat_get_existing_friend_chat(identity, friend_number);
if (chat) if (chat)
return chat; return chat;
else else
return tox_weechat_friend_chat_new(friend_number); return tox_weechat_friend_chat_new(identity, friend_number);
} }
struct t_tox_chat * struct t_tox_weechat_chat *
tox_weechat_get_chat_for_buffer(struct t_gui_buffer *buffer) tox_weechat_get_chat_for_buffer(struct t_gui_buffer *buffer)
{ {
for (struct t_tox_chat *chat = tox_weechat_chats_head; for (struct t_tox_weechat_identity *identity = tox_weechat_identities;
identity;
identity = identity->next_identity)
{
for (struct t_tox_weechat_chat *chat = identity->chats;
chat; chat;
chat = chat->next) chat = chat->next_chat)
{ {
if (chat->buffer == buffer) if (chat->buffer == buffer)
return chat; return chat;
} }
}
return NULL; return NULL;
} }
void void
tox_weechat_chat_print_message(struct t_tox_chat *chat, tox_weechat_chat_print_message(struct t_tox_weechat_chat *chat,
const char *sender, const char *sender,
const char *message) const char *message)
{ {
@ -142,7 +145,7 @@ tox_weechat_chat_print_message(struct t_tox_chat *chat,
} }
void void
tox_weechat_chat_print_action(struct t_tox_chat *chat, tox_weechat_chat_print_action(struct t_tox_weechat_chat *chat,
const char *sender, const char *sender,
const char *message) const char *message)
{ {
@ -157,14 +160,14 @@ tox_weechat_buffer_input_callback(void *data,
struct t_gui_buffer *weechat_buffer, struct t_gui_buffer *weechat_buffer,
const char *input_data) const char *input_data)
{ {
struct t_tox_chat *chat = data; struct t_tox_weechat_chat *chat = data;
tox_send_message(tox, tox_send_message(chat->identity->tox,
chat->friend_number, chat->friend_number,
(uint8_t *)input_data, (uint8_t *)input_data,
strlen(input_data)); strlen(input_data));
char *name = tox_weechat_get_self_name_nt(); char *name = tox_weechat_get_self_name_nt(chat->identity->tox);
tox_weechat_chat_print_message(chat, name, input_data); tox_weechat_chat_print_message(chat, name, input_data);
@ -177,7 +180,7 @@ int
tox_weechat_buffer_close_callback(void *data, tox_weechat_buffer_close_callback(void *data,
struct t_gui_buffer *weechat_buffer) struct t_gui_buffer *weechat_buffer)
{ {
struct t_tox_chat *chat = data; struct t_tox_weechat_chat *chat = data;
tox_weechat_chat_remove(chat); tox_weechat_chat_remove(chat);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;

View File

@ -5,30 +5,37 @@
#include <tox/tox.h> #include <tox/tox.h>
struct t_tox_chat struct t_tox_weechat_chat
{ {
struct t_gui_buffer *buffer; struct t_gui_buffer *buffer;
int32_t friend_number; int32_t friend_number;
// linked list pointers struct t_tox_weechat_identity *identity;
struct t_tox_chat *next;
struct t_tox_chat *prev; struct t_tox_weechat_chat *next_chat;
struct t_tox_weechat_chat *prev_chat;
}; };
struct t_tox_chat *tox_weechat_get_friend_chat(int32_t friend_number); struct t_tox_weechat_chat *
struct t_tox_chat *tox_weechat_get_existing_friend_chat(int32_t friend_number); tox_weechat_get_friend_chat(struct t_tox_weechat_identity *identity,
struct t_tox_chat *tox_weechat_get_chat_for_buffer(struct t_gui_buffer *target_buffer); int32_t friend_number);
struct t_tox_chat *tox_weechat_get_first_chat(); struct t_tox_weechat_chat *
tox_weechat_get_existing_friend_chat(struct t_tox_weechat_identity *identity,
int32_t friend_number);
void tox_weechat_chat_print_message(struct t_tox_chat *chat, struct t_tox_weechat_chat *
const char *sender, tox_weechat_get_chat_for_buffer(struct t_gui_buffer *target_buffer);
const char *message);
void tox_weechat_chat_print_action(struct t_tox_chat *chat, void tox_weechat_chat_print_message(struct t_tox_weechat_chat *chat,
const char *sender, const char *sender,
const char *message); const char *message);
void tox_weechat_chat_refresh(struct t_tox_chat *chat); void tox_weechat_chat_print_action(struct t_tox_weechat_chat *chat,
const char *sender,
const char *message);
void tox_weechat_chat_refresh(struct t_tox_weechat_chat *chat);
#endif // TOX_WEECHAT_CHATS_H #endif // TOX_WEECHAT_CHATS_H

View File

@ -15,6 +15,8 @@ int
tox_weechat_cmd_bootstrap(void *data, struct t_gui_buffer *buffer, tox_weechat_cmd_bootstrap(void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol) int argc, char **argv, char **argv_eol)
{ {
struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
if (argc != 4) if (argc != 4)
return WEECHAT_RC_ERROR; return WEECHAT_RC_ERROR;
@ -22,9 +24,9 @@ tox_weechat_cmd_bootstrap(void *data, struct t_gui_buffer *buffer,
uint16_t port = atoi(argv[2]); uint16_t port = atoi(argv[2]);
char *tox_address = argv[3]; char *tox_address = argv[3];
if (!tox_weechat_bootstrap(address, port, tox_address)) if (!tox_weechat_bootstrap_tox(identity->tox, address, port, tox_address))
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sInvalid arguments for bootstrap.", "%sInvalid arguments for bootstrap.",
weechat_prefix("error")); weechat_prefix("error"));
} }
@ -36,41 +38,46 @@ int
tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer, tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol) int argc, char **argv, char **argv_eol)
{ {
struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
// /friend [list] // /friend [list]
if (argc == 1 || (argc == 2 && weechat_strcasecmp(argv[1], "list") == 0)) if (argc == 1 || (argc == 2 && weechat_strcasecmp(argv[1], "list") == 0))
{ {
size_t friend_count = tox_count_friendlist(tox); size_t friend_count = tox_count_friendlist(identity->tox);
int32_t friend_numbers[friend_count]; int32_t friend_numbers[friend_count];
tox_get_friendlist(tox, friend_numbers, friend_count); tox_get_friendlist(identity->tox, friend_numbers, friend_count);
if (friend_count == 0) if (friend_count == 0)
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sYou have no friends :(", "%sYou have no friends :(",
weechat_prefix("network")); weechat_prefix("network"));
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%s[#] Name [client ID]", "%s[#] Name [client ID]",
weechat_prefix("network")); weechat_prefix("network"));
for (size_t i = 0; i < friend_count; ++i) for (size_t i = 0; i < friend_count; ++i)
{ {
int32_t friend_number = friend_numbers[i]; int32_t friend_number = friend_numbers[i];
char *name = tox_weechat_get_name_nt(friend_number); char *name = tox_weechat_get_name_nt(identity->tox, friend_number);
uint8_t client_id[TOX_CLIENT_ID_SIZE]; uint8_t client_id[TOX_CLIENT_ID_SIZE];
tox_get_client_id(tox, friend_number, client_id); tox_get_client_id(identity->tox, friend_number, client_id);
char *hex_id = tox_weechat_bin2hex(client_id, TOX_CLIENT_ID_SIZE); char *hex_address = malloc(TOX_CLIENT_ID_SIZE * 2 + 1);
tox_weechat_bin2hex(client_id,
TOX_CLIENT_ID_SIZE,
hex_address);
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%s[%d] %s [%s]", "%s[%d] %s [%s]",
weechat_prefix("network"), weechat_prefix("network"),
friend_number, name, hex_id); friend_number, name, hex_address);
free(name); free(name);
free(hex_id); free(hex_address);
} }
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
@ -78,7 +85,8 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
if (argc >= 3 && (weechat_strcasecmp(argv[1], "add") == 0)) if (argc >= 3 && (weechat_strcasecmp(argv[1], "add") == 0))
{ {
uint8_t *address = tox_weechat_hex2bin(argv[2]); char *address = malloc(TOX_FRIEND_ADDRESS_SIZE);
tox_weechat_hex2bin(argv[2], address);
char *message; char *message;
if (argc == 3 || strlen(argv_eol[3]) == 0) if (argc == 3 || strlen(argv_eol[3]) == 0)
@ -86,46 +94,46 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
else else
message = argv_eol[3]; message = argv_eol[3];
int32_t result = tox_add_friend(tox, int32_t result = tox_add_friend(identity->tox,
address, (uint8_t *)address,
(uint8_t *)message, (uint8_t *)message,
strlen(message)); strlen(message));
switch (result) switch (result)
{ {
case TOX_FAERR_TOOLONG: case TOX_FAERR_TOOLONG:
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sFriend request message too long! Try again.", "%sFriend request message too long! Try again.",
weechat_prefix("error")); weechat_prefix("error"));
break; break;
case TOX_FAERR_ALREADYSENT: case TOX_FAERR_ALREADYSENT:
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sYou have already sent a friend request to that address.", "%sYou have already sent a friend request to that address.",
weechat_prefix("error")); weechat_prefix("error"));
break; break;
case TOX_FAERR_OWNKEY: case TOX_FAERR_OWNKEY:
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sYou can't add yourself as a friend.", "%sYou can't add yourself as a friend.",
weechat_prefix("error")); weechat_prefix("error"));
break; break;
case TOX_FAERR_BADCHECKSUM: case TOX_FAERR_BADCHECKSUM:
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sInvalid friend address - try again.", "%sInvalid friend address - try again.",
weechat_prefix("error")); weechat_prefix("error"));
break; break;
case TOX_FAERR_NOMEM: case TOX_FAERR_NOMEM:
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sCould not add friend (out of memory).", "%sCould not add friend (out of memory).",
weechat_prefix("error")); weechat_prefix("error"));
break; break;
case TOX_FAERR_UNKNOWN: case TOX_FAERR_UNKNOWN:
case TOX_FAERR_SETNEWNOSPAM: case TOX_FAERR_SETNEWNOSPAM:
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sCould not add friend (unknown error).", "%sCould not add friend (unknown error).",
weechat_prefix("error")); weechat_prefix("error"));
break; break;
default: default:
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sFriend request sent!", "%sFriend request sent!",
weechat_prefix("network")); weechat_prefix("network"));
break; break;
@ -140,28 +148,30 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
char *endptr; char *endptr;
unsigned long friend_number = strtoul(argv[2], &endptr, 10); unsigned long friend_number = strtoul(argv[2], &endptr, 10);
if (endptr == argv[2] || !tox_friend_exists(tox, friend_number)) if (endptr == argv[2] || !tox_friend_exists(identity->tox, friend_number))
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sInvalid friend number.", "%sInvalid friend number.",
weechat_prefix("error")); weechat_prefix("error"));
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
char *name = tox_weechat_get_name_nt(friend_number); char *name = tox_weechat_get_name_nt(identity->tox, friend_number);
if (tox_del_friend(tox, friend_number) == 0) if (tox_del_friend(identity->tox, friend_number) == 0)
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sRemoved %s from friend list.", "%sRemoved %s from friend list.",
weechat_prefix("network"), name); weechat_prefix("network"), name);
} }
else else
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sCould not remove friend!", "%sCould not remove friend!",
weechat_prefix("error")); weechat_prefix("error"));
} }
free(name);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
@ -172,11 +182,11 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
{ {
int accept = weechat_strcasecmp(argv[1], "accept") == 0; int accept = weechat_strcasecmp(argv[1], "accept") == 0;
struct t_tox_friend_request *request; struct t_tox_weechat_friend_request *request;
if (weechat_strcasecmp(argv[2], "all") == 0) if (weechat_strcasecmp(argv[2], "all") == 0)
{ {
int count = 0; int count = 0;
while ((request = tox_weechat_friend_request_with_num(1)) != NULL) while ((request = tox_weechat_friend_request_with_num(identity, 1)) != NULL)
{ {
if (accept) if (accept)
tox_weechat_accept_friend_request(request); tox_weechat_accept_friend_request(request);
@ -186,7 +196,7 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
++count; ++count;
} }
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%s%s %d friend requests.", "%s%s %d friend requests.",
weechat_prefix("network"), weechat_prefix("network"),
accept ? "Accepted" : "Declined", accept ? "Accepted" : "Declined",
@ -197,27 +207,30 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
else else
{ {
unsigned int num = atoi(argv[2]); unsigned int num = atoi(argv[2]);
if (num == 0 || (request = tox_weechat_friend_request_with_num(num)) == NULL) if (num == 0 || (request = tox_weechat_friend_request_with_num(identity, num)) == NULL)
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sInvalid friend request ID.", "%sInvalid friend request ID.",
weechat_prefix("error")); weechat_prefix("error"));
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
char *hex_id = tox_weechat_bin2hex(request->address, TOX_CLIENT_ID_SIZE); char *hex_address = malloc(TOX_CLIENT_ID_SIZE * 2 + 1);
tox_weechat_bin2hex(request->address,
TOX_CLIENT_ID_SIZE,
hex_address);
if (accept) if (accept)
tox_weechat_accept_friend_request(request); tox_weechat_accept_friend_request(request);
else else
tox_weechat_decline_friend_request(request); tox_weechat_decline_friend_request(request);
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%s%s friend request from %s.", "%s%s friend request from %s.",
weechat_prefix("network"), weechat_prefix("network"),
accept ? "Accepted" : "Declined", accept ? "Accepted" : "Declined",
hex_id); hex_address);
free(hex_id); free(hex_address);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
@ -225,25 +238,29 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
if (argc == 2 && weechat_strcasecmp(argv[1], "requests") == 0) if (argc == 2 && weechat_strcasecmp(argv[1], "requests") == 0)
{ {
struct t_tox_friend_request *request = tox_weechat_first_friend_request(); if (identity->friend_requests == NULL)
if (request == NULL)
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sNo pending friend requests :(", "%sNo pending friend requests :(",
weechat_prefix("network")); weechat_prefix("network"));
} }
else else
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sPending friend requests:", "%sPending friend requests:",
weechat_prefix("network")); weechat_prefix("network"));
unsigned int num = 1; unsigned int num = 1;
while (request) for (struct t_tox_weechat_friend_request *request = identity->friend_requests;
request;
request = request->next_request)
{ {
char *hex_address = tox_weechat_bin2hex(request->address, char *hex_address = malloc(TOX_CLIENT_ID_SIZE * 2 + 1);
TOX_CLIENT_ID_SIZE); tox_weechat_bin2hex(request->address,
weechat_printf(tox_main_buffer, TOX_CLIENT_ID_SIZE,
hex_address);
weechat_printf(identity->buffer,
"%s[%d] Address: %s\n" "%s[%d] Address: %s\n"
"[%d] Message: %s", "[%d] Message: %s",
weechat_prefix("network"), weechat_prefix("network"),
@ -252,7 +269,6 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
free(hex_address); free(hex_address);
request = request->next;
++num; ++num;
} }
} }
@ -270,14 +286,16 @@ tox_weechat_cmd_me(void *data, struct t_gui_buffer *buffer,
if (argc == 1) if (argc == 1)
return WEECHAT_RC_ERROR; return WEECHAT_RC_ERROR;
struct t_tox_chat *chat = tox_weechat_get_chat_for_buffer(buffer); struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
tox_send_action(tox, struct t_tox_weechat_chat *chat = tox_weechat_get_chat_for_buffer(buffer);
tox_send_action(identity->tox,
chat->friend_number, chat->friend_number,
(uint8_t *)argv_eol[1], (uint8_t *)argv_eol[1],
strlen(argv_eol[1])); strlen(argv_eol[1]));
char *name = tox_weechat_get_self_name_nt(); char *name = tox_weechat_get_self_name_nt(identity->tox);
tox_weechat_chat_print_action(chat, name, argv_eol[1]); tox_weechat_chat_print_action(chat, name, argv_eol[1]);
free(name); free(name);
@ -292,25 +310,27 @@ tox_weechat_cmd_msg(void *data, struct t_gui_buffer *buffer,
if (argc == 1) if (argc == 1)
return WEECHAT_RC_ERROR; return WEECHAT_RC_ERROR;
struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
char *endptr; char *endptr;
unsigned long friend_number = strtoul(argv[1], &endptr, 10); unsigned long friend_number = strtoul(argv[1], &endptr, 10);
if (endptr == argv[1] || !tox_friend_exists(tox, friend_number)) if (endptr == argv[1] || !tox_friend_exists(identity->tox, friend_number))
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sInvalid friend number.", "%sInvalid friend number.",
weechat_prefix("error")); weechat_prefix("error"));
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
struct t_tox_chat *chat = tox_weechat_get_friend_chat(friend_number); struct t_tox_weechat_chat *chat = tox_weechat_get_friend_chat(identity, friend_number);
if (argc >= 3) if (argc >= 3)
{ {
tox_send_message(tox, tox_send_message(identity->tox,
friend_number, friend_number,
(uint8_t *)argv_eol[1], (uint8_t *)argv_eol[1],
strlen(argv_eol[2])); strlen(argv_eol[2]));
char *name = tox_weechat_get_self_name_nt(); char *name = tox_weechat_get_self_name_nt(identity->tox);
tox_weechat_chat_print_message(chat, name, argv_eol[1]); tox_weechat_chat_print_message(chat, name, argv_eol[1]);
free(name); free(name);
} }
@ -322,12 +342,15 @@ int
tox_weechat_cmd_myaddress(void *data, struct t_gui_buffer *buffer, tox_weechat_cmd_myaddress(void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol) int argc, char **argv, char **argv_eol)
{ {
struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
tox_get_address(tox, address); tox_get_address(identity->tox, address);
char *address_str = tox_weechat_bin2hex(address, TOX_FRIEND_ADDRESS_SIZE); char *address_str = malloc(TOX_FRIEND_ADDRESS_SIZE * 2 + 1);
tox_weechat_bin2hex(address, TOX_FRIEND_ADDRESS_SIZE, address_str);
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sYour Tox address: %s", "%sYour Tox address: %s",
weechat_prefix("network"), weechat_prefix("network"),
address_str); address_str);
@ -344,12 +367,14 @@ tox_weechat_cmd_name(void *data, struct t_gui_buffer *buffer,
if (argc == 1) if (argc == 1)
return WEECHAT_RC_ERROR; return WEECHAT_RC_ERROR;
struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
char *name = argv_eol[1]; char *name = argv_eol[1];
int result = tox_set_name(tox, (uint8_t *)name, strlen(name)); int result = tox_set_name(identity->tox, (uint8_t *)name, strlen(name));
if (result == -1) if (result == -1)
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%s%s", "%s%s",
weechat_prefix("error"), weechat_prefix("error"),
"Could not change name."); "Could not change name.");
@ -358,14 +383,14 @@ tox_weechat_cmd_name(void *data, struct t_gui_buffer *buffer,
weechat_bar_item_update("input_prompt"); weechat_bar_item_update("input_prompt");
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%sYou are now known as %s", "%sYou are now known as %s",
weechat_prefix("network"), weechat_prefix("network"),
name); name);
for (struct t_tox_chat *chat = tox_weechat_get_first_chat(); for (struct t_tox_weechat_chat *chat = identity->chats;
chat; chat;
chat = chat->next) chat = chat->next_chat)
{ {
weechat_printf(chat->buffer, weechat_printf(chat->buffer,
"%sYou are now known as %s", "%sYou are now known as %s",
@ -383,6 +408,8 @@ tox_weechat_cmd_status(void *data, struct t_gui_buffer *buffer,
if (argc != 2) if (argc != 2)
return WEECHAT_RC_ERROR; return WEECHAT_RC_ERROR;
struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
TOX_USERSTATUS status = TOX_USERSTATUS_INVALID; TOX_USERSTATUS status = TOX_USERSTATUS_INVALID;
if (weechat_strcasecmp(argv[1], "online") == 0) if (weechat_strcasecmp(argv[1], "online") == 0)
status = TOX_USERSTATUS_NONE; status = TOX_USERSTATUS_NONE;
@ -394,7 +421,7 @@ tox_weechat_cmd_status(void *data, struct t_gui_buffer *buffer,
if (status == TOX_USERSTATUS_INVALID) if (status == TOX_USERSTATUS_INVALID)
return WEECHAT_RC_ERROR; return WEECHAT_RC_ERROR;
tox_set_user_status(tox, status); tox_set_user_status(identity->tox, status);
weechat_bar_item_update("away"); weechat_bar_item_update("away");
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
@ -404,12 +431,16 @@ int
tox_weechat_cmd_statusmsg(void *data, struct t_gui_buffer *buffer, tox_weechat_cmd_statusmsg(void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol) int argc, char **argv, char **argv_eol)
{ {
struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
char *message = argc > 1 ? argv_eol[1] : " "; char *message = argc > 1 ? argv_eol[1] : " ";
int result = tox_set_status_message(tox, (uint8_t *)message, strlen(message)); int result = tox_set_status_message(identity->tox,
(uint8_t *)message,
strlen(message));
if (result == -1) if (result == -1)
{ {
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%s%s", "%s%s",
weechat_prefix("error"), weechat_prefix("error"),
"Could not set status message."); "Could not set status message.");

94
src/tox-weechat-config.c Normal file
View File

@ -0,0 +1,94 @@
#include <weechat/weechat-plugin.h>
#include "tox-weechat.h"
struct t_config_file *tox_weechat_config_file = NULL;
struct t_config_section *tox_weechat_config_section_server = NULL;
int
tox_weechat_config_reload_callback(void *data,
struct t_config_file *config_file)
{
return WEECHAT_RC_OK;
}
int
tox_weechat_config_identity_read_callback(void *data,
struct t_config_file *config_file,
struct t_config_section *section,
const char *option_name,
const char *value)
{
return WEECHAT_CONFIG_OPTION_SET_OK_CHANGED;
/* return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; */
/* return WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND; */
/* return WEECHAT_CONFIG_OPTION_SET_ERROR; */
}
int
tox_weechat_config_identity_write_callback(void *data,
struct t_config_file *config_file,
const char *section_name)
{
return WEECHAT_CONFIG_WRITE_OK;
/* return WEECHAT_CONFIG_WRITE_ERROR; */
}
int
tox_weechat_config_identity_write_default_callback(void *data,
struct t_config_file *config_file,
const char *section_name)
{
return WEECHAT_CONFIG_WRITE_OK;
/* return WEECHAT_CONFIG_WRITE_ERROR; */
}
int
tox_weechat_config_server_name_check_callback(void *data,
struct t_config_option *option,
const char *value)
{
return 1;
}
void
tox_weechat_config_server_name_change_callback(void *data,
struct t_config_option *option)
{
}
void
tox_weechat_config_init()
{
tox_weechat_config_file = weechat_config_new("tox",
tox_weechat_config_reload_callback, NULL);
tox_weechat_config_section_server =
weechat_config_new_section(tox_weechat_config_file, "identity",
0, 0,
tox_weechat_config_identity_read_callback, NULL,
tox_weechat_config_identity_write_callback, NULL,
tox_weechat_config_identity_write_default_callback, NULL,
NULL, NULL,
NULL, NULL);
}
void
tox_weechat_config_init_server(const char *name)
{
struct t_config_option *option =
weechat_config_new_option(tox_weechat_config_file,
tox_weechat_config_section_server,
"name", "string",
"identity name",
NULL, 0, 0,
name, name,
0,
tox_weechat_config_server_name_check_callback, NULL,
tox_weechat_config_server_name_change_callback, NULL,
NULL, NULL);
}

7
src/tox-weechat-config.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef TOX_WEECHAT_CONFIG_H
#define TOX_WEECHAT_CONFIG_H
void
tox_weechat_config_init();
#endif // TOX_WEECHAT_CONFIG_H

View File

@ -6,144 +6,93 @@
#include <tox/tox.h> #include <tox/tox.h>
#include "tox-weechat.h" #include "tox-weechat.h"
#include "tox-weechat-tox.h"
#include "tox-weechat-utils.h" #include "tox-weechat-utils.h"
#include "tox-weechat-friend-requests.h" #include "tox-weechat-friend-requests.h"
#define MAX_FRIEND_REQUESTS 250 #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 void
tox_weechat_friend_request_free(struct t_tox_friend_request *request) tox_weechat_friend_request_free(struct t_tox_weechat_friend_request *request)
{ {
free(request->message); free(request->message);
free(request); free(request);
} }
void void
tox_weechat_friend_request_add(struct t_tox_friend_request *request) tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
struct t_tox_weechat_friend_request *request)
{ {
if (tox_weechat_friend_requests_head == NULL) request->identity = identity;
{
tox_weechat_friend_requests_head = tox_weechat_friend_requests_tail = request; request->prev_request = identity->last_friend_request;
request->prev = request->next = NULL; request->next_request = NULL;
friend_request_count = 1;
} if (identity->friend_requests == NULL)
identity->friend_requests = request;
else else
{ identity->last_friend_request->next_request = request;
tox_weechat_friend_requests_tail->next = request;
request->prev = tox_weechat_friend_requests_tail;
request->next = NULL;
tox_weechat_friend_requests_tail = request; identity->last_friend_request = request;
++(identity->friend_request_count);
++friend_request_count;
}
} }
void void
tox_weechat_friend_request_remove(struct t_tox_friend_request *request) tox_weechat_friend_request_remove(struct t_tox_weechat_friend_request *request)
{ {
if (request->prev) if (request->prev_request)
request->prev->next = request->next; request->prev_request->next_request = request->next_request;
if (request->next) if (request->next_request)
request->next->prev = request->prev; request->next_request->prev_request = request->prev_request;
if (request == tox_weechat_friend_requests_head) if (request == request->identity->friend_requests)
tox_weechat_friend_requests_head = request->next; request->identity->friend_requests = request->next_request;
if (request == tox_weechat_friend_requests_tail) if (request == request->identity->last_friend_request)
tox_weechat_friend_requests_tail = request->prev; request->identity->last_friend_request = request->prev_request;
tox_weechat_friend_request_free(request); 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 void
tox_weechat_accept_friend_request(struct t_tox_friend_request *request) tox_weechat_accept_friend_request(struct t_tox_weechat_friend_request *request)
{ {
tox_add_friend_norequest(tox, request->address); tox_add_friend_norequest(request->identity->tox, request->address);
tox_weechat_friend_request_remove(request); tox_weechat_friend_request_remove(request);
} }
void void
tox_weechat_decline_friend_request(struct t_tox_friend_request *request) tox_weechat_decline_friend_request(struct t_tox_weechat_friend_request *request)
{ {
tox_weechat_friend_request_remove(request); tox_weechat_friend_request_remove(request);
} }
struct t_tox_friend_request * struct t_tox_weechat_friend_request *
tox_weechat_friend_request_with_num(unsigned int num) tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
unsigned int num)
{ {
if (num < 1 || num > friend_request_count) return NULL; if (num < 1 || num > identity->friend_request_count) return NULL;
unsigned int i = 1; unsigned int i = 1;
struct t_tox_friend_request *request = tox_weechat_friend_requests_head; struct t_tox_weechat_friend_request *request = identity->friend_requests;
while (i != num && request->next) while (i != num && request->next_request)
{ {
request = request->next; request = request->next_request;
++i; ++i;
} }
return request; 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 void
tox_weechat_friend_requests_init() tox_weechat_friend_requests_init()
{ {
tox_callback_friend_request(tox,
tox_weechat_callback_friend_request,
NULL);
} }
void void
tox_weechat_friend_requests_free() tox_weechat_friend_requests_free(struct t_tox_weechat_identity *identity)
{ {
// TODO: persist requests // TODO: persist requests
while (tox_weechat_friend_requests_head) while (identity->friend_requests)
tox_weechat_friend_request_remove(tox_weechat_friend_requests_head); tox_weechat_friend_request_remove(identity->friend_requests);
} }

View File

@ -5,7 +5,7 @@
#include <tox/tox.h> #include <tox/tox.h>
struct t_tox_friend_request struct t_tox_weechat_friend_request
{ {
// public address of friend request // public address of friend request
uint8_t address[TOX_CLIENT_ID_SIZE]; uint8_t address[TOX_CLIENT_ID_SIZE];
@ -13,18 +13,27 @@ struct t_tox_friend_request
// message sent with request // message sent with request
char *message; char *message;
// linked list pointers struct t_tox_weechat_identity *identity;
struct t_tox_friend_request *next;
struct t_tox_friend_request *prev; struct t_tox_weechat_friend_request *next_request;
struct t_tox_weechat_friend_request *prev_request;
}; };
struct t_tox_friend_request *tox_weechat_first_friend_request(); void
tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
struct t_tox_weechat_friend_request *request);
void tox_weechat_accept_friend_request(struct t_tox_friend_request *request); void
void tox_weechat_decline_friend_request(struct t_tox_friend_request *request); tox_weechat_accept_friend_request(struct t_tox_weechat_friend_request *request);
struct t_tox_friend_request *tox_weechat_friend_request_with_num(unsigned int num);
void tox_weechat_friend_requests_init(); void
void tox_weechat_friend_requests_free(); tox_weechat_decline_friend_request(struct t_tox_weechat_friend_request *request);
struct t_tox_weechat_friend_request *
tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
unsigned int num);
void
tox_weechat_friend_requests_free(struct t_tox_weechat_identity *identity);
#endif // TOX_WEECHAT_FRIEND_REQUESTS_H #endif // TOX_WEECHAT_FRIEND_REQUESTS_H

View File

@ -7,6 +7,7 @@
#include "tox-weechat.h" #include "tox-weechat.h"
#include "tox-weechat-utils.h" #include "tox-weechat-utils.h"
#include "tox-weechat-tox.h"
#include "tox-weechat-gui.h" #include "tox-weechat-gui.h"
@ -17,8 +18,10 @@ bar_item_away(void *data,
struct t_gui_buffer *buffer, struct t_gui_buffer *buffer,
struct t_hashtable *extra_info) struct t_hashtable *extra_info)
{ {
struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
char *status = NULL;; char *status = NULL;;
switch (tox_get_self_user_status(tox)) switch (tox_get_self_user_status(identity->tox))
{ {
case TOX_USERSTATUS_BUSY: case TOX_USERSTATUS_BUSY:
status = strdup("busy"); status = strdup("busy");
@ -38,7 +41,8 @@ bar_item_input_prompt(void *data,
struct t_gui_buffer *buffer, struct t_gui_buffer *buffer,
struct t_hashtable *extra_info) struct t_hashtable *extra_info)
{ {
return tox_weechat_get_self_name_nt(); struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
return tox_weechat_get_self_name_nt(identity->tox);
} }
char * char *
@ -47,12 +51,14 @@ bar_item_buffer_plugin(void *data, struct t_gui_bar_item *item,
struct t_gui_buffer *buffer, struct t_gui_buffer *buffer,
struct t_hashtable *extra_info) struct t_hashtable *extra_info)
{ {
struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer);
char string[256]; char string[256];
const char *name = weechat_plugin_get_name(weechat_plugin); const char *name = weechat_plugin_get_name(weechat_plugin);
const char *status = tox_weechat_online_status ? "online" : "offline"; const char *status = identity->is_connected ? "online" : "offline";
const char *color = weechat_color(tox_weechat_online_status ? "green" : "red"); const char *color = weechat_color(identity->is_connected ? "green" : "red");
snprintf(string, sizeof(string), snprintf(string, sizeof(string),
"%s %s%s", name, color, status); "%s %s%s", name, color, status);

View File

@ -10,6 +10,7 @@
#include "tox-weechat.h" #include "tox-weechat.h"
#include "tox-weechat-utils.h" #include "tox-weechat-utils.h"
#include "tox-weechat-chats.h" #include "tox-weechat-chats.h"
#include "tox-weechat-friend-requests.h"
#include "tox-weechat-tox.h" #include "tox-weechat-tox.h"
@ -19,26 +20,23 @@
#define BOOTSTRAP_PORT 33445 #define BOOTSTRAP_PORT 33445
#define BOOTSTRAP_KEY "951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F" #define BOOTSTRAP_KEY "951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F"
Tox *tox = NULL;
struct t_tox_weechat_identity *tox_weechat_identities = NULL; struct t_tox_weechat_identity *tox_weechat_identities = NULL;
struct t_tox_weechat_identity *tox_weechat_last_identity = NULL;
/**
* Return the path to the Tox data file. Must be freed.
*/
char * char *
tox_weechat_data_path() tox_weechat_default_data_path(const char *name)
{ {
const char *weechat_dir = weechat_info_get("weechat_dir", NULL); const char *weechat_dir = weechat_info_get("weechat_dir", NULL);
const char *file_path = "/tox/data"; const char *tox_dir = "/tox/";
weechat_mkdir_home("tox", 0755); weechat_mkdir_home("tox", 0755);
int path_length = strlen(weechat_dir) + strlen(file_path) + 1; int path_length = strlen(weechat_dir) + strlen(tox_dir) + strlen(name) + 1;
char *tox_data_path = malloc(path_length); char *tox_data_path = malloc(sizeof(*tox_data_path) + path_length);
strcpy(tox_data_path, weechat_dir); strcpy(tox_data_path, weechat_dir);
strcat(tox_data_path, file_path); strcat(tox_data_path, tox_dir);
strcat(tox_data_path, name);
tox_data_path[path_length-1] = 0; tox_data_path[path_length-1] = 0;
return tox_data_path; return tox_data_path;
@ -88,7 +86,6 @@ tox_weechat_do_timer_cb(void *data,
if (connected ^ identity->is_connected) if (connected ^ identity->is_connected)
{ {
identity->is_connected = connected; identity->is_connected = connected;
tox_weechat_online_status = connected;
weechat_bar_item_update("buffer_plugin"); weechat_bar_item_update("buffer_plugin");
} }
@ -98,7 +95,7 @@ tox_weechat_do_timer_cb(void *data,
int int
tox_weechat_chat_refresh_timer_callback(void *data, int remaining) tox_weechat_chat_refresh_timer_callback(void *data, int remaining)
{ {
struct t_tox_chat *chat = data; struct t_tox_weechat_chat *chat = data;
tox_weechat_chat_refresh(chat); tox_weechat_chat_refresh(chat);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
@ -111,9 +108,11 @@ tox_weechat_friend_message_callback(Tox *tox,
uint16_t length, uint16_t length,
void *data) void *data)
{ {
struct t_tox_chat *chat = tox_weechat_get_friend_chat(friend_number); struct t_tox_weechat_identity *identity = data;
struct t_tox_weechat_chat *chat = tox_weechat_get_friend_chat(identity,
friend_number);
char *name = tox_weechat_get_name_nt(friend_number); char *name = tox_weechat_get_name_nt(identity->tox, friend_number);
char *message_nt = tox_weechat_null_terminate(message, length); char *message_nt = tox_weechat_null_terminate(message, length);
tox_weechat_chat_print_message(chat, name, message_nt); tox_weechat_chat_print_message(chat, name, message_nt);
@ -129,9 +128,11 @@ tox_weechat_friend_action_callback(Tox *tox,
uint16_t length, uint16_t length,
void *data) void *data)
{ {
struct t_tox_chat *chat = tox_weechat_get_friend_chat(friend_number); struct t_tox_weechat_identity *identity = data;
struct t_tox_weechat_chat *chat = tox_weechat_get_friend_chat(identity,
friend_number);
char *name = tox_weechat_get_name_nt(friend_number); char *name = tox_weechat_get_name_nt(identity->tox, friend_number);
char *message_nt = tox_weechat_null_terminate(message, length); char *message_nt = tox_weechat_null_terminate(message, length);
tox_weechat_chat_print_action(chat, name, message_nt); tox_weechat_chat_print_action(chat, name, message_nt);
@ -148,9 +149,10 @@ tox_weechat_connection_status_callback(Tox *tox,
{ {
if (status == 1) if (status == 1)
{ {
char *name = tox_weechat_get_name_nt(friend_number); struct t_tox_weechat_identity *identity = data;
char *name = tox_weechat_get_name_nt(identity->tox, friend_number);
weechat_printf(tox_main_buffer, weechat_printf(identity->buffer,
"%s%s just went online!", "%s%s just went online!",
weechat_prefix("network"), weechat_prefix("network"),
name); name);
@ -165,30 +167,34 @@ tox_weechat_name_change_callback(Tox *tox,
uint16_t length, uint16_t length,
void *data) void *data)
{ {
struct t_tox_chat *chat = tox_weechat_get_existing_friend_chat(friend_number); struct t_tox_weechat_identity *identity = data;
struct t_tox_weechat_chat *chat = tox_weechat_get_existing_friend_chat(identity,
friend_number);
char *old_name = tox_weechat_get_name_nt(identity->tox, friend_number);
char *new_name = tox_weechat_null_terminate(name, length);
if (strcmp(old_name, new_name) != 0)
{
if (chat) if (chat)
{ {
weechat_hook_timer(10, 0, 1, weechat_hook_timer(10, 0, 1,
tox_weechat_chat_refresh_timer_callback, chat); tox_weechat_chat_refresh_timer_callback, chat);
char *old_name = tox_weechat_get_name_nt(friend_number);
char *new_name = tox_weechat_null_terminate(name, length);
if (strcmp(old_name, new_name) != 0)
{
weechat_printf(chat->buffer, weechat_printf(chat->buffer,
"%s%s is now known as %s", "%s%s is now known as %s",
weechat_prefix("network"), weechat_prefix("network"),
old_name, new_name); old_name, new_name);
weechat_printf(tox_main_buffer, }
}
weechat_printf(identity->buffer,
"%s%s is now known as %s", "%s%s is now known as %s",
weechat_prefix("network"), weechat_prefix("network"),
old_name, new_name); old_name, new_name);
}
free(old_name); free(old_name);
free(new_name); free(new_name);
}
} }
void void
@ -197,7 +203,9 @@ tox_weechat_user_status_callback(Tox *tox,
uint8_t status, uint8_t status,
void *data) void *data)
{ {
struct t_tox_chat *chat = tox_weechat_get_existing_friend_chat(friend_number); struct t_tox_weechat_identity *identity = data;
struct t_tox_weechat_chat *chat = tox_weechat_get_existing_friend_chat(identity,
friend_number);
if (chat) if (chat)
weechat_hook_timer(10, 0, 1, weechat_hook_timer(10, 0, 1,
tox_weechat_chat_refresh_timer_callback, chat); tox_weechat_chat_refresh_timer_callback, chat);
@ -210,17 +218,59 @@ tox_weechat_status_message_callback(Tox *tox,
uint16_t length, uint16_t length,
void *data) void *data)
{ {
struct t_tox_chat *chat = tox_weechat_get_existing_friend_chat(friend_number); struct t_tox_weechat_identity *identity = data;
struct t_tox_weechat_chat *chat = tox_weechat_get_existing_friend_chat(identity,
friend_number);
if (chat) if (chat)
weechat_hook_timer(10, 0, 1, weechat_hook_timer(10, 0, 1,
tox_weechat_chat_refresh_timer_callback, chat); tox_weechat_chat_refresh_timer_callback, chat);
} }
void
tox_weechat_callback_friend_request(Tox *tox,
const uint8_t *public_key,
const uint8_t *message,
uint16_t length,
void *data)
{
struct t_tox_weechat_identity *identity = data;
if (identity->friend_request_count >= identity->max_friend_requests)
{
weechat_printf(identity->buffer,
"%sReceived a friend request, but your friend request "
"list is full!",
weechat_prefix("warning"));
return;
}
struct t_tox_weechat_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(identity, request);
char *hex_address = malloc(TOX_CLIENT_ID_SIZE * 2 + 1);
tox_weechat_bin2hex(request->address, TOX_CLIENT_ID_SIZE, hex_address);
weechat_printf(identity->buffer,
"%sReceived a friend request from %s: \"%s\"",
weechat_prefix("network"),
hex_address,
request->message);
free(hex_address);
}
int int
tox_weechat_bootstrap_tox(Tox *tox, char *address, uint16_t port, char *public_key) tox_weechat_bootstrap_tox(Tox *tox, char *address, uint16_t port, char *public_key)
{ {
uint8_t *binary_key = tox_weechat_hex2bin(public_key); char *binary_key = malloc(TOX_FRIEND_ADDRESS_SIZE);
int result = tox_bootstrap_from_address(tox, address, htons(port), binary_key); tox_weechat_hex2bin(public_key, binary_key);
int result = tox_bootstrap_from_address(tox,
address,
htons(port),
(uint8_t *)binary_key);
free(binary_key); free(binary_key);
return result; return result;
@ -229,7 +279,7 @@ tox_weechat_bootstrap_tox(Tox *tox, char *address, uint16_t port, char *public_k
int int
tox_weechat_bootstrap(char *address, uint16_t port, char *public_key) tox_weechat_bootstrap(char *address, uint16_t port, char *public_key)
{ {
return tox_weechat_bootstrap_tox(tox, address, port, public_key); return tox_weechat_bootstrap_tox(tox_weechat_identities->tox, address, port, public_key);
} }
void void
@ -241,22 +291,36 @@ tox_weechat_tox_init()
tox_weechat_init_identity(identity); tox_weechat_init_identity(identity);
tox = identity->tox;
tox_main_buffer = identity->buffer;
tox_weechat_identities = identity; tox_weechat_identities = identity;
} }
struct t_tox_weechat_identity *
tox_weechat_identity_new(const char *name)
{
struct t_tox_weechat_identity *identity = malloc(sizeof(*identity));
identity->name = strdup(name);
// TODO: add close callback
identity->buffer = weechat_buffer_new(identity->name, NULL, NULL, NULL, NULL);
identity->tox = tox_new(NULL);
identity->prev_identity= tox_weechat_last_identity;
identity->next_identity = NULL;
if (tox_weechat_identities == NULL)
tox_weechat_identities = identity;
else
tox_weechat_last_identity->next_identity = identity;
tox_weechat_last_identity = identity;
return identity;
}
void void
tox_weechat_init_identity(struct t_tox_weechat_identity *identity) tox_weechat_init_identity(struct t_tox_weechat_identity *identity)
{ {
// create weechat buffer
// TODO: add close callback
identity->buffer = weechat_buffer_new(identity->name, NULL, NULL, NULL, NULL);
// initialize Tox
identity->tox = tox_new(NULL);
// try loading Tox saved data // try loading Tox saved data
if (tox_weechat_load_file(identity->tox, identity->data_path) == -1) if (tox_weechat_load_file(identity->tox, identity->data_path) == -1)
{ {
@ -280,6 +344,29 @@ tox_weechat_init_identity(struct t_tox_weechat_identity *identity)
tox_callback_name_change(identity->tox, tox_weechat_name_change_callback, identity); tox_callback_name_change(identity->tox, tox_weechat_name_change_callback, identity);
tox_callback_user_status(identity->tox, tox_weechat_user_status_callback, identity); tox_callback_user_status(identity->tox, tox_weechat_user_status_callback, identity);
tox_callback_status_message(identity->tox, tox_weechat_status_message_callback, identity); tox_callback_status_message(identity->tox, tox_weechat_status_message_callback, identity);
tox_callback_friend_request(identity->tox, tox_weechat_callback_friend_request, identity);
}
struct t_tox_weechat_identity *
tox_weechat_identity_for_buffer(struct t_gui_buffer *buffer)
{
for (struct t_tox_weechat_identity *identity = tox_weechat_identities;
identity;
identity = identity->next_identity)
{
if (identity->buffer == buffer)
return identity;
for (struct t_tox_weechat_chat *chat = identity->chats;
chat;
chat = chat->next_chat)
{
if (chat->buffer == buffer)
return identity;
}
}
return NULL;
} }
void void

View File

@ -1,11 +1,11 @@
#ifndef TOX_WEECHAT_TOX_H #ifndef TOX_WEECHAT_TOX_H
#define TOX_WEECHAT_TOX_H #define TOX_WEECHAT_TOX_H
#include <tox/tox.h> #include <stdint.h>
struct t_tox_weechat_identity struct t_tox_weechat_identity
{ {
Tox *tox; struct Tox *tox;
char *name; char *name;
char *data_path; char *data_path;
@ -14,23 +14,35 @@ struct t_tox_weechat_identity
int is_connected; int is_connected;
struct t_tox_weechat_chat *chats;
struct t_tox_weechat_chat *last_chat;
struct t_tox_weechat_friend_request *friend_requests;
struct t_tox_weechat_friend_request *last_friend_request;
unsigned int friend_request_count;
unsigned int max_friend_requests;
struct t_tox_weechat_identity *next_identity; struct t_tox_weechat_identity *next_identity;
struct t_tox_weechat_identity *prev_identity; struct t_tox_weechat_identity *prev_identity;
}; };
extern struct t_tox_weechat_identity *tox_weechat_identities; extern struct t_tox_weechat_identity *tox_weechat_identities;
extern struct t_tox_weechat_identity *tox_weechat_last_identity;
/** /**
* Initialize the Tox object, bootstrap the DHT and start working. * Initialize the Tox object, bootstrap the DHT and start working.
*/ */
void tox_weechat_tox_init(); void tox_weechat_tox_init();
struct t_tox_weechat_identity *tox_weechat_identity_for_buffer(struct t_gui_buffer *buffer);
void tox_weechat_init_identity(struct t_tox_weechat_identity *identity); void tox_weechat_init_identity(struct t_tox_weechat_identity *identity);
/** /**
* Bootstrap DHT using an inet address, port and a Tox address. * Bootstrap DHT using an inet address, port and a Tox address.
*/ */
int tox_weechat_bootstrap(char *address, uint16_t port, char *public_key); int tox_weechat_bootstrap(char *address, uint16_t port, char *public_key);
int tox_weechat_bootstrap_tox(struct Tox *tox, char *address, uint16_t port, char *public_key);
/** /**
* Dump Tox to file and de-initialize. * Dump Tox to file and de-initialize.

View File

@ -10,27 +10,23 @@
#include "tox-weechat-utils.h" #include "tox-weechat-utils.h"
uint8_t * void
tox_weechat_hex2bin(const char *hex) tox_weechat_hex2bin(const char *hex, char *out)
{ {
size_t length = strlen(hex) / 2; size_t length = strlen(hex) / 2;
uint8_t *bin = malloc(length);
const char *position = hex; const char *position = hex;
for (size_t i = 0; i < length; ++i) for (size_t i = 0; i < length; ++i)
{ {
sscanf(position, "%2hhx", &bin[i]); sscanf(position, "%2hhx", &out[i]);
position += 2; position += 2;
} }
return bin;
} }
char * void
tox_weechat_bin2hex(const uint8_t *bin, size_t size) tox_weechat_bin2hex(const uint8_t *bin, size_t size, char *out)
{ {
char *hex = malloc(size * 2 + 1); char *position = out;
char *position = hex;
for (size_t i = 0; i < size; ++i) for (size_t i = 0; i < size; ++i)
{ {
@ -38,8 +34,6 @@ tox_weechat_bin2hex(const uint8_t *bin, size_t size)
position += 2; position += 2;
} }
*position = 0; *position = 0;
return hex;
} }
char * char *
@ -53,25 +47,29 @@ tox_weechat_null_terminate(const uint8_t *str, size_t length)
} }
char * char *
tox_weechat_get_name_nt(int32_t friend_number) tox_weechat_get_name_nt(Tox *tox, int32_t friend_number)
{ {
size_t length = tox_get_name_size(tox, friend_number); size_t length = tox_get_name_size(tox, friend_number);
uint8_t name[length]; uint8_t name[length];
tox_get_name(tox, friend_number, name);
// if no name, return client ID instead // if no name, return client ID instead
if (!length) if (!length)
{ {
uint8_t client_id[TOX_CLIENT_ID_SIZE]; uint8_t client_id[TOX_CLIENT_ID_SIZE];
tox_get_client_id(tox, friend_number, client_id); tox_get_client_id(tox, friend_number, client_id);
return tox_weechat_bin2hex(client_id, TOX_CLIENT_ID_SIZE);
char *hex = malloc(TOX_CLIENT_ID_SIZE * 2 + 1);
tox_weechat_bin2hex(client_id, TOX_CLIENT_ID_SIZE, hex);
return hex;
} }
tox_get_name(tox, friend_number, name);
return tox_weechat_null_terminate(name, length); return tox_weechat_null_terminate(name, length);
} }
char * char *
tox_weechat_get_status_message_nt(int32_t friend_number) tox_weechat_get_status_message_nt(Tox *tox, int32_t friend_number)
{ {
size_t length = tox_get_status_message_size(tox, friend_number); size_t length = tox_get_status_message_size(tox, friend_number);
uint8_t message[length]; uint8_t message[length];
@ -81,7 +79,7 @@ tox_weechat_get_status_message_nt(int32_t friend_number)
} }
char * char *
tox_weechat_get_self_name_nt() tox_weechat_get_self_name_nt(Tox *tox)
{ {
size_t length = tox_get_self_name_size(tox); size_t length = tox_get_self_name_size(tox);
uint8_t name[length]; uint8_t name[length];

View File

@ -4,36 +4,24 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
/** #include <tox/tox.h>
* Convert a hex string to a binary address. Return value must be freed.
*/
uint8_t *tox_weechat_hex2bin(const char *hex);
/** void
* Convert a binary address to a null-terminated hex string. Must be freed. tox_weechat_hex2bin(const char *hex, char *out);
*/
char *tox_weechat_bin2hex(const uint8_t *bin, size_t size);
/** void
* Return a new string that is a null-terminated version of the argument. Must tox_weechat_bin2hex(const uint8_t *bin, size_t size, char *out);
* be freed.
*/
char *tox_weechat_null_terminate(const uint8_t *str, size_t length);
/** char *
* Get the name of a friend as a null-terminated string. Must be freed. tox_weechat_null_terminate(const uint8_t *str, size_t length);
*/
char *tox_weechat_get_name_nt(int32_t friend_number);
/** char *
* Get the status message of a friend as a null-terminated string. Must be tox_weechat_get_name_nt(Tox *tox, int32_t friend_number);
* freed.
*/
char *tox_weechat_get_status_message_nt(int32_t friend_number);
/** char *
* Get the name of the user as a null-terminated string. Must be freed. tox_weechat_get_status_message_nt(Tox *tox, int32_t friend_number);
*/
char *tox_weechat_get_self_name_nt(); char *
tox_weechat_get_self_name_nt(Tox *tox);
#endif // TOX_WEECHAT_UTILS_H #endif // TOX_WEECHAT_UTILS_H

View File

@ -27,7 +27,6 @@ weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[])
tox_weechat_tox_init(); tox_weechat_tox_init();
tox_weechat_commands_init(); tox_weechat_commands_init();
tox_weechat_gui_init(); tox_weechat_gui_init();
tox_weechat_friend_requests_init();
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
@ -36,7 +35,6 @@ int
weechat_plugin_end(struct t_weechat_plugin *plugin) weechat_plugin_end(struct t_weechat_plugin *plugin)
{ {
tox_weechat_tox_free(); tox_weechat_tox_free();
tox_weechat_friend_requests_free();
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }

View File

@ -4,9 +4,5 @@
#include <tox/tox.h> #include <tox/tox.h>
extern struct t_weechat_plugin *weechat_plugin; extern struct t_weechat_plugin *weechat_plugin;
extern Tox *tox;
extern int tox_weechat_online_status;
extern struct t_gui_buffer *tox_main_buffer;
#endif // TOX_WEECHAT_H #endif // TOX_WEECHAT_H