2013-07-26 09:45:56 +08:00
|
|
|
/* nTox.c
|
|
|
|
*
|
|
|
|
* Textual frontend for Tox.
|
|
|
|
*
|
|
|
|
* 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-07-14 01:31:16 +08:00
|
|
|
#include "nTox.h"
|
2013-07-28 00:10:41 +08:00
|
|
|
#include "misc_tools.h"
|
2013-07-28 00:17:31 +08:00
|
|
|
|
2013-07-22 04:54:09 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2013-07-17 07:02:44 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
#define c_sleep(x) Sleep(1*x)
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#define c_sleep(x) usleep(1000*x)
|
|
|
|
#endif
|
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
char lines[HISTORY][STRING_LENGTH];
|
|
|
|
char line[STRING_LENGTH];
|
|
|
|
int x,y;
|
2013-07-28 08:04:56 +08:00
|
|
|
|
|
|
|
uint8_t pending_requests[256][CLIENT_ID_SIZE];
|
|
|
|
uint8_t num_requests;
|
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
void new_lines(char *line)
|
|
|
|
{
|
|
|
|
int i;
|
2013-07-27 11:07:25 +08:00
|
|
|
for (i = HISTORY-1; i > 0; i--)
|
2013-07-14 01:31:16 +08:00
|
|
|
strcpy(lines[i],lines[i-1]);
|
2013-07-27 11:07:25 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
strcpy(lines[0],line);
|
2013-07-14 09:57:09 +08:00
|
|
|
do_refresh();
|
2013-07-14 01:31:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
|
|
|
|
{
|
|
|
|
if (line[0] == '/') {
|
2013-07-14 10:27:23 +08:00
|
|
|
char command[STRING_LENGTH + 2] = "> ";
|
|
|
|
strcat(command, line);
|
|
|
|
new_lines(command);
|
2013-07-14 01:31:16 +08:00
|
|
|
if (line[1] == 'f') { // add friend command: /f ID
|
|
|
|
int i;
|
|
|
|
char temp_id[128];
|
2013-07-27 11:07:25 +08:00
|
|
|
for (i=0; i<128; i++)
|
2013-07-14 01:31:16 +08:00
|
|
|
temp_id[i] = line[i+3];
|
|
|
|
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
|
|
|
|
char numstring[100];
|
2013-07-21 21:52:43 +08:00
|
|
|
sprintf(numstring, "[i] added friend %d", num);
|
2013-07-14 01:31:16 +08:00
|
|
|
new_lines(numstring);
|
|
|
|
do_refresh();
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
|
|
|
else if (line[1] == 'd') {
|
2013-07-14 01:31:16 +08:00
|
|
|
doMessenger();
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
|
|
|
else if (line[1] == 'm') { //message command: /m friendnumber messsage
|
2013-07-14 01:31:16 +08:00
|
|
|
int i;
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(line);
|
2013-07-14 01:31:16 +08:00
|
|
|
char numstring[len-3];
|
|
|
|
char message[len-3];
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (line[i+3] != ' ') {
|
|
|
|
numstring[i] = line[i+3];
|
|
|
|
} else {
|
|
|
|
int j;
|
2013-07-27 11:07:25 +08:00
|
|
|
for (j=i+1; j<len; j++)
|
2013-07-14 11:11:57 +08:00
|
|
|
message[j-i-1] = line[j+3];
|
2013-07-14 01:31:16 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int num = atoi(numstring);
|
2013-07-28 09:33:01 +08:00
|
|
|
if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) {
|
2013-07-30 02:17:30 +08:00
|
|
|
new_lines("[i] could not send message");
|
2013-07-28 09:33:01 +08:00
|
|
|
}
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
|
|
|
else if (line[1] == 'n') {
|
2013-07-19 01:56:50 +08:00
|
|
|
uint8_t name[MAX_NAME_LENGTH];
|
|
|
|
int i = 0;
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(line);
|
|
|
|
for (i=3; i<len; i++) {
|
2013-07-19 01:56:50 +08:00
|
|
|
if (line[i] == 0 || line[i] == '\n') break;
|
|
|
|
name[i - 3] = line[i];
|
|
|
|
}
|
|
|
|
name[i - 3] = 0;
|
|
|
|
setname(name, i);
|
|
|
|
char numstring[100];
|
2013-07-21 21:52:43 +08:00
|
|
|
sprintf(numstring, "[i] changed nick to %s", (char*)name);
|
2013-07-19 01:56:50 +08:00
|
|
|
new_lines(numstring);
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
|
|
|
else if (line[1] == 's') {
|
2013-07-19 01:56:50 +08:00
|
|
|
uint8_t status[MAX_USERSTATUS_LENGTH];
|
|
|
|
int i = 0;
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(line);
|
|
|
|
for (i=3; i<len; i++) {
|
2013-07-19 01:56:50 +08:00
|
|
|
if (line[i] == 0 || line[i] == '\n') break;
|
|
|
|
status[i - 3] = line[i];
|
|
|
|
}
|
|
|
|
status[i - 3] = 0;
|
|
|
|
m_set_userstatus(status, strlen((char*)status));
|
|
|
|
char numstring[100];
|
2013-07-21 21:52:43 +08:00
|
|
|
sprintf(numstring, "[i] changed status to %s", (char*)status);
|
2013-07-19 01:56:50 +08:00
|
|
|
new_lines(numstring);
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
2013-07-28 08:04:56 +08:00
|
|
|
else if (line[1] == 'a') {
|
|
|
|
uint8_t numf = atoi(line + 3);
|
|
|
|
char numchar[100];
|
|
|
|
sprintf(numchar, "[i] friend request %u accepted", numf);
|
|
|
|
new_lines(numchar);
|
|
|
|
int num = m_addfriend_norequest(pending_requests[numf]);
|
|
|
|
sprintf(numchar, "[i] added friendnumber %d", num);
|
|
|
|
new_lines(numchar);
|
|
|
|
do_refresh();
|
|
|
|
|
|
|
|
}
|
2013-07-27 11:07:25 +08:00
|
|
|
else if (line[1] == 'q') { //exit
|
2013-07-24 00:11:07 +08:00
|
|
|
endwin();
|
|
|
|
exit(EXIT_SUCCESS);
|
2013-07-14 01:31:16 +08:00
|
|
|
}
|
|
|
|
} else {
|
2013-07-14 09:57:09 +08:00
|
|
|
//new_lines(line);
|
2013-07-14 01:31:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
|
2013-07-14 01:31:16 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
strcpy(output,input);
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(output);
|
2013-07-27 11:07:25 +08:00
|
|
|
for (i = line_width; i < len; i = i + line_width) {
|
2013-07-14 01:31:16 +08:00
|
|
|
while (output[i] != ' ' && i != 0) {
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
if (i > 0) {
|
|
|
|
output[i] = '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
int count_lines(char *string)
|
2013-07-14 01:31:16 +08:00
|
|
|
{
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(string);
|
2013-07-14 01:31:16 +08:00
|
|
|
int i;
|
|
|
|
int count = 1;
|
|
|
|
for (i=0; i < len; i++) {
|
2013-07-27 11:07:25 +08:00
|
|
|
if (string[i] == '\n')
|
2013-07-14 01:31:16 +08:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *appender(char *str, const char c)
|
|
|
|
{
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(str);
|
2013-07-14 01:31:16 +08:00
|
|
|
if (len < STRING_LENGTH) {
|
|
|
|
str[len + 1] = str[len];
|
|
|
|
str[len] = c;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_refresh()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int count=0;
|
|
|
|
int l;
|
|
|
|
char wrap_output[STRING_LENGTH];
|
|
|
|
for (i=0; i<HISTORY; i++) {
|
|
|
|
wrap(wrap_output, lines[i], x);
|
|
|
|
l = count_lines(wrap_output);
|
|
|
|
count = count + l;
|
|
|
|
if (count < y) {
|
|
|
|
move(y-1-count,0);
|
|
|
|
printw(wrap_output);
|
|
|
|
clrtoeol();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
move(y-1,0);
|
|
|
|
clrtoeol();
|
|
|
|
printw(">> ");
|
|
|
|
printw(line);
|
|
|
|
clrtoeol();
|
|
|
|
refresh();
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
|
2013-07-14 01:31:16 +08:00
|
|
|
{
|
2013-07-28 08:04:56 +08:00
|
|
|
new_lines("[i] received friend request with message:");
|
|
|
|
new_lines((char *)data);
|
|
|
|
char numchar[100];
|
2013-07-30 02:17:30 +08:00
|
|
|
sprintf(numchar, "[i] accept request with /a %u", num_requests);
|
2013-07-28 08:04:56 +08:00
|
|
|
new_lines(numchar);
|
|
|
|
memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
|
|
|
|
++num_requests;
|
2013-07-14 01:31:16 +08:00
|
|
|
do_refresh();
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
void print_message(int friendnumber, uint8_t * string, uint16_t length)
|
|
|
|
{
|
2013-07-24 00:11:07 +08:00
|
|
|
char name[MAX_NAME_LENGTH];
|
2013-07-19 01:56:50 +08:00
|
|
|
getname(friendnumber, (uint8_t*)name);
|
|
|
|
char msg[100+length+strlen(name)+1];
|
2013-07-22 04:54:09 +08:00
|
|
|
time_t rawtime;
|
|
|
|
struct tm * timeinfo;
|
|
|
|
time ( &rawtime );
|
|
|
|
timeinfo = localtime ( &rawtime );
|
2013-07-23 03:30:59 +08:00
|
|
|
char* temp = asctime(timeinfo);
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(temp);
|
2013-07-23 03:30:59 +08:00
|
|
|
temp[len-1]='\0';
|
2013-07-30 02:17:30 +08:00
|
|
|
sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // timestamp
|
2013-07-19 01:56:50 +08:00
|
|
|
new_lines(msg);
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
|
2013-07-24 00:11:07 +08:00
|
|
|
char name[MAX_NAME_LENGTH];
|
2013-07-19 01:56:50 +08:00
|
|
|
getname(friendnumber, (uint8_t*)name);
|
2013-07-14 10:27:23 +08:00
|
|
|
char msg[100+length];
|
2013-07-21 21:52:43 +08:00
|
|
|
sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
|
2013-07-19 01:56:50 +08:00
|
|
|
new_lines(msg);
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
|
2013-07-24 00:11:07 +08:00
|
|
|
char name[MAX_NAME_LENGTH];
|
2013-07-19 01:56:50 +08:00
|
|
|
getname(friendnumber, (uint8_t*)name);
|
|
|
|
char msg[100+length+strlen(name)+1];
|
2013-07-21 21:52:43 +08:00
|
|
|
sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
|
2013-07-14 10:27:23 +08:00
|
|
|
new_lines(msg);
|
2013-07-14 01:31:16 +08:00
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-07-24 00:11:07 +08:00
|
|
|
void load_key(){
|
2013-07-23 01:09:30 +08:00
|
|
|
FILE *data_file = NULL;
|
|
|
|
if ((data_file = fopen("data","r"))) {
|
2013-07-24 00:11:07 +08:00
|
|
|
//load keys
|
|
|
|
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){
|
2013-07-30 02:17:30 +08:00
|
|
|
printf("[i] could not read data file\n[i] exiting\n");
|
2013-07-24 00:11:07 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Messenger_load(data, size);
|
|
|
|
} else {
|
|
|
|
//else save new keys
|
|
|
|
int size = Messenger_size();
|
|
|
|
uint8_t data[size];
|
|
|
|
Messenger_save(data);
|
|
|
|
data_file = fopen("data","w");
|
|
|
|
if(fwrite(data, sizeof(uint8_t), size, data_file) != size){
|
2013-07-30 02:17:30 +08:00
|
|
|
printf("[i] could not write data file\n[i] exiting\n");
|
2013-07-24 00:11:07 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2013-07-23 01:09:30 +08:00
|
|
|
fclose(data_file);
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-07-14 06:27:52 +08:00
|
|
|
if (argc < 4) {
|
2013-07-23 01:09:30 +08:00
|
|
|
printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
|
2013-07-14 06:27:52 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
2013-07-14 01:31:16 +08:00
|
|
|
int c;
|
2013-07-17 07:45:20 +08:00
|
|
|
int on = 0;
|
2013-07-14 01:31:16 +08:00
|
|
|
initMessenger();
|
2013-07-22 08:52:02 +08:00
|
|
|
//if keyfiles exist
|
2013-07-23 01:09:30 +08:00
|
|
|
if(argc > 4){
|
|
|
|
if(strncmp(argv[4], "nokey", 6) < 0){
|
2013-07-23 02:52:42 +08:00
|
|
|
//load_key();
|
2013-07-22 19:26:25 +08:00
|
|
|
}
|
2013-07-23 01:09:30 +08:00
|
|
|
} else {
|
|
|
|
load_key();
|
2013-07-22 08:52:02 +08:00
|
|
|
}
|
2013-07-14 01:31:16 +08:00
|
|
|
m_callback_friendrequest(print_request);
|
|
|
|
m_callback_friendmessage(print_message);
|
2013-07-19 01:56:50 +08:00
|
|
|
m_callback_namechange(print_nickchange);
|
|
|
|
m_callback_userstatus(print_statuschange);
|
2013-07-14 01:31:16 +08:00
|
|
|
char idstring0[200];
|
|
|
|
char idstring1[32][5];
|
|
|
|
char idstring2[32][5];
|
|
|
|
uint32_t i;
|
|
|
|
for(i = 0; i < 32; i++)
|
|
|
|
{
|
2013-07-27 11:07:25 +08:00
|
|
|
if(self_public_key[i] < 16)
|
2013-07-14 01:31:16 +08:00
|
|
|
strcpy(idstring1[i],"0");
|
2013-07-27 11:07:25 +08:00
|
|
|
else
|
2013-07-14 01:31:16 +08:00
|
|
|
strcpy(idstring1[i], "");
|
|
|
|
sprintf(idstring2[i], "%hhX",self_public_key[i]);
|
|
|
|
}
|
2013-07-21 21:52:43 +08:00
|
|
|
strcpy(idstring0,"[i] your ID: ");
|
2013-07-27 11:07:25 +08:00
|
|
|
for (i=0; i<32; i++) {
|
2013-07-14 01:31:16 +08:00
|
|
|
strcat(idstring0,idstring1[i]);
|
|
|
|
strcat(idstring0,idstring2[i]);
|
|
|
|
}
|
|
|
|
initscr();
|
|
|
|
noecho();
|
|
|
|
raw();
|
|
|
|
getmaxyx(stdscr,y,x);
|
|
|
|
new_lines(idstring0);
|
2013-07-21 21:52:43 +08:00
|
|
|
new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status), /n nick (to change nickname), /q (to quit)");
|
2013-07-14 01:31:16 +08:00
|
|
|
strcpy(line, "");
|
2013-07-14 06:27:52 +08:00
|
|
|
IP_Port bootstrap_ip_port;
|
|
|
|
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
2013-07-21 05:56:18 +08:00
|
|
|
int resolved_address = resolve_addr(argv[1]);
|
2013-07-27 11:07:25 +08:00
|
|
|
if (resolved_address != -1)
|
2013-07-21 02:50:54 +08:00
|
|
|
bootstrap_ip_port.ip.i = resolved_address;
|
2013-07-27 11:07:25 +08:00
|
|
|
else
|
2013-07-21 02:50:54 +08:00
|
|
|
exit(1);
|
2013-07-27 11:07:25 +08:00
|
|
|
|
2013-07-14 06:27:52 +08:00
|
|
|
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
|
2013-07-14 09:57:09 +08:00
|
|
|
nodelay(stdscr, TRUE);
|
2013-07-14 06:27:52 +08:00
|
|
|
while(true) {
|
2013-07-26 16:02:17 +08:00
|
|
|
if (on == 0 && DHT_isconnected()) {
|
2013-07-24 00:11:07 +08:00
|
|
|
new_lines("[i] connected to DHT\n[i] define username with /n");
|
|
|
|
on = 1;
|
2013-07-17 07:45:20 +08:00
|
|
|
}
|
2013-07-24 00:11:07 +08:00
|
|
|
|
2013-07-14 06:27:52 +08:00
|
|
|
doMessenger();
|
2013-07-17 07:02:44 +08:00
|
|
|
c_sleep(1);
|
2013-07-14 01:31:16 +08:00
|
|
|
do_refresh();
|
2013-07-24 00:11:07 +08:00
|
|
|
|
|
|
|
c = getch();
|
|
|
|
|
|
|
|
if (c == ERR || c == 27)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
getmaxyx(stdscr, y, x);
|
|
|
|
if (c == '\n') {
|
|
|
|
line_eval(lines, line);
|
|
|
|
strcpy(line, "");
|
|
|
|
} else if (c == 127) {
|
|
|
|
line[strlen(line) - 1] = '\0';
|
|
|
|
} else if (isalnum(c) || ispunct(c) || c == ' ') {
|
|
|
|
strcpy(line, appender(line, (char) c));
|
|
|
|
}
|
2013-07-14 01:31:16 +08:00
|
|
|
}
|
|
|
|
endwin();
|
|
|
|
return 0;
|
|
|
|
}
|