Merge branch 'nTox' of https://github.com/CharmlessCoin/ProjectTox-Core into CharmlessCoin-nTox

Conflicts:
	testing/nTox.c
This commit is contained in:
irungentoo 2013-08-03 16:46:58 -04:00
commit 09a6d2d351
2 changed files with 68 additions and 32 deletions

View File

@ -36,11 +36,12 @@
char lines[HISTORY][STRING_LENGTH]; char lines[HISTORY][STRING_LENGTH];
char line[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" char *help = "[i] commands:\n/f ID (to add friend)\n/m friendnumber message "
"[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"; "(to send message)\n/s status (to change status)\n[i] /l list (l"
"ist friends)\n/h for help\n/i for info\n/n nick (to change nick"
"name)\n/q (to quit)";
int x, y; int x, y;
uint8_t pending_requests[256][CLIENT_ID_SIZE]; uint8_t pending_requests[256][CLIENT_ID_SIZE];
uint8_t num_requests = 0; uint8_t num_requests = 0;
@ -82,18 +83,23 @@ void new_lines(char *line)
void print_friendlist() void print_friendlist()
{ {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
int i = 0;
new_lines("[i] Friend List:"); new_lines("[i] Friend List:");
uint32_t i; while(getname(i, (uint8_t *)name) != -1) {
for (i = 0; i <= num_requests; i++) { /* account for the longest name and the longest "base" string */
char fstring[128]; char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")];
getname(i, (uint8_t*)name);
if (strlen(name) <= 0) { if (strlen(name) <= 0) {
sprintf(fstring, "[i] Friend: NULL\n\tid: %i", i); sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i);
} else { } else {
sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i); sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i);
} }
i++;
new_lines(fstring); new_lines(fstring);
} }
if(i == 0)
new_lines("\tno friends! D:");
} }
char *format_message(char *message, int friendnum) char *format_message(char *message, int friendnum)
@ -122,7 +128,7 @@ char *format_message(char *message, int friendnum)
return msg; return msg;
} }
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) void line_eval(char *line)
{ {
if (line[0] == '/') { if (line[0] == '/') {
char inpt_command = line[1]; char inpt_command = line[1];
@ -236,8 +242,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
do_refresh(); do_refresh();
} }
else if (inpt_command == 'h') { //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(help);
new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)");
} }
else if (inpt_command == 'i') { //info else if (inpt_command == 'i') { //info
char idstring[200]; char idstring[200];
@ -353,10 +358,10 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
new_lines(msg); new_lines(msg);
} }
void load_key() void load_key(char *path)
{ {
FILE *data_file = NULL; FILE *data_file = fopen(path, "r");
data_file = fopen("data","r");
if (data_file) { if (data_file) {
//load keys //load keys
fseek(data_file, 0, SEEK_END); fseek(data_file, 0, SEEK_END);
@ -368,51 +373,81 @@ void load_key()
exit(1); exit(1);
} }
Messenger_load(data, size); Messenger_load(data, size);
} else {
} else {
//else save new keys //else save new keys
int size = Messenger_size(); int size = Messenger_size();
uint8_t data[size]; uint8_t data[size];
Messenger_save(data); Messenger_save(data);
data_file = fopen("data","w"); data_file = fopen(path, "w");
if(!data_file) {
perror("[!] load_key");
exit(1);
}
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"); puts("[i] could not write data file! exiting...");
exit(1); exit(1);
} }
} }
fclose(data_file); fclose(data_file);
} }
void print_help(void)
{
printf("nTox %.1f - Command-line tox-core client\n", 0.1);
puts("Options:");
puts("\t-h\t-\tPrint this help and exit.");
puts("\t-f\t-\tSpecify a keyfile to read (or write to) from.");
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int on = 0;
int c = 0;
int i = 0;
char *filename = "data";
char idstring[200] = {0};
if (argc < 4) { if (argc < 4) {
printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]);
exit(0); exit(0);
} }
int c;
int on = 0; for(i = 0; i < argc; i++) {
initMessenger(); if(argv[i][0] == '-') {
//if keyfiles exist if(argv[i][1] == 'h') {
if(argc > 4){ print_help();
if(strncmp(argv[4], "nokey", 6) < 0){ exit(0);
//load_key(); } else if(argv[i][1] == 'f') {
if(argv[i + 1] != NULL)
filename = argv[i + 1];
else {
fputs("[!] you passed '-f' without giving an argument!\n", stderr);
}
}
} }
} else {
load_key();
} }
initMessenger();
load_key(filename);
m_callback_friendrequest(print_request); m_callback_friendrequest(print_request);
m_callback_friendmessage(print_message); m_callback_friendmessage(print_message);
m_callback_namechange(print_nickchange); m_callback_namechange(print_nickchange);
m_callback_userstatus(print_statuschange); m_callback_userstatus(print_statuschange);
char idstring[200];
get_id(idstring);
initscr(); initscr();
noecho(); noecho();
raw(); raw();
getmaxyx(stdscr, y, x); getmaxyx(stdscr, y, x);
new_lines("/h for list of commands");
get_id(idstring);
new_lines(idstring); new_lines(idstring);
new_lines(help);
strcpy(line, ""); strcpy(line, "");
IP_Port bootstrap_ip_port; IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.port = htons(atoi(argv[2]));
int resolved_address = resolve_addr(argv[1]); int resolved_address = resolve_addr(argv[1]);
@ -439,7 +474,7 @@ int main(int argc, char *argv[])
getmaxyx(stdscr, y, x); getmaxyx(stdscr, y, x);
if (c == '\n') { if (c == '\n') {
line_eval(lines, line); line_eval(line);
strcpy(line, ""); strcpy(line, "");
} else if (c == 8 || c == 127) { } else if (c == 8 || c == 127) {
line[strlen(line)-1] = '\0'; line[strlen(line)-1] = '\0';

View File

@ -37,12 +37,13 @@
#include <netdb.h> #include <netdb.h>
#include "../core/Messenger.h" #include "../core/Messenger.h"
#include "../core/network.h" #include "../core/network.h"
#define STRING_LENGTH 256 #define STRING_LENGTH 256
#define HISTORY 50 #define HISTORY 50
#define PUB_KEY_BYTES 32 #define PUB_KEY_BYTES 32
void new_lines(char *line); void new_lines(char *line);
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line); void line_eval(char *line);
void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ;
int count_lines(char *string) ; int count_lines(char *string) ;
char *appender(char *str, const char c); char *appender(char *str, const char c);