mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Ported some of the code in testing/ to the new api.
This commit is contained in:
parent
8fa8e9dcd7
commit
d83efd35dd
|
@ -106,21 +106,6 @@ tox_shell_LDADD = $(LIBSODIUM_LDFLAGS) \
|
|||
-lutil
|
||||
|
||||
|
||||
noinst_PROGRAMS += test_avatars
|
||||
|
||||
test_avatars_SOURCES = ../testing/test_avatars.c
|
||||
|
||||
test_avatars_CFLAGS = $(LIBSODIUM_CFLAGS) \
|
||||
$(NACL_CFLAGS)
|
||||
|
||||
test_avatars_LDADD = $(LIBSODIUM_LDFLAGS) \
|
||||
$(NACL_LDFLAGS) \
|
||||
libtoxcore.la \
|
||||
$(LIBSODIUM_LIBS) \
|
||||
$(NACL_OBJECTS) \
|
||||
$(NACL_LIBS)
|
||||
|
||||
|
||||
noinst_PROGRAMS += irc_syncbot
|
||||
|
||||
irc_syncbot_SOURCES = ../testing/irc_syncbot.c
|
||||
|
|
|
@ -56,17 +56,17 @@
|
|||
|
||||
#endif
|
||||
|
||||
void print_message(Messenger *m, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
|
||||
void print_message(Messenger *m, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata)
|
||||
{
|
||||
printf("Message with length %u received from %u: %s \n", length, friendnumber, string);
|
||||
m_sendmessage(m, friendnumber, (uint8_t *)"Test1", 6);
|
||||
printf("Message with length %lu received from %u: %s \n", length, friendnumber, string);
|
||||
m_sendmessage(m, friendnumber, (uint8_t *)"Test1", 6, 0);
|
||||
}
|
||||
|
||||
/* FIXME needed as print_request has to match the interface expected by
|
||||
* networking_requesthandler and so cannot take a Messenger * */
|
||||
static Messenger *m;
|
||||
|
||||
void print_request(Messenger *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata)
|
||||
void print_request(Messenger *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
|
||||
{
|
||||
printf("Friend request received from: \n");
|
||||
printf("ClientID: ");
|
||||
|
@ -79,7 +79,7 @@ void print_request(Messenger *m, const uint8_t *public_key, const uint8_t *data,
|
|||
printf("%hhX", public_key[j]);
|
||||
}
|
||||
|
||||
printf("\nOf length: %u with data: %s \n", length, data);
|
||||
printf("\nOf length: %lu with data: %s \n", length, data);
|
||||
|
||||
if (length != sizeof("Install Gentoo")) {
|
||||
return;
|
||||
|
@ -184,7 +184,7 @@ int main(int argc, char *argv[])
|
|||
getname(m, num, name);
|
||||
printf("%s\n", name);
|
||||
|
||||
m_sendmessage(m, num, (uint8_t *)"Test", 5);
|
||||
m_sendmessage(m, num, (uint8_t *)"Test", 5, 0);
|
||||
do_messenger(m);
|
||||
c_sleep(30);
|
||||
FILE *file = fopen("Save.bak", "wb");
|
||||
|
|
|
@ -98,7 +98,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
for (i = r_len - 1; i != 0 && buffer[i] != '='; --i);
|
||||
|
||||
uint8_t tox_id[TOX_FRIEND_ADDRESS_SIZE];
|
||||
uint8_t tox_id[TOX_ADDRESS_SIZE];
|
||||
|
||||
if (tox_decrypt_dns3_TXT(d, tox_id, buffer + i + 1, r_len - (i + 1), request_id) != 0)
|
||||
return -1;
|
||||
|
@ -106,7 +106,7 @@ int main(int argc, char *argv[])
|
|||
printf("The Tox id for username %s is:\n", argv[3]);
|
||||
|
||||
//unsigned int i;
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
|
||||
for (i = 0; i < TOX_ADDRESS_SIZE; ++i) {
|
||||
printf("%02hhX", tox_id[i]);
|
||||
}
|
||||
|
||||
|
|
|
@ -89,7 +89,7 @@ static void callback_group_invite(Tox *tox, int fid, uint8_t type, const uint8_t
|
|||
current_group = tox_join_groupchat(tox, fid, data, length);
|
||||
}
|
||||
|
||||
void callback_friend_message(Tox *tox, int fid, const uint8_t *message, uint16_t length, void *userdata)
|
||||
void callback_friend_message(Tox *tox, uint32_t fid, const uint8_t *message, size_t length, void *userdata)
|
||||
{
|
||||
if (length == 1 && *message == 'c') {
|
||||
if (tox_del_groupchat(tox, current_group) == 0)
|
||||
|
@ -203,7 +203,7 @@ void send_irc_group(Tox *tox, uint8_t *msg, uint16_t len)
|
|||
|
||||
Tox *init_tox(int argc, char *argv[])
|
||||
{
|
||||
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
|
||||
uint8_t ipv6enabled = 1; /* x */
|
||||
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
|
||||
|
||||
if (argvoffset < 0)
|
||||
|
@ -215,12 +215,12 @@ Tox *init_tox(int argc, char *argv[])
|
|||
exit(0);
|
||||
}
|
||||
|
||||
Tox *tox = tox_new(0);
|
||||
Tox *tox = tox_new(0, 0, 0, 0);
|
||||
|
||||
if (!tox)
|
||||
exit(1);
|
||||
|
||||
tox_set_name(tox, (uint8_t *)IRC_NAME, sizeof(IRC_NAME) - 1);
|
||||
tox_self_set_name(tox, (uint8_t *)IRC_NAME, sizeof(IRC_NAME) - 1, 0);
|
||||
tox_callback_friend_message(tox, &callback_friend_message, 0);
|
||||
tox_callback_group_invite(tox, &callback_group_invite, 0);
|
||||
tox_callback_group_message(tox, ©_groupmessage, 0);
|
||||
|
@ -228,7 +228,7 @@ Tox *init_tox(int argc, char *argv[])
|
|||
|
||||
uint16_t port = atoi(argv[argvoffset + 2]);
|
||||
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
|
||||
int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], port, binary_string);
|
||||
tox_bootstrap(tox, argv[argvoffset + 1], port, binary_string, 0);
|
||||
free(binary_string);
|
||||
|
||||
char temp_id[128];
|
||||
|
@ -239,10 +239,10 @@ Tox *init_tox(int argc, char *argv[])
|
|||
}
|
||||
|
||||
uint8_t *bin_id = hex_string_to_bin(temp_id);
|
||||
int num = tox_add_friend(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo") - 1);
|
||||
uint32_t num = tox_friend_add(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo") - 1, 0);
|
||||
free(bin_id);
|
||||
|
||||
if (num < 0) {
|
||||
if (num == UINT32_MAX) {
|
||||
printf("\nSomething went wrong when adding friend.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -342,7 +342,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
tox_do(tox);
|
||||
tox_iteration(tox);
|
||||
usleep(1000 * 50);
|
||||
}
|
||||
|
||||
|
|
133
testing/nTox.c
133
testing/nTox.c
|
@ -105,7 +105,7 @@ int x, y;
|
|||
int conversation_default = 0;
|
||||
|
||||
typedef struct {
|
||||
uint8_t id[TOX_CLIENT_ID_SIZE];
|
||||
uint8_t id[TOX_PUBLIC_KEY_SIZE];
|
||||
uint8_t accepted;
|
||||
} Friend_request;
|
||||
|
||||
|
@ -179,19 +179,19 @@ int add_filesender(Tox *m, uint16_t friendnum, char *filename)
|
|||
|
||||
|
||||
#define FRADDR_TOSTR_CHUNK_LEN 8
|
||||
#define FRADDR_TOSTR_BUFSIZE (TOX_FRIEND_ADDRESS_SIZE * 2 + TOX_FRIEND_ADDRESS_SIZE / FRADDR_TOSTR_CHUNK_LEN + 1)
|
||||
#define FRADDR_TOSTR_BUFSIZE (TOX_ADDRESS_SIZE * 2 + TOX_ADDRESS_SIZE / FRADDR_TOSTR_CHUNK_LEN + 1)
|
||||
|
||||
static void fraddr_to_str(uint8_t *id_bin, char *id_str)
|
||||
{
|
||||
uint32_t i, delta = 0, pos_extra, sum_extra = 0;
|
||||
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
|
||||
for (i = 0; i < TOX_ADDRESS_SIZE; i++) {
|
||||
sprintf(&id_str[2 * i + delta], "%02hhX", id_bin[i]);
|
||||
|
||||
if ((i + 1) == TOX_CLIENT_ID_SIZE)
|
||||
if ((i + 1) == TOX_PUBLIC_KEY_SIZE)
|
||||
pos_extra = 2 * (i + 1) + delta;
|
||||
|
||||
if (i >= TOX_CLIENT_ID_SIZE)
|
||||
if (i >= TOX_PUBLIC_KEY_SIZE)
|
||||
sum_extra |= id_bin[i];
|
||||
|
||||
if (!((i + 1) % FRADDR_TOSTR_CHUNK_LEN)) {
|
||||
|
@ -210,14 +210,15 @@ void get_id(Tox *m, char *data)
|
|||
{
|
||||
sprintf(data, "[i] ID: ");
|
||||
int offset = strlen(data);
|
||||
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
||||
tox_get_address(m, address);
|
||||
uint8_t address[TOX_ADDRESS_SIZE];
|
||||
tox_self_get_address(m, address);
|
||||
fraddr_to_str(address, data + offset);
|
||||
}
|
||||
|
||||
int getfriendname_terminated(Tox *m, int friendnum, char *namebuf)
|
||||
{
|
||||
int res = tox_get_name(m, friendnum, (uint8_t *)namebuf);
|
||||
tox_friend_get_name(m, friendnum, (uint8_t *)namebuf, NULL);
|
||||
int res = tox_friend_get_name_size(m, friendnum, NULL);
|
||||
|
||||
if (res >= 0)
|
||||
namebuf[res] = 0;
|
||||
|
@ -249,13 +250,13 @@ void new_lines(char *line)
|
|||
|
||||
|
||||
const char ptrn_friend[] = "[i] Friend %i: %s\n+ id: %s";
|
||||
const int id_str_len = TOX_FRIEND_ADDRESS_SIZE * 2 + 3;
|
||||
const int id_str_len = TOX_ADDRESS_SIZE * 2 + 3;
|
||||
void print_friendlist(Tox *m)
|
||||
{
|
||||
new_lines("[i] Friend List:");
|
||||
|
||||
char name[TOX_MAX_NAME_LENGTH + 1];
|
||||
uint8_t fraddr_bin[TOX_FRIEND_ADDRESS_SIZE];
|
||||
uint8_t fraddr_bin[TOX_ADDRESS_SIZE];
|
||||
char fraddr_str[FRADDR_TOSTR_BUFSIZE];
|
||||
|
||||
/* account for the longest name and the longest "base" string and number (int) and id_str */
|
||||
|
@ -264,7 +265,7 @@ void print_friendlist(Tox *m)
|
|||
uint32_t i = 0;
|
||||
|
||||
while (getfriendname_terminated(m, i, name) != -1) {
|
||||
if (!tox_get_client_id(m, i, fraddr_bin))
|
||||
if (tox_friend_get_public_key(m, i, fraddr_bin, NULL))
|
||||
fraddr_to_str(fraddr_bin, fraddr_str);
|
||||
else
|
||||
sprintf(fraddr_str, "???");
|
||||
|
@ -347,50 +348,59 @@ void line_eval(Tox *m, char *line)
|
|||
}
|
||||
|
||||
unsigned char *bin_string = hex_string_to_bin(temp_id);
|
||||
int num = tox_add_friend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
TOX_ERR_FRIEND_ADD error;
|
||||
uint32_t num = tox_friend_add(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"), &error);
|
||||
free(bin_string);
|
||||
char numstring[100];
|
||||
|
||||
switch (num) {
|
||||
case TOX_FAERR_TOOLONG:
|
||||
switch (error) {
|
||||
case TOX_ERR_FRIEND_ADD_TOO_LONG:
|
||||
sprintf(numstring, "[i] Message is too long.");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_NOMESSAGE:
|
||||
case TOX_ERR_FRIEND_ADD_NO_MESSAGE:
|
||||
sprintf(numstring, "[i] Please add a message to your request.");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_OWNKEY:
|
||||
case TOX_ERR_FRIEND_ADD_OWN_KEY:
|
||||
sprintf(numstring, "[i] That appears to be your own ID.");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_ALREADYSENT:
|
||||
case TOX_ERR_FRIEND_ADD_ALREADY_SENT:
|
||||
sprintf(numstring, "[i] Friend request already sent.");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_UNKNOWN:
|
||||
sprintf(numstring, "[i] Undefined error when adding friend.");
|
||||
case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM:
|
||||
sprintf(numstring, "[i] Address has a bad checksum.");
|
||||
break;
|
||||
|
||||
default:
|
||||
if (num >= 0) {
|
||||
sprintf(numstring, "[i] Added friend as %d.", num);
|
||||
save_data(m);
|
||||
} else
|
||||
sprintf(numstring, "[i] Unknown error %i.", num);
|
||||
case TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM:
|
||||
sprintf(numstring, "[i] New nospam set.");
|
||||
break;
|
||||
|
||||
case TOX_ERR_FRIEND_ADD_MALLOC:
|
||||
sprintf(numstring, "[i] malloc error.");
|
||||
break;
|
||||
|
||||
case TOX_ERR_FRIEND_ADD_NULL:
|
||||
sprintf(numstring, "[i] message was NULL.");
|
||||
break;
|
||||
|
||||
case TOX_ERR_FRIEND_ADD_OK:
|
||||
sprintf(numstring, "[i] Added friend as %d.", num);
|
||||
save_data(m);
|
||||
break;
|
||||
}
|
||||
|
||||
new_lines(numstring);
|
||||
} else if (inpt_command == 'd') {
|
||||
tox_do(m);
|
||||
tox_iteration(m);
|
||||
} else if (inpt_command == 'm') { //message command: /m friendnumber messsage
|
||||
char *posi[1];
|
||||
int num = strtoul(line + prompt_offset, posi, 0);
|
||||
|
||||
if (**posi != 0) {
|
||||
if (tox_send_message(m, num, (uint8_t *) *posi + 1, strlen(*posi + 1)) < 1) {
|
||||
if (tox_send_message(m, num, (uint8_t *) *posi + 1, strlen(*posi + 1), NULL) < 1) {
|
||||
char sss[256];
|
||||
sprintf(sss, "[i] could not send message to friend num %u", num);
|
||||
new_lines(sss);
|
||||
|
@ -410,14 +420,14 @@ void line_eval(Tox *m, char *line)
|
|||
}
|
||||
|
||||
name[i - 3] = 0;
|
||||
tox_set_name(m, name, i - 2);
|
||||
tox_self_set_name(m, name, i - 2, NULL);
|
||||
char numstring[100];
|
||||
sprintf(numstring, "[i] changed nick to %s", (char *)name);
|
||||
new_lines(numstring);
|
||||
} else if (inpt_command == 'l') {
|
||||
print_friendlist(m);
|
||||
} else if (inpt_command == 's') {
|
||||
uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH];
|
||||
uint8_t status[TOX_MAX_STATUS_MESSAGE_LENGTH];
|
||||
size_t i, len = strlen(line);
|
||||
|
||||
for (i = 3; i < len; i++) {
|
||||
|
@ -427,7 +437,7 @@ void line_eval(Tox *m, char *line)
|
|||
}
|
||||
|
||||
status[i - 3] = 0;
|
||||
tox_set_status_message(m, status, strlen((char *)status));
|
||||
tox_self_set_status_message(m, status, strlen((char *)status), NULL);
|
||||
char numstring[100];
|
||||
sprintf(numstring, "[i] changed status to %s", (char *)status);
|
||||
new_lines(numstring);
|
||||
|
@ -439,9 +449,9 @@ void line_eval(Tox *m, char *line)
|
|||
sprintf(numchar, "[i] you either didn't receive that request or you already accepted it");
|
||||
new_lines(numchar);
|
||||
} else {
|
||||
int num = tox_add_friend_norequest(m, pending_requests[numf].id);
|
||||
uint32_t num = tox_friend_add_norequest(m, pending_requests[numf].id, NULL);
|
||||
|
||||
if (num != -1) {
|
||||
if (num != UINT32_MAX) {
|
||||
pending_requests[numf].accepted = 1;
|
||||
sprintf(numchar, "[i] friend request %u accepted as friend no. %d", numf, num);
|
||||
new_lines(numchar);
|
||||
|
@ -475,9 +485,9 @@ void line_eval(Tox *m, char *line)
|
|||
} while ((c != 'y') && (c != 'n') && (c != EOF));
|
||||
|
||||
if (c == 'y') {
|
||||
int res = tox_del_friend(m, numf);
|
||||
int res = tox_friend_delete(m, numf, NULL);
|
||||
|
||||
if (res == 0)
|
||||
if (res)
|
||||
sprintf(msg, "[i] [%i: %s] is no longer your friend", numf, fname);
|
||||
else
|
||||
sprintf(msg, "[i] failed to remove friend");
|
||||
|
@ -602,7 +612,7 @@ void line_eval(Tox *m, char *line)
|
|||
if (conversation_default != 0) {
|
||||
if (conversation_default > 0) {
|
||||
int friendnumber = conversation_default - 1;
|
||||
uint32_t res = tox_send_message(m, friendnumber, (uint8_t *)line, strlen(line));
|
||||
uint32_t res = tox_send_message(m, friendnumber, (uint8_t *)line, strlen(line), NULL);
|
||||
|
||||
if (res == 0) {
|
||||
char sss[128];
|
||||
|
@ -863,20 +873,20 @@ void do_refresh()
|
|||
refresh();
|
||||
}
|
||||
|
||||
void print_request(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata)
|
||||
void print_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
|
||||
{
|
||||
new_lines("[i] received friend request with message:");
|
||||
new_lines((char *)data);
|
||||
char numchar[100];
|
||||
sprintf(numchar, "[i] accept request with /a %u", num_requests);
|
||||
new_lines(numchar);
|
||||
memcpy(pending_requests[num_requests].id, public_key, TOX_CLIENT_ID_SIZE);
|
||||
memcpy(pending_requests[num_requests].id, public_key, TOX_PUBLIC_KEY_SIZE);
|
||||
pending_requests[num_requests].accepted = 0;
|
||||
++num_requests;
|
||||
do_refresh();
|
||||
}
|
||||
|
||||
void print_message(Tox *m, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
|
||||
void print_message(Tox *m, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata)
|
||||
{
|
||||
/* ensure null termination */
|
||||
uint8_t null_string[length + 1];
|
||||
|
@ -885,7 +895,7 @@ void print_message(Tox *m, int friendnumber, const uint8_t *string, uint16_t len
|
|||
print_formatted_message(m, (char *)null_string, friendnumber, 0);
|
||||
}
|
||||
|
||||
void print_nickchange(Tox *m, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
|
||||
void print_nickchange(Tox *m, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata)
|
||||
{
|
||||
char name[TOX_MAX_NAME_LENGTH + 1];
|
||||
|
||||
|
@ -901,7 +911,7 @@ void print_nickchange(Tox *m, int friendnumber, const uint8_t *string, uint16_t
|
|||
}
|
||||
}
|
||||
|
||||
void print_statuschange(Tox *m, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
|
||||
void print_statuschange(Tox *m, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata)
|
||||
{
|
||||
char name[TOX_MAX_NAME_LENGTH + 1];
|
||||
|
||||
|
@ -919,7 +929,7 @@ void print_statuschange(Tox *m, int friendnumber, const uint8_t *string, uint16_
|
|||
|
||||
static char *data_file_name = NULL;
|
||||
|
||||
static int load_data(Tox *m)
|
||||
static Tox *load_data()
|
||||
{
|
||||
FILE *data_file = fopen(data_file_name, "r");
|
||||
|
||||
|
@ -936,7 +946,7 @@ static int load_data(Tox *m)
|
|||
return 0;
|
||||
}
|
||||
|
||||
tox_load(m, data, size);
|
||||
Tox *m = tox_new(0, data, size, NULL);
|
||||
|
||||
if (fclose(data_file) < 0) {
|
||||
perror("[!] fclose failed");
|
||||
|
@ -944,10 +954,10 @@ static int load_data(Tox *m)
|
|||
/* return 0; */
|
||||
}
|
||||
|
||||
return 1;
|
||||
return m;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return tox_new(0, 0, 0, NULL);
|
||||
}
|
||||
|
||||
static int save_data(Tox *m)
|
||||
|
@ -960,7 +970,7 @@ static int save_data(Tox *m)
|
|||
}
|
||||
|
||||
int res = 1;
|
||||
size_t size = tox_size(m);
|
||||
size_t size = tox_save_size(m);
|
||||
uint8_t data[size];
|
||||
tox_save(m, data);
|
||||
|
||||
|
@ -977,13 +987,10 @@ static int save_data(Tox *m)
|
|||
return res;
|
||||
}
|
||||
|
||||
static int load_data_or_init(Tox *m, char *path)
|
||||
static int save_data_file(Tox *m, char *path)
|
||||
{
|
||||
data_file_name = path;
|
||||
|
||||
if (load_data(m))
|
||||
return 1;
|
||||
|
||||
if (save_data(m))
|
||||
return 1;
|
||||
|
||||
|
@ -1176,7 +1183,7 @@ void write_file(Tox *m, int friendnumber, uint8_t filenumber, const uint8_t *dat
|
|||
char timeout_getch(Tox *m)
|
||||
{
|
||||
char c;
|
||||
int slpval = tox_do_interval(m);
|
||||
int slpval = tox_iteration_interval(m);
|
||||
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
|
@ -1214,7 +1221,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
/* let user override default by cmdline */
|
||||
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
|
||||
uint8_t ipv6enabled = 1; /* x */
|
||||
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
|
||||
|
||||
if (argvoffset < 0)
|
||||
|
@ -1231,19 +1238,20 @@ int main(int argc, char *argv[])
|
|||
if (!strcmp(argv[argc - 2], "-f"))
|
||||
filename = argv[argc - 1];
|
||||
|
||||
m = tox_new(0);
|
||||
data_file_name = filename;
|
||||
m = load_data();
|
||||
|
||||
if ( !m ) {
|
||||
fputs("Failed to allocate Messenger datastructure", stderr);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
load_data_or_init(m, filename);
|
||||
save_data_file(m, filename);
|
||||
|
||||
tox_callback_friend_request(m, print_request, NULL);
|
||||
tox_callback_friend_message(m, print_message, NULL);
|
||||
tox_callback_name_change(m, print_nickchange, NULL);
|
||||
tox_callback_status_message(m, print_statuschange, NULL);
|
||||
tox_callback_friend_name(m, print_nickchange, NULL);
|
||||
tox_callback_friend_status_message(m, print_statuschange, NULL);
|
||||
tox_callback_group_invite(m, print_invite, NULL);
|
||||
tox_callback_group_message(m, print_groupmessage, NULL);
|
||||
tox_callback_file_data(m, write_file, NULL);
|
||||
|
@ -1263,7 +1271,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
uint16_t port = atoi(argv[argvoffset + 2]);
|
||||
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
|
||||
int res = tox_bootstrap_from_address(m, argv[argvoffset + 1], port, binary_string);
|
||||
int res = tox_bootstrap(m, argv[argvoffset + 1], port, binary_string, NULL);
|
||||
|
||||
if (!res) {
|
||||
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
|
||||
|
@ -1275,7 +1283,8 @@ int main(int argc, char *argv[])
|
|||
|
||||
new_lines("[i] change username with /n");
|
||||
uint8_t name[TOX_MAX_NAME_LENGTH + 1];
|
||||
uint16_t namelen = tox_get_self_name(m, name);
|
||||
tox_self_get_name(m, name);
|
||||
uint16_t namelen = tox_self_get_name_size(m);
|
||||
name[namelen] = 0;
|
||||
|
||||
if (namelen > 0) {
|
||||
|
@ -1288,7 +1297,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
while (1) {
|
||||
if (on == 0) {
|
||||
if (tox_isconnected(m)) {
|
||||
if (tox_get_connection_status(m)) {
|
||||
new_lines("[i] connected to DHT");
|
||||
on = 1;
|
||||
} else {
|
||||
|
@ -1296,15 +1305,13 @@ int main(int argc, char *argv[])
|
|||
|
||||
if (timestamp0 + 10 < timestamp1) {
|
||||
timestamp0 = timestamp1;
|
||||
tox_bootstrap_from_address(m, argv[argvoffset + 1], port, binary_string);
|
||||
tox_bootstrap(m, argv[argvoffset + 1], port, binary_string, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
send_filesenders(m);
|
||||
tox_do(m);
|
||||
tox_iteration(m);
|
||||
do_refresh();
|
||||
|
||||
int c = timeout_getch(m);
|
||||
|
@ -1320,7 +1327,7 @@ int main(int argc, char *argv[])
|
|||
} else if (c == 8 || c == 127) {
|
||||
input_line[strlen(input_line) - 1] = '\0';
|
||||
} else if (isalnum(c) || ispunct(c) || c == ' ') {
|
||||
strcpy(input_line, appender(input_line, (char) c));
|
||||
appender(input_line, (char) c);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,755 +0,0 @@
|
|||
/*
|
||||
* A bot to test Tox avatars
|
||||
*
|
||||
* Usage: ./test_avatars <data dir>
|
||||
*
|
||||
* Connects to the Tox network, publishes our avatar, requests our friends
|
||||
* avatars and, if available, saves them to a local cache.
|
||||
* This bot automatically accepts any friend request.
|
||||
*
|
||||
*
|
||||
* Data dir MUST have:
|
||||
*
|
||||
* - A file named "data" (named accordingly to STS Draft v0.1.0) with
|
||||
* user id, friends, bootstrap data, etc. from a previously configured
|
||||
* Tox session; use a client (eg. toxic) to configure it, add friends,
|
||||
* etc.
|
||||
*
|
||||
* Data dir MAY have:
|
||||
*
|
||||
* - A directory named "avatars" with the user's avatar and cached avatars.
|
||||
* The user avatar must be named in the format: "<uppercase pub key>.png"
|
||||
*
|
||||
*
|
||||
* The bot will answer to these commands:
|
||||
*
|
||||
* !debug-on - Enable extended debug messages
|
||||
* !debug-off - Disenable extended debug messages
|
||||
* !set-avatar - Set our avatar from "avatars/<USERID>.png"
|
||||
* !remove-avatar - Remove our avatar
|
||||
*
|
||||
*/
|
||||
|
||||
#define DATA_FILE_NAME "data"
|
||||
#define AVATAR_DIR_NAME "avatars"
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "../toxcore/tox.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
#include <limits.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
|
||||
/* Basic debug utils */
|
||||
|
||||
#define DEBUG(format, ...) debug_printf("DEBUG: %s:%d %s: " format "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)
|
||||
|
||||
static bool print_debug_msgs = true;
|
||||
|
||||
static void debug_printf(const char *fmt, ...)
|
||||
{
|
||||
if (print_debug_msgs == true) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* ------------ Avatar cache managenment functions ------------ */
|
||||
|
||||
typedef struct {
|
||||
uint8_t format;
|
||||
char *suffix;
|
||||
} avatar_format_data_t;
|
||||
|
||||
static const avatar_format_data_t avatar_formats[] = {
|
||||
/* In order of preference */
|
||||
{ TOX_AVATAR_FORMAT_PNG, "png" },
|
||||
{ TOX_AVATAR_FORMAT_NONE, NULL }, /* Must be the last one */
|
||||
};
|
||||
|
||||
|
||||
|
||||
static void set_avatar(Tox *tox, const char *base_dir);
|
||||
|
||||
|
||||
static char *get_avatar_suffix_from_format(uint8_t format)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; avatar_formats[i].format != TOX_AVATAR_FORMAT_NONE; i++)
|
||||
if (avatar_formats[i].format == format)
|
||||
return avatar_formats[i].suffix;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* Load avatar data from a file into a memory buffer 'buf'.
|
||||
* buf must have at least TOX_MAX_AVATAR_DATA_LENGTH bytes
|
||||
* Returns the length of the data sucess or < 0 on error
|
||||
*/
|
||||
static int load_avatar_data(char *fname, uint8_t *buf)
|
||||
{
|
||||
FILE *fp = fopen(fname, "rb");
|
||||
|
||||
if (fp == NULL)
|
||||
return -1; /* Error */
|
||||
|
||||
size_t n = fread(buf, 1, TOX_AVATAR_MAX_DATA_LENGTH, fp);
|
||||
int ret;
|
||||
|
||||
if (ferror(fp) != 0 || n == 0)
|
||||
ret = -1; /* Error */
|
||||
else
|
||||
ret = n;
|
||||
|
||||
fclose(fp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* Save avatar data to a file */
|
||||
static int save_avatar_data(char *fname, uint8_t *data, uint32_t len)
|
||||
{
|
||||
FILE *fp = fopen(fname, "wb");
|
||||
|
||||
if (fp == NULL)
|
||||
return -1; /* Error */
|
||||
|
||||
int ret = 0; /* Ok */
|
||||
|
||||
if (fwrite(data, 1, len, fp) != len)
|
||||
ret = -1; /* Error */
|
||||
|
||||
if (fclose(fp) != 0)
|
||||
ret = -1; /* Error */
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static void byte_to_hex_str(const uint8_t *buf, const size_t buflen, char *dst)
|
||||
{
|
||||
const char *hex_chars = "0123456789ABCDEF";
|
||||
size_t i = 0;
|
||||
size_t j = 0;
|
||||
|
||||
while (i < buflen) {
|
||||
dst[j++] = hex_chars[(buf[i] >> 4) & 0xf];
|
||||
dst[j++] = hex_chars[buf[i] & 0xf];
|
||||
i++;
|
||||
}
|
||||
|
||||
dst[j++] = '\0';
|
||||
}
|
||||
|
||||
/* Make the cache file name for an avatar of the given format for the given
|
||||
* public key.
|
||||
*/
|
||||
static int make_avatar_file_name(char *dst, size_t dst_len, const char *base_dir,
|
||||
const uint8_t format, uint8_t *public_key)
|
||||
{
|
||||
char public_key_str[2 * TOX_PUBLIC_KEY_SIZE + 1];
|
||||
byte_to_hex_str(public_key, TOX_PUBLIC_KEY_SIZE, public_key_str);
|
||||
|
||||
const char *suffix = get_avatar_suffix_from_format(format);
|
||||
|
||||
if (suffix == NULL)
|
||||
return -1; /* Error */
|
||||
|
||||
int n = snprintf(dst, dst_len, "%s/%s/%s.%s", base_dir, AVATAR_DIR_NAME,
|
||||
public_key_str, suffix);
|
||||
dst[dst_len - 1] = '\0';
|
||||
|
||||
if (n >= dst_len)
|
||||
return -1; /* Error: Output truncated */
|
||||
|
||||
return 0; /* Ok */
|
||||
}
|
||||
|
||||
|
||||
/* Load a cached avatar into the buffer 'data' (which must be at least
|
||||
* TOX_MAX_AVATAR_DATA_LENGTH bytes long). Gets the file name from client
|
||||
* id and the given data format.
|
||||
* Returns 0 on success, or -1 on error.
|
||||
*/
|
||||
static int load_user_avatar(Tox *tox, char *base_dir, int friendnum,
|
||||
uint8_t format, uint8_t *hash, uint8_t *data, uint32_t *datalen)
|
||||
{
|
||||
uint8_t addr[TOX_PUBLIC_KEY_SIZE];
|
||||
|
||||
if (tox_get_client_id(tox, friendnum, addr) != 0) {
|
||||
DEBUG("Bad client id, friendnumber=%d", friendnum);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char path[PATH_MAX];
|
||||
int ret = make_avatar_file_name(path, sizeof(path), base_dir, format, addr);
|
||||
|
||||
if (ret != 0) {
|
||||
DEBUG("Can't create an file name for this user/avatar.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = load_avatar_data(path, data);
|
||||
|
||||
if (ret < 0) {
|
||||
DEBUG("Failed to load avatar data.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*datalen = ret;
|
||||
tox_hash(hash, data, *datalen);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Save a user avatar into the cache. Gets the file name from the public key
|
||||
* and the given data format.
|
||||
* Returns 0 on success, or -1 on error.
|
||||
*/
|
||||
static int save_user_avatar(Tox *tox, char *base_dir, int friendnum,
|
||||
uint8_t format, uint8_t *data, uint32_t datalen)
|
||||
{
|
||||
uint8_t addr[TOX_PUBLIC_KEY_SIZE];
|
||||
|
||||
if (tox_get_client_id(tox, friendnum, addr) != 0) {
|
||||
DEBUG("Bad client id, friendnumber=%d", friendnum);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char path[PATH_MAX];
|
||||
int ret = make_avatar_file_name(path, sizeof(path), base_dir, format, addr);
|
||||
|
||||
if (ret != 0) {
|
||||
DEBUG("Can't create a file name for this user/avatar");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return save_avatar_data(path, data, datalen);
|
||||
}
|
||||
|
||||
/* Delete all cached avatars for a given user */
|
||||
static int delete_user_avatar(Tox *tox, char *base_dir, int friendnum)
|
||||
{
|
||||
uint8_t addr[TOX_PUBLIC_KEY_SIZE];
|
||||
|
||||
if (tox_get_client_id(tox, friendnum, addr) != 0) {
|
||||
DEBUG("Bad client id, friendnumber=%d", friendnum);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char path[PATH_MAX];
|
||||
|
||||
/* This iteration is dumb and inefficient */
|
||||
int i;
|
||||
|
||||
for (i = 0; avatar_formats[i].format != TOX_AVATAR_FORMAT_NONE; i++) {
|
||||
int ret = make_avatar_file_name(path, sizeof(path), base_dir,
|
||||
avatar_formats[i].format, addr);
|
||||
|
||||
if (ret != 0) {
|
||||
DEBUG("Failed to create avatar path for friend #%d, format %d\n",
|
||||
friendnum, avatar_formats[i].format);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (unlink(path) == 0)
|
||||
printf("Avatar file %s deleted.\n", path);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* ------------ Protocol callbacks ------------ */
|
||||
|
||||
static void friend_status_cb(Tox *tox, int n, uint8_t status, void *ud)
|
||||
{
|
||||
uint8_t addr[TOX_PUBLIC_KEY_SIZE];
|
||||
char addr_str[2 * TOX_PUBLIC_KEY_SIZE + 1];
|
||||
|
||||
if (tox_get_client_id(tox, n, addr) == 0) {
|
||||
byte_to_hex_str(addr, TOX_PUBLIC_KEY_SIZE, addr_str);
|
||||
printf("Receiving status from %s: %u\n", addr_str, status);
|
||||
}
|
||||
}
|
||||
|
||||
static void friend_avatar_info_cb(Tox *tox, int32_t n, uint8_t format, uint8_t *hash, void *ud)
|
||||
{
|
||||
char *base_dir = (char *) ud;
|
||||
uint8_t addr[TOX_PUBLIC_KEY_SIZE];
|
||||
char addr_str[2 * TOX_PUBLIC_KEY_SIZE + 1];
|
||||
char hash_str[2 * TOX_HASH_LENGTH + 1];
|
||||
|
||||
if (tox_get_client_id(tox, n, addr) == 0) {
|
||||
byte_to_hex_str(addr, TOX_PUBLIC_KEY_SIZE, addr_str);
|
||||
printf("Receiving avatar information from %s.\n", addr_str);
|
||||
} else {
|
||||
DEBUG("tox_get_client_id failed");
|
||||
printf("Receiving avatar information from friend number %u.\n", n);
|
||||
}
|
||||
|
||||
byte_to_hex_str(hash, TOX_HASH_LENGTH, hash_str);
|
||||
DEBUG("format=%u, hash=%s", format, hash_str);
|
||||
|
||||
if (format == TOX_AVATAR_FORMAT_NONE) {
|
||||
printf(" -> User do not have an avatar.\n");
|
||||
/* User have no avatar anymore, delete it from our cache */
|
||||
delete_user_avatar(tox, base_dir, n);
|
||||
} else {
|
||||
/* Check the hash of the currently cached user avatar
|
||||
* WARNING: THIS IS ONLY AN EXAMPLE!
|
||||
*
|
||||
* Real clients should keep the hashes in memory (eg. in the object
|
||||
* used to represent a friend in the friend list) and do not access
|
||||
* the file system or do anything resource intensive in reply of
|
||||
* these events.
|
||||
*/
|
||||
uint32_t cur_av_len;
|
||||
uint8_t cur_av_data[TOX_AVATAR_MAX_DATA_LENGTH];
|
||||
uint8_t cur_av_hash[TOX_HASH_LENGTH];
|
||||
int ret;
|
||||
|
||||
ret = load_user_avatar(tox, base_dir, n, format, cur_av_hash, cur_av_data, &cur_av_len);
|
||||
|
||||
if (ret != 0
|
||||
&& memcpy(cur_av_hash, hash, TOX_HASH_LENGTH) != 0) {
|
||||
printf(" -> Cached avatar is outdated. Requesting avatar data.\n");
|
||||
tox_request_avatar_data(tox, n);
|
||||
} else {
|
||||
printf(" -> Cached avatar is still updated.\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void friend_avatar_data_cb(Tox *tox, int32_t n, uint8_t format,
|
||||
uint8_t *hash, uint8_t *data, uint32_t datalen, void *ud)
|
||||
{
|
||||
char *base_dir = (char *) ud;
|
||||
uint8_t addr[TOX_PUBLIC_KEY_SIZE];
|
||||
char addr_str[2 * TOX_PUBLIC_KEY_SIZE + 1];
|
||||
char hash_str[2 * TOX_HASH_LENGTH + 1];
|
||||
|
||||
if (tox_get_client_id(tox, n, addr) == 0) {
|
||||
byte_to_hex_str(addr, TOX_PUBLIC_KEY_SIZE, addr_str);
|
||||
printf("Receiving avatar data from %s.\n", addr_str);
|
||||
} else {
|
||||
DEBUG("tox_get_client_id failed");
|
||||
printf("Receiving avatar data from friend number %u.\n", n);
|
||||
}
|
||||
|
||||
byte_to_hex_str(hash, TOX_HASH_LENGTH, hash_str);
|
||||
DEBUG("format=%u, datalen=%d, hash=%s\n", format, datalen, hash_str);
|
||||
|
||||
delete_user_avatar(tox, base_dir, n);
|
||||
|
||||
if (format != TOX_AVATAR_FORMAT_NONE) {
|
||||
int ret = save_user_avatar(tox, base_dir, n, format, data, datalen);
|
||||
|
||||
if (ret == 0)
|
||||
printf(" -> Avatar updated in the cache.\n");
|
||||
else
|
||||
printf(" -> Failed to save user avatar.\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void friend_msg_cb(Tox *tox, int n, const uint8_t *msg, uint16_t len, void *ud)
|
||||
{
|
||||
const char *base_dir = (char *) ud;
|
||||
const char *msg_str = (char *) msg;
|
||||
uint8_t addr[TOX_PUBLIC_KEY_SIZE];
|
||||
char addr_str[2 * TOX_PUBLIC_KEY_SIZE + 1];
|
||||
|
||||
if (tox_get_client_id(tox, n, addr) == 0) {
|
||||
byte_to_hex_str(addr, TOX_FRIEND_ADDRESS_SIZE, addr_str);
|
||||
printf("Receiving message from %s:\n %s\n", addr_str, msg);
|
||||
}
|
||||
|
||||
/* Handle bot commands for the tests */
|
||||
char *reply_ptr = NULL;
|
||||
|
||||
if (strstr(msg_str, "!debug-on") != NULL) {
|
||||
print_debug_msgs = true;
|
||||
reply_ptr = "Debug enabled.";
|
||||
} else if (strstr(msg_str, "!debug-off") != NULL) {
|
||||
print_debug_msgs = false;
|
||||
reply_ptr = "Debug disabled.";
|
||||
} else if (strstr(msg_str, "!set-avatar") != NULL) {
|
||||
set_avatar(tox, base_dir);
|
||||
reply_ptr = "Setting image avatar";
|
||||
} else if (strstr(msg_str, "!remove-avatar") != NULL) {
|
||||
int r = tox_set_avatar(tox, TOX_AVATAR_FORMAT_NONE, NULL, 0);
|
||||
DEBUG("tox_set_avatar returned %d", r);
|
||||
reply_ptr = "Removing avatar";
|
||||
}
|
||||
|
||||
/* Add more useful commands here: add friend, etc. */
|
||||
|
||||
char reply[TOX_MAX_MESSAGE_LENGTH];
|
||||
int reply_len;
|
||||
|
||||
if (reply_ptr)
|
||||
reply_len = snprintf(reply, sizeof(reply), "%s", reply_ptr);
|
||||
else
|
||||
reply_len = snprintf(reply, sizeof(reply),
|
||||
"No command found in message: %s", msg);
|
||||
|
||||
reply[sizeof(reply) - 1] = '\0';
|
||||
printf(" -> Reply: %s\n", reply);
|
||||
tox_send_message(tox, n, (uint8_t *) reply, reply_len);
|
||||
}
|
||||
|
||||
|
||||
static void friend_request_cb(Tox *tox, const uint8_t *public_key,
|
||||
const uint8_t *data, uint16_t length, void *ud)
|
||||
{
|
||||
char addr_str[2 * TOX_PUBLIC_KEY_SIZE + 1];
|
||||
byte_to_hex_str(public_key, TOX_PUBLIC_KEY_SIZE, addr_str);
|
||||
printf("Accepting friend request from %s.\n %s\n", addr_str, data);
|
||||
tox_add_friend_norequest(tox, public_key);
|
||||
}
|
||||
|
||||
|
||||
static void set_avatar(Tox *tox, const char *base_dir)
|
||||
{
|
||||
uint8_t addr[TOX_FRIEND_ADDRESS_SIZE];
|
||||
char path[PATH_MAX];
|
||||
uint8_t buf[2 * TOX_AVATAR_MAX_DATA_LENGTH];
|
||||
|
||||
tox_get_address(tox, addr);
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; ; i++) {
|
||||
if (avatar_formats[i].format == TOX_AVATAR_FORMAT_NONE) {
|
||||
tox_set_avatar(tox, TOX_AVATAR_FORMAT_NONE, NULL, 0);
|
||||
printf("No avatar file found, setting to NONE.\n");
|
||||
break;
|
||||
} else {
|
||||
int ret = make_avatar_file_name(path, sizeof(path), base_dir,
|
||||
avatar_formats[i].format, addr);
|
||||
|
||||
if (ret < 0) {
|
||||
printf("Failed to generate avatar file name.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int len = load_avatar_data(path, buf);
|
||||
|
||||
if (len < 0) {
|
||||
printf("Failed to load avatar data from file: %s\n", path);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (len > TOX_AVATAR_MAX_DATA_LENGTH) {
|
||||
printf("Avatar file %s is too big (more than %d bytes)",
|
||||
path, TOX_AVATAR_MAX_DATA_LENGTH);
|
||||
return;
|
||||
}
|
||||
|
||||
ret = tox_set_avatar(tox, avatar_formats[i].format, buf, len);
|
||||
DEBUG("tox_set_avatar returned=%d", ret);
|
||||
|
||||
if (ret == 0)
|
||||
printf("Setting avatar from %s (%d bytes).\n", path, len);
|
||||
else
|
||||
printf("Error setting avatar from %s.\n", path);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void print_avatar_info(Tox *tox)
|
||||
{
|
||||
uint8_t format;
|
||||
uint8_t data[TOX_AVATAR_MAX_DATA_LENGTH];
|
||||
uint8_t hash[TOX_HASH_LENGTH];
|
||||
uint32_t data_length;
|
||||
char hash_str[2 * TOX_HASH_LENGTH + 1];
|
||||
|
||||
int ret = tox_get_self_avatar(tox, &format, data, &data_length, sizeof(data), hash);
|
||||
DEBUG("tox_get_self_avatar returned %d", ret);
|
||||
DEBUG("format: %d, data_length: %d", format, data_length);
|
||||
byte_to_hex_str(hash, TOX_HASH_LENGTH, hash_str);
|
||||
DEBUG("hash: %s", hash_str);
|
||||
}
|
||||
|
||||
|
||||
/* ------------ Initialization functions ------------ */
|
||||
|
||||
/* Create directory to store tha avatars. Returns 0 if it was sucessfuly
|
||||
* created or already existed. Returns -1 on error.
|
||||
*/
|
||||
static int create_avatar_diretory(const char *base_dir)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
int n = snprintf(path, sizeof(path), "%s/%s", base_dir, AVATAR_DIR_NAME);
|
||||
path[sizeof(path) - 1] = '\0';
|
||||
|
||||
if (n >= sizeof(path))
|
||||
return -1;
|
||||
|
||||
if (mkdir(path, 0755) == 0) {
|
||||
return 0; /* Done */
|
||||
} else if (errno == EEXIST) {
|
||||
/* Check if the existing path is a directory */
|
||||
struct stat st;
|
||||
|
||||
if (stat(path, &st) != 0) {
|
||||
perror("stat()ing avatar directory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (S_ISDIR(st.st_mode))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1; /* Error */
|
||||
}
|
||||
|
||||
|
||||
static void *load_bootstrap_data(const char *base_dir, uint32_t *len)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
int n = snprintf(path, sizeof(path), "%s/%s", base_dir, DATA_FILE_NAME);
|
||||
path[sizeof(path) - 1] = '\0';
|
||||
|
||||
if (n >= sizeof(path)) {
|
||||
printf("Load error: path %s too long\n", path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* We should be using POSIX functions here, but let's try to be
|
||||
* compatible with Windows.
|
||||
*/
|
||||
|
||||
FILE *fp = fopen(path, "rb");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("fatal error: file %s not found.\n", path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fseek(fp, 0, SEEK_END) != 0) {
|
||||
printf("seek fail\n");
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int32_t flen = ftell(fp);
|
||||
|
||||
if (flen < 8 || flen > 2e6) {
|
||||
printf("Fatal error: file %s have %u bytes. Out of acceptable range.\n", path, flen);
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fseek(fp, 0, SEEK_SET) != 0) {
|
||||
printf("seek fail\n");
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *buf = malloc(flen);
|
||||
|
||||
if (buf == NULL) {
|
||||
printf("malloc failed, %u bytes", flen);
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*len = fread(buf, 1, flen, fp);
|
||||
fclose(fp);
|
||||
|
||||
if (*len != flen) {
|
||||
printf("fatal: %s have %u bytes, read only %u\n", path, flen, *len);
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
printf("bootstrap data loaded from %s (%u bytes)\n", path, flen);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int save_bootstrap_data(Tox *tox, const char *base_dir)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
int n = snprintf(path, sizeof(path), "%s/%s", base_dir, DATA_FILE_NAME);
|
||||
path[sizeof(path) - 1] = '\0';
|
||||
|
||||
if (n >= sizeof(path)) {
|
||||
printf("Save error: path %s too long\n", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char path_tmp[PATH_MAX];
|
||||
n = snprintf(path_tmp, sizeof(path_tmp), "%s.tmp", path);
|
||||
path_tmp[sizeof(path_tmp) - 1] = '\0';
|
||||
|
||||
if (n >= sizeof(path_tmp)) {
|
||||
printf("error: path %s too long\n", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t len = tox_size(tox);
|
||||
|
||||
if (len < 8 || len > 2e6) {
|
||||
printf("save data length == %u, out of acceptable range\n", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *buf = malloc(len);
|
||||
|
||||
if (buf == NULL) {
|
||||
printf("save data: malloc failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
tox_save(tox, buf);
|
||||
|
||||
FILE *fp = fopen(path_tmp, "wb");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("Error saving data: can't open %s\n", path_tmp);
|
||||
free(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fwrite(buf, 1, len, fp) != len) {
|
||||
printf("Error writing data to %s\n", path_tmp);
|
||||
free(buf);
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
|
||||
if (fclose(fp) != 0) {
|
||||
printf("Error writing data to %s\n", path_tmp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (rename(path_tmp, path) != 0) {
|
||||
printf("Error renaming %s to %s\n", path_tmp, path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Bootstrap data saved to %s\n", path);
|
||||
return 0; /* Done */
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (argc != 2) {
|
||||
printf("usage: %s <data dir>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *base_dir = argv[1];
|
||||
|
||||
if (create_avatar_diretory(base_dir) != 0)
|
||||
printf("Error creating avatar directory.\n");
|
||||
|
||||
Tox *tox = tox_new(NULL);
|
||||
|
||||
uint32_t len;
|
||||
void *data = load_bootstrap_data(base_dir, &len);
|
||||
|
||||
if (data == NULL)
|
||||
return 1;
|
||||
|
||||
ret = tox_load(tox, data, len);
|
||||
free(data);
|
||||
|
||||
if (ret == 0) {
|
||||
printf("Tox initialized\n");
|
||||
} else {
|
||||
printf("Fatal: tox_load returned %d\n", ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
tox_callback_connection_status(tox, friend_status_cb, NULL);
|
||||
tox_callback_friend_message(tox, friend_msg_cb, base_dir);
|
||||
tox_callback_friend_request(tox, friend_request_cb, NULL);
|
||||
tox_callback_avatar_info(tox, friend_avatar_info_cb, base_dir);
|
||||
tox_callback_avatar_data(tox, friend_avatar_data_cb, base_dir);
|
||||
|
||||
uint8_t addr[TOX_FRIEND_ADDRESS_SIZE];
|
||||
char addr_str[2 * TOX_FRIEND_ADDRESS_SIZE + 1];
|
||||
tox_get_address(tox, addr);
|
||||
byte_to_hex_str(addr, TOX_FRIEND_ADDRESS_SIZE, addr_str);
|
||||
printf("Using local tox address: %s\n", addr_str);
|
||||
|
||||
#ifdef TEST_SET_RESET_AVATAR
|
||||
printf("Printing default avatar information:\n");
|
||||
print_avatar_info(tox);
|
||||
|
||||
printf("Setting a new avatar:\n");
|
||||
set_avatar(tox, base_dir);
|
||||
print_avatar_info(tox);
|
||||
|
||||
printf("Removing the avatar we just set:\n");
|
||||
tox_avatar(tox, TOX_AVATARFORMAT_NONE, NULL, 0);
|
||||
print_avatar_info(tox);
|
||||
|
||||
printf("Setting that avatar again:\n");
|
||||
#endif /* TEST_SET_RESET_AVATAR */
|
||||
|
||||
set_avatar(tox, base_dir);
|
||||
print_avatar_info(tox);
|
||||
|
||||
bool waiting = true;
|
||||
time_t last_save = time(0);
|
||||
|
||||
while (1) {
|
||||
if (tox_isconnected(tox) && waiting) {
|
||||
printf("DHT connected.\n");
|
||||
waiting = false;
|
||||
}
|
||||
|
||||
tox_do(tox);
|
||||
|
||||
time_t now = time(0);
|
||||
|
||||
if (now - last_save > 120) {
|
||||
save_bootstrap_data(tox, base_dir);
|
||||
last_save = now;
|
||||
}
|
||||
|
||||
usleep(500000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -45,15 +45,15 @@
|
|||
|
||||
#define c_sleep(x) usleep(1000*x)
|
||||
|
||||
void print_online(Tox *tox, int friendnumber, uint8_t status, void *userdata)
|
||||
void print_online(Tox *tox, uint32_t friendnumber, uint8_t status, void *userdata)
|
||||
{
|
||||
if (status == 1)
|
||||
if (status)
|
||||
printf("\nOther went online.\n");
|
||||
else
|
||||
printf("\nOther went offline.\n");
|
||||
}
|
||||
|
||||
void print_message(Tox *tox, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
|
||||
void print_message(Tox *tox, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata)
|
||||
{
|
||||
int master = *((int *)userdata);
|
||||
write(master, string, length);
|
||||
|
@ -62,7 +62,7 @@ void print_message(Tox *tox, int friendnumber, const uint8_t *string, uint16_t l
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
|
||||
uint8_t ipv6enabled = 1; /* x */
|
||||
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
|
||||
|
||||
if (argvoffset < 0)
|
||||
|
@ -94,14 +94,14 @@ int main(int argc, char *argv[])
|
|||
printf("error setting flags\n");
|
||||
}
|
||||
|
||||
Tox *tox = tox_new(0);
|
||||
tox_callback_connection_status(tox, print_online, NULL);
|
||||
Tox *tox = tox_new(0, 0, 0, 0);
|
||||
tox_callback_friend_connection_status(tox, print_online, NULL);
|
||||
tox_callback_friend_message(tox, print_message, master);
|
||||
|
||||
|
||||
uint16_t port = atoi(argv[argvoffset + 2]);
|
||||
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
|
||||
int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], port, binary_string);
|
||||
int res = tox_bootstrap(tox, argv[argvoffset + 1], port, binary_string, 0);
|
||||
free(binary_string);
|
||||
|
||||
if (!res) {
|
||||
|
@ -109,11 +109,11 @@ int main(int argc, char *argv[])
|
|||
exit(1);
|
||||
}
|
||||
|
||||
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
||||
tox_get_address(tox, address);
|
||||
uint8_t address[TOX_ADDRESS_SIZE];
|
||||
tox_self_get_address(tox, address);
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
|
||||
for (i = 0; i < TOX_ADDRESS_SIZE; i++) {
|
||||
printf("%02X", address[i]);
|
||||
}
|
||||
|
||||
|
@ -125,10 +125,10 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
uint8_t *bin_id = hex_string_to_bin(temp_id);
|
||||
int num = tox_add_friend(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
uint32_t num = tox_friend_add(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"), 0);
|
||||
free(bin_id);
|
||||
|
||||
if (num < 0) {
|
||||
if (num == UINT32_MAX) {
|
||||
printf("\nSomething went wrong when adding friend.\n");
|
||||
return 1;
|
||||
}
|
||||
|
@ -136,22 +136,22 @@ int main(int argc, char *argv[])
|
|||
uint8_t notconnected = 1;
|
||||
|
||||
while (1) {
|
||||
if (tox_isconnected(tox) && notconnected) {
|
||||
if (tox_get_connection_status(tox) && notconnected) {
|
||||
printf("\nDHT connected.\n");
|
||||
notconnected = 0;
|
||||
}
|
||||
|
||||
while (tox_get_friend_connection_status(tox, num) == 1) {
|
||||
while (tox_friend_get_connection_status(tox, num, 0)) {
|
||||
uint8_t buf[TOX_MAX_MESSAGE_LENGTH];
|
||||
ret = read(*master, buf, sizeof(buf));
|
||||
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
tox_send_message(tox, num, buf, ret);
|
||||
tox_send_message(tox, num, buf, ret, 0);
|
||||
}
|
||||
|
||||
tox_do(tox);
|
||||
tox_iteration(tox);
|
||||
c_sleep(1);
|
||||
}
|
||||
|
||||
|
|
|
@ -192,9 +192,9 @@ void write_file(Tox *m, int friendnumber, uint8_t filenumber, const uint8_t *dat
|
|||
printf("Error writing data\n");
|
||||
}
|
||||
|
||||
void print_online(Tox *tox, int friendnumber, uint8_t status, void *userdata)
|
||||
void print_online(Tox *tox, uint32_t friendnumber, uint8_t status, void *userdata)
|
||||
{
|
||||
if (status == 1)
|
||||
if (status)
|
||||
printf("\nOther went online.\n");
|
||||
else
|
||||
printf("\nOther went offline.\n");
|
||||
|
@ -202,7 +202,7 @@ void print_online(Tox *tox, int friendnumber, uint8_t status, void *userdata)
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
|
||||
uint8_t ipv6enabled = 1; /* x */
|
||||
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
|
||||
|
||||
if (argvoffset < 0)
|
||||
|
@ -214,15 +214,15 @@ int main(int argc, char *argv[])
|
|||
exit(0);
|
||||
}
|
||||
|
||||
Tox *tox = tox_new(0);
|
||||
Tox *tox = tox_new(0, 0, 0, 0);
|
||||
tox_callback_file_data(tox, write_file, NULL);
|
||||
tox_callback_file_control(tox, file_print_control, NULL);
|
||||
tox_callback_file_send_request(tox, file_request_accept, NULL);
|
||||
tox_callback_connection_status(tox, print_online, NULL);
|
||||
tox_callback_friend_connection_status(tox, print_online, NULL);
|
||||
|
||||
uint16_t port = atoi(argv[argvoffset + 2]);
|
||||
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
|
||||
int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], port, binary_string);
|
||||
int res = tox_bootstrap(tox, argv[argvoffset + 1], port, binary_string, 0);
|
||||
free(binary_string);
|
||||
|
||||
if (!res) {
|
||||
|
@ -230,11 +230,11 @@ int main(int argc, char *argv[])
|
|||
exit(1);
|
||||
}
|
||||
|
||||
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
||||
tox_get_address(tox, address);
|
||||
uint8_t address[TOX_ADDRESS_SIZE];
|
||||
tox_self_get_address(tox, address);
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
|
||||
for (i = 0; i < TOX_ADDRESS_SIZE; i++) {
|
||||
printf("%02X", address[i]);
|
||||
}
|
||||
|
||||
|
@ -246,10 +246,10 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
uint8_t *bin_id = hex_string_to_bin(temp_id);
|
||||
int num = tox_add_friend(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
uint32_t num = tox_friend_add(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"), 0);
|
||||
free(bin_id);
|
||||
|
||||
if (num < 0) {
|
||||
if (num == UINT32_MAX) {
|
||||
printf("\nSomething went wrong when adding friend.\n");
|
||||
return 1;
|
||||
}
|
||||
|
@ -260,12 +260,12 @@ int main(int argc, char *argv[])
|
|||
uint8_t notconnected = 1;
|
||||
|
||||
while (1) {
|
||||
if (tox_isconnected(tox) && notconnected) {
|
||||
if (tox_get_connection_status(tox) && notconnected) {
|
||||
printf("\nDHT connected.\n");
|
||||
notconnected = 0;
|
||||
}
|
||||
|
||||
if (not_sending() && tox_get_friend_connection_status(tox, num)) {
|
||||
if (not_sending() && tox_friend_get_connection_status(tox, num, 0)) {
|
||||
d = opendir(path);
|
||||
|
||||
if (d) {
|
||||
|
@ -291,7 +291,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
send_filesenders(tox);
|
||||
tox_do(tox);
|
||||
tox_iteration(tox);
|
||||
c_sleep(1);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user