Browse Source

add toxbot files

msgbot
Stefan Wunsch 7 years ago
parent
commit
c3fc0c0496
5 changed files with 385 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +26
    -0
      CMakeLists.txt
  3. +11
    -0
      cmake/Findlibtoxcore.cmake
  4. +111
    -0
      cmake/LibFindMacros.cmake
  5. +236
    -0
      toxbot.c

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
build/

+ 26
- 0
CMakeLists.txt View File

@ -0,0 +1,26 @@
# Setup cmake
cmake_minimum_required(VERSION 2.6)
# Set version number
set(Tutorial_VERSION_MAJOR 1)
set(Tutorial_VERSION_MINOR 0)
# Setup project
project(Toxbot C)
# Find dependencies
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) # set module path
find_package(libtoxcore REQUIRED)
# Add executable
add_executable(Toxbot toxbot.c)
# Setup the include and linker paths
include_directories(
${libtoxcore_INCLUDE_DIRS}
)
set(LIBS ${LIBS} ${libtoxcore_LIBRARIES})
# Link libraries
target_link_libraries(Toxbot ${LIBS})

+ 11
- 0
cmake/Findlibtoxcore.cmake View File

@ -0,0 +1,11 @@
# Try finding libtoxcore for building
include(LibFindMacros)
libfind_pkg_check_modules(libtoxcore_PKGCONF libtoxcore)
find_path(libtoxcore_INCLUDE_DIR NAMES tox/tox.h PATHS ${libtoxcore_PKGCONF_INCLUDE_DIRS})
find_library(libtoxcore_LIBRARY NAMES toxcore PATHS ${libtoxcore_PKGCONF_LIBRARY_DIRS})
set(libtoxcore_PROCESS_INCLUDES libtoxcore_INCLUDE_DIR)
set(libtoxcore_PROCESS_LIBS libtoxcore_LIBRARY)
libfind_process(libtoxcore)

+ 111
- 0
cmake/LibFindMacros.cmake View File

