Merge branch 'master' into new-api

This commit is contained in:
irungentoo 2013-08-23 06:50:42 -04:00
commit bcb87139a2
22 changed files with 1 additions and 3148 deletions

View File

@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 2.6.0)
cmake_policy(SET CMP0011 NEW)
#include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_cryptosendfiletest.cmake)
#include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_sendfiletest.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_test.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testclient.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testserver.cmake)
@ -11,8 +9,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Messenger_test.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/crypto_speed_test.cmake)
if(WIN32)
# include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox_win32.cmake)
#Do nothing
else()
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox.cmake)
add_subdirectory(toxic)
endif()

View File

@ -1,258 +0,0 @@
/* DHT cryptosendfiletest
*
* This program sends or receives a friend request.
*
* it also sends the encrypted data from a file to another client.
* Receives the file data that that client sends us.
*
* NOTE: this program simulates 33% packet loss.
*
* This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/$HOSTNAME/lib/amd64/* DHT_cryptosendfiletest.c
*
*
* Command line arguments are the ip and port of a node (for bootstrapping).
*
* Saves all received data to: received.txt
*
* EX: ./test 127.0.0.1 33445 filename.txt
*
* 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/>.
*
*/
#include "../core/network.h"
#include "../core/DHT.h"
#include "../core/net_crypto.h"
#include "misc_tools.h"
#include <string.h>
/* Sleep function (x = milliseconds) */
#ifdef WIN32
#define c_sleep(x) Sleep(1*x)
#else
#include <unistd.h>
#include <arpa/inet.h>
#define c_sleep(x) usleep(1000*x)
#endif
#define PORT 33445
void printip(IP_Port ip_port)
{
printf("\nIP: %u.%u.%u.%u Port: %u\n", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3],
ntohs(ip_port.port));
}
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
int main(int argc, char *argv[])
{
if (argc < 4) {
printf("usage %s ip port filename(of file to send)\n", argv[0]);
exit(0);
}
new_keys();
printf("OUR ID: ");
uint32_t i;
for (i = 0; i < 32; i++) {
if (self_public_key[i] < 16)
printf("0");
printf("%hhX", self_public_key[i]);
}
printf("\n");
memcpy(self_client_id, self_public_key, 32);
char temp_id[128];
printf("Enter the client_id of the friend to connect to (32 bytes HEX format):\n");
scanf("%s", temp_id);
uint8_t friend_id[32];
memcpy(friend_id, hex_string_to_bin(temp_id), 32);
/* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */
DHT_addfriend(friend_id);
IP_Port friend_ip;
int connection = -1;
int inconnection = -1;
uint8_t acceptedfriend_public_key[crypto_box_PUBLICKEYBYTES];
int friendrequest = -1;
uint8_t request_data[512];
/* initialize networking
* bind to ip 0.0.0.0:PORT */
IP ip;
ip.i = 0;
init_networking(ip, PORT);
initNetCrypto();
perror("Initialization");
IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2]));
bootstrap_ip_port.ip.i = inet_addr(argv[1]);
DHT_bootstrap(bootstrap_ip_port);
IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length;
uint8_t buffer1[128];
int read1 = 0;
uint8_t buffer2[128];
int read2 = 0;
FILE *file1 = fopen(argv[3], "rb");
if ( file1 == NULL ) {
printf("Error opening file.\n");
return 1;
}
FILE *file2 = fopen("received.txt", "wb");
if ( file2 == NULL ) {
return 1;
}
read1 = fread(buffer1, 1, 128, file1);
while (1) {
while (receivepacket(&ip_port, data, &length) != -1) {
if (rand() % 3 != 1) { /* simulate packet loss */
if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) {
/* if packet is not recognized */
printf("Received unhandled packet with length: %u\n", length);
} else {
printf("Received handled packet with length: %u\n", length);
}
}
}
friend_ip = DHT_getfriendip(friend_id);
if (friend_ip.ip.i != 0) {
if (connection == -1 && friendrequest == -1) {
printf("Sending friend request to peer:");
printip(friend_ip);
friendrequest = send_friendrequest(friend_id, friend_ip, (uint8_t *) "Hello World", 12);
/* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */
/* connection = new_connection(friend_ip); */
}
if (check_friendrequest(friendrequest) == 1) {
printf("Started connecting to friend:");
connection = crypto_connect(friend_id, friend_ip);
}
}
if (inconnection == -1) {
uint8_t secret_nonce[crypto_box_NONCEBYTES];
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
inconnection = crypto_inbound(public_key, secret_nonce, session_key);
inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key);
/* inconnection = incoming_connection(); */
if (inconnection != -1) {
printf("Someone connected to us:\n");
/* printip(connection_ip(inconnection)); */
}
}
if (handle_friendrequest(acceptedfriend_public_key, request_data) > 1) {
printf("RECEIVED FRIEND REQUEST: %s\n", request_data);
}
/* if someone connected to us write what he sends to a file
* also send him our file. */
if (inconnection != -1) {
if (write_cryptpacket(inconnection, buffer1, read1)) {
printf("Wrote data1.\n");
read1 = fread(buffer1, 1, 128, file1);
}
read2 = read_cryptpacket(inconnection, buffer2);
if (read2 != 0) {
printf("Received data1.\n");
if (!fwrite(buffer2, read2, 1, file2)) {
printf("file write error1\n");
}
if (read2 < 128) {
printf("Closed file1 %u\n", read2);
fclose(file2);
}
}
/* if buffer is empty and the connection timed out. */
else if (is_cryptoconnected(inconnection) == 4) {
crypto_kill(inconnection);
}
}
/* if we are connected to a friend send him data from the file.
* also put what he sends us in a file. */
if (is_cryptoconnected(connection) >= 3) {
if (write_cryptpacket(0, buffer1, read1)) {
printf("Wrote data2.\n");
read1 = fread(buffer1, 1, 128, file1);
}
read2 = read_cryptpacket(0, buffer2);
if (read2 != 0) {
printf("Received data2.\n");
if (!fwrite(buffer2, read2, 1, file2)) {
printf("file write error2\n");
}
if (read2 < 128) {
printf("Closed file2 %u\n", read2);
fclose(file2);
}
}
/* if buffer is empty and the connection timed out. */
else if (is_cryptoconnected(connection) == 4) {
crypto_kill(connection);
}
}
doDHT();
doLossless_UDP();
doNetCrypto();
/*print_clientlist();
*print_friendlist();
*c_sleep(300); */
c_sleep(1);
}
shutdown_networking();
return 0;
}

View File

