Merge branch 'master' of https://github.com/JFreegman/ProjectTox-Core into JFreegman-master

Conflicts:
	testing/nTox.c
This commit is contained in:
irungentoo 2013-07-31 10:25:29 -04:00
commit bdd13d1954
4 changed files with 125 additions and 128 deletions

View File

@ -23,9 +23,9 @@
#include "nTox.h"
#include "misc_tools.h"
#include <stdio.h>
#include <time.h>
#ifdef WIN32
#define c_sleep(x) Sleep(1*x)
#else
@ -35,20 +35,22 @@
char lines[HISTORY][STRING_LENGTH];
char line[STRING_LENGTH];
char *help = "[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)\n"
"[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)";
int x,y;
int x, y;
uint8_t pending_requests[256][CLIENT_ID_SIZE];
uint8_t num_requests;
uint8_t num_requests = 0;
void new_lines(char *line)
{
int i;
for (i = HISTORY-1; i > 0; i--)
strcpy(lines[i],lines[i-1]);
strcpy(lines[i], lines[i-1]);
strcpy(lines[0],line);
strcpy(lines[0], line);
do_refresh();
}
@ -56,10 +58,10 @@ void new_lines(char *line)
void print_friendlist()
{
char name[MAX_NAME_LENGTH];
uint32_t i;
new_lines("[i] Friend List:");
for (i=0; i <= num_requests; i++) {
uint32_t i;
for (i = 0; i <= num_requests; i++) {
printf ("num_resusts: %d\n", num_requests);
char fstring[128];
getname(i, (uint8_t*)name);
@ -75,19 +77,20 @@ void print_friendlist()
char *format_message(char *message, int friendnum)
{
char name[MAX_NAME_LENGTH];
if(friendnum != -1) {
getname(friendnum, (uint8_t*)name);
if (friendnum != -1) {
getname(friendnum, (uint8_t*)name);
} else {
getself_name((uint8_t*)name);
getself_name((uint8_t*)name);
}
char *msg = malloc(100+strlen(message)+strlen(name)+1);
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
char* time = asctime(timeinfo);
size_t len = strlen(time);
time[len-1]='\0';
time[len-1] = '\0';
sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message); // timestamp
return msg;
}
@ -95,13 +98,14 @@ char *format_message(char *message, int friendnum)
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
{
if (line[0] == '/') {
char command[STRING_LENGTH + 2] = "> ";
strcat(command, line);
new_lines(command);
if (line[1] == 'f') { // add friend command: /f ID
char inpt_command = line[1];
char prompt[STRING_LENGTH + 2] = "> ";
strcat(prompt, line);
new_lines(prompt);
if (inpt_command == 'f') { // add friend command: /f ID
int i;
char temp_id[128];
for (i=0; i<128; i++)
for (i = 0; i < 128; i++)
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];
@ -109,63 +113,63 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
new_lines(numstring);
do_refresh();
}
else if (line[1] == 'd') {
else if (inpt_command == 'd') {
doMessenger();
}
else if (line[1] == 'm') { //message command: /m friendnumber messsage
int i;
else if (inpt_command == 'm') { //message command: /m friendnumber messsage
size_t len = strlen(line);
char numstring[len-3];
char message[len-3];
for (i=0; i<len; i++) {
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++)
for (j = (i+1); j < len; j++)
message[j-i-1] = line[j+3];
break;
}
}
int num = atoi(numstring);
if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) {
if (m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) {
new_lines("[i] could not send message");
} else {
new_lines(format_message(message, -1));
}
}
else if (line[1] == 'n') {
else if (inpt_command == 'n') {
uint8_t name[MAX_NAME_LENGTH];
int i = 0;
size_t len = strlen(line);
for (i=3; i<len; i++) {
for (i = 3; i < len; i++) {
if (line[i] == 0 || line[i] == '\n') break;
name[i - 3] = line[i];
name[i-3] = line[i];
}
name[i - 3] = 0;
name[i-3] = 0;
setname(name, i);
char numstring[100];
sprintf(numstring, "[i] changed nick to %s", (char*)name);
new_lines(numstring);
}
else if (line[1] == 'l') {
else if (inpt_command == 'l') {
print_friendlist();
}
else if (line[1] == 's') {
else if (inpt_command == 's') {
uint8_t status[MAX_USERSTATUS_LENGTH];
int i = 0;
size_t len = strlen(line);
for (i=3; i<len; i++) {
for (i = 3; i < len; i++) {
if (line[i] == 0 || line[i] == '\n') break;
status[i - 3] = line[i];
status[i-3] = line[i];
}
status[i - 3] = 0;
status[i-3] = 0;
m_set_userstatus(status, strlen((char*)status));
char numstring[100];
sprintf(numstring, "[i] changed status to %s", (char*)status);
new_lines(numstring);
}
else if (line[1] == 'a') {
else if (inpt_command == 'a') {
uint8_t numf = atoi(line + 3);
char numchar[100];
sprintf(numchar, "[i] friend request %u accepted", numf);
@ -176,34 +180,34 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
do_refresh();
}
else if (line[1] == 'h') { //help
new_lines(help);
else if (inpt_command == 'h') { //help
new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)");
new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)");
}
else if (line[1] == 'i') { //info
else if (inpt_command == 'i') { //info
char idstring0[200];
char idstring1[32][5];
char idstring2[32][5];
uint32_t i;
for(i = 0; i < 32; i++)
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] < 16)
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]);
sprintf(idstring2[i], "%hhX", self_public_key[i]);
}
//
strcpy(idstring0,"[i] ID: ");
for (i=0; i<32; i++) {
strcat(idstring0,idstring1[i]);
strcat(idstring0,idstring2[i]);
int j;
for (j = 0; j < PUB_KEY_BYTES; j++) {
strcat(idstring0,idstring1[j]);
strcat(idstring0,idstring2[j]);
}
new_lines(idstring0);
}
else if (line[1] == 'q') { //exit
else if (inpt_command == 'q') { //exit
endwin();
exit(EXIT_SUCCESS);
} else {
@ -217,9 +221,9 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
{
int i = 0;
strcpy(output,input);
size_t len = strlen(output);
int i = 0;
for (i = line_width; i < len; i = i + line_width) {
while (output[i] != ' ' && i != 0) {
i--;
@ -233,9 +237,9 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
int count_lines(char *string)
{
size_t len = strlen(string);
int i;
int count = 1;
for (i=0; i < len; i++) {
int i;
for (i = 0; i < len; i++) {
if (string[i] == '\n')
count++;
}
@ -246,7 +250,7 @@ char *appender(char *str, const char c)
{
size_t len = strlen(str);
if (len < STRING_LENGTH) {
str[len + 1] = str[len];
str[len+1] = str[len];
str[len] = c;
}
return str;
@ -254,21 +258,21 @@ char *appender(char *str, const char c)
void do_refresh()
{
int i;
int count=0;
int l;
char wrap_output[STRING_LENGTH];
for (i=0; i<HISTORY; i++) {
int L;
int i;
for (i = 0; i < HISTORY; i++) {
wrap(wrap_output, lines[i], x);
l = count_lines(wrap_output);
count = count + l;
L = count_lines(wrap_output);
count = count + L;
if (count < y) {
move(y-1-count,0);
move(y-1-count, 0);
printw(wrap_output);
clrtoeol();
}
}
move(y-1,0);
move(y-1, 0);
clrtoeol();
printw(">> ");
printw(line);
@ -299,12 +303,13 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
timeinfo = localtime ( &rawtime );
char* temp = asctime(timeinfo);
size_t len = strlen(temp);
temp[len-1]='\0';
temp[len-1] = '\0';
sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // timestamp
new_lines(format_message((char*)string, friendnumber));
}
void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
{
char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name);
char msg[100+length];
@ -312,7 +317,8 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
new_lines(msg);
}
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
{
char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name);
char msg[100+length+strlen(name)+1];
@ -320,15 +326,17 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
new_lines(msg);
}
void load_key(){
void load_key()
{
FILE *data_file = NULL;
if ((data_file = fopen("data","r"))) {
data_file = fopen("data","r");
if (data_file) {
//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){
if (fread(data, sizeof(uint8_t), size, data_file) != size){
printf("[i] could not read data file\n[i] exiting\n");
exit(1);
}
@ -339,7 +347,7 @@ void load_key(){
uint8_t data[size];
Messenger_save(data);
data_file = fopen("data","w");
if(fwrite(data, sizeof(uint8_t), size, data_file) != size){
if (fwrite(data, sizeof(uint8_t), size, data_file) != size){
printf("[i] could not write data file\n[i] exiting\n");
exit(1);
}
@ -369,26 +377,27 @@ int main(int argc, char *argv[])
m_callback_namechange(print_nickchange);
m_callback_userstatus(print_statuschange);
char idstring0[200];
char idstring1[32][5];
char idstring2[32][5];
uint32_t i;
for(i = 0; i < 32; i++)
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] < 16)
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(idstring0,"[i] your ID: ");
for (i=0; i<32; i++) {
strcat(idstring0,idstring1[i]);
strcat(idstring0,idstring2[i]);
int j;
for (j = 0; j < PUB_KEY_BYTES; j++) {
strcat(idstring0,idstring1[j]);
strcat(idstring0,idstring2[j]);
}
initscr();
noecho();
raw();
getmaxyx(stdscr,y,x);
getmaxyx(stdscr, y, x);
new_lines(idstring0);
new_lines(help);
strcpy(line, "");
@ -413,7 +422,6 @@ int main(int argc, char *argv[])
do_refresh();
c = getch();
if (c == ERR || c == 27)
continue;
@ -422,7 +430,7 @@ int main(int argc, char *argv[])
line_eval(lines, line);
strcpy(line, "");
} else if (c == 127) {
line[strlen(line) - 1] = '\0';
line[strlen(line)-1] = '\0';
} else if (isalnum(c) || ispunct(c) || c == ' ') {
strcpy(line, appender(line, (char) c));
}

View File

@ -39,6 +39,7 @@
#include "../core/network.h"
#define STRING_LENGTH 256
#define HISTORY 50
#define PUB_KEY_BYTES 32
void new_lines(char *line);
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line);

View File

@ -27,7 +27,7 @@
#include <process.h>
uint8_t pending_requests[256][CLIENT_ID_SIZE];
uint8_t num_requests;
uint8_t num_requests = 0;
char line[STRING_LENGTH];
char users_id[200];
@ -88,14 +88,13 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
void load_key()
{
FILE *data_file = NULL;
if ((data_file = fopen("data", "r"))) {
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) {
if (fread(data, sizeof(uint8_t), size, data_file) != size) {
printf("\n[i] Could not read the data file. Exiting.");
exit(1);
}
@ -107,23 +106,23 @@ void load_key()
Messenger_save(data);
data_file = fopen("data", "w");
if(fwrite(data, sizeof(uint8_t), size, data_file) != size) {
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 line_eval(char* line)
{
if(line[0] == '/') {
char inpt_command = line[1];
/* Add friend */
if(line[1] == 'f') {
if(inpt_command == 'f') {
int i;
char temp_id[128];
for (i=0; i<128; i++)
for (i = 0; i < 128; i++)
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];
@ -131,28 +130,27 @@ void line_eval(char* line)
printf(numstring);
}
else if (line[1] == 'r') {
else if (inpt_command == 'r') {
do_header();
printf("\n\n");
}
else if (line[1] == 'l') {
else if (inpt_command == 'l') {
printf("\n[i] Friend List | Total: %d\n\n", getnumfriends());
int i;
for (i=0; i < getnumfriends(); i++) {
for (i = 0; i < getnumfriends(); i++) {
char name[MAX_NAME_LENGTH];
getname(i, (uint8_t*)name);
printf("[%d] %s\n\n", i, (uint8_t*)name);
}
}
else if (line[1] == 'd') {
else if (inpt_command == 'd') {
size_t len = strlen(line);
char numstring[len-3];
int i;
for (i=0; i<len; i++) {
for (i = 0; i < len; i++) {
if (line[i+3] != ' ') {
numstring[i] = line[i+3];
}
@ -161,17 +159,17 @@ void line_eval(char* line)
m_delfriend(num);
}
/* Send message to friend */
else if (line[1] == 'm') {
int i;
else if (inpt_command == 'm') {
size_t len = strlen(line);
char numstring[len-3];
char message[len-3];
for (i=0; i<len; i++) {
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++)
for (j = (i+1); j < len; j++)
message[j-i-1] = line[j+3];
break;
}
@ -185,37 +183,37 @@ void line_eval(char* line)
}
}
else if (line[1] == 'n') {
else if (inpt_command == 'n') {
uint8_t name[MAX_NAME_LENGTH];
int i = 0;
size_t len = strlen(line);
for (i=3; i<len; i++) {
for (i = 3; i < len; i++) {
if (line[i] == 0 || line[i] == '\n') break;
name[i - 3] = line[i];
name[i-3] = line[i];
}
name[i - 3] = 0;
name[i-3] = 0;
setname(name, i);
char numstring[100];
sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name);
printf(numstring);
}
else if (line[1] == 's') {
else if (inpt_command == 's') {
uint8_t status[MAX_USERSTATUS_LENGTH];
int i = 0;
size_t len = strlen(line);
for (i=3; i<len; i++) {
for (i = 3; i < len; i++) {
if (line[i] == 0 || line[i] == '\n') break;
status[i - 3] = line[i];
status[i-3] = line[i];
}
status[i - 3] = 0;
status[i-3] = 0;
m_set_userstatus(status, strlen((char*)status));
char numstring[100];
sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status);
printf(numstring);
}
else if (line[1] == 'a') {
else if (inpt_command == 'a') {
uint8_t numf = atoi(line + 3);
char numchar[100];
sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf);
@ -225,12 +223,10 @@ void line_eval(char* line)
printf(numchar);
}
/* EXIT */
else if (line[1] == 'q') {
else if (inpt_command == 'q') {
exit(EXIT_SUCCESS);
}
}
else {
} else {
//nothing atm
}
}
@ -250,12 +246,10 @@ int main(int argc, char *argv[])
printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
exit(0);
}
if (initMessenger() == -1) {
printf("initMessenger failed");
exit(0);
}
if (argc > 4) {
if(strncmp(argv[4], "nokey", 6) < 0) {
}
@ -267,26 +261,25 @@ int main(int argc, char *argv[])
m_callback_friendmessage(print_message);
m_callback_namechange(print_nickchange);
m_callback_userstatus(print_statuschange);
char idstring1[32][5];
char idstring2[32][5];
uint32_t i;
for(i = 0; i < 32; i++)
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] < 16)
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: ");
for (i=0; i<32; i++) {
strcat(users_id,idstring1[i]);
strcat(users_id,idstring2[i]);
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]);
@ -296,12 +289,9 @@ int main(int argc, char *argv[])
exit(1);
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
int c;
int on = 0;
_beginthread(get_input, 0, NULL);
while(1) {
if (on == 1 && DHT_isconnected() == -1) {
printf("\n---------------------------------");
@ -309,16 +299,13 @@ int main(int argc, char *argv[])
printf("\n---------------------------------\n\n");
on = 0;
}
if (on == 0 && DHT_isconnected()) {
printf("\n[i] Connected to DHT");
printf("\n---------------------------------\n\n");
on = 1;
}
doMessenger();
Sleep(1);
}
return 0;
}

View File

@ -27,5 +27,6 @@
#include "../core/network.h"
#define STRING_LENGTH 256
#define PUB_KEY_BYTES 32
#endif