mirror of
https://github.com/Tha14/toxic.git
synced 2025-12-06 21:26:34 +01:00
Passed everything through astyle.
This commit is contained in:
299
main.c
299
main.c
@@ -28,7 +28,7 @@
|
||||
/* Export for use in Callbacks */
|
||||
char *DATA_FILE = NULL;
|
||||
|
||||
void on_window_resize(int sig)
|
||||
void on_window_resize(int sig)
|
||||
{
|
||||
endwin();
|
||||
refresh();
|
||||
@@ -37,44 +37,45 @@ void on_window_resize(int sig)
|
||||
|
||||
static void init_term()
|
||||
{
|
||||
/* Setup terminal */
|
||||
signal(SIGWINCH, on_window_resize);
|
||||
initscr();
|
||||
cbreak();
|
||||
keypad(stdscr, 1);
|
||||
noecho();
|
||||
timeout(100);
|
||||
/* Setup terminal */
|
||||
signal(SIGWINCH, on_window_resize);
|
||||
initscr();
|
||||
cbreak();
|
||||
keypad(stdscr, 1);
|
||||
noecho();
|
||||
timeout(100);
|
||||
|
||||
if (has_colors()) {
|
||||
start_color();
|
||||
init_pair(1, COLOR_GREEN, COLOR_BLACK);
|
||||
init_pair(2, COLOR_CYAN, COLOR_BLACK);
|
||||
init_pair(3, COLOR_RED, COLOR_BLACK);
|
||||
init_pair(4, COLOR_BLUE, COLOR_BLACK);
|
||||
init_pair(5, COLOR_YELLOW, COLOR_BLACK);
|
||||
}
|
||||
refresh();
|
||||
if (has_colors()) {
|
||||
start_color();
|
||||
init_pair(1, COLOR_GREEN, COLOR_BLACK);
|
||||
init_pair(2, COLOR_CYAN, COLOR_BLACK);
|
||||
init_pair(3, COLOR_RED, COLOR_BLACK);
|
||||
init_pair(4, COLOR_BLUE, COLOR_BLACK);
|
||||
init_pair(5, COLOR_YELLOW, COLOR_BLACK);
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
static Messenger *init_tox()
|
||||
{
|
||||
/* Init core */
|
||||
Messenger *m = initMessenger();
|
||||
/* Init core */
|
||||
Messenger *m = initMessenger();
|
||||
|
||||
/* Callbacks */
|
||||
m_callback_friendrequest(m, on_request, NULL);
|
||||
m_callback_friendmessage(m, on_message, NULL);
|
||||
m_callback_namechange(m, on_nickchange, NULL);
|
||||
m_callback_statusmessage(m, on_statuschange, NULL);
|
||||
m_callback_action(m, on_action, NULL);
|
||||
/* Callbacks */
|
||||
m_callback_friendrequest(m, on_request, NULL);
|
||||
m_callback_friendmessage(m, on_message, NULL);
|
||||
m_callback_namechange(m, on_nickchange, NULL);
|
||||
m_callback_statusmessage(m, on_statuschange, NULL);
|
||||
m_callback_action(m, on_action, NULL);
|
||||
#ifdef __linux__
|
||||
setname(m, (uint8_t*) "Cool guy", sizeof("Cool guy"));
|
||||
setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy"));
|
||||
#elif WIN32
|
||||
setname(m, (uint8_t*) "I should install GNU/Linux", sizeof("I should install GNU/Linux"));
|
||||
setname(m, (uint8_t *) "I should install GNU/Linux", sizeof("I should install GNU/Linux"));
|
||||
#else
|
||||
setname(m, (uint8_t*) "Hipster", sizeof("Hipster"));
|
||||
setname(m, (uint8_t *) "Hipster", sizeof("Hipster"));
|
||||
#endif
|
||||
return m;
|
||||
return m;
|
||||
}
|
||||
|
||||
#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
|
||||
@@ -84,67 +85,75 @@ static Messenger *init_tox()
|
||||
/* Connects to a random DHT server listed in the DHTservers file */
|
||||
int init_connection(void)
|
||||
{
|
||||
if (DHT_isconnected())
|
||||
return 0;
|
||||
if (DHT_isconnected())
|
||||
return 0;
|
||||
|
||||
FILE *fp = fopen("../../../other/DHTservers", "r");
|
||||
if (!fp)
|
||||
return 1;
|
||||
FILE *fp = fopen("../../../other/DHTservers", "r");
|
||||
|
||||
if (!fp)
|
||||
return 1;
|
||||
|
||||
char servers[MAXSERVERS][MAXLINE];
|
||||
char line[MAXLINE];
|
||||
int linecnt = 0;
|
||||
|
||||
while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) {
|
||||
if (strlen(line) > MINLINE)
|
||||
strcpy(servers[linecnt++], line);
|
||||
}
|
||||
|
||||
if (linecnt < 1) {
|
||||
fclose(fp);
|
||||
return 2;
|
||||
}
|
||||
|
||||
char servers[MAXSERVERS][MAXLINE];
|
||||
char line[MAXLINE];
|
||||
int linecnt = 0;
|
||||
while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) {
|
||||
if (strlen(line) > MINLINE)
|
||||
strcpy(servers[linecnt++], line);
|
||||
}
|
||||
if (linecnt < 1) {
|
||||
fclose(fp);
|
||||
return 2;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
char *server = servers[rand() % linecnt];
|
||||
char *ip = strtok(server, " ");
|
||||
char *port = strtok(NULL, " ");
|
||||
char *key = strtok(NULL, " ");
|
||||
if (!ip || !port || !key)
|
||||
return 3;
|
||||
char *server = servers[rand() % linecnt];
|
||||
char *ip = strtok(server, " ");
|
||||
char *port = strtok(NULL, " ");
|
||||
char *key = strtok(NULL, " ");
|
||||
|
||||
IP_Port dht;
|
||||
dht.port = htons(atoi(port));
|
||||
uint32_t resolved_address = resolve_addr(ip);
|
||||
if (resolved_address == 0)
|
||||
if (!ip || !port || !key)
|
||||
return 3;
|
||||
|
||||
IP_Port dht;
|
||||
dht.port = htons(atoi(port));
|
||||
uint32_t resolved_address = resolve_addr(ip);
|
||||
|
||||
if (resolved_address == 0)
|
||||
return 0;
|
||||
|
||||
dht.ip.i = resolved_address;
|
||||
unsigned char *binary_string = hex_string_to_bin(key);
|
||||
DHT_bootstrap(dht, binary_string);
|
||||
free(binary_string);
|
||||
return 0;
|
||||
dht.ip.i = resolved_address;
|
||||
unsigned char *binary_string = hex_string_to_bin(key);
|
||||
DHT_bootstrap(dht, binary_string);
|
||||
free(binary_string);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void do_tox(Messenger *m, ToxWindow * prompt)
|
||||
static void do_tox(Messenger *m, ToxWindow *prompt)
|
||||
{
|
||||
static int conn_try = 0;
|
||||
static int conn_err = 0;
|
||||
static bool dht_on = false;
|
||||
if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) {
|
||||
if (!conn_err) {
|
||||
conn_err = init_connection();
|
||||
wprintw(prompt->window, "\nEstablishing connection...\n");
|
||||
if (conn_err)
|
||||
wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err);
|
||||
static int conn_try = 0;
|
||||
static int conn_err = 0;
|
||||
static bool dht_on = false;
|
||||
|
||||
if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) {
|
||||
if (!conn_err) {
|
||||
conn_err = init_connection();
|
||||
wprintw(prompt->window, "\nEstablishing connection...\n");
|
||||
|
||||
if (conn_err)
|
||||
wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err);
|
||||
}
|
||||
} else if (!dht_on && DHT_isconnected()) {
|
||||
dht_on = true;
|
||||
wprintw(prompt->window, "\nDHT connected.\n");
|
||||
} else if (dht_on && !DHT_isconnected()) {
|
||||
dht_on = false;
|
||||
wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
|
||||
}
|
||||
}
|
||||
else if (!dht_on && DHT_isconnected()) {
|
||||
dht_on = true;
|
||||
wprintw(prompt->window, "\nDHT connected.\n");
|
||||
}
|
||||
else if (dht_on && !DHT_isconnected()) {
|
||||
dht_on = false;
|
||||
wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
|
||||
}
|
||||
doMessenger(m);
|
||||
|
||||
doMessenger(m);
|
||||
}
|
||||
|
||||
int f_loadfromfile;
|
||||
@@ -160,18 +169,22 @@ int store_data(Messenger *m, char *path)
|
||||
{
|
||||
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
|
||||
return 0;
|
||||
|
||||
FILE *fd;
|
||||
size_t len;
|
||||
uint8_t *buf;
|
||||
|
||||
len = Messenger_size(m);
|
||||
buf = malloc(len);
|
||||
|
||||
if (buf == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Messenger_save(m, buf);
|
||||
|
||||
fd = fopen(path, "w");
|
||||
|
||||
if (fd == NULL) {
|
||||
free(buf);
|
||||
return 2;
|
||||
@@ -192,6 +205,7 @@ static void load_data(Messenger *m, char *path)
|
||||
{
|
||||
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
|
||||
return;
|
||||
|
||||
FILE *fd;
|
||||
size_t len;
|
||||
uint8_t *buf;
|
||||
@@ -202,12 +216,14 @@ static void load_data(Messenger *m, char *path)
|
||||
fseek(fd, 0, SEEK_SET);
|
||||
|
||||
buf = malloc(len);
|
||||
|
||||
if (buf == NULL) {
|
||||
fprintf(stderr, "malloc() failed.\n");
|
||||
fclose(fd);
|
||||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (fread(buf, len, 1, fd) != 1) {
|
||||
fprintf(stderr, "fread() failed.\n");
|
||||
free(buf);
|
||||
@@ -215,9 +231,11 @@ static void load_data(Messenger *m, char *path)
|
||||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Messenger_load(m, buf, len);
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < m->numfriends; i++) {
|
||||
on_friendadded(m, i);
|
||||
}
|
||||
@@ -226,6 +244,7 @@ static void load_data(Messenger *m, char *path)
|
||||
fclose(fd);
|
||||
} else {
|
||||
int st;
|
||||
|
||||
if ((st = store_data(m, path)) != 0) {
|
||||
fprintf(stderr, "Store messenger failed with return code: %d\n", st);
|
||||
endwin();
|
||||
@@ -236,70 +255,74 @@ static void load_data(Messenger *m, char *path)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *user_config_dir = get_user_config_dir();
|
||||
int config_err = 0;
|
||||
char *user_config_dir = get_user_config_dir();
|
||||
int config_err = 0;
|
||||
|
||||
f_loadfromfile = 1;
|
||||
int f_flag = 0;
|
||||
int i = 0;
|
||||
for (i = 0; i < argc; ++i) {
|
||||
if (argv[i] == NULL)
|
||||
break;
|
||||
else if (argv[i][0] == '-') {
|
||||
if (argv[i][1] == 'f') {
|
||||
if (argv[i + 1] != NULL)
|
||||
DATA_FILE = strdup(argv[i + 1]);
|
||||
else
|
||||
f_flag = -1;
|
||||
} else if (argv[i][1] == 'n') {
|
||||
f_loadfromfile = 0;
|
||||
}
|
||||
f_loadfromfile = 1;
|
||||
int f_flag = 0;
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < argc; ++i) {
|
||||
if (argv[i] == NULL)
|
||||
break;
|
||||
else if (argv[i][0] == '-') {
|
||||
if (argv[i][1] == 'f') {
|
||||
if (argv[i + 1] != NULL)
|
||||
DATA_FILE = strdup(argv[i + 1]);
|
||||
else
|
||||
f_flag = -1;
|
||||
} else if (argv[i][1] == 'n') {
|
||||
f_loadfromfile = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (DATA_FILE == NULL ) {
|
||||
config_err = create_user_config_dir(user_config_dir);
|
||||
|
||||
if (config_err) {
|
||||
DATA_FILE = strdup("data");
|
||||
} else {
|
||||
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
|
||||
strcpy(DATA_FILE, user_config_dir);
|
||||
strcat(DATA_FILE, CONFIGDIR);
|
||||
strcat(DATA_FILE, "data");
|
||||
}
|
||||
}
|
||||
|
||||
free(user_config_dir);
|
||||
|
||||
init_term();
|
||||
Messenger *m = init_tox();
|
||||
ToxWindow *prompt = init_windows(m);
|
||||
init_window_status();
|
||||
|
||||
if (f_loadfromfile)
|
||||
load_data(m, DATA_FILE);
|
||||
|
||||
if (f_flag == -1) {
|
||||
attron(COLOR_PAIR(3) | A_BOLD);
|
||||
wprintw(prompt->window, "You passed '-f' without giving an argument.\n"
|
||||
"defaulting to 'data' for a keyfile...\n");
|
||||
attroff(COLOR_PAIR(3) | A_BOLD);
|
||||
}
|
||||
}
|
||||
|
||||
if (DATA_FILE == NULL ) {
|
||||
config_err = create_user_config_dir(user_config_dir);
|
||||
if (config_err) {
|
||||
DATA_FILE = strdup("data");
|
||||
} else {
|
||||
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
|
||||
strcpy(DATA_FILE, user_config_dir);
|
||||
strcat(DATA_FILE, CONFIGDIR);
|
||||
strcat(DATA_FILE, "data");
|
||||
attron(COLOR_PAIR(3) | A_BOLD);
|
||||
wprintw(prompt->window, "Unable to determine configuration directory.\n"
|
||||
"defaulting to 'data' for a keyfile...\n");
|
||||
attroff(COLOR_PAIR(3) | A_BOLD);
|
||||
}
|
||||
}
|
||||
free(user_config_dir);
|
||||
|
||||
init_term();
|
||||
Messenger *m = init_tox();
|
||||
ToxWindow *prompt = init_windows(m);
|
||||
init_window_status();
|
||||
while (true) {
|
||||
/* Update tox */
|
||||
do_tox(m, prompt);
|
||||
|
||||
if(f_loadfromfile)
|
||||
load_data(m, DATA_FILE);
|
||||
/* Draw */
|
||||
draw_active_window(m);
|
||||
}
|
||||
|
||||
if (f_flag == -1) {
|
||||
attron(COLOR_PAIR(3) | A_BOLD);
|
||||
wprintw(prompt->window, "You passed '-f' without giving an argument.\n"
|
||||
"defaulting to 'data' for a keyfile...\n");
|
||||
attroff(COLOR_PAIR(3) | A_BOLD);
|
||||
}
|
||||
|
||||
if(config_err) {
|
||||
attron(COLOR_PAIR(3) | A_BOLD);
|
||||
wprintw(prompt->window, "Unable to determine configuration directory.\n"
|
||||
"defaulting to 'data' for a keyfile...\n");
|
||||
attroff(COLOR_PAIR(3) | A_BOLD);
|
||||
}
|
||||
while(true) {
|
||||
/* Update tox */
|
||||
do_tox(m, prompt);
|
||||
|
||||
/* Draw */
|
||||
draw_active_window(m);
|
||||
}
|
||||
|
||||
cleanupMessenger(m);
|
||||
free(DATA_FILE);
|
||||
return 0;
|
||||
cleanupMessenger(m);
|
||||
free(DATA_FILE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user