@ -0,0 +1,111 @@
# Version 1.0 (2013-04-12)
# Public Domain, originally written by Lasse Kärkkäinen <tronic@zi.fi>
# Published at http://www.cmake.org/Wiki/CMake:How_To_Find_Libraries
# If you improve the script, please modify the forementioned wiki page because
# I no longer maintain my scripts (hosted as static files at zi.fi). Feel free
# to remove this entire header if you use real version control instead.
# Changelog:
# 2013-04-12 Added version number (1.0) and this header, no other changes
# 2009-10-08 Originally published
# Works the same as find_package, but forwards the "REQUIRED" and "QUIET" arguments
# used for the current package. For this to work, the first parameter must be the
# prefix of the current package, then the prefix of the new package etc, which are
# passed to find_package.
macro (libfind_package PREFIX)
set (LIBFIND_PACKAGE_ARGS ${ARGN})
if (${PREFIX}_FIND_QUIETLY)
set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} QUIET)
endif (${PREFIX}_FIND_QUIETLY)
if (${PREFIX}_FIND_REQUIRED)
set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED)
endif (${PREFIX}_FIND_REQUIRED)
find_package(${LIBFIND_PACKAGE_ARGS})
endmacro (libfind_package)
# CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
# where they added pkg_check_modules. Consequently I need to support both in my scripts
# to avoid those deprecated warnings. Here's a helper that does just that.
# Works identically to pkg_check_modules, except that no checks are needed prior to use.
macro (libfind_pkg_check_modules PREFIX PKGNAME)
if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
include(UsePkgConfig)
pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(${PREFIX} ${PKGNAME})
endif (PKG_CONFIG_FOUND)
endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
endmacro (libfind_pkg_check_modules)
# Do the final processing once the paths have been detected.
# If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
# all the variables, each of which contain one include directory.
# Ditto for ${PREFIX}_PROCESS_LIBS and library files.
# Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
# Also handles errors in case library detection was required, etc.
macro (libfind_process PREFIX)
# Skip processing if already processed during this run
if (NOT ${PREFIX}_FOUND)
# Start with the assumption that the library was found
set (${PREFIX}_FOUND TRUE)
# Process all includes and set _FOUND to false if any are missing
foreach (i ${${PREFIX}_PROCESS_INCLUDES})
if (${i})
set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
mark_as_advanced(${i})
else (${i})
set (${PREFIX}_FOUND FALSE)
endif (${i})
endforeach (i)
# Process all libraries and set _FOUND to false if any are missing
foreach (i ${${PREFIX}_PROCESS_LIBS})
if (${i})
set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
mark_as_advanced(${i})
else (${i})
set (${PREFIX}_FOUND FALSE)
endif (${i})
endforeach (i)
# Print message and/or exit on fatal error
if (${PREFIX}_FOUND)
if (NOT ${PREFIX}_FIND_QUIETLY)
message (STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
endif (NOT ${PREFIX}_FIND_QUIETLY)
else (${PREFIX}_FOUND)
if (${PREFIX}_FIND_REQUIRED)
foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
message("${i}=${${i}}")
endforeach (i)
message (FATAL_ERROR "Required library ${PREFIX} NOT FOUND.\nInstall the library (dev version) and try again. If the library is already installed, use ccmake to set the missing variables manually.")
endif (${PREFIX}_FIND_REQUIRED)
endif (${PREFIX}_FOUND)
endif (NOT ${PREFIX}_FOUND)
endmacro (libfind_process)
macro(libfind_library PREFIX basename)
set(TMP "")
if(MSVC80)
set(TMP -vc80)
endif(MSVC80)
if(MSVC90)
set(TMP -vc90)
endif(MSVC90)
set(${PREFIX}_LIBNAMES ${basename}${TMP})
if(${ARGC} GREATER 2)
set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2})
string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES})
set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP})
endif(${ARGC} GREATER 2)
find_library(${PREFIX}_LIBRARY
NAMES ${${PREFIX}_LIBNAMES}
PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}
)
endmacro(libfind_library)

+ 236
- 0
toxbot.c View File