@ -1,196 +0,0 @@
/* DHT sendfiletest
*
* Sends the data from a file to another client.
* Receives the file data that that client sends us.
*
* NOTE: this program simulates 33% packet loss.
*
* Compile with: gcc -O2 -Wall -o test ../core/DHT.c ../core/network.c ../core/Lossless_UDP.c DHT_sendfiletest.c
*
* Command line arguments are the ip and port of a node (for bootstrapping), the
* client_id (32 bytes) of the friend you want to send the data in filename to and
* the client_id this node will take.
*
* Saves all received data to: received.txt
*
* EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef filename.txt ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeg
*
* 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/>.
*
*/
#include "../core/network.h"
#include "../core/DHT.h"
#include "../core/Lossless_UDP.h"
#include <string.h>
//Sleep function (x = milliseconds)
#ifdef WIN32
#define c_sleep(x) Sleep(1*x)
#else
#include <unistd.h>
#include <arpa/inet.h>
#define c_sleep(x) usleep(1000*x)
#endif
#define PORT 33445
void printip(IP_Port ip_port)
{
printf("\nIP: %u.%u.%u.%u Port: %u\n", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3],
ntohs(ip_port.port));
}
int main(int argc, char *argv[])
{
//memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
if (argc < 6) {
printf("usage %s ip port client_id(of friend to find ip_port of) filename(of file to send) client_id(ours)\n", argv[0]);
exit(0);
}
DHT_addfriend((uint8_t *)argv[3]);
IP_Port friend_ip;
int connection = -1;
int inconnection = -1;
//initialize networking
//bind to ip 0.0.0.0:PORT
IP ip;
ip.i = 0;
init_networking(ip, PORT);
memcpy(self_client_id, argv[5], 32);
perror("Initialization");
IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2]));
bootstrap_ip_port.ip.i = inet_addr(argv[1]);
DHT_bootstrap(bootstrap_ip_port);
IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length;
uint8_t buffer1[128];
int read1 = 0;
uint8_t buffer2[128];
int read2 = 0;
FILE *file1 = fopen(argv[4], "rb");
if (file1 == NULL) {
printf("Error opening file.\n");
return 1;
}
FILE *file2 = fopen("received.txt", "wb");
if (file2 == NULL)
return 1;
read1 = fread(buffer1, 1, 128, file1);
while (1) {
while (receivepacket(&ip_port, data, &length) != -1) {
if (rand() % 3 != 1) { /* simulate packet loss */
if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
printf("Received unhandled packet with length: %u\n", length); /* if packet is not recognized */
else
printf("Received handled packet with length: %u\n", length);
}
}
friend_ip = DHT_getfriendip((uint8_t *)argv[3]);
if (friend_ip.ip.i != 0) {
if (connection == -1) {
printf("Started connecting to friend:");
printip(friend_ip);
connection = new_connection(friend_ip);
}
}
if (inconnection == -1) {
inconnection = incoming_connection();
if (inconnection != -1) {
printf("Someone connected to us:");
printip(connection_ip(inconnection));
}
}
/* if someone connected to us write what he sends to a file */
/* also send him our file. */
if (inconnection != -1) {
if (write_packet(inconnection, buffer1, read1)) {
printf("Wrote data.\n");
read1 = fread(buffer1, 1, 128, file1);
}
read2 = read_packet(inconnection, buffer2);
if (read2 != 0) {
printf("Received data.\n");
if (!fwrite(buffer2, read2, 1, file2))
printf("file write error\n");
if (read2 < 128) {
fclose(file2);
}
}
}
/* if we are connected to a friend send him data from the file.
* also put what he sends us in a file. */
if (is_connected(connection) == 3) {
if (write_packet(0, buffer1, read1)) {
printf("Wrote data.\n");
read1 = fread(buffer1, 1, 128, file1);
}
read2 = read_packet(0, buffer2);
if (read2 != 0) {
printf("Received data.\n");
if (!fwrite(buffer2, read2, 1, file2))
printf("file write error\n");
if (read2 < 128)
fclose(file2);
}
}
doDHT();
doLossless_UDP();
/* print_clientlist();
* print_friendlist();
* c_sleep(300); */
c_sleep(1);
}
shutdown_networking();
return 0;
}

View File

@ -1,10 +0,0 @@
cmake_minimum_required(VERSION 2.6.0)
project(DHT_cryptosendfiletest C)
set(exe_name DHT_cryptosendfiletest)
add_executable(${exe_name}
DHT_cryptosendfiletest.c
misc_tools.c)
linkCoreLibraries(${exe_name})

View File

@ -1,9 +0,0 @@
cmake_minimum_required(VERSION 2.6.0)
project(DHT_sendfiletest C)
set(exe_name DHT_sendfiletest)
add_executable(${exe_name}
DHT_sendfiletest.c)
linkCoreLibraries(${exe_name})

View File

@ -1,9 +0,0 @@
cmake_minimum_required(VERSION 2.6.0)
project(nTox_win32 C)
set(exe_name nTox_win32)
add_executable(${exe_name}
nTox_win32.c misc_tools.c)
linkCoreLibraries(${exe_name})

View File

@ -1,505 +0,0 @@
/* 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/>.
*
*/
#include "nTox_win32.h"
#include "misc_tools.h"
#include <process.h>
typedef struct {
uint8_t id[CLIENT_ID_SIZE];
uint8_t accepted;
} Friend_request;
Friend_request pending_requests[256];
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---------------------------------");
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");
printf("'%s'", (char *)data);
char numchar[100];
sprintf(numchar, "\n[i] accept request with /a %u\n\n", num_requests);
printf(numchar);
memcpy(pending_requests[num_requests].id, public_key, CLIENT_ID_SIZE);
pending_requests[num_requests].accepted = 0;
++num_requests;
}
void print_message(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{
char name[MAX_NAME_LENGTH];
getname(messenger, friendnumber, (uint8_t *)name);
char msg[100 + length + strlen(name) + 1];
time_t rawtime;
struct tm *timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
char *temp = asctime(timeinfo);
size_t len = strlen(temp);
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];
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)
{
char name[MAX_NAME_LENGTH];
getname(messenger, friendnumber, (uint8_t *)name);
char msg[100 + length + strlen(name) + 1];
sprintf(msg, "\n\n[i] [%d] %s's status changed to %s.\n\n", friendnumber, name, string);
printf(msg);
}
void load_key()
{
FILE *data_file = NULL;
data_file = fopen("data", "r");
if (data_file) {
fseek(data_file, 0, SEEK_END);
int size = ftell(data_file);
fseek(data_file, 0, SEEK_SET);
uint8_t data[size];
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);
fclose(data_file);
data_file = fopen("data", "w");
if (fwrite(data, sizeof(uint8_t), size, data_file) != size) {
printf("\n[i] Could not write data to file. Exiting.");
exit(1);
}
}
fclose(data_file);
}
void add_friend()
{
int i;
char temp_id[128];
for (i = 0; i < 128; i++)
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;
} else if (num == -1)
printf("\n[i] Message is too long.\n\n");
else if (num == -2)
printf("\n[i] Please add a message to your friend request.\n\n");
else if (num == -3)
printf("\n[i] That appears to be your own ID.\n\n");
else if (num == -4)
printf("\n[i] Friend request already sent.\n\n");
else if (num == -5)
printf("\n[i] Undefined error when adding friend\n\n");
}
void list_friends()
{
int i;
printf("\n[i] Friend List");
printf("----- PENDING -----\n\n");
for (i = 0; i <= maxnumfriends; i++) {
char name[MAX_NAME_LENGTH];
getname(messenger, i, (uint8_t *)name);
if (m_friendstatus(messenger, i) > 0 && m_friendstatus(messenger, i) < 4)
printf("[%d] %s\n", i, (uint8_t *)name);
}
printf("\n");
printf("----- ACTIVE -----\n\n");
for (i = 0; i <= maxnumfriends; i++) {
char name[MAX_NAME_LENGTH];
getname(messenger, i, (uint8_t *)name);
if (m_friendstatus(messenger, i) == 4)
printf("[%d] %s\n", i, (uint8_t *)name);
}
printf("\n");
}
void delete_friend()
{
size_t len = strlen(line);
char numstring[len - 3];
int i;
for (i = 0; i < len; i++) {
if (line[i + 3] != ' ')
numstring[i] = line[i + 3];
}
int num = atoi(numstring);
m_delfriend(messenger, num);
--maxnumfriends;
printf("\n\n");
}
void message_friend()
{
size_t len = strlen(line);
char numstring[len - 3];
char message[len - 3];
int i;
for (i = 0; i < len; i++) {
if (line[i + 3] != ' ')
numstring[i] = line[i + 3];
else {
int j;
for (j = (i + 1); j < len; j++)
message[j - i - 1] = line[j + 3];
break;
}
}
int num = atoi(numstring);
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);
else
printf("\n");
}
void change_nickname()
{
uint8_t name[MAX_NAME_LENGTH];
int i = 0;
size_t len = strlen(line);
for (i = 3; i < len; i++) {
if (line[i] == 0 || line[i] == '\n')
break;
name[i - 3] = line[i];
}
name[i - 3] = 0;
setname(messenger, name, i);
char numstring[100];
sprintf(numstring, "\n[i] changed nick to %s\n\n", (char *)name);
printf(numstring);
FILE *name_file = NULL;
name_file = fopen("namefile.txt", "w");
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);
for (i = 3; i < len; i++) {
if (line[i] == 0 || line[i] == '\n')
break;
status[i - 3] = line[i];
}
status[i - 3] = 0;
m_set_statusmessage(messenger, status, strlen((char *)status));
char numstring[100];
sprintf(numstring, "\n[i] changed status to %s\n\n", (char *)status);
printf(numstring);
if (savetofile == 1) {
FILE *status_file = NULL;
status_file = fopen("statusfile.txt", "w");
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];
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);
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);
}
}
}
void line_eval(char *line)
{
if (line[0] == '/') {
char inpt_command = line[1];
if (inpt_command == 'f') {
add_friend();
}
else if (inpt_command == 'r') {
do_header();
printf("\n\n");
}
else if (inpt_command == 'l') {
list_friends();
}
else if (inpt_command == 'd') {
delete_friend();
}
/* Send message to friend */
else if (inpt_command == 'm') {
message_friend();
}
else if (inpt_command == 'n') {
change_nickname();
}
else if (inpt_command == 's') {
change_status(1);
}
else if (inpt_command == 'a') {
if (friend_request_received == 1)
accept_friend_request();
}
/* EXIT */
else if (inpt_command == 'q') {
strcpy(line, "---Offline");
change_status(0);
exit(EXIT_SUCCESS);
}
}
}
void get_input()
{
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);
}
messenger = initMessenger();
if (messenger == 0) {
printf("initMessenger failed");
exit(0);
}
if (argc > 4) {
if (strncmp(argv[4], "nokey", 6) < 0) {
}
} else {
load_key();
}
int nameloaded = 0;
int statusloaded = 0;
FILE *name_file = NULL;
name_file = fopen("namefile.txt", "r");
if (name_file) {
uint8_t name[MAX_NAME_LENGTH];
while (fgets(line, MAX_NAME_LENGTH, name_file) != NULL) {
sscanf(line, "%s", (char *)name);
}
setname(messenger, name, strlen((char *)name) + 1);
nameloaded = 1;
printf("%s\n", name);
fclose(name_file);
}
FILE *status_file = NULL;
status_file = fopen("statusfile.txt", "r");
if (status_file) {
uint8_t status[MAX_STATUSMESSAGE_LENGTH];
while (fgets(line, MAX_STATUSMESSAGE_LENGTH, status_file) != NULL) {
sscanf(line, "%s", (char *)status);
}
m_set_statusmessage(messenger, status, strlen((char *)status) + 1);
statusloaded = 1;
printf("%s\n", status);
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);
char idstring1[PUB_KEY_BYTES][5];
char idstring2[PUB_KEY_BYTES][5];
int i;
for (i = 0; i < PUB_KEY_BYTES; i++) {
if (self_public_key[i] < (PUB_KEY_BYTES / 2))
strcpy(idstring1[i], "0");
else
strcpy(idstring1[i], "");
sprintf(idstring2[i], "%hhX", self_public_key[i]);
}
strcpy(users_id, "[i] your ID: ");
int j;
for (j = 0; j < PUB_KEY_BYTES; j++) {
strcat(users_id, idstring1[j]);
strcat(users_id, idstring2[j]);
}
do_header();
IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2]));
int resolved_address = resolve_addr(argv[1]);
if (resolved_address != 0)
bootstrap_ip_port.ip.i = resolved_address;
else
exit(1);
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---------------------------------");
}
while (1) {
if (on == 1 && DHT_isconnected() == -1) {
printf("\n---------------------------------");
printf("\n[i] Disconnected from the DHT");
printf("\n---------------------------------\n\n");
on = 0;
}
if (on == 0 && DHT_isconnected()) {
printf("\n[i] Connected to DHT");
printf("\n---------------------------------\n\n");
on = 1;
}
doMessenger(messenger);
Sleep(1);
}
return 0;
}

