1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 15:03:27 +01:00

Passed everything through astyle.

This commit is contained in:
irungentoo 2013-08-16 13:11:09 -04:00
parent 9f9ca0a971
commit 9dce121a76
10 changed files with 1256 additions and 1137 deletions

19
chat.c
View File

@ -92,6 +92,7 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t
{ {
ChatContext *ctx = (ChatContext *) self->x; ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time(); struct tm *timeinfo = get_time();
if (ctx->friendnum != num) if (ctx->friendnum != num)
return; return;
@ -112,6 +113,7 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1
{ {
ChatContext *ctx = (ChatContext *) self->x; ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time(); struct tm *timeinfo = get_time();
if (ctx->friendnum != num) if (ctx->friendnum != num)
return; return;
@ -161,6 +163,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
else if (key == 0x107 || key == 0x8 || key == 0x7f) { else if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (ctx->pos > 0) { if (ctx->pos > 0) {
ctx->line[--ctx->pos] = '\0'; ctx->line[--ctx->pos] = '\0';
if (x == 0) if (x == 0)
mvwdelch(self->window, y - 1, x2 - 1); mvwdelch(self->window, y - 1, x2 - 1);
else else
@ -173,6 +176,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
wclear(ctx->linewin); wclear(ctx->linewin);
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
wclrtobot(self->window); wclrtobot(self->window);
if (ctx->line[0] == '/') if (ctx->line[0] == '/')
execute(self, ctx, m, ctx->line); execute(self, ctx, m, ctx->line);
else { else {
@ -189,6 +193,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
wprintw(ctx->history, "%s: ", selfname); wprintw(ctx->history, "%s: ", selfname);
wattroff(ctx->history, COLOR_PAIR(1)); wattroff(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s\n", ctx->line); wprintw(ctx->history, "%s\n", ctx->line);
if (m_sendmessage(m, ctx->friendnum, (uint8_t *) ctx->line, strlen(ctx->line) + 1) == 0) { if (m_sendmessage(m, ctx->friendnum, (uint8_t *) ctx->line, strlen(ctx->line) + 1) == 0) {
wattron(ctx->history, COLOR_PAIR(3)); wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send message.\n"); wprintw(ctx->history, " * Failed to send message.\n");
@ -196,6 +201,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
} }
} }
} }
ctx->line[0] = '\0'; ctx->line[0] = '\0';
ctx->pos = 0; ctx->pos = 0;
} }
@ -223,10 +229,12 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
else if (!strncmp(cmd, "/me ", strlen("/me "))) { else if (!strncmp(cmd, "/me ", strlen("/me "))) {
struct tm *timeinfo = get_time(); struct tm *timeinfo = get_time();
char *action = strchr(cmd, ' '); char *action = strchr(cmd, ' ');
if (action == NULL) { if (action == NULL) {
wprintw(self->window, "Invalid syntax.\n"); wprintw(self->window, "Invalid syntax.\n");
return; return;
} }
action++; action++;
wattron(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(2));
@ -241,6 +249,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
wattron(ctx->history, COLOR_PAIR(5)); wattron(ctx->history, COLOR_PAIR(5));
wprintw(ctx->history, msg); wprintw(ctx->history, msg);
wattroff(ctx->history, COLOR_PAIR(5)); wattroff(ctx->history, COLOR_PAIR(5));
if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) { if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) {
wattron(ctx->history, COLOR_PAIR(3)); wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send action\n"); wprintw(ctx->history, " * Failed to send action\n");
@ -252,12 +261,15 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
char *status = strchr(cmd, ' '); char *status = strchr(cmd, ' ');
char *msg; char *msg;
char *status_text; char *status_text;
if (status == NULL) { if (status == NULL) {
wprintw(ctx->history, "Invalid syntax.\n"); wprintw(ctx->history, "Invalid syntax.\n");
return; return;
} }
status++; status++;
USERSTATUS status_kind; USERSTATUS status_kind;
if (!strncmp(status, "online", strlen("online"))) { if (!strncmp(status, "online", strlen("online"))) {
status_kind = USERSTATUS_NONE; status_kind = USERSTATUS_NONE;
status_text = "ONLINE"; status_text = "ONLINE";
@ -279,11 +291,11 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
} }
msg = strchr(status, ' '); msg = strchr(status, ' ');
if (msg == NULL) { if (msg == NULL) {
m_set_userstatus(m, status_kind); m_set_userstatus(m, status_kind);
wprintw(ctx->history, "Status set to: %s\n", status_text); wprintw(ctx->history, "Status set to: %s\n", status_text);
} } else {
else {
msg++; msg++;
m_set_userstatus(m, status_kind); m_set_userstatus(m, status_kind);
m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
@ -294,6 +306,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
char *nick; char *nick;
nick = strchr(cmd, ' '); nick = strchr(cmd, ' ');
if (nick == NULL) { if (nick == NULL) {
wprintw(ctx->history, "Invalid syntax.\n"); wprintw(ctx->history, "Invalid syntax.\n");
return; return;
@ -309,11 +322,13 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
int i; int i;
uint8_t address[FRIEND_ADDRESS_SIZE]; uint8_t address[FRIEND_ADDRESS_SIZE];
getaddress(m, address); getaddress(m, address);
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
char xx[3]; char xx[3];
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx); strcat(id, xx);
} }
wprintw(ctx->history, "%s\n", id); wprintw(ctx->history, "%s\n", id);
} }

View File

@ -50,6 +50,7 @@ char *get_user_config_dir(void)
BOOL ok; BOOL ok;
ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE); ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE);
if (!ok) { if (!ok) {
return NULL; return NULL;
} }
@ -72,13 +73,16 @@ char *get_user_config_dir(void)
int rc; int rc;
rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf); rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
if (rc == 0) { if (rc == 0) {
home = pwd.pw_dir; home = pwd.pw_dir;
} else { } else {
home = getenv("HOME"); home = getenv("HOME");
if (home == NULL) { if (home == NULL) {
return NULL; return NULL;
} }
/* env variables can be tainted */ /* env variables can be tainted */
snprintf(buf, sizeof(buf), "%s", home); snprintf(buf, sizeof(buf), "%s", home);
home = buf; home = buf;
@ -87,6 +91,7 @@ char *get_user_config_dir(void)
# if defined(__APPLE__) # if defined(__APPLE__)
len = strlen(home) + strlen("/Library/Application Support") + 1; len = strlen(home) + strlen("/Library/Application Support") + 1;
user_config_dir = malloc(len); user_config_dir = malloc(len);
if (user_config_dir == NULL) { if (user_config_dir == NULL) {
return NULL; return NULL;
} }
@ -95,6 +100,7 @@ char *get_user_config_dir(void)
# else /* __APPLE__ */ # else /* __APPLE__ */
len = strlen(home) + strlen("/.config") + 1; len = strlen(home) + strlen("/.config") + 1;
user_config_dir = malloc(len); user_config_dir = malloc(len);
if (user_config_dir == NULL) { if (user_config_dir == NULL) {
return NULL; return NULL;
} }
@ -123,6 +129,7 @@ int create_user_config_dir(char *path)
mkdir_err = _mkdir(fullpath); mkdir_err = _mkdir(fullpath);
struct __stat64 buf; struct __stat64 buf;
if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
free(fullpath); free(fullpath);
return -1; return -1;

View File

@ -18,6 +18,7 @@ static void dhtstatus_onKey(ToxWindow *self, Messenger *m, int key)
case 'k': case 'k':
if (--num_selected < 0) if (--num_selected < 0)
num_selected = CLIENT_ID_SIZE - 1; num_selected = CLIENT_ID_SIZE - 1;
break; break;
case KEY_DOWN: case KEY_DOWN:
@ -42,12 +43,18 @@ static void dhtstatus_onDraw(ToxWindow *self)
uint64_t now = unix_time(); uint64_t now = unix_time();
uint32_t i, j; uint32_t i, j;
ipbuf ipbuf; ipbuf ipbuf;
wprintw(self->window,"\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n", now); wprintw(self->window,
"\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n",
now);
for (i = 0; i < 32; i++) { /*Number of nodes in closelist*/ for (i = 0; i < 32; i++) { /*Number of nodes in closelist*/
Client_data *client = close_clientlist + i; Client_data *client = close_clientlist + i;
if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
wprintw(self->window, "[%02i] ", i); wprintw(self->window, "[%02i] ", i);
uint16_t port = ntohs(client->ip_port.port); uint16_t port = ntohs(client->ip_port.port);
if (port) { if (port) {
for (j = 0; j < CLIENT_ID_SIZE; j++) for (j = 0; j < CLIENT_ID_SIZE; j++)
wprintw(self->window, "%02hhx", client->client_id[j]); wprintw(self->window, "%02hhx", client->client_id[j]);
@ -58,12 +65,15 @@ static void dhtstatus_onDraw(ToxWindow *self)
wprintw(self->window, " %3llu ", now - client->last_pinged); wprintw(self->window, " %3llu ", now - client->last_pinged);
port = ntohs(client->ret_ip_port.port); port = ntohs(client->ret_ip_port.port);
if (port) { if (port) {
printip(ipbuf, client->ret_ip_port.ip); printip(ipbuf, client->ret_ip_port.ip);
wprintw(self->window, " %15s %5u %3llu", ipbuf, port, now - close_clientlist[i].ret_timestamp); wprintw(self->window, " %15s %5u %3llu", ipbuf, port, now - close_clientlist[i].ret_timestamp);
} }
} }
wprintw(self->window, "\n"); wprintw(self->window, "\n");
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3)); if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
} }

View File

@ -31,11 +31,14 @@ void fix_name(uint8_t *name)
/* Remove all non alphanumeric characters */ /* Remove all non alphanumeric characters */
uint8_t *p = name; uint8_t *p = name;
uint8_t *q = name; uint8_t *q = name;
while (*p != 0) { while (*p != 0) {
if (isprint(*p)) if (isprint(*p))
*q++ = *p; *q++ = *p;
p++; p++;
} }
*q = 0; *q = 0;
} }
@ -47,6 +50,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str,
if (friends[num].chatwin == -1) { if (friends[num].chatwin == -1) {
friends[num].chatwin = num; friends[num].chatwin = num;
int i; int i;
/* Find first open slot to hold chat window */ /* Find first open slot to hold chat window */
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == -1) { if (WINDOW_STATUS[i] == -1) {
@ -96,15 +100,14 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
if (key == KEY_UP) { if (key == KEY_UP) {
if (--num_selected < 0) if (--num_selected < 0)
num_selected = num_friends - 1; num_selected = num_friends - 1;
} } else if (key == KEY_DOWN) {
else if (key == KEY_DOWN) {
if (num_friends != 0) if (num_friends != 0)
num_selected = (num_selected + 1) % num_friends; num_selected = (num_selected + 1) % num_friends;
} } else if (key == '\n') {
else if (key == '\n') {
/* Jump to chat window if already open */ /* Jump to chat window if already open */
if (friends[num_selected].chatwin != -1) { if (friends[num_selected].chatwin != -1) {
int i; int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == num_selected) { if (WINDOW_STATUS[i] == num_selected) {
set_active_window(i); set_active_window(i);
@ -113,6 +116,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
} }
} else { } else {
int i; int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == -1) { if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num_selected; WINDOW_STATUS[i] = num_selected;
@ -129,10 +133,10 @@ static void friendlist_onDraw(ToxWindow *self)
{ {
curs_set(0); curs_set(0);
werase(self->window); werase(self->window);
if (num_friends == 0) { if (num_friends == 0) {
wprintw(self->window, "Empty. Add some friends! :-)\n"); wprintw(self->window, "Empty. Add some friends! :-)\n");
} } else {
else {
wattron(self->window, COLOR_PAIR(2) | A_BOLD); wattron(self->window, COLOR_PAIR(2) | A_BOLD);
wprintw(self->window, "Open chat with.. (up/down keys, enter)\n"); wprintw(self->window, "Open chat with.. (up/down keys, enter)\n");
wattroff(self->window, COLOR_PAIR(2) | A_BOLD); wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
@ -140,9 +144,12 @@ static void friendlist_onDraw(ToxWindow *self)
wprintw(self->window, "\n"); wprintw(self->window, "\n");
int i; int i;
for (i = 0; i < num_friends; ++i) { for (i = 0; i < num_friends; ++i) {
if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
wprintw(self->window, " [#%d] ", friends[i].num); wprintw(self->window, " [#%d] ", friends[i].num);
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3)); if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
attron(A_BOLD); attron(A_BOLD);
@ -151,6 +158,7 @@ static void friendlist_onDraw(ToxWindow *self)
wprintw(self->window, "(%s)\n", friends[i].status); wprintw(self->window, "(%s)\n", friends[i].status);
} }
wrefresh(self->window); wrefresh(self->window);
} }
@ -164,7 +172,8 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m)
} }
ToxWindow new_friendlist(char * ws) { ToxWindow new_friendlist(char *ws)
{
WINDOW_STATUS = ws; WINDOW_STATUS = ws;
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));

31
main.c
View File

@ -53,6 +53,7 @@ static void init_term()
init_pair(4, COLOR_BLUE, COLOR_BLACK); init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_YELLOW, COLOR_BLACK); init_pair(5, COLOR_YELLOW, COLOR_BLACK);
} }
refresh(); refresh();
} }
@ -88,34 +89,41 @@ int init_connection(void)
return 0; return 0;
FILE *fp = fopen("../../../other/DHTservers", "r"); FILE *fp = fopen("../../../other/DHTservers", "r");
if (!fp) if (!fp)
return 1; return 1;
char servers[MAXSERVERS][MAXLINE]; char servers[MAXSERVERS][MAXLINE];
char line[MAXLINE]; char line[MAXLINE];
int linecnt = 0; int linecnt = 0;
while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) {
if (strlen(line) > MINLINE) if (strlen(line) > MINLINE)
strcpy(servers[linecnt++], line); strcpy(servers[linecnt++], line);
} }
if (linecnt < 1) { if (linecnt < 1) {
fclose(fp); fclose(fp);
return 2; return 2;
} }
fclose(fp); fclose(fp);
char *server = servers[rand() % linecnt]; char *server = servers[rand() % linecnt];
char *ip = strtok(server, " "); char *ip = strtok(server, " ");
char *port = strtok(NULL, " "); char *port = strtok(NULL, " ");
char *key = strtok(NULL, " "); char *key = strtok(NULL, " ");
if (!ip || !port || !key) if (!ip || !port || !key)
return 3; return 3;
IP_Port dht; IP_Port dht;
dht.port = htons(atoi(port)); dht.port = htons(atoi(port));
uint32_t resolved_address = resolve_addr(ip); uint32_t resolved_address = resolve_addr(ip);
if (resolved_address == 0) if (resolved_address == 0)
return 0; return 0;
dht.ip.i = resolved_address; dht.ip.i = resolved_address;
unsigned char *binary_string = hex_string_to_bin(key); unsigned char *binary_string = hex_string_to_bin(key);
DHT_bootstrap(dht, binary_string); DHT_bootstrap(dht, binary_string);
@ -128,22 +136,23 @@ static void do_tox(Messenger *m, ToxWindow * prompt)
static int conn_try = 0; static int conn_try = 0;
static int conn_err = 0; static int conn_err = 0;
static bool dht_on = false; static bool dht_on = false;
if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) { if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) {
if (!conn_err) { if (!conn_err) {
conn_err = init_connection(); conn_err = init_connection();
wprintw(prompt->window, "\nEstablishing connection...\n"); wprintw(prompt->window, "\nEstablishing connection...\n");
if (conn_err) if (conn_err)
wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err); wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err);
} }
} } else if (!dht_on && DHT_isconnected()) {
else if (!dht_on && DHT_isconnected()) {
dht_on = true; dht_on = true;
wprintw(prompt->window, "\nDHT connected.\n"); wprintw(prompt->window, "\nDHT connected.\n");
} } else if (dht_on && !DHT_isconnected()) {
else if (dht_on && !DHT_isconnected()) {
dht_on = false; dht_on = false;
wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
} }
doMessenger(m); doMessenger(m);
} }
@ -160,18 +169,22 @@ int store_data(Messenger *m, char *path)
{ {
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
return 0; return 0;
FILE *fd; FILE *fd;
size_t len; size_t len;
uint8_t *buf; uint8_t *buf;
len = Messenger_size(m); len = Messenger_size(m);
buf = malloc(len); buf = malloc(len);
if (buf == NULL) { if (buf == NULL) {
return 1; return 1;
} }
Messenger_save(m, buf); Messenger_save(m, buf);
fd = fopen(path, "w"); fd = fopen(path, "w");
if (fd == NULL) { if (fd == NULL) {
free(buf); free(buf);
return 2; return 2;
@ -192,6 +205,7 @@ static void load_data(Messenger *m, char *path)
{ {
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
return; return;
FILE *fd; FILE *fd;
size_t len; size_t len;
uint8_t *buf; uint8_t *buf;
@ -202,12 +216,14 @@ static void load_data(Messenger *m, char *path)
fseek(fd, 0, SEEK_SET); fseek(fd, 0, SEEK_SET);
buf = malloc(len); buf = malloc(len);
if (buf == NULL) { if (buf == NULL) {
fprintf(stderr, "malloc() failed.\n"); fprintf(stderr, "malloc() failed.\n");
fclose(fd); fclose(fd);
endwin(); endwin();
exit(1); exit(1);
} }
if (fread(buf, len, 1, fd) != 1) { if (fread(buf, len, 1, fd) != 1) {
fprintf(stderr, "fread() failed.\n"); fprintf(stderr, "fread() failed.\n");
free(buf); free(buf);
@ -215,9 +231,11 @@ static void load_data(Messenger *m, char *path)
endwin(); endwin();
exit(1); exit(1);
} }
Messenger_load(m, buf, len); Messenger_load(m, buf, len);
uint32_t i; uint32_t i;
for (i = 0; i < m->numfriends; i++) { for (i = 0; i < m->numfriends; i++) {
on_friendadded(m, i); on_friendadded(m, i);
} }
@ -226,6 +244,7 @@ static void load_data(Messenger *m, char *path)
fclose(fd); fclose(fd);
} else { } else {
int st; int st;
if ((st = store_data(m, path)) != 0) { if ((st = store_data(m, path)) != 0) {
fprintf(stderr, "Store messenger failed with return code: %d\n", st); fprintf(stderr, "Store messenger failed with return code: %d\n", st);
endwin(); endwin();
@ -242,6 +261,7 @@ int main(int argc, char *argv[])
f_loadfromfile = 1; f_loadfromfile = 1;
int f_flag = 0; int f_flag = 0;
int i = 0; int i = 0;
for (i = 0; i < argc; ++i) { for (i = 0; i < argc; ++i) {
if (argv[i] == NULL) if (argv[i] == NULL)
break; break;
@ -259,6 +279,7 @@ int main(int argc, char *argv[])
if (DATA_FILE == NULL ) { if (DATA_FILE == NULL ) {
config_err = create_user_config_dir(user_config_dir); config_err = create_user_config_dir(user_config_dir);
if (config_err) { if (config_err) {
DATA_FILE = strdup("data"); DATA_FILE = strdup("data");
} else { } else {
@ -268,6 +289,7 @@ int main(int argc, char *argv[])
strcat(DATA_FILE, "data"); strcat(DATA_FILE, "data");
} }
} }
free(user_config_dir); free(user_config_dir);
init_term(); init_term();
@ -291,6 +313,7 @@ int main(int argc, char *argv[])
"defaulting to 'data' for a keyfile...\n"); "defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD); attroff(COLOR_PAIR(3) | A_BOLD);
} }
while (true) { while (true) {
/* Update tox */ /* Update tox */
do_tox(m, prompt); do_tox(m, prompt);

View File

@ -70,20 +70,24 @@ unsigned char *hex_string_to_bin(char hex_string[])
unsigned char *val = malloc(len); unsigned char *val = malloc(len);
char *pos = hex_string; char *pos = hex_string;
int i; int i;
for (i = 0; i < len; ++i, pos += 2) for (i = 0; i < len; ++i, pos += 2)
sscanf(pos, "%2hhx", &val[i]); sscanf(pos, "%2hhx", &val[i]);
return val; return val;
} }
void cmd_accept(ToxWindow *self, Messenger *m, char **args) void cmd_accept(ToxWindow *self, Messenger *m, char **args)
{ {
int num = atoi(args[1]); int num = atoi(args[1]);
if (num >= num_requests) { if (num >= num_requests) {
wprintw(self->window, "Invalid syntax.\n"); wprintw(self->window, "Invalid syntax.\n");
return; return;
} }
num = m_addfriend_norequest(m, pending_requests[num]); num = m_addfriend_norequest(m, pending_requests[num]);
if (num == -1) if (num == -1)
wprintw(self->window, "Failed to add friend.\n"); wprintw(self->window, "Failed to add friend.\n");
else { else {
@ -104,6 +108,7 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
wprintw(self->window, "Invalid command: add expected at least one argument.\n"); wprintw(self->window, "Invalid command: add expected at least one argument.\n");
return; return;
} }
if (!msg) if (!msg)
msg = ""; msg = "";
@ -111,15 +116,19 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
wprintw(self->window, "Invalid ID length.\n"); wprintw(self->window, "Invalid ID length.\n");
return; return;
} }
int i; int i;
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) { for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
xx[0] = id[2 * i]; xx[0] = id[2 * i];
xx[1] = id[2 * i + 1]; xx[1] = id[2 * i + 1];
xx[2] = '\0'; xx[2] = '\0';
if (sscanf(xx, "%02x", &x) != 1) { if (sscanf(xx, "%02x", &x) != 1) {
wprintw(self->window, "Invalid ID.\n"); wprintw(self->window, "Invalid ID.\n");
return; return;
} }
id_bin[i] = x; id_bin[i] = x;
} }
@ -128,28 +137,36 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
} }
int num = m_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1); int num = m_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1);
switch (num) { switch (num) {
case FAERR_TOOLONG: case FAERR_TOOLONG:
wprintw(self->window, "Message is too long.\n"); wprintw(self->window, "Message is too long.\n");
break; break;
case FAERR_NOMESSAGE: case FAERR_NOMESSAGE:
wprintw(self->window, "Please add a message to your request.\n"); wprintw(self->window, "Please add a message to your request.\n");
break; break;
case FAERR_OWNKEY: case FAERR_OWNKEY:
wprintw(self->window, "That appears to be your own ID.\n"); wprintw(self->window, "That appears to be your own ID.\n");
break; break;
case FAERR_ALREADYSENT: case FAERR_ALREADYSENT:
wprintw(self->window, "Friend request already sent.\n"); wprintw(self->window, "Friend request already sent.\n");
break; break;
case FAERR_UNKNOWN: case FAERR_UNKNOWN:
wprintw(self->window, "Undefined error when adding friend.\n"); wprintw(self->window, "Undefined error when adding friend.\n");
break; break;
case FAERR_BADCHECKSUM: case FAERR_BADCHECKSUM:
wprintw(self->window, "Bad checksum in address.\n"); wprintw(self->window, "Bad checksum in address.\n");
break; break;
case FAERR_SETNEWNOSPAM: case FAERR_SETNEWNOSPAM:
wprintw(self->window, "Nospam was different.\n"); wprintw(self->window, "Nospam was different.\n");
break; break;
default: default:
wprintw(self->window, "Friend added as %d.\n", num); wprintw(self->window, "Friend added as %d.\n", num);
on_friendadded_cb(m, num); on_friendadded_cb(m, num);
@ -176,6 +193,7 @@ void cmd_connect(ToxWindow *self, Messenger *m, char **args)
dht.port = htons(atoi(port)); dht.port = htons(atoi(port));
uint32_t resolved_address = resolve_addr(ip); uint32_t resolved_address = resolve_addr(ip);
if (resolved_address == 0) { if (resolved_address == 0) {
return; return;
} }
@ -221,6 +239,7 @@ void cmd_msg(ToxWindow *self, Messenger *m, char **args)
{ {
char *id = args[1]; char *id = args[1];
char *msg = args[2]; char *msg = args[2];
if (m_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0) if (m_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0)
wprintw(self->window, "Error occurred while sending message.\n"); wprintw(self->window, "Error occurred while sending message.\n");
else else
@ -233,11 +252,13 @@ void cmd_myid(ToxWindow *self, Messenger *m, char **args)
size_t i; size_t i;
uint8_t address[FRIEND_ADDRESS_SIZE]; uint8_t address[FRIEND_ADDRESS_SIZE];
getaddress(m, address); getaddress(m, address);
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) { for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
char xx[3]; char xx[3];
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx); strcat(id, xx);
} }
wprintw(self->window, "%s\n", id); wprintw(self->window, "%s\n", id);
} }
@ -254,30 +275,27 @@ void cmd_status(ToxWindow *self, Messenger *m, char **args)
char *status_text; char *status_text;
USERSTATUS status_kind; USERSTATUS status_kind;
if (!strncmp(status, "online", strlen("online"))) { if (!strncmp(status, "online", strlen("online"))) {
status_kind = USERSTATUS_NONE; status_kind = USERSTATUS_NONE;
status_text = "ONLINE"; status_text = "ONLINE";
} } else if (!strncmp(status, "away", strlen("away"))) {
else if (!strncmp(status, "away", strlen("away"))) {
status_kind = USERSTATUS_AWAY; status_kind = USERSTATUS_AWAY;
status_text = "AWAY"; status_text = "AWAY";
} } else if (!strncmp(status, "busy", strlen("busy"))) {
else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = USERSTATUS_BUSY; status_kind = USERSTATUS_BUSY;
status_text = "BUSY"; status_text = "BUSY";
} } else {
else
{
wprintw(self->window, "Invalid status.\n"); wprintw(self->window, "Invalid status.\n");
return; return;
} }
char *msg = args[2]; char *msg = args[2];
if (msg == NULL) { if (msg == NULL) {
m_set_userstatus(m, status_kind); m_set_userstatus(m, status_kind);
wprintw(self->window, "Status set to: %s\n", status_text); wprintw(self->window, "Status set to: %s\n", status_text);
} } else {
else {
m_set_userstatus(m, status_kind); m_set_userstatus(m, status_kind);
m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
wprintw(self->window, "Status set to: %s, %s\n", status_text, msg); wprintw(self->window, "Status set to: %s, %s\n", status_text, msg);
@ -296,6 +314,7 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
int newlines = 0; int newlines = 0;
char cmd[MAX_STR_SIZE] = {0}; char cmd[MAX_STR_SIZE] = {0};
int i; int i;
for (i = 0; i < strlen(prompt_buf); ++i) { for (i = 0; i < strlen(prompt_buf); ++i) {
if (u_cmd[i] == '\n') if (u_cmd[i] == '\n')
++newlines; ++newlines;
@ -304,21 +323,27 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
} }
int leading_spc = 0; int leading_spc = 0;
for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i) for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i)
leading_spc++; leading_spc++;
memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc); memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc);
int cmd_end = strlen(cmd); int cmd_end = strlen(cmd);
while (cmd_end > 0 && cmd_end--) while (cmd_end > 0 && cmd_end--)
if (!isspace(cmd[cmd_end])) if (!isspace(cmd[cmd_end]))
break; break;
cmd[cmd_end + 1] = '\0'; cmd[cmd_end + 1] = '\0';
/* insert \0 at argument boundaries */ /* insert \0 at argument boundaries */
int numargs = 0; int numargs = 0;
for (i = 0; i < MAX_STR_SIZE; i++) { for (i = 0; i < MAX_STR_SIZE; i++) {
if (cmd[i] == '\"') if (cmd[i] == '\"')
while (cmd[++i] != '\"'); /* skip over strings */ while (cmd[++i] != '\"'); /* skip over strings */
if (cmd[i] == ' ') { if (cmd[i] == ' ') {
cmd[i] = '\0'; cmd[i] = '\0';
numargs++; numargs++;
@ -334,6 +359,7 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
/* read arguments into array */ /* read arguments into array */
char *cmdargs[5]; char *cmdargs[5];
int pos = 0; int pos = 0;
for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) {
cmdargs[i] = cmd + pos; cmdargs[i] = cmd + pos;
pos += strlen(cmdargs[i]) + 1; pos += strlen(cmdargs[i]) + 1;
@ -348,6 +374,7 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
if (!strcmp(cmdargs[0], commands[i].name)) { if (!strcmp(cmdargs[0], commands[i].name)) {
/* check for missing arguments */ /* check for missing arguments */
int j; int j;
for (j = 0; j <= commands[i].numargs; j++) { for (j = 0; j <= commands[i].numargs; j++) {
if (strlen(cmdargs[j]) == 0) { if (strlen(cmdargs[j]) == 0) {
wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n", wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n",
@ -355,11 +382,13 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
return; return;
} }
} }
/* check for excess arguments */ /* check for excess arguments */
if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) { if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) {
wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name); wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name);
return; return;
} }
/* pass arguments to command function */ /* pass arguments to command function */
(commands[i].func)(self, m, cmdargs); (commands[i].func)(self, m, cmdargs);
return; return;
@ -378,15 +407,14 @@ static void prompt_onKey(ToxWindow *self, Messenger *m, int key)
wprintw(self->window, "\nToo Long.\n"); wprintw(self->window, "\nToo Long.\n");
prompt_buf_pos = 0; prompt_buf_pos = 0;
prompt_buf[0] = 0; prompt_buf[0] = 0;
} } else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS)
else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS)
&& (prompt_buf_pos % (COLS - 3) == 0)) { && (prompt_buf_pos % (COLS - 3) == 0)) {
prompt_buf[prompt_buf_pos++] = '\n'; prompt_buf[prompt_buf_pos++] = '\n';
} } else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
&& ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) { && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
prompt_buf[prompt_buf_pos++] = '\n'; prompt_buf[prompt_buf_pos++] = '\n';
} }
prompt_buf[prompt_buf_pos++] = key; prompt_buf[prompt_buf_pos++] = key;
prompt_buf[prompt_buf_pos] = 0; prompt_buf[prompt_buf_pos] = 0;
} }
@ -414,6 +442,7 @@ static void prompt_onDraw(ToxWindow *self)
getyx(self->window, y, x); getyx(self->window, y, x);
(void) x; (void) x;
int i; int i;
for (i = 0; i < (strlen(prompt_buf)); ++i) { for (i = 0; i < (strlen(prompt_buf)); ++i) {
if ((prompt_buf[i] == '\n') && (y != 0)) if ((prompt_buf[i] == '\n') && (y != 0))
--y; --y;

View File

@ -23,6 +23,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userd
wprintw(prompt->window, "\nFriend request from:\n"); wprintw(prompt->window, "\nFriend request from:\n");
int i; int i;
for (i = 0; i < KEY_SIZE_BYTES; ++i) { for (i = 0; i < KEY_SIZE_BYTES; ++i) {
wprintw(prompt->window, "%02x", public_key[i] & 0xff); wprintw(prompt->window, "%02x", public_key[i] & 0xff);
} }
@ -39,6 +40,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userd
void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{ {
int i; int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onMessage != NULL) if (windows[i].onMessage != NULL)
windows[i].onMessage(&windows[i], m, friendnumber, string, length); windows[i].onMessage(&windows[i], m, friendnumber, string, length);
@ -48,6 +50,7 @@ void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length
void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{ {
int i; int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onAction != NULL) if (windows[i].onAction != NULL)
windows[i].onAction(&windows[i], m, friendnumber, string, length); windows[i].onAction(&windows[i], m, friendnumber, string, length);
@ -58,6 +61,7 @@ void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t len
{ {
wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
int i; int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onNickChange != NULL) if (windows[i].onNickChange != NULL)
windows[i].onNickChange(&windows[i], friendnumber, string, length); windows[i].onNickChange(&windows[i], friendnumber, string, length);
@ -68,6 +72,7 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l
{ {
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
int i; int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onStatusChange != NULL) if (windows[i].onStatusChange != NULL)
windows[i].onStatusChange(&windows[i], friendnumber, string, length); windows[i].onStatusChange(&windows[i], friendnumber, string, length);
@ -77,6 +82,7 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l
void on_friendadded(Messenger *m, int friendnumber) void on_friendadded(Messenger *m, int friendnumber)
{ {
friendlist_onFriendAdded(m, friendnumber); friendlist_onFriendAdded(m, friendnumber);
if (store_data(m, DATA_FILE) != 0) { if (store_data(m, DATA_FILE) != 0) {
wprintw(prompt->window, "\nCould not store Messenger data\n"); wprintw(prompt->window, "\nCould not store Messenger data\n");
} }
@ -92,6 +98,7 @@ int add_window(Messenger *m, ToxWindow w, int n)
return -1; return -1;
w.window = newwin(LINES - 2, COLS, 0, 0); w.window = newwin(LINES - 2, COLS, 0, 0);
if (w.window == NULL) if (w.window == NULL)
return -1; return -1;
@ -108,6 +115,7 @@ void del_window(ToxWindow *w, int f_num)
active_window = 0; // Go to prompt screen active_window = 0; // Go to prompt screen
delwin(w->window); delwin(w->window);
int i; int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == f_num) { if (WINDOW_STATUS[i] == f_num) {
WINDOW_STATUS[i] = -1; WINDOW_STATUS[i] = -1;
@ -115,6 +123,7 @@ void del_window(ToxWindow *w, int f_num)
break; break;
} }
} }
clear(); clear();
refresh(); refresh();
} }
@ -124,14 +133,18 @@ void set_active_window(int ch)
{ {
int f_inf = 0; int f_inf = 0;
int max = MAX_WINDOW_SLOTS - 1; int max = MAX_WINDOW_SLOTS - 1;
if (ch == '\t') { if (ch == '\t') {
int i = (active_window + 1) % max; int i = (active_window + 1) % max;
while (true) { while (true) {
if (WINDOW_STATUS[i] != -1) { if (WINDOW_STATUS[i] != -1) {
active_window = i; active_window = i;
return; return;
} }
i = (i + 1) % max; i = (i + 1) % max;
if (f_inf++ > max) { // infinite loop check if (f_inf++ > max) { // infinite loop check
endwin(); endwin();
exit(2); exit(2);
@ -139,13 +152,17 @@ void set_active_window(int ch)
} }
} else { } else {
int i = active_window - 1; int i = active_window - 1;
if (i < 0) i = max; if (i < 0) i = max;
while (true) { while (true) {
if (WINDOW_STATUS[i] != -1) { if (WINDOW_STATUS[i] != -1) {
active_window = i; active_window = i;
return; return;
} }
if (--i < 0) i = max; if (--i < 0) i = max;
if (f_inf++ > max) { if (f_inf++ > max) {
endwin(); endwin();
exit(2); exit(2);
@ -158,10 +175,12 @@ void init_window_status()
{ {
/* Default window values decrement from -2 */ /* Default window values decrement from -2 */
int i; int i;
for (i = 0; i < N_DEFAULT_WINS; ++i) for (i = 0; i < N_DEFAULT_WINS; ++i)
WINDOW_STATUS[i] = -(i + 2); WINDOW_STATUS[i] = -(i + 2);
int j; int j;
for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++) for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
WINDOW_STATUS[j] = -1; WINDOW_STATUS[j] = -1;
} }
@ -172,6 +191,7 @@ ToxWindow *init_windows()
int n_prompt = 0; int n_prompt = 0;
int n_friendslist = 1; int n_friendslist = 1;
int n_dhtstatus = 2; int n_dhtstatus = 2;
if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1 if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1
|| add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1 || add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1
|| add_window(m, new_dhtstatus(), n_dhtstatus) == -1) { || add_window(m, new_dhtstatus(), n_dhtstatus) == -1) {
@ -179,6 +199,7 @@ ToxWindow *init_windows()
endwin(); endwin();
exit(1); exit(1);
} }
active_window = n_prompt; active_window = n_prompt;
prompt = &windows[n_prompt]; prompt = &windows[n_prompt];
return prompt; return prompt;
@ -200,16 +221,19 @@ static void draw_bar()
attroff(COLOR_PAIR(4) | A_BOLD); attroff(COLOR_PAIR(4) | A_BOLD);
int i; int i;
for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) { for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
if (WINDOW_STATUS[i] != -1) { if (WINDOW_STATUS[i] != -1) {
if (i == active_window) if (i == active_window)
attron(A_BOLD); attron(A_BOLD);
odd = (odd + 1) % blinkrate; odd = (odd + 1) % blinkrate;
if (windows[i].blink && (odd < (blinkrate / 2))) if (windows[i].blink && (odd < (blinkrate / 2)))
attron(COLOR_PAIR(3)); attron(COLOR_PAIR(3));
printw(" %s", windows[i].title); printw(" %s", windows[i].title);
if (windows[i].blink && (odd < (blinkrate / 2))) if (windows[i].blink && (odd < (blinkrate / 2)))
attroff(COLOR_PAIR(3)); attroff(COLOR_PAIR(3));
@ -218,6 +242,7 @@ static void draw_bar()
} }
} }
} }
refresh(); refresh();
} }
@ -238,6 +263,7 @@ void draw_active_window(Messenger *m)
/* Handle input */ /* Handle input */
int ch = getch(); int ch = getch();
if (ch == '\t' || ch == KEY_BTAB) if (ch == '\t' || ch == KEY_BTAB)
set_active_window(ch); set_active_window(ch);
else if (ch != ERR) else if (ch != ERR)