@ -0,0 +1,236 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tox/tox.h>
#define SLEEP_TIME_ISNOTCONNECTED 100000
#define SLEEP_TIME_MAINLOOP 50000
#define BOOTSTRAP_ADDRESS "192.254.75.98"
#define BOOTSTRAP_PORT 33445
#define BOOTSTRAP_KEY "951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F"
#define MY_NAME "Toxbot"
#define STATUS_MSG "Write me something!"
#define SAVEFILE "savetox.bin"
#define RETURN_MSG "Check, I have invited you!"
#define GROUPCHAT_NUMBER 0
char
*hex_string_to_bin(const char *hex_string)
{
size_t len = strlen(hex_string);
char *val = malloc(len);
if (val == NULL)
printf("failed in hex_string_to_bin");
size_t i;
for (i = 0; i < len; ++i, hex_string += 2)
sscanf(hex_string, "%2hhx", &val[i]);
return val;
}
void
get_myid(Tox *m)
{
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
const char address[TOX_FRIEND_ADDRESS_SIZE];
tox_get_address(m, (uint8_t *) address);
id_from_data(address, id);
printf("My ID: %s\n",id);
}
void
id_from_data(const uint8_t *address, uint8_t *id)
{
size_t i;
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
char xx[3];
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
}
}
int
store_data(Tox *m)
{
int len = tox_size(m);
char *buf = malloc(len);
tox_save(m, (uint8_t *) buf);
FILE *fd = fopen(SAVEFILE, "wb");
fwrite(buf, len, 1, fd);
free(buf);
fclose(fd);
return 0;
}
int
load_data(Tox *m)
{
FILE *fd;
if ((fd = fopen(SAVEFILE, "rb")) != NULL) {
fseek(fd, 0, SEEK_END);
int len = ftell(fd);
fseek(fd, 0, SEEK_SET);
char *buf = malloc(len);
fread(buf, len, 1, fd);
int load_status;
load_status = tox_load(m, (uint8_t *) buf, len);
if(load_status==0){
printf("Loaded savefile successfully.\n");
}
if(load_status==-1){
printf("Failed to load savefile.\n");
return -1;
}
free(buf);
fclose(fd);
return 0;
}else{
printf("No savefile found, writing new one.\n");
store_data(m);
return 1;
}
}
void
on_request(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata){
printf("Got friend request\n");
// Get friend id and send message
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
char address[TOX_FRIEND_ADDRESS_SIZE];
id_from_data(public_key, id);
printf("Friend ID [Msg]: %s [%s]\n",id,data);
// Answer friend request
char key_answer[TOX_CLIENT_ID_SIZE];
memcpy(key_answer, public_key, TOX_CLIENT_ID_SIZE);
int friend_number;
friend_number = tox_add_friend_norequest(m, key_answer);
printf("Friendnumber: %i\n",friend_number);
}
void
on_message(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t length, void *userdata){
printf("[%i] %s\n",friendnumber,string);
tox_invite_friend(m,friendnumber,GROUPCHAT_NUMBER);
tox_send_message(m,friendnumber,RETURN_MSG,strlen(RETURN_MSG));
}
static
Tox *init_tox(void)
{
// Set tox option
Tox_Options tox_opts;
tox_opts.ipv6enabled = 0;
tox_opts.udp_disabled = 0;
tox_opts.proxy_enabled = 0;
// Init core
Tox *m = tox_new(&tox_opts);
// Register callback
tox_callback_friend_request(m, on_request, NULL);
tox_callback_friend_message(m, on_message, NULL);
// Set name
tox_set_name(m, (uint8_t *) MY_NAME, strlen(MY_NAME));
tox_set_status_message(m,STATUS_MSG,strlen(STATUS_MSG));
// Return tox object
return m;
}
int
main(int argc, const char *argv[]){
// Start toxbot
printf("// TOXBOT\n");
// Convert public keyto binary data
char *pub_key = hex_string_to_bin(BOOTSTRAP_KEY);
// Setup mytox
Tox *m = init_tox();
int load_status;
load_status = load_data(m);
if(load_status == -1) return 1;
// Init connection
int bootstrap;
int i;
bootstrap = tox_bootstrap_from_address(m, BOOTSTRAP_ADDRESS, BOOTSTRAP_PORT, (uint8_t *) pub_key);
if(bootstrap){
printf("Boostrap done.\n");
}
else{
printf("Boostrap crashed.\n");
return 1;
}
// Main loop
int is_connected = 0;
int first_connect = 1;
while(1){
is_connected = tox_isconnected(m);
tox_do(m);
// Do loop of first connect
if(is_connected && first_connect){
first_connect = 0;
printf("Connected to DHT.\n");
get_myid(m); // get my id
int groupchat_num;
groupchat_num = tox_add_groupchat(m);
if(groupchat_num == -1){
printf("Creating groupchat failed.\n");
return 1;
}
if(groupchat_num == 0){
printf("Setup groupchat.\n");
}
else{
printf("Setup groupchat with undefined number.\n");
return 1;
}
usleep(SLEEP_TIME_MAINLOOP);
}
// Do loops after first connect (save data and do short sleep)
else if(is_connected && !first_connect){
store_data(m);
usleep(SLEEP_TIME_MAINLOOP);
}
// Catch error if connection is lost
else if(!is_connected && !first_connect){
printf("Lost connection to DHT.\n");
return 1;
}
// Wait for connection
else{
usleep(SLEEP_TIME_ISNOTCONNECTED);
}
}
// Kill tox
tox_kill(m);
return 0;
}

Loading…
Cancel
Save