View File

@ -1,48 +0,0 @@
/* nTox_win32.h
*
* 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/>.
*/
#ifndef NTOX_WIN32_H
#define NTOX_WIN32_H
#include "../core/Messenger.h"
#include "../core/network.h"
#define STRING_LENGTH 256
#define PUB_KEY_BYTES 32
void do_header();
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata);
void print_message(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void print_nickchange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void print_statuschange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void load_key();
void add_friend();
void list_friends();
void delete_friend();
void message_friend();
void change_nickname();
void change_status(int savetofile);
void accept_friend_request();
void line_eval(char *line);
void get_input();
#endif

View File

@ -1,28 +0,0 @@
cmake_minimum_required(VERSION 2.6.0)
project(toxic C)
execute_process(COMMAND git rev-list HEAD --count OUTPUT_VARIABLE COMMIT)
SET(GCC_COVERAGE_COMPILE_FLAGS '-DTOXICVER="0.1.1_r${COMMIT}"')
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
set(exe_name toxic)
add_executable(${exe_name}
main.c
windows.c
prompt.c
friendlist.c
dhtstatus.c
chat.c
configdir.c)
if(CURSES_HAVE_WIDE_CHAR)
add_definitions( -D_XOPEN_SOURCE_EXTENDED )
add_definitions( -DHAVE_WIDE_CHAR )
endif()
include_directories(${CURSES_INCLUDE_DIR})
target_link_libraries(${exe_name}
${CURSES_LIBRARIES})
linkCoreLibraries(${exe_name})

View File

@ -1,445 +0,0 @@
/*
* Toxic -- Tox Curses Client
*/
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <time.h>
#include <limits.h>
#include "../../core/Messenger.h"
#include "../../core/network.h"
#include "windows.h"
#include "friendlist.h"
#include "chat.h"
#define CURS_Y_OFFSET 3
typedef struct {
int friendnum;
wchar_t line[MAX_STR_SIZE];
size_t pos;
WINDOW *history;
WINDOW *linewin;
} ChatContext;
void print_help(ChatContext *self);
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd);
struct tm *get_time(void)
{
struct tm *timeinfo;
time_t now;
time(&now);
timeinfo = localtime(&now);
return timeinfo;
}
static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len)
{
ChatContext *ctx = (ChatContext *) self->x;
uint8_t nick[MAX_NAME_LENGTH] = {0};
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
getname(m, num, (uint8_t *) &nick);
msg[len - 1] = '\0';
nick[MAX_NAME_LENGTH - 1] = '\0';
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s: ", nick);
wattroff(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s\n", msg);
self->blink = true;
beep();
}
static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len)
{
ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
action[len - 1] = '\0';
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(5));
wprintw(ctx->history, "%s\n", action);
wattroff(ctx->history, COLOR_PAIR(5));
self->blink = true;
beep();
}
static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
{
ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
nick[len - 1] = '\0';
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick);
wattroff(ctx->history, COLOR_PAIR(3));
}
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
{
ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
status[len - 1] = '\0';
snprintf(self->title, sizeof(self->title), "[%s (%d)]", status, num);
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, "* Your partner changed status to '%s'\n", status);
wattroff(ctx->history, COLOR_PAIR(3));
}
/* check that the string has one non-space character */
int string_is_empty(char *string)
{
int rc = 0;
char *copy = strdup(string);
rc = ((strtok(copy, " ") == NULL) ? 1 : 0);
free(copy);
return rc;
}
/* convert wide characters to null terminated string */
static char *wcs_to_char(wchar_t *string)
{
size_t len = 0;
char *ret = NULL;
len = wcstombs(NULL, string, 0);
if (len != (size_t) -1) {
len++;
ret = malloc(len);
wcstombs(ret, string, len);
} else {
ret = malloc(2);
ret[0] = ' ';
ret[1] = '\0';
}
return ret;
}
/* convert a wide char to null terminated string */
static char *wc_to_char(wchar_t ch)
{
int len = 0;
static char ret[MB_LEN_MAX + 1];
len = wctomb(ret, ch);
if (len == -1) {
ret[0] = ' ';
ret[1] = '\0';
} else {
ret[len] = '\0';
}
return ret;
}
static void chat_onKey(ToxWindow *self, Messenger *m, wint_t key)
{
ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
int x, y, y2, x2;
getyx(self->window, y, x);
getmaxyx(self->window, y2, x2);
/* Add printable chars to buffer and print on input space */
#if HAVE_WIDECHAR
if (iswprint(key)) {
#else
if (isprint(key)) {
#endif
if (ctx->pos != sizeof(ctx->line) - 1) {
mvwaddstr(self->window, y, x, wc_to_char(key));
ctx->line[ctx->pos++] = key;
ctx->line[ctx->pos] = L'\0';
}
}
/* BACKSPACE key: Remove one character from line */
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (ctx->pos > 0) {
ctx->line[--ctx->pos] = L'\0';
if (x == 0)
mvwdelch(self->window, y - 1, x2 - 1);
else
mvwdelch(self->window, y, x - 1);
}
}
/* RETURN key: Execute command or print line */
else if (key == '\n') {
char *line = wcs_to_char(ctx->line);
wclear(ctx->linewin);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
wclrtobot(self->window);
if (line[0] == '/')
execute(self, ctx, m, line);
else {
/* make sure the string has at least non-space character */
if (!string_is_empty(line)) {
uint8_t selfname[MAX_NAME_LENGTH];
getself_name(m, selfname, sizeof(selfname));
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s: ", selfname);
wattroff(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s\n", line);
if (m_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) {
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send message.\n");
wattroff(ctx->history, COLOR_PAIR(3));
}
}
}
ctx->line[0] = L'\0';
ctx->pos = 0;
free(line);
}
}
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
{
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
wclear(self->window);
wclear(ctx->history);
int x, y;
getmaxyx(self->window, y, x);
(void) x;
wmove(self->window, y - CURS_Y_OFFSET, 0);
}
else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
print_help(ctx);
else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
endwin();
exit(0);
}
else if (!strncmp(cmd, "/me ", strlen("/me "))) {
struct tm *timeinfo = get_time();
char *action = strchr(cmd, ' ');
if (action == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
action++;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
uint8_t selfname[MAX_NAME_LENGTH];
int len = getself_name(m, selfname, sizeof(selfname));
char msg[MAX_STR_SIZE - len - 4];
snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action);
wattron(ctx->history, COLOR_PAIR(5));
wprintw(ctx->history, msg);
wattroff(ctx->history, COLOR_PAIR(5));
if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) {
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send action\n");
wattroff(ctx->history, COLOR_PAIR(3));
}
}
else if (!strncmp(cmd, "/status ", strlen("/status "))) {
char *status = strchr(cmd, ' ');
char *msg;
char *status_text;
if (status == NULL) {
wprintw(ctx->history, "Invalid syntax.\n");
return;
}
status++;
USERSTATUS status_kind;
if (!strncmp(status, "online", strlen("online"))) {
status_kind = USERSTATUS_NONE;
status_text = "ONLINE";
}
else if (!strncmp(status, "away", strlen("away"))) {
status_kind = USERSTATUS_AWAY;
status_text = "AWAY";
}
else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = USERSTATUS_BUSY;
status_text = "BUSY";
}
else {
wprintw(ctx->history, "Invalid status.\n");
return;
}
msg = strchr(status, ' ');
if (msg == NULL) {
m_set_userstatus(m, status_kind);
wprintw(ctx->history, "Status set to: %s\n", status_text);
} else {
msg++;
m_set_userstatus(m, status_kind);
m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
}
}
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
char *nick;
nick = strchr(cmd, ' ');
if (nick == NULL) {
wprintw(ctx->history, "Invalid syntax.\n");
return;
}
nick++;
setname(m, (uint8_t *) nick, strlen(nick) + 1);
wprintw(ctx->history, "Nickname set to: %s\n", nick);
}
else if (!strcmp(cmd, "/myid")) {
char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
int i;
uint8_t address[FRIEND_ADDRESS_SIZE];
getaddress(m, address);
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
char xx[3];
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
}
wprintw(ctx->history, "%s\n", id);
}
else if (strcmp(cmd, "/close") == 0) {
int f_num = ctx->friendnum;
delwin(ctx->linewin);
del_window(self);
disable_chatwin(f_num);
}
else
wprintw(ctx->history, "Invalid command.\n");
}
static void chat_onDraw(ToxWindow *self, Messenger *m)
{
curs_set(1);
int x, y;
getmaxyx(self->window, y, x);
(void) y;
ChatContext *ctx = (ChatContext *) self->x;
mvwhline(ctx->linewin, 0, 0, '_', x);
wrefresh(self->window);
}
static void chat_onInit(ToxWindow *self, Messenger *m)
{
int x, y;
ChatContext *ctx = (ChatContext *) self->x;
getmaxyx(self->window, y, x);
ctx->history = subwin(self->window, y - 4, x, 0, 0);
scrollok(ctx->history, 1);
ctx->linewin = subwin(self->window, 2, x, y - 4, 0);
print_help(ctx);
wmove(self->window, y - CURS_Y_OFFSET, 0);
}
void print_help(ChatContext *self)
{
wattron(self->history, COLOR_PAIR(2) | A_BOLD);
wprintw(self->history, "Commands:\n");
wattroff(self->history, A_BOLD);
wprintw(self->history, " /status <type> <message> : Set your status\n");
wprintw(self->history, " /nick <nickname> : Set your nickname\n");
wprintw(self->history, " /me <action> : Do an action\n");
wprintw(self->history, " /myid : Print your ID\n");
wprintw(self->history, " /clear : Clear the screen\n");
wprintw(self->history, " /close : Close the current chat window\n");
wprintw(self->history, " /quit or /exit : Exit program\n");
wprintw(self->history, " /help : Print this message again\n\n");
wattroff(self->history, COLOR_PAIR(2));
}
ToxWindow new_chat(Messenger *m, int friendnum)
{
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.onKey = &chat_onKey;
ret.onDraw = &chat_onDraw;
ret.onInit = &chat_onInit;
ret.onMessage = &chat_onMessage;
ret.onNickChange = &chat_onNickChange;
ret.onStatusChange = &chat_onStatusChange;
ret.onAction = &chat_onAction;
uint8_t nick[MAX_NAME_LENGTH] = {0};
getname(m, friendnum, (uint8_t *) &nick);
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
ChatContext *x = calloc(1, sizeof(ChatContext));
x->friendnum = friendnum;
ret.x = (void *) x;
return ret;
}

