From b60ac98a3e835bc3b5897c21dd53fdf04e789109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Sun, 7 Sep 2014 03:04:42 +0200 Subject: [PATCH] More support for multiple identities. --- src/tox-weechat-chats.c | 121 +++++++++---------- src/tox-weechat-chats.h | 29 +++-- src/tox-weechat-commands.c | 161 +++++++++++++++----------- src/tox-weechat-config.c | 94 +++++++++++++++ src/tox-weechat-config.h | 7 ++ src/tox-weechat-friend-requests.c | 123 ++++++-------------- src/tox-weechat-friend-requests.h | 29 +++-- src/tox-weechat-gui.c | 20 ++-- src/tox-weechat-tox.c | 185 ++++++++++++++++++++++-------- src/tox-weechat-tox.h | 16 ++- src/tox-weechat-utils.c | 32 +++--- src/tox-weechat-utils.h | 50 +++----- src/tox-weechat.c | 2 - src/tox-weechat.h | 4 - 14 files changed, 529 insertions(+), 344 deletions(-) create mode 100644 src/tox-weechat-config.c create mode 100644 src/tox-weechat-config.h diff --git a/src/tox-weechat-chats.c b/src/tox-weechat-chats.c index 7a8afb0..a82e98b 100644 --- a/src/tox-weechat-chats.c +++ b/src/tox-weechat-chats.c @@ -5,13 +5,11 @@ #include #include "tox-weechat.h" +#include "tox-weechat-tox.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); @@ -20,44 +18,45 @@ int tox_weechat_buffer_close_callback(void *data, struct t_gui_buffer *buffer); 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) - { - tox_weechat_chats_head = tox_weechat_chats_tail = chat; - chat->prev = chat->next = NULL; - } + chat->identity = identity; + + chat->prev_chat = identity->last_chat; + chat->next_chat = NULL; + + if (identity->chats == NULL) + identity->chats = chat; else - { - tox_weechat_chats_tail->next = chat; - chat->prev = tox_weechat_chats_tail; - chat->next = NULL; + identity->last_chat->next_chat = chat; - tox_weechat_chats_tail = chat; - } + identity->last_chat = chat; } void -tox_weechat_chat_remove(struct t_tox_chat *chat) +tox_weechat_chat_remove(struct t_tox_weechat_chat *chat) { - if (chat->prev) - chat->prev->next = chat->next; - if (chat->next) - chat->next->prev = chat->prev; + if (chat->prev_chat) + chat->prev_chat->next_chat = chat->next_chat; + if (chat->next_chat) + chat->next_chat->prev_chat = chat->prev_chat; - if (chat == tox_weechat_chats_head) - tox_weechat_chats_head = chat->next; - if (chat == tox_weechat_chats_tail) - tox_weechat_chats_tail = chat->prev; + if (chat == chat->identity->chats) + chat->identity->chats = chat->next_chat; + if (chat == chat->identity->last_chat) + chat->identity->last_chat = chat->prev_chat; free(chat); } 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 *status_message = tox_weechat_get_status_message_nt(chat->friend_number); + 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); weechat_buffer_set(chat->buffer, "short_name", name); weechat_buffer_set(chat->buffer, "title", status_message); @@ -66,40 +65,38 @@ tox_weechat_chat_refresh(struct t_tox_chat *chat) free(status_message); } -struct t_tox_chat * -tox_weechat_friend_chat_new(int32_t friend_number) +struct t_tox_weechat_chat * +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; 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, tox_weechat_buffer_input_callback, chat, tox_weechat_buffer_close_callback, chat); tox_weechat_chat_refresh(chat); - tox_weechat_chat_add(chat); + tox_weechat_chat_add(identity, chat); free(buffer_name); return chat; } -struct t_tox_chat * -tox_weechat_get_first_chat() -{ - return tox_weechat_chats_head; -} - -struct t_tox_chat * -tox_weechat_get_existing_friend_chat(int32_t friend_number) +struct t_tox_weechat_chat * +tox_weechat_get_existing_friend_chat(struct t_tox_weechat_identity *identity, + int32_t friend_number) { - for (struct t_tox_chat *chat = tox_weechat_chats_head; + for (struct t_tox_weechat_chat *chat = identity->chats; chat; - chat = chat->next) + chat = chat->next_chat) { if (chat->friend_number == friend_number) return chat; @@ -108,33 +105,39 @@ tox_weechat_get_existing_friend_chat(int32_t friend_number) return NULL; } -struct t_tox_chat * -tox_weechat_get_friend_chat(int32_t friend_number) +struct t_tox_weechat_chat * +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) return chat; 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) { - for (struct t_tox_chat *chat = tox_weechat_chats_head; - chat; - chat = chat->next) + for (struct t_tox_weechat_identity *identity = tox_weechat_identities; + identity; + identity = identity->next_identity) { - if (chat->buffer == buffer) - return chat; + for (struct t_tox_weechat_chat *chat = identity->chats; + chat; + chat = chat->next_chat) + { + if (chat->buffer == buffer) + return chat; + } } return NULL; } 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 *message) { @@ -142,7 +145,7 @@ tox_weechat_chat_print_message(struct t_tox_chat *chat, } 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 *message) { @@ -157,14 +160,14 @@ tox_weechat_buffer_input_callback(void *data, struct t_gui_buffer *weechat_buffer, 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, (uint8_t *)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); @@ -177,7 +180,7 @@ int tox_weechat_buffer_close_callback(void *data, struct t_gui_buffer *weechat_buffer) { - struct t_tox_chat *chat = data; + struct t_tox_weechat_chat *chat = data; tox_weechat_chat_remove(chat); return WEECHAT_RC_OK; diff --git a/src/tox-weechat-chats.h b/src/tox-weechat-chats.h index bf09542..31f772b 100644 --- a/src/tox-weechat-chats.h +++ b/src/tox-weechat-chats.h @@ -5,30 +5,37 @@ #include -struct t_tox_chat +struct t_tox_weechat_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_weechat_identity *identity; + + 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_chat *tox_weechat_get_existing_friend_chat(int32_t friend_number); -struct t_tox_chat *tox_weechat_get_chat_for_buffer(struct t_gui_buffer *target_buffer); +struct t_tox_weechat_chat * +tox_weechat_get_friend_chat(struct t_tox_weechat_identity *identity, + int32_t friend_number); + +struct t_tox_weechat_chat * +tox_weechat_get_existing_friend_chat(struct t_tox_weechat_identity *identity, + int32_t friend_number); -struct t_tox_chat *tox_weechat_get_first_chat(); +struct t_tox_weechat_chat * +tox_weechat_get_chat_for_buffer(struct t_gui_buffer *target_buffer); -void tox_weechat_chat_print_message(struct t_tox_chat *chat, +void tox_weechat_chat_print_message(struct t_tox_weechat_chat *chat, const char *sender, const char *message); -void tox_weechat_chat_print_action(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_chat *chat); +void tox_weechat_chat_refresh(struct t_tox_weechat_chat *chat); #endif // TOX_WEECHAT_CHATS_H diff --git a/src/tox-weechat-commands.c b/src/tox-weechat-commands.c index 2cdc795..ac49dfa 100644 --- a/src/tox-weechat-commands.c +++ b/src/tox-weechat-commands.c @@ -15,6 +15,8 @@ int tox_weechat_cmd_bootstrap(void *data, struct t_gui_buffer *buffer, int argc, char **argv, char **argv_eol) { + struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer); + if (argc != 4) 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]); 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.", weechat_prefix("error")); } @@ -36,41 +38,46 @@ int tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer, int argc, char **argv, char **argv_eol) { + struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer); + // /friend [list] 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]; - tox_get_friendlist(tox, friend_numbers, friend_count); + tox_get_friendlist(identity->tox, friend_numbers, friend_count); if (friend_count == 0) { - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sYou have no friends :(", weechat_prefix("network")); return WEECHAT_RC_OK; } - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%s[#] Name [client ID]", weechat_prefix("network")); for (size_t i = 0; i < friend_count; ++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]; - tox_get_client_id(tox, friend_number, client_id); - char *hex_id = tox_weechat_bin2hex(client_id, TOX_CLIENT_ID_SIZE); + tox_get_client_id(identity->tox, friend_number, client_id); + 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]", weechat_prefix("network"), - friend_number, name, hex_id); + friend_number, name, hex_address); free(name); - free(hex_id); + free(hex_address); } 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)) { - uint8_t *address = tox_weechat_hex2bin(argv[2]); + char *address = malloc(TOX_FRIEND_ADDRESS_SIZE); + tox_weechat_hex2bin(argv[2], address); char *message; if (argc == 3 || strlen(argv_eol[3]) == 0) @@ -86,46 +94,46 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer, else message = argv_eol[3]; - int32_t result = tox_add_friend(tox, - address, + int32_t result = tox_add_friend(identity->tox, + (uint8_t *)address, (uint8_t *)message, strlen(message)); switch (result) { case TOX_FAERR_TOOLONG: - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sFriend request message too long! Try again.", weechat_prefix("error")); break; case TOX_FAERR_ALREADYSENT: - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sYou have already sent a friend request to that address.", weechat_prefix("error")); break; case TOX_FAERR_OWNKEY: - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sYou can't add yourself as a friend.", weechat_prefix("error")); break; case TOX_FAERR_BADCHECKSUM: - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sInvalid friend address - try again.", weechat_prefix("error")); break; case TOX_FAERR_NOMEM: - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sCould not add friend (out of memory).", weechat_prefix("error")); break; case TOX_FAERR_UNKNOWN: case TOX_FAERR_SETNEWNOSPAM: - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sCould not add friend (unknown error).", weechat_prefix("error")); break; default: - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sFriend request sent!", weechat_prefix("network")); break; @@ -140,28 +148,30 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer, char *endptr; 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.", weechat_prefix("error")); return WEECHAT_RC_OK; } - char *name = tox_weechat_get_name_nt(friend_number); - if (tox_del_friend(tox, friend_number) == 0) + char *name = tox_weechat_get_name_nt(identity->tox, friend_number); + if (tox_del_friend(identity->tox, friend_number) == 0) { - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sRemoved %s from friend list.", weechat_prefix("network"), name); } else { - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sCould not remove friend!", weechat_prefix("error")); } + free(name); + 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; - struct t_tox_friend_request *request; + struct t_tox_weechat_friend_request *request; if (weechat_strcasecmp(argv[2], "all") == 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) tox_weechat_accept_friend_request(request); @@ -186,7 +196,7 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer, ++count; } - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%s%s %d friend requests.", weechat_prefix("network"), accept ? "Accepted" : "Declined", @@ -197,27 +207,30 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer, else { 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.", weechat_prefix("error")); 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) tox_weechat_accept_friend_request(request); else tox_weechat_decline_friend_request(request); - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%s%s friend request from %s.", weechat_prefix("network"), accept ? "Accepted" : "Declined", - hex_id); - free(hex_id); + hex_address); + free(hex_address); 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) { - struct t_tox_friend_request *request = tox_weechat_first_friend_request(); - if (request == NULL) + if (identity->friend_requests == NULL) { - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sNo pending friend requests :(", weechat_prefix("network")); } else { - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sPending friend requests:", weechat_prefix("network")); 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, - TOX_CLIENT_ID_SIZE); - weechat_printf(tox_main_buffer, + 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, "%s[%d] Address: %s\n" "[%d] Message: %s", weechat_prefix("network"), @@ -252,7 +269,6 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer, free(hex_address); - request = request->next; ++num; } } @@ -270,14 +286,16 @@ tox_weechat_cmd_me(void *data, struct t_gui_buffer *buffer, if (argc == 1) 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); + + struct t_tox_weechat_chat *chat = tox_weechat_get_chat_for_buffer(buffer); - tox_send_action(tox, + tox_send_action(identity->tox, chat->friend_number, (uint8_t *)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]); free(name); @@ -292,25 +310,27 @@ tox_weechat_cmd_msg(void *data, struct t_gui_buffer *buffer, if (argc == 1) return WEECHAT_RC_ERROR; + struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer); + char *endptr; 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.", weechat_prefix("error")); 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) { - tox_send_message(tox, + tox_send_message(identity->tox, friend_number, (uint8_t *)argv_eol[1], 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]); free(name); } @@ -322,12 +342,15 @@ int tox_weechat_cmd_myaddress(void *data, struct t_gui_buffer *buffer, 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]; - 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", weechat_prefix("network"), address_str); @@ -344,12 +367,14 @@ tox_weechat_cmd_name(void *data, struct t_gui_buffer *buffer, if (argc == 1) return WEECHAT_RC_ERROR; + struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer); + 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) { - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%s%s", weechat_prefix("error"), "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_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%sYou are now known as %s", weechat_prefix("network"), name); - for (struct t_tox_chat *chat = tox_weechat_get_first_chat(); + for (struct t_tox_weechat_chat *chat = identity->chats; chat; - chat = chat->next) + chat = chat->next_chat) { weechat_printf(chat->buffer, "%sYou are now known as %s", @@ -383,6 +408,8 @@ tox_weechat_cmd_status(void *data, struct t_gui_buffer *buffer, if (argc != 2) return WEECHAT_RC_ERROR; + struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer); + TOX_USERSTATUS status = TOX_USERSTATUS_INVALID; if (weechat_strcasecmp(argv[1], "online") == 0) status = TOX_USERSTATUS_NONE; @@ -394,7 +421,7 @@ tox_weechat_cmd_status(void *data, struct t_gui_buffer *buffer, if (status == TOX_USERSTATUS_INVALID) return WEECHAT_RC_ERROR; - tox_set_user_status(tox, status); + tox_set_user_status(identity->tox, status); weechat_bar_item_update("away"); return WEECHAT_RC_OK; @@ -404,12 +431,16 @@ int tox_weechat_cmd_statusmsg(void *data, struct t_gui_buffer *buffer, 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] : " "; - 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) { - weechat_printf(tox_main_buffer, + weechat_printf(identity->buffer, "%s%s", weechat_prefix("error"), "Could not set status message."); diff --git a/src/tox-weechat-config.c b/src/tox-weechat-config.c new file mode 100644 index 0000000..5a0467c --- /dev/null +++ b/src/tox-weechat-config.c @@ -0,0 +1,94 @@ +#include + +#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); +} diff --git a/src/tox-weechat-config.h b/src/tox-weechat-config.h new file mode 100644 index 0000000..a67ccfe --- /dev/null +++ b/src/tox-weechat-config.h @@ -0,0 +1,7 @@ +#ifndef TOX_WEECHAT_CONFIG_H +#define TOX_WEECHAT_CONFIG_H + +void +tox_weechat_config_init(); + +#endif // TOX_WEECHAT_CONFIG_H diff --git a/src/tox-weechat-friend-requests.c b/src/tox-weechat-friend-requests.c index 2aabfbc..0fa4871 100644 --- a/src/tox-weechat-friend-requests.c +++ b/src/tox-weechat-friend-requests.c @@ -6,144 +6,93 @@ #include #include "tox-weechat.h" +#include "tox-weechat-tox.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) +tox_weechat_friend_request_free(struct t_tox_weechat_friend_request *request) { free(request->message); free(request); } 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) - { - 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; + request->identity = identity; - tox_weechat_friend_requests_tail = request; + request->prev_request = identity->last_friend_request; + request->next_request = NULL; - ++friend_request_count; - } + if (identity->friend_requests == NULL) + identity->friend_requests = request; + else + identity->last_friend_request->next_request = request; + + identity->last_friend_request = request; + ++(identity->friend_request_count); } 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) - request->prev->next = request->next; - if (request->next) - request->next->prev = request->prev; + if (request->prev_request) + request->prev_request->next_request = request->next_request; + if (request->next_request) + request->next_request->prev_request = request->prev_request; - 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; + if (request == request->identity->friend_requests) + request->identity->friend_requests = request->next_request; + if (request == request->identity->last_friend_request) + request->identity->last_friend_request = request->prev_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 -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); } 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); } -struct t_tox_friend_request * -tox_weechat_friend_request_with_num(unsigned int num) +struct t_tox_weechat_friend_request * +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; - struct t_tox_friend_request *request = tox_weechat_friend_requests_head; - while (i != num && request->next) + struct t_tox_weechat_friend_request *request = identity->friend_requests; + while (i != num && request->next_request) { - request = request->next; + request = request->next_request; ++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() +tox_weechat_friend_requests_free(struct t_tox_weechat_identity *identity) { // TODO: persist requests - while (tox_weechat_friend_requests_head) - tox_weechat_friend_request_remove(tox_weechat_friend_requests_head); + while (identity->friend_requests) + tox_weechat_friend_request_remove(identity->friend_requests); } diff --git a/src/tox-weechat-friend-requests.h b/src/tox-weechat-friend-requests.h index c24f845..72a0eeb 100644 --- a/src/tox-weechat-friend-requests.h +++ b/src/tox-weechat-friend-requests.h @@ -5,7 +5,7 @@ #include -struct t_tox_friend_request +struct t_tox_weechat_friend_request { // public address of friend request uint8_t address[TOX_CLIENT_ID_SIZE]; @@ -13,18 +13,27 @@ struct t_tox_friend_request // message sent with request char *message; - // linked list pointers - struct t_tox_friend_request *next; - struct t_tox_friend_request *prev; + struct t_tox_weechat_identity *identity; + + 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_weechat_friend_request *request); + +void +tox_weechat_decline_friend_request(struct t_tox_weechat_friend_request *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); +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_init(); -void tox_weechat_friend_requests_free(); +void +tox_weechat_friend_requests_free(struct t_tox_weechat_identity *identity); #endif // TOX_WEECHAT_FRIEND_REQUESTS_H diff --git a/src/tox-weechat-gui.c b/src/tox-weechat-gui.c index 1dc3970..0f6e819 100644 --- a/src/tox-weechat-gui.c +++ b/src/tox-weechat-gui.c @@ -7,6 +7,7 @@ #include "tox-weechat.h" #include "tox-weechat-utils.h" +#include "tox-weechat-tox.h" #include "tox-weechat-gui.h" @@ -17,8 +18,10 @@ bar_item_away(void *data, struct t_gui_buffer *buffer, struct t_hashtable *extra_info) { + struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer); + char *status = NULL;; - switch (tox_get_self_user_status(tox)) + switch (tox_get_self_user_status(identity->tox)) { case TOX_USERSTATUS_BUSY: status = strdup("busy"); @@ -38,21 +41,24 @@ bar_item_input_prompt(void *data, struct t_gui_buffer *buffer, 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 * bar_item_buffer_plugin(void *data, struct t_gui_bar_item *item, - struct t_gui_window *window, - struct t_gui_buffer *buffer, - struct t_hashtable *extra_info) + struct t_gui_window *window, + struct t_gui_buffer *buffer, + struct t_hashtable *extra_info) { + struct t_tox_weechat_identity *identity = tox_weechat_identity_for_buffer(buffer); + char string[256]; const char *name = weechat_plugin_get_name(weechat_plugin); - const char *status = tox_weechat_online_status ? "online" : "offline"; - const char *color = weechat_color(tox_weechat_online_status ? "green" : "red"); + const char *status = identity->is_connected ? "online" : "offline"; + const char *color = weechat_color(identity->is_connected ? "green" : "red"); snprintf(string, sizeof(string), "%s %s%s", name, color, status); diff --git a/src/tox-weechat-tox.c b/src/tox-weechat-tox.c index 1d94abb..2df25c3 100644 --- a/src/tox-weechat-tox.c +++ b/src/tox-weechat-tox.c @@ -10,6 +10,7 @@ #include "tox-weechat.h" #include "tox-weechat-utils.h" #include "tox-weechat-chats.h" +#include "tox-weechat-friend-requests.h" #include "tox-weechat-tox.h" @@ -19,26 +20,23 @@ #define BOOTSTRAP_PORT 33445 #define BOOTSTRAP_KEY "951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F" -Tox *tox = 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 * -tox_weechat_data_path() +tox_weechat_default_data_path(const char *name) { 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); - int path_length = strlen(weechat_dir) + strlen(file_path) + 1; - char *tox_data_path = malloc(path_length); + int path_length = strlen(weechat_dir) + strlen(tox_dir) + strlen(name) + 1; + char *tox_data_path = malloc(sizeof(*tox_data_path) + path_length); 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; return tox_data_path; @@ -88,7 +86,6 @@ tox_weechat_do_timer_cb(void *data, if (connected ^ identity->is_connected) { identity->is_connected = connected; - tox_weechat_online_status = connected; weechat_bar_item_update("buffer_plugin"); } @@ -98,7 +95,7 @@ tox_weechat_do_timer_cb(void *data, int 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); return WEECHAT_RC_OK; @@ -111,9 +108,11 @@ tox_weechat_friend_message_callback(Tox *tox, uint16_t length, 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); tox_weechat_chat_print_message(chat, name, message_nt); @@ -129,9 +128,11 @@ tox_weechat_friend_action_callback(Tox *tox, uint16_t length, 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); tox_weechat_chat_print_action(chat, name, message_nt); @@ -148,9 +149,10 @@ tox_weechat_connection_status_callback(Tox *tox, { 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!", weechat_prefix("network"), name); @@ -165,30 +167,34 @@ tox_weechat_name_change_callback(Tox *tox, uint16_t length, void *data) { - struct t_tox_chat *chat = tox_weechat_get_existing_friend_chat(friend_number); - if (chat) - { - weechat_hook_timer(10, 0, 1, - tox_weechat_chat_refresh_timer_callback, chat); + 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(friend_number); - char *new_name = tox_weechat_null_terminate(name, length); + 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 (strcmp(old_name, new_name) != 0) + { + if (chat) { - weechat_printf(chat->buffer, - "%s%s is now known as %s", - weechat_prefix("network"), - old_name, new_name); - weechat_printf(tox_main_buffer, - "%s%s is now known as %s", - weechat_prefix("network"), - old_name, new_name); - } + weechat_hook_timer(10, 0, 1, + tox_weechat_chat_refresh_timer_callback, chat); - free(old_name); - free(new_name); + weechat_printf(chat->buffer, + "%s%s is now known as %s", + weechat_prefix("network"), + old_name, new_name); + } } + + weechat_printf(identity->buffer, + "%s%s is now known as %s", + weechat_prefix("network"), + old_name, new_name); + + free(old_name); + free(new_name); } void @@ -197,7 +203,9 @@ tox_weechat_user_status_callback(Tox *tox, uint8_t status, 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) weechat_hook_timer(10, 0, 1, tox_weechat_chat_refresh_timer_callback, chat); @@ -210,17 +218,59 @@ tox_weechat_status_message_callback(Tox *tox, uint16_t length, 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) weechat_hook_timer(10, 0, 1, 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 tox_weechat_bootstrap_tox(Tox *tox, char *address, uint16_t port, char *public_key) { - uint8_t *binary_key = tox_weechat_hex2bin(public_key); - int result = tox_bootstrap_from_address(tox, address, htons(port), binary_key); + char *binary_key = malloc(TOX_FRIEND_ADDRESS_SIZE); + tox_weechat_hex2bin(public_key, binary_key); + + int result = tox_bootstrap_from_address(tox, + address, + htons(port), + (uint8_t *)binary_key); free(binary_key); return result; @@ -229,7 +279,7 @@ tox_weechat_bootstrap_tox(Tox *tox, char *address, uint16_t port, char *public_k int 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 @@ -241,22 +291,36 @@ tox_weechat_tox_init() tox_weechat_init_identity(identity); - tox = identity->tox; - tox_main_buffer = identity->buffer; - tox_weechat_identities = identity; } -void -tox_weechat_init_identity(struct t_tox_weechat_identity *identity) +struct t_tox_weechat_identity * +tox_weechat_identity_new(const char *name) { - // create weechat buffer + 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); - // initialize Tox 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 +tox_weechat_init_identity(struct t_tox_weechat_identity *identity) +{ // try loading Tox saved data 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_user_status(identity->tox, tox_weechat_user_status_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 diff --git a/src/tox-weechat-tox.h b/src/tox-weechat-tox.h index ece9acf..de552ae 100644 --- a/src/tox-weechat-tox.h +++ b/src/tox-weechat-tox.h @@ -1,11 +1,11 @@ #ifndef TOX_WEECHAT_TOX_H #define TOX_WEECHAT_TOX_H -#include +#include struct t_tox_weechat_identity { - Tox *tox; + struct Tox *tox; char *name; char *data_path; @@ -14,23 +14,35 @@ struct t_tox_weechat_identity 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 *prev_identity; }; 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. */ 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); /** * 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_tox(struct Tox *tox, char *address, uint16_t port, char *public_key); /** * Dump Tox to file and de-initialize. diff --git a/src/tox-weechat-utils.c b/src/tox-weechat-utils.c index 488e888..44c1d56 100644 --- a/src/tox-weechat-utils.c +++ b/src/tox-weechat-utils.c @@ -10,27 +10,23 @@ #include "tox-weechat-utils.h" -uint8_t * -tox_weechat_hex2bin(const char *hex) +void +tox_weechat_hex2bin(const char *hex, char *out) { size_t length = strlen(hex) / 2; - uint8_t *bin = malloc(length); const char *position = hex; for (size_t i = 0; i < length; ++i) { - sscanf(position, "%2hhx", &bin[i]); + sscanf(position, "%2hhx", &out[i]); position += 2; } - - return bin; } -char * -tox_weechat_bin2hex(const uint8_t *bin, size_t size) +void +tox_weechat_bin2hex(const uint8_t *bin, size_t size, char *out) { - char *hex = malloc(size * 2 + 1); - char *position = hex; + char *position = out; 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 = 0; - - return hex; } char * @@ -53,25 +47,29 @@ tox_weechat_null_terminate(const uint8_t *str, size_t length) } 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); uint8_t name[length]; - tox_get_name(tox, friend_number, name); // if no name, return client ID instead if (!length) { uint8_t client_id[TOX_CLIENT_ID_SIZE]; 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); } 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); uint8_t message[length]; @@ -81,7 +79,7 @@ tox_weechat_get_status_message_nt(int32_t friend_number) } char * -tox_weechat_get_self_name_nt() +tox_weechat_get_self_name_nt(Tox *tox) { size_t length = tox_get_self_name_size(tox); uint8_t name[length]; diff --git a/src/tox-weechat-utils.h b/src/tox-weechat-utils.h index bc70903..9b4202c 100644 --- a/src/tox-weechat-utils.h +++ b/src/tox-weechat-utils.h @@ -4,36 +4,24 @@ #include #include -/** - * Convert a hex string to a binary address. Return value must be freed. - */ -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 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(); +#include + +void +tox_weechat_hex2bin(const char *hex, char *out); + +void +tox_weechat_bin2hex(const uint8_t *bin, size_t size, char *out); + +char * +tox_weechat_null_terminate(const uint8_t *str, size_t length); + +char * +tox_weechat_get_name_nt(Tox *tox, int32_t friend_number); + +char * +tox_weechat_get_status_message_nt(Tox *tox, int32_t friend_number); + +char * +tox_weechat_get_self_name_nt(Tox *tox); #endif // TOX_WEECHAT_UTILS_H diff --git a/src/tox-weechat.c b/src/tox-weechat.c index f015e74..c1f87e3 100644 --- a/src/tox-weechat.c +++ b/src/tox-weechat.c @@ -27,7 +27,6 @@ weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[]) tox_weechat_tox_init(); tox_weechat_commands_init(); tox_weechat_gui_init(); - tox_weechat_friend_requests_init(); return WEECHAT_RC_OK; } @@ -36,7 +35,6 @@ int weechat_plugin_end(struct t_weechat_plugin *plugin) { tox_weechat_tox_free(); - tox_weechat_friend_requests_free(); return WEECHAT_RC_OK; } diff --git a/src/tox-weechat.h b/src/tox-weechat.h index 8b871b8..ff1e588 100644 --- a/src/tox-weechat.h +++ b/src/tox-weechat.h @@ -4,9 +4,5 @@ #include 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