Clean up/format code

This commit is contained in:
Stuart Banks 2013-07-31 02:15:01 -04:00
parent 52a77cc997
commit 207c067cf2
4 changed files with 116 additions and 124 deletions

View File

@ -23,9 +23,9 @@
#include "nTox.h" #include "nTox.h"
#include "misc_tools.h" #include "misc_tools.h"
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#ifdef WIN32 #ifdef WIN32
#define c_sleep(x) Sleep(1*x) #define c_sleep(x) Sleep(1*x)
#else #else
@ -54,9 +54,8 @@ void new_lines(char *line)
void print_friendlist() void print_friendlist()
{ {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
uint32_t i;
new_lines("[i] Friend List:"); new_lines("[i] Friend List:");
uint32_t i;
for (i = 0; i <= num_requests; i++) { for (i = 0; i <= num_requests; i++) {
char fstring[128]; char fstring[128];
@ -93,10 +92,11 @@ char *format_message(char *message, int friendnum)
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
{ {
if (line[0] == '/') { if (line[0] == '/') {
char command[STRING_LENGTH + 2] = "> "; char inpt_command = line[1];
strcat(command, line); char prompt[STRING_LENGTH + 2] = "> ";
new_lines(command); strcat(prompt, line);
if (line[1] == 'f') { // add friend command: /f ID new_lines(prompt);
if (inpt_command == 'f') { // add friend command: /f ID
int i; int i;
char temp_id[128]; char temp_id[128];
for (i = 0; i < 128; i++) for (i = 0; i < 128; i++)
@ -107,20 +107,20 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
new_lines(numstring); new_lines(numstring);
do_refresh(); do_refresh();
} }
else if (line[1] == 'd') { else if (inpt_command == 'd') {
doMessenger(); doMessenger();
} }
else if (line[1] == 'm') { //message command: /m friendnumber messsage else if (inpt_command == 'm') { //message command: /m friendnumber messsage
int i;
size_t len = strlen(line); size_t len = strlen(line);
char numstring[len-3]; char numstring[len-3];
char message[len-3]; char message[len-3];
int i;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (line[i+3] != ' ') { if (line[i+3] != ' ') {
numstring[i] = line[i+3]; numstring[i] = line[i+3];
} else { } else {
int j; int j;
for (j=i+1; j<len; j++) for (j = (i+1); j < len; j++)
message[j-i-1] = line[j+3]; message[j-i-1] = line[j+3];
break; break;
} }
@ -132,7 +132,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
new_lines(format_message(message, -1)); new_lines(format_message(message, -1));
} }
} }
else if (line[1] == 'n') { else if (inpt_command == 'n') {
uint8_t name[MAX_NAME_LENGTH]; uint8_t name[MAX_NAME_LENGTH];
int i = 0; int i = 0;
size_t len = strlen(line); size_t len = strlen(line);
@ -146,10 +146,10 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
sprintf(numstring, "[i] changed nick to %s", (char*)name); sprintf(numstring, "[i] changed nick to %s", (char*)name);
new_lines(numstring); new_lines(numstring);
} }
else if (line[1] == 'l') { else if (inpt_command == 'l') {
print_friendlist(); print_friendlist();
} }
else if (line[1] == 's') { else if (inpt_command == 's') {
uint8_t status[MAX_USERSTATUS_LENGTH]; uint8_t status[MAX_USERSTATUS_LENGTH];
int i = 0; int i = 0;
size_t len = strlen(line); size_t len = strlen(line);
@ -163,7 +163,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
sprintf(numstring, "[i] changed status to %s", (char*)status); sprintf(numstring, "[i] changed status to %s", (char*)status);
new_lines(numstring); new_lines(numstring);
} }
else if (line[1] == 'a') { else if (inpt_command == 'a') {
uint8_t numf = atoi(line + 3); uint8_t numf = atoi(line + 3);
char numchar[100]; char numchar[100];
sprintf(numchar, "[i] friend request %u accepted", numf); sprintf(numchar, "[i] friend request %u accepted", numf);
@ -174,20 +174,18 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
do_refresh(); do_refresh();
} }
else if (inpt_command == 'h') { //help
else if (line[1] == '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] 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)"); 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 (line[1] == 'i') { //info
char idstring0[200]; char idstring0[200];
char idstring1[32][5]; char idstring1[PUB_KEY_BYTES][5];
char idstring2[32][5]; char idstring2[PUB_KEY_BYTES][5];
uint32_t i; int i;
for(i = 0; i < 32; 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"); strcpy(idstring1[i],"0");
else else
strcpy(idstring1[i], ""); strcpy(idstring1[i], "");
@ -195,14 +193,15 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
} }
// //
strcpy(idstring0,"[i] ID: "); strcpy(idstring0,"[i] ID: ");
for (i=0; i<32; i++) { int j;
strcat(idstring0,idstring1[i]); for (j = 0; j < PUB_KEY_BYTES; j++) {
strcat(idstring0,idstring2[i]); strcat(idstring0,idstring1[j]);
strcat(idstring0,idstring2[j]);
} }
new_lines(idstring0); new_lines(idstring0);
} }
else if (line[1] == 'q') { //exit else if (inpt_command == 'q') { //exit
endwin(); endwin();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else { } else {
@ -216,9 +215,9 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], 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 i = 0;
strcpy(output,input); strcpy(output,input);
size_t len = strlen(output); size_t len = strlen(output);
int i = 0;
for (i = line_width; i < len; i = i + line_width) { for (i = line_width; i < len; i = i + line_width) {
while (output[i] != ' ' && i != 0) { while (output[i] != ' ' && i != 0) {
i--; i--;
@ -232,8 +231,8 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
int count_lines(char *string) int count_lines(char *string)
{ {
size_t len = strlen(string); size_t len = strlen(string);
int i;
int count = 1; int count = 1;
int i;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (string[i] == '\n') if (string[i] == '\n')
count++; count++;
@ -253,14 +252,14 @@ char *appender(char *str, const char c)
void do_refresh() void do_refresh()
{ {
int i;
int count=0; int count=0;
int l;
char wrap_output[STRING_LENGTH]; char wrap_output[STRING_LENGTH];
int L;
int i;
for (i = 0; i < HISTORY; i++) { for (i = 0; i < HISTORY; i++) {
wrap(wrap_output, lines[i], x); wrap(wrap_output, lines[i], x);
l = count_lines(wrap_output); L = count_lines(wrap_output);
count = count + l; count = count + L;
if (count < y) { if (count < y) {
move(y-1-count, 0); move(y-1-count, 0);
printw(wrap_output); printw(wrap_output);
@ -303,7 +302,8 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
new_lines(format_message((char*)string, friendnumber)); 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]; char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name); getname(friendnumber, (uint8_t*)name);
char msg[100+length]; char msg[100+length];
@ -311,7 +311,8 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
new_lines(msg); 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]; char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name); getname(friendnumber, (uint8_t*)name);
char msg[100+length+strlen(name)+1]; char msg[100+length+strlen(name)+1];
@ -319,9 +320,11 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
new_lines(msg); new_lines(msg);
} }
void load_key(){ void load_key()
{
FILE *data_file = NULL; FILE *data_file = NULL;
if ((data_file = fopen("data","r"))) { data_file = fopen("data","r");
if (data_file) {
//load keys //load keys
fseek(data_file, 0, SEEK_END); fseek(data_file, 0, SEEK_END);
int size = ftell(data_file); int size = ftell(data_file);
@ -368,21 +371,22 @@ int main(int argc, char *argv[])
m_callback_namechange(print_nickchange); m_callback_namechange(print_nickchange);
m_callback_userstatus(print_statuschange); m_callback_userstatus(print_statuschange);
char idstring0[200]; char idstring0[200];
char idstring1[32][5]; char idstring1[PUB_KEY_BYTES][5];
char idstring2[32][5]; char idstring2[PUB_KEY_BYTES][5];
uint32_t i; int i;
for(i = 0; i < 32; 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"); strcpy(idstring1[i],"0");
else else
strcpy(idstring1[i], ""); strcpy(idstring1[i], "");
sprintf(idstring2[i], "%hhX",self_public_key[i]); sprintf(idstring2[i], "%hhX",self_public_key[i]);
} }
strcpy(idstring0,"[i] your ID: "); strcpy(idstring0,"[i] your ID: ");
for (i=0; i<32; i++) { int j;
strcat(idstring0,idstring1[i]); for (j = 0; j < PUB_KEY_BYTES; j++) {
strcat(idstring0,idstring2[i]); strcat(idstring0,idstring1[j]);
strcat(idstring0,idstring2[j]);
} }
initscr(); initscr();
noecho(); noecho();
@ -413,7 +417,6 @@ int main(int argc, char *argv[])
do_refresh(); do_refresh();
c = getch(); c = getch();
if (c == ERR || c == 27) if (c == ERR || c == 27)
continue; continue;

View File

@ -39,6 +39,7 @@
#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
void new_lines(char *line); void new_lines(char *line);
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line); void line_eval(char lines[HISTORY][STRING_LENGTH], char *line);

View File

@ -88,13 +88,12 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
void load_key() void load_key()
{ {
FILE *data_file = NULL; FILE *data_file = NULL;
data_file = fopen("data","r");
if ((data_file = fopen("data", "r"))) { if (data_file) {
fseek(data_file, 0, SEEK_END); fseek(data_file, 0, SEEK_END);
int size = ftell(data_file); int size = ftell(data_file);
fseek(data_file, 0, SEEK_SET); fseek(data_file, 0, SEEK_SET);
uint8_t data[size]; 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."); printf("\n[i] Could not read the data file. Exiting.");
exit(1); exit(1);
@ -112,15 +111,15 @@ void load_key()
exit(1); exit(1);
} }
} }
fclose(data_file); fclose(data_file);
} }
void line_eval(char* line) void line_eval(char* line)
{ {
if(line[0] == '/') { if(line[0] == '/') {
char inpt_command = line[1];
/* Add friend */ /* Add friend */
if(line[1] == 'f') { if(inpt_command == 'f') {
int i; int i;
char temp_id[128]; char temp_id[128];
for (i = 0; i < 128; i++) for (i = 0; i < 128; i++)
@ -131,16 +130,15 @@ void line_eval(char* line)
printf(numstring); printf(numstring);
} }
else if (line[1] == 'r') { else if (inpt_command == 'r') {
do_header(); do_header();
printf("\n\n"); printf("\n\n");
} }
else if (line[1] == 'l') { else if (inpt_command == 'l') {
printf("\n[i] Friend List | Total: %d\n\n", getnumfriends()); printf("\n[i] Friend List | Total: %d\n\n", getnumfriends());
int i; int i;
for (i = 0; i < getnumfriends(); i++) { for (i = 0; i < getnumfriends(); i++) {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
getname(i, (uint8_t*)name); getname(i, (uint8_t*)name);
@ -148,7 +146,7 @@ void line_eval(char* line)
} }
} }
else if (line[1] == 'd') { else if (inpt_command == 'd') {
size_t len = strlen(line); size_t len = strlen(line);
char numstring[len-3]; char numstring[len-3];
int i; int i;
@ -161,17 +159,17 @@ void line_eval(char* line)
m_delfriend(num); m_delfriend(num);
} }
/* Send message to friend */ /* Send message to friend */
else if (line[1] == 'm') { else if (inpt_command == 'm') {
int i;
size_t len = strlen(line); size_t len = strlen(line);
char numstring[len-3]; char numstring[len-3];
char message[len-3]; char message[len-3];
int i;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (line[i+3] != ' ') { if (line[i+3] != ' ') {
numstring[i] = line[i+3]; numstring[i] = line[i+3];
} else { } else {
int j; int j;
for (j=i+1; j<len; j++) for (j = (i+1); j < len; j++)
message[j-i-1] = line[j+3]; message[j-i-1] = line[j+3];
break; break;
} }
@ -185,7 +183,7 @@ void line_eval(char* line)
} }
} }
else if (line[1] == 'n') { else if (inpt_command == 'n') {
uint8_t name[MAX_NAME_LENGTH]; uint8_t name[MAX_NAME_LENGTH];
int i = 0; int i = 0;
size_t len = strlen(line); size_t len = strlen(line);
@ -200,7 +198,7 @@ void line_eval(char* line)
printf(numstring); printf(numstring);
} }
else if (line[1] == 's') { else if (inpt_command == 's') {
uint8_t status[MAX_USERSTATUS_LENGTH]; uint8_t status[MAX_USERSTATUS_LENGTH];
int i = 0; int i = 0;
size_t len = strlen(line); size_t len = strlen(line);
@ -215,7 +213,7 @@ void line_eval(char* line)
printf(numstring); printf(numstring);
} }
else if (line[1] == 'a') { else if (inpt_command == 'a') {
uint8_t numf = atoi(line + 3); uint8_t numf = atoi(line + 3);
char numchar[100]; char numchar[100];
sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf); sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf);
@ -225,12 +223,10 @@ void line_eval(char* line)
printf(numchar); printf(numchar);
} }
/* EXIT */ /* EXIT */
else if (line[1] == 'q') { else if (inpt_command == 'q') {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
} } else {
else {
//nothing atm //nothing atm
} }
} }
@ -250,12 +246,10 @@ int main(int argc, char *argv[])
printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
exit(0); exit(0);
} }
if (initMessenger() == -1) { if (initMessenger() == -1) {
printf("initMessenger failed"); printf("initMessenger failed");
exit(0); exit(0);
} }
if (argc > 4) { if (argc > 4) {
if(strncmp(argv[4], "nokey", 6) < 0) { if(strncmp(argv[4], "nokey", 6) < 0) {
} }
@ -267,26 +261,25 @@ int main(int argc, char *argv[])
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 idstring1[PUB_KEY_BYTES][5];
char idstring1[32][5]; char idstring2[PUB_KEY_BYTES][5];
char idstring2[32][5]; int i;
uint32_t i; for(i = 0; i < PUB_KEY_BYTES; i++)
for(i = 0; i < 32; i++)
{ {
if(self_public_key[i] < 16) if(self_public_key[i] < (PUB_KEY_BYTES/2))
strcpy(idstring1[i],"0"); strcpy(idstring1[i],"0");
else else
strcpy(idstring1[i], ""); strcpy(idstring1[i], "");
sprintf(idstring2[i], "%hhX",self_public_key[i]); sprintf(idstring2[i], "%hhX",self_public_key[i]);
} }
strcpy(users_id,"[i] your ID: "); strcpy(users_id,"[i] your ID: ");
for (i=0; i<32; i++) { int j;
strcat(users_id,idstring1[i]); for (j = 0; j < PUB_KEY_BYTES; j++) {
strcat(users_id,idstring2[i]); strcat(users_id,idstring1[j]);
strcat(users_id,idstring2[j]);
} }
do_header(); do_header();
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]);
@ -296,12 +289,9 @@ int main(int argc, char *argv[])
exit(1); exit(1);
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
int c; int c;
int on = 0; int on = 0;
_beginthread(get_input, 0, NULL); _beginthread(get_input, 0, NULL);
while(1) { while(1) {
if (on == 1 && DHT_isconnected() == -1) { if (on == 1 && DHT_isconnected() == -1) {
printf("\n---------------------------------"); printf("\n---------------------------------");
@ -309,15 +299,12 @@ int main(int argc, char *argv[])
printf("\n---------------------------------\n\n"); printf("\n---------------------------------\n\n");
on = 0; on = 0;
} }
if (on == 0 && DHT_isconnected()) { if (on == 0 && DHT_isconnected()) {
printf("\n[i] Connected to DHT"); printf("\n[i] Connected to DHT");
printf("\n---------------------------------\n\n"); printf("\n---------------------------------\n\n");
on = 1; on = 1;
} }
doMessenger(); doMessenger();
} }
return 0; return 0;
} }

View File

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