View File

@ -1,6 +0,0 @@
#ifndef CHAT_H_6489PZ13
#define CHAT_H_6489PZ13
ToxWindow new_chat(Messenger *m, int friendnum);
#endif /* end of include guard: CHAT_H_6489PZ13 */

View File

@ -1,165 +0,0 @@
/*
* 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/>.
*
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#ifdef WIN32
#include <shlobj.h>
#include <direct.h>
#else /* WIN32 */
#include <unistd.h>
#include <pwd.h>
#endif /* WIN32 */
#include "configdir.h"
/**
* @brief Get the users config directory.
*
* This is without a trailing slash.
*
* @return The users config dir or NULL on error.
*/
char *get_user_config_dir(void)
{
char *user_config_dir;
#ifdef WIN32
char appdata[MAX_PATH];
BOOL ok;
ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE);
if (!ok) {
return NULL;
}
user_config_dir = strdup(appdata);
return user_config_dir;
#else /* WIN32 */
#ifndef NSS_BUFLEN_PASSWD
#define NSS_BUFLEN_PASSWD 4096
#endif /* NSS_BUFLEN_PASSWD */
struct passwd pwd;
struct passwd *pwdbuf;
const char *home;
char buf[NSS_BUFLEN_PASSWD];
size_t len;
int rc;
rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
if (rc == 0) {
home = pwd.pw_dir;
} else {
home = getenv("HOME");
if (home == NULL) {
return NULL;
}
/* env variables can be tainted */
snprintf(buf, sizeof(buf), "%s", home);
home = buf;
}
# if defined(__APPLE__)
len = strlen(home) + strlen("/Library/Application Support") + 1;
user_config_dir = malloc(len);
if (user_config_dir == NULL) {
return NULL;
}
snprintf(user_config_dir, len, "%s/Library/Application Support", home);
# else /* __APPLE__ */
if (!(user_config_dir = getenv("XDG_CONFIG_HOME"))) {
len = strlen(home) + strlen("/.config") + 1;
user_config_dir = malloc(len);
if (user_config_dir == NULL) {
return NULL;
}
snprintf(user_config_dir, len, "%s/.config", home);
}
# endif /* __APPLE__ */
return user_config_dir;
#undef NSS_BUFLEN_PASSWD
#endif /* WIN32 */
}
/*
* Creates the config directory.
*/
int create_user_config_dir(char *path)
{
int mkdir_err;
#ifdef WIN32
char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
strcpy(fullpath, path);
strcat(fullpath, CONFIGDIR);
mkdir_err = _mkdir(fullpath);
struct __stat64 buf;
if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
free(fullpath);
return -1;
}
#else
mkdir_err = mkdir(path, 0700);
struct stat buf;
if (mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) {
return -1;
}
char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
strcpy(fullpath, path);
strcat(fullpath, CONFIGDIR);
mkdir_err = mkdir(fullpath, 0700);
if (mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
free(fullpath);
return -1;
}
#endif
free(fullpath);
return 0;
}

View File

@ -1,33 +0,0 @@
/*
* 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/>.
*
*/
#ifdef _win32
#define CONFIGDIR "\\tox\\"
#else
#define CONFIGDIR "/tox/"
#endif
#ifndef S_ISDIR
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
char *get_user_config_dir(void);
int create_user_config_dir(char *path);

