diff -Nru tox-weechat-0.1+20150104.r247/cmake/FindTox.cmake tox-weechat-0.1+20150410.r255/cmake/FindTox.cmake --- tox-weechat-0.1+20150104.r247/cmake/FindTox.cmake 1970-01-01 00:00:00.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/cmake/FindTox.cmake 2015-04-09 22:08:40.000000000 +0000 @@ -0,0 +1,8 @@ +find_path(TOX_INCLUDE_DIR tox/tox.h) +find_path(TOXAV_INCLUDE_DIR tox/toxav.h) + +find_library(TOX_LIBRARY toxcore) +find_library(TOXAV_LIBRARY toxav) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Tox DEFAULT_MSG TOX_INCLUDE_DIR TOXAV_INCLUDE_DIR TOX_LIBRARY TOXAV_LIBRARY) diff -Nru tox-weechat-0.1+20150104.r247/cmake/FindWeeChat.cmake tox-weechat-0.1+20150410.r255/cmake/FindWeeChat.cmake --- tox-weechat-0.1+20150104.r247/cmake/FindWeeChat.cmake 1970-01-01 00:00:00.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/cmake/FindWeeChat.cmake 2015-04-09 22:08:40.000000000 +0000 @@ -0,0 +1,4 @@ +find_path(WEECHAT_INCLUDE_DIR weechat/weechat-plugin.h) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(WeeChat DEFAULT_MSG WEECHAT_INCLUDE_DIR) diff -Nru tox-weechat-0.1+20150104.r247/CMakeLists.txt tox-weechat-0.1+20150410.r255/CMakeLists.txt --- tox-weechat-0.1+20150104.r247/CMakeLists.txt 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/CMakeLists.txt 2015-04-09 22:08:40.000000000 +0000 @@ -21,6 +21,10 @@ project(tox-weechat C) +set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) +find_package(WeeChat) +find_package(Tox) + set(PLUGIN_PATH "lib/weechat/plugins" CACHE PATH "Path to install the plugin binary to.") @@ -37,7 +41,6 @@ src/twc-list.c src/twc-message-queue.c src/twc-profile.c - src/twc-sqlite.c src/twc-tox-callbacks.c src/twc-utils.c ) @@ -45,9 +48,11 @@ set(CMAKE_C_FLAGS_DEBUG "-DTWC_DEBUG") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -Wextra -Wno-unused-parameter") -target_link_libraries(tox toxcore) -target_link_libraries(tox toxav) -target_link_libraries(tox sqlite3) +include_directories(${TOX_INCLUDE_DIR}) +include_directories(${WEECHAT_INCLUDE_DIR}) + +target_link_libraries(tox ${TOX_LIBRARY}) +target_link_libraries(tox ${TOXAV_LIBRARY}) # remove lib prefix (libtox.so -> tox.so) set_target_properties(tox PROPERTIES PREFIX "") diff -Nru tox-weechat-0.1+20150104.r247/debian/changelog tox-weechat-0.1+20150410.r255/debian/changelog --- tox-weechat-0.1+20150104.r247/debian/changelog 2015-01-04 23:25:28.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/debian/changelog 2015-04-14 19:11:23.000000000 +0000 @@ -1,8 +1,34 @@ -tox-weechat (0.1+20150104.r247-1ubuntu1) utopic; urgency=medium +tox-weechat (0.1+20150410.r255-1ubuntu1) vivid; urgency=medium - * Building for Utopic + * Building for Vivid - -- Kill Your TV Sun, 04 Jan 2015 23:25:19 +0000 + -- Kill Your TV Tue, 14 Apr 2015 19:11:13 +0000 + +tox-weechat (0.1+20150410.r255-1) unstable; urgency=medium + + [ Håvard Pettersson ] + * Added CMake find modules, fixing build on FreeBSD. + * Rename macros per the new core API. + * Update Tox_Options use to match new API. + * asd + * Define function return codes. + * New API cleanup. + + [ Kill Your TV ] + * New upstream git snapshot + + -- Kill Your TV Tue, 14 Apr 2015 09:41:56 +0000 + +tox-weechat (0.1+20150106.r249-1) unstable; urgency=medium + + [ Håvard Pettersson ] + * Reflowed README. + * Got rid of SQLite. + + [ Kill Your TV ] + * New upsream git snapshot + + -- Kill Your TV Thu, 08 Jan 2015 10:14:11 +0000 tox-weechat (0.1+20150104.r247-1) unstable; urgency=medium diff -Nru tox-weechat-0.1+20150104.r247/debian/control tox-weechat-0.1+20150410.r255/debian/control --- tox-weechat-0.1+20150104.r247/debian/control 2015-01-04 23:25:28.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/debian/control 2015-04-14 19:11:23.000000000 +0000 @@ -2,7 +2,11 @@ Section: net Priority: optional Maintainer: Kill Your TV -Build-Depends: debhelper (>= 9), cmake, weechat-dev (>= 1.0.1-1~bpo70+1), libtoxcore-dev, libtoxav-dev, libsqlite3-dev +Build-Depends: debhelper (>= 9), cmake, + weechat-dev (>= 1.0.1-1~bpo70+1), + libtoxcore-dev (>=0.0.0+20150413.r3315), + libtoxav-dev (>= 0.0.0+20150413.r3315), + libsqlite3-dev Standards-Version: 3.9.6 Homepage: https://github.com/haavardp/tox-weechat Vcs-Git: git://killyourtv.i2p/git/tox-weechat.git diff -Nru tox-weechat-0.1+20150104.r247/README.md tox-weechat-0.1+20150410.r255/README.md --- tox-weechat-0.1+20150104.r247/README.md 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/README.md 2015-04-09 22:08:40.000000000 +0000 @@ -1,18 +1,18 @@ Tox-WeeChat =========== -Tox-WeeChat is a [Tox][1] protocol plugin for [WeeChat][2]. It is functional, but lacks certain features like Tox DNS. +Tox-WeeChat is a [Tox][1] protocol plugin for [WeeChat][2]. It is functional, +but lacks certain features like Tox DNS. Current build status: [![Build Status](https://travis-ci.org/haavardp/tox-weechat.svg?branch=master)][3] Features -------- -Below is a list of implemented features, as well as features that will be supported in the future. +Below is a list of implemented features, as well as a rough roadmap for the +future. - [x] One-to-one chats - [x] Group chats (text only) - - [x] Proxies - - [x] Faux offline messaging - - [x] NoSpam editing + - [x] Proxy support - [x] Multiple profiles - [ ] Tox DNS - [ ] Encrypted save files @@ -23,33 +23,43 @@ Installation ------------ -> Tox-WeeChat is available in the [AUR][4] and the [[haavard]][5] pacman repository. +> Tox-WeeChat is available in the [AUR][4] and the [[haavard]][5] pacman +repository. -Tox-WeeChat requires [WeeChat][2] >= 1.0.1, [SQLite][6] >= 3.6.19 and the latest-ish [libtoxcore][7]. It also requires CMake to be built. Installation is fairly simple; after getting the source, compile and install using CMake: +Tox-WeeChat requires [WeeChat][2] >= 1.0.1 and the latest-ish [libtoxcore][6]. +It also requires CMake to be built. Installation is fairly simple; after +getting the source, compile and install using CMake: $ mkdir build && cd build $ cmake -DPLUGIN_PATH=~/.weechat/plugins .. $ make install -This installs the plugin binary `tox.so` to the recommended location `~/.weechat/plugins`. The default location is `/usr/local/lib/weechat/plugins`. +This installs the plugin binary `tox.so` to the recommended location +`~/.weechat/plugins`. The default location is `/usr/local/lib/weechat/plugins`. Usage ----- - - 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. - - Create a new profile with `/tox create `. The data file is stored in `~/.weechat/tox/` by default. - - Load your profile and connect to the Tox network with `/tox load `. - - Run `/help -listfull tox` to get a list of all available commands, and `/set tox.*` for a list of options. + - 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. + - Create a new profile with `/tox create `. The data file is + stored in `~/.weechat/tox/` by default. + - Load your profile and connect to the Tox network with + `/tox load `. + - Run `/help -listfull tox` to get a list of all available commands, and + `/set tox.*` for a list of options. ### Common issues #### Long Tox names messing up WeeChat layout -Tox allows names up to 128 bytes long. To prevent long names from taking all your screen space, you can set the following options in WeeChat: +Tox allows names up to 128 bytes long. To prevent long names from taking all +your screen space, you can set the following options in WeeChat: - `weechat.bar.nicklist.size_max` - `weechat.look.prefix_align_max` - `buffers.look.name_size_max` (if using buffers.pl) #### Tox won't connect through my proxy -Make sure the address and port is correct, and that UDP is disabled (`/set tox.profile.*.udp`). +Make sure the proxy type, address and port is correct, and that UDP is +disabled (`/set tox.profile.*.udp`). License --------- @@ -75,6 +85,5 @@ [3]: https://travis-ci.org/haavardp/tox-weechat [4]: https://aur.archlinux.org/packages/tox-weechat-git [5]: https://haavard.me/archlinux -[6]: http://www.sqlite.org -[7]: https://github.com/irungentoo/toxcore +[6]: https://github.com/irungentoo/toxcore diff -Nru tox-weechat-0.1+20150104.r247/src/twc-bootstrap.c tox-weechat-0.1+20150410.r255/src/twc-bootstrap.c --- tox-weechat-0.1+20150104.r247/src/twc-bootstrap.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-bootstrap.c 2015-04-09 22:08:40.000000000 +0000 @@ -68,8 +68,8 @@ twc_bootstrap_tox(Tox *tox, const char *address, uint16_t port, const char *public_key) { - uint8_t binary_key[TOX_FRIEND_ADDRESS_SIZE]; - twc_hex2bin(public_key, TOX_FRIEND_ADDRESS_SIZE, binary_key); + uint8_t binary_key[TOX_ADDRESS_SIZE]; + twc_hex2bin(public_key, TOX_ADDRESS_SIZE, binary_key); int result = tox_bootstrap_from_address(tox, address, port, binary_key); diff -Nru tox-weechat-0.1+20150104.r247/src/twc.c tox-weechat-0.1+20150410.r255/src/twc.c --- tox-weechat-0.1+20150104.r247/src/twc.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc.c 2015-04-09 22:08:40.000000000 +0000 @@ -26,7 +26,6 @@ #include "twc-gui.h" #include "twc-config.h" #include "twc-completion.h" -#include "twc-sqlite.h" #include "twc.h" @@ -43,14 +42,6 @@ { weechat_plugin = plugin; - if (twc_sqlite_init() != 0) - { - weechat_printf(NULL, - "%s%s: failed to initialize persistent storage, some " - "data will not be saved", - weechat_prefix("error"), weechat_plugin->name); - } - twc_profile_init(); twc_commands_init(); twc_gui_init(); @@ -81,8 +72,6 @@ twc_profile_free_all(); - twc_sqlite_end(); - return WEECHAT_RC_OK; } diff -Nru tox-weechat-0.1+20150104.r247/src/twc-chat.c tox-weechat-0.1+20150410.r255/src/twc-chat.c --- tox-weechat-0.1+20150104.r247/src/twc-chat.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-chat.c 2015-04-09 22:08:40.000000000 +0000 @@ -59,7 +59,7 @@ twc_tox_id_compare_callback(struct t_hashtable *hashtable, const void *id1, const void *id2) { - return memcmp(id1, id2, TOX_CLIENT_ID_SIZE); + return memcmp(id1, id2, TOX_PUBLIC_KEY_SIZE); } /** @@ -102,11 +102,11 @@ struct t_twc_chat * twc_chat_new_friend(struct t_twc_profile *profile, int32_t friend_number) { - uint8_t client_id[TOX_CLIENT_ID_SIZE]; + uint8_t client_id[TOX_PUBLIC_KEY_SIZE]; tox_get_client_id(profile->tox, friend_number, client_id); - char buffer_name[TOX_CLIENT_ID_SIZE * 2 + 1]; - twc_bin2hex(client_id, TOX_CLIENT_ID_SIZE, buffer_name); + char buffer_name[TOX_PUBLIC_KEY_SIZE * 2 + 1]; + twc_bin2hex(client_id, TOX_PUBLIC_KEY_SIZE, buffer_name); struct t_twc_chat *chat = twc_chat_new(profile, buffer_name); if (chat) diff -Nru tox-weechat-0.1+20150104.r247/src/twc-commands.c tox-weechat-0.1+20150410.r255/src/twc-commands.c --- tox-weechat-0.1+20150104.r247/src/twc-commands.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-commands.c 2015-04-09 22:08:40.000000000 +0000 @@ -30,7 +30,6 @@ #include "twc-friend-request.h" #include "twc-group-invite.h" #include "twc-bootstrap.h" -#include "twc-sqlite.h" #include "twc-config.h" #include "twc-utils.h" @@ -158,13 +157,13 @@ size_t search_size = strlen(search_string); for (uint32_t i = 0; i < friend_count; ++i) { - if (search_size == TOX_CLIENT_ID_SIZE * 2) + if (search_size == TOX_PUBLIC_KEY_SIZE * 2) { - uint8_t tox_id[TOX_CLIENT_ID_SIZE]; - char hex_id[TOX_CLIENT_ID_SIZE * 2 + 1]; + uint8_t tox_id[TOX_PUBLIC_KEY_SIZE]; + char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1]; tox_get_client_id(profile->tox, friend_numbers[i], tox_id); - twc_bin2hex(tox_id, TOX_CLIENT_ID_SIZE, hex_id); + twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id); if (weechat_strcasecmp(hex_id, search_string) == 0) return friend_numbers[i]; @@ -285,7 +284,7 @@ if (!message) message = weechat_config_string(twc_config_friend_request_message); - if (strlen(hex_id) != TOX_FRIEND_ADDRESS_SIZE * 2) + if (strlen(hex_id) != TOX_ADDRESS_SIZE * 2) { weechat_printf(profile->buffer, "%sTox ID length invalid. Please try again.", @@ -294,13 +293,13 @@ return WEECHAT_RC_OK; } - uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; - twc_hex2bin(hex_id, TOX_FRIEND_ADDRESS_SIZE, address); + uint8_t address[TOX_ADDRESS_SIZE]; + twc_hex2bin(hex_id, TOX_ADDRESS_SIZE, address); if (force) { bool fail = false; - char *hex_key = strndup(hex_id, TOX_CLIENT_ID_SIZE * 2); + char *hex_key = strndup(hex_id, TOX_PUBLIC_KEY_SIZE * 2); int32_t friend_number = twc_match_friend(profile, hex_key); free(hex_key); @@ -319,50 +318,52 @@ } } - int32_t result = tox_add_friend(profile->tox, - (uint8_t *)address, - (uint8_t *)message, - strlen(message)); + TOX_ERR_FRIEND_ADD result = tox_add_friend(profile->tox, + (uint8_t *)address, + (uint8_t *)message, + strlen(message)); switch (result) { - case TOX_FAERR_TOOLONG: + case TOX_ERR_FRIEND_ADD_OK: + weechat_printf(profile->buffer, + "%sFriend request sent!", + weechat_prefix("network")); + break; + case TOX_ERR_FRIEND_ADD_TOO_LONG: weechat_printf(profile->buffer, "%sFriend request message too long! Try again.", weechat_prefix("error")); break; - case TOX_FAERR_ALREADYSENT: + case TOX_ERR_FRIEND_ADD_ALREADY_SENT: + case TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM: weechat_printf(profile->buffer, "%sYou have already sent a friend request to " "that address (use -force to circumvent)", weechat_prefix("error")); break; - case TOX_FAERR_OWNKEY: + case TOX_ERR_FRIEND_ADD_OWN_KEY: weechat_printf(profile->buffer, "%sYou can't add yourself as a friend.", weechat_prefix("error")); break; - case TOX_FAERR_BADCHECKSUM: + case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM: weechat_printf(profile->buffer, "%sInvalid friend address - try again.", weechat_prefix("error")); break; - case TOX_FAERR_NOMEM: + case TOX_ERR_FRIEND_ADD_MALLOC: weechat_printf(profile->buffer, "%sCould not add friend (out of memory).", weechat_prefix("error")); break; - case TOX_FAERR_UNKNOWN: - case TOX_FAERR_SETNEWNOSPAM: - case TOX_FAERR_NOMESSAGE: - weechat_printf(profile->buffer, - "%sCould not add friend (unknown error).", - weechat_prefix("error")); - break; + case TOX_ERR_FRIEND_ADD_NULL: + case TOX_ERR_FRIEND_ADD_NO_MESSAGE: /* this should not happen as we + validate the message */ default: weechat_printf(profile->buffer, - "%sFriend request sent!", - weechat_prefix("network")); + "%sCould not add friend (unknown error %d).", + weechat_prefix("error"), result); break; } @@ -404,10 +405,9 @@ struct t_twc_friend_request *request; if (weechat_strcasecmp(argv[2], "all") == 0) { - struct t_twc_list *requests = twc_sqlite_friend_requests(profile); size_t index; struct t_twc_list_item *item; - twc_list_foreach(requests, index, item) + twc_list_foreach(profile->friend_requests, index, item) { if (accept) twc_friend_request_accept(item->friend_request); @@ -435,9 +435,9 @@ return WEECHAT_RC_OK; } - char hex_address[TOX_CLIENT_ID_SIZE * 2 + 1]; + char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1]; twc_bin2hex(request->tox_id, - TOX_CLIENT_ID_SIZE, + TOX_PUBLIC_KEY_SIZE, hex_address); if (accept) @@ -464,11 +464,9 @@ "%sPending friend requests:", weechat_prefix("network")); - struct t_twc_list *friend_requests = twc_sqlite_friend_requests(profile); - size_t index; struct t_twc_list_item *item; - twc_list_foreach(friend_requests, index, item) + twc_list_foreach(profile->friend_requests, index, item) { size_t short_id_length = weechat_config_integer(twc_config_short_id_size); char hex_address[short_id_length + 1]; @@ -480,14 +478,10 @@ "%s[%d] Address: %s\n" "[%d] Message: %s", weechat_prefix("network"), - item->friend_request->request_id, - hex_address, - item->friend_request->request_id, - item->friend_request->message); + index, hex_address, + index, item->friend_request->message); } - twc_friend_request_free_list(friend_requests); - return WEECHAT_RC_OK; } @@ -698,11 +692,11 @@ TWC_CHECK_PROFILE(profile); TWC_CHECK_PROFILE_LOADED(profile); - uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; + uint8_t address[TOX_ADDRESS_SIZE]; tox_get_address(profile->tox, address); - char address_str[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; - twc_bin2hex(address, TOX_FRIEND_ADDRESS_SIZE, address_str); + char address_str[TOX_ADDRESS_SIZE * 2 + 1]; + twc_bin2hex(address, TOX_ADDRESS_SIZE, address_str); weechat_printf(profile->buffer, "%sYour Tox address: %s", @@ -883,15 +877,14 @@ TWC_CHECK_PROFILE(profile); TWC_CHECK_PROFILE_LOADED(profile); - TOX_USERSTATUS status = TOX_USERSTATUS_INVALID; + TOX_USER_STATUS status; if (weechat_strcasecmp(argv[1], "online") == 0) - status = TOX_USERSTATUS_NONE; + status = TOX_USER_STATUS_NONE; else if (weechat_strcasecmp(argv[1], "busy") == 0) - status = TOX_USERSTATUS_BUSY; + status = TOX_USER_STATUS_BUSY; else if (weechat_strcasecmp(argv[1], "away") == 0) - status = TOX_USERSTATUS_AWAY; - - if (status == TOX_USERSTATUS_INVALID) + status = TOX_USER_STATUS_AWAY; + else return WEECHAT_RC_ERROR; tox_set_user_status(profile->tox, status); diff -Nru tox-weechat-0.1+20150104.r247/src/twc-completion.c tox-weechat-0.1+20150410.r255/src/twc-completion.c --- tox-weechat-0.1+20150104.r247/src/twc-completion.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-completion.c 2015-04-09 22:08:40.000000000 +0000 @@ -65,11 +65,11 @@ { if (flags & TWC_COMPLETE_FRIEND_ID) { - uint8_t tox_id[TOX_CLIENT_ID_SIZE]; - char hex_id[TOX_CLIENT_ID_SIZE * 2 + 1]; + uint8_t tox_id[TOX_PUBLIC_KEY_SIZE]; + char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1]; tox_get_client_id(profile->tox, friend_numbers[i], tox_id); - twc_bin2hex(tox_id, TOX_CLIENT_ID_SIZE, hex_id); + twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id); weechat_hook_completion_list_add(completion, hex_id, 0, WEECHAT_LIST_POS_SORT); diff -Nru tox-weechat-0.1+20150104.r247/src/twc-config.c tox-weechat-0.1+20150410.r255/src/twc-config.c --- tox-weechat-0.1+20150104.r247/src/twc-config.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-config.c 2015-04-09 22:08:40.000000000 +0000 @@ -309,7 +309,7 @@ twc_config_file, twc_config_section_look, "short_id_size", "integer", "length of Tox IDs shown in short format; must be a multiple of two", - NULL, 2, TOX_CLIENT_ID_SIZE * 2, + NULL, 2, TOX_PUBLIC_KEY_SIZE * 2, "8", NULL, 0, twc_config_check_value_callback, NULL, NULL, NULL, NULL, NULL); diff -Nru tox-weechat-0.1+20150104.r247/src/twc-dns.c tox-weechat-0.1+20150410.r255/src/twc-dns.c --- tox-weechat-0.1+20150104.r247/src/twc-dns.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-dns.c 2015-04-09 22:08:40.000000000 +0000 @@ -52,16 +52,16 @@ { struct t_twc_dns_callback_info *callback_info = data; - char buffer[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; + char buffer[TOX_ADDRESS_SIZE * 2 + 1]; ssize_t size = read(fd, buffer, sizeof(buffer) - 1); buffer[size] = '\0'; if (size > 0) { - if (size == TOX_FRIEND_ADDRESS_SIZE * 2) + if (size == TOX_ADDRESS_SIZE * 2) { - uint8_t tox_id[TOX_FRIEND_ADDRESS_SIZE]; - twc_hex2bin(buffer, TOX_FRIEND_ADDRESS_SIZE, tox_id); + uint8_t tox_id[TOX_ADDRESS_SIZE]; + twc_hex2bin(buffer, TOX_ADDRESS_SIZE, tox_id); callback_info->callback(callback_info->data, TWC_DNS_RC_OK, tox_id); diff -Nru tox-weechat-0.1+20150104.r247/src/twc-friend-request.c tox-weechat-0.1+20150410.r255/src/twc-friend-request.c --- tox-weechat-0.1+20150104.r247/src/twc-friend-request.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-friend-request.c 2015-04-09 22:08:40.000000000 +0000 @@ -25,25 +25,24 @@ #include "twc.h" #include "twc-list.h" #include "twc-profile.h" -#include "twc-sqlite.h" #include "twc-utils.h" #include "twc-friend-request.h" /** - * Add a new friend request to a profile. Return it's index for accepting. + * Add a new friend request to a profile. * - * Returns ID on success, -1 on a full friend request list, -2 on other error. + * Returns index on success, -1 on a full friend request list and -2 for any + * other error. */ int twc_friend_request_add(struct t_twc_profile *profile, const uint8_t *client_id, const char *message) { - int max_request_count = + size_t max_request_count = TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS); - int current_request_count = twc_sqlite_friend_request_count(profile); - if (current_request_count >= max_request_count) + if (profile->friend_requests->count >= max_request_count) return -1; // create a new request @@ -54,14 +53,12 @@ request->profile = profile; request->message = strdup(message); - memcpy(request->tox_id, client_id, TOX_CLIENT_ID_SIZE); + memcpy(request->tox_id, client_id, TOX_PUBLIC_KEY_SIZE); - int rc = twc_sqlite_add_friend_request(profile, request); - - if (rc == -1) + if (!twc_list_item_new_data_add(profile->friend_requests, request)) return -2; - return rc; + return profile->friend_requests->count - 1; } /** @@ -80,8 +77,7 @@ void twc_friend_request_remove(struct t_twc_friend_request *request) { - twc_sqlite_delete_friend_request_with_id(request->profile, - request->request_id); + twc_list_remove_with_data(request->profile->friend_requests, request); } /** @@ -90,7 +86,7 @@ struct t_twc_friend_request * twc_friend_request_with_index(struct t_twc_profile *profile, int64_t index) { - return twc_sqlite_friend_request_with_id(profile, index); + return twc_list_get(profile->friend_requests, index)->friend_request; } /** diff -Nru tox-weechat-0.1+20150104.r247/src/twc-friend-request.h tox-weechat-0.1+20150410.r255/src/twc-friend-request.h --- tox-weechat-0.1+20150104.r247/src/twc-friend-request.h 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-friend-request.h 2015-04-09 22:08:40.000000000 +0000 @@ -31,8 +31,7 @@ { struct t_twc_profile *profile; - int request_id; - uint8_t tox_id[TOX_CLIENT_ID_SIZE]; + uint8_t tox_id[TOX_PUBLIC_KEY_SIZE]; char *message; }; diff -Nru tox-weechat-0.1+20150104.r247/src/twc-gui.c tox-weechat-0.1+20150410.r255/src/twc-gui.c --- tox-weechat-0.1+20150104.r247/src/twc-gui.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-gui.c 2015-04-09 22:08:40.000000000 +0000 @@ -43,10 +43,10 @@ char *status = NULL;; switch (tox_get_self_user_status(profile->tox)) { - case TOX_USERSTATUS_BUSY: + case TOX_USER_STATUS_BUSY: status = strdup("busy"); break; - case TOX_USERSTATUS_AWAY: + case TOX_USER_STATUS_AWAY: status = strdup("away"); break; } diff -Nru tox-weechat-0.1+20150104.r247/src/twc.h tox-weechat-0.1+20150410.r255/src/twc.h --- tox-weechat-0.1+20150104.r247/src/twc.h 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc.h 2015-04-09 22:08:40.000000000 +0000 @@ -22,5 +22,15 @@ extern struct t_weechat_plugin *weechat_plugin; +/// Return codes used throughout Tox-WeeChat. +enum t_twc_rc +{ + TWC_RC_OK, + /// Generic error return code. + TWC_RC_ERROR, + /// Malloc error return code. + TWC_RC_ERROR_MALLOC, +}; + #endif // TOX_WEECHAT_H diff -Nru tox-weechat-0.1+20150104.r247/src/twc-profile.c tox-weechat-0.1+20150410.r255/src/twc-profile.c --- tox-weechat-0.1+20150104.r247/src/twc-profile.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-profile.c 2015-04-09 22:08:40.000000000 +0000 @@ -34,7 +34,6 @@ #include "twc-message-queue.h" #include "twc-chat.h" #include "twc-tox-callbacks.h" -#include "twc-sqlite.h" #include "twc-utils.h" #include "twc-profile.h" @@ -62,42 +61,6 @@ } /** - * Try loading a Tox save file for a profile. - * - * Returns 0 on success, -1 on failure. - * - * TODO: support encrypted save files - */ -int -twc_profile_load_data(struct t_twc_profile *profile) -{ - char *full_path = twc_profile_expanded_data_path(profile); - FILE *file = fopen(full_path, "r"); - free(full_path); - - if (file) - { - // get file size - fseek(file, 0, SEEK_END); - size_t size = ftell(file); - rewind(file); - - // allocate a buffer and read file into it - uint8_t *data = malloc(sizeof(*data) * size); - fread(data, sizeof(uint8_t), size, file); - fclose(file); - - // try loading the data - int status = tox_load(profile->tox, data, size); - free(data); - - return status; - } - - return -1; -} - -/** * Save a profile's Tox data to disk. * * Returns 0 on success, -1 on failure. @@ -119,9 +82,9 @@ free(dir_path); // save Tox data to a buffer - uint32_t size = tox_size(profile->tox); + uint32_t size = tox_get_savedata_size(profile->tox); uint8_t *data = malloc(size); - tox_save(profile->tox, data); + tox_get_savedata(profile->tox, data); // save buffer to a file FILE *file = fopen(full_path, "w"); @@ -179,6 +142,7 @@ profile->tox_online = false; profile->chats = twc_list_new(); + profile->friend_requests = twc_list_new(); profile->group_chat_invites = twc_list_new(); profile->message_queues = weechat_hashtable_new(32, WEECHAT_HASHTABLE_INTEGER, @@ -192,14 +156,106 @@ } /** + * Load Tox options from WeeChat configuration files into a Tox_Options struct. + */ +void +twc_profile_set_options(struct Tox_Options *options, + struct t_twc_profile *profile) +{ + tox_options_default(options); + + const char *proxy_host = + TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_PROXY_ADDRESS); + if (proxy_host) + options->proxy_host = proxy_host; + + switch (TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_TYPE)) + { + case TWC_PROXY_NONE: + options->proxy_type = TOX_PROXY_TYPE_NONE; + break; + case TWC_PROXY_SOCKS5: + options->proxy_type = TOX_PROXY_TYPE_SOCKS5; + break; + case TWC_PROXY_HTTP: + options->proxy_type = TOX_PROXY_TYPE_HTTP; + break; + } + + options->proxy_port = + TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_PORT); + options->udp_enabled = + TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_UDP); + options->ipv6_enabled = + TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_IPV6); +} + +void +twc_tox_new_print_error(struct t_twc_profile *profile, + struct Tox_Options *options, + TOX_ERR_NEW error) +{ + switch (error) + { + case TOX_ERR_NEW_MALLOC: + weechat_printf(profile->buffer, + "%scould not load Tox (malloc error)", + weechat_prefix("error")); + break; + case TOX_ERR_NEW_PORT_ALLOC: + weechat_printf(profile->buffer, + "%scould not load Tox (failed to allocate a port)", + weechat_prefix("error")); + break; + case TOX_ERR_NEW_PROXY_BAD_TYPE: + weechat_printf(profile->buffer, + "%scould not load Tox (internal error; bad proxy type)", + weechat_prefix("error")); + break; + case TOX_ERR_NEW_PROXY_BAD_HOST: + weechat_printf(profile->buffer, + "%scould not load Tox (invalid proxy host: \"%s\")", + weechat_prefix("error"), options->proxy_host); + break; + case TOX_ERR_NEW_PROXY_BAD_PORT: + weechat_printf(profile->buffer, + "%scould not load Tox (invalid proxy port: \"%d\")", + weechat_prefix("error"), options->proxy_port); + break; + case TOX_ERR_NEW_PROXY_NOT_FOUND: + weechat_printf(profile->buffer, + "%scould not load Tox (proxy host not found: \"%s\")", + weechat_prefix("error"), options->proxy_host); + break; + case TOX_ERR_NEW_LOAD_ENCRYPTED: + weechat_printf(profile->buffer, + "%scould not load Tox (encrypted data files are not yet supported)", + weechat_prefix("error")); + break; + case TOX_ERR_NEW_LOAD_BAD_FORMAT: + weechat_printf(profile->buffer, + "%scould not load Tox (invalid data file, some data " + "may have been loaded; use -force to try using it)", + weechat_prefix("error")); + break; + default: + weechat_printf(profile->buffer, + "%scould not load Tox (unknown error %d)", + weechat_prefix("error"), error); + break; + } + +} + +/** * Load a profile's Tox object, creating a new one if it can't be loaded from * disk, and bootstraps the Tox DHT. */ -void +enum t_twc_rc twc_profile_load(struct t_twc_profile *profile) { if (profile->tox) - return; + return TWC_RC_ERROR; if (!(profile->buffer)) { @@ -208,101 +264,73 @@ NULL, NULL, twc_profile_buffer_close_callback, profile); if (!(profile->buffer)) - return; + return TWC_RC_ERROR; } weechat_printf(profile->buffer, - "%s%s: profile %s connecting", - weechat_prefix("network"), weechat_plugin->name, - profile->name); + "%s profile %s connecting", + weechat_prefix("network"), profile->name); // create Tox options object - Tox_Options options; + struct Tox_Options options; + twc_profile_set_options(&options, profile); - char *proxy_type; - switch (TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_TYPE)) + // print a proxy message + if (options.proxy_type != TOX_PROXY_TYPE_NONE) { - case TWC_PROXY_NONE: - options.proxy_type = TOX_PROXY_NONE; - break; - case TWC_PROXY_SOCKS5: - options.proxy_type = TOX_PROXY_SOCKS5; - proxy_type = "SOCKS5"; - break; - case TWC_PROXY_HTTP: - options.proxy_type = TOX_PROXY_HTTP; - proxy_type = "HTTP"; - break; + weechat_printf(profile->buffer, + "%susing %s proxy %s:%d", + weechat_prefix("network"), + options.proxy_type == TOX_PROXY_TYPE_HTTP ? "HTTP" : + TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" : + NULL, + options.proxy_host, options.proxy_port); } - const char *proxy_address = - TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_PROXY_ADDRESS); - if (proxy_address) - memcpy(options.proxy_address, proxy_address, strlen(proxy_address) + 1); + // try loading data file + char *path = twc_profile_expanded_data_path(profile); + uint8_t *data; + size_t data_size = 0; + enum t_twc_rc data_rc = twc_read_file(path, &data, &data_size); - options.proxy_port = - TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_PORT); - options.udp_disabled = - !TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_UDP); - options.ipv6enabled = - TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_IPV6); - - if (options.proxy_type != TOX_PROXY_NONE) + if (data_rc == TWC_RC_ERROR_MALLOC) { - if (!options.proxy_address || !options.proxy_port) - { - weechat_printf(profile->buffer, - "%sproxy is enabled, but proxy information is " - "incomplete; aborting", - weechat_prefix("error")); - return; - } - else - { - weechat_printf(profile->buffer, - "%susing %s proxy %s:%d", - weechat_prefix("network"), - proxy_type, - options.proxy_address, options.proxy_port); - } + weechat_printf(profile->buffer, + "%scould not load Tox data file, aborting (malloc error)", + weechat_prefix("error")); + return TWC_RC_ERROR_MALLOC; } // create Tox - profile->tox = tox_new(&options); - if (!(profile->tox)) + TOX_ERR_NEW rc; + profile->tox = tox_new(&options, data, data_size, &rc); + if (rc != TOX_ERR_NEW_OK) { - weechat_printf(profile->buffer, - "%scould not create Tox instance", - weechat_prefix("error")); - return; + twc_tox_new_print_error(profile, &options, rc); + return rc == TOX_ERR_NEW_MALLOC ? TWC_RC_ERROR_MALLOC : TWC_RC_ERROR; } - // try loading Tox saved data - // TODO: this can return -1 even if it does not fail - if (twc_profile_load_data(profile) == -1) + if (data_size == 0) { - // we failed to load - set some defaults - char *name; + // no data file loaded, set default name + const char *default_name = "Tox-WeeChat User"; + + const char *name; struct passwd *user_pwd; if ((user_pwd = getpwuid(geteuid()))) name = user_pwd->pw_name; else - name = "Tox-WeeChat User"; + name = default_name; - tox_set_name(profile->tox, - (uint8_t *)name, strlen(name)); - } - - // register with sqlite - twc_sqlite_add_profile(profile); - - int friend_request_count = twc_sqlite_friend_request_count(profile); - if (friend_request_count > 0) - { - weechat_printf(profile->buffer, - "%sYou have %d pending friend requests.", - weechat_prefix("network"), - friend_request_count); + TOX_ERR_SET_INFO rc; + tox_self_set_name(profile->tox, + (uint8_t *)name, strlen(name), + &rc); + + if (rc == TOX_ERR_SET_INFO_TOO_LONG) + tox_self_set_name(profile->tox, + (uint8_t *)default_name, strlen(default_name), + NULL); } // bootstrap DHT @@ -311,16 +339,15 @@ for (int i = 0; i < bootstrap_node_count; ++i) twc_bootstrap_random_node(profile->tox); - // start Tox_do loop + // start tox_iterate loop twc_do_timer_cb(profile, 0); // register Tox callbacks tox_callback_friend_message(profile->tox, twc_friend_message_callback, profile); - tox_callback_friend_action(profile->tox, twc_friend_action_callback, profile); - tox_callback_connection_status(profile->tox, twc_connection_status_callback, profile); - tox_callback_name_change(profile->tox, twc_name_change_callback, profile); - tox_callback_user_status(profile->tox, twc_user_status_callback, profile); - tox_callback_status_message(profile->tox, twc_status_message_callback, profile); + tox_callback_friend_connection_status(profile->tox, twc_connection_status_callback, profile); + tox_callback_friend_name(profile->tox, twc_name_change_callback, profile); + tox_callback_friend_status(profile->tox, twc_user_status_callback, profile); + tox_callback_friend_status_message(profile->tox, twc_status_message_callback, profile); tox_callback_friend_request(profile->tox, twc_friend_request_callback, profile); tox_callback_group_invite(profile->tox, twc_group_invite_callback, profile); tox_callback_group_message(profile->tox, twc_group_message_callback, profile); @@ -467,7 +494,6 @@ { char *data_path = twc_profile_expanded_data_path(profile); - twc_sqlite_delete_profile(profile); twc_profile_free(profile); if (delete_data) @@ -492,6 +518,7 @@ // free things twc_chat_free_list(profile->chats); + twc_friend_request_free_list(profile->friend_requests); twc_group_chat_invite_free_list(profile->group_chat_invites); twc_message_queue_free_profile(profile); free(profile->name); diff -Nru tox-weechat-0.1+20150104.r247/src/twc-profile.h tox-weechat-0.1+20150410.r255/src/twc-profile.h --- tox-weechat-0.1+20150104.r247/src/twc-profile.h 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-profile.h 2015-04-09 22:08:40.000000000 +0000 @@ -52,6 +52,7 @@ struct t_hook *tox_do_timer; struct t_twc_list *chats; + struct t_twc_list *friend_requests; struct t_twc_list *group_chat_invites; struct t_hashtable *message_queues; }; @@ -92,7 +93,7 @@ struct t_twc_profile * twc_profile_new(const char *name); -void +enum t_twc_rc twc_profile_load(struct t_twc_profile *profile); void diff -Nru tox-weechat-0.1+20150104.r247/src/twc-sqlite.c tox-weechat-0.1+20150410.r255/src/twc-sqlite.c --- tox-weechat-0.1+20150104.r247/src/twc-sqlite.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-sqlite.c 1970-01-01 00:00:00.000000000 +0000 @@ -1,425 +0,0 @@ -/* - * Copyright (c) 2015 Håvard Pettersson - * - * This file is part of Tox-WeeChat. - * - * 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 - * 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, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Tox-WeeChat. If not, see . - */ - -#include -#include - -#include -#include -#include - -#include "twc.h" -#include "twc-list.h" -#include "twc-profile.h" -#include "twc-friend-request.h" - -#include "twc-sqlite.h" - -sqlite3 *twc_sqlite_db = NULL; -struct t_twc_list *twc_sqlite_statements = NULL; - -#ifdef TWC_DEBUG -#define TWC_SQLITE_DEBUG_RC(rc, expected_rc) \ - if (rc != expected_rc) \ - weechat_printf(NULL, \ - "%s%s: SQLite error in %s: error code %d", \ - weechat_prefix("error"), weechat_plugin->name, \ - __func__, rc); -#else -#define TWC_SQLITE_DEBUG_RC(rc, expected_rc) (void)rc; -#endif // TWC_DEBUG - -/** - * Create or reset an SQLite statement. - */ -#define TWC_SQLITE_STMT(statement, statement_str) \ - static sqlite3_stmt *statement = NULL; \ - if (!statement) \ - { \ - int rc = sqlite3_prepare_v2(twc_sqlite_db, \ - statement_str, \ - strlen(statement_str) + 1, \ - &statement, \ - NULL); \ - TWC_SQLITE_DEBUG_RC(rc, SQLITE_OK); \ - if (rc != SQLITE_OK) \ - statement = NULL; \ - else \ - twc_list_item_new_data_add(twc_sqlite_statements, statement); \ - } \ - else \ - { \ - sqlite3_reset(statement); \ - } - -/** - * Return the full path to our SQLite database file. Must be freed. - */ -char * -twc_sqlite_db_path() -{ - const char *weechat_dir = weechat_info_get("weechat_dir", NULL); - return weechat_string_replace("%h/tox/data.db", "%h", weechat_dir); -} - -/** - * Initialize profile table. Return 0 on success, -1 on error. - */ -int -twc_sqlite_init_profiles() -{ - TWC_SQLITE_STMT(statement, - "CREATE TABLE IF NOT EXISTS profiles (" - "id INTEGER PRIMARY KEY," - "tox_id BLOB UNIQUE NOT NULL" - ")"); - - int rc = sqlite3_step(statement); - if (rc != SQLITE_DONE) - return -1; - else - return 0; -} - -/** - * Initialize friend request table. Return 0 on success, -1 on error. - */ -int -twc_sqlite_init_friend_requests() -{ - TWC_SQLITE_STMT(statement, - "CREATE TABLE IF NOT EXISTS friend_requests (" - "id INTEGER PRIMARY KEY," - "tox_id BLOB NOT NULL," - "message TEXT," - "profile_id INTEGER NOT NULL," - "FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE," - "UNIQUE(tox_id, profile_id)" - ")"); - int rc = sqlite3_step(statement); - TWC_SQLITE_DEBUG_RC(rc, SQLITE_DONE) - if (rc != SQLITE_DONE) - return -1; - else - return 0; -} - -/** - * Add a profile, if it does not exist. - */ -int -twc_sqlite_add_profile(struct t_twc_profile *profile) -{ - TWC_SQLITE_STMT(statement, - "INSERT OR IGNORE INTO profiles (tox_id)" - "VALUES (?)"); - - uint8_t tox_id[TOX_FRIEND_ADDRESS_SIZE]; - tox_get_address(profile->tox, tox_id); - sqlite3_bind_blob(statement, 1, - tox_id, TOX_CLIENT_ID_SIZE, - NULL); - - int rc = sqlite3_step(statement); - TWC_SQLITE_DEBUG_RC(rc, SQLITE_DONE) - if (rc != SQLITE_DONE) - return -1; - else - return 0; -} - -/** - * Get the rowid of a profile. Returns true on success, false on error (e.g. - * not found). - */ -bool -twc_sqlite_profile_id(struct t_twc_profile *profile, int64_t *out) -{ - if (!(profile->tox)) - return false; - - TWC_SQLITE_STMT(statement, - "SELECT id FROM profiles WHERE tox_id == ?"); - - uint8_t tox_id[TOX_FRIEND_ADDRESS_SIZE]; - tox_get_address(profile->tox, tox_id); - sqlite3_bind_blob(statement, 1, - tox_id, TOX_CLIENT_ID_SIZE, - NULL); - - int rc = sqlite3_step(statement); - TWC_SQLITE_DEBUG_RC(rc, SQLITE_ROW) - if (rc != SQLITE_ROW) - { - return false; - } - else - { - *out = sqlite3_column_int(statement, 0); - return true; - } -} - -/** - * Delete a profile from persistent storage. - */ -int -twc_sqlite_delete_profile(struct t_twc_profile *profile) -{ - int64_t profile_id; - if (!twc_sqlite_profile_id(profile, &profile_id)) - { - weechat_printf(NULL, "missing profile!"); - return -1; - } - - TWC_SQLITE_STMT(statement, - "DELETE FROM profiles " - "WHERE id == ?"); - sqlite3_bind_int(statement, 1, - profile_id); - - int rc = sqlite3_step(statement); - TWC_SQLITE_DEBUG_RC(rc, SQLITE_DONE) - if (rc == SQLITE_DONE) - return 0; - else - return -1; -} - -/** - * Add a friend request. Return ID on success, -1 on error. - */ -int -twc_sqlite_add_friend_request(struct t_twc_profile *profile, - struct t_twc_friend_request *friend_request) -{ - int64_t profile_id; - if (!twc_sqlite_profile_id(profile, &profile_id)) - { - weechat_printf(NULL, "missing profile!"); - return -1; - } - - TWC_SQLITE_STMT(statement, - "INSERT OR REPLACE INTO friend_requests (tox_id, message, profile_id)" - "VALUES (?, ?, ?)"); - sqlite3_bind_blob(statement, 1, - friend_request->tox_id, TOX_CLIENT_ID_SIZE, - NULL); - sqlite3_bind_text(statement, 2, - friend_request->message, strlen(friend_request->message) + 1, - NULL); - sqlite3_bind_int(statement, 3, - profile_id); - - int rc = sqlite3_step(statement); - TWC_SQLITE_DEBUG_RC(rc, SQLITE_DONE) - if (rc != SQLITE_DONE) - return -1; - else - return sqlite3_last_insert_rowid(twc_sqlite_db); -} - -/** - * Return the number of friend requests for a profile. - */ -int -twc_sqlite_friend_request_count(struct t_twc_profile *profile) -{ - int64_t profile_id; - if (!twc_sqlite_profile_id(profile, &profile_id)) - { - weechat_printf(NULL, "missing profile!"); - return -1; - } - - TWC_SQLITE_STMT(statement, - "SELECT COUNT(*) FROM friend_requests WHERE profile_id == ?"); - sqlite3_bind_int(statement, 1, - profile_id); - - int rc = sqlite3_step(statement); - TWC_SQLITE_DEBUG_RC(rc, SQLITE_ROW) - if (rc != SQLITE_ROW) - return 0; - else - return sqlite3_column_int(statement, 0); -} - -/** - * Convert a row from an SQLite statement to a friend request object. - */ -struct t_twc_friend_request * -twc_sqlite_friend_request_row(sqlite3_stmt *statement, - struct t_twc_profile *profile) -{ - struct t_twc_friend_request *request = malloc(sizeof(struct t_twc_friend_request)); - request->request_id = sqlite3_column_int(statement, 0); - memcpy(request->tox_id, - sqlite3_column_blob(statement, 1), - TOX_CLIENT_ID_SIZE); - request->message = strdup((const char *)sqlite3_column_text(statement, 2)); - request->profile = profile; - - return request; -} - -/** - * Return a list of all friend requests for the given profile. - */ -struct t_twc_list * -twc_sqlite_friend_requests(struct t_twc_profile *profile) -{ - int64_t profile_id; - if (!twc_sqlite_profile_id(profile, &profile_id)) - { - weechat_printf(NULL, "missing profile!"); - return NULL; - } - - TWC_SQLITE_STMT(statement, - "SELECT id, tox_id, message " - "FROM friend_requests " - "WHERE profile_id == ?"); - sqlite3_bind_int(statement, 1, - profile_id); - - struct t_twc_list *friend_requests = twc_list_new(); - - int rc; - while ((rc = sqlite3_step(statement)) == SQLITE_ROW) - { - struct t_twc_friend_request *request = - twc_sqlite_friend_request_row(statement, profile); - twc_list_item_new_data_add(friend_requests, request); - } - TWC_SQLITE_DEBUG_RC(rc, SQLITE_DONE) - - return friend_requests; -} - -struct t_twc_friend_request * -twc_sqlite_friend_request_with_id(struct t_twc_profile *profile, int64_t id) -{ - int64_t profile_id; - if (!twc_sqlite_profile_id(profile, &profile_id)) - { - weechat_printf(NULL, "missing profile!"); - return NULL; - } - - TWC_SQLITE_STMT(statement, - "SELECT id, tox_id, message " - "FROM friend_requests " - "WHERE id == ? AND profile_id == ?"); - sqlite3_bind_int(statement, 1, id); - sqlite3_bind_int(statement, 2, profile_id); - - struct t_twc_friend_request *request; - int rc = sqlite3_step(statement); - if (rc == SQLITE_ROW) - { - request = twc_sqlite_friend_request_row(statement, profile); - return request; - } - else - TWC_SQLITE_DEBUG_RC(rc, SQLITE_DONE) - - return NULL; -} - -int -twc_sqlite_delete_friend_request_with_id(struct t_twc_profile *profile, - int64_t id) -{ - int64_t profile_id; - if (!twc_sqlite_profile_id(profile, &profile_id)) - { - weechat_printf(NULL, "missing profile!"); - return -1; - } - - TWC_SQLITE_STMT(statement, - "DELETE FROM friend_requests " - "WHERE id == ? AND profile_id == ?"); - sqlite3_bind_int(statement, 1, - id); - sqlite3_bind_int(statement, 2, - profile_id); - - int rc = sqlite3_step(statement); - TWC_SQLITE_DEBUG_RC(rc, SQLITE_DONE) - if (rc == SQLITE_DONE) - return 0; - else - return -1; -} - -/** - * Initialize connection to SQLite database and create tables if necessary. - * Returns 0 on success, -1 on failure. - */ -int -twc_sqlite_init() -{ - char *path = twc_sqlite_db_path(); - - int rc = sqlite3_open(path, &twc_sqlite_db); - free(path); - - TWC_SQLITE_DEBUG_RC(rc, SQLITE_OK) - if (rc != SQLITE_OK) - { - weechat_printf(NULL, - "%s: could not open database: %s\n", - weechat_plugin->name, - sqlite3_errmsg(twc_sqlite_db)); - sqlite3_close(twc_sqlite_db); - twc_sqlite_db = NULL; - - return -1; - } - - // statement list (so we can finalize later) - twc_sqlite_statements = twc_list_new(); - - // initialize tables - if (twc_sqlite_init_profiles() != 0 || - twc_sqlite_init_friend_requests() != 0) - return -1; - - return 0; -} - -/** - * Close connection to SQLite database. - */ -void -twc_sqlite_end() -{ - size_t index; - struct t_twc_list_item *item; - twc_list_foreach(twc_sqlite_statements, index, item) - sqlite3_finalize(item->data); - - int rc = sqlite3_close(twc_sqlite_db); - TWC_SQLITE_DEBUG_RC(rc, SQLITE_OK) -} - diff -Nru tox-weechat-0.1+20150104.r247/src/twc-sqlite.h tox-weechat-0.1+20150410.r255/src/twc-sqlite.h --- tox-weechat-0.1+20150104.r247/src/twc-sqlite.h 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-sqlite.h 1970-01-01 00:00:00.000000000 +0000 @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2015 Håvard Pettersson - * - * This file is part of Tox-WeeChat. - * - * 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 - * 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, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Tox-WeeChat. If not, see . - */ - -#ifndef TWC_SQLITE_H -#define TWC_SQLITE_H - -#include - -struct t_twc_profile; -struct t_twc_friend_request; - -int -twc_sqlite_init(); - -int -twc_sqlite_add_profile(struct t_twc_profile *profile); - -int -twc_sqlite_delete_profile(struct t_twc_profile *profile); - -int -twc_sqlite_add_friend_request(struct t_twc_profile *profile, - struct t_twc_friend_request *friend_request); - -int -twc_sqlite_friend_request_count(struct t_twc_profile *profile); - -struct t_twc_list * -twc_sqlite_friend_requests(struct t_twc_profile *profile); - -struct t_twc_friend_request * -twc_sqlite_friend_request_with_id(struct t_twc_profile *profile, - int64_t id); - -int -twc_sqlite_delete_friend_request_with_id(struct t_twc_profile *profile, - int64_t id); - -void -twc_sqlite_end(); - -#endif // TWC_SQLITE_H diff -Nru tox-weechat-0.1+20150104.r247/src/twc-tox-callbacks.c tox-weechat-0.1+20150410.r255/src/twc-tox-callbacks.c --- tox-weechat-0.1+20150104.r247/src/twc-tox-callbacks.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-tox-callbacks.c 2015-04-09 22:08:40.000000000 +0000 @@ -38,24 +38,25 @@ { struct t_twc_profile *profile = data; - tox_do(profile->tox); - struct t_hook *hook = weechat_hook_timer(tox_do_interval(profile->tox), 0, 1, - twc_do_timer_cb, profile); + tox_iterate(profile->tox); + struct t_hook *hook = weechat_hook_timer(tox_iteration_interval(profile->tox), + 0, 1, twc_do_timer_cb, profile); profile->tox_do_timer = hook; // check connection status - int connected = tox_isconnected(profile->tox); - twc_profile_set_online_status(profile, connected); + TOX_CONNECTION connection = tox_self_get_connection_status(profile->tox); + bool is_connected = connection == TOX_CONNECTION_TCP + || connection == TOX_CONNECTION_UDP; + twc_profile_set_online_status(profile, is_connected); return WEECHAT_RC_OK; } void -twc_handle_friend_message(Tox *tox, int32_t friend_number, - const uint8_t *message, uint16_t length, - void *data, - enum TWC_MESSAGE_TYPE message_type) - +twc_friend_message_callback(Tox *tox, uint32_t friend_number, + TOX_MESSAGE_TYPE type, + const uint8_t *message, size_t length, + void *data) { struct t_twc_profile *profile = data; struct t_twc_chat *chat = twc_chat_search_friend(profile, @@ -66,36 +67,15 @@ char *message_nt = twc_null_terminate(message, length); twc_chat_print_message(chat, "", name, - message_nt, message_type); + message_nt, type); free(name); free(message_nt); } void -twc_friend_message_callback(Tox *tox, int32_t friend_number, - const uint8_t *message, uint16_t length, - void *data) -{ - twc_handle_friend_message(tox, friend_number, - message, length, - data, TWC_MESSAGE_TYPE_MESSAGE); -} - -void -twc_friend_action_callback(Tox *tox, int32_t friend_number, - const uint8_t *message, uint16_t length, - void *data) -{ - twc_handle_friend_message(tox, friend_number, - message, length, - data, TWC_MESSAGE_TYPE_ACTION); -} - -void -twc_connection_status_callback(Tox *tox, - int32_t friend_number, uint8_t status, - void *data) +twc_connection_status_callback(Tox *tox, uint32_t friend_number, + TOX_CONNECTION status, void *data) { struct t_twc_profile *profile = data; char *name = twc_get_name_nt(profile->tox, friend_number); @@ -120,8 +100,8 @@ } void -twc_name_change_callback(Tox *tox, int32_t friend_number, - const uint8_t *name, uint16_t length, +twc_name_change_callback(Tox *tox, uint32_t friend_number, + const uint8_t *name, size_t length, void *data) { struct t_twc_profile *profile = data; @@ -155,9 +135,8 @@ } void -twc_user_status_callback(Tox *tox, - int32_t friend_number, uint8_t status, - void *data) +twc_user_status_callback(Tox *tox, uint32_t friend_number, + TOX_USER_STATUS status, void *data) { struct t_twc_profile *profile = data; struct t_twc_chat *chat = twc_chat_search_friend(profile, @@ -168,8 +147,8 @@ } void -twc_status_message_callback(Tox *tox, int32_t friend_number, - const uint8_t *message, uint16_t length, +twc_status_message_callback(Tox *tox, uint32_t friend_number, + const uint8_t *message, size_t length, void *data) { struct t_twc_profile *profile = data; @@ -182,7 +161,7 @@ void twc_friend_request_callback(Tox *tox, const uint8_t *public_key, - const uint8_t *message, uint16_t length, + const uint8_t *message, size_t length, void *data) { struct t_twc_profile *profile = data; @@ -198,8 +177,8 @@ } else { - char hex_address[TOX_CLIENT_ID_SIZE * 2 + 1]; - twc_bin2hex(public_key, TOX_CLIENT_ID_SIZE, hex_address); + char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1]; + twc_bin2hex(public_key, TOX_PUBLIC_KEY_SIZE, hex_address); weechat_printf(profile->buffer, "%sReceived a friend request from %s with message \"%s\"; " @@ -328,7 +307,7 @@ char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); char *prev_name = NULL; - uint8_t pubkey[TOX_CLIENT_ID_SIZE]; + uint8_t pubkey[TOX_PUBLIC_KEY_SIZE]; int pkrc = tox_group_peer_pubkey(profile->tox, group_number, peer_number, pubkey); if (pkrc == 0) @@ -353,7 +332,7 @@ name, NULL, NULL, NULL, 1); if (nick) weechat_hashtable_set_with_size(chat->nicks, - pubkey, TOX_CLIENT_ID_SIZE, + pubkey, TOX_PUBLIC_KEY_SIZE, nick, 0); } } diff -Nru tox-weechat-0.1+20150104.r247/src/twc-tox-callbacks.h tox-weechat-0.1+20150410.r255/src/twc-tox-callbacks.h --- tox-weechat-0.1+20150104.r247/src/twc-tox-callbacks.h 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-tox-callbacks.h 2015-04-09 22:08:40.000000000 +0000 @@ -27,38 +27,32 @@ int remaining_calls); void -twc_friend_message_callback(Tox *tox, int32_t friend_number, - const uint8_t *message, uint16_t length, +twc_friend_message_callback(Tox *tox, uint32_t friend_number, + TOX_MESSAGE_TYPE type, + const uint8_t *message, size_t length, void *data); void -twc_friend_action_callback(Tox *tox, int32_t friend_number, - const uint8_t *message, uint16_t length, - void *data); +twc_connection_status_callback(Tox *tox, uint32_t friend_number, + TOX_CONNECTION status, void *data); void -twc_connection_status_callback(Tox *tox, - int32_t friend_number, uint8_t status, - void *data); - -void -twc_name_change_callback(Tox *tox, int32_t friend_number, - const uint8_t *name, uint16_t length, +twc_name_change_callback(Tox *tox, uint32_t friend_number, + const uint8_t *name, size_t length, void *data); void -twc_user_status_callback(Tox *tox, - int32_t friend_number, uint8_t status, - void *data); +twc_user_status_callback(Tox *tox, uint32_t friend_number, + TOX_USER_STATUS status, void *data); void -twc_status_message_callback(Tox *tox, int32_t friend_number, - const uint8_t *message, uint16_t length, +twc_status_message_callback(Tox *tox, uint32_t friend_number, + const uint8_t *message, size_t length, void *data); void twc_friend_request_callback(Tox *tox, const uint8_t *public_key, - const uint8_t *message, uint16_t length, + const uint8_t *message, size_t length, void *data); void diff -Nru tox-weechat-0.1+20150104.r247/src/twc-utils.c tox-weechat-0.1+20150410.r255/src/twc-utils.c --- tox-weechat-0.1+20150104.r247/src/twc-utils.c 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-utils.c 2015-04-09 22:08:40.000000000 +0000 @@ -140,7 +140,7 @@ char * twc_get_friend_id_short(Tox *tox, int32_t friend_number) { - uint8_t client_id[TOX_CLIENT_ID_SIZE]; + uint8_t client_id[TOX_PUBLIC_KEY_SIZE]; tox_get_client_id(tox, friend_number, client_id); size_t short_id_length = weechat_config_integer(twc_config_short_id_size); @@ -170,16 +170,49 @@ } /** - * Hash a Tox ID of size TOX_CLIENT_ID_SIZE bytes using a modified djb2 hash. + * Hash a Tox ID of size TOX_PUBLIC_KEY_SIZE bytes using a modified djb2 hash. */ unsigned long long twc_hash_tox_id(const uint8_t *tox_id) { unsigned long long hash = 5381; - for (size_t i = 0; i < TOX_CLIENT_ID_SIZE; ++i) + for (size_t i = 0; i < TOX_PUBLIC_KEY_SIZE; ++i) hash ^= (hash << 5) + (hash >> 2) + tox_id[i]; return hash; } +/** + * Read an entire file into memory. + * + * @return TWC_RC_OK on success, TWC_RC_ERROR if file can not be opened, and + * TWC_RC_ERROR_MALLOC if an appropriate buffer can not be allocated. + */ +enum t_twc_rc +twc_read_file(const char *path, uint8_t **data, size_t *size) +{ + FILE *file; + if (file = fopen(path, "r")) + { + // get file size + fseek(file, 0, SEEK_END); + *size = ftell(file); + rewind(file); + + if (data = malloc(sizeof(*data) * *size)) + { + fread(data, sizeof(uint8_t), *size, file); + fclose(file); + return TWC_RC_OK; + } + else + { + fclose(file); + return TWC_RC_ERROR_MALLOC; + } + } + + return TWC_RC_ERROR; +} + diff -Nru tox-weechat-0.1+20150104.r247/src/twc-utils.h tox-weechat-0.1+20150410.r255/src/twc-utils.h --- tox-weechat-0.1+20150104.r247/src/twc-utils.h 2015-01-04 14:10:22.000000000 +0000 +++ tox-weechat-0.1+20150410.r255/src/twc-utils.h 2015-04-09 22:08:40.000000000 +0000 @@ -54,5 +54,8 @@ unsigned long long twc_hash_tox_id(const uint8_t *tox_id); +enum t_twc_rc +twc_read_file(const char *path, uint8_t **data, size_t *size); + #endif // TOX_WEECHAT_UTILS_H