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 "misc_tools.h"
#include <stdio.h>
#include <time.h>
#ifdef WIN32
#define c_sleep(x) Sleep(1*x)
#else
@ -54,9 +54,8 @@ void new_lines(char *line)
void print_friendlist()
{
char name[MAX_NAME_LENGTH];
uint32_t i;
new_lines("[i] Friend List:");
uint32_t i;
for (i = 0; i <= num_requests; i++) {
char fstring[128];
@ -93,10 +92,11 @@ 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++)
@ -107,20 +107,20 @@ 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];
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;
}
@ -132,7 +132,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
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);
@ -146,10 +146,10 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
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);
@ -163,7 +163,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
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);
@ -174,20 +174,18 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
do_refresh();
}
else if (line[1] == '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("[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], "");
@ -195,14 +193,15 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
}
//
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 {
@ -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)
{
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--;
@ -232,8 +231,8 @@ 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;
int i;
for (i = 0; i < len; i++) {
if (string[i] == '\n')
count++;
@ -253,14 +252,14 @@ char *appender(char *str, const char c)
void do_refresh()
{
int i;
int count=0;
int l;
char wrap_output[STRING_LENGTH];
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);
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));
}
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];
@ -311,7 +311,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];
@ -319,9 +320,11 @@ 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);
@ -368,21 +371,22 @@ 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();
@ -413,7 +417,6 @@ int main(int argc, char *argv[])
do_refresh();
c = getch();
if (c == ERR || c == 27)
continue;

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

@ -88,13 +88,12 @@ 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) {
printf("\n[i] Could not read the data file. Exiting.");
exit(1);
@ -112,15 +111,15 @@ void load_key()
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++)
@ -131,16 +130,15 @@ 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++) {
char name[MAX_NAME_LENGTH];
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);
char numstring[len-3];
int i;
@ -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];
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,7 +183,7 @@ 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);
@ -200,7 +198,7 @@ void line_eval(char* line)
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);
@ -215,7 +213,7 @@ void line_eval(char* line)
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,15 +299,12 @@ 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();
}
return 0;
}

View File

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