View File

@ -1,99 +0,0 @@
#include "dhtstatus.h"
#include "string.h"
#include "../../core/network.h"
#include "../../core/DHT.h"
typedef uint8_t ipbuf[3 * 4 + 3 + 1];
static int num_selected = 0;
static void printip(ipbuf buf, IP ip)
{
sprintf((char *)buf, "%u.%u.%u.%u", ip.c[0], ip.c[1], ip.c[2], ip.c[3]);
}
static void dhtstatus_onKey(ToxWindow *self, Messenger *m, wint_t key)
{
switch (key) {
case KEY_UP:
case 'k':
if (--num_selected < 0)
num_selected = CLIENT_ID_SIZE - 1;
break;
case KEY_DOWN:
case 'j':
num_selected = (num_selected + 1) % CLIENT_ID_SIZE;
break;
case '\n':
break;
default:
break;
}
}
static void dhtstatus_onDraw(ToxWindow *self, Messenger *m)
{
Client_data *close_clientlist = DHT_get_close_list(m->dht);
curs_set(0);
werase(self->window);
uint64_t now = unix_time();
uint32_t i, j;
ipbuf ipbuf;
wprintw(self->window,
"\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n",
now);
for (i = 0; i < 32; i++) { /*Number of nodes in closelist*/
Client_data *client = close_clientlist + i;
if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
wprintw(self->window, "[%02i] ", i);
uint16_t port = ntohs(client->ip_port.port);
if (port) {
for (j = 0; j < CLIENT_ID_SIZE; j++)
wprintw(self->window, "%02hhx", client->client_id[j]);
printip(ipbuf, client->ip_port.ip);
wprintw(self->window, " %15s %5u ", ipbuf, port);
wprintw(self->window, " %3llu ", now - client->timestamp);
wprintw(self->window, " %3llu ", now - client->last_pinged);
port = ntohs(client->ret_ip_port.port);
if (port) {
printip(ipbuf, client->ret_ip_port.ip);
wprintw(self->window, " %15s %5u %3llu", ipbuf, port, now - close_clientlist[i].ret_timestamp);
}
}
wprintw(self->window, "\n");
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
}
wrefresh(self->window);
}
static void dhtstatus_onInit(ToxWindow *self, Messenger *m)
{
}
ToxWindow new_dhtstatus()
{
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.onKey = &dhtstatus_onKey;
ret.onDraw = &dhtstatus_onDraw;
ret.onInit = &dhtstatus_onInit;
strcpy(ret.title, "[dht status]");
return ret;
}

View File

@ -1,8 +0,0 @@
#ifndef _dhtstatus_h
#define _dhtstatus_h
#include "windows.h"
ToxWindow new_dhtstatus();
#endif

View File

@ -1,145 +0,0 @@
/*
* Toxic -- Tox Curses Client
*/
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include "../../core/Messenger.h"
#include "../../core/network.h"
#include "windows.h"
#include "friendlist.h"
typedef struct {
uint8_t name[MAX_NAME_LENGTH];
uint8_t status[MAX_STATUSMESSAGE_LENGTH];
int num;
int chatwin;
} friend_t;
static friend_t friends[MAX_FRIENDS_NUM];
static int num_friends = 0;
static int num_selected = 0;
void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len)
{
if (num >= num_friends)
return;
if (friends[num].chatwin == -1) {
friends[num].chatwin = add_window(m, new_chat(m, num));
}
}
void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{
if (len >= MAX_NAME_LENGTH || num >= num_friends)
return;
memcpy((char *) &friends[num].name, (char *) str, len);
friends[num].name[len] = 0;
}
void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{
if (len >= MAX_STATUSMESSAGE_LENGTH || num >= num_friends)
return;
memcpy((char *) &friends[num].status, (char *) str, len);
friends[num].status[len] = 0;
}
int friendlist_onFriendAdded(Messenger *m, int num)
{
if (num_friends == MAX_FRIENDS_NUM)
return -1;
friends[num_friends].num = num;
getname(m, num, friends[num_friends].name);
strcpy((char *) friends[num_friends].name, "unknown");
strcpy((char *) friends[num_friends].status, "unknown");
friends[num_friends++].chatwin = -1;
return 0;
}
static void friendlist_onKey(ToxWindow *self, Messenger *m, wint_t key)
{
if (key == KEY_UP) {
if (--num_selected < 0)
num_selected = num_friends - 1;
} else if (key == KEY_DOWN) {
if (num_friends != 0)
num_selected = (num_selected + 1) % num_friends;
} else if (key == '\n') {
/* Jump to chat window if already open */
if (friends[num_selected].chatwin != -1) {
set_active_window(friends[num_selected].chatwin);
} else {
friends[num_selected].chatwin = add_window(m, new_chat(m, num_selected));
}
}
}
static void friendlist_onDraw(ToxWindow *self, Messenger *m)
{
curs_set(0);
werase(self->window);
if (num_friends == 0) {
wprintw(self->window, "Empty. Add some friends! :-)\n");
} else {
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
wprintw(self->window, "Open chat with.. (up/down keys, enter)\n");
wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
}
wprintw(self->window, "\n");
int i;
for (i = 0; i < num_friends; ++i) {
if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
wprintw(self->window, " [#%d] ", friends[i].num);
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
attron(A_BOLD);
wprintw(self->window, "%s ", friends[i].name);
attroff(A_BOLD);
wprintw(self->window, "(%s)\n", friends[i].status);
}
wrefresh(self->window);
}
void disable_chatwin(int f_num)
{
friends[f_num].chatwin = -1;
}
static void friendlist_onInit(ToxWindow *self, Messenger *m)
{
}
ToxWindow new_friendlist()
{
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.onKey = &friendlist_onKey;
ret.onDraw = &friendlist_onDraw;
ret.onInit = &friendlist_onInit;
ret.onMessage = &friendlist_onMessage;
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
ret.onNickChange = &friendlist_onNickChange;
ret.onStatusChange = &friendlist_onStatusChange;
strcpy(ret.title, "[friends]");
return ret;
}

View File

@ -1,11 +0,0 @@
#ifndef FRIENDLIST_H_53I41IM
#define FRIENDLIST_H_53I41IM
#include "windows.h"
#include "chat.h"
ToxWindow new_friendlist();
int friendlist_onFriendAdded(Messenger *m, int num);
void disable_chatwin(int f_num);
#endif /* end of include guard: FRIENDLIST_H_53I41IM */

View File

