toxcore/testing/nTox_win32.c

506 lines
13 KiB
C
Raw Normal View History

/* nTox_win32.c
*
* Textual frontend for Tox - Windows version
*
* Copyright (C) 2013 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
* Tox 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 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. If not, see <http://www.gnu.org/licenses/>.
2013-08-06 08:20:41 +08:00
*
*/
#include "nTox_win32.h"
#include "misc_tools.h"
#include <process.h>
2013-08-04 19:01:20 +08:00
typedef struct {
uint8_t id[CLIENT_ID_SIZE];
uint8_t accepted;
} Friend_request;
Friend_request pending_requests[256];
2013-07-31 17:52:08 +08:00
uint8_t num_requests = 0;
uint32_t maxnumfriends;
char line[STRING_LENGTH];
char users_id[200];
int friend_request_received;
Messenger *messenger;
void do_header()
{
system("cls");
printf(users_id);
printf("\n---------------------------------");
2013-07-30 16:56:39 +08:00
printf("\n[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status), /n nick (to change nickname), /l (lists friends), /d friendnumber (deletes friend), /q (to quit), /r (reset screen)");
printf("\n---------------------------------");
}
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
{
friend_request_received = 1;
printf("\n\n[i] received friend request with message\n");
2013-08-17 01:11:09 +08:00
printf("'%s'", (char *)data);
char numchar[100];
sprintf(numchar, "\n[i] accept request with /a %u\n\n", num_requests);
printf(numchar);
2013-08-04 19:01:20 +08:00
memcpy(pending_requests[num_requests].id, public_key, CLIENT_ID_SIZE);
pending_requests[num_requests].accepted = 0;
++num_requests;
}
2013-08-17 01:11:09 +08:00
void print_message(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{
char name[MAX_NAME_LENGTH];
2013-08-17 01:11:09 +08:00
getname(messenger, friendnumber, (uint8_t *)name);
char msg[100 + length + strlen(name) + 1];
time_t rawtime;
2013-08-17 01:11:09 +08:00
struct tm *timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
2013-08-17 01:11:09 +08:00
char *temp = asctime(timeinfo);
size_t len = strlen(temp);
2013-08-17 01:11:09 +08:00
temp[len - 1] = '\0';
sprintf(msg, "\n[%d] %s <%s> %s\n\n", friendnumber, temp, name, string); // timestamp
printf(msg);
}
void print_nickchange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{
char name[MAX_NAME_LENGTH];
2013-08-17 01:11:09 +08:00
getname(messenger, friendnumber, (uint8_t *)name);
char msg[100 + length];
sprintf(msg, "\n\n[i] [%d] %s is now known as %s.\n\n", friendnumber, name, string);
printf(msg);
}
void print_statuschange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
2013-08-01 06:01:59 +08:00
{
char name[MAX_NAME_LENGTH];
2013-08-17 01:11:09 +08:00
getname(messenger, friendnumber, (uint8_t *)name);
char msg[100 + length + strlen(name) + 1];
2013-08-01 06:01:59 +08:00
sprintf(msg, "\n\n[i] [%d] %s's status changed to %s.\n\n", friendnumber, name, string);
printf(msg);
}
2013-08-06 08:20:41 +08:00
void load_key()
{
FILE *data_file = NULL;
2013-08-17 01:11:09 +08:00
data_file = fopen("data", "r");
2013-07-31 14:15:01 +08:00
if (data_file) {
fseek(data_file, 0, SEEK_END);
int size = ftell(data_file);
fseek(data_file, 0, SEEK_SET);
uint8_t data[size];
2013-08-17 01:11:09 +08:00
2013-07-31 14:15:01 +08:00
if (fread(data, sizeof(uint8_t), size, data_file) != size) {
printf("\n[i] Could not read the data file. Exiting.");
exit(1);
}
Messenger_load(messenger, data, size);
} else {
int size = Messenger_size(messenger);
uint8_t data[size];
Messenger_save(messenger, data);
2013-08-03 04:56:53 +08:00
fclose(data_file);
data_file = fopen("data", "w");
2013-07-31 14:15:01 +08:00
if (fwrite(data, sizeof(uint8_t), size, data_file) != size) {
printf("\n[i] Could not write data to file. Exiting.");
exit(1);
}
}
2013-08-17 01:11:09 +08:00
fclose(data_file);
}
void add_friend()
{
int i;
char temp_id[128];
2013-08-17 01:11:09 +08:00
2013-08-06 08:20:41 +08:00
for (i = 0; i < 128; i++)
2013-08-17 01:11:09 +08:00
temp_id[i] = line[i + 3];
int num = m_addfriend(messenger, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
if (num >= 0) {
char numstring[100];
sprintf(numstring, "\n[i] Friend request sent. Wait to be accepted. Friend id: %d\n\n", num);
printf(numstring);
++maxnumfriends;
2013-08-17 01:11:09 +08:00
} else if (num == -1)
printf("\n[i] Message is too long.\n\n");
2013-08-17 01:11:09 +08:00
else if (num == -2)
printf("\n[i] Please add a message to your friend request.\n\n");
2013-08-17 01:11:09 +08:00
else if (num == -3)
printf("\n[i] That appears to be your own ID.\n\n");
2013-08-17 01:11:09 +08:00
else if (num == -4)
printf("\n[i] Friend request already sent.\n\n");
2013-08-17 01:11:09 +08:00
else if (num == -5)
printf("\n[i] Undefined error when adding friend\n\n");
}
void list_friends()
{
int i;
2013-08-17 01:11:09 +08:00
printf("\n[i] Friend List");
printf("----- PENDING -----\n\n");
for (i = 0; i <= maxnumfriends; i++) {
char name[MAX_NAME_LENGTH];
2013-08-17 01:11:09 +08:00
getname(messenger, i, (uint8_t *)name);
if (m_friendstatus(messenger, i) > 0 && m_friendstatus(messenger, i) < 4)
2013-08-17 01:11:09 +08:00
printf("[%d] %s\n", i, (uint8_t *)name);
}
2013-08-17 01:11:09 +08:00
printf("\n");
printf("----- ACTIVE -----\n\n");
for (i = 0; i <= maxnumfriends; i++) {
char name[MAX_NAME_LENGTH];
2013-08-17 01:11:09 +08:00
getname(messenger, i, (uint8_t *)name);
if (m_friendstatus(messenger, i) == 4)
2013-08-17 01:11:09 +08:00
printf("[%d] %s\n", i, (uint8_t *)name);
}
printf("\n");
}
void delete_friend()
{
size_t len = strlen(line);
2013-08-17 01:11:09 +08:00
char numstring[len - 3];
int i;
2013-08-17 01:11:09 +08:00
for (i = 0; i < len; i++) {
2013-08-17 01:11:09 +08:00
if (line[i + 3] != ' ')
numstring[i] = line[i + 3];
}
2013-08-17 01:11:09 +08:00
int num = atoi(numstring);
m_delfriend(messenger, num);
--maxnumfriends;
printf("\n\n");
}
void message_friend()
{
size_t len = strlen(line);
2013-08-17 01:11:09 +08:00
char numstring[len - 3];
char message[len - 3];
int i;
2013-08-17 01:11:09 +08:00
for (i = 0; i < len; i++) {
2013-08-17 01:11:09 +08:00
if (line[i + 3] != ' ')
numstring[i] = line[i + 3];
else {
int j;
2013-08-17 01:11:09 +08:00
for (j = (i + 1); j < len; j++)
message[j - i - 1] = line[j + 3];
break;
}
}
int num = atoi(numstring);
2013-08-17 01:11:09 +08:00
if (m_sendmessage(messenger, num, (uint8_t *) message, sizeof(message)) != 1)
printf("\n[i] could not send message (they may be offline): %s\n", message);
2013-08-17 01:11:09 +08:00
else
printf("\n");
}
void change_nickname()
{
uint8_t name[MAX_NAME_LENGTH];
int i = 0;
size_t len = strlen(line);
2013-08-17 01:11:09 +08:00
for (i = 3; i < len; i++) {
2013-08-06 08:20:41 +08:00
if (line[i] == 0 || line[i] == '\n')
break;
2013-08-17 01:11:09 +08:00
name[i - 3] = line[i];
}
2013-08-17 01:11:09 +08:00
name[i - 3] = 0;
setname(messenger, name, i);
char numstring[100];
2013-08-17 01:11:09 +08:00
sprintf(numstring, "\n[i] changed nick to %s\n\n", (char *)name);
printf(numstring);
FILE *name_file = NULL;
name_file = fopen("namefile.txt", "w");
2013-08-17 01:11:09 +08:00
fprintf(name_file, "%s", (char *)name);
fclose(name_file);
}
void change_status(int savetofile)
{
uint8_t status[MAX_STATUSMESSAGE_LENGTH];
int i = 0;
size_t len = strlen(line);
2013-08-17 01:11:09 +08:00
for (i = 3; i < len; i++) {
2013-08-06 08:20:41 +08:00
if (line[i] == 0 || line[i] == '\n')
break;
2013-08-17 01:11:09 +08:00
status[i - 3] = line[i];
}
2013-08-17 01:11:09 +08:00
status[i - 3] = 0;
m_set_statusmessage(messenger, status, strlen((char *)status));
char numstring[100];
2013-08-17 01:11:09 +08:00
sprintf(numstring, "\n[i] changed status to %s\n\n", (char *)status);
printf(numstring);
if (savetofile == 1) {
2013-08-17 01:11:09 +08:00
FILE *status_file = NULL;
status_file = fopen("statusfile.txt", "w");
2013-08-17 01:11:09 +08:00
fprintf(status_file, "%s", (char *)status);
fclose(status_file);
}
}
void accept_friend_request()
{
friend_request_received = 0;
uint8_t numf = atoi(line + 3);
char numchar[100];
2013-08-17 01:11:09 +08:00
2013-08-04 19:01:20 +08:00
if (numf >= num_requests || pending_requests[numf].accepted) {
sprintf(numchar, "\n[i] you either didn't receive that request or you already accepted it");
printf(numchar);
} else {
int num = m_addfriend_norequest(messenger, pending_requests[numf].id);
2013-08-17 01:11:09 +08:00
2013-08-04 19:01:20 +08:00
if (num != -1) {
pending_requests[numf].accepted = 1;
sprintf(numchar, "\n[i] Added friendnumber: %d\n\n", num);
printf(numchar);
++maxnumfriends;
} else {
sprintf(numchar, "[i] failed to add friend");
printf(numchar);
}
}
}
2013-08-17 01:11:09 +08:00
void line_eval(char *line)
{
2013-08-17 01:11:09 +08:00
if (line[0] == '/') {
2013-07-31 14:15:01 +08:00
char inpt_command = line[1];
2013-08-17 01:11:09 +08:00
if (inpt_command == 'f') {
add_friend();
}
2013-07-31 14:15:01 +08:00
else if (inpt_command == 'r') {
do_header();
printf("\n\n");
}
2013-07-31 14:15:01 +08:00
else if (inpt_command == 'l') {
list_friends();
}
2013-07-31 14:15:01 +08:00
else if (inpt_command == 'd') {
delete_friend();
}
/* Send message to friend */
2013-07-31 14:15:01 +08:00
else if (inpt_command == 'm') {
message_friend();
}
2013-07-31 14:15:01 +08:00
else if (inpt_command == 'n') {
change_nickname();
}
2013-07-31 14:15:01 +08:00
else if (inpt_command == 's') {
change_status(1);
}
2013-07-31 14:15:01 +08:00
else if (inpt_command == 'a') {
if (friend_request_received == 1)
accept_friend_request();
}
/* EXIT */
2013-08-06 08:20:41 +08:00
else if (inpt_command == 'q') {
strcpy(line, "---Offline");
change_status(0);
exit(EXIT_SUCCESS);
}
}
}
void get_input()
{
2013-08-17 01:11:09 +08:00
while (1) {
fgets(line, STRING_LENGTH, stdin);
line_eval(line);
strcpy(line, "");
}
}
int main(int argc, char *argv[])
{
if (argc < 4) {
printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
exit(0);
}
2013-08-17 01:11:09 +08:00
messenger = initMessenger();
2013-08-17 01:11:09 +08:00
if (messenger == 0) {
printf("initMessenger failed");
exit(0);
}
2013-08-17 01:11:09 +08:00
if (argc > 4) {
2013-08-17 01:11:09 +08:00
if (strncmp(argv[4], "nokey", 6) < 0) {
}
} else {
load_key();
}
int nameloaded = 0;
int statusloaded = 0;
2013-08-17 01:11:09 +08:00
FILE *name_file = NULL;
name_file = fopen("namefile.txt", "r");
2013-08-17 01:11:09 +08:00
if (name_file) {
uint8_t name[MAX_NAME_LENGTH];
2013-08-17 01:11:09 +08:00
while (fgets(line, MAX_NAME_LENGTH, name_file) != NULL) {
2013-08-17 01:11:09 +08:00
sscanf(line, "%s", (char *)name);
}
2013-08-17 01:11:09 +08:00
setname(messenger, name, strlen((char *)name) + 1);
nameloaded = 1;
printf("%s\n", name);
2013-08-03 04:56:53 +08:00
fclose(name_file);
2013-08-06 08:20:41 +08:00
}
2013-08-17 01:11:09 +08:00
FILE *status_file = NULL;
status_file = fopen("statusfile.txt", "r");
2013-08-17 01:11:09 +08:00
if (status_file) {
uint8_t status[MAX_STATUSMESSAGE_LENGTH];
2013-08-17 01:11:09 +08:00
while (fgets(line, MAX_STATUSMESSAGE_LENGTH, status_file) != NULL) {
2013-08-17 01:11:09 +08:00
sscanf(line, "%s", (char *)status);
}
2013-08-17 01:11:09 +08:00
m_set_statusmessage(messenger, status, strlen((char *)status) + 1);
statusloaded = 1;
printf("%s\n", status);
2013-08-03 04:56:53 +08:00
fclose(status_file);
}
m_callback_friendrequest(messenger, print_request, &status_file);
m_callback_friendmessage(messenger, print_message, &status_file);
m_callback_namechange(messenger, print_nickchange, &status_file);
m_callback_statusmessage(messenger, print_statuschange, &status_file);
2013-07-31 14:15:01 +08:00
char idstring1[PUB_KEY_BYTES][5];
char idstring2[PUB_KEY_BYTES][5];
int i;
2013-08-17 01:11:09 +08:00
for (i = 0; i < PUB_KEY_BYTES; i++) {
if (self_public_key[i] < (PUB_KEY_BYTES / 2))
strcpy(idstring1[i], "0");
2013-08-06 08:20:41 +08:00
else
strcpy(idstring1[i], "");
2013-08-17 01:11:09 +08:00
sprintf(idstring2[i], "%hhX", self_public_key[i]);
}
2013-08-17 01:11:09 +08:00
strcpy(users_id, "[i] your ID: ");
2013-07-31 14:15:01 +08:00
int j;
2013-08-17 01:11:09 +08:00
2013-07-31 14:15:01 +08:00
for (j = 0; j < PUB_KEY_BYTES; j++) {
2013-08-17 01:11:09 +08:00
strcat(users_id, idstring1[j]);
strcat(users_id, idstring2[j]);
}
do_header();
2013-08-17 01:11:09 +08:00
IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2]));
int resolved_address = resolve_addr(argv[1]);
2013-08-17 01:11:09 +08:00
if (resolved_address != 0)
bootstrap_ip_port.ip.i = resolved_address;
2013-08-06 08:20:41 +08:00
else
exit(1);
2013-08-17 01:11:09 +08:00
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
int c;
int on = 0;
_beginthread(get_input, 0, NULL);
if (nameloaded == 1) {
printf("\nNickname automatically loaded");
printf("\n---------------------------------");
}
if (statusloaded == 1) {
printf("\nStatus automatically loaded");
printf("\n---------------------------------");
}
2013-08-17 01:11:09 +08:00
while (1) {
if (on == 1 && DHT_isconnected() == -1) {
printf("\n---------------------------------");
printf("\n[i] Disconnected from the DHT");
printf("\n---------------------------------\n\n");
on = 0;
}
2013-08-17 01:11:09 +08:00
if (on == 0 && DHT_isconnected()) {
printf("\n[i] Connected to DHT");
printf("\n---------------------------------\n\n");
on = 1;
}
2013-08-17 01:11:09 +08:00
doMessenger(messenger);
2013-07-31 11:32:18 +08:00
Sleep(1);
}
2013-08-17 01:11:09 +08:00
return 0;
}