Add .clang-format. Reformat everything.

This commit is contained in:
Håvard Pettersson 2017-02-10 19:08:26 -08:00
parent 2947cc1562
commit fa579df623
32 changed files with 994 additions and 1126 deletions

62
.clang-format Normal file
View File

@ -0,0 +1,62 @@
---
BasedOnStyle: LLVM
Language: Cpp
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: All
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
DerivePointerAlignment: false
ForEachMacros: [ 'twc_list_foreach', 'twc_list_foreach_reverse' ]
IncludeCategories:
- Regex: '^"'
Priority: 1
- Regex: '^<'
Priority: 2
IndentCaseLabels: true
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Never
...

View File

@ -17,20 +17,9 @@
# along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>. # along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
# #
cmake_minimum_required(VERSION 2.8.11) cmake_minimum_required(VERSION 2.8.12)
project(tox-weechat C) project(tox-weechat C)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
find_package(WeeChat REQUIRED)
find_package(Tox REQUIRED
COMPONENTS CORE
OPTIONAL_COMPONENTS AV ENCRYPTSAVE)
set(PLUGIN_PATH "lib/weechat/plugins" CACHE PATH
"Path to install the plugin binary to.")
add_library(tox MODULE add_library(tox MODULE
src/twc.c src/twc.c
src/twc-bootstrap.c src/twc-bootstrap.c
@ -45,27 +34,33 @@ add_library(tox MODULE
src/twc-message-queue.c src/twc-message-queue.c
src/twc-profile.c src/twc-profile.c
src/twc-tox-callbacks.c src/twc-tox-callbacks.c
src/twc-utils.c src/twc-utils.c)
)
set(CMAKE_C_FLAGS_DEBUG "-DTWC_DEBUG") set_target_properties(tox PROPERTIES
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -Wextra -Wno-unused-parameter") PREFIX "" # remove lib prefix (libtox.so -> tox.so)
C_STANDARD 99)
target_compile_options(tox PRIVATE -Wall -Wextra -Wno-unused-parameter)
include_directories(${Tox_INCLUDE_DIRS}) # find include and shared library locations
include_directories(${WeeChat_INCLUDE_DIRS}) list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
find_package(WeeChat)
find_package(Tox REQUIRED CORE OPTIONAL_COMPONENTS AV ENCRYPTSAVE)
target_link_libraries(tox ${Tox_LIBRARIES}) target_include_directories(tox PRIVATE
"${WeeChat_INCLUDE_DIRS}"
"${Tox_INCLUDE_DIRS}")
target_link_libraries(tox "${Tox_LIBRARIES}")
if(Tox_AV_FOUND) if(Tox_AV_FOUND)
add_definitions(-DTOXAV_ENABLED) target_compile_definitions(tox PRIVATE TOXAV_ENABLED)
endif() endif()
if(Tox_ENCRYPTSAVE_FOUND) if(Tox_ENCRYPTSAVE_FOUND)
add_definitions(-DTOXENCRYPTSAVE_ENABLED) target_compile_definitions(tox PRIVATE TOXENCRYPTSAVE_ENABLED)
endif() endif()
# remove lib prefix (libtox.so -> tox.so) # install plugin binary
set_target_properties(tox PROPERTIES PREFIX "") set(PLUGIN_PATH "lib/weechat/plugins" CACHE PATH
"Path to install the plugin binary to.")
install(TARGETS tox DESTINATION "${PLUGIN_PATH}") install(TARGETS tox DESTINATION "${PLUGIN_PATH}")

View File

@ -2,7 +2,10 @@
Tox-WeeChat is a [Tox][1] protocol plugin for [WeeChat][2]. It is functional, Tox-WeeChat is a [Tox][1] protocol plugin for [WeeChat][2]. It is functional,
but lacks certain features that might be expected of a full-fledged Tox client. but lacks certain features that might be expected of a full-fledged Tox client.
[![Build Status](https://travis-ci.org/haavard/tox-weechat.svg?branch=master)][3] Tox-WeeChat is compliant with all "Required" points in the [Tox Client
Standard][3].
[![Build Status](https://travis-ci.org/haavard/tox-weechat.svg?branch=master)][4]
## Features ## Features
- One-to-one chats - One-to-one chats
@ -12,8 +15,8 @@ but lacks certain features that might be expected of a full-fledged Tox client.
- Encrypted save files - Encrypted save files
## Installation ## Installation
Tox-WeeChat is tested with [WeeChat][2] 1.7 and [TokTok c-toxcore][4] 0.1.6. Tox-WeeChat is tested with [WeeChat][2] 1.7 and [TokTok c-toxcore][5] 0.1.6.
It also requires CMake 2.8.11 or later to be built. Installation is fairly It also requires CMake 2.8.12 or newer to be built. Installation is fairly
simple; after getting the source code, compile and install with CMake: simple; after getting the source code, compile and install with CMake:
$ mkdir build && cd build $ mkdir build && cd build
@ -25,7 +28,8 @@ This installs the plugin binary `tox.so` to the recommended location
## Usage ## Usage
- If the plugin does not load automatically, load it with `/plugin load tox`. - If the plugin does not load automatically, load it with `/plugin load tox`.
You may have to specify the full path to the plugin binary. You may have to specify the full path to the plugin binary if you installed
it to a non-standard location.
- Create a new profile with `/tox create <profile name>`. The data file is - Create a new profile with `/tox create <profile name>`. The data file is
stored in `~/.weechat/tox/` by default. stored in `~/.weechat/tox/` by default.
- Load your profile and connect to the Tox network with - Load your profile and connect to the Tox network with
@ -45,8 +49,7 @@ your screen space, you can set the following options in WeeChat:
Make sure the proxy type, address and port is correct, and that UDP is Make sure the proxy type, address and port is correct, and that UDP is
disabled (`/set tox.profile.*.udp`). disabled (`/set tox.profile.*.udp`).
License ## License
---------
Copyright (c) 2017 Håvard Pettersson <mail@haavard.me> Copyright (c) 2017 Håvard Pettersson <mail@haavard.me>
This file is part of Tox-WeeChat. This file is part of Tox-WeeChat.
@ -66,6 +69,7 @@ along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
[1]: http://tox.chat [1]: http://tox.chat
[2]: http://weechat.org [2]: http://weechat.org
[3]: https://travis-ci.org/haavard/tox-weechat [3]: https://github.com/Tox/Tox-Client-Standard
[4]: https://github.com/TokTok/c-toxcore [4]: https://travis-ci.org/haavard/tox-weechat
[5]: https://github.com/TokTok/c-toxcore

View File

@ -31,22 +31,25 @@ find_library(Tox_ENCRYPTSAVE_LIBRARY toxencryptsave)
if(Tox_CORE_INCLUDE_DIR AND Tox_CORE_LIBRARY) if(Tox_CORE_INCLUDE_DIR AND Tox_CORE_LIBRARY)
set(Tox_CORE_FOUND TRUE) set(Tox_CORE_FOUND TRUE)
list(APPEND Tox_INCLUDE_DIRS ${Tox_CORE_INCLUDE_DIR}) list(APPEND Tox_INCLUDE_DIRS "${Tox_CORE_INCLUDE_DIR}")
list(APPEND Tox_LIBRARIES ${Tox_CORE_LIBRARY}) list(APPEND Tox_LIBRARIES "${Tox_CORE_LIBRARY}")
endif() endif()
if(Tox_AV_INCLUDE_DIR AND Tox_AV_LIBRARY) if(Tox_AV_INCLUDE_DIR AND Tox_AV_LIBRARY)
set(Tox_AV_FOUND TRUE) set(Tox_AV_FOUND TRUE)
list(APPEND Tox_INCLUDE_DIRS ${Tox_AV_INCLUDE_DIR}) list(APPEND Tox_INCLUDE_DIRS "${Tox_AV_INCLUDE_DIR}")
list(APPEND Tox_LIBRARIES ${Tox_AV_LIBRARY}) list(APPEND Tox_LIBRARIES "${Tox_AV_LIBRARY}")
endif() endif()
if(Tox_ENCRYPTSAVE_INCLUDE_DIR AND Tox_ENCRYPTSAVE_LIBRARY) if(Tox_ENCRYPTSAVE_INCLUDE_DIR AND Tox_ENCRYPTSAVE_LIBRARY)
set(Tox_ENCRYPTSAVE_FOUND TRUE) set(Tox_ENCRYPTSAVE_FOUND TRUE)
list(APPEND Tox_INCLUDE_DIRS ${Tox_ENCRYPTSAVE_INCLUDE_DIR}) list(APPEND Tox_INCLUDE_DIRS "${Tox_ENCRYPTSAVE_INCLUDE_DIR}")
list(APPEND Tox_LIBRARIES ${Tox_ENCRYPTSAVE_LIBRARY}) list(APPEND Tox_LIBRARIES "${Tox_ENCRYPTSAVE_LIBRARY}")
endif() endif()
list(REMOVE_DUPLICATES Tox_INCLUDE_DIRS)
list(REMOVE_DUPLICATES Tox_LIBRARIES)
include(FindPackageHandleStandardArgs) include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Tox find_package_handle_standard_args(Tox
FOUND_VAR Tox_FOUND FOUND_VAR Tox_FOUND

View File

@ -78,27 +78,24 @@ char *twc_bootstrap_keys[] = {
/* Maintainer: ws, location: NZ */ /* Maintainer: ws, location: NZ */
"F202E0936ABEE09067F55B0955C3FF6A84ABEED3C750A9EB930D926D03248F4C", "F202E0936ABEE09067F55B0955C3FF6A84ABEED3C750A9EB930D926D03248F4C",
/* Maintainer: clearmartin, location: DE */ /* Maintainer: clearmartin, location: DE */
"CD133B521159541FB1D326DE9850F5E56A6C724B5B8E5EB5CD8D950408E95707" "CD133B521159541FB1D326DE9850F5E56A6C724B5B8E5EB5CD8D950408E95707"};
};
char *twc_bootstrap_addresses[] = { char *twc_bootstrap_addresses[] = {
"130.133.110.14", "205.185.116.116", "198.98.51.198", "130.133.110.14", "205.185.116.116", "198.98.51.198", "108.61.165.198",
"108.61.165.198", "194.249.212.109", "185.25.116.107", "194.249.212.109", "185.25.116.107", "95.215.46.114", "5.189.176.217",
"95.215.46.114", "5.189.176.217", "148.251.23.146", "104.223.122.15", "148.251.23.146", "104.223.122.15", "104.233.104.126", "51.254.84.212",
"104.233.104.126", "51.254.84.212", "185.58.206.164", "92.54.84.70", "185.58.206.164", "92.54.84.70", "95.215.44.78", "163.172.136.118",
"95.215.44.78", "163.172.136.118", "37.97.185.116", "193.124.186.205", "37.97.185.116", "193.124.186.205", "80.87.193.193", "46.229.52.198",
"80.87.193.193", "46.229.52.198", "185.14.30.213", "77.37.160.178", "185.14.30.213", "77.37.160.178", "85.21.144.224", "37.187.122.30",
"85.21.144.224", "37.187.122.30", "202.36.75.162", "46.101.197.175" "202.36.75.162", "46.101.197.175"};
};
uint16_t twc_bootstrap_ports[] = { uint16_t twc_bootstrap_ports[] = {
33445, 33445, 33445, 33445, 33445, 33445, 33445, 5190, 2306, 33445, 33445, 33445, 33445, 33445, 33445, 33445, 33445, 5190, 2306,
33445, 33445, 33445, 33445, 33445, 33445, 33445, 5228, 33445, 33445, 33445, 33445, 33445, 33445, 33445, 33445, 33445, 33445, 5228,
443, 33440, 33445, 33445, 33445, 443 33445, 33445, 443, 33440, 33445, 33445, 33445, 443};
};
int twc_bootstrap_count = sizeof(twc_bootstrap_addresses) int twc_bootstrap_count =
/ sizeof(twc_bootstrap_addresses[0]); sizeof(twc_bootstrap_addresses) / sizeof(twc_bootstrap_addresses[0]);
/** /**
* Bootstrap a Tox object with a DHT bootstrap node. Returns the result of * Bootstrap a Tox object with a DHT bootstrap node. Returns the result of
@ -112,8 +109,7 @@ twc_bootstrap_tox(Tox *tox, const char *address, uint16_t port,
twc_hex2bin(public_key, TOX_ADDRESS_SIZE, binary_key); twc_hex2bin(public_key, TOX_ADDRESS_SIZE, binary_key);
TOX_ERR_BOOTSTRAP err; TOX_ERR_BOOTSTRAP err;
int result = tox_bootstrap(tox, address, port, int result = tox_bootstrap(tox, address, port, binary_key, &err);
binary_key, &err);
return result; return result;
} }
@ -125,8 +121,6 @@ void
twc_bootstrap_random_node(Tox *tox) twc_bootstrap_random_node(Tox *tox)
{ {
int i = rand() % twc_bootstrap_count; int i = rand() % twc_bootstrap_count;
twc_bootstrap_tox(tox, twc_bootstrap_addresses[i], twc_bootstrap_tox(tox, twc_bootstrap_addresses[i], twc_bootstrap_ports[i],
twc_bootstrap_ports[i], twc_bootstrap_keys[i]);
twc_bootstrap_keys[i]);
} }

View File

@ -29,5 +29,4 @@ twc_bootstrap_tox(Tox *tox, const char *address, uint16_t port,
void void
twc_bootstrap_random_node(Tox *tox); twc_bootstrap_random_node(Tox *tox);
#endif // TOX_WEECHAT_BOOTSTRAP_H #endif /* TOX_WEECHAT_BOOTSTRAP_H */

View File

@ -20,14 +20,14 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#include "twc.h"
#include "twc-list.h" #include "twc-list.h"
#include "twc-profile.h"
#include "twc-message-queue.h" #include "twc-message-queue.h"
#include "twc-profile.h"
#include "twc-utils.h" #include "twc-utils.h"
#include "twc.h"
#include "twc-chat.h" #include "twc-chat.h"
@ -36,13 +36,11 @@ const char *twc_tag_sent_message = "tox_sent";
const char *twc_tag_received_message = "tox_received"; const char *twc_tag_received_message = "tox_received";
int int
twc_chat_buffer_input_callback(const void *pointer, twc_chat_buffer_input_callback(const void *pointer, void *data,
void *data,
struct t_gui_buffer *weechat_buffer, struct t_gui_buffer *weechat_buffer,
const char *input_data); const char *input_data);
int int
twc_chat_buffer_close_callback(const void *pointer, twc_chat_buffer_close_callback(const void *pointer, void *data,
void *data,
struct t_gui_buffer *weechat_buffer); struct t_gui_buffer *weechat_buffer);
/** /**
@ -65,20 +63,20 @@ twc_chat_new(struct t_twc_profile *profile, const char *name)
chat->buffer = weechat_buffer_search("tox", full_name); chat->buffer = weechat_buffer_search("tox", full_name);
if (!(chat->buffer)) if (!(chat->buffer))
{ {
chat->buffer = weechat_buffer_new(full_name, chat->buffer = weechat_buffer_new(
twc_chat_buffer_input_callback, chat, NULL, full_name, twc_chat_buffer_input_callback, chat, NULL,
twc_chat_buffer_close_callback, chat, NULL); twc_chat_buffer_close_callback, chat, NULL);
} }
else else
{ {
weechat_buffer_set_pointer(chat->buffer, weechat_buffer_set_pointer(chat->buffer, "input_callback",
"input_callback",
twc_chat_buffer_input_callback); twc_chat_buffer_input_callback);
weechat_buffer_set_pointer(chat->buffer, "input_callback_pointer", chat); weechat_buffer_set_pointer(chat->buffer, "input_callback_pointer",
weechat_buffer_set_pointer(chat->buffer, chat);
"close_callback", weechat_buffer_set_pointer(chat->buffer, "close_callback",
twc_chat_buffer_close_callback); twc_chat_buffer_close_callback);
weechat_buffer_set_pointer(chat->buffer, "close_callback_pointer", chat); weechat_buffer_set_pointer(chat->buffer, "close_callback_pointer",
chat);
} }
free(full_name); free(full_name);
@ -109,7 +107,7 @@ twc_chat_new_friend(struct t_twc_profile *profile, int32_t friend_number)
TOX_ERR_FRIEND_GET_PUBLIC_KEY err; TOX_ERR_FRIEND_GET_PUBLIC_KEY err;
tox_friend_get_public_key(profile->tox, friend_number, client_id, &err); tox_friend_get_public_key(profile->tox, friend_number, client_id, &err);
if (err != TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK) if (err != TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK)
return NULL; return NULL;
char buffer_name[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char buffer_name[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(client_id, TOX_PUBLIC_KEY_SIZE, buffer_name); twc_bin2hex(client_id, TOX_PUBLIC_KEY_SIZE, buffer_name);
@ -135,8 +133,8 @@ twc_chat_new_group(struct t_twc_profile *profile, int32_t group_number)
{ {
chat->group_number = group_number; chat->group_number = group_number;
chat->nicklist_group = weechat_nicklist_add_group(chat->buffer, NULL, chat->nicklist_group =
NULL, NULL, true); weechat_nicklist_add_group(chat->buffer, NULL, NULL, NULL, true);
chat->nicks = weechat_list_new(); chat->nicks = weechat_list_new();
weechat_buffer_set(chat->buffer, "nicklist", "1"); weechat_buffer_set(chat->buffer, "nicklist", "1");
@ -158,19 +156,20 @@ twc_chat_refresh(const struct t_twc_chat *chat)
if (chat->friend_number >= 0) if (chat->friend_number >= 0)
{ {
name = twc_get_name_nt(chat->profile->tox, name = twc_get_name_nt(chat->profile->tox, chat->friend_number);
chat->friend_number); title =
title = twc_get_status_message_nt(chat->profile->tox, twc_get_status_message_nt(chat->profile->tox, chat->friend_number);
chat->friend_number);
} }
else if (chat->group_number >= 0) else if (chat->group_number >= 0)
{ {
char group_name[TOX_MAX_NAME_LENGTH + 1] = {0}; char group_name[TOX_MAX_NAME_LENGTH + 1] = {0};
int len = tox_conference_get_title_size(chat->profile->tox, chat->group_number, int len = tox_conference_get_title_size(chat->profile->tox,
&err); chat->group_number, &err);
if ((err == TOX_ERR_CONFERENCE_TITLE_OK) && (len <= TOX_MAX_NAME_LENGTH)) if ((err == TOX_ERR_CONFERENCE_TITLE_OK) &&
rc = tox_conference_get_title(chat->profile->tox, chat->group_number, (len <= TOX_MAX_NAME_LENGTH))
(uint8_t *)group_name, &err); rc =
tox_conference_get_title(chat->profile->tox, chat->group_number,
(uint8_t *)group_name, &err);
if (!rc) if (!rc)
sprintf(group_name, "Group Chat %d", chat->group_number); sprintf(group_name, "Group Chat %d", chat->group_number);
@ -204,8 +203,7 @@ twc_chat_refresh_timer_callback(const void *pointer, void *data, int remaining)
void void
twc_chat_queue_refresh(struct t_twc_chat *chat) twc_chat_queue_refresh(struct t_twc_chat *chat)
{ {
weechat_hook_timer(1, 0, 1, weechat_hook_timer(1, 0, 1, twc_chat_refresh_timer_callback, chat, NULL);
twc_chat_refresh_timer_callback, chat, NULL);
} }
/** /**
@ -213,12 +211,12 @@ twc_chat_queue_refresh(struct t_twc_chat *chat)
* create a new one. * create a new one.
*/ */
struct t_twc_chat * struct t_twc_chat *
twc_chat_search_friend(struct t_twc_profile *profile, twc_chat_search_friend(struct t_twc_profile *profile, int32_t friend_number,
int32_t friend_number, bool create_new) bool create_new)
{ {
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(profile->chats, index, item) twc_list_foreach (profile->chats, index, item)
{ {
if (item->chat->friend_number == friend_number) if (item->chat->friend_number == friend_number)
return item->chat; return item->chat;
@ -235,12 +233,12 @@ twc_chat_search_friend(struct t_twc_profile *profile,
* create a new one. * create a new one.
*/ */
struct t_twc_chat * struct t_twc_chat *
twc_chat_search_group(struct t_twc_profile *profile, twc_chat_search_group(struct t_twc_profile *profile, int32_t group_number,
int32_t group_number, bool create_new) bool create_new)
{ {
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(profile->chats, index, item) twc_list_foreach (profile->chats, index, item)
{ {
if (item->chat->group_number == group_number) if (item->chat->group_number == group_number)
return item->chat; return item->chat;
@ -260,11 +258,11 @@ twc_chat_search_buffer(struct t_gui_buffer *buffer)
{ {
size_t profile_index; size_t profile_index;
struct t_twc_list_item *profile_item; struct t_twc_list_item *profile_item;
twc_list_foreach(twc_profiles, profile_index, profile_item) twc_list_foreach (twc_profiles, profile_index, profile_item)
{ {
size_t chat_index; size_t chat_index;
struct t_twc_list_item *chat_item; struct t_twc_list_item *chat_item;
twc_list_foreach(profile_item->profile->chats, chat_index, chat_item) twc_list_foreach (profile_item->profile->chats, chat_index, chat_item)
{ {
if (chat_item->chat->buffer == buffer) if (chat_item->chat->buffer == buffer)
{ {
@ -275,32 +273,24 @@ twc_chat_search_buffer(struct t_gui_buffer *buffer)
return NULL; return NULL;
} }
/** /**
* Print a chat message to a chat's buffer. * Print a chat message to a chat's buffer.
*/ */
void void
twc_chat_print_message(struct t_twc_chat *chat, twc_chat_print_message(struct t_twc_chat *chat, const char *tags,
const char *tags, const char *color, const char *sender,
const char *color, const char *message, TOX_MESSAGE_TYPE message_type)
const char *sender,
const char *message,
TOX_MESSAGE_TYPE message_type)
{ {
switch (message_type) switch (message_type)
{ {
case TOX_MESSAGE_TYPE_NORMAL: case TOX_MESSAGE_TYPE_NORMAL:
weechat_printf_date_tags(chat->buffer, 0, tags, weechat_printf_date_tags(chat->buffer, 0, tags, "%s%s%s\t%s", color,
"%s%s%s\t%s", sender, weechat_color("reset"), message);
color, sender,
weechat_color("reset"), message);
break; break;
case TOX_MESSAGE_TYPE_ACTION: case TOX_MESSAGE_TYPE_ACTION:
weechat_printf_date_tags(chat->buffer, 0, tags, weechat_printf_date_tags(chat->buffer, 0, tags, "%s%s%s%s %s",
"%s%s%s%s %s", weechat_prefix("action"), color, sender,
weechat_prefix("action"), weechat_color("reset"), message);
color, sender,
weechat_color("reset"), message);
break; break;
} }
} }
@ -315,13 +305,12 @@ twc_chat_send_message(struct t_twc_chat *chat, const char *message,
TOX_ERR_CONFERENCE_SEND_MESSAGE err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK; TOX_ERR_CONFERENCE_SEND_MESSAGE err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK;
if (chat->friend_number >= 0) if (chat->friend_number >= 0)
{ {
twc_message_queue_add_friend_message(chat->profile, twc_message_queue_add_friend_message(chat->profile, chat->friend_number,
chat->friend_number,
message, message_type); message, message_type);
char *name = twc_get_self_name_nt(chat->profile->tox); char *name = twc_get_self_name_nt(chat->profile->tox);
twc_chat_print_message(chat, "notify_message", twc_chat_print_message(chat, "notify_message",
weechat_color("chat_nick_self"), name, weechat_color("chat_nick_self"), name, message,
message, message_type); message_type);
free(name); free(name);
} }
else if (chat->group_number >= 0) else if (chat->group_number >= 0)
@ -332,8 +321,8 @@ twc_chat_send_message(struct t_twc_chat *chat, const char *message,
int fit_len = twc_fit_utf8(message, TWC_MAX_GROUP_MESSAGE_LENGTH); int fit_len = twc_fit_utf8(message, TWC_MAX_GROUP_MESSAGE_LENGTH);
err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK; err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK;
tox_conference_send_message(chat->profile->tox, chat->group_number, tox_conference_send_message(chat->profile->tox, chat->group_number,
message_type, (uint8_t *)message, fit_len, message_type, (uint8_t *)message,
&err); fit_len, &err);
if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK) if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK)
break; break;
message += fit_len; message += fit_len;
@ -341,12 +330,10 @@ twc_chat_send_message(struct t_twc_chat *chat, const char *message,
} }
if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK) if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK)
{ {
weechat_printf(chat->buffer, weechat_printf(
"%s%sFailed to send message with error %d%s", chat->buffer, "%s%sFailed to send message with error %d%s",
weechat_prefix("error"), weechat_prefix("error"), weechat_color("chat_highlight"), err,
weechat_color("chat_highlight"), weechat_color("reset"));
err,
weechat_color("reset"));
} }
} }
} }
@ -382,9 +369,10 @@ twc_chat_buffer_close_callback(const void *pointer, void *data,
tox_conference_delete(chat->profile->tox, chat->group_number, &err); tox_conference_delete(chat->profile->tox, chat->group_number, &err);
if (err != TOX_ERR_CONFERENCE_DELETE_OK) if (err != TOX_ERR_CONFERENCE_DELETE_OK)
{ {
weechat_printf(chat->profile->buffer, weechat_printf(
"%swarning: failed to leave group chat with error %d", chat->profile->buffer,
weechat_prefix("error"), err); "%swarning: failed to leave group chat with error %d",
weechat_prefix("error"), err);
} }
} }
@ -425,4 +413,3 @@ twc_chat_free_list(struct t_twc_list *list)
free(list); free(list);
} }

View File

@ -20,8 +20,8 @@
#ifndef TOX_WEECHAT_CHAT_H #ifndef TOX_WEECHAT_CHAT_H
#define TOX_WEECHAT_CHAT_H #define TOX_WEECHAT_CHAT_H
#include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
struct t_twc_list; struct t_twc_list;
@ -42,12 +42,12 @@ struct t_twc_chat
}; };
struct t_twc_chat * struct t_twc_chat *
twc_chat_search_friend(struct t_twc_profile *profile, twc_chat_search_friend(struct t_twc_profile *profile, int32_t friend_number,
int32_t friend_number, bool create_new); bool create_new);
struct t_twc_chat * struct t_twc_chat *
twc_chat_search_group(struct t_twc_profile *profile, twc_chat_search_group(struct t_twc_profile *profile, int32_t group_number,
int32_t group_number, bool create_new); bool create_new);
struct t_twc_chat * struct t_twc_chat *
twc_chat_search_buffer(struct t_gui_buffer *target_buffer); twc_chat_search_buffer(struct t_gui_buffer *target_buffer);
@ -56,12 +56,9 @@ enum t_twc_rc
twc_chat_set_logging(struct t_twc_chat const *const chat, bool logging); twc_chat_set_logging(struct t_twc_chat const *const chat, bool logging);
void void
twc_chat_print_message(struct t_twc_chat *chat, twc_chat_print_message(struct t_twc_chat *chat, const char *tags,
const char *tags, const char *color, const char *sender,
const char *color, const char *message, TOX_MESSAGE_TYPE message_type);
const char *sender,
const char *message,
TOX_MESSAGE_TYPE message_type);
void void
twc_chat_send_message(struct t_twc_chat *chat, const char *message, twc_chat_send_message(struct t_twc_chat *chat, const char *message,
@ -76,5 +73,4 @@ twc_chat_free(struct t_twc_chat *chat);
void void
twc_chat_free_list(struct t_twc_list *list); twc_chat_free_list(struct t_twc_list *list);
#endif // TOX_WEECHAT_CHAT_H #endif /* TOX_WEECHAT_CHAT_H */

View File

@ -17,21 +17,21 @@
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>. * along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#include "twc.h" #include "twc-bootstrap.h"
#include "twc-list.h"
#include "twc-profile.h"
#include "twc-chat.h" #include "twc-chat.h"
#include "twc-config.h"
#include "twc-friend-request.h" #include "twc-friend-request.h"
#include "twc-group-invite.h" #include "twc-group-invite.h"
#include "twc-bootstrap.h" #include "twc-list.h"
#include "twc-config.h" #include "twc-profile.h"
#include "twc-utils.h" #include "twc-utils.h"
#include "twc.h"
#include "twc-commands.h" #include "twc-commands.h"
@ -45,96 +45,88 @@ enum TWC_FRIEND_MATCH
* Make sure a command is executed on a Tox profile buffer. If not, warn user * Make sure a command is executed on a Tox profile buffer. If not, warn user
* and abort. * and abort.
*/ */
#define TWC_CHECK_PROFILE(profile) \ #define TWC_CHECK_PROFILE(profile) \
if (!profile) \ if (!profile) \
{ \ { \
weechat_printf(NULL, \ weechat_printf(NULL, "%s%s: command \"%s\" must be executed on a Tox " \
"%s%s: command \"%s\" must be executed on a Tox " \ "buffer", \
"buffer", \ weechat_prefix("error"), weechat_plugin->name, \
weechat_prefix("error"), weechat_plugin->name, \ argv[0]); \
argv[0]); \ return WEECHAT_RC_OK; \
return WEECHAT_RC_OK; \ }
} \
/** /**
* Make sure a command is executed in a chat buffer. If not, warn user and * Make sure a command is executed in a chat buffer. If not, warn user and
* abort. * abort.
*/ */
#define TWC_CHECK_CHAT(chat) \ #define TWC_CHECK_CHAT(chat) \
if (!chat) \ if (!chat) \
{ \ { \
weechat_printf(NULL, \ weechat_printf( \
"%s%s: command \"%s\" must be executed in a chat " \ NULL, "%s%s: command \"%s\" must be executed in a chat " \
"buffer", \ "buffer", \
weechat_prefix("error"), \ weechat_prefix("error"), weechat_plugin->name, argv[0]); \
weechat_plugin->name, \ return WEECHAT_RC_OK; \
argv[0]); \
return WEECHAT_RC_OK; \
} }
/** /**
* Make sure a command is executed in a group chat buffer. If not, warn user * Make sure a command is executed in a group chat buffer. If not, warn user
* and abort. * and abort.
*/ */
#define TWC_CHECK_GROUP_CHAT(chat) \ #define TWC_CHECK_GROUP_CHAT(chat) \
if (!chat || chat->group_number < 0) \ if (!chat || chat->group_number < 0) \
{ \ { \
weechat_printf(NULL, \ weechat_printf( \
"%s%s: command \"%s\" must be executed in a group " \ NULL, "%s%s: command \"%s\" must be executed in a group " \
"chat buffer ", \ "chat buffer ", \
weechat_prefix("error"), \ weechat_prefix("error"), weechat_plugin->name, argv[0]); \
weechat_plugin->name, \ return WEECHAT_RC_OK; \
argv[0]); \
return WEECHAT_RC_OK; \
} }
/** /**
* Make sure a profile with the given name exists. If not, warn user and abort. * Make sure a profile with the given name exists. If not, warn user and abort.
*/ */
#define TWC_CHECK_PROFILE_EXISTS(profile) \ #define TWC_CHECK_PROFILE_EXISTS(profile) \
if (!profile) \ if (!profile) \
{ \ { \
weechat_printf(NULL, \ weechat_printf(NULL, "%s%s: profile \"%s\" does not exist.", \
"%s%s: profile \"%s\" does not exist.", \ weechat_prefix("error"), weechat_plugin->name, name); \
weechat_prefix("error"), weechat_plugin->name, \ return WEECHAT_RC_OK; \
name); \
return WEECHAT_RC_OK; \
} }
/** /**
* Make sure a profile is loaded. * Make sure a profile is loaded.
*/ */
#define TWC_CHECK_PROFILE_LOADED(profile) \ #define TWC_CHECK_PROFILE_LOADED(profile) \
if (!(profile->tox)) \ if (!(profile->tox)) \
{ \ { \
weechat_printf(profile->buffer, \ weechat_printf(profile->buffer, \
"%sprofile must be loaded for command \"%s\"", \ "%sprofile must be loaded for command \"%s\"", \
weechat_prefix("error"), argv[0]); \ weechat_prefix("error"), argv[0]); \
return WEECHAT_RC_OK; \ return WEECHAT_RC_OK; \
} }
/** /**
* Make sure friend exists. * Make sure friend exists.
*/ */
#define TWC_CHECK_FRIEND_NUMBER(profile, number, string) \ #define TWC_CHECK_FRIEND_NUMBER(profile, number, string) \
if (number == TWC_FRIEND_MATCH_NOMATCH) \ if (number == TWC_FRIEND_MATCH_NOMATCH) \
{ \ { \
weechat_printf(profile->buffer, \ weechat_printf(profile->buffer, \
"%sno friend number, name or Tox ID found matching " \ "%sno friend number, name or Tox ID found matching " \
"\"%s\"", \ "\"%s\"", \
weechat_prefix("error"), string); \ weechat_prefix("error"), string); \
return WEECHAT_RC_OK; \ return WEECHAT_RC_OK; \
} \ } \
if (number == TWC_FRIEND_MATCH_AMBIGUOUS) \ if (number == TWC_FRIEND_MATCH_AMBIGUOUS) \
{ \ { \
weechat_printf(profile->buffer, \ weechat_printf(profile->buffer, \
"%smultiple friends with name \"%s\" found; please " \ "%smultiple friends with name \"%s\" found; please " \
"use Tox ID instead", \ "use Tox ID instead", \
weechat_prefix("error"), string); \ weechat_prefix("error"), string); \
return WEECHAT_RC_OK; \ return WEECHAT_RC_OK; \
} }
/** /**
* Get number of friend matching string. Tries to match number, name and * Get number of friend matching string. Tries to match number, name and
* Tox ID. * Tox ID.
@ -150,8 +142,8 @@ twc_match_friend(struct t_twc_profile *profile, const char *search_string)
char *endptr; char *endptr;
uint32_t friend_number = (uint32_t)strtoul(search_string, &endptr, 10); uint32_t friend_number = (uint32_t)strtoul(search_string, &endptr, 10);
if (endptr == search_string + strlen(search_string) if (endptr == search_string + strlen(search_string) &&
&& tox_friend_exists(profile->tox, friend_number)) tox_friend_exists(profile->tox, friend_number))
return friend_number; return friend_number;
size_t search_size = strlen(search_string); size_t search_size = strlen(search_string);
@ -162,7 +154,8 @@ twc_match_friend(struct t_twc_profile *profile, const char *search_string)
uint8_t tox_id[TOX_PUBLIC_KEY_SIZE]; uint8_t tox_id[TOX_PUBLIC_KEY_SIZE];
char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1];
if (tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, NULL)) if (tox_friend_get_public_key(profile->tox, friend_numbers[i],
tox_id, NULL))
{ {
twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id); twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id);
@ -195,7 +188,7 @@ twc_cmd_bootstrap(const void *pointer, void *data, struct t_gui_buffer *buffer,
TWC_CHECK_PROFILE(profile); TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile); TWC_CHECK_PROFILE_LOADED(profile);
// /bootstrap connect <address> <port> <key> /* /bootstrap connect <address> <port> <key> */
if (argc == 5 && weechat_strcasecmp(argv[1], "connect") == 0) if (argc == 5 && weechat_strcasecmp(argv[1], "connect") == 0)
{ {
char *address = argv[2]; char *address = argv[2];
@ -226,7 +219,7 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
TWC_CHECK_PROFILE(profile); TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile); TWC_CHECK_PROFILE_LOADED(profile);
// /friend or /friend list /* /friend or /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_self_get_friend_list_size(profile->tox); size_t friend_count = tox_self_get_friend_list_size(profile->tox);
@ -235,30 +228,32 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
if (friend_count == 0) if (friend_count == 0)
{ {
weechat_printf(profile->buffer, weechat_printf(profile->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(profile->buffer, weechat_printf(profile->buffer, "%s[#] Name [Tox ID (short)] Status",
"%s[#] Name [Tox ID (short)] Status",
weechat_prefix("network")); weechat_prefix("network"));
for (size_t i = 0; i < friend_count; ++i) for (size_t i = 0; i < friend_count; ++i)
{ {
uint32_t friend_number = friend_numbers[i]; uint32_t friend_number = friend_numbers[i];
char *name = twc_get_name_nt(profile->tox, friend_number); char *name = twc_get_name_nt(profile->tox, friend_number);
char *hex_address = twc_get_friend_id_short(profile->tox, char *hex_address =
friend_number); twc_get_friend_id_short(profile->tox, friend_number);
char *status = twc_get_status_message_nt(profile->tox, friend_number); char *status =
twc_get_status_message_nt(profile->tox, friend_number);
char *online_color = char *online_color =
(tox_friend_get_connection_status(profile->tox, friend_number, NULL) != TOX_CONNECTION_NONE) ? "chat_nick" : "chat_nick_offline"; (tox_friend_get_connection_status(profile->tox, friend_number,
weechat_printf(profile->buffer, NULL) != TOX_CONNECTION_NONE)
"%s[%d] %s%s [%s]%s %s", ? "chat_nick"
weechat_prefix("network"), : "chat_nick_offline";
friend_number, weechat_color(online_color), name, hex_address, weechat_color("reset"), status); weechat_printf(profile->buffer, "%s[%d] %s%s [%s]%s %s",
weechat_prefix("network"), friend_number,
weechat_color(online_color), name, hex_address,
weechat_color("reset"), status);
free(name); free(name);
free(status); free(status);
@ -268,7 +263,7 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
// /friend add [-force] <Tox ID> [<message>] /* /friend add [-force] <Tox ID> [<message>] */
else if (argc >= 3 && weechat_strcasecmp(argv[1], "add") == 0) else if (argc >= 3 && weechat_strcasecmp(argv[1], "add") == 0)
{ {
bool force; bool force;
@ -325,16 +320,13 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
} }
TOX_ERR_FRIEND_ADD err; TOX_ERR_FRIEND_ADD err;
(void)tox_friend_add(profile->tox, (void)tox_friend_add(profile->tox, (uint8_t *)address,
(uint8_t *)address, (uint8_t *)message, strlen(message), &err);
(uint8_t *)message,
strlen(message), &err);
switch (err) switch (err)
{ {
case TOX_ERR_FRIEND_ADD_OK: case TOX_ERR_FRIEND_ADD_OK:
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%sFriend request sent!",
"%sFriend request sent!",
weechat_prefix("network")); weechat_prefix("network"));
break; break;
case TOX_ERR_FRIEND_ADD_TOO_LONG: case TOX_ERR_FRIEND_ADD_TOO_LONG:
@ -377,7 +369,7 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
// /friend remove /* /friend remove */
else if (argc >= 3 && (weechat_strcasecmp(argv[1], "remove") == 0)) else if (argc >= 3 && (weechat_strcasecmp(argv[1], "remove") == 0))
{ {
int32_t friend_number = twc_match_friend(profile, argv[2]); int32_t friend_number = twc_match_friend(profile, argv[2]);
@ -386,14 +378,12 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
char *name = twc_get_name_nt(profile->tox, friend_number); char *name = twc_get_name_nt(profile->tox, friend_number);
if (tox_friend_delete(profile->tox, friend_number, NULL)) if (tox_friend_delete(profile->tox, friend_number, NULL))
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%sRemoved %s from friend list.",
"%sRemoved %s from friend list.",
weechat_prefix("network"), name); weechat_prefix("network"), name);
} }
else else
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%sCould not remove friend!",
"%sCould not remove friend!",
weechat_prefix("error")); weechat_prefix("error"));
} }
@ -402,10 +392,9 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
// friend accept|decline <number>|all /* friend accept|decline <number>|all */
else if (argc == 3 && else if (argc == 3 && (weechat_strcasecmp(argv[1], "accept") == 0 ||
(weechat_strcasecmp(argv[1], "accept") == 0 weechat_strcasecmp(argv[1], "decline") == 0))
|| weechat_strcasecmp(argv[1], "decline") == 0))
{ {
int accept = weechat_strcasecmp(argv[1], "accept") == 0; int accept = weechat_strcasecmp(argv[1], "accept") == 0;
@ -415,7 +404,7 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
size_t index; size_t index;
size_t count = 0; size_t count = 0;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(profile->friend_requests, index, item) twc_list_foreach (profile->friend_requests, index, item)
{ {
if (accept) if (accept)
{ {
@ -427,11 +416,11 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
{ {
char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(item->friend_request->tox_id, twc_bin2hex(item->friend_request->tox_id,
TOX_PUBLIC_KEY_SIZE, TOX_PUBLIC_KEY_SIZE, hex_address);
hex_address); weechat_printf(
weechat_printf(profile->buffer, profile->buffer,
"%sCould not accept friend request from %s", "%sCould not accept friend request from %s",
weechat_prefix("error"), hex_address); weechat_prefix("error"), hex_address);
} }
} }
else else
@ -441,11 +430,9 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
} }
} }
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%s%s %d friend requests.",
"%s%s %d friend requests.",
weechat_prefix("network"), weechat_prefix("network"),
accept ? "Accepted" : "Declined", accept ? "Accepted" : "Declined", count);
count);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
@ -453,18 +440,16 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
{ {
char *endptr; char *endptr;
unsigned long num = strtoul(argv[2], &endptr, 10); unsigned long num = strtoul(argv[2], &endptr, 10);
if (endptr == argv[2] || (request = twc_friend_request_with_index(profile, num)) == NULL) if (endptr == argv[2] ||
(request = twc_friend_request_with_index(profile, num)) == NULL)
{ {
weechat_printf(profile->buffer, weechat_printf(profile->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_address[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(request->tox_id, twc_bin2hex(request->tox_id, TOX_PUBLIC_KEY_SIZE, hex_address);
TOX_PUBLIC_KEY_SIZE,
hex_address);
if (accept) if (accept)
{ {
@ -495,27 +480,24 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
} }
} }
// /friend requests /* /friend requests */
else if (argc == 2 && weechat_strcasecmp(argv[1], "requests") == 0) else if (argc == 2 && weechat_strcasecmp(argv[1], "requests") == 0)
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%sPending friend requests:",
"%sPending friend requests:",
weechat_prefix("network")); weechat_prefix("network"));
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(profile->friend_requests, index, item) twc_list_foreach (profile->friend_requests, index, item)
{ {
char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(item->friend_request->tox_id, twc_bin2hex(item->friend_request->tox_id, TOX_PUBLIC_KEY_SIZE,
TOX_PUBLIC_KEY_SIZE, hex_address); hex_address);
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%s[%d] Address: %s\n"
"%s[%d] Address: %s\n" "[%d] Message: %s",
"[%d] Message: %s", weechat_prefix("network"), index, hex_address, index,
weechat_prefix("network"), item->friend_request->message);
index, hex_address,
index, item->friend_request->message);
} }
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
@ -536,7 +518,7 @@ twc_cmd_group(const void *pointer, void *data, struct t_gui_buffer *buffer,
TWC_CHECK_PROFILE(profile); TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile); TWC_CHECK_PROFILE_LOADED(profile);
// /group create /* /group create */
if (argc == 2 && weechat_strcasecmp(argv[1], "create") == 0) if (argc == 2 && weechat_strcasecmp(argv[1], "create") == 0)
{ {
int rc = tox_conference_new(profile->tox, &err); int rc = tox_conference_new(profile->tox, &err);
@ -550,10 +532,9 @@ twc_cmd_group(const void *pointer, void *data, struct t_gui_buffer *buffer,
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
// /group join|decline <number> /* /group join|decline <number> */
else if (argc == 3 && else if (argc == 3 && (weechat_strcasecmp(argv[1], "join") == 0 ||
(weechat_strcasecmp(argv[1], "join") == 0 weechat_strcasecmp(argv[1], "decline") == 0))
|| weechat_strcasecmp(argv[1], "decline") == 0))
{ {
bool join = weechat_strcasecmp(argv[1], "join") == 0; bool join = weechat_strcasecmp(argv[1], "join") == 0;
@ -561,10 +542,10 @@ twc_cmd_group(const void *pointer, void *data, struct t_gui_buffer *buffer,
char *endptr; char *endptr;
unsigned long num = strtoul(argv[2], &endptr, 10); unsigned long num = strtoul(argv[2], &endptr, 10);
if (endptr == argv[2] || (invite = twc_group_chat_invite_with_index(profile, num)) == NULL) if (endptr == argv[2] ||
(invite = twc_group_chat_invite_with_index(profile, num)) == NULL)
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%sInvalid group chat invite ID.",
"%sInvalid group chat invite ID.",
weechat_prefix("error")); weechat_prefix("error"));
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
@ -573,7 +554,7 @@ twc_cmd_group(const void *pointer, void *data, struct t_gui_buffer *buffer,
{ {
int group_number = twc_group_chat_invite_join(invite); int group_number = twc_group_chat_invite_join(invite);
// create a buffer for the new group chat /* create a buffer for the new group chat */
if (group_number >= 0) if (group_number >= 0)
twc_chat_search_group(profile, group_number, true); twc_chat_search_group(profile, group_number, true);
else else
@ -589,23 +570,20 @@ twc_cmd_group(const void *pointer, void *data, struct t_gui_buffer *buffer,
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
// /group invites /* /group invites */
else if (argc == 2 && weechat_strcasecmp(argv[1], "invites") == 0) else if (argc == 2 && weechat_strcasecmp(argv[1], "invites") == 0)
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%sPending group chat invites:",
"%sPending group chat invites:",
weechat_prefix("network")); weechat_prefix("network"));
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(profile->group_chat_invites, index, item) twc_list_foreach (profile->group_chat_invites, index, item)
{ {
char *friend_name = char *friend_name = twc_get_name_nt(
twc_get_name_nt(profile->tox, item->group_chat_invite->friend_number); profile->tox, item->group_chat_invite->friend_number);
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%s[%d] From: %s",
"%s[%d] From: %s", weechat_prefix("network"), index, friend_name);
weechat_prefix("network"),
index, friend_name);
free(friend_name); free(friend_name);
} }
@ -634,14 +612,15 @@ twc_cmd_invite(const void *pointer, void *data, struct t_gui_buffer *buffer,
int32_t friend_number = twc_match_friend(chat->profile, argv_eol[1]); int32_t friend_number = twc_match_friend(chat->profile, argv_eol[1]);
TWC_CHECK_FRIEND_NUMBER(chat->profile, friend_number, argv_eol[1]); TWC_CHECK_FRIEND_NUMBER(chat->profile, friend_number, argv_eol[1]);
tox_conference_invite(chat->profile->tox, friend_number, chat->group_number, &err); tox_conference_invite(chat->profile->tox, friend_number, chat->group_number,
&err);
if (err == TOX_ERR_CONFERENCE_INVITE_OK) if (err == TOX_ERR_CONFERENCE_INVITE_OK)
{ {
char *friend_name = twc_get_name_nt(chat->profile->tox, friend_number); char *friend_name = twc_get_name_nt(chat->profile->tox, friend_number);
weechat_printf(chat->buffer, "%sInvited %s to the group chat.", weechat_printf(chat->buffer, "%sInvited %s to the group chat.",
weechat_prefix("network"), friend_name); weechat_prefix("network"), friend_name);
free(friend_name); free(friend_name);
} }
else else
{ {
@ -685,8 +664,8 @@ twc_cmd_msg(const void *pointer, void *data, struct t_gui_buffer *buffer,
TWC_CHECK_PROFILE(profile); TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile); TWC_CHECK_PROFILE_LOADED(profile);
// do a shell split in case a friend has spaces in his name and we need /* do a shell split in case a friend has spaces in his name and we need
// quotes * quotes */
int shell_argc; int shell_argc;
char **shell_argv = weechat_string_split_shell(argv_eol[0], &shell_argc); char **shell_argv = weechat_string_split_shell(argv_eol[0], &shell_argc);
@ -697,7 +676,7 @@ twc_cmd_msg(const void *pointer, void *data, struct t_gui_buffer *buffer,
char *message = NULL; char *message = NULL;
if (shell_argc >= 3) if (shell_argc >= 3)
{ {
// extract message, add two if quotes are used /* extract message, add two if quotes are used */
message = argv_eol[1] + strlen(recipient); message = argv_eol[1] + strlen(recipient);
if (*argv[1] == '"' || *argv[1] == '\'') if (*argv[1] == '"' || *argv[1] == '\'')
message += 2; message += 2;
@ -706,13 +685,13 @@ twc_cmd_msg(const void *pointer, void *data, struct t_gui_buffer *buffer,
int32_t friend_number = twc_match_friend(profile, recipient); int32_t friend_number = twc_match_friend(profile, recipient);
TWC_CHECK_FRIEND_NUMBER(profile, friend_number, recipient); TWC_CHECK_FRIEND_NUMBER(profile, friend_number, recipient);
// create chat buffer if it does not exist /* create chat buffer if it does not exist */
struct t_twc_chat *chat = twc_chat_search_friend(profile, friend_number, true); struct t_twc_chat *chat =
twc_chat_search_friend(profile, friend_number, true);
// send a message if provided /* send a message if provided */
if (message) if (message)
twc_chat_send_message(chat, twc_chat_send_message(chat, weechat_string_strip(message, 1, 1, " "),
weechat_string_strip(message, 1, 1, " "),
TOX_MESSAGE_TYPE_NORMAL); TOX_MESSAGE_TYPE_NORMAL);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
@ -735,10 +714,8 @@ twc_cmd_myid(const void *pointer, void *data, struct t_gui_buffer *buffer,
char address_str[TOX_ADDRESS_SIZE * 2 + 1]; char address_str[TOX_ADDRESS_SIZE * 2 + 1];
twc_bin2hex(address, TOX_ADDRESS_SIZE, address_str); twc_bin2hex(address, TOX_ADDRESS_SIZE, address_str);
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%sYour Tox address: %s",
"%sYour Tox address: %s", weechat_prefix("network"), address_str);
weechat_prefix("network"),
address_str);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
@ -776,28 +753,22 @@ twc_cmd_name(const void *pointer, void *data, struct t_gui_buffer *buffer,
err_msg = "unknown error"; err_msg = "unknown error";
break; break;
} }
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%s%s%s", weechat_prefix("error"),
"%s%s%s",
weechat_prefix("error"),
"Could not change name: ", err_msg); "Could not change name: ", err_msg);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
weechat_bar_item_update("input_prompt"); weechat_bar_item_update("input_prompt");
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%sYou are now known as %s",
"%sYou are now known as %s", weechat_prefix("network"), name);
weechat_prefix("network"),
name);
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(profile->chats, index, item) twc_list_foreach (profile->chats, index, item)
{ {
weechat_printf(item->chat->buffer, weechat_printf(item->chat->buffer, "%sYou are now known as %s",
"%sYou are now known as %s", weechat_prefix("network"), name);
weechat_prefix("network"),
name);
} }
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
@ -832,7 +803,7 @@ twc_cmd_nospam(const void *pointer, void *data, struct t_gui_buffer *buffer,
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
// reverse the value bytes so it's displayed as entered in the Tox ID /* reverse the value bytes so it's displayed as entered in the Tox ID */
new_nospam = twc_uint32_reverse_bytes(value); new_nospam = twc_uint32_reverse_bytes(value);
} }
else else
@ -846,7 +817,8 @@ twc_cmd_nospam(const void *pointer, void *data, struct t_gui_buffer *buffer,
weechat_printf(profile->buffer, weechat_printf(profile->buffer,
"%snew nospam has been set; this changes your Tox ID! To " "%snew nospam has been set; this changes your Tox ID! To "
"revert, run \"/nospam %x\"", "revert, run \"/nospam %x\"",
weechat_prefix("network"), twc_uint32_reverse_bytes(old_nospam)); weechat_prefix("network"),
twc_uint32_reverse_bytes(old_nospam));
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
@ -866,8 +838,7 @@ twc_cmd_part(const void *pointer, void *data, struct t_gui_buffer *buffer,
tox_conference_delete(chat->profile->tox, chat->group_number, &err); tox_conference_delete(chat->profile->tox, chat->group_number, &err);
if (err == TOX_ERR_CONFERENCE_DELETE_OK) if (err == TOX_ERR_CONFERENCE_DELETE_OK)
{ {
weechat_printf(chat->buffer, weechat_printf(chat->buffer, "%sYou have left the group chat",
"%sYou have left the group chat",
weechat_prefix("network")); weechat_prefix("network"));
} }
else else
@ -890,27 +861,26 @@ twc_cmd_part(const void *pointer, void *data, struct t_gui_buffer *buffer,
* Save Tox profile data when /save is executed. * Save Tox profile data when /save is executed.
*/ */
int int
twc_cmd_save(const void *pointer, void *data, struct t_gui_buffer *buffer, const char *command) twc_cmd_save(const void *pointer, void *data, struct t_gui_buffer *buffer,
const char *command)
{ {
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(twc_profiles, index, item) twc_list_foreach (twc_profiles, index, item)
{ {
if (!(item->profile->tox)) continue; if (!(item->profile->tox))
continue;
int rc = twc_profile_save_data_file(item->profile); int rc = twc_profile_save_data_file(item->profile);
if (rc == -1) if (rc == -1)
{ {
weechat_printf(NULL, weechat_printf(NULL, "%s%s: failed to save data for profile %s",
"%s%s: failed to save data for profile %s",
weechat_prefix("error"), weechat_plugin->name, weechat_prefix("error"), weechat_plugin->name,
item->profile->name); item->profile->name);
} }
} }
weechat_printf(NULL, weechat_printf(NULL, "%s: profile data saved", weechat_plugin->name);
"%s: profile data saved",
weechat_plugin->name);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
@ -959,8 +929,7 @@ twc_cmd_statusmsg(const void *pointer, void *data, struct t_gui_buffer *buffer,
char *message = argc > 1 ? argv_eol[1] : " "; char *message = argc > 1 ? argv_eol[1] : " ";
TOX_ERR_SET_INFO err; TOX_ERR_SET_INFO err;
tox_self_set_status_message(profile->tox, tox_self_set_status_message(profile->tox, (uint8_t *)message,
(uint8_t *)message,
strlen(message), &err); strlen(message), &err);
if (err != TOX_ERR_SET_INFO_OK) if (err != TOX_ERR_SET_INFO_OK)
{ {
@ -977,9 +946,7 @@ twc_cmd_statusmsg(const void *pointer, void *data, struct t_gui_buffer *buffer,
err_msg = "unknown error"; err_msg = "unknown error";
break; break;
} }
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%s%s%s", weechat_prefix("error"),
"%s%s%s",
weechat_prefix("error"),
"Could not set status message: ", err_msg); "Could not set status message: ", err_msg);
} }
@ -1014,11 +981,11 @@ twc_cmd_topic(const void *pointer, void *data, struct t_gui_buffer *buffer,
char *topic = argv_eol[1]; char *topic = argv_eol[1];
tox_conference_set_title(chat->profile->tox, chat->group_number, tox_conference_set_title(chat->profile->tox, chat->group_number,
(uint8_t *)topic, strlen(topic), &err); (uint8_t *)topic, strlen(topic), &err);
if (err != TOX_ERR_CONFERENCE_TITLE_OK) if (err != TOX_ERR_CONFERENCE_TITLE_OK)
{ {
weechat_printf(chat->buffer, "%s%s", weechat_printf(chat->buffer, "%s%s", weechat_prefix("error"),
weechat_prefix("error"), "Could not set topic."); "Could not set topic.");
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
@ -1032,53 +999,46 @@ twc_cmd_topic(const void *pointer, void *data, struct t_gui_buffer *buffer,
*/ */
int int
twc_cmd_tox(const void *pointer, void *data, struct t_gui_buffer *buffer, twc_cmd_tox(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol) int argc, char **argv, char **argv_eol)
{ {
// /tox [list] /* /tox [list] */
if (argc == 1 || (argc == 2 && weechat_strcasecmp(argv[1], "list") == 0)) if (argc == 1 || (argc == 2 && weechat_strcasecmp(argv[1], "list") == 0))
{ {
weechat_printf(NULL, weechat_printf(NULL, "%sAll Tox profiles:", weechat_prefix("network"));
"%sAll Tox profiles:",
weechat_prefix("network"));
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(twc_profiles, index, item) twc_list_foreach (twc_profiles, index, item)
{ {
weechat_printf(NULL, weechat_printf(NULL, "%s%s", weechat_prefix("network"),
"%s%s",
weechat_prefix("network"),
item->profile->name); item->profile->name);
} }
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
// /tox create /* /tox create */
else if (argc == 3 && (weechat_strcasecmp(argv[1], "create") == 0)) else if (argc == 3 && (weechat_strcasecmp(argv[1], "create") == 0))
{ {
char *name = argv[2]; char *name = argv[2];
if (twc_profile_search_name(name)) if (twc_profile_search_name(name))
{ {
weechat_printf(NULL, weechat_printf(NULL, "%s%s: profile \"%s\" already exists!",
"%s%s: profile \"%s\" already exists!", weechat_prefix("error"), weechat_plugin->name, name);
weechat_prefix("error"), weechat_plugin->name,
name);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
struct t_twc_profile *profile = twc_profile_new(name); struct t_twc_profile *profile = twc_profile_new(name);
weechat_printf(NULL, weechat_printf(NULL, "%s%s: profile \"%s\" created!",
"%s%s: profile \"%s\" created!",
weechat_prefix("network"), weechat_plugin->name, weechat_prefix("network"), weechat_plugin->name,
profile->name); profile->name);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
// /tox delete /* /tox delete */
else if ((argc == 3 || argc == 4) else if ((argc == 3 || argc == 4) &&
&& (weechat_strcasecmp(argv[1], "delete") == 0)) (weechat_strcasecmp(argv[1], "delete") == 0))
{ {
char *name = argv[2]; char *name = argv[2];
char *flag = argv[3]; char *flag = argv[3];
@ -1096,25 +1056,22 @@ twc_cmd_tox(const void *pointer, void *data, struct t_gui_buffer *buffer,
} }
else else
{ {
weechat_printf(NULL, weechat_printf(NULL, "%s%s: You must confirm deletion with either "
"%s%s: You must confirm deletion with either " "\"-keepdata\" or \"-yes\" (see /help tox)",
"\"-keepdata\" or \"-yes\" (see /help tox)",
weechat_prefix("error"), weechat_plugin->name); weechat_prefix("error"), weechat_plugin->name);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
weechat_printf(NULL, weechat_printf(NULL, "%s%s: profile \"%s\" has been deleted.",
"%s%s: profile \"%s\" has been deleted.", weechat_prefix("error"), weechat_plugin->name, name);
weechat_prefix("error"), weechat_plugin->name,
name);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }
// /tox load|unload|reload [<profile>...] /* /tox load|unload|reload [<profile>...] */
else if (argc >= 2 && (weechat_strcasecmp(argv[1], "load") == 0 else if (argc >= 2 && (weechat_strcasecmp(argv[1], "load") == 0 ||
|| weechat_strcasecmp(argv[1], "unload") == 0 weechat_strcasecmp(argv[1], "unload") == 0 ||
|| weechat_strcasecmp(argv[1], "reload") == 0)) weechat_strcasecmp(argv[1], "reload") == 0))
{ {
bool load = weechat_strcasecmp(argv[1], "load") == 0; bool load = weechat_strcasecmp(argv[1], "load") == 0;
bool unload = weechat_strcasecmp(argv[1], "unload") == 0; bool unload = weechat_strcasecmp(argv[1], "unload") == 0;
@ -1158,16 +1115,14 @@ twc_cmd_tox(const void *pointer, void *data, struct t_gui_buffer *buffer,
void void
twc_commands_init() twc_commands_init()
{ {
weechat_hook_command("bootstrap", weechat_hook_command("bootstrap", "manage bootstrap nodes",
"manage bootstrap nodes",
"connect <address> <port> <Tox ID>", "connect <address> <port> <Tox ID>",
"address: internet address of node to bootstrap with\n" "address: internet address of node to bootstrap with\n"
" port: port of the node\n" " port: port of the node\n"
" Tox ID: Tox ID of the node", " Tox ID: Tox ID of the node",
"connect", twc_cmd_bootstrap, NULL, NULL); "connect", twc_cmd_bootstrap, NULL, NULL);
weechat_hook_command("friend", weechat_hook_command("friend", "manage friends",
"manage friends",
"list" "list"
" || add [-force] <address> [<message>]" " || add [-force] <address> [<message>]"
" || remove <number>|<name>|<Tox ID>" " || remove <number>|<name>|<Tox ID>"
@ -1187,12 +1142,10 @@ twc_commands_init()
" || decline", " || decline",
twc_cmd_friend, NULL, NULL); twc_cmd_friend, NULL, NULL);
weechat_hook_command("group", weechat_hook_command("group", "manage group chats", "create"
"manage group chats", " || invites"
"create" " || join <number>"
" || invites" " || decline <number>",
" || join <number>"
" || decline <number>",
" create: create a new group chat\n" " create: create a new group chat\n"
"invites: list group chat invites\n" "invites: list group chat invites\n"
" join: join a group chat by its invite ID\n" " join: join a group chat by its invite ID\n"
@ -1202,94 +1155,71 @@ twc_commands_init()
" || join", " || join",
twc_cmd_group, NULL, NULL); twc_cmd_group, NULL, NULL);
weechat_hook_command("invite", weechat_hook_command(
"invite someone to a group chat", "invite", "invite someone to a group chat", "<number>|<name>|<Tox ID>",
"<number>|<name>|<Tox ID>", "number, name, Tox ID: friend to message\n",
"number, name, Tox ID: friend to message\n", "%(tox_friend_name)|%(tox_friend_tox_id)", twc_cmd_invite, NULL, NULL);
"%(tox_friend_name)|%(tox_friend_tox_id)",
twc_cmd_invite, NULL, NULL);
weechat_hook_command("me", weechat_hook_command("me", "send an action to the current chat",
"send an action to the current chat", "<message>", "message: message to send", NULL,
"<message>", twc_cmd_me, NULL, NULL);
"message: message to send",
NULL, twc_cmd_me, NULL, NULL);
weechat_hook_command("msg", weechat_hook_command("msg", "send a message to a Tox friend",
"send a message to a Tox friend",
"<number>|<name>|<Tox ID> [<message>]", "<number>|<name>|<Tox ID> [<message>]",
"number, name, Tox ID: friend to message\n" "number, name, Tox ID: friend to message\n"
"message: message to send", "message: message to send",
"%(tox_friend_name)|%(tox_friend_tox_id)", "%(tox_friend_name)|%(tox_friend_tox_id)", twc_cmd_msg,
twc_cmd_msg, NULL, NULL); NULL, NULL);
weechat_hook_command("myid", weechat_hook_command("myid", "get your Tox ID to give to friends", "", "",
"get your Tox ID to give to friends",
"", "",
NULL, twc_cmd_myid, NULL, NULL); NULL, twc_cmd_myid, NULL, NULL);
weechat_hook_command("name", weechat_hook_command("name", "change your Tox name", "<name>",
"change your Tox name", "name: your new name", NULL, twc_cmd_name, NULL, NULL);
"<name>",
"name: your new name",
NULL, twc_cmd_name, NULL, NULL);
weechat_hook_command("nospam", weechat_hook_command("nospam", "change nospam value", "[<hex value>]",
"change nospam value",
"[<hex value>]",
"hex value: new nospam value; when omitted, a random " "hex value: new nospam value; when omitted, a random "
"new value is used\n\n" "new value is used\n\n"
"Warning: changing your nospam value will alter your " "Warning: changing your nospam value will alter your "
"Tox ID!", "Tox ID!",
NULL, twc_cmd_nospam, NULL, NULL); NULL, twc_cmd_nospam, NULL, NULL);
weechat_hook_command("part", weechat_hook_command("part", "leave a group chat", "", "", NULL,
"leave a group chat", twc_cmd_part, NULL, NULL);
"", "",
NULL, twc_cmd_part, NULL, NULL);
weechat_hook_command_run("/save", twc_cmd_save, NULL, NULL); weechat_hook_command_run("/save", twc_cmd_save, NULL, NULL);
weechat_hook_command("status", weechat_hook_command("status", "change your Tox status", "online|busy|away",
"change your Tox status", "", NULL, twc_cmd_status, NULL, NULL);
"online|busy|away",
"",
NULL, twc_cmd_status, NULL, NULL);
weechat_hook_command("statusmsg", weechat_hook_command("statusmsg", "change your Tox status message",
"change your Tox status message", "[<message>]", "message: your new status message",
"[<message>]",
"message: your new status message",
NULL, twc_cmd_statusmsg, NULL, NULL); NULL, twc_cmd_statusmsg, NULL, NULL);
weechat_hook_command("topic", weechat_hook_command("topic", "set a group chat topic", "<topic>",
"set a group chat topic", "topic: new group chat topic", NULL, twc_cmd_topic,
"<topic>", NULL, NULL);
"topic: new group chat topic",
NULL, twc_cmd_topic, NULL, NULL);
weechat_hook_command("tox", weechat_hook_command(
"manage Tox profiles", "tox", "manage Tox profiles", "list"
"list" " || create <name>"
" || create <name>" " || delete <name> -yes|-keepdata"
" || delete <name> -yes|-keepdata" " || load [<name>...]"
" || load [<name>...]" " || unload [<name>...]"
" || unload [<name>...]" " || reload [<name>...]",
" || reload [<name>...]", " list: list all Tox profile\n"
" list: list all Tox profile\n" "create: create a new Tox profile\n"
"create: create a new Tox profile\n" "delete: delete a Tox profile; requires either -yes "
"delete: delete a Tox profile; requires either -yes " "to confirm deletion or -keepdata to delete the "
"to confirm deletion or -keepdata to delete the " "profile but keep the Tox data file\n"
"profile but keep the Tox data file\n" " load: load one or more Tox profiles and connect to the network\n"
" load: load one or more Tox profiles and connect to the network\n" "unload: unload one or more Tox profiles\n"
"unload: unload one or more Tox profiles\n" "reload: reload one or more Tox profiles\n",
"reload: reload one or more Tox profiles\n", "list"
"list" " || create"
" || create" " || delete %(tox_profiles) -yes|-keepdata"
" || delete %(tox_profiles) -yes|-keepdata" " || load %(tox_unloaded_profiles)|%*"
" || load %(tox_unloaded_profiles)|%*" " || unload %(tox_loaded_profiles)|%*"
" || unload %(tox_loaded_profiles)|%*" " || reload %(tox_loaded_profiles)|%*",
" || reload %(tox_loaded_profiles)|%*", twc_cmd_tox, NULL, NULL);
twc_cmd_tox, NULL, NULL);
} }

View File

@ -20,7 +20,7 @@
#ifndef TOX_WEECHAT_COMMANDS_H #ifndef TOX_WEECHAT_COMMANDS_H
#define TOX_WEECHAT_COMMANDS_H #define TOX_WEECHAT_COMMANDS_H
void twc_commands_init(); void
twc_commands_init();
#endif // TOX_WEECHAT_COMMANDS_H
#endif /* TOX_WEECHAT_COMMANDS_H */

View File

@ -17,15 +17,15 @@
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>. * along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <weechat/weechat-plugin.h> #include <weechat/weechat-plugin.h>
#include "twc.h"
#include "twc-list.h" #include "twc-list.h"
#include "twc-profile.h" #include "twc-profile.h"
#include "twc-utils.h" #include "twc-utils.h"
#include "twc.h"
#include "twc-completion.h" #include "twc-completion.h"
@ -47,8 +47,7 @@ enum
*/ */
int int
twc_completion_friend(const void *pointer, void *data, twc_completion_friend(const void *pointer, void *data,
const char *completion_item, const char *completion_item, struct t_gui_buffer *buffer,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion) struct t_gui_completion *completion)
{ {
int flags = (int)(intptr_t)pointer; int flags = (int)(intptr_t)pointer;
@ -69,7 +68,8 @@ twc_completion_friend(const void *pointer, void *data,
char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1];
TOX_ERR_FRIEND_GET_PUBLIC_KEY err; TOX_ERR_FRIEND_GET_PUBLIC_KEY err;
tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, &err); tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id,
&err);
if (err == TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK) if (err == TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK)
{ {
twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id); twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id);
@ -83,7 +83,7 @@ twc_completion_friend(const void *pointer, void *data,
{ {
char *name = twc_get_name_nt(profile->tox, friend_numbers[i]); char *name = twc_get_name_nt(profile->tox, friend_numbers[i]);
// add quotes if needed /* add quotes if needed */
if (strchr(name, ' ')) if (strchr(name, ' '))
{ {
size_t length = strlen(name) + 3; size_t length = strlen(name) + 3;
@ -109,23 +109,21 @@ twc_completion_friend(const void *pointer, void *data,
*/ */
int int
twc_completion_profile(const void *pointer, void *data, twc_completion_profile(const void *pointer, void *data,
const char *completion_item, const char *completion_item, struct t_gui_buffer *buffer,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion) struct t_gui_completion *completion)
{ {
int flag = (int)(intptr_t)pointer; int flag = (int)(intptr_t)pointer;
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(twc_profiles, index, item) twc_list_foreach (twc_profiles, index, item)
{ {
if (flag == TWC_ALL_PROFILES if (flag == TWC_ALL_PROFILES ||
|| (flag == TWC_LOADED_PROFILES && item->profile->tox != NULL) (flag == TWC_LOADED_PROFILES && item->profile->tox != NULL) ||
|| (flag == TWC_UNLOADED_PROFILES && item->profile->tox == NULL)) (flag == TWC_UNLOADED_PROFILES && item->profile->tox == NULL))
{ {
weechat_hook_completion_list_add(completion, weechat_hook_completion_list_add(completion, item->profile->name, 0,
item->profile->name, WEECHAT_LIST_POS_SORT);
0, WEECHAT_LIST_POS_SORT);
} }
} }
@ -135,8 +133,7 @@ twc_completion_profile(const void *pointer, void *data,
void void
twc_completion_init() twc_completion_init()
{ {
weechat_hook_completion("tox_profiles", "profile", weechat_hook_completion("tox_profiles", "profile", twc_completion_profile,
twc_completion_profile,
(void *)(intptr_t)TWC_ALL_PROFILES, NULL); (void *)(intptr_t)TWC_ALL_PROFILES, NULL);
weechat_hook_completion("tox_loaded_profiles", "loaded profile", weechat_hook_completion("tox_loaded_profiles", "loaded profile",
twc_completion_profile, twc_completion_profile,
@ -151,4 +148,3 @@ twc_completion_init()
twc_completion_friend, twc_completion_friend,
(void *)(intptr_t)TWC_COMPLETE_FRIEND_NAME, NULL); (void *)(intptr_t)TWC_COMPLETE_FRIEND_NAME, NULL);
} }

View File

@ -23,5 +23,4 @@
void void
twc_completion_init(); twc_completion_init();
#endif // TOX_WEECHAT_COMPLETION_H #endif /* TOX_WEECHAT_COMPLETION_H */

View File

@ -17,16 +17,16 @@
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>. * along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <weechat/weechat-plugin.h> #include <weechat/weechat-plugin.h>
#include "twc.h"
#include "twc-list.h" #include "twc-list.h"
#include "twc-profile.h" #include "twc-profile.h"
#include "twc.h"
#include "twc-config.h" #include "twc-config.h"
@ -38,19 +38,10 @@ struct t_config_section *twc_config_section_profile_default = NULL;
struct t_config_option *twc_config_friend_request_message; struct t_config_option *twc_config_friend_request_message;
struct t_config_option *twc_config_short_id_size; struct t_config_option *twc_config_short_id_size;
char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] = char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] = {
{ "save_file", "autoload", "autojoin", "max_friend_requests",
"save_file", "proxy_address", "proxy_port", "proxy_type", "udp",
"autoload", "ipv6", "passphrase", "logging",
"autojoin",
"max_friend_requests",
"proxy_address",
"proxy_port",
"proxy_type",
"udp",
"ipv6",
"passphrase",
"logging",
}; };
/** /**
@ -75,8 +66,7 @@ int
twc_config_profile_read_callback(const void *pointer, void *data, twc_config_profile_read_callback(const void *pointer, void *data,
struct t_config_file *config_file, struct t_config_file *config_file,
struct t_config_section *section, struct t_config_section *section,
const char *option_name, const char *option_name, const char *value)
const char *value)
{ {
int rc = WEECHAT_CONFIG_OPTION_SET_ERROR; int rc = WEECHAT_CONFIG_OPTION_SET_ERROR;
@ -85,12 +75,13 @@ twc_config_profile_read_callback(const void *pointer, void *data,
char *dot_pos = strrchr(option_name, '.'); char *dot_pos = strrchr(option_name, '.');
if (dot_pos) if (dot_pos)
{ {
char *profile_name = weechat_strndup(option_name, char *profile_name =
dot_pos-option_name); weechat_strndup(option_name, dot_pos - option_name);
char *option_name = dot_pos + 1; char *option_name = dot_pos + 1;
if (profile_name) if (profile_name)
{ {
int option_index = twc_config_profile_option_search(option_name); int option_index =
twc_config_profile_option_search(option_name);
if (option_index >= 0) if (option_index >= 0)
{ {
struct t_twc_profile *profile = struct t_twc_profile *profile =
@ -101,16 +92,15 @@ twc_config_profile_read_callback(const void *pointer, void *data,
if (profile) if (profile)
{ {
rc = weechat_config_option_set(profile->options[option_index], rc = weechat_config_option_set(
value, 1); profile->options[option_index], value, 1);
} }
else else
{ {
weechat_printf(NULL, weechat_printf(NULL,
"%s%s: error creating profile \"%s\"", "%s%s: error creating profile \"%s\"",
weechat_prefix("error"), weechat_prefix("error"),
weechat_plugin->name, weechat_plugin->name, profile_name);
profile_name);
} }
} }
@ -121,8 +111,7 @@ twc_config_profile_read_callback(const void *pointer, void *data,
if (rc == WEECHAT_CONFIG_OPTION_SET_ERROR) if (rc == WEECHAT_CONFIG_OPTION_SET_ERROR)
{ {
weechat_printf(NULL, weechat_printf(NULL, "%s%s: error creating profile option \"%s\"",
"%s%s: error creating profile option \"%s\"",
weechat_prefix("error"), weechat_plugin->name, weechat_prefix("error"), weechat_plugin->name,
option_name); option_name);
} }
@ -140,7 +129,7 @@ twc_config_check_value_callback(const void *pointer, void *data,
{ {
int int_value = atoi(value); int int_value = atoi(value);
// must be multiple of two /* must be multiple of two */
if (option == twc_config_short_id_size && int_value % 2) if (option == twc_config_short_id_size && int_value % 2)
return 0; return 0;
@ -190,11 +179,10 @@ twc_config_profile_change_callback(const void *pointer, void *data,
/* option changed for default profile, update all profiles */ /* option changed for default profile, update all profiles */
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(twc_profiles, index, item) twc_list_foreach (twc_profiles, index, item)
{ {
bool logging_enabled bool logging_enabled = TWC_PROFILE_OPTION_BOOLEAN(
= TWC_PROFILE_OPTION_BOOLEAN(item->profile, item->profile, TWC_PROFILE_OPTION_LOGGING);
TWC_PROFILE_OPTION_LOGGING);
twc_profile_set_logging(item->profile, logging_enabled); twc_profile_set_logging(item->profile, logging_enabled);
} }
} }
@ -202,7 +190,6 @@ twc_config_profile_change_callback(const void *pointer, void *data,
default: default:
break; break;
} }
} }
/** /**
@ -210,9 +197,8 @@ twc_config_profile_change_callback(const void *pointer, void *data,
*/ */
struct t_config_option * struct t_config_option *
twc_config_init_option(struct t_twc_profile *profile, twc_config_init_option(struct t_twc_profile *profile,
struct t_config_section *section, struct t_config_section *section, int option_index,
int option_index, const char *option_name, const char *option_name, bool is_default_profile)
bool is_default_profile)
{ {
char *type; char *type;
char *description; char *description;
@ -251,7 +237,8 @@ twc_config_init_option(struct t_twc_profile *profile,
type = "integer"; type = "integer";
description = "maximum amount of friend requests to retain before " description = "maximum amount of friend requests to retain before "
"ignoring new ones"; "ignoring new ones";
min = 0; max = INT_MAX; min = 0;
max = INT_MAX;
default_value = "100"; default_value = "100";
break; break;
case TWC_PROFILE_OPTION_PASSPHRASE: case TWC_PROFILE_OPTION_PASSPHRASE:
@ -267,14 +254,16 @@ twc_config_init_option(struct t_twc_profile *profile,
case TWC_PROFILE_OPTION_PROXY_PORT: case TWC_PROFILE_OPTION_PROXY_PORT:
type = "integer"; type = "integer";
description = "proxy port"; description = "proxy port";
min = 0; max = UINT16_MAX; min = 0;
max = UINT16_MAX;
null_allowed = true; null_allowed = true;
break; break;
case TWC_PROFILE_OPTION_PROXY_TYPE: case TWC_PROFILE_OPTION_PROXY_TYPE:
type = "integer"; type = "integer";
description = "proxy type; requires profile reload to take effect"; description = "proxy type; requires profile reload to take effect";
string_values = "none|socks5|http"; string_values = "none|socks5|http";
min = 0; max = 0; min = 0;
max = 0;
default_value = "none"; default_value = "none";
break; break;
case TWC_PROFILE_OPTION_SAVEFILE: case TWC_PROFILE_OPTION_SAVEFILE:
@ -308,12 +297,11 @@ twc_config_init_option(struct t_twc_profile *profile,
*index_change_pointer = option_index; *index_change_pointer = option_index;
return weechat_config_new_option( return weechat_config_new_option(
twc_config_file, section, twc_config_file, section, option_name, type, description, string_values,
option_name, type, description, string_values, min, max, min, max, default_value, value, null_allowed,
default_value, value, null_allowed,
twc_config_profile_check_value_callback, profile, index_check_pointer, twc_config_profile_check_value_callback, profile, index_check_pointer,
twc_config_profile_change_callback, profile, index_change_pointer, twc_config_profile_change_callback, profile, index_change_pointer, NULL,
NULL, NULL, NULL); NULL, NULL);
} }
/** /**
@ -324,57 +312,39 @@ twc_config_init()
{ {
twc_config_file = weechat_config_new("tox", NULL, NULL, NULL); twc_config_file = weechat_config_new("tox", NULL, NULL, NULL);
twc_config_section_profile = twc_config_section_profile = weechat_config_new_section(
weechat_config_new_section(twc_config_file, "profile", twc_config_file, "profile", 0, 0, twc_config_profile_read_callback,
0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
twc_config_profile_read_callback, NULL, NULL);
NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
twc_config_section_profile_default = twc_config_section_profile_default = weechat_config_new_section(
weechat_config_new_section(twc_config_file, "profile_default", twc_config_file, "profile_default", 0, 0, NULL, NULL, NULL, NULL, NULL,
0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
for (int i = 0; i < TWC_PROFILE_NUM_OPTIONS; ++i) for (int i = 0; i < TWC_PROFILE_NUM_OPTIONS; ++i)
{ {
twc_config_profile_default[i] = twc_config_profile_default[i] =
twc_config_init_option(NULL, twc_config_section_profile_default, twc_config_init_option(NULL, twc_config_section_profile_default, i,
i, twc_profile_option_names[i], true); twc_profile_option_names[i], true);
} }
twc_config_section_look = twc_config_section_look = weechat_config_new_section(
weechat_config_new_section(twc_config_file, "look", twc_config_file, "look", 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
twc_config_friend_request_message = weechat_config_new_option( twc_config_friend_request_message = weechat_config_new_option(
twc_config_file, twc_config_section_look, twc_config_file, twc_config_section_look, "friend_request_message",
"friend_request_message", "string", "string",
"message sent with friend requests if no other message is specified", "message sent with friend requests if no other message is specified",
NULL, 0, 0, NULL, 0, 0, "Hi! Please add me on Tox!", NULL, 0,
"Hi! Please add me on Tox!", NULL, 0, twc_config_check_value_callback, NULL, NULL, NULL, NULL, NULL, NULL,
twc_config_check_value_callback, NULL, NULL, NULL, NULL);
NULL, NULL, NULL, NULL, NULL, NULL);
twc_config_short_id_size = weechat_config_new_option( twc_config_short_id_size = weechat_config_new_option(
twc_config_file, twc_config_section_look, twc_config_file, twc_config_section_look, "short_id_size", "integer",
"short_id_size", "integer",
"length of Tox IDs shown in short format; must be a multiple of two", "length of Tox IDs shown in short format; must be a multiple of two",
NULL, 2, TOX_PUBLIC_KEY_SIZE * 2, NULL, 2, TOX_PUBLIC_KEY_SIZE * 2, "8", NULL, 0,
"8", NULL, 0, twc_config_check_value_callback, NULL, NULL, NULL, NULL, NULL, NULL,
twc_config_check_value_callback, NULL, NULL, NULL, NULL);
NULL, NULL, NULL, NULL, NULL, NULL);
} }
/** /**
@ -385,19 +355,18 @@ twc_config_init_profile(struct t_twc_profile *profile)
{ {
for (int i = 0; i < TWC_PROFILE_NUM_OPTIONS; ++i) for (int i = 0; i < TWC_PROFILE_NUM_OPTIONS; ++i)
{ {
// length: name + . + option + \0 /* length: name + . + option + \0 */
size_t length = strlen(profile->name) + 1 size_t length =
+ strlen(twc_profile_option_names[i]) + 1; strlen(profile->name) + 1 + strlen(twc_profile_option_names[i]) + 1;
char *option_name = malloc(sizeof(*option_name) * length); char *option_name = malloc(sizeof(*option_name) * length);
if (option_name) if (option_name)
{ {
snprintf(option_name, length, "%s.%s", snprintf(option_name, length, "%s.%s", profile->name,
profile->name,
twc_profile_option_names[i]); twc_profile_option_names[i]);
profile->options[i] = twc_config_init_option(profile, profile->options[i] = twc_config_init_option(
twc_config_section_profile, i, option_name, false); profile, twc_config_section_profile, i, option_name, false);
free(option_name); free(option_name);
} }
} }
@ -420,4 +389,3 @@ twc_config_write()
{ {
return weechat_config_write(twc_config_file); return weechat_config_write(twc_config_file);
} }

View File

@ -44,5 +44,4 @@ twc_config_write();
void void
twc_config_init_profile(struct t_twc_profile *profile); twc_config_init_profile(struct t_twc_profile *profile);
#endif // TOX_WEECHAT_CONFIG_H #endif /* TOX_WEECHAT_CONFIG_H */

View File

@ -19,13 +19,13 @@
#include <string.h> #include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#include "twc.h"
#include "twc-list.h" #include "twc-list.h"
#include "twc-profile.h" #include "twc-profile.h"
#include "twc-utils.h" #include "twc-utils.h"
#include "twc.h"
#include "twc-friend-request.h" #include "twc-friend-request.h"
@ -36,18 +36,17 @@
* other error. * other error.
*/ */
int int
twc_friend_request_add(struct t_twc_profile *profile, twc_friend_request_add(struct t_twc_profile *profile, const uint8_t *client_id,
const uint8_t *client_id,
const char *message) const char *message)
{ {
size_t max_request_count = size_t max_request_count = TWC_PROFILE_OPTION_INTEGER(
TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS); profile, TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS);
if (profile->friend_requests->count >= max_request_count) if (profile->friend_requests->count >= max_request_count)
return -1; return -1;
// create a new request /* create a new request */
struct t_twc_friend_request *request struct t_twc_friend_request *request =
= malloc(sizeof(struct t_twc_friend_request)); malloc(sizeof(struct t_twc_friend_request));
if (!request) if (!request)
return -2; return -2;
@ -114,4 +113,3 @@ twc_friend_request_free_list(struct t_twc_list *list)
free(list); free(list);
} }

View File

@ -36,8 +36,7 @@ struct t_twc_friend_request
}; };
int int
twc_friend_request_add(struct t_twc_profile *profile, twc_friend_request_add(struct t_twc_profile *profile, const uint8_t *client_id,
const uint8_t *client_id,
const char *message); const char *message);
bool bool
@ -47,8 +46,7 @@ void
twc_friend_request_remove(struct t_twc_friend_request *request); twc_friend_request_remove(struct t_twc_friend_request *request);
struct t_twc_friend_request * struct t_twc_friend_request *
twc_friend_request_with_index(struct t_twc_profile *profile, twc_friend_request_with_index(struct t_twc_profile *profile, int64_t index);
int64_t index);
void void
twc_friend_request_free(struct t_twc_friend_request *request); twc_friend_request_free(struct t_twc_friend_request *request);
@ -56,5 +54,4 @@ twc_friend_request_free(struct t_twc_friend_request *request);
void void
twc_friend_request_free_list(struct t_twc_list *list); twc_friend_request_free_list(struct t_twc_list *list);
#endif // TOX_WEECHAT_FRIEND_REQUEST_H #endif /* TOX_WEECHAT_FRIEND_REQUEST_H */

View File

@ -19,17 +19,17 @@
#include <string.h> #include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#ifdef TOXAV_ENABLED #ifdef TOXAV_ENABLED
#include <tox/toxav.h> #include <tox/toxav.h>
#endif // TOXAV_ENABLED #endif /* TOXAV_ENABLED */
#include "twc.h"
#include "twc-list.h" #include "twc-list.h"
#include "twc-profile.h" #include "twc-profile.h"
#include "twc-utils.h" #include "twc-utils.h"
#include "twc.h"
#include "twc-group-invite.h" #include "twc-group-invite.h"
@ -39,13 +39,12 @@
* Returns the index of the invite on success, -1 on error. * Returns the index of the invite on success, -1 on error.
*/ */
int int
twc_group_chat_invite_add(struct t_twc_profile *profile, twc_group_chat_invite_add(struct t_twc_profile *profile, int32_t friend_number,
int32_t friend_number, uint8_t group_chat_type, uint8_t group_chat_type, uint8_t *data, size_t size)
uint8_t *data, size_t size)
{ {
// create a new invite object /* create a new invite object */
struct t_twc_group_chat_invite *invite struct t_twc_group_chat_invite *invite =
= malloc(sizeof(struct t_twc_group_chat_invite)); malloc(sizeof(struct t_twc_group_chat_invite));
if (!invite) if (!invite)
return -1; return -1;
@ -75,15 +74,15 @@ twc_group_chat_invite_join(struct t_twc_group_chat_invite *invite)
switch (invite->group_chat_type) switch (invite->group_chat_type)
{ {
case TOX_CONFERENCE_TYPE_TEXT: case TOX_CONFERENCE_TYPE_TEXT:
rc = tox_conference_join(invite->profile->tox, invite->friend_number, rc =
invite->data, invite->data_size, &err); tox_conference_join(invite->profile->tox, invite->friend_number,
invite->data, invite->data_size, &err);
break; break;
#ifdef TOXAV_ENABLED #ifdef TOXAV_ENABLED
case TOX_CONFERENCE_TYPE_AV: case TOX_CONFERENCE_TYPE_AV:
rc = toxav_join_av_groupchat(invite->profile->tox, rc = toxav_join_av_groupchat(invite->profile->tox,
invite->friend_number, invite->friend_number, invite->data,
invite->data, invite->data_size, invite->data_size, NULL, NULL);
NULL, NULL);
break; break;
#endif #endif
default: default:
@ -112,8 +111,7 @@ twc_group_chat_invite_remove(struct t_twc_group_chat_invite *invite)
* Get group chat invite with a given index. * Get group chat invite with a given index.
*/ */
struct t_twc_group_chat_invite * struct t_twc_group_chat_invite *
twc_group_chat_invite_with_index(struct t_twc_profile *profile, twc_group_chat_invite_with_index(struct t_twc_profile *profile, size_t index)
size_t index)
{ {
struct t_twc_list_item *item = struct t_twc_list_item *item =
twc_list_get(profile->group_chat_invites, index); twc_list_get(profile->group_chat_invites, index);
@ -145,4 +143,3 @@ twc_group_chat_invite_free_list(struct t_twc_list *list)
free(list); free(list);
} }

View File

@ -40,9 +40,8 @@ struct t_twc_group_chat_invite
}; };
int int
twc_group_chat_invite_add(struct t_twc_profile *profile, twc_group_chat_invite_add(struct t_twc_profile *profile, int32_t friend_number,
int32_t friend_number, uint8_t group_chat_type, uint8_t group_chat_type, uint8_t *data, size_t size);
uint8_t *data, size_t size);
int int
twc_group_chat_invite_join(struct t_twc_group_chat_invite *invite); twc_group_chat_invite_join(struct t_twc_group_chat_invite *invite);
@ -51,8 +50,7 @@ void
twc_group_chat_invite_remove(struct t_twc_group_chat_invite *invite); twc_group_chat_invite_remove(struct t_twc_group_chat_invite *invite);
struct t_twc_group_chat_invite * struct t_twc_group_chat_invite *
twc_group_chat_invite_with_index(struct t_twc_profile *profile, twc_group_chat_invite_with_index(struct t_twc_profile *profile, size_t index);
size_t index);
void void
twc_group_chat_invite_free(struct t_twc_group_chat_invite *invite); twc_group_chat_invite_free(struct t_twc_group_chat_invite *invite);
@ -60,5 +58,4 @@ twc_group_chat_invite_free(struct t_twc_group_chat_invite *invite);
void void
twc_group_chat_invite_free_list(struct t_twc_list *list); twc_group_chat_invite_free_list(struct t_twc_list *list);
#endif // TOX_WEECHAT_GROUP_INVITE_H #endif /* TOX_WEECHAT_GROUP_INVITE_H */

View File

@ -5,7 +5,8 @@
* *
* Tox-WeeChat is free software: you can redistribute it and/or modify * Tox-WeeChat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * the Free Software Foundation, either version 3 of the License, or * (at your
* option) any later version.
* *
* Tox-WeeChat is distributed in the hope that it will be useful, * Tox-WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -19,20 +20,18 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#include "twc.h"
#include "twc-profile.h" #include "twc-profile.h"
#include "twc-utils.h" #include "twc-utils.h"
#include "twc.h"
#include "twc-gui.h" #include "twc-gui.h"
char * char *
twc_bar_item_away(const void *pointer, void *data, twc_bar_item_away(const void *pointer, void *data, struct t_gui_bar_item *item,
struct t_gui_bar_item *item, struct t_gui_window *window, struct t_gui_buffer *buffer,
struct t_gui_window *window,
struct t_gui_buffer *buffer,
struct t_hashtable *extra_info) struct t_hashtable *extra_info)
{ {
struct t_twc_profile *profile = twc_profile_search_buffer(buffer); struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
@ -90,23 +89,19 @@ twc_bar_item_buffer_plugin(const void *pointer, void *data,
const char *profile_name = profile->name; const char *profile_name = profile->name;
snprintf(string, sizeof(string), snprintf(string, sizeof(string), "%s%s/%s%s%s/%s%s", plugin_name,
"%s%s/%s%s%s/%s%s", weechat_color("bar_delim"), weechat_color("bar_fg"), profile_name,
plugin_name, weechat_color("bar_delim"), weechat_color("bar_fg"),
weechat_color("bar_delim"),
weechat_color("bar_fg"),
profile_name,
weechat_color("bar_delim"),
weechat_color("bar_fg"),
profile->tox_online ? "online" : "offline"); profile->tox_online ? "online" : "offline");
return strdup(string); return strdup(string);
} }
void twc_gui_init() void
twc_gui_init()
{ {
weechat_bar_item_new("away", twc_bar_item_away, NULL, NULL); weechat_bar_item_new("away", twc_bar_item_away, NULL, NULL);
weechat_bar_item_new("input_prompt", twc_bar_item_input_prompt, NULL, NULL); weechat_bar_item_new("input_prompt", twc_bar_item_input_prompt, NULL, NULL);
weechat_bar_item_new("buffer_plugin", twc_bar_item_buffer_plugin, NULL, NULL); weechat_bar_item_new("buffer_plugin", twc_bar_item_buffer_plugin, NULL,
NULL);
} }

View File

@ -20,7 +20,7 @@
#ifndef TOX_WEECHAT_GUI_H #ifndef TOX_WEECHAT_GUI_H
#define TOX_WEECHAT_GUI_H #define TOX_WEECHAT_GUI_H
void twc_gui_init(); void
twc_gui_init();
#endif // TOX_WEECHAT_GUI_H
#endif /* TOX_WEECHAT_GUI_H */

View File

@ -82,8 +82,7 @@ twc_list_item_new_data_add(struct t_twc_list *list, const void *data)
* Add an item to the list. * Add an item to the list.
*/ */
void void
twc_list_add(struct t_twc_list *list, twc_list_add(struct t_twc_list *list, struct t_twc_list_item *item)
struct t_twc_list_item *item)
{ {
item->list = list; item->list = list;
@ -140,7 +139,7 @@ twc_list_remove_with_data(struct t_twc_list *list, const void *data)
{ {
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(list, index, item) twc_list_foreach (list, index, item)
{ {
if (item->data == data) if (item->data == data)
{ {
@ -176,7 +175,7 @@ twc_list_get(struct t_twc_list *list, size_t index)
struct t_twc_list_item *item; struct t_twc_list_item *item;
if (list->count - index > index / 2) if (list->count - index > index / 2)
{ {
twc_list_foreach(list, current_index, item) twc_list_foreach (list, current_index, item)
{ {
if (current_index == index) if (current_index == index)
return item; return item;
@ -184,7 +183,7 @@ twc_list_get(struct t_twc_list *list, size_t index)
} }
else else
{ {
twc_list_foreach_reverse(list, current_index, item) twc_list_foreach_reverse (list, current_index, item)
{ {
if (current_index == index) if (current_index == index)
return item; return item;
@ -193,4 +192,3 @@ twc_list_get(struct t_twc_list *list, size_t index)
return NULL; return NULL;
} }

View File

@ -33,10 +33,9 @@ struct t_twc_list_item
{ {
struct t_twc_list *list; struct t_twc_list *list;
// don't know if this is a good idea /* don't know if this is a good idea
// probably not * probably not */
union union {
{
void *data; void *data;
struct t_twc_profile *profile; struct t_twc_profile *profile;
struct t_twc_friend_request *friend_request; struct t_twc_friend_request *friend_request;
@ -79,15 +78,11 @@ twc_list_pop(struct t_twc_list *list);
struct t_twc_list_item * struct t_twc_list_item *
twc_list_get(struct t_twc_list *list, size_t index); twc_list_get(struct t_twc_list *list, size_t index);
#define twc_list_foreach(list, index, item) \ #define twc_list_foreach(list, index, item) \
for (item = list->head, index = 0; \ for (item = list->head, index = 0; item; item = item->next_item, ++index)
item; \
item = item->next_item, ++index)
#define twc_list_foreach_reverse(list, index, item) \ #define twc_list_foreach_reverse(list, index, item) \
for (item = list->tail, index = list->count - 1; \ for (item = list->tail, index = list->count - 1; item; \
item; \
item = item->prev_item, --index) item = item->prev_item, --index)
#endif // TOX_WEECHAT_LIST_H #endif /* TOX_WEECHAT_LIST_H */

View File

@ -19,13 +19,13 @@
#include <string.h> #include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#include "twc.h"
#include "twc-list.h" #include "twc-list.h"
#include "twc-profile.h" #include "twc-profile.h"
#include "twc-utils.h" #include "twc-utils.h"
#include "twc.h"
#include "twc-message-queue.h" #include "twc-message-queue.h"
@ -36,12 +36,12 @@ struct t_twc_list *
twc_message_queue_get_or_create(struct t_twc_profile *profile, twc_message_queue_get_or_create(struct t_twc_profile *profile,
int32_t friend_number) int32_t friend_number)
{ {
struct t_twc_list *message_queue = weechat_hashtable_get(profile->message_queues, &friend_number); struct t_twc_list *message_queue =
weechat_hashtable_get(profile->message_queues, &friend_number);
if (!message_queue) if (!message_queue)
{ {
message_queue = twc_list_new(); message_queue = twc_list_new();
weechat_hashtable_set(profile->message_queues, weechat_hashtable_set(profile->message_queues, &friend_number,
&friend_number,
message_queue); message_queue);
} }
@ -50,12 +50,12 @@ twc_message_queue_get_or_create(struct t_twc_profile *profile,
/** /**
* Add a friend message to the message queue and tries to send it if the * Add a friend message to the message queue and tries to send it if the
* friend is online. Handles splitting of messages. (TODO: actually split messages) * friend is online. Handles splitting of messages. (TODO: actually split
* messages)
*/ */
void void
twc_message_queue_add_friend_message(struct t_twc_profile *profile, twc_message_queue_add_friend_message(struct t_twc_profile *profile,
int32_t friend_number, int32_t friend_number, const char *message,
const char *message,
TOX_MESSAGE_TYPE message_type) TOX_MESSAGE_TYPE message_type)
{ {
int len = strlen(message); int len = strlen(message);
@ -63,8 +63,8 @@ twc_message_queue_add_friend_message(struct t_twc_profile *profile,
{ {
int fit_len = twc_fit_utf8(message, TWC_MAX_FRIEND_MESSAGE_LENGTH); int fit_len = twc_fit_utf8(message, TWC_MAX_FRIEND_MESSAGE_LENGTH);
struct t_twc_queued_message *queued_message struct t_twc_queued_message *queued_message =
= malloc(sizeof(struct t_twc_queued_message)); malloc(sizeof(struct t_twc_queued_message));
time_t rawtime = time(NULL); time_t rawtime = time(NULL);
queued_message->time = malloc(sizeof(struct tm)); queued_message->time = malloc(sizeof(struct tm));
@ -76,15 +76,16 @@ twc_message_queue_add_friend_message(struct t_twc_profile *profile,
message += fit_len; message += fit_len;
len -= fit_len; len -= fit_len;
// create a queue if needed and add message /* create a queue if needed and add message */
struct t_twc_list *message_queue struct t_twc_list *message_queue =
= twc_message_queue_get_or_create(profile, friend_number); twc_message_queue_get_or_create(profile, friend_number);
twc_list_item_new_data_add(message_queue, queued_message); twc_list_item_new_data_add(message_queue, queued_message);
} }
// flush if friend is online /* flush if friend is online */
if (profile->tox if (profile->tox &&
&& (tox_friend_get_connection_status(profile->tox, friend_number, NULL) != TOX_CONNECTION_NONE)) (tox_friend_get_connection_status(profile->tox, friend_number, NULL) !=
TOX_CONNECTION_NONE))
twc_message_queue_flush_friend(profile, friend_number); twc_message_queue_flush_friend(profile, friend_number);
} }
@ -95,32 +96,30 @@ void
twc_message_queue_flush_friend(struct t_twc_profile *profile, twc_message_queue_flush_friend(struct t_twc_profile *profile,
int32_t friend_number) int32_t friend_number)
{ {
struct t_twc_list *message_queue struct t_twc_list *message_queue =
= twc_message_queue_get_or_create(profile, friend_number); twc_message_queue_get_or_create(profile, friend_number);
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(message_queue, index, item) twc_list_foreach (message_queue, index, item)
{ {
struct t_twc_queued_message *queued_message = item->queued_message; struct t_twc_queued_message *queued_message = item->queued_message;
// TODO: store and deal with message IDs /* TODO: store and deal with message IDs */
TOX_ERR_FRIEND_SEND_MESSAGE err; TOX_ERR_FRIEND_SEND_MESSAGE err;
(void)tox_friend_send_message(profile->tox, (void)tox_friend_send_message(profile->tox, friend_number,
friend_number,
queued_message->message_type, queued_message->message_type,
(uint8_t *)queued_message->message, (uint8_t *)queued_message->message,
strlen(queued_message->message), strlen(queued_message->message), &err);
&err);
if (err == TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_CONNECTED) if (err == TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_CONNECTED)
{ {
// break if message send failed /* break if message send failed */
break; break;
} }
else else
{ {
char *err_str; char *err_str;
// check if error occured /* check if error occured */
switch (err) switch (err)
{ {
case TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG: case TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG:
@ -146,22 +145,20 @@ twc_message_queue_flush_friend(struct t_twc_profile *profile,
} }
if (err != TOX_ERR_FRIEND_SEND_MESSAGE_OK) if (err != TOX_ERR_FRIEND_SEND_MESSAGE_OK)
{ {
struct t_twc_chat *friend_chat struct t_twc_chat *friend_chat =
= twc_chat_search_friend(profile, friend_number, true); twc_chat_search_friend(profile, friend_number, true);
weechat_printf(friend_chat->buffer, weechat_printf(
"%s%sFailed to send message: %s%s", friend_chat->buffer, "%s%sFailed to send message: %s%s",
weechat_prefix("error"), weechat_prefix("error"), weechat_color("chat_highlight"),
weechat_color("chat_highlight"), err_str, weechat_color("reset"));
err_str,
weechat_color("reset"));
} }
twc_message_queue_free_message(queued_message); twc_message_queue_free_message(queued_message);
item->queued_message = NULL; item->queued_message = NULL;
} }
} }
// remove any now-empty items /* remove any now-empty items */
while (message_queue->head && !(message_queue->head->queued_message)) while (message_queue->head && !(message_queue->head->queued_message))
twc_list_remove(message_queue->head); twc_list_remove(message_queue->head);
} }
@ -200,4 +197,3 @@ twc_message_queue_free_profile(struct t_twc_profile *profile)
twc_message_queue_free_map_callback, NULL); twc_message_queue_free_map_callback, NULL);
weechat_hashtable_free(profile->message_queues); weechat_hashtable_free(profile->message_queues);
} }

View File

@ -38,8 +38,7 @@ struct t_twc_queued_message
void void
twc_message_queue_add_friend_message(struct t_twc_profile *profile, twc_message_queue_add_friend_message(struct t_twc_profile *profile,
int32_t friend_number, int32_t friend_number, const char *message,
const char *message,
TOX_MESSAGE_TYPE message_type); TOX_MESSAGE_TYPE message_type);
void void
@ -52,5 +51,4 @@ twc_message_queue_free_message(struct t_twc_queued_message *message);
void void
twc_message_queue_free_profile(struct t_twc_profile *profile); twc_message_queue_free_profile(struct t_twc_profile *profile);
#endif // TOX_WEECHAT_MESSAGE_QUEUE_H #endif /* TOX_WEECHAT_MESSAGE_QUEUE_H */

View File

@ -17,27 +17,27 @@
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>. * along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <pwd.h>
#include <unistd.h> #include <unistd.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#ifdef TOXENCRYPTSAVE_ENABLED #ifdef TOXENCRYPTSAVE_ENABLED
#include <tox/toxencryptsave.h> #include <tox/toxencryptsave.h>
#endif // TOXENCRYPTSAVE_ENABLED #endif /* TOXENCRYPTSAVE_ENABLED */
#include "twc.h"
#include "twc-list.h"
#include "twc-bootstrap.h" #include "twc-bootstrap.h"
#include "twc-chat.h"
#include "twc-config.h" #include "twc-config.h"
#include "twc-friend-request.h" #include "twc-friend-request.h"
#include "twc-group-invite.h" #include "twc-group-invite.h"
#include "twc-list.h"
#include "twc-message-queue.h" #include "twc-message-queue.h"
#include "twc-chat.h"
#include "twc-tox-callbacks.h" #include "twc-tox-callbacks.h"
#include "twc-utils.h" #include "twc-utils.h"
#include "twc.h"
#include "twc-profile.h" #include "twc-profile.h"
@ -54,10 +54,12 @@ struct t_config_option *twc_config_profile_default[TWC_PROFILE_NUM_OPTIONS];
char * char *
twc_profile_expanded_data_path(struct t_twc_profile *profile) twc_profile_expanded_data_path(struct t_twc_profile *profile)
{ {
const char *weechat_dir = weechat_info_get ("weechat_dir", NULL); const char *weechat_dir = weechat_info_get("weechat_dir", NULL);
const char *base_path = TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_SAVEFILE); const char *base_path =
TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_SAVEFILE);
char *home_expanded = weechat_string_replace(base_path, "%h", weechat_dir); char *home_expanded = weechat_string_replace(base_path, "%h", weechat_dir);
char *full_path = weechat_string_replace(home_expanded, "%p", profile->name); char *full_path =
weechat_string_replace(home_expanded, "%p", profile->name);
free(home_expanded); free(home_expanded);
return full_path; return full_path;
@ -72,17 +74,17 @@ int
twc_profile_save_data_file(struct t_twc_profile *profile) twc_profile_save_data_file(struct t_twc_profile *profile)
{ {
if (!(profile->tox)) if (!(profile->tox))
return -1; return -1;
char *full_path = twc_profile_expanded_data_path(profile); char *full_path = twc_profile_expanded_data_path(profile);
// create containing folder if it doesn't exist /* create containing folder if it doesn't exist */
char *rightmost_slash = strrchr(full_path, '/'); char *rightmost_slash = strrchr(full_path, '/');
char *dir_path = weechat_strndup(full_path, rightmost_slash - full_path); char *dir_path = weechat_strndup(full_path, rightmost_slash - full_path);
weechat_mkdir_parents(dir_path, 0755); weechat_mkdir_parents(dir_path, 0755);
free(dir_path); free(dir_path);
// save Tox data to a buffer /* save Tox data to a buffer */
size_t size = tox_get_savedata_size(profile->tox); size_t size = tox_get_savedata_size(profile->tox);
uint8_t data[size]; uint8_t data[size];
uint8_t *d = data; uint8_t *d = data;
@ -90,13 +92,14 @@ twc_profile_save_data_file(struct t_twc_profile *profile)
#ifdef TOXENCRYPTSAVE_ENABLED #ifdef TOXENCRYPTSAVE_ENABLED
uint8_t enc_data[size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH]; uint8_t enc_data[size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
const char *pw const char *pw =
= weechat_config_string(profile->options[TWC_PROFILE_OPTION_PASSPHRASE]); weechat_config_string(profile->options[TWC_PROFILE_OPTION_PASSPHRASE]);
if (pw) if (pw)
{ {
pw = weechat_string_eval_expression(pw, NULL, NULL, NULL); pw = weechat_string_eval_expression(pw, NULL, NULL, NULL);
if (!tox_pass_encrypt(data, size, (uint8_t *)pw, strlen(pw), enc_data, NULL)) if (!tox_pass_encrypt(data, size, (uint8_t *)pw, strlen(pw), enc_data,
NULL))
{ {
weechat_printf(profile->buffer, "error encrypting data"); weechat_printf(profile->buffer, "error encrypting data");
return -1; return -1;
@ -104,9 +107,9 @@ twc_profile_save_data_file(struct t_twc_profile *profile)
d = enc_data; d = enc_data;
size += TOX_PASS_ENCRYPTION_EXTRA_LENGTH; size += TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
} }
#endif // TOXENCRYPTSAVE_ENABLED #endif /* TOXENCRYPTSAVE_ENABLED */
// save buffer to a file /* save buffer to a file */
FILE *file = fopen(full_path, "w"); FILE *file = fopen(full_path, "w");
if (file) if (file)
{ {
@ -152,10 +155,10 @@ twc_profile_new(const char *name)
struct t_twc_profile *profile = malloc(sizeof(struct t_twc_profile)); struct t_twc_profile *profile = malloc(sizeof(struct t_twc_profile));
profile->name = strdup(name); profile->name = strdup(name);
// add to profile list /* add to profile list */
twc_list_item_new_data_add(twc_profiles, profile); twc_list_item_new_data_add(twc_profiles, profile);
// set up internal vars /* set up internal vars */
profile->tox = NULL; profile->tox = NULL;
profile->buffer = NULL; profile->buffer = NULL;
profile->tox_do_timer = NULL; profile->tox_do_timer = NULL;
@ -164,12 +167,10 @@ twc_profile_new(const char *name)
profile->chats = twc_list_new(); profile->chats = twc_list_new();
profile->friend_requests = twc_list_new(); profile->friend_requests = twc_list_new();
profile->group_chat_invites = twc_list_new(); profile->group_chat_invites = twc_list_new();
profile->message_queues = weechat_hashtable_new(32, profile->message_queues = weechat_hashtable_new(
WEECHAT_HASHTABLE_INTEGER, 32, WEECHAT_HASHTABLE_INTEGER, WEECHAT_HASHTABLE_POINTER, NULL, NULL);
WEECHAT_HASHTABLE_POINTER,
NULL, NULL);
// set up config /* set up config */
twc_config_init_profile(profile); twc_config_init_profile(profile);
return profile; return profile;
@ -203,11 +204,11 @@ twc_profile_set_options(struct Tox_Options *options,
} }
options->proxy_port = options->proxy_port =
TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_PORT); TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_PORT);
options->udp_enabled = options->udp_enabled =
TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_UDP); TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_UDP);
options->ipv6_enabled = options->ipv6_enabled =
TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_IPV6); TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_IPV6);
#ifndef NDEBUG #ifndef NDEBUG
options->log_callback = twc_tox_log_callback; options->log_callback = twc_tox_log_callback;
@ -217,57 +218,58 @@ twc_profile_set_options(struct Tox_Options *options,
void void
twc_tox_new_print_error(struct t_twc_profile *profile, twc_tox_new_print_error(struct t_twc_profile *profile,
struct Tox_Options *options, struct Tox_Options *options, TOX_ERR_NEW error)
TOX_ERR_NEW error)
{ {
switch (error) switch (error)
{ {
case TOX_ERR_NEW_MALLOC: case TOX_ERR_NEW_MALLOC:
weechat_printf(profile->buffer, weechat_printf(profile->buffer,
"%scould not load Tox (malloc error)", "%scould not load Tox (malloc error)",
weechat_prefix("error")); weechat_prefix("error"));
break; break;
case TOX_ERR_NEW_PORT_ALLOC: case TOX_ERR_NEW_PORT_ALLOC:
weechat_printf(profile->buffer, weechat_printf(profile->buffer,
"%scould not load Tox (failed to allocate a port)", "%scould not load Tox (failed to allocate a port)",
weechat_prefix("error")); weechat_prefix("error"));
break; break;
case TOX_ERR_NEW_PROXY_BAD_TYPE: case TOX_ERR_NEW_PROXY_BAD_TYPE:
weechat_printf(profile->buffer, weechat_printf(
"%scould not load Tox (internal error; bad proxy type)", profile->buffer,
weechat_prefix("error")); "%scould not load Tox (internal error; bad proxy type)",
break; weechat_prefix("error"));
case TOX_ERR_NEW_PROXY_BAD_HOST: break;
weechat_printf(profile->buffer, case TOX_ERR_NEW_PROXY_BAD_HOST:
"%scould not load Tox (invalid proxy host: \"%s\")", weechat_printf(profile->buffer,
weechat_prefix("error"), options->proxy_host); "%scould not load Tox (invalid proxy host: \"%s\")",
break; weechat_prefix("error"), options->proxy_host);
case TOX_ERR_NEW_PROXY_BAD_PORT: break;
weechat_printf(profile->buffer, case TOX_ERR_NEW_PROXY_BAD_PORT:
"%scould not load Tox (invalid proxy port: \"%d\")", weechat_printf(profile->buffer,
weechat_prefix("error"), options->proxy_port); "%scould not load Tox (invalid proxy port: \"%d\")",
break; weechat_prefix("error"), options->proxy_port);
case TOX_ERR_NEW_PROXY_NOT_FOUND: break;
weechat_printf(profile->buffer, case TOX_ERR_NEW_PROXY_NOT_FOUND:
"%scould not load Tox (proxy host not found: \"%s\")", weechat_printf(
weechat_prefix("error"), options->proxy_host); profile->buffer,
break; "%scould not load Tox (proxy host not found: \"%s\")",
case TOX_ERR_NEW_LOAD_ENCRYPTED: weechat_prefix("error"), options->proxy_host);
weechat_printf(profile->buffer, break;
"%scould not load Tox (encrypted data files are not yet supported)", case TOX_ERR_NEW_LOAD_ENCRYPTED:
weechat_prefix("error")); weechat_printf(profile->buffer, "%scould not load Tox (encrypted "
break; "data files are not yet supported)",
case TOX_ERR_NEW_LOAD_BAD_FORMAT: weechat_prefix("error"));
weechat_printf(profile->buffer, break;
"%scould not load Tox (invalid data file, some data " case TOX_ERR_NEW_LOAD_BAD_FORMAT:
"may have been loaded; use -force to try using it)", weechat_printf(profile->buffer,
weechat_prefix("error")); "%scould not load Tox (invalid data file, some data "
break; "may have been loaded; use -force to try using it)",
default: weechat_prefix("error"));
weechat_printf(profile->buffer, break;
"%scould not load Tox (unknown error %d)", default:
weechat_prefix("error"), error); weechat_printf(profile->buffer,
break; "%scould not load Tox (unknown error %d)",
weechat_prefix("error"), error);
break;
} }
} }
@ -283,43 +285,39 @@ twc_profile_load(struct t_twc_profile *profile)
if (!(profile->buffer)) if (!(profile->buffer))
{ {
// create main buffer /* create main buffer */
profile->buffer = weechat_buffer_new(profile->name, profile->buffer = weechat_buffer_new(profile->name, NULL, NULL, NULL,
NULL, NULL, NULL,
twc_profile_buffer_close_callback, twc_profile_buffer_close_callback,
profile, NULL); profile, NULL);
if (!(profile->buffer)) if (!(profile->buffer))
return TWC_RC_ERROR; return TWC_RC_ERROR;
/* disable logging for buffer if option is off */ /* disable logging for buffer if option is off */
bool logging = TWC_PROFILE_OPTION_BOOLEAN(profile, bool logging =
TWC_PROFILE_OPTION_LOGGING); TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_LOGGING);
twc_set_buffer_logging(profile->buffer, logging); twc_set_buffer_logging(profile->buffer, logging);
profile->nicklist_group = weechat_nicklist_add_group(profile->buffer, NULL, profile->nicklist_group =
NULL, NULL, true); weechat_nicklist_add_group(profile->buffer, NULL, NULL, NULL, true);
weechat_buffer_set(profile->buffer, "nicklist", "1"); weechat_buffer_set(profile->buffer, "nicklist", "1");
} }
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%sprofile %s connecting",
"%sprofile %s connecting",
weechat_prefix("network"), profile->name); weechat_prefix("network"), profile->name);
// create Tox options object /* create Tox options object */
struct Tox_Options options; struct Tox_Options options;
//tox_options_default(&options);
twc_profile_set_options(&options, profile); twc_profile_set_options(&options, profile);
// print a proxy message /* print a proxy message */
if (options.proxy_type != TOX_PROXY_TYPE_NONE) if (options.proxy_type != TOX_PROXY_TYPE_NONE)
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%susing %s proxy %s:%d",
"%susing %s proxy %s:%d",
weechat_prefix("network"), weechat_prefix("network"),
options.proxy_type == TOX_PROXY_TYPE_HTTP ? "HTTP" : options.proxy_type == TOX_PROXY_TYPE_HTTP
TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" : ? "HTTP"
NULL, : TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" : NULL,
options.proxy_host, options.proxy_port); options.proxy_host, options.proxy_port);
if (options.udp_enabled) if (options.udp_enabled)
@ -327,12 +325,12 @@ twc_profile_load(struct t_twc_profile *profile)
"%s%swarning:%s Tox is configured to use a proxy, " "%s%swarning:%s Tox is configured to use a proxy, "
"but UDP is not disabled. Your IP address may not " "but UDP is not disabled. Your IP address may not "
"be hidden.", "be hidden.",
weechat_prefix("error"), weechat_prefix("error"), weechat_color("lightred"),
weechat_color("lightred"), weechat_color("reset"), weechat_color("reset"), options.proxy_host,
options.proxy_host, options.proxy_port); options.proxy_port);
} }
// try loading data file /* try loading data file */
char *path = twc_profile_expanded_data_path(profile); char *path = twc_profile_expanded_data_path(profile);
FILE *file = NULL; FILE *file = NULL;
size_t data_size; size_t data_size;
@ -355,7 +353,8 @@ twc_profile_load(struct t_twc_profile *profile)
if ((data_size != fread(&data, 1, data_size, file))) if ((data_size != fread(&data, 1, data_size, file)))
{ {
fclose(file); fclose(file);
weechat_printf(profile->buffer, "%scould not load Tox data file, aborting", weechat_printf(profile->buffer,
"%scould not load Tox data file, aborting",
weechat_prefix("error")); weechat_prefix("error"));
return TWC_RC_ERROR; return TWC_RC_ERROR;
} }
@ -367,15 +366,19 @@ twc_profile_load(struct t_twc_profile *profile)
if (data_size && tox_is_data_encrypted(data)) if (data_size && tox_is_data_encrypted(data))
{ {
const char *pw = weechat_config_string(profile->options[TWC_PROFILE_OPTION_PASSPHRASE]); const char *pw = weechat_config_string(
profile->options[TWC_PROFILE_OPTION_PASSPHRASE]);
if (pw) if (pw)
{ {
// evaluate password option and duplicate as tox_*_decrypt wipes it /* evaluate password option and duplicate as tox_*_decrypt wipes
* it */
pw = weechat_string_eval_expression(pw, NULL, NULL, NULL); pw = weechat_string_eval_expression(pw, NULL, NULL, NULL);
if (!tox_pass_decrypt(data, data_size, (uint8_t *)pw, strlen(pw), dec_data, NULL)) if (!tox_pass_decrypt(data, data_size, (uint8_t *)pw, strlen(pw),
dec_data, NULL))
{ {
weechat_printf(profile->buffer, "%scould not decrypt Tox data file, aborting", weechat_printf(profile->buffer,
"%scould not decrypt Tox data file, aborting",
weechat_prefix("error")); weechat_prefix("error"));
return TWC_RC_ERROR; return TWC_RC_ERROR;
} }
@ -387,12 +390,13 @@ twc_profile_load(struct t_twc_profile *profile)
{ {
options.savedata_data = data; options.savedata_data = data;
} }
#endif // TOXENCRYPTSAVE_ENABLED #endif /* TOXENCRYPTSAVE_ENABLED */
options.savedata_type = (data_size == 0) ? TOX_SAVEDATA_TYPE_NONE : TOX_SAVEDATA_TYPE_TOX_SAVE; options.savedata_type =
(data_size == 0) ? TOX_SAVEDATA_TYPE_NONE : TOX_SAVEDATA_TYPE_TOX_SAVE;
options.savedata_length = data_size; options.savedata_length = data_size;
// create Tox /* create Tox */
TOX_ERR_NEW rc; TOX_ERR_NEW rc;
profile->tox = tox_new(&options, &rc); profile->tox = tox_new(&options, &rc);
if (rc != TOX_ERR_NEW_OK) if (rc != TOX_ERR_NEW_OK)
@ -404,7 +408,7 @@ twc_profile_load(struct t_twc_profile *profile)
if (data_size == 0) if (data_size == 0)
{ {
// no data file loaded, set default name /* no data file loaded, set default name */
const char *default_name = "Tox-WeeChat User"; const char *default_name = "Tox-WeeChat User";
const char *name; const char *name;
@ -415,35 +419,35 @@ twc_profile_load(struct t_twc_profile *profile)
name = default_name; name = default_name;
TOX_ERR_SET_INFO rc; TOX_ERR_SET_INFO rc;
tox_self_set_name(profile->tox, tox_self_set_name(profile->tox, (uint8_t *)name, strlen(name), &rc);
(uint8_t *)name, strlen(name),
&rc);
if (rc == TOX_ERR_SET_INFO_TOO_LONG) if (rc == TOX_ERR_SET_INFO_TOO_LONG)
tox_self_set_name(profile->tox, tox_self_set_name(profile->tox, (uint8_t *)default_name,
(uint8_t *)default_name, strlen(default_name), strlen(default_name), NULL);
NULL);
} }
// bootstrap DHT /* bootstrap DHT
// TODO: add count to config * TODO: add count to config */
int bootstrap_node_count = 5; int bootstrap_node_count = 5;
for (int i = 0; i < bootstrap_node_count; ++i) for (int i = 0; i < bootstrap_node_count; ++i)
twc_bootstrap_random_node(profile->tox); twc_bootstrap_random_node(profile->tox);
// start tox_iterate loop /* start tox_iterate loop */
twc_do_timer_cb(profile, NULL, 0); twc_do_timer_cb(profile, NULL, 0);
// register Tox callbacks /* register Tox callbacks */
tox_callback_friend_message(profile->tox, twc_friend_message_callback); tox_callback_friend_message(profile->tox, twc_friend_message_callback);
tox_callback_friend_connection_status(profile->tox, twc_connection_status_callback); tox_callback_friend_connection_status(profile->tox,
twc_connection_status_callback);
tox_callback_friend_name(profile->tox, twc_name_change_callback); tox_callback_friend_name(profile->tox, twc_name_change_callback);
tox_callback_friend_status(profile->tox, twc_user_status_callback); tox_callback_friend_status(profile->tox, twc_user_status_callback);
tox_callback_friend_status_message(profile->tox, twc_status_message_callback); tox_callback_friend_status_message(profile->tox,
twc_status_message_callback);
tox_callback_friend_request(profile->tox, twc_friend_request_callback); tox_callback_friend_request(profile->tox, twc_friend_request_callback);
tox_callback_conference_invite(profile->tox, twc_group_invite_callback); tox_callback_conference_invite(profile->tox, twc_group_invite_callback);
tox_callback_conference_message(profile->tox, twc_group_message_callback); tox_callback_conference_message(profile->tox, twc_group_message_callback);
tox_callback_conference_namelist_change(profile->tox, twc_group_namelist_change_callback); tox_callback_conference_namelist_change(profile->tox,
twc_group_namelist_change_callback);
tox_callback_conference_title(profile->tox, twc_group_title_callback); tox_callback_conference_title(profile->tox, twc_group_title_callback);
return TWC_RC_OK; return TWC_RC_OK;
} }
@ -454,11 +458,11 @@ twc_profile_load(struct t_twc_profile *profile)
void void
twc_profile_unload(struct t_twc_profile *profile) twc_profile_unload(struct t_twc_profile *profile)
{ {
// check that we're not already disconnected /* check that we're not already disconnected */
if (!(profile->tox)) if (!(profile->tox))
return; return;
// save and kill tox /* save and kill tox */
int result = twc_profile_save_data_file(profile); int result = twc_profile_save_data_file(profile);
tox_kill(profile->tox); tox_kill(profile->tox);
profile->tox = NULL; profile->tox = NULL;
@ -466,19 +470,17 @@ twc_profile_unload(struct t_twc_profile *profile)
if (result == -1) if (result == -1)
{ {
char *path = twc_profile_expanded_data_path(profile); char *path = twc_profile_expanded_data_path(profile);
weechat_printf(NULL, weechat_printf(
"%s%s: Could not save Tox data for profile %s to file: %s", NULL, "%s%s: Could not save Tox data for profile %s to file: %s",
weechat_prefix("error"), weechat_plugin->name, weechat_prefix("error"), weechat_plugin->name, profile->name, path);
profile->name,
path);
free(path); free(path);
} }
// stop Tox timer /* stop Tox timer */
weechat_unhook(profile->tox_do_timer); weechat_unhook(profile->tox_do_timer);
// have to refresh and hide bar items even if we were already offline /* have to refresh and hide bar items even if we were already offline
// TODO * TODO */
twc_profile_refresh_online_status(profile); twc_profile_refresh_online_status(profile);
twc_profile_set_online_status(profile, false); twc_profile_set_online_status(profile, false);
} }
@ -491,9 +493,10 @@ twc_profile_autoload()
{ {
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(twc_profiles, index, item) twc_list_foreach (twc_profiles, index, item)
{ {
if (TWC_PROFILE_OPTION_BOOLEAN(item->profile, TWC_PROFILE_OPTION_AUTOLOAD)) if (TWC_PROFILE_OPTION_BOOLEAN(item->profile,
TWC_PROFILE_OPTION_AUTOLOAD))
twc_profile_load(item->profile); twc_profile_load(item->profile);
} }
} }
@ -507,8 +510,7 @@ twc_profile_refresh_online_status(struct t_twc_profile *profile)
} }
void void
twc_profile_set_online_status(struct t_twc_profile *profile, twc_profile_set_online_status(struct t_twc_profile *profile, bool status)
bool status)
{ {
if (profile->tox_online ^ status) if (profile->tox_online ^ status)
{ {
@ -517,18 +519,14 @@ twc_profile_set_online_status(struct t_twc_profile *profile,
if (profile->tox_online) if (profile->tox_online)
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%s%s: profile %s connected",
"%s%s: profile %s connected", weechat_prefix("network"), weechat_plugin->name,
weechat_prefix("network"),
weechat_plugin->name,
profile->name); profile->name);
} }
else else
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%s%s: profile %s disconnected",
"%s%s: profile %s disconnected", weechat_prefix("network"), weechat_plugin->name,
weechat_prefix("network"),
weechat_plugin->name,
profile->name); profile->name);
} }
} }
@ -542,7 +540,7 @@ twc_profile_search_name(const char *name)
{ {
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(twc_profiles, index, item) twc_list_foreach (twc_profiles, index, item)
{ {
if (weechat_strcasecmp(item->profile->name, name) == 0) if (weechat_strcasecmp(item->profile->name, name) == 0)
return item->profile; return item->profile;
@ -559,14 +557,14 @@ twc_profile_search_buffer(struct t_gui_buffer *buffer)
{ {
size_t profile_index; size_t profile_index;
struct t_twc_list_item *profile_item; struct t_twc_list_item *profile_item;
twc_list_foreach(twc_profiles, profile_index, profile_item) twc_list_foreach (twc_profiles, profile_index, profile_item)
{ {
if (profile_item->profile->buffer == buffer) if (profile_item->profile->buffer == buffer)
return profile_item->profile; return profile_item->profile;
size_t chat_index; size_t chat_index;
struct t_twc_list_item *chat_item; struct t_twc_list_item *chat_item;
twc_list_foreach(profile_item->profile->chats, chat_index, chat_item) twc_list_foreach (profile_item->profile->chats, chat_index, chat_item)
{ {
if (chat_item->chat->buffer == buffer) if (chat_item->chat->buffer == buffer)
return profile_item->profile; return profile_item->profile;
@ -597,22 +595,23 @@ twc_profile_set_logging(struct t_twc_profile *profile, bool logging)
{ {
error = true; error = true;
weechat_printf(profile->buffer, weechat_printf(profile->buffer,
"%swarning: failed to %s logging in this buffer!", "%swarning: failed to %s logging in this buffer!",
weechat_prefix("error"), logging ? "enable" : "disable"); weechat_prefix("error"), logging ? "enable" : "disable");
} }
/* signal all chat buffers for profile */ /* signal all chat buffers for profile */
size_t index; size_t index;
struct t_twc_list_item *item; struct t_twc_list_item *item;
twc_list_foreach(profile->chats, index, item) twc_list_foreach (profile->chats, index, item)
{ {
if (WEECHAT_RC_ERROR == twc_set_buffer_logging(item->chat->buffer, if (WEECHAT_RC_ERROR ==
logging)) twc_set_buffer_logging(item->chat->buffer, logging))
{ {
error = true; error = true;
weechat_printf(item->chat->buffer, weechat_printf(item->chat->buffer,
"%swarning: failed to %s logging in this buffer!", "%swarning: failed to %s logging in this buffer!",
weechat_prefix("error"), logging ? "enable" : "disable"); weechat_prefix("error"),
logging ? "enable" : "disable");
} }
} }
@ -624,8 +623,7 @@ twc_profile_set_logging(struct t_twc_profile *profile, bool logging)
* true, Tox data on disk is also deleted. * true, Tox data on disk is also deleted.
*/ */
void void
twc_profile_delete(struct t_twc_profile *profile, twc_profile_delete(struct t_twc_profile *profile, bool delete_data)
bool delete_data)
{ {
char *data_path = twc_profile_expanded_data_path(profile); char *data_path = twc_profile_expanded_data_path(profile);
@ -644,17 +642,17 @@ twc_profile_delete(struct t_twc_profile *profile,
void void
twc_profile_free(struct t_twc_profile *profile) twc_profile_free(struct t_twc_profile *profile)
{ {
// unload if needed /* unload if needed */
twc_profile_unload(profile); twc_profile_unload(profile);
// close buffer /* close buffer */
if (profile->buffer) if (profile->buffer)
{ {
weechat_buffer_set_pointer(profile->buffer, "close_callback", NULL); weechat_buffer_set_pointer(profile->buffer, "close_callback", NULL);
weechat_buffer_close(profile->buffer); weechat_buffer_close(profile->buffer);
} }
// free things /* free things */
twc_chat_free_list(profile->chats); twc_chat_free_list(profile->chats);
twc_friend_request_free_list(profile->friend_requests); twc_friend_request_free_list(profile->friend_requests);
twc_group_chat_invite_free_list(profile->group_chat_invites); twc_group_chat_invite_free_list(profile->group_chat_invites);
@ -662,7 +660,7 @@ twc_profile_free(struct t_twc_profile *profile)
free(profile->name); free(profile->name);
free(profile); free(profile);
// remove from list /* remove from list */
twc_list_remove_with_data(twc_profiles, profile); twc_list_remove_with_data(twc_profiles, profile);
} }
@ -678,4 +676,3 @@ twc_profile_free_all()
free(twc_profiles); free(twc_profiles);
} }

View File

@ -22,8 +22,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
enum t_twc_profile_option enum t_twc_profile_option
{ {
@ -61,34 +61,41 @@ struct t_twc_profile
}; };
extern struct t_twc_list *twc_profiles; extern struct t_twc_list *twc_profiles;
extern struct t_config_option *twc_config_profile_default[TWC_PROFILE_NUM_OPTIONS]; extern struct t_config_option
*twc_config_profile_default[TWC_PROFILE_NUM_OPTIONS];
#define TWC_PROFILE_OPTION_BOOLEAN(profile, index) \ #define TWC_PROFILE_OPTION_BOOLEAN(profile, index) \
(!weechat_config_option_is_null(profile->options[index]) \ (!weechat_config_option_is_null(profile->options[index]) \
? weechat_config_boolean(profile->options[index]) \ ? weechat_config_boolean(profile->options[index]) \
: (!weechat_config_option_is_null(twc_config_profile_default[index]) \ : (!weechat_config_option_is_null(twc_config_profile_default[index]) \
? weechat_config_boolean(twc_config_profile_default[index]) \ ? weechat_config_boolean(twc_config_profile_default[index]) \
: (!weechat_config_option_default_is_null(twc_config_profile_default[index]) \ : (!weechat_config_option_default_is_null( \
? weechat_config_boolean_default(twc_config_profile_default[index]) \ twc_config_profile_default[index]) \
: 0))) ? weechat_config_boolean_default( \
twc_config_profile_default[index]) \
: 0)))
#define TWC_PROFILE_OPTION_INTEGER(profile, index) \ #define TWC_PROFILE_OPTION_INTEGER(profile, index) \
(!weechat_config_option_is_null(profile->options[index]) \ (!weechat_config_option_is_null(profile->options[index]) \
? weechat_config_integer(profile->options[index]) \ ? weechat_config_integer(profile->options[index]) \
: (!weechat_config_option_is_null(twc_config_profile_default[index]) \ : (!weechat_config_option_is_null(twc_config_profile_default[index]) \
? weechat_config_integer(twc_config_profile_default[index]) \ ? weechat_config_integer(twc_config_profile_default[index]) \
: (!weechat_config_option_default_is_null(twc_config_profile_default[index]) \ : (!weechat_config_option_default_is_null( \
? weechat_config_integer_default(twc_config_profile_default[index]) \ twc_config_profile_default[index]) \
: 0))) ? weechat_config_integer_default( \
twc_config_profile_default[index]) \
: 0)))
#define TWC_PROFILE_OPTION_STRING(profile, index) \ #define TWC_PROFILE_OPTION_STRING(profile, index) \
(!weechat_config_option_is_null(profile->options[index]) \ (!weechat_config_option_is_null(profile->options[index]) \
? weechat_config_string(profile->options[index]) \ ? weechat_config_string(profile->options[index]) \
: (!weechat_config_option_is_null(twc_config_profile_default[index]) \ : (!weechat_config_option_is_null(twc_config_profile_default[index]) \
? weechat_config_string(twc_config_profile_default[index]) \ ? weechat_config_string(twc_config_profile_default[index]) \
: (!weechat_config_option_default_is_null(twc_config_profile_default[index]) \ : (!weechat_config_option_default_is_null( \
? weechat_config_string_default(twc_config_profile_default[index]) \ twc_config_profile_default[index]) \
: NULL))) ? weechat_config_string_default( \
twc_config_profile_default[index]) \
: NULL)))
void void
twc_profile_init(); twc_profile_init();
@ -132,5 +139,4 @@ twc_profile_free(struct t_twc_profile *profile);
void void
twc_profile_free_all(); twc_profile_free_all();
#endif // TOX_WEECHAT_PROFILE_H #endif /* TOX_WEECHAT_PROFILE_H */

View File

@ -17,42 +17,42 @@
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>. * along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <string.h>
#include <inttypes.h> #include <inttypes.h>
#include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#ifdef TOXAV_ENABLED #ifdef TOXAV_ENABLED
#include <tox/toxav.h> #include <tox/toxav.h>
#endif // TOXAV_ENABLED #endif /* TOXAV_ENABLED */
#include "twc.h"
#include "twc-profile.h"
#include "twc-chat.h" #include "twc-chat.h"
#include "twc-friend-request.h" #include "twc-friend-request.h"
#include "twc-group-invite.h" #include "twc-group-invite.h"
#include "twc-message-queue.h" #include "twc-message-queue.h"
#include "twc-profile.h"
#include "twc-utils.h" #include "twc-utils.h"
#include "twc.h"
#include "twc-tox-callbacks.h" #include "twc-tox-callbacks.h"
int int
twc_do_timer_cb(const void *pointer, void *data, twc_do_timer_cb(const void *pointer, void *data, int remaining_calls)
int remaining_calls)
{ {
/* TODO: don't strip the const */ /* TODO: don't strip the const */
struct t_twc_profile *profile = (void *)pointer; struct t_twc_profile *profile = (void *)pointer;
tox_iterate(profile->tox, profile); tox_iterate(profile->tox, profile);
struct t_hook *hook = weechat_hook_timer(tox_iteration_interval(profile->tox), struct t_hook *hook =
0, 1, twc_do_timer_cb, profile, NULL); weechat_hook_timer(tox_iteration_interval(profile->tox), 0, 1,
twc_do_timer_cb, profile, NULL);
profile->tox_do_timer = hook; profile->tox_do_timer = hook;
// check connection status /* check connection status */
TOX_CONNECTION connection = tox_self_get_connection_status(profile->tox); TOX_CONNECTION connection = tox_self_get_connection_status(profile->tox);
bool is_connected = connection == TOX_CONNECTION_TCP bool is_connected =
|| connection == TOX_CONNECTION_UDP; connection == TOX_CONNECTION_TCP || connection == TOX_CONNECTION_UDP;
twc_profile_set_online_status(profile, is_connected); twc_profile_set_online_status(profile, is_connected);
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
@ -60,20 +60,19 @@ twc_do_timer_cb(const void *pointer, void *data,
void void
twc_friend_message_callback(Tox *tox, uint32_t friend_number, twc_friend_message_callback(Tox *tox, uint32_t friend_number,
TOX_MESSAGE_TYPE type, TOX_MESSAGE_TYPE type, const uint8_t *message,
const uint8_t *message, size_t length, size_t length, void *data)
void *data)
{ {
struct t_twc_profile *profile = data; struct t_twc_profile *profile = data;
struct t_twc_chat *chat = twc_chat_search_friend(profile, struct t_twc_chat *chat =
friend_number, twc_chat_search_friend(profile, friend_number, true);
true);
char *name = twc_get_name_nt(profile->tox, friend_number); char *name = twc_get_name_nt(profile->tox, friend_number);
char *message_nt = twc_null_terminate(message, length); char *message_nt = twc_null_terminate(message, length);
twc_chat_print_message(chat, "notify_private", weechat_color("chat_nick_other"), name, twc_chat_print_message(chat, "notify_private",
message_nt, type); weechat_color("chat_nick_other"), name, message_nt,
type);
free(name); free(name);
free(message_nt); free(message_nt);
@ -86,11 +85,10 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number,
struct t_twc_profile *profile = data; struct t_twc_profile *profile = data;
char *name = twc_get_name_nt(profile->tox, friend_number); char *name = twc_get_name_nt(profile->tox, friend_number);
struct t_gui_nick *nick = NULL; struct t_gui_nick *nick = NULL;
struct t_twc_chat *chat = twc_chat_search_friend(profile, struct t_twc_chat *chat =
friend_number, twc_chat_search_friend(profile, friend_number, false);
false);
// TODO: print in friend's buffer if it exists /* TODO: print in friend's buffer if it exists */
if (status == 0) if (status == 0)
{ {
nick = weechat_nicklist_search_nick(profile->buffer, nick = weechat_nicklist_search_nick(profile->buffer,
@ -98,16 +96,12 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number,
if (nick) if (nick)
weechat_nicklist_remove_nick(profile->buffer, nick); weechat_nicklist_remove_nick(profile->buffer, nick);
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%s%s just went offline.",
"%s%s just went offline.", weechat_prefix("network"), name);
weechat_prefix("network"),
name);
if (chat) if (chat)
{ {
weechat_printf(chat->buffer, weechat_printf(chat->buffer, "%s%s just went offline.",
"%s%s just went offline.", weechat_prefix("network"), name);
weechat_prefix("network"),
name);
} }
} }
else if (status == 1) else if (status == 1)
@ -115,16 +109,12 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number,
weechat_nicklist_add_nick(profile->buffer, profile->nicklist_group, weechat_nicklist_add_nick(profile->buffer, profile->nicklist_group,
name, NULL, NULL, NULL, 1); name, NULL, NULL, NULL, 1);
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%s%s just came online.",
"%s%s just came online.", weechat_prefix("network"), name);
weechat_prefix("network"),
name);
if (chat) if (chat)
{ {
weechat_printf(chat->buffer, weechat_printf(chat->buffer, "%s%s just came online.",
"%s%s just came online.", weechat_prefix("network"), name);
weechat_prefix("network"),
name);
} }
twc_message_queue_flush_friend(profile, friend_number); twc_message_queue_flush_friend(profile, friend_number);
} }
@ -132,15 +122,13 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number,
} }
void void
twc_name_change_callback(Tox *tox, uint32_t friend_number, twc_name_change_callback(Tox *tox, uint32_t friend_number, const uint8_t *name,
const uint8_t *name, size_t length, size_t length, void *data)
void *data)
{ {
struct t_twc_profile *profile = data; struct t_twc_profile *profile = data;
struct t_gui_nick *nick = NULL; struct t_gui_nick *nick = NULL;
struct t_twc_chat *chat = twc_chat_search_friend(profile, struct t_twc_chat *chat =
friend_number, twc_chat_search_friend(profile, friend_number, false);
false);
char *old_name = twc_get_name_nt(profile->tox, friend_number); char *old_name = twc_get_name_nt(profile->tox, friend_number);
char *new_name = twc_null_terminate(name, length); char *new_name = twc_null_terminate(name, length);
@ -151,10 +139,8 @@ twc_name_change_callback(Tox *tox, uint32_t friend_number,
{ {
twc_chat_queue_refresh(chat); twc_chat_queue_refresh(chat);
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"), old_name, new_name);
weechat_prefix("network"),
old_name, new_name);
} }
nick = weechat_nicklist_search_nick(profile->buffer, nick = weechat_nicklist_search_nick(profile->buffer,
@ -165,10 +151,8 @@ twc_name_change_callback(Tox *tox, uint32_t friend_number,
weechat_nicklist_add_nick(profile->buffer, profile->nicklist_group, weechat_nicklist_add_nick(profile->buffer, profile->nicklist_group,
new_name, NULL, NULL, NULL, 1); new_name, NULL, NULL, NULL, 1);
weechat_printf(profile->buffer, weechat_printf(profile->buffer, "%s%s is now known as %s",
"%s%s is now known as %s", weechat_prefix("network"), old_name, new_name);
weechat_prefix("network"),
old_name, new_name);
} }
free(old_name); free(old_name);
@ -180,30 +164,26 @@ twc_user_status_callback(Tox *tox, uint32_t friend_number,
TOX_USER_STATUS status, void *data) TOX_USER_STATUS status, void *data)
{ {
struct t_twc_profile *profile = data; struct t_twc_profile *profile = data;
struct t_twc_chat *chat = twc_chat_search_friend(profile, struct t_twc_chat *chat =
friend_number, twc_chat_search_friend(profile, friend_number, false);
false);
if (chat) if (chat)
twc_chat_queue_refresh(chat); twc_chat_queue_refresh(chat);
} }
void void
twc_status_message_callback(Tox *tox, uint32_t friend_number, twc_status_message_callback(Tox *tox, uint32_t friend_number,
const uint8_t *message, size_t length, const uint8_t *message, size_t length, void *data)
void *data)
{ {
struct t_twc_profile *profile = data; struct t_twc_profile *profile = data;
struct t_twc_chat *chat = twc_chat_search_friend(profile, struct t_twc_chat *chat =
friend_number, twc_chat_search_friend(profile, friend_number, false);
false);
if (chat) if (chat)
twc_chat_queue_refresh(chat); twc_chat_queue_refresh(chat);
} }
void void
twc_friend_request_callback(Tox *tox, const uint8_t *public_key, twc_friend_request_callback(Tox *tox, const uint8_t *public_key,
const uint8_t *message, size_t length, const uint8_t *message, size_t length, void *data)
void *data)
{ {
struct t_twc_profile *profile = data; struct t_twc_profile *profile = data;
@ -213,26 +193,28 @@ twc_friend_request_callback(Tox *tox, const uint8_t *public_key,
if (rc == -1) if (rc == -1)
{ {
weechat_printf_date_tags(profile->buffer, 0, "notify_highlight", weechat_printf_date_tags(profile->buffer, 0, "notify_highlight",
"%sReceived a friend request, but your friend request list is full!", "%sReceived a friend request, but your friend "
weechat_prefix("warning")); "request list is full!",
weechat_prefix("warning"));
} }
else else
{ {
char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(public_key, TOX_PUBLIC_KEY_SIZE, hex_address); twc_bin2hex(public_key, TOX_PUBLIC_KEY_SIZE, hex_address);
weechat_printf_date_tags(profile->buffer, 0, "notify_highlight", weechat_printf_date_tags(
"%sReceived a friend request from %s with message \"%s\"; " profile->buffer, 0, "notify_highlight",
"accept it with \"/friend accept %d\"", "%sReceived a friend request from %s with message \"%s\"; "
weechat_prefix("network"), "accept it with \"/friend accept %d\"",
hex_address, message_nt, rc); weechat_prefix("network"), hex_address, message_nt, rc);
if (rc == -2) if (rc == -2)
{ {
weechat_printf_date_tags(profile->buffer, 0, "notify_highlight", weechat_printf_date_tags(
"%sFailed to save friend request, try manually " profile->buffer, 0, "notify_highlight",
"accepting with /friend add", "%sFailed to save friend request, try manually "
weechat_prefix("error")); "accepting with /friend add",
weechat_prefix("error"));
} }
} }
@ -240,16 +222,15 @@ twc_friend_request_callback(Tox *tox, const uint8_t *public_key,
} }
void void
twc_group_invite_callback(Tox *tox, twc_group_invite_callback(Tox *tox, uint32_t friend_number,
uint32_t friend_number, TOX_CONFERENCE_TYPE type, TOX_CONFERENCE_TYPE type, const uint8_t *invite_data,
const uint8_t *invite_data, size_t length, size_t length, void *data)
void *data)
{ {
TOX_ERR_CONFERENCE_JOIN err = TOX_ERR_CONFERENCE_JOIN_OK; TOX_ERR_CONFERENCE_JOIN err = TOX_ERR_CONFERENCE_JOIN_OK;
struct t_twc_profile *profile = data; struct t_twc_profile *profile = data;
char *friend_name = twc_get_name_nt(profile->tox, friend_number); char *friend_name = twc_get_name_nt(profile->tox, friend_number);
struct t_twc_chat *friend_chat struct t_twc_chat *friend_chat =
= twc_chat_search_friend(profile, friend_number, false); twc_chat_search_friend(profile, friend_number, false);
int64_t rc; int64_t rc;
char *tags; char *tags;
@ -257,11 +238,14 @@ twc_group_invite_callback(Tox *tox,
switch (type) switch (type)
{ {
case TOX_CONFERENCE_TYPE_TEXT: case TOX_CONFERENCE_TYPE_TEXT:
type_str = "a text-only group chat"; break; type_str = "a text-only group chat";
break;
case TOX_CONFERENCE_TYPE_AV: case TOX_CONFERENCE_TYPE_AV:
type_str = "an audio/vikdeo group chat"; break; type_str = "an audio/vikdeo group chat";
break;
default: default:
type_str = "a group chat"; break; type_str = "a group chat";
break;
} }
if (TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_AUTOJOIN)) if (TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_AUTOJOIN))
@ -269,14 +253,13 @@ twc_group_invite_callback(Tox *tox,
switch (type) switch (type)
{ {
case TOX_CONFERENCE_TYPE_TEXT: case TOX_CONFERENCE_TYPE_TEXT:
rc = tox_conference_join(tox, friend_number, rc = tox_conference_join(tox, friend_number, invite_data,
invite_data, length, &err); length, &err);
break; break;
#ifdef TOXAV_ENABLED #ifdef TOXAV_ENABLED
case TOX_CONFERENCE_TYPE_AV: case TOX_CONFERENCE_TYPE_AV:
rc = toxav_join_av_groupchat(tox, friend_number, rc = toxav_join_av_groupchat(tox, friend_number, invite_data,
invite_data, length, length, NULL, NULL);
NULL, NULL);
break; break;
#endif #endif
default: default:
@ -289,38 +272,38 @@ twc_group_invite_callback(Tox *tox,
tags = "notify_private"; tags = "notify_private";
if (friend_chat) if (friend_chat)
{ {
weechat_printf_date_tags(friend_chat->buffer, 0, tags, weechat_printf_date_tags(
"%sWe joined the %s%s%s's invite to %s.", friend_chat->buffer, 0, tags,
weechat_prefix("network"), "%sWe joined the %s%s%s's invite to %s.",
weechat_color("chat_nick_other"), friend_name, weechat_prefix("network"), weechat_color("chat_nick_other"),
weechat_color("reset"), type_str); friend_name, weechat_color("reset"), type_str);
tags = ""; tags = "";
} }
weechat_printf_date_tags(profile->buffer, 0, tags, weechat_printf_date_tags(
"%sWe joined the %s%s%s's invite to %s.", profile->buffer, 0, tags,
weechat_prefix("network"), "%sWe joined the %s%s%s's invite to %s.",
weechat_color("chat_nick_other"), friend_name, weechat_prefix("network"), weechat_color("chat_nick_other"),
weechat_color("reset"), type_str); friend_name, weechat_color("reset"), type_str);
} }
else else
{ {
tags = "notify_highlight"; tags = "notify_highlight";
if (friend_chat) if (friend_chat)
{ {
weechat_printf_date_tags(friend_chat->buffer, 0, tags, weechat_printf_date_tags(
"%s%s%s%s invites you to join %s, but we failed to " friend_chat->buffer, 0, tags,
"process the invite. Please try again.", "%s%s%s%s invites you to join %s, but we failed to "
weechat_prefix("network"), "process the invite. Please try again.",
weechat_color("chat_nick_other"), friend_name, weechat_prefix("network"), weechat_color("chat_nick_other"),
weechat_color("reset")); friend_name, weechat_color("reset"));
tags = ""; tags = "";
} }
weechat_printf_date_tags(profile->buffer, 0, tags, weechat_printf_date_tags(
"%s%s%s%s invites you to join %s, but we failed to " profile->buffer, 0, tags,
"process the invite. Please try again.", "%s%s%s%s invites you to join %s, but we failed to "
weechat_prefix("network"), "process the invite. Please try again.",
weechat_color("chat_nick_other"), friend_name, weechat_prefix("network"), weechat_color("chat_nick_other"),
weechat_color("reset")); friend_name, weechat_color("reset"));
} }
} }
else else
@ -333,59 +316,56 @@ twc_group_invite_callback(Tox *tox,
tags = "notify_highlight"; tags = "notify_highlight";
if (friend_chat) if (friend_chat)
{ {
weechat_printf_date_tags(friend_chat->buffer, 0, tags, weechat_printf_date_tags(
"%s%s%s%s invites you to join %s. Type " friend_chat->buffer, 0, tags,
"\"/group join %d\" to accept.", "%s%s%s%s invites you to join %s. Type "
weechat_prefix("network"), "\"/group join %d\" to accept.",
weechat_color("chat_nick_other"), friend_name, weechat_prefix("network"), weechat_color("chat_nick_other"),
weechat_color("reset"), type_str, rc); friend_name, weechat_color("reset"), type_str, rc);
tags = ""; tags = "";
} }
weechat_printf_date_tags(profile->buffer, 0, tags, weechat_printf_date_tags(
"%s%s%s%s invites you to join %s. Type " profile->buffer, 0, tags,
"\"/group join %d\" to accept.", "%s%s%s%s invites you to join %s. Type "
weechat_prefix("network"), "\"/group join %d\" to accept.",
weechat_color("chat_nick_other"), friend_name, weechat_prefix("network"), weechat_color("chat_nick_other"),
weechat_color("reset"), type_str, rc); friend_name, weechat_color("reset"), type_str, rc);
} }
else else
{ {
tags = "notify_highlight"; tags = "notify_highlight";
if (friend_chat) if (friend_chat)
{ {
weechat_printf_date_tags(friend_chat->buffer, 0, tags, weechat_printf_date_tags(
"%s%s%s%s invites you to join %s, but we failed to " friend_chat->buffer, 0, tags,
"process the invite with error %d. Please try again.", "%s%s%s%s invites you to join %s, but we failed to "
weechat_prefix("network"), "process the invite with error %d. Please try again.",
weechat_color("chat_nick_other"), friend_name, weechat_prefix("network"), weechat_color("chat_nick_other"),
weechat_color("reset"), rc, err); friend_name, weechat_color("reset"), rc, err);
tags = ""; tags = "";
} }
weechat_printf_date_tags(profile->buffer, 0, tags, weechat_printf_date_tags(
"%s%s%s%s invites you to join %s, but we failed to " profile->buffer, 0, tags,
"process the invite. Please try again.", "%s%s%s%s invites you to join %s, but we failed to "
weechat_prefix("network"), "process the invite. Please try again.",
weechat_color("chat_nick_other"), friend_name, weechat_prefix("network"), weechat_color("chat_nick_other"),
weechat_color("reset"), rc); friend_name, weechat_color("reset"), rc);
} }
} }
free(friend_name); free(friend_name);
} }
void void
twc_handle_group_message(Tox *tox, twc_handle_group_message(Tox *tox, int32_t group_number, int32_t peer_number,
int32_t group_number, int32_t peer_number, const uint8_t *message, uint16_t length, void *data,
const uint8_t *message, uint16_t length,
void *data,
TOX_MESSAGE_TYPE message_type) TOX_MESSAGE_TYPE message_type)
{ {
TOX_ERR_CONFERENCE_PEER_QUERY err = TOX_ERR_CONFERENCE_PEER_QUERY_OK; TOX_ERR_CONFERENCE_PEER_QUERY err = TOX_ERR_CONFERENCE_PEER_QUERY_OK;
bool rc; bool rc;
struct t_twc_profile *profile = data; struct t_twc_profile *profile = data;
struct t_twc_chat *chat = twc_chat_search_group(profile, struct t_twc_chat *chat =
group_number, twc_chat_search_group(profile, group_number, true);
true);
char *myname = twc_get_self_name_nt(profile->tox); char *myname = twc_get_self_name_nt(profile->tox);
char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number);
@ -393,7 +373,8 @@ twc_handle_group_message(Tox *tox,
char *message_nt = twc_null_terminate(message, length); char *message_nt = twc_null_terminate(message, length);
const char *nick_color; const char *nick_color;
rc = tox_conference_peer_number_is_ours(tox, group_number, peer_number, &err); rc = tox_conference_peer_number_is_ours(tox, group_number, peer_number,
&err);
if (rc && (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK)) if (rc && (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK))
nick_color = weechat_color("chat_nick_self"); nick_color = weechat_color("chat_nick_self");
else else
@ -401,8 +382,8 @@ twc_handle_group_message(Tox *tox,
if (weechat_string_has_highlight(message_nt, myname)) if (weechat_string_has_highlight(message_nt, myname))
tags = "notify_highlight"; tags = "notify_highlight";
twc_chat_print_message(chat, tags, nick_color, name, twc_chat_print_message(chat, tags, nick_color, name, message_nt,
message_nt, message_type); message_type);
free(name); free(name);
free(myname); free(myname);
@ -410,30 +391,23 @@ twc_handle_group_message(Tox *tox,
} }
void void
twc_group_message_callback(Tox *tox, twc_group_message_callback(Tox *tox, uint32_t group_number,
uint32_t group_number, uint32_t peer_number, uint32_t peer_number, TOX_MESSAGE_TYPE type,
TOX_MESSAGE_TYPE type, const uint8_t *message, const uint8_t *message, size_t length, void *data)
size_t length, void *data)
{ {
twc_handle_group_message(tox, twc_handle_group_message(tox, group_number, peer_number, message, length,
group_number, data, type);
peer_number,
message,
length,
data,
type);
} }
void void
twc_group_namelist_change_callback(Tox *tox, twc_group_namelist_change_callback(Tox *tox, uint32_t group_number,
uint32_t group_number, uint32_t peer_number, uint32_t peer_number,
TOX_CONFERENCE_STATE_CHANGE change_type, TOX_CONFERENCE_STATE_CHANGE change_type,
void *data) void *data)
{ {
struct t_twc_profile *profile = data; struct t_twc_profile *profile = data;
struct t_twc_chat *chat = twc_chat_search_group(profile, struct t_twc_chat *chat =
group_number, twc_chat_search_group(profile, group_number, true);
true);
struct t_gui_nick *nick = NULL; struct t_gui_nick *nick = NULL;
int i, npeers; int i, npeers;
@ -457,7 +431,7 @@ twc_group_namelist_change_callback(Tox *tox,
else else
return; return;
// searching for exits /* searching for exits */
n = weechat_list_get(chat->nicks, 0); n = weechat_list_get(chat->nicks, 0);
while (n) while (n)
@ -468,14 +442,13 @@ twc_group_namelist_change_callback(Tox *tox,
weechat_printf(chat->buffer, "%s%s just left the group chat", weechat_printf(chat->buffer, "%s%s just left the group chat",
weechat_prefix("quit"), name); weechat_prefix("quit"), name);
nick = weechat_nicklist_search_nick(chat->buffer, nick = weechat_nicklist_search_nick(chat->buffer,
chat->nicklist_group, chat->nicklist_group, name);
name);
weechat_nicklist_remove_nick(chat->buffer, nick); weechat_nicklist_remove_nick(chat->buffer, nick);
} }
n = weechat_list_next(n); n = weechat_list_next(n);
} }
// searching for joins /* searching for joins */
n = weechat_list_get(new_nicks, 0); n = weechat_list_get(new_nicks, 0);
while (n) while (n)
@ -485,8 +458,8 @@ twc_group_namelist_change_callback(Tox *tox,
{ {
weechat_printf(chat->buffer, "%s%s just joined the group chat", weechat_printf(chat->buffer, "%s%s just joined the group chat",
weechat_prefix("join"), name); weechat_prefix("join"), name);
weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, name,
name, NULL, NULL, NULL, 1); NULL, NULL, NULL, 1);
} }
n = weechat_list_next(n); n = weechat_list_next(n);
} }
@ -497,15 +470,12 @@ twc_group_namelist_change_callback(Tox *tox,
} }
void void
twc_group_title_callback(Tox *tox, twc_group_title_callback(Tox *tox, uint32_t group_number, uint32_t peer_number,
uint32_t group_number, uint32_t peer_number, const uint8_t *title, size_t length, void *data)
const uint8_t *title, size_t length,
void *data)
{ {
struct t_twc_profile *profile = data; struct t_twc_profile *profile = data;
struct t_twc_chat *chat = twc_chat_search_group(profile, struct t_twc_chat *chat =
group_number, twc_chat_search_group(profile, group_number, true);
true);
twc_chat_queue_refresh(chat); twc_chat_queue_refresh(chat);
char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number);
@ -518,10 +488,9 @@ twc_group_title_callback(Tox *tox,
#ifndef NDEBUG #ifndef NDEBUG
void void
twc_tox_log_callback(Tox *tox, twc_tox_log_callback(Tox *tox, TOX_LOG_LEVEL level, const char *file,
TOX_LOG_LEVEL level, uint32_t line, const char *func, const char *message,
const char *file, uint32_t line, const char *func, void *user_data)
const char *message, void *user_data)
{ {
struct t_twc_profile *const profile = user_data; struct t_twc_profile *const profile = user_data;
@ -547,9 +516,8 @@ twc_tox_log_callback(Tox *tox,
color = weechat_color("reset"); color = weechat_color("reset");
} }
weechat_printf(profile->buffer, "%stox\t%s%s:%"PRIu32" [%s]%s %s", weechat_printf(profile->buffer, "%stox\t%s%s:%" PRIu32 " [%s]%s %s", color,
color, weechat_color("reset"), file, line, func, weechat_color("reset"), file, line, func,
weechat_color("lightred"), message); weechat_color("lightred"), message);
} }
#endif /* !NDEBUG */ #endif /* !NDEBUG */

View File

@ -23,8 +23,7 @@
#include <tox/tox.h> #include <tox/tox.h>
int int
twc_do_timer_cb(const void *pointer, void *data, twc_do_timer_cb(const void *pointer, void *data, int remaining_calls);
int remaining_calls);
void void
twc_friend_message_callback(Tox *tox, uint32_t friend_number, twc_friend_message_callback(Tox *tox, uint32_t friend_number,
@ -36,9 +35,8 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number,
TOX_CONNECTION status, void *data); TOX_CONNECTION status, void *data);
void void
twc_name_change_callback(Tox *tox, uint32_t friend_number, twc_name_change_callback(Tox *tox, uint32_t friend_number, const uint8_t *name,
const uint8_t *name, size_t length, size_t length, void *data);
void *data);
void void
twc_user_status_callback(Tox *tox, uint32_t friend_number, twc_user_status_callback(Tox *tox, uint32_t friend_number,
@ -46,45 +44,37 @@ twc_user_status_callback(Tox *tox, uint32_t friend_number,
void void
twc_status_message_callback(Tox *tox, uint32_t friend_number, twc_status_message_callback(Tox *tox, uint32_t friend_number,
const uint8_t *message, size_t length, const uint8_t *message, size_t length, void *data);
void *data);
void void
twc_friend_request_callback(Tox *tox, const uint8_t *public_key, twc_friend_request_callback(Tox *tox, const uint8_t *public_key,
const uint8_t *message, size_t length, const uint8_t *message, size_t length, void *data);
void *data);
void void
twc_group_invite_callback(Tox *tox, twc_group_invite_callback(Tox *tox, uint32_t friend_number,
uint32_t friend_number, TOX_CONFERENCE_TYPE type, TOX_CONFERENCE_TYPE type, const uint8_t *invite_data,
const uint8_t *invite_data, size_t length, size_t length, void *data);
void *data);
void void
twc_group_message_callback(Tox *tox, twc_group_message_callback(Tox *tox, uint32_t group_number,
uint32_t group_number, uint32_t peer_number, uint32_t peer_number, TOX_MESSAGE_TYPE type,
TOX_MESSAGE_TYPE type, const uint8_t *message, const uint8_t *message, size_t length, void *data);
size_t length, void *data);
void void
twc_group_namelist_change_callback(Tox *tox, twc_group_namelist_change_callback(Tox *tox, uint32_t group_number,
uint32_t group_number, uint32_t peer_number, uint32_t peer_number,
TOX_CONFERENCE_STATE_CHANGE change_type, TOX_CONFERENCE_STATE_CHANGE change_type,
void *data); void *data);
void void
twc_group_title_callback(Tox *tox, twc_group_title_callback(Tox *tox, uint32_t group_number, uint32_t peer_number,
uint32_t group_number, uint32_t peer_number, const uint8_t *title, size_t length, void *data);
const uint8_t *title, size_t length,
void *data);
#ifndef NDEBUG #ifndef NDEBUG
void void
twc_tox_log_callback(Tox *tox, twc_tox_log_callback(Tox *tox, TOX_LOG_LEVEL level, const char *file,
TOX_LOG_LEVEL level, uint32_t line, const char *func, const char *message,
const char *file, uint32_t line, const char *func, void *user_data);
const char *message, void *user_data);
#endif /* !NDEBUG */ #endif /* !NDEBUG */
#endif // TOX_WEECHAT_TOX_CALLBACKS_H #endif /* TOX_WEECHAT_TOX_CALLBACKS_H */

View File

@ -17,14 +17,14 @@
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>. * along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#include "twc.h"
#include "twc-config.h" #include "twc-config.h"
#include "twc.h"
#include "twc-utils.h" #include "twc-utils.h"
@ -84,9 +84,8 @@ twc_get_name_nt(Tox *tox, int32_t friend_number)
TOX_ERR_FRIEND_QUERY err; TOX_ERR_FRIEND_QUERY err;
size_t length = tox_friend_get_name_size(tox, friend_number, &err); size_t length = tox_friend_get_name_size(tox, friend_number, &err);
if ((err != TOX_ERR_FRIEND_QUERY_OK) || if ((err != TOX_ERR_FRIEND_QUERY_OK) || (length == 0))
(length == 0)) return twc_get_friend_id_short(tox, friend_number);
return twc_get_friend_id_short(tox, friend_number);
uint8_t name[length]; uint8_t name[length];
@ -101,13 +100,14 @@ char *
twc_get_status_message_nt(Tox *tox, int32_t friend_number) twc_get_status_message_nt(Tox *tox, int32_t friend_number)
{ {
TOX_ERR_FRIEND_QUERY err; TOX_ERR_FRIEND_QUERY err;
size_t length = tox_friend_get_status_message_size(tox, friend_number, &err); size_t length =
tox_friend_get_status_message_size(tox, friend_number, &err);
if ((err != TOX_ERR_FRIEND_QUERY_OK) || if ((err != TOX_ERR_FRIEND_QUERY_OK) || (length == SIZE_MAX))
(length == SIZE_MAX)) { {
char *msg = malloc(1); char *msg = malloc(1);
*msg = 0; *msg = 0;
return msg; return msg;
} }
uint8_t message[length]; uint8_t message[length];
@ -123,13 +123,16 @@ twc_get_status_message_nt(Tox *tox, int32_t friend_number)
char * char *
twc_get_peer_name_nt(Tox *tox, int32_t group_number, int32_t peer_number) twc_get_peer_name_nt(Tox *tox, int32_t group_number, int32_t peer_number)
{ {
uint8_t name[TOX_MAX_NAME_LENGTH+1] = {0}; uint8_t name[TOX_MAX_NAME_LENGTH + 1] = {0};
TOX_ERR_CONFERENCE_PEER_QUERY err = TOX_ERR_CONFERENCE_PEER_QUERY_OK; TOX_ERR_CONFERENCE_PEER_QUERY err = TOX_ERR_CONFERENCE_PEER_QUERY_OK;
int length = tox_conference_peer_get_name_size(tox, group_number, peer_number, &err); int length =
if ((err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) && (length <= TOX_MAX_NAME_LENGTH)) tox_conference_peer_get_name_size(tox, group_number, peer_number, &err);
if ((err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) &&
(length <= TOX_MAX_NAME_LENGTH))
{ {
tox_conference_peer_get_name(tox, group_number, peer_number, name, &err); tox_conference_peer_get_name(tox, group_number, peer_number, name,
&err);
if (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) if (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK)
return twc_null_terminate(name, length); return twc_null_terminate(name, length);
else else
@ -165,13 +168,11 @@ twc_get_friend_id_short(Tox *tox, int32_t friend_number)
tox_friend_get_public_key(tox, friend_number, client_id, &err); tox_friend_get_public_key(tox, friend_number, client_id, &err);
// return a zero public key on failure /* return a zero public key on failure */
if (err != TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK) if (err != TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK)
memset(client_id, 0, TOX_PUBLIC_KEY_SIZE); memset(client_id, 0, TOX_PUBLIC_KEY_SIZE);
twc_bin2hex(client_id, twc_bin2hex(client_id, short_id_length / 2, hex_address);
short_id_length / 2,
hex_address);
return hex_address; return hex_address;
} }
@ -184,9 +185,15 @@ twc_uint32_reverse_bytes(uint32_t num)
{ {
uint32_t res = 0; uint32_t res = 0;
res += num & 0xFF; num >>= 8; res <<= 8; res += num & 0xFF;
res += num & 0xFF; num >>= 8; res <<= 8; num >>= 8;
res += num & 0xFF; num >>= 8; res <<= 8; res <<= 8;
res += num & 0xFF;
num >>= 8;
res <<= 8;
res += num & 0xFF;
num >>= 8;
res <<= 8;
res += num & 0xFF; res += num & 0xFF;
return res; return res;
@ -222,6 +229,6 @@ twc_set_buffer_logging(struct t_gui_buffer *buffer, bool logging)
signal = "logger_stop"; signal = "logger_stop";
} }
return weechat_hook_signal_send(signal, return weechat_hook_signal_send(signal, WEECHAT_HOOK_SIGNAL_POINTER,
WEECHAT_HOOK_SIGNAL_POINTER, buffer); buffer);
} }

View File

@ -22,8 +22,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <weechat/weechat-plugin.h>
void void
twc_hex2bin(const char *hex, size_t size, uint8_t *out); twc_hex2bin(const char *hex, size_t size, uint8_t *out);
@ -58,5 +58,4 @@ twc_fit_utf8(const char *str, int max);
int int
twc_set_buffer_logging(struct t_gui_buffer *buffer, bool logging); twc_set_buffer_logging(struct t_gui_buffer *buffer, bool logging);
#endif // TOX_WEECHAT_UTILS_H #endif /* TOX_WEECHAT_UTILS_H */

View File

@ -21,11 +21,11 @@
#include <weechat/weechat-plugin.h> #include <weechat/weechat-plugin.h>
#include "twc-profile.h"
#include "twc-commands.h" #include "twc-commands.h"
#include "twc-gui.h"
#include "twc-config.h"
#include "twc-completion.h" #include "twc-completion.h"
#include "twc-config.h"
#include "twc-gui.h"
#include "twc-profile.h"
#include "twc.h" #include "twc.h"
@ -53,8 +53,8 @@ weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[])
bool no_autoconnect = false; bool no_autoconnect = false;
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
{ {
if (weechat_strcasecmp(argv[i], "-a") == 0 if (weechat_strcasecmp(argv[i], "-a") == 0 ||
|| weechat_strcasecmp(argv[i], "--no-connect") == 0) weechat_strcasecmp(argv[i], "--no-connect") == 0)
{ {
no_autoconnect = true; no_autoconnect = true;
} }
@ -74,4 +74,3 @@ weechat_plugin_end(struct t_weechat_plugin *plugin)
return WEECHAT_RC_OK; return WEECHAT_RC_OK;
} }

View File

@ -22,18 +22,17 @@
extern struct t_weechat_plugin *weechat_plugin; extern struct t_weechat_plugin *weechat_plugin;
/// Return codes used throughout Tox-WeeChat. /** Return codes used throughout Tox-WeeChat. */
enum t_twc_rc enum t_twc_rc
{ {
TWC_RC_OK = 0, TWC_RC_OK = 0,
/// Generic error return code. /* Generic error return code. */
TWC_RC_ERROR = -1, TWC_RC_ERROR = -1,
/// Malloc error return code. /* Malloc error return code. */
TWC_RC_ERROR_MALLOC = -2, TWC_RC_ERROR_MALLOC = -2,
}; };
#define TWC_MAX_FRIEND_MESSAGE_LENGTH (TOX_MAX_MESSAGE_LENGTH-1) #define TWC_MAX_FRIEND_MESSAGE_LENGTH (TOX_MAX_MESSAGE_LENGTH - 1)
#define TWC_MAX_GROUP_MESSAGE_LENGTH (TOX_MAX_MESSAGE_LENGTH-16) #define TWC_MAX_GROUP_MESSAGE_LENGTH (TOX_MAX_MESSAGE_LENGTH - 16)
#endif // TOX_WEECHAT_H
#endif /* TOX_WEECHAT_H */