@ -1,345 +0,0 @@
/*
* Toxic -- Tox Curses Client
*/
#include <curses.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <signal.h>
#include <locale.h>
#ifdef _win32
#include <direct.h>
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
#include "../../core/Messenger.h"
#include "../../core/network.h"
#include "configdir.h"
#include "windows.h"
#include "prompt.h"
#include "friendlist.h"
/* Export for use in Callbacks */
char *DATA_FILE = NULL;
char *SRVLIST_FILE = NULL;
void on_window_resize(int sig)
{
endwin();
refresh();
clear();
}
static void init_term()
{
/* Setup terminal */
signal(SIGWINCH, on_window_resize);
setlocale(LC_ALL, "");
initscr();
cbreak();
keypad(stdscr, 1);
noecho();
timeout(100);
if (has_colors()) {
start_color();
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
init_pair(3, COLOR_RED, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_YELLOW, COLOR_BLACK);
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
init_pair(7, COLOR_BLACK, COLOR_BLACK);
init_pair(8, COLOR_BLACK, COLOR_WHITE);
}
refresh();
}
static Messenger *init_tox()
{
/* Init core */
Messenger *m = initMessenger();
/* Callbacks */
m_callback_friendrequest(m, on_request, NULL);
m_callback_friendmessage(m, on_message, NULL);
m_callback_namechange(m, on_nickchange, NULL);
m_callback_statusmessage(m, on_statuschange, NULL);
m_callback_action(m, on_action, NULL);
#ifdef __linux__
setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy"));
#elif defined(WIN32)
setname(m, (uint8_t *) "I should install GNU/Linux", sizeof("I should install GNU/Linux"));
#elif defined(__APPLE__)
setname(m, (uint8_t *) "Hipster", sizeof("Hipster")); //This used to users of other Unixes are hipsters
#else
setname(m, (uint8_t *) "Registered Minix user #4", sizeof("Registered Minix user #4"));
#endif
return m;
}
#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
#define MINLINE 70
#define MAXSERVERS 50
/* Connects to a random DHT server listed in the DHTservers file */
int init_connection(Messenger *m)
{
FILE *fp = NULL;
if (DHT_isconnected(m->dht))
return 0;
fp = fopen(SRVLIST_FILE, "r");
if (!fp)
return 1;
char servers[MAXSERVERS][MAXLINE];
char line[MAXLINE];
int linecnt = 0;
while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) {
if (strlen(line) > MINLINE)
strcpy(servers[linecnt++], line);
}
if (linecnt < 1) {
fclose(fp);
return 2;
}
fclose(fp);
char *server = servers[rand() % linecnt];
char *ip = strtok(server, " ");
char *port = strtok(NULL, " ");
char *key = strtok(NULL, " ");
if (!ip || !port || !key)
return 3;
IP_Port dht;
dht.port = htons(atoi(port));
uint32_t resolved_address = resolve_addr(ip);
if (resolved_address == 0)
return 0;
dht.ip.i = resolved_address;
unsigned char *binary_string = hex_string_to_bin(key);
DHT_bootstrap(m->dht, dht, binary_string);
free(binary_string);
return 0;
}
static void do_tox(Messenger *m, ToxWindow *prompt)
{
static int conn_try = 0;
static int conn_err = 0;
static bool dht_on = false;
if (!dht_on && !DHT_isconnected(m->dht) && !(conn_try++ % 100)) {
if (!conn_err) {
conn_err = init_connection(m);
wprintw(prompt->window, "\nEstablishing connection...\n");
if (conn_err)
wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err);
}
} else if (!dht_on && DHT_isconnected(m->dht)) {
dht_on = true;
wprintw(prompt->window, "\nDHT connected.\n");
} else if (dht_on && !DHT_isconnected(m->dht)) {
dht_on = false;
wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
}
doMessenger(m);
}
int f_loadfromfile;
/*
* Store Messenger to given location
* Return 0 stored successfully
* Return 1 malloc failed
* Return 2 opening path failed
* Return 3 fwrite failed
*/
int store_data(Messenger *m, char *path)
{
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
return 0;
FILE *fd;
size_t len;
uint8_t *buf;
len = Messenger_size(m);
buf = malloc(len);
if (buf == NULL) {
return 1;
}
Messenger_save(m, buf);
fd = fopen(path, "w");
if (fd == NULL) {
free(buf);
return 2;
}
if (fwrite(buf, len, 1, fd) != 1) {
free(buf);
fclose(fd);
return 3;
}
free(buf);
fclose(fd);
return 0;
}
static void load_data(Messenger *m, char *path)
{
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
return;
FILE *fd;
size_t len;
uint8_t *buf;
if ((fd = fopen(path, "r")) != NULL) {
fseek(fd, 0, SEEK_END);
len = ftell(fd);
fseek(fd, 0, SEEK_SET);
buf = malloc(len);
if (buf == NULL) {
fprintf(stderr, "malloc() failed.\n");
fclose(fd);
endwin();
exit(1);
}
if (fread(buf, len, 1, fd) != 1) {
fprintf(stderr, "fread() failed.\n");
free(buf);
fclose(fd);
endwin();
exit(1);
}
Messenger_load(m, buf, len);
uint32_t i;
for (i = 0; i < m->numfriends; i++) {
on_friendadded(m, i);
}
free(buf);
fclose(fd);
} else {
int st;
if ((st = store_data(m, path)) != 0) {
fprintf(stderr, "Store messenger failed with return code: %d\n", st);
endwin();
exit(1);
}
}
}
int main(int argc, char *argv[])
{
char *user_config_dir = get_user_config_dir();
int config_err = 0;
f_loadfromfile = 1;
int f_flag = 0;
int i = 0;
for (i = 0; i < argc; ++i) {
if (argv[i] == NULL)
break;
else if (argv[i][0] == '-') {
if (argv[i][1] == 'f') {
if (argv[i + 1] != NULL)
DATA_FILE = strdup(argv[i + 1]);
else
f_flag = -1;
} else if (argv[i][1] == 'n') {
f_loadfromfile = 0;
}
}
}
if (DATA_FILE == NULL ) {
config_err = create_user_config_dir(user_config_dir);
if (config_err) {
DATA_FILE = strdup("data");
SRVLIST_FILE = strdup("../../other/DHTservers");
} else {
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
strcpy(DATA_FILE, user_config_dir);
strcat(DATA_FILE, CONFIGDIR);
strcat(DATA_FILE, "data");
SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1);
strcpy(SRVLIST_FILE, user_config_dir);
strcat(SRVLIST_FILE, CONFIGDIR);
strcat(SRVLIST_FILE, "DHTservers");
}
}
free(user_config_dir);
init_term();
Messenger *m = init_tox();
ToxWindow *prompt = init_windows(m);
if (f_loadfromfile)
load_data(m, DATA_FILE);
if (f_flag == -1) {
attron(COLOR_PAIR(3) | A_BOLD);
wprintw(prompt->window, "You passed '-f' without giving an argument.\n"
"defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD);
}
if (config_err) {
attron(COLOR_PAIR(3) | A_BOLD);
wprintw(prompt->window, "Unable to determine configuration directory.\n"
"defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD);
}
while (true) {
/* Update tox */
do_tox(m, prompt);
/* Draw */
draw_active_window(m);
}
cleanupMessenger(m);
free(DATA_FILE);
free(SRVLIST_FILE);
return 0;
}

View File

@ -1,509 +0,0 @@
/*
* Toxic -- Tox Curses Client
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../../core/Messenger.h"
#include "../../core/network.h"
#include "windows.h"
#include "prompt.h"
extern char *DATA_FILE;
extern int store_data(Messenger *m, char *path);
uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX
uint8_t num_requests = 0; // XXX
static char prompt_buf[MAX_STR_SIZE] = {0};
static int prompt_buf_pos = 0;
/* commands */
void cmd_accept(ToxWindow *, Messenger *m, char **);
void cmd_add(ToxWindow *, Messenger *m, char **);
void cmd_clear(ToxWindow *, Messenger *m, char **);
void cmd_connect(ToxWindow *, Messenger *m, char **);
void cmd_help(ToxWindow *, Messenger *m, char **);
void cmd_msg(ToxWindow *, Messenger *m, char **);
void cmd_myid(ToxWindow *, Messenger *m, char **);
void cmd_nick(ToxWindow *, Messenger *m, char **);
void cmd_mynick(ToxWindow *, Messenger *m, char **);
void cmd_quit(ToxWindow *, Messenger *m, char **);
void cmd_status(ToxWindow *, Messenger *m, char **);
void cmd_statusmsg(ToxWindow *, Messenger *m, char **);
#define NUM_COMMANDS 14
static struct {
char *name;
int numargs;
void (*func)(ToxWindow *, Messenger *m, char **);
} commands[] = {
{ "accept", 1, cmd_accept },
{ "add", 1, cmd_add },
{ "clear", 0, cmd_clear },
{ "connect", 3, cmd_connect },
{ "exit", 0, cmd_quit },
{ "help", 0, cmd_help },
{ "msg", 2, cmd_msg },
{ "myid", 0, cmd_myid },
{ "nick", 1, cmd_nick },
{ "mynick", 0, cmd_mynick },
{ "q", 0, cmd_quit },
{ "quit", 0, cmd_quit },
{ "status", 2, cmd_status },
{ "statusmsg", 1, cmd_statusmsg },
};
// XXX:
int add_req(uint8_t *public_key)
{
memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
++num_requests;
return num_requests - 1;
}
// XXX: FIX
unsigned char *hex_string_to_bin(char hex_string[])
{
size_t len = strlen(hex_string);
unsigned char *val = malloc(len);
char *pos = hex_string;
int i;
for (i = 0; i < len; ++i, pos += 2)
sscanf(pos, "%2hhx", &val[i]);
return val;
}
void cmd_accept(ToxWindow *self, Messenger *m, char **args)
{
int num = atoi(args[1]);
if (num >= num_requests) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
num = m_addfriend_norequest(m, pending_requests[num]);
if (num == -1)
wprintw(self->window, "Failed to add friend.\n");
else {
wprintw(self->window, "Friend accepted as: %d.\n", num);
on_friendadded(m, num);
}
}
void cmd_add(ToxWindow *self, Messenger *m, char **args)
{
uint8_t id_bin[FRIEND_ADDRESS_SIZE];
char xx[3];
uint32_t x;
char *id = args[1];
char *msg = args[2];
if (!id) {
wprintw(self->window, "Invalid command: add expected at least one argument.\n");
return;
}
if (!msg)
msg = "";
if (strlen(id) != 2 * FRIEND_ADDRESS_SIZE) {
wprintw(self->window, "Invalid ID length.\n");
return;
}
int i;
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
xx[0] = id[2 * i];
xx[1] = id[2 * i + 1];
xx[2] = '\0';
if (sscanf(xx, "%02x", &x) != 1) {
wprintw(self->window, "Invalid ID.\n");
return;
}
id_bin[i] = x;
}
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
id[i] = toupper(id[i]);
}
int num = m_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1);
switch (num) {
case FAERR_TOOLONG:
wprintw(self->window, "Message is too long.\n");
break;
case FAERR_NOMESSAGE:
wprintw(self->window, "Please add a message to your request.\n");
break;
case FAERR_OWNKEY:
wprintw(self->window, "That appears to be your own ID.\n");
break;
case FAERR_ALREADYSENT:
wprintw(self->window, "Friend request already sent.\n");
break;
case FAERR_UNKNOWN:
wprintw(self->window, "Undefined error when adding friend.\n");
break;
case FAERR_BADCHECKSUM:
wprintw(self->window, "Bad checksum in address.\n");
break;
case FAERR_SETNEWNOSPAM:
wprintw(self->window, "Nospam was different.\n");
break;
default:
wprintw(self->window, "Friend added as %d.\n", num);
on_friendadded(m, num);
break;
}
}
void cmd_clear(ToxWindow *self, Messenger *m, char **args)
{
wclear(self->window);
}
void cmd_connect(ToxWindow *self, Messenger *m, char **args)
{
IP_Port dht;
char *ip = args[1];
char *port = args[2];
char *key = args[3];
if (atoi(port) == 0) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
dht.port = htons(atoi(port));
uint32_t resolved_address = resolve_addr(ip);
if (resolved_address == 0) {
return;
}
dht.ip.i = resolved_address;
unsigned char *binary_string = hex_string_to_bin(key);
DHT_bootstrap(m->dht, dht, binary_string);
free(binary_string);
}
void cmd_quit(ToxWindow *self, Messenger *m, char **args)
{
endwin();
exit(0);
}
void cmd_help(ToxWindow *self, Messenger *m, char **args)
{
wclear(self->window);
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
wprintw(self->window, "Commands:\n");
wattroff(self->window, A_BOLD);
wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
wprintw(self->window, " add <id> <message> : Add friend\n");
wprintw(self->window, " status <type> <message> : Set your status\n");
wprintw(self->window, " statusmsg <message> : Set your status\n");
wprintw(self->window, " nick <nickname> : Set your nickname\n");
wprintw(self->window, " mynick : Print your current nickname\n");
wprintw(self->window, " accept <number> : Accept friend request\n");
wprintw(self->window, " myid : Print your ID\n");
wprintw(self->window, " quit/exit : Exit program\n");
wprintw(self->window, " help : Print this message again\n");
wprintw(self->window, " clear : Clear this window\n");
wattron(self->window, A_BOLD);
wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");
wattroff(self->window, A_BOLD);
wattroff(self->window, COLOR_PAIR(2));
}
void cmd_msg(ToxWindow *self, Messenger *m, char **args)
{
char *id = args[1];
char *msg = args[2];
if (m_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0)
wprintw(self->window, "Error occurred while sending message.\n");
else
wprintw(self->window, "Message successfully sent.\n");
}
void cmd_myid(ToxWindow *self, Messenger *m, char **args)
{
char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
size_t i;
uint8_t address[FRIEND_ADDRESS_SIZE];
getaddress(m, address);
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
char xx[3];
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
}
wprintw(self->window, "%s\n", id);
}
void cmd_nick(ToxWindow *self, Messenger *m, char **args)
{
char *nick = args[1];
setname(m, (uint8_t *) nick, strlen(nick) + 1);
wprintw(self->window, "Nickname set to: %s\n", nick);
if (store_data(m, DATA_FILE)) {
wprintw(self->window, "\nCould not store Messenger data\n");
}
}
void cmd_mynick(ToxWindow *self, Messenger *m, char **args)
{
uint8_t *nick = malloc(m->name_length);
getself_name(m, nick, m->name_length);
wprintw(self->window, "Current nickname: %s\n", nick);
free(nick);
}
void cmd_status(ToxWindow *self, Messenger *m, char **args)
{
char *status = args[1];
char *status_text;
USERSTATUS status_kind;
if (!strncmp(status, "online", strlen("online"))) {
status_kind = USERSTATUS_NONE;
status_text = "ONLINE";
} else if (!strncmp(status, "away", strlen("away"))) {
status_kind = USERSTATUS_AWAY;
status_text = "AWAY";
} else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = USERSTATUS_BUSY;
status_text = "BUSY";
} else {
wprintw(self->window, "Invalid status.\n");
return;
}
char *msg = args[2];
if (msg == NULL) {
m_set_userstatus(m, status_kind);
wprintw(self->window, "Status set to: %s\n", status_text);
} else {
m_set_userstatus(m, status_kind);
m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
wprintw(self->window, "Status set to: %s, %s\n", status_text, msg);
}
}
void cmd_statusmsg(ToxWindow *self, Messenger *m, char **args)
{
char *msg = args[1];
m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
wprintw(self->window, "Status set to: %s\n", msg);
}
static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
{
int newlines = 0;
char cmd[MAX_STR_SIZE] = {0};
int i;
for (i = 0; i < strlen(prompt_buf); ++i) {
if (u_cmd[i] == '\n')
++newlines;
else
cmd[i - newlines] = u_cmd[i];
}
int leading_spc = 0;
for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i)
leading_spc++;
memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc);
int cmd_end = strlen(cmd);
while (cmd_end > 0 && cmd_end--)
if (!isspace(cmd[cmd_end]))
break;
cmd[cmd_end + 1] = '\0';
/* insert \0 at argument boundaries */
int numargs = 0;
for (i = 0; i < MAX_STR_SIZE; i++) {
char quote_chr;
if (cmd[i] == '\"' || cmd[i] == '\'') {
quote_chr = cmd[i];
while (cmd[++i] != quote_chr && i < MAX_STR_SIZE); /* skip over strings */
/* Check if got qoute character */
if (cmd[i] != quote_chr) {
wprintw(self->window, "Missing terminating %c character\n", quote_chr);
return;
}
}
if (cmd[i] == ' ') {
cmd[i] = '\0';
int j = i;
while (++j < MAX_STR_SIZE && isspace(cmd[j]));
i = j - 1;
numargs++;
}
}
/* excessive arguments */
if (numargs > 3) {
wprintw(self->window, "Invalid command: too many arguments.\n");
return;
}
/* read arguments into array */
char *cmdargs[5];
int pos = 0;
for (i = 0; i < 5; i++) {
cmdargs[i] = cmd + pos;
pos += strlen(cmdargs[i]) + 1;
while (isspace(cmd[pos]) && pos < MAX_STR_SIZE)
++pos;
}
/* no input */
if (strlen(cmdargs[0]) == 0)
return;
/* match input to command list */
for (i = 0; i < NUM_COMMANDS; i++) {
if (!strcmp(cmdargs[0], commands[i].name)) {
/* check for missing arguments */
int j;
for (j = 0; j <= commands[i].numargs; j++) {
if (strlen(cmdargs[j]) == 0) {
wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n",
commands[i].name, commands[i].numargs, j - 1);
return;
}
}
/* check for excess arguments */
if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) {
wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name);
return;
}
/* pass arguments to command function */
(commands[i].func)(self, m, cmdargs);
return;
}
}
/* no match */
wprintw(self->window, "Invalid command.\n");
}
static void prompt_onKey(ToxWindow *self, Messenger *m, wint_t key)
{
/* Add printable characters to line */
if (isprint(key)) {
if (prompt_buf_pos == (sizeof(prompt_buf) - 1)) {
wprintw(self->window, "\nToo Long.\n");
prompt_buf_pos = 0;
prompt_buf[0] = 0;
} else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS)
&& (prompt_buf_pos % (COLS - 3) == 0)) {
prompt_buf[prompt_buf_pos++] = '\n';
} else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
&& ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
prompt_buf[prompt_buf_pos++] = '\n';
}
prompt_buf[prompt_buf_pos++] = key;
prompt_buf[prompt_buf_pos] = 0;
}
/* RETURN key: execute command */
else if (key == '\n') {
wprintw(self->window, "\n");
execute(self, m, prompt_buf);
prompt_buf_pos = 0;
prompt_buf[0] = 0;
}
/* BACKSPACE key: Remove one character from line */
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (prompt_buf_pos != 0) {
prompt_buf[--prompt_buf_pos] = 0;
}
}
}
static void prompt_onDraw(ToxWindow *self, Messenger *m)
{
curs_set(1);
int x, y;
getyx(self->window, y, x);
(void) x;
int i;
for (i = 0; i < (strlen(prompt_buf)); ++i) {
if ((prompt_buf[i] == '\n') && (y != 0))
--y;
}
wattron(self->window, COLOR_PAIR(1));
mvwprintw(self->window, y, 0, "# ");
wattroff(self->window, COLOR_PAIR(1));
mvwprintw(self->window, y, 2, "%s", prompt_buf);
wclrtoeol(self->window);
wrefresh(self->window);
}
static void prompt_onInit(ToxWindow *self, Messenger *m)
{
scrollok(self->window, 1);
cmd_help(self, m, NULL);
wclrtoeol(self->window);
}
ToxWindow new_prompt()
{
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.onKey = &prompt_onKey;
ret.onDraw = &prompt_onDraw;
ret.onInit = &prompt_onInit;
strcpy(ret.title, "[prompt]");
return ret;
}

