Archived
1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
ToxEcho/main.c

166 lines
3.3 KiB
C
Raw Normal View History

#include <tox/tox.h>
2015-07-25 13:56:31 -04:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2015-07-25 16:07:21 -04:00
#include <time.h>
2015-07-25 13:56:31 -04:00
#define IP_LENGTH_MAX 15
2015-07-25 14:40:05 -04:00
typedef char IP[IP_LENGTH_MAX + 1];
typedef char Key[(TOX_PUBLIC_KEY_SIZE * 2) + 1];
2015-07-25 13:56:31 -04:00
struct Node {
2015-07-25 14:40:05 -04:00
IP ip;
2015-07-25 13:56:31 -04:00
int port;
2015-07-25 14:40:05 -04:00
Key key;
2015-07-25 13:56:31 -04:00
};
#define NODES_COUNT 26
#define NODES_FILE_NAME "nodes"
static void Key_to_KeyBin(const char *key, uint8_t *key_bin);
2015-07-25 14:26:48 -04:00
static void onFriendRequest(
Tox *tox,
const uint8_t *key,
const uint8_t *data,
size_t length,
void *user_data);
2015-07-25 14:35:39 -04:00
static void onFriendMessage(
Tox *tox,
uint32_t friend_number,
TOX_MESSAGE_TYPE type,
const uint8_t *string,
size_t length,
void *user_data);
2015-07-25 11:27:51 -04:00
int main()
{
2015-07-25 15:21:52 -04:00
// Allocate Tox options and initialize with defaults
struct Tox_Options tox_options;
memset(&tox_options, 0, sizeof(struct Tox_Options));
tox_options_default(&tox_options);
2015-07-25 15:21:52 -04:00
// Initialize Tox instance
TOX_ERR_NEW err;
Tox *tox = tox_new(&tox_options, &err);
if (err != TOX_ERR_NEW_OK)
exit(EXIT_FAILURE);
2015-07-25 15:21:52 -04:00
// Print ID to STDOUT
char address[TOX_ADDRESS_SIZE];
tox_self_get_address(tox, (uint8_t*)address);
printf("ID: ");
for (int i = 0; i < TOX_ADDRESS_SIZE; ++i) {
char d[3];
snprintf(d, sizeof(d), "%02X", address[i] & 0xFF);
printf("%s", d);
}
printf("\n");
2015-07-25 15:21:52 -04:00
// Load list of nodes from file
2015-07-25 13:56:31 -04:00
struct Node nodes[NODES_COUNT];
FILE *nodes_file = fopen(NODES_FILE_NAME, "r");
if (!nodes_file)
exit(EXIT_FAILURE);
for (int node_index = 0; node_index < NODES_COUNT; ++node_index) {
struct Node *const node = &nodes[node_index];
fscanf(nodes_file, "%s %d %s",
node->ip,
&node->port,
node->key
);
}
fclose(nodes_file);
2015-07-25 15:21:52 -04:00
// Bootstrap Tox with loaded nodes
2015-07-25 14:08:43 -04:00
for (int node_index = 0; node_index < NODES_COUNT; ++node_index) {
struct Node *const node = &nodes[node_index];
uint8_t key_bin[TOX_PUBLIC_KEY_SIZE];
Key_to_KeyBin(node->key, key_bin);
2015-07-25 14:08:43 -04:00
TOX_ERR_BOOTSTRAP err;
tox_bootstrap(tox, node->ip, node->port, key_bin, &err);
if (err != TOX_ERR_BOOTSTRAP_OK)
fprintf(stderr, "Failed to bootstrap (\"%s\", %d, \"%s\") with error code %d\n",
node->ip,
node->port,
node->key,
err
);
}
2015-07-25 15:21:52 -04:00
// Setup callbacks for Tox instance
2015-07-25 14:26:48 -04:00
tox_callback_friend_request(tox, onFriendRequest, NULL);
2015-07-25 14:35:39 -04:00
tox_callback_friend_message(tox, onFriendMessage, NULL);
2015-07-25 14:26:48 -04:00
2015-07-25 15:21:52 -04:00
// Event loop
2015-07-25 16:07:21 -04:00
struct timespec delay;
2015-07-25 16:26:07 -04:00
2015-07-25 16:07:21 -04:00
while (true) {
delay.tv_sec = 0;
delay.tv_nsec = tox_iteration_interval(tox) * 1000000;
2015-07-25 16:26:07 -04:00
nanosleep(&delay, NULL);
2015-07-25 14:16:43 -04:00
tox_iterate(tox);
2015-07-25 14:11:08 -04:00
}
2015-07-25 15:21:52 -04:00
// Exit gracefully
2015-07-25 14:12:39 -04:00
tox_kill(tox);
exit(EXIT_SUCCESS);
2015-07-25 11:27:51 -04:00
}
2015-07-25 14:26:48 -04:00
void Key_to_KeyBin(const char *const key, uint8_t *const key_bin)
{
for (int i = 0; i < TOX_PUBLIC_KEY_SIZE; ++i)
sscanf(&key[i * 2], "%2hhx", &key_bin[i]);
}
2015-07-25 14:26:48 -04:00
void onFriendRequest(
Tox *const tox,
const uint8_t *const key,
const uint8_t *const data,
size_t length,
void *const user_data)
{
tox_friend_add_norequest(tox, key, NULL);
}
2015-07-25 14:35:39 -04:00
void onFriendMessage(
Tox *const tox,
const uint32_t friend_number,
const TOX_MESSAGE_TYPE type,
const uint8_t *const string,
const size_t length,
void *const user_data)
{
if (type != TOX_MESSAGE_TYPE_NORMAL)
return;
tox_friend_send_message(tox, friend_number, TOX_MESSAGE_TYPE_NORMAL, string, length, NULL);
}