View File

@ -1,12 +0,0 @@
#ifndef PROMPT_H_UZYGWFFL
#define PROMPT_H_UZYGWFFL
#include "windows.h"
ToxWindow new_prompt();
int add_req(uint8_t *public_key);
unsigned char *hex_string_to_bin(char hex_string[]);
#endif /* end of include guard: PROMPT_H_UZYGWFFL */

View File

@ -1,247 +0,0 @@
#include "friendlist.h"
#include "prompt.h"
#include "dhtstatus.h"
#include "windows.h"
extern char *DATA_FILE;
extern int store_data(Messenger *m, char *path);
static ToxWindow windows[MAX_WINDOWS_NUM];
static ToxWindow *active_window;
static ToxWindow *prompt;
static Messenger *m;
/* CALLBACKS START */
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
{
int n = add_req(public_key);
wprintw(prompt->window, "\nFriend request from:\n");
int i;
for (i = 0; i < KEY_SIZE_BYTES; ++i) {
wprintw(prompt->window, "%02x", public_key[i] & 0xff);
}
wprintw(prompt->window, "\nWith the message: %s\n", data);
wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onFriendRequest != NULL)
windows[i].onFriendRequest(&windows[i], public_key, data, length);
}
}
void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onMessage != NULL)
windows[i].onMessage(&windows[i], m, friendnumber, string, length);
}
}
void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onAction != NULL)
windows[i].onAction(&windows[i], m, friendnumber, string, length);
}
}
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{
wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onNickChange != NULL)
windows[i].onNickChange(&windows[i], friendnumber, string, length);
}
}
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onStatusChange != NULL)
windows[i].onStatusChange(&windows[i], friendnumber, string, length);
}
}
void on_friendadded(Messenger *m, int friendnumber)
{
friendlist_onFriendAdded(m, friendnumber);
if (store_data(m, DATA_FILE)) {
wprintw(prompt->window, "\nCould not store Messenger data\n");
}
}
/* CALLBACKS END */
int add_window(Messenger *m, ToxWindow w)
{
if (LINES < 2)
return -1;
int i;
for (i = 0; i < MAX_WINDOWS_NUM; i++) {
if (windows[i].window)
continue;
w.window = newwin(LINES - 2, COLS, 0, 0);
if (w.window == NULL)
return -1;
windows[i] = w;
w.onInit(&w, m);
active_window = windows + i;
return i;
}
return -1;
}
/* Deletes window w and cleans up */
void del_window(ToxWindow *w)
{
active_window = windows; // Go to prompt screen
delwin(w->window);
if (w->x)
free(w->x);
w->window = NULL;
memset(w, 0, sizeof(ToxWindow));
clear();
refresh();
}
/* Shows next window when tab or back-tab is pressed */
void set_next_window(int ch)
{
ToxWindow *end = windows + MAX_WINDOWS_NUM - 1;
ToxWindow *inf = active_window;
while (true) {
if (ch == '\t') {
if (++active_window > end)
active_window = windows;
} else if (--active_window < windows)
active_window = end;
if (active_window->window)
return;
if (active_window == inf) { // infinite loop check
endwin();
exit(2);
}
}
}
void set_active_window(int index)
{
if (index < 0 || index >= MAX_WINDOWS_NUM)
return;
active_window = windows + index;
}
ToxWindow *init_windows()
{
int n_prompt = add_window(m, new_prompt());
if (n_prompt == -1
|| add_window(m, new_friendlist()) == -1
|| add_window(m, new_dhtstatus()) == -1) {
fprintf(stderr, "add_window() failed.\n");
endwin();
exit(1);
}
prompt = &windows[n_prompt];
active_window = prompt;
return prompt;
}
static void draw_bar()
{
static int odd = 0;
int blinkrate = 30;
attron(COLOR_PAIR(4));
mvhline(LINES - 2, 0, '_', COLS);
attroff(COLOR_PAIR(4));
move(LINES - 1, 0);
attron(COLOR_PAIR(4) | A_BOLD);
printw(" TOXIC " TOXICVER "|");
attroff(COLOR_PAIR(4) | A_BOLD);
int i;
for (i = 0; i < (MAX_WINDOWS_NUM); ++i) {
if (windows[i].window) {
if (windows + i == active_window)
attron(A_BOLD);
odd = (odd + 1) % blinkrate;
if (windows[i].blink && (odd < (blinkrate / 2)))
attron(COLOR_PAIR(3));
clrtoeol();
printw(" %s", windows[i].title);
if (windows[i].blink && (odd < (blinkrate / 2)))
attroff(COLOR_PAIR(3));
if (windows + i == active_window) {
attroff(A_BOLD);
}
}
}
refresh();
}
void prepare_window(WINDOW *w)
{
mvwin(w, 0, 0);
wresize(w, LINES - 2, COLS);
}
void draw_active_window(Messenger *m)
{
ToxWindow *a = active_window;
wint_t ch = 0;
prepare_window(a->window);
a->blink = false;
draw_bar();
a->onDraw(a, m);
/* Handle input */
#ifdef HAVE_WIDECHAR
get_wch(&ch);
#else
ch = getch();
#endif
if (ch == '\t' || ch == KEY_BTAB)
set_next_window((int) ch);
else if (ch != ERR)
a->onKey(a, m, ch);
}

View File

@ -1,56 +0,0 @@
/*
* Toxic -- Tox Curses Client
*/
#ifndef _windows_h
#define _windows_h
#include <curses.h>
#include <stdint.h>
#include <stdbool.h>
#include <wctype.h>
#include <wchar.h>
#include "../../core/Messenger.h"
#define MAX_WINDOWS_NUM 32
#define MAX_FRIENDS_NUM 100
#define MAX_STR_SIZE 256
#define KEY_SIZE_BYTES 32
/* number of permanent default windows */
#define N_DEFAULT_WINS 3
#ifndef TOXICVER
#define TOXICVER "NOVER" //Use the -D flag to set this
#endif
typedef struct ToxWindow_ ToxWindow;
struct ToxWindow_ {
void(*onKey)(ToxWindow *, Messenger *, wint_t);
void(*onDraw)(ToxWindow *, Messenger *);
void(*onInit)(ToxWindow *, Messenger *);
void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t);
void(*onMessage)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t);
void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onStatusChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onAction)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t);
char title[256];
void *x;
bool blink;
WINDOW *window;
};
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata);
void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_friendadded(Messenger *m, int friendnumber);
ToxWindow *init_windows();
void draw_active_window(Messenger *m);
int add_window(Messenger *m, ToxWindow w);
void del_window(ToxWindow *w);
void set_active_window(int ch);
#endif