1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-03 17:57:45 +02: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

631
chat.c
View File

@ -19,376 +19,391 @@
#define CURS_Y_OFFSET 3 #define CURS_Y_OFFSET 3
typedef struct { typedef struct {
int friendnum; int friendnum;
char line[MAX_STR_SIZE]; char line[MAX_STR_SIZE];
size_t pos; size_t pos;
WINDOW* history; WINDOW *history;
WINDOW* linewin; WINDOW *linewin;
} ChatContext; } ChatContext;
void print_help(ChatContext *self); void print_help(ChatContext *self);
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd); void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd);
struct tm *get_time(void) struct tm *get_time(void)
{ {
struct tm *timeinfo; struct tm *timeinfo;
time_t now; time_t now;
time(&now); time(&now);
timeinfo = localtime(&now); timeinfo = localtime(&now);
return timeinfo; return timeinfo;
} }
static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len) static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len)
{ {
ChatContext *ctx = (ChatContext*) self->x; ChatContext *ctx = (ChatContext *) self->x;
uint8_t nick[MAX_NAME_LENGTH] = {0}; uint8_t nick[MAX_NAME_LENGTH] = {0};
struct tm *timeinfo = get_time(); struct tm *timeinfo = get_time();
if (ctx->friendnum != num) if (ctx->friendnum != num)
return; return;
getname(m, num, (uint8_t*) &nick); getname(m, num, (uint8_t *) &nick);
msg[len-1] = '\0'; msg[len - 1] = '\0';
nick[MAX_NAME_LENGTH-1] = '\0'; nick[MAX_NAME_LENGTH - 1] = '\0';
fix_name(msg); fix_name(msg);
fix_name(nick); fix_name(nick);
wattron(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(4)); wattron(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s: ", nick); wprintw(ctx->history, "%s: ", nick);
wattroff(ctx->history, COLOR_PAIR(4)); wattroff(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s\n", msg); wprintw(ctx->history, "%s\n", msg);
self->blink = true; self->blink = true;
beep(); beep();
} }
static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len) static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len)
{ {
ChatContext *ctx = (ChatContext*) self->x; ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
action[len-1] = '\0';
fix_name(action);
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(5));
wprintw(ctx->history, "%s\n", action);
wattroff(ctx->history, COLOR_PAIR(5));
self->blink = true;
beep();
}
static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
{
ChatContext *ctx = (ChatContext*) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
nick[len-1] = '\0';
fix_name(nick);
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick);
wattroff(ctx->history, COLOR_PAIR(3));
}
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
{
ChatContext *ctx = (ChatContext*) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
status[len-1] = '\0';
fix_name(status);
snprintf(self->title, sizeof(self->title), "[%s (%d)]", status, num);
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, "* Your partner changed status to '%s'\n", status);
wattroff(ctx->history, COLOR_PAIR(3));
}
/* check that the string has one non-space character */
int string_is_empty(char *string)
{
int rc = 0;
char *copy = strdup(string);
rc = ((strtok(copy, " ") == NULL) ? 1:0);
free(copy);
return rc;
}
static void chat_onKey(ToxWindow *self, Messenger *m, int key)
{
ChatContext *ctx = (ChatContext*) self->x;
struct tm *timeinfo = get_time();
int x, y, y2, x2;
getyx(self->window, y, x);
getmaxyx(self->window, y2, x2);
/* Add printable chars to buffer and print on input space */
if (isprint(key)) {
if (ctx->pos != sizeof(ctx->line)-1) {
mvwaddch(self->window, y, x, key);
ctx->line[ctx->pos++] = key;
ctx->line[ctx->pos] = '\0';
}
}
/* BACKSPACE key: Remove one character from line */
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (ctx->pos > 0) {
ctx->line[--ctx->pos] = '\0';
if (x == 0)
mvwdelch(self->window, y-1, x2-1);
else
mvwdelch(self->window, y, x-1);
}
}
/* RETURN key: Execute command or print line */
else if (key == '\n') {
wclear(ctx->linewin);
wmove(self->window, y2-CURS_Y_OFFSET, 0);
wclrtobot(self->window);
if (ctx->line[0] == '/')
execute(self, ctx, m, ctx->line);
else {
/* make sure the string has at least non-space character */
if (!string_is_empty(ctx->line)) {
uint8_t selfname[MAX_NAME_LENGTH];
getself_name(m, selfname, sizeof(selfname));
fix_name(selfname);
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s: ", selfname);
wattroff(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s\n", ctx->line);
if (m_sendmessage(m, ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) {
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send message.\n");
wattroff(ctx->history, COLOR_PAIR(3));
}
}
}
ctx->line[0] = '\0';
ctx->pos = 0;
}
}
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
{
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
wclear(self->window);
wclear(ctx->history);
int x, y;
getmaxyx(self->window, y, x);
(void) x;
wmove(self->window, y-CURS_Y_OFFSET, 0);
}
else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
print_help(ctx);
else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
endwin();
exit(0);
}
else if (!strncmp(cmd, "/me ", strlen("/me "))) {
struct tm *timeinfo = get_time(); struct tm *timeinfo = get_time();
char *action = strchr(cmd, ' ');
if (action == NULL) { if (ctx->friendnum != num)
wprintw(self->window, "Invalid syntax.\n"); return;
return;
} action[len - 1] = '\0';
action++; fix_name(action);
wattron(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(2));
uint8_t selfname[MAX_NAME_LENGTH];
int len = getself_name(m, selfname, sizeof(selfname));
char msg[MAX_STR_SIZE-len-4];
snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action);
wattron(ctx->history, COLOR_PAIR(5)); wattron(ctx->history, COLOR_PAIR(5));
wprintw(ctx->history, msg); wprintw(ctx->history, "%s\n", action);
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) {
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send action\n");
wattroff(ctx->history, COLOR_PAIR(3));
}
}
else if (!strncmp(cmd, "/status ", strlen("/status "))) { self->blink = true;
char *status = strchr(cmd, ' '); beep();
char *msg; }
char *status_text;
if (status == NULL) { static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
wprintw(ctx->history, "Invalid syntax.\n"); {
return; ChatContext *ctx = (ChatContext *) self->x;
} struct tm *timeinfo = get_time();
status++;
USERSTATUS status_kind; if (ctx->friendnum != num)
if (!strncmp(status, "online", strlen("online"))) { return;
status_kind = USERSTATUS_NONE;
status_text = "ONLINE"; wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
nick[len - 1] = '\0';
fix_name(nick);
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick);
wattroff(ctx->history, COLOR_PAIR(3));
}
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
{
ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
status[len - 1] = '\0';
fix_name(status);
snprintf(self->title, sizeof(self->title), "[%s (%d)]", status, num);
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, "* Your partner changed status to '%s'\n", status);
wattroff(ctx->history, COLOR_PAIR(3));
}
/* check that the string has one non-space character */
int string_is_empty(char *string)
{
int rc = 0;
char *copy = strdup(string);
rc = ((strtok(copy, " ") == NULL) ? 1 : 0);
free(copy);
return rc;
}
static void chat_onKey(ToxWindow *self, Messenger *m, int key)
{
ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
int x, y, y2, x2;
getyx(self->window, y, x);
getmaxyx(self->window, y2, x2);
/* Add printable chars to buffer and print on input space */
if (isprint(key)) {
if (ctx->pos != sizeof(ctx->line) - 1) {
mvwaddch(self->window, y, x, key);
ctx->line[ctx->pos++] = key;
ctx->line[ctx->pos] = '\0';
}
} }
else if (!strncmp(status, "away", strlen("away"))) { /* BACKSPACE key: Remove one character from line */
status_kind = USERSTATUS_AWAY; else if (key == 0x107 || key == 0x8 || key == 0x7f) {
status_text = "AWAY"; if (ctx->pos > 0) {
ctx->line[--ctx->pos] = '\0';
if (x == 0)
mvwdelch(self->window, y - 1, x2 - 1);
else
mvwdelch(self->window, y, x - 1);
}
} }
else if (!strncmp(status, "busy", strlen("busy"))) { /* RETURN key: Execute command or print line */
status_kind = USERSTATUS_BUSY; else if (key == '\n') {
status_text = "BUSY"; wclear(ctx->linewin);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
wclrtobot(self->window);
if (ctx->line[0] == '/')
execute(self, ctx, m, ctx->line);
else {
/* make sure the string has at least non-space character */
if (!string_is_empty(ctx->line)) {
uint8_t selfname[MAX_NAME_LENGTH];
getself_name(m, selfname, sizeof(selfname));
fix_name(selfname);
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s: ", selfname);
wattroff(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s\n", ctx->line);
if (m_sendmessage(m, ctx->friendnum, (uint8_t *) ctx->line, strlen(ctx->line) + 1) == 0) {
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send message.\n");
wattroff(ctx->history, COLOR_PAIR(3));
}
}
}
ctx->line[0] = '\0';
ctx->pos = 0;
}
}
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
{
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
wclear(self->window);
wclear(ctx->history);
int x, y;
getmaxyx(self->window, y, x);
(void) x;
wmove(self->window, y - CURS_Y_OFFSET, 0);
} }
else { else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
wprintw(ctx->history, "Invalid status.\n"); print_help(ctx);
return;
else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
endwin();
exit(0);
} }
msg = strchr(status, ' '); else if (!strncmp(cmd, "/me ", strlen("/me "))) {
if (msg == NULL) { struct tm *timeinfo = get_time();
m_set_userstatus(m, status_kind); char *action = strchr(cmd, ' ');
wprintw(ctx->history, "Status set to: %s\n", status_text);
}
else {
msg++;
m_set_userstatus(m, status_kind);
m_set_statusmessage(m, ( uint8_t*) msg, strlen(msg)+1);
wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
}
}
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { if (action == NULL) {
char *nick; wprintw(self->window, "Invalid syntax.\n");
nick = strchr(cmd, ' '); return;
if (nick == NULL) { }
wprintw(ctx->history, "Invalid syntax.\n");
return; action++;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
uint8_t selfname[MAX_NAME_LENGTH];
int len = getself_name(m, selfname, sizeof(selfname));
char msg[MAX_STR_SIZE - len - 4];
snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action);
wattron(ctx->history, COLOR_PAIR(5));
wprintw(ctx->history, msg);
wattroff(ctx->history, COLOR_PAIR(5));
if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) {
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send action\n");
wattroff(ctx->history, COLOR_PAIR(3));
}
} }
nick++; else if (!strncmp(cmd, "/status ", strlen("/status "))) {
setname(m, (uint8_t*) nick, strlen(nick)+1); char *status = strchr(cmd, ' ');
wprintw(ctx->history, "Nickname set to: %s\n", nick); char *msg;
} char *status_text;
else if (!strcmp(cmd, "/myid")) { if (status == NULL) {
char id[FRIEND_ADDRESS_SIZE*2+1] = {0}; wprintw(ctx->history, "Invalid syntax.\n");
int i; return;
uint8_t address[FRIEND_ADDRESS_SIZE]; }
getaddress(m, address);
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { status++;
char xx[3]; USERSTATUS status_kind;
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx); if (!strncmp(status, "online", strlen("online"))) {
status_kind = USERSTATUS_NONE;
status_text = "ONLINE";
}
else if (!strncmp(status, "away", strlen("away"))) {
status_kind = USERSTATUS_AWAY;
status_text = "AWAY";
}
else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = USERSTATUS_BUSY;
status_text = "BUSY";
}
else {
wprintw(ctx->history, "Invalid status.\n");
return;
}
msg = strchr(status, ' ');
if (msg == NULL) {
m_set_userstatus(m, status_kind);
wprintw(ctx->history, "Status set to: %s\n", status_text);
} else {
msg++;
m_set_userstatus(m, status_kind);
m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
}
} }
wprintw(ctx->history, "%s\n", id);
}
else if (strcmp(ctx->line, "/close") == 0) { else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
int f_num = ctx->friendnum; char *nick;
delwin(ctx->linewin); nick = strchr(cmd, ' ');
del_window(self, f_num);
}
else if (nick == NULL) {
wprintw(ctx->history, "Invalid command.\n"); wprintw(ctx->history, "Invalid syntax.\n");
return;
}
nick++;
setname(m, (uint8_t *) nick, strlen(nick) + 1);
wprintw(ctx->history, "Nickname set to: %s\n", nick);
}
else if (!strcmp(cmd, "/myid")) {
char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
int i;
uint8_t address[FRIEND_ADDRESS_SIZE];
getaddress(m, address);
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
char xx[3];
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
}
wprintw(ctx->history, "%s\n", id);
}
else if (strcmp(ctx->line, "/close") == 0) {
int f_num = ctx->friendnum;
delwin(ctx->linewin);
del_window(self, f_num);
}
else
wprintw(ctx->history, "Invalid command.\n");
} }
static void chat_onDraw(ToxWindow *self) static void chat_onDraw(ToxWindow *self)
{ {
curs_set(1); curs_set(1);
int x, y; int x, y;
getmaxyx(self->window, y, x); getmaxyx(self->window, y, x);
(void) y; (void) y;
ChatContext *ctx = (ChatContext*) self->x; ChatContext *ctx = (ChatContext *) self->x;
mvwhline(ctx->linewin, 0, 0, '_', x); mvwhline(ctx->linewin, 0, 0, '_', x);
wrefresh(self->window); wrefresh(self->window);
} }
static void chat_onInit(ToxWindow *self, Messenger *m) static void chat_onInit(ToxWindow *self, Messenger *m)
{ {
int x, y; int x, y;
ChatContext *ctx = (ChatContext*) self->x; ChatContext *ctx = (ChatContext *) self->x;
getmaxyx(self->window, y, x); getmaxyx(self->window, y, x);
ctx->history = subwin(self->window, y-4, x, 0, 0); ctx->history = subwin(self->window, y - 4, x, 0, 0);
scrollok(ctx->history, 1); scrollok(ctx->history, 1);
ctx->linewin = subwin(self->window, 2, x, y-4, 0); ctx->linewin = subwin(self->window, 2, x, y - 4, 0);
print_help(ctx); print_help(ctx);
wmove(self->window, y-CURS_Y_OFFSET, 0); wmove(self->window, y - CURS_Y_OFFSET, 0);
} }
void print_help(ChatContext *self) void print_help(ChatContext *self)
{ {
wattron(self->history, COLOR_PAIR(2) | A_BOLD); wattron(self->history, COLOR_PAIR(2) | A_BOLD);
wprintw(self->history, "Commands:\n"); wprintw(self->history, "Commands:\n");
wattroff(self->history, A_BOLD); wattroff(self->history, A_BOLD);
wprintw(self->history, " /status <type> <message> : Set your status\n"); wprintw(self->history, " /status <type> <message> : Set your status\n");
wprintw(self->history, " /nick <nickname> : Set your nickname\n"); wprintw(self->history, " /nick <nickname> : Set your nickname\n");
wprintw(self->history, " /me <action> : Do an action\n"); wprintw(self->history, " /me <action> : Do an action\n");
wprintw(self->history, " /myid : Print your ID\n"); wprintw(self->history, " /myid : Print your ID\n");
wprintw(self->history, " /clear : Clear the screen\n"); wprintw(self->history, " /clear : Clear the screen\n");
wprintw(self->history, " /close : Close the current chat window\n"); wprintw(self->history, " /close : Close the current chat window\n");
wprintw(self->history, " /quit or /exit : Exit program\n"); wprintw(self->history, " /quit or /exit : Exit program\n");
wprintw(self->history, " /help : Print this message again\n\n"); wprintw(self->history, " /help : Print this message again\n\n");
wattroff(self->history, COLOR_PAIR(2)); wattroff(self->history, COLOR_PAIR(2));
} }
ToxWindow new_chat(Messenger *m, int friendnum) ToxWindow new_chat(Messenger *m, int friendnum)
{ {
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
ret.onKey = &chat_onKey; ret.onKey = &chat_onKey;
ret.onDraw = &chat_onDraw; ret.onDraw = &chat_onDraw;
ret.onInit = &chat_onInit; ret.onInit = &chat_onInit;
ret.onMessage = &chat_onMessage; ret.onMessage = &chat_onMessage;
ret.onNickChange = &chat_onNickChange; ret.onNickChange = &chat_onNickChange;
ret.onStatusChange = &chat_onStatusChange; ret.onStatusChange = &chat_onStatusChange;
ret.onAction = &chat_onAction; ret.onAction = &chat_onAction;
uint8_t nick[MAX_NAME_LENGTH] = {0}; uint8_t nick[MAX_NAME_LENGTH] = {0};
getname(m, friendnum, (uint8_t*) &nick); getname(m, friendnum, (uint8_t *) &nick);
fix_name(nick); fix_name(nick);
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
ChatContext *x = calloc(1, sizeof(ChatContext)); ChatContext *x = calloc(1, sizeof(ChatContext));
x->friendnum = friendnum; x->friendnum = friendnum;
ret.x = (void*) x; ret.x = (void *) x;
return ret; return ret;
} }

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;
} }
@ -111,44 +117,45 @@ char *get_user_config_dir(void)
* Creates the config directory. * Creates the config directory.
*/ */
int create_user_config_dir(char *path) int create_user_config_dir(char *path)
{ {
int mkdir_err; int mkdir_err;
#ifdef WIN32 #ifdef WIN32
char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
strcpy(fullpath, path); strcpy(fullpath, path);
strcat(fullpath, CONFIGDIR); strcat(fullpath, CONFIGDIR);
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);
return -1;
}
#else
mkdir_err = mkdir(path, 0700);
struct stat buf;
if (mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) {
return -1;
}
char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
strcpy(fullpath, path);
strcat(fullpath, CONFIGDIR);
mkdir_err = mkdir(fullpath, 0700);
if (mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
free(fullpath);
return -1;
}
#endif
free(fullpath); free(fullpath);
return -1; return 0;
}
#else
mkdir_err = mkdir(path, 0700);
struct stat buf;
if(mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) {
return -1;
}
char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
strcpy(fullpath, path);
strcat(fullpath, CONFIGDIR);
mkdir_err = mkdir(fullpath, 0700);
if(mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
free(fullpath);
return -1;
}
#endif
free(fullpath);
return 0;
} }

View File

@ -23,7 +23,7 @@
#else #else
#define CONFIGDIR "/toxic/" #define CONFIGDIR "/toxic/"
#endif #endif
#ifndef S_ISDIR #ifndef S_ISDIR
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif #endif

View File

@ -3,87 +3,97 @@
#include "../../core/network.h" #include "../../core/network.h"
#include "../../core/DHT.h" #include "../../core/DHT.h"
typedef uint8_t ipbuf[3*4+3+1]; typedef uint8_t ipbuf[3 * 4 + 3 + 1];
static int num_selected = 0; static int num_selected = 0;
static void printip(ipbuf buf, IP ip) static void printip(ipbuf buf, IP ip)
{ {
sprintf((char*)buf, "%u.%u.%u.%u", ip.c[0], ip.c[1], ip.c[2], ip.c[3]); sprintf((char *)buf, "%u.%u.%u.%u", ip.c[0], ip.c[1], ip.c[2], ip.c[3]);
} }
static void dhtstatus_onKey(ToxWindow *self, Messenger *m, int key) static void dhtstatus_onKey(ToxWindow *self, Messenger *m, int key)
{ {
switch(key) { switch (key) {
case KEY_UP: case KEY_UP:
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 'j': case KEY_DOWN:
num_selected = (num_selected+1) % CLIENT_ID_SIZE; case 'j':
break; num_selected = (num_selected + 1) % CLIENT_ID_SIZE;
break;
case '\n':
break; case '\n':
break;
default:
break; default:
} break;
}
} }
static void dhtstatus_onDraw(ToxWindow *self) static void dhtstatus_onDraw(ToxWindow *self)
{ {
Client_data * close_clientlist = DHT_get_close_list(); Client_data *close_clientlist = DHT_get_close_list();
curs_set(0); curs_set(0);
werase(self->window); werase(self->window);
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,
for(i = 0; i < 32; i++) { /*Number of nodes in closelist*/ "\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n",
Client_data * client = close_clientlist + i; now);
if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
wprintw(self->window,"[%02i] ", i); for (i = 0; i < 32; i++) { /*Number of nodes in closelist*/
uint16_t port = ntohs(client->ip_port.port); Client_data *client = close_clientlist + i;
if(port) {
for(j = 0; j < CLIENT_ID_SIZE; j++) if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
wprintw(self->window, "%02hhx", client->client_id[j]);
wprintw(self->window, "[%02i] ", i);
printip(ipbuf, client->ip_port.ip); uint16_t port = ntohs(client->ip_port.port);
wprintw(self->window, " %15s %5u ", ipbuf, port);
wprintw(self->window, " %3llu ", now - client->timestamp); if (port) {
wprintw(self->window, " %3llu ", now - client->last_pinged); for (j = 0; j < CLIENT_ID_SIZE; j++)
wprintw(self->window, "%02hhx", client->client_id[j]);
port = ntohs(client->ret_ip_port.port);
if(port) { printip(ipbuf, client->ip_port.ip);
printip(ipbuf, client->ret_ip_port.ip); wprintw(self->window, " %15s %5u ", ipbuf, port);
wprintw(self->window, " %15s %5u %3llu", ipbuf, port, now - close_clientlist[i].ret_timestamp); wprintw(self->window, " %3llu ", now - client->timestamp);
} wprintw(self->window, " %3llu ", now - client->last_pinged);
port = ntohs(client->ret_ip_port.port);
if (port) {
printip(ipbuf, client->ret_ip_port.ip);
wprintw(self->window, " %15s %5u %3llu", ipbuf, port, now - close_clientlist[i].ret_timestamp);
}
}
wprintw(self->window, "\n");
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
} }
wprintw(self->window, "\n");
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3)); wrefresh(self->window);
}
wrefresh(self->window);
} }
static void dhtstatus_onInit(ToxWindow *self, Messenger *m) static void dhtstatus_onInit(ToxWindow *self, Messenger *m)
{ {
} }
ToxWindow new_dhtstatus() ToxWindow new_dhtstatus()
{ {
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
ret.onKey = &dhtstatus_onKey; ret.onKey = &dhtstatus_onKey;
ret.onDraw = &dhtstatus_onDraw; ret.onDraw = &dhtstatus_onDraw;
ret.onInit = &dhtstatus_onInit; ret.onInit = &dhtstatus_onInit;
strcpy(ret.title, "[dht status]"); strcpy(ret.title, "[dht status]");
return ret; return ret;
} }

View File

@ -13,13 +13,13 @@
#include "windows.h" #include "windows.h"
#include "friendlist.h" #include "friendlist.h"
static char * WINDOW_STATUS; static char *WINDOW_STATUS;
typedef struct { typedef struct {
uint8_t name[MAX_NAME_LENGTH]; uint8_t name[MAX_NAME_LENGTH];
uint8_t status[MAX_STATUSMESSAGE_LENGTH]; uint8_t status[MAX_STATUSMESSAGE_LENGTH];
int num; int num;
int chatwin; int chatwin;
} friend_t; } friend_t;
static friend_t friends[MAX_FRIENDS_NUM]; static friend_t friends[MAX_FRIENDS_NUM];
@ -28,135 +28,143 @@ static int num_selected = 0;
void fix_name(uint8_t *name) 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) {
if (isprint(*p)) while (*p != 0) {
*q++ = *p; if (isprint(*p))
p++; *q++ = *p;
}
*q = 0; p++;
}
*q = 0;
} }
void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len) void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len)
{ {
if (num >= num_friends) if (num >= num_friends)
return; return;
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 */
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { /* Find first open slot to hold chat window */
if (WINDOW_STATUS[i] == -1) { for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
WINDOW_STATUS[i] = num; if (WINDOW_STATUS[i] == -1) {
add_window(m, new_chat(m, num), i); WINDOW_STATUS[i] = num;
break; add_window(m, new_chat(m, num), i);
} break;
}
}
} }
}
} }
void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{ {
if (len >= MAX_NAME_LENGTH || num >= num_friends) if (len >= MAX_NAME_LENGTH || num >= num_friends)
return; return;
memcpy((char*) &friends[num].name, (char*) str, len); memcpy((char *) &friends[num].name, (char *) str, len);
friends[num].name[len] = 0; friends[num].name[len] = 0;
fix_name(friends[num].name); fix_name(friends[num].name);
} }
void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{ {
if (len >= MAX_STATUSMESSAGE_LENGTH || num >= num_friends) if (len >= MAX_STATUSMESSAGE_LENGTH || num >= num_friends)
return; return;
memcpy((char*) &friends[num].status, (char*) str, len); memcpy((char *) &friends[num].status, (char *) str, len);
friends[num].status[len] = 0; friends[num].status[len] = 0;
fix_name(friends[num].status); fix_name(friends[num].status);
} }
int friendlist_onFriendAdded(Messenger *m, int num) int friendlist_onFriendAdded(Messenger *m, int num)
{ {
if (num_friends == MAX_FRIENDS_NUM) if (num_friends == MAX_FRIENDS_NUM)
return -1; return -1;
friends[num_friends].num = num; friends[num_friends].num = num;
getname(m, num, friends[num_friends].name); getname(m, num, friends[num_friends].name);
strcpy((char*) friends[num_friends].name, "unknown"); strcpy((char *) friends[num_friends].name, "unknown");
strcpy((char*) friends[num_friends].status, "unknown"); strcpy((char *) friends[num_friends].status, "unknown");
friends[num_friends++].chatwin = -1; friends[num_friends++].chatwin = -1;
return 0; return 0;
} }
static void friendlist_onKey(ToxWindow *self, Messenger *m, int key) 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') {
} /* Jump to chat window if already open */
else if (key == '\n') { if (friends[num_selected].chatwin != -1) {
/* Jump to chat window if already open */ int i;
if (friends[num_selected].chatwin != -1) {
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); break;
break; }
}
} else {
int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num_selected;
friends[num_selected].chatwin = num_selected;
add_window(m, new_chat(m, num_selected), i);
break;
}
}
} }
}
}else {
int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num_selected;
friends[num_selected].chatwin = num_selected;
add_window(m, new_chat(m, num_selected), i);
break;
}
}
} }
}
} }
static void friendlist_onDraw(ToxWindow *self) static void friendlist_onDraw(ToxWindow *self)
{ {
curs_set(0); curs_set(0);
werase(self->window); werase(self->window);
if (num_friends == 0) {
wprintw(self->window, "Empty. Add some friends! :-)\n");
}
else {
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
wprintw(self->window, "Open chat with.. (up/down keys, enter)\n");
wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
}
wprintw(self->window, "\n"); if (num_friends == 0) {
int i; wprintw(self->window, "Empty. Add some friends! :-)\n");
for (i = 0; i < num_friends; ++i) { } else {
if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); wattron(self->window, COLOR_PAIR(2) | A_BOLD);
wprintw(self->window, " [#%d] ", friends[i].num); wprintw(self->window, "Open chat with.. (up/down keys, enter)\n");
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3)); wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
}
attron(A_BOLD); wprintw(self->window, "\n");
wprintw(self->window, "%s ", friends[i].name); int i;
attroff(A_BOLD);
wprintw(self->window, "(%s)\n", friends[i].status); for (i = 0; i < num_friends; ++i) {
} if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
wrefresh(self->window);
wprintw(self->window, " [#%d] ", friends[i].num);
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
attron(A_BOLD);
wprintw(self->window, "%s ", friends[i].name);
attroff(A_BOLD);
wprintw(self->window, "(%s)\n", friends[i].status);
}
wrefresh(self->window);
} }
void disable_chatwin(int f_num) void disable_chatwin(int f_num)
{ {
friends[f_num].chatwin = -1; friends[f_num].chatwin = -1;
} }
static void friendlist_onInit(ToxWindow *self, Messenger *m) static void friendlist_onInit(ToxWindow *self, Messenger *m)
@ -164,19 +172,20 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m)
} }
ToxWindow new_friendlist(char * ws) { ToxWindow new_friendlist(char *ws)
WINDOW_STATUS = ws; {
ToxWindow ret; WINDOW_STATUS = ws;
memset(&ret, 0, sizeof(ret)); ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.onKey = &friendlist_onKey; ret.onKey = &friendlist_onKey;
ret.onDraw = &friendlist_onDraw; ret.onDraw = &friendlist_onDraw;
ret.onInit = &friendlist_onInit; ret.onInit = &friendlist_onInit;
ret.onMessage = &friendlist_onMessage; ret.onMessage = &friendlist_onMessage;
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
ret.onNickChange = &friendlist_onNickChange; ret.onNickChange = &friendlist_onNickChange;
ret.onStatusChange = &friendlist_onStatusChange; ret.onStatusChange = &friendlist_onStatusChange;
strcpy(ret.title, "[friends]"); strcpy(ret.title, "[friends]");
return ret; return ret;
} }

View File

@ -4,7 +4,7 @@
#include "windows.h" #include "windows.h"
#include "chat.h" #include "chat.h"
ToxWindow new_friendlist(char * ws); ToxWindow new_friendlist(char *ws);
int friendlist_onFriendAdded(Messenger *m, int num); int friendlist_onFriendAdded(Messenger *m, int num);
void disable_chatwin(int f_num); void disable_chatwin(int f_num);
void fix_name(uint8_t *name); void fix_name(uint8_t *name);

299
main.c
View File

@ -28,7 +28,7 @@
/* Export for use in Callbacks */ /* Export for use in Callbacks */
char *DATA_FILE = NULL; char *DATA_FILE = NULL;
void on_window_resize(int sig) void on_window_resize(int sig)
{ {
endwin(); endwin();
refresh(); refresh();
@ -37,44 +37,45 @@ void on_window_resize(int sig)
static void init_term() static void init_term()
{ {
/* Setup terminal */ /* Setup terminal */
signal(SIGWINCH, on_window_resize); signal(SIGWINCH, on_window_resize);
initscr(); initscr();
cbreak(); cbreak();
keypad(stdscr, 1); keypad(stdscr, 1);
noecho(); noecho();
timeout(100); timeout(100);
if (has_colors()) { if (has_colors()) {
start_color(); start_color();
init_pair(1, COLOR_GREEN, COLOR_BLACK); init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK);
init_pair(3, COLOR_RED, COLOR_BLACK); init_pair(3, COLOR_RED, COLOR_BLACK);
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();
} }
static Messenger *init_tox() static Messenger *init_tox()
{ {
/* Init core */ /* Init core */
Messenger *m = initMessenger(); Messenger *m = initMessenger();
/* Callbacks */ /* Callbacks */
m_callback_friendrequest(m, on_request, NULL); m_callback_friendrequest(m, on_request, NULL);
m_callback_friendmessage(m, on_message, NULL); m_callback_friendmessage(m, on_message, NULL);
m_callback_namechange(m, on_nickchange, NULL); m_callback_namechange(m, on_nickchange, NULL);
m_callback_statusmessage(m, on_statuschange, NULL); m_callback_statusmessage(m, on_statuschange, NULL);
m_callback_action(m, on_action, NULL); m_callback_action(m, on_action, NULL);
#ifdef __linux__ #ifdef __linux__
setname(m, (uint8_t*) "Cool guy", sizeof("Cool guy")); setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy"));
#elif WIN32 #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 #else
setname(m, (uint8_t*) "Hipster", sizeof("Hipster")); setname(m, (uint8_t *) "Hipster", sizeof("Hipster"));
#endif #endif
return m; return m;
} }
#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ #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 */ /* Connects to a random DHT server listed in the DHTservers file */
int init_connection(void) int init_connection(void)
{ {
if (DHT_isconnected()) if (DHT_isconnected())
return 0; return 0;
FILE *fp = fopen("../../../other/DHTservers", "r"); FILE *fp = fopen("../../../other/DHTservers", "r");
if (!fp)
return 1; 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); fclose(fp);
return 2;
}
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)
return 3;
IP_Port dht; if (!ip || !port || !key)
dht.port = htons(atoi(port)); return 3;
uint32_t resolved_address = resolve_addr(ip);
if (resolved_address == 0) 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; 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_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 (!conn_err) { if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) {
conn_err = init_connection(); if (!conn_err) {
wprintw(prompt->window, "\nEstablishing connection...\n"); conn_err = init_connection();
if (conn_err) wprintw(prompt->window, "\nEstablishing connection...\n");
wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err);
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()) { doMessenger(m);
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);
} }
int f_loadfromfile; int f_loadfromfile;
@ -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();
@ -236,70 +255,74 @@ static void load_data(Messenger *m, char *path)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *user_config_dir = get_user_config_dir(); char *user_config_dir = get_user_config_dir();
int config_err = 0; int config_err = 0;
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) {
if (argv[i] == NULL) for (i = 0; i < argc; ++i) {
break; if (argv[i] == NULL)
else if (argv[i][0] == '-') { break;
if (argv[i][1] == 'f') { else if (argv[i][0] == '-') {
if (argv[i + 1] != NULL) if (argv[i][1] == 'f') {
DATA_FILE = strdup(argv[i + 1]); if (argv[i + 1] != NULL)
else DATA_FILE = strdup(argv[i + 1]);
f_flag = -1; else
} else if (argv[i][1] == 'n') { f_flag = -1;
f_loadfromfile = 0; } 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) { if (config_err) {
DATA_FILE = strdup("data"); attron(COLOR_PAIR(3) | A_BOLD);
} else { wprintw(prompt->window, "Unable to determine configuration directory.\n"
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); "defaulting to 'data' for a keyfile...\n");
strcpy(DATA_FILE, user_config_dir); attroff(COLOR_PAIR(3) | A_BOLD);
strcat(DATA_FILE, CONFIGDIR);
strcat(DATA_FILE, "data");
} }
}
free(user_config_dir);
init_term(); while (true) {
Messenger *m = init_tox(); /* Update tox */
ToxWindow *prompt = init_windows(m); do_tox(m, prompt);
init_window_status();
if(f_loadfromfile) /* Draw */
load_data(m, DATA_FILE); draw_active_window(m);
}
if (f_flag == -1) { cleanupMessenger(m);
attron(COLOR_PAIR(3) | A_BOLD); free(DATA_FILE);
wprintw(prompt->window, "You passed '-f' without giving an argument.\n" return 0;
"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;
} }

647
prompt.c
View File

@ -14,7 +14,7 @@
#include "prompt.h" #include "prompt.h"
uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX
uint8_t num_requests=0; // XXX uint8_t num_requests = 0; // XXX
static friendAddedFn *on_friendadded_cb; static friendAddedFn *on_friendadded_cb;
static char prompt_buf[MAX_STR_SIZE] = {0}; static char prompt_buf[MAX_STR_SIZE] = {0};
@ -36,412 +36,441 @@ void cmd_statusmsg(ToxWindow *, Messenger *m, char **);
#define NUM_COMMANDS 13 #define NUM_COMMANDS 13
static struct { static struct {
char *name; char *name;
int numargs; int numargs;
void (*func)(ToxWindow *, Messenger *m, char **); void (*func)(ToxWindow *, Messenger *m, char **);
} commands[] = { } commands[] = {
{ "accept", 1, cmd_accept }, { "accept", 1, cmd_accept },
{ "add", 1, cmd_add }, { "add", 1, cmd_add },
{ "clear", 0, cmd_clear }, { "clear", 0, cmd_clear },
{ "connect", 3, cmd_connect }, { "connect", 3, cmd_connect },
{ "exit", 0, cmd_quit }, { "exit", 0, cmd_quit },
{ "help", 0, cmd_help }, { "help", 0, cmd_help },
{ "msg", 2, cmd_msg }, { "msg", 2, cmd_msg },
{ "myid", 0, cmd_myid }, { "myid", 0, cmd_myid },
{ "nick", 1, cmd_nick }, { "nick", 1, cmd_nick },
{ "q", 0, cmd_quit }, { "q", 0, cmd_quit },
{ "quit", 0, cmd_quit }, { "quit", 0, cmd_quit },
{ "status", 2, cmd_status }, { "status", 2, cmd_status },
{ "statusmsg", 1, cmd_statusmsg }, { "statusmsg", 1, cmd_statusmsg },
}; };
// XXX: // XXX:
int add_req(uint8_t *public_key) int add_req(uint8_t *public_key)
{ {
memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
++num_requests; ++num_requests;
return num_requests-1; return num_requests - 1;
} }
// XXX: FIX // XXX: FIX
unsigned char *hex_string_to_bin(char hex_string[]) unsigned char *hex_string_to_bin(char hex_string[])
{ {
size_t len = strlen(hex_string); size_t len = strlen(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)
sscanf(pos,"%2hhx",&val[i]); for (i = 0; i < len; ++i, pos += 2)
return val; sscanf(pos, "%2hhx", &val[i]);
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) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
num = m_addfriend_norequest(m, pending_requests[num]); if (num >= num_requests) {
if (num == -1) wprintw(self->window, "Invalid syntax.\n");
wprintw(self->window, "Failed to add friend.\n"); return;
else { }
wprintw(self->window, "Friend accepted as: %d.\n", num);
on_friendadded_cb(m, num); num = m_addfriend_norequest(m, pending_requests[num]);
}
if (num == -1)
wprintw(self->window, "Failed to add friend.\n");
else {
wprintw(self->window, "Friend accepted as: %d.\n", num);
on_friendadded_cb(m, num);
}
} }
void cmd_add(ToxWindow *self, Messenger *m, char **args) void cmd_add(ToxWindow *self, Messenger *m, char **args)
{ {
uint8_t id_bin[FRIEND_ADDRESS_SIZE]; uint8_t id_bin[FRIEND_ADDRESS_SIZE];
char xx[3]; char xx[3];
uint32_t x; uint32_t x;
char *id = args[1]; char *id = args[1];
char *msg = args[2]; char *msg = args[2];
if (!id) { if (!id) {
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)
msg = ""; if (!msg)
msg = "";
if (strlen(id) != 2*FRIEND_ADDRESS_SIZE) {
wprintw(self->window, "Invalid ID length.\n"); if (strlen(id) != 2 * FRIEND_ADDRESS_SIZE) {
return; wprintw(self->window, "Invalid ID length.\n");
} return;
int i; }
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
xx[0] = id[2*i]; int i;
xx[1] = id[2*i+1];
xx[2] = '\0'; for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
if (sscanf(xx, "%02x", &x) != 1) { xx[0] = id[2 * i];
wprintw(self->window, "Invalid ID.\n"); xx[1] = id[2 * i + 1];
return; xx[2] = '\0';
if (sscanf(xx, "%02x", &x) != 1) {
wprintw(self->window, "Invalid ID.\n");
return;
}
id_bin[i] = x;
}
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
id[i] = toupper(id[i]);
}
int num = m_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1);
switch (num) {
case FAERR_TOOLONG:
wprintw(self->window, "Message is too long.\n");
break;
case FAERR_NOMESSAGE:
wprintw(self->window, "Please add a message to your request.\n");
break;
case FAERR_OWNKEY:
wprintw(self->window, "That appears to be your own ID.\n");
break;
case FAERR_ALREADYSENT:
wprintw(self->window, "Friend request already sent.\n");
break;
case FAERR_UNKNOWN:
wprintw(self->window, "Undefined error when adding friend.\n");
break;
case FAERR_BADCHECKSUM:
wprintw(self->window, "Bad checksum in address.\n");
break;
case FAERR_SETNEWNOSPAM:
wprintw(self->window, "Nospam was different.\n");
break;
default:
wprintw(self->window, "Friend added as %d.\n", num);
on_friendadded_cb(m, num);
break;
} }
id_bin[i] = x;
}
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
id[i] = toupper(id[i]);
}
int num = m_addfriend(m, id_bin, (uint8_t*) msg, strlen(msg)+1);
switch (num) {
case FAERR_TOOLONG:
wprintw(self->window, "Message is too long.\n");
break;
case FAERR_NOMESSAGE:
wprintw(self->window, "Please add a message to your request.\n");
break;
case FAERR_OWNKEY:
wprintw(self->window, "That appears to be your own ID.\n");
break;
case FAERR_ALREADYSENT:
wprintw(self->window, "Friend request already sent.\n");
break;
case FAERR_UNKNOWN:
wprintw(self->window, "Undefined error when adding friend.\n");
break;
case FAERR_BADCHECKSUM:
wprintw(self->window, "Bad checksum in address.\n");
break;
case FAERR_SETNEWNOSPAM:
wprintw(self->window, "Nospam was different.\n");
break;
default:
wprintw(self->window, "Friend added as %d.\n", num);
on_friendadded_cb(m, num);
break;
}
} }
void cmd_clear(ToxWindow *self, Messenger *m, char **args) void cmd_clear(ToxWindow *self, Messenger *m, char **args)
{ {
wclear(self->window); wclear(self->window);
} }
void cmd_connect(ToxWindow *self, Messenger *m, char **args) void cmd_connect(ToxWindow *self, Messenger *m, char **args)
{ {
IP_Port dht; IP_Port dht;
char *ip = args[1]; char *ip = args[1];
char *port = args[2]; char *port = args[2];
char *key = args[3]; char *key = args[3];
if (atoi(port) == 0) { if (atoi(port) == 0) {
wprintw(self->window, "Invalid syntax.\n"); wprintw(self->window, "Invalid syntax.\n");
return; return;
} }
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) {
return;
}
dht.ip.i = resolved_address; if (resolved_address == 0) {
unsigned char *binary_string = hex_string_to_bin(key); return;
DHT_bootstrap(dht, binary_string); }
free(binary_string);
dht.ip.i = resolved_address;
unsigned char *binary_string = hex_string_to_bin(key);
DHT_bootstrap(dht, binary_string);
free(binary_string);
} }
void cmd_quit(ToxWindow *self, Messenger *m, char **args) void cmd_quit(ToxWindow *self, Messenger *m, char **args)
{ {
endwin(); endwin();
exit(0); exit(0);
} }
void cmd_help(ToxWindow *self, Messenger *m, char **args) void cmd_help(ToxWindow *self, Messenger *m, char **args)
{ {
wclear(self->window); wclear(self->window);
wattron(self->window, COLOR_PAIR(2) | A_BOLD); wattron(self->window, COLOR_PAIR(2) | A_BOLD);
wprintw(self->window, "Commands:\n"); wprintw(self->window, "Commands:\n");
wattroff(self->window, A_BOLD); wattroff(self->window, A_BOLD);
wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
wprintw(self->window, " add <id> <message> : Add friend\n"); wprintw(self->window, " add <id> <message> : Add friend\n");
wprintw(self->window, " status <type> <message> : Set your status\n"); wprintw(self->window, " status <type> <message> : Set your status\n");
wprintw(self->window, " statusmsg <message> : Set your status\n"); wprintw(self->window, " statusmsg <message> : Set your status\n");
wprintw(self->window, " nick <nickname> : Set your nickname\n"); wprintw(self->window, " nick <nickname> : Set your nickname\n");
wprintw(self->window, " accept <number> : Accept friend request\n"); wprintw(self->window, " accept <number> : Accept friend request\n");
wprintw(self->window, " myid : Print your ID\n"); wprintw(self->window, " myid : Print your ID\n");
wprintw(self->window, " quit/exit : Exit program\n"); wprintw(self->window, " quit/exit : Exit program\n");
wprintw(self->window, " help : Print this message again\n"); wprintw(self->window, " help : Print this message again\n");
wprintw(self->window, " clear : Clear this window\n"); wprintw(self->window, " clear : Clear this window\n");
wattron(self->window, A_BOLD); wattron(self->window, A_BOLD);
wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");
wattroff(self->window, A_BOLD); wattroff(self->window, A_BOLD);
wattroff(self->window, COLOR_PAIR(2)); wattroff(self->window, COLOR_PAIR(2));
} }
void cmd_msg(ToxWindow *self, Messenger *m, char **args) 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)
wprintw(self->window, "Error occurred while sending message.\n"); if (m_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0)
else wprintw(self->window, "Error occurred while sending message.\n");
wprintw(self->window, "Message successfully sent.\n"); else
wprintw(self->window, "Message successfully sent.\n");
} }
void cmd_myid(ToxWindow *self, Messenger *m, char **args) void cmd_myid(ToxWindow *self, Messenger *m, char **args)
{ {
char id[FRIEND_ADDRESS_SIZE*2 + 1] = {0}; char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
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) {
char xx[3]; for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); char xx[3];
strcat(id, xx); snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
} strcat(id, xx);
wprintw(self->window, "%s\n", id); }
wprintw(self->window, "%s\n", id);
} }
void cmd_nick(ToxWindow *self, Messenger *m, char **args) void cmd_nick(ToxWindow *self, Messenger *m, char **args)
{ {
char *nick = args[1]; char *nick = args[1];
setname(m, (uint8_t*) nick, strlen(nick)+1); setname(m, (uint8_t *) nick, strlen(nick) + 1);
wprintw(self->window, "Nickname set to: %s\n", nick); wprintw(self->window, "Nickname set to: %s\n", nick);
} }
void cmd_status(ToxWindow *self, Messenger *m, char **args) void cmd_status(ToxWindow *self, Messenger *m, char **args)
{ {
char *status = args[1]; char *status = args[1];
char *status_text; char *status_text;
USERSTATUS status_kind; USERSTATUS status_kind;
if (!strncmp(status, "online", strlen("online"))) {
status_kind = USERSTATUS_NONE;
status_text = "ONLINE";
}
else if (!strncmp(status, "away", strlen("away"))) {
status_kind = USERSTATUS_AWAY;
status_text = "AWAY";
}
else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = USERSTATUS_BUSY;
status_text = "BUSY";
}
else
{
wprintw(self->window, "Invalid status.\n");
return;
}
char *msg = args[2]; if (!strncmp(status, "online", strlen("online"))) {
if (msg == NULL) { status_kind = USERSTATUS_NONE;
m_set_userstatus(m, status_kind); status_text = "ONLINE";
wprintw(self->window, "Status set to: %s\n", status_text); } else if (!strncmp(status, "away", strlen("away"))) {
} status_kind = USERSTATUS_AWAY;
else { status_text = "AWAY";
m_set_userstatus(m, status_kind); } else if (!strncmp(status, "busy", strlen("busy"))) {
m_set_statusmessage(m, (uint8_t*) msg, strlen(msg)+1); status_kind = USERSTATUS_BUSY;
wprintw(self->window, "Status set to: %s, %s\n", status_text, msg); status_text = "BUSY";
} } else {
wprintw(self->window, "Invalid status.\n");
return;
}
char *msg = args[2];
if (msg == NULL) {
m_set_userstatus(m, status_kind);
wprintw(self->window, "Status set to: %s\n", status_text);
} else {
m_set_userstatus(m, status_kind);
m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
wprintw(self->window, "Status set to: %s, %s\n", status_text, msg);
}
} }
void cmd_statusmsg(ToxWindow *self, Messenger *m, char **args) void cmd_statusmsg(ToxWindow *self, Messenger *m, char **args)
{ {
char *msg = args[1]; char *msg = args[1];
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\n", msg); wprintw(self->window, "Status set to: %s\n", msg);
} }
static void execute(ToxWindow *self, Messenger *m, char *u_cmd) 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) {
if (u_cmd[i] == '\n')
++newlines;
else
cmd[i - newlines] = u_cmd[i];
}
int leading_spc = 0; for (i = 0; i < strlen(prompt_buf); ++i) {
for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i) if (u_cmd[i] == '\n')
leading_spc++; ++newlines;
memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc); else
cmd[i - newlines] = u_cmd[i];
int cmd_end = strlen(cmd);
while (cmd_end > 0 && cmd_end--)
if (!isspace(cmd[cmd_end]))
break;
cmd[cmd_end + 1] = '\0';
/* insert \0 at argument boundaries */
int numargs = 0;
for (i = 0; i < MAX_STR_SIZE; i++) {
if (cmd[i] == '\"')
while (cmd[++i] != '\"'); /* skip over strings */
if (cmd[i] == ' ') {
cmd[i] = '\0';
numargs++;
} }
}
/* excessive arguments */ int leading_spc = 0;
if (numargs > 3) {
wprintw(self->window, "Invalid command: too many arguments.\n");
return;
}
/* read arguments into array */ for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i)
char *cmdargs[5]; leading_spc++;
int pos = 0;
for (i = 0; i < 5; i++) {
cmdargs[i] = cmd + pos;
pos += strlen(cmdargs[i]) + 1;
}
/* no input */ memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc);
if (strlen(cmdargs[0]) == 0)
return;
/* match input to command list */ int cmd_end = strlen(cmd);
for (i = 0; i < NUM_COMMANDS; i++) {
if (!strcmp(cmdargs[0], commands[i].name)) { while (cmd_end > 0 && cmd_end--)
/* check for missing arguments */ if (!isspace(cmd[cmd_end]))
int j; break;
for (j = 0; j <= commands[i].numargs; j++) {
if (strlen(cmdargs[j]) == 0) { cmd[cmd_end + 1] = '\0';
wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n",
commands[i].name, commands[i].numargs, j - 1); /* insert \0 at argument boundaries */
return; int numargs = 0;
for (i = 0; i < MAX_STR_SIZE; i++) {
if (cmd[i] == '\"')
while (cmd[++i] != '\"'); /* skip over strings */
if (cmd[i] == ' ') {
cmd[i] = '\0';
numargs++;
} }
}
/* check for excess arguments */
if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) {
wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name);
return;
}
/* pass arguments to command function */
(commands[i].func)(self, m, cmdargs);
return;
} }
}
/* no match */ /* excessive arguments */
wprintw(self->window, "Invalid command.\n"); if (numargs > 3) {
wprintw(self->window, "Invalid command: too many arguments.\n");
return;
}
/* read arguments into array */
char *cmdargs[5];
int pos = 0;
for (i = 0; i < 5; i++) {
cmdargs[i] = cmd + pos;
pos += strlen(cmdargs[i]) + 1;
}
/* no input */
if (strlen(cmdargs[0]) == 0)
return;
/* match input to command list */
for (i = 0; i < NUM_COMMANDS; i++) {
if (!strcmp(cmdargs[0], commands[i].name)) {
/* check for missing arguments */
int j;
for (j = 0; j <= commands[i].numargs; j++) {
if (strlen(cmdargs[j]) == 0) {
wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n",
commands[i].name, commands[i].numargs, j - 1);
return;
}
}
/* check for excess arguments */
if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) {
wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name);
return;
}
/* pass arguments to command function */
(commands[i].func)(self, m, cmdargs);
return;
}
}
/* no match */
wprintw(self->window, "Invalid command.\n");
} }
static void prompt_onKey(ToxWindow *self, Messenger *m, int key) static void prompt_onKey(ToxWindow *self, Messenger *m, int key)
{ {
/* Add printable characters to line */ /* Add printable characters to line */
if (isprint(key)) { if (isprint(key)) {
if (prompt_buf_pos == (sizeof(prompt_buf) - 1)) { if (prompt_buf_pos == (sizeof(prompt_buf) - 1)) {
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)
} && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS) prompt_buf[prompt_buf_pos++] = '\n';
&& ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) { }
prompt_buf[prompt_buf_pos++] = '\n';
}
prompt_buf[prompt_buf_pos++] = key;
prompt_buf[prompt_buf_pos] = 0;
}
/* RETURN key: execute command */ prompt_buf[prompt_buf_pos++] = key;
else if (key == '\n') { prompt_buf[prompt_buf_pos] = 0;
wprintw(self->window, "\n"); }
execute(self, m, prompt_buf);
prompt_buf_pos = 0; /* RETURN key: execute command */
prompt_buf[0] = 0; else if (key == '\n') {
} wprintw(self->window, "\n");
execute(self, m, prompt_buf);
/* BACKSPACE key: Remove one character from line */ prompt_buf_pos = 0;
else if (key == 0x107 || key == 0x8 || key == 0x7f) { prompt_buf[0] = 0;
if (prompt_buf_pos != 0) { }
prompt_buf[--prompt_buf_pos] = 0;
/* BACKSPACE key: Remove one character from line */
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (prompt_buf_pos != 0) {
prompt_buf[--prompt_buf_pos] = 0;
}
} }
}
} }
static void prompt_onDraw(ToxWindow *self) static void prompt_onDraw(ToxWindow *self)
{ {
curs_set(1); curs_set(1);
int x, y; int x, y;
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) {
if ((prompt_buf[i] == '\n') && (y != 0))
--y;
}
wattron(self->window, COLOR_PAIR(1)); for (i = 0; i < (strlen(prompt_buf)); ++i) {
mvwprintw(self->window, y, 0, "# "); if ((prompt_buf[i] == '\n') && (y != 0))
wattroff(self->window, COLOR_PAIR(1)); --y;
mvwprintw(self->window, y, 2, "%s", prompt_buf); }
wclrtoeol(self->window);
wrefresh(self->window); wattron(self->window, COLOR_PAIR(1));
mvwprintw(self->window, y, 0, "# ");
wattroff(self->window, COLOR_PAIR(1));
mvwprintw(self->window, y, 2, "%s", prompt_buf);
wclrtoeol(self->window);
wrefresh(self->window);
} }
static void prompt_onInit(ToxWindow *self, Messenger *m) static void prompt_onInit(ToxWindow *self, Messenger *m)
{ {
scrollok(self->window, 1); scrollok(self->window, 1);
cmd_help(self, m, NULL); cmd_help(self, m, NULL);
wclrtoeol(self->window); wclrtoeol(self->window);
} }
ToxWindow new_prompt(friendAddedFn *f) ToxWindow new_prompt(friendAddedFn *f)
{ {
on_friendadded_cb = f; on_friendadded_cb = f;
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
ret.onKey = &prompt_onKey; ret.onKey = &prompt_onKey;
ret.onDraw = &prompt_onDraw; ret.onDraw = &prompt_onDraw;
ret.onInit = &prompt_onInit; ret.onInit = &prompt_onInit;
strcpy(ret.title, "[prompt]"); strcpy(ret.title, "[prompt]");
return ret; return ret;
} }

336
windows.c
View File

@ -14,232 +14,258 @@ static ToxWindow windows[MAX_WINDOW_SLOTS];
static Messenger *m; static Messenger *m;
int active_window; int active_window;
static ToxWindow* prompt; static ToxWindow *prompt;
/* CALLBACKS START */ /* CALLBACKS START */
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata) void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
{ {
int n = add_req(public_key); int n = add_req(public_key);
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) {
wprintw(prompt->window, "%02x", public_key[i] & 0xff);
}
wprintw(prompt->window, "\nWith the message: %s\n", data); for (i = 0; i < KEY_SIZE_BYTES; ++i) {
wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); wprintw(prompt->window, "%02x", public_key[i] & 0xff);
}
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { wprintw(prompt->window, "\nWith the message: %s\n", data);
if (windows[i].onFriendRequest != NULL) wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
windows[i].onFriendRequest(&windows[i], public_key, data, length);
} for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onFriendRequest != NULL)
windows[i].onFriendRequest(&windows[i], public_key, data, length);
}
} }
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) {
if (windows[i].onMessage != NULL) for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
windows[i].onMessage(&windows[i], m, friendnumber, string, length); if (windows[i].onMessage != NULL)
} windows[i].onMessage(&windows[i], m, friendnumber, string, 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) {
if (windows[i].onAction != NULL) for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
windows[i].onAction(&windows[i], m, friendnumber, string, length); if (windows[i].onAction != NULL)
} windows[i].onAction(&windows[i], m, friendnumber, string, length);
}
} }
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{ {
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) {
if (windows[i].onNickChange != NULL) for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
windows[i].onNickChange(&windows[i], friendnumber, string, length); if (windows[i].onNickChange != NULL)
} windows[i].onNickChange(&windows[i], friendnumber, string, length);
}
} }
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{ {
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) {
if (windows[i].onStatusChange != NULL) for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
windows[i].onStatusChange(&windows[i], friendnumber, string, length); if (windows[i].onStatusChange != NULL)
} windows[i].onStatusChange(&windows[i], friendnumber, string, length);
}
} }
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) {
wprintw(prompt->window, "\nCould not store Messenger data\n"); if (store_data(m, DATA_FILE) != 0) {
} wprintw(prompt->window, "\nCould not store Messenger data\n");
}
} }
/* CALLBACKS END */ /* CALLBACKS END */
int add_window(Messenger *m, ToxWindow w, int n) int add_window(Messenger *m, ToxWindow w, int n)
{ {
if (w_num >= TOXWINDOWS_MAX_NUM) if (w_num >= TOXWINDOWS_MAX_NUM)
return -1; return -1;
if (LINES < 2) if (LINES < 2)
return -1; return -1;
w.window = newwin(LINES - 2, COLS, 0, 0); w.window = newwin(LINES - 2, COLS, 0, 0);
if (w.window == NULL)
return -1;
windows[n] = w; if (w.window == NULL)
w.onInit(&w, m); return -1;
w_num++;
active_window = n; windows[n] = w;
return n; w.onInit(&w, m);
w_num++;
active_window = n;
return n;
} }
/* Deletes window w and cleans up */ /* Deletes window w and cleans up */
void del_window(ToxWindow *w, int f_num) 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) {
if (WINDOW_STATUS[i] == f_num) { for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
WINDOW_STATUS[i] = -1; if (WINDOW_STATUS[i] == f_num) {
disable_chatwin(f_num); WINDOW_STATUS[i] = -1;
break; disable_chatwin(f_num);
break;
}
} }
}
clear(); clear();
refresh(); refresh();
} }
/* Shows next window when tab or back-tab is pressed */ /* Shows next window when tab or back-tab is pressed */
void set_active_window(int ch) 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') {
int i = (active_window + 1) % max; if (ch == '\t') {
while (true) { int i = (active_window + 1) % max;
if (WINDOW_STATUS[i] != -1) {
active_window = i; while (true) {
return; if (WINDOW_STATUS[i] != -1) {
} active_window = i;
i = (i + 1) % max; return;
if (f_inf++ > max) { // infinite loop check }
endwin();
exit(2); i = (i + 1) % max;
}
if (f_inf++ > max) { // infinite loop check
endwin();
exit(2);
}
}
} else {
int i = active_window - 1;
if (i < 0) i = max;
while (true) {
if (WINDOW_STATUS[i] != -1) {
active_window = i;
return;
}
if (--i < 0) i = max;
if (f_inf++ > max) {
endwin();
exit(2);
}
}
} }
}else {
int i = active_window - 1;
if (i < 0) i = max;
while (true) {
if (WINDOW_STATUS[i] != -1) {
active_window = i;
return;
}
if (--i < 0) i = max;
if (f_inf++ > max) {
endwin();
exit(2);
}
}
}
} }
void init_window_status() 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)
WINDOW_STATUS[i] = -(i+2);
int j; for (i = 0; i < N_DEFAULT_WINS; ++i)
for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++) WINDOW_STATUS[i] = -(i + 2);
WINDOW_STATUS[j] = -1;
int j;
for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
WINDOW_STATUS[j] = -1;
} }
ToxWindow *init_windows() ToxWindow *init_windows()
{ {
w_num = 0; w_num = 0;
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
|| add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1 if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1
|| add_window(m, new_dhtstatus(), n_dhtstatus) == -1) { || add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1
fprintf(stderr, "add_window() failed.\n"); || add_window(m, new_dhtstatus(), n_dhtstatus) == -1) {
endwin(); fprintf(stderr, "add_window() failed.\n");
exit(1); endwin();
} exit(1);
active_window = n_prompt; }
prompt = &windows[n_prompt];
return prompt; active_window = n_prompt;
prompt = &windows[n_prompt];
return prompt;
} }
static void draw_bar() static void draw_bar()
{ {
static int odd = 0; static int odd = 0;
int blinkrate = 30; int blinkrate = 30;
attron(COLOR_PAIR(4)); attron(COLOR_PAIR(4));
mvhline(LINES - 2, 0, '_', COLS); mvhline(LINES - 2, 0, '_', COLS);
attroff(COLOR_PAIR(4)); attroff(COLOR_PAIR(4));
move(LINES - 1, 0); move(LINES - 1, 0);
attron(COLOR_PAIR(4) | A_BOLD); attron(COLOR_PAIR(4) | A_BOLD);
printw(" TOXIC " TOXICVER "|"); printw(" TOXIC " TOXICVER "|");
attroff(COLOR_PAIR(4) | A_BOLD); attroff(COLOR_PAIR(4) | A_BOLD);
int i; int i;
for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
if (WINDOW_STATUS[i] != -1) {
if (i == active_window)
attron(A_BOLD);
odd = (odd+1) % blinkrate; for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
if (windows[i].blink && (odd < (blinkrate/2))) if (WINDOW_STATUS[i] != -1) {
attron(COLOR_PAIR(3)); if (i == active_window)
attron(A_BOLD);
printw(" %s", windows[i].title); odd = (odd + 1) % blinkrate;
if (windows[i].blink && (odd < (blinkrate/2)))
attroff(COLOR_PAIR(3));
if (i == active_window) { if (windows[i].blink && (odd < (blinkrate / 2)))
attroff(A_BOLD); attron(COLOR_PAIR(3));
}
printw(" %s", windows[i].title);
if (windows[i].blink && (odd < (blinkrate / 2)))
attroff(COLOR_PAIR(3));
if (i == active_window) {
attroff(A_BOLD);
}
}
} }
}
refresh(); refresh();
} }
void prepare_window(WINDOW *w) void prepare_window(WINDOW *w)
{ {
mvwin(w, 0, 0); mvwin(w, 0, 0);
wresize(w, LINES-2, COLS); wresize(w, LINES - 2, COLS);
} }
void draw_active_window(Messenger *m) void draw_active_window(Messenger *m)
{ {
ToxWindow *a = &windows[active_window]; ToxWindow *a = &windows[active_window];
prepare_window(a->window); prepare_window(a->window);
a->blink = false; a->blink = false;
draw_bar(); draw_bar();
a->onDraw(a); a->onDraw(a);
/* Handle input */ /* Handle input */
int ch = getch(); int ch = getch();
if (ch == '\t' || ch == KEY_BTAB)
set_active_window(ch); if (ch == '\t' || ch == KEY_BTAB)
else if (ch != ERR) set_active_window(ch);
a->onKey(a, m, ch); else if (ch != ERR)
a->onKey(a, m, ch);
} }

View File

@ -26,31 +26,31 @@
typedef struct ToxWindow_ ToxWindow; typedef struct ToxWindow_ ToxWindow;
struct ToxWindow_ { struct ToxWindow_ {
void(*onKey)(ToxWindow*, Messenger*, int); void(*onKey)(ToxWindow *, Messenger *, int);
void(*onDraw)(ToxWindow*); void(*onDraw)(ToxWindow *);
void(*onInit)(ToxWindow*, Messenger*); void(*onInit)(ToxWindow *, Messenger *);
void(*onFriendRequest)(ToxWindow*, uint8_t*, uint8_t*, uint16_t); void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t);
void(*onMessage)(ToxWindow*, Messenger*, int, uint8_t*, uint16_t); void(*onMessage)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t);
void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t); void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t); void(*onStatusChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onAction)(ToxWindow*, Messenger*, int, uint8_t*, uint16_t); void(*onAction)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t);
char title[256]; char title[256];
void* x; void *x;
bool blink; bool blink;
WINDOW* window; WINDOW *window;
}; };
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata); void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata);
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);
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);
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_friendadded(Messenger *m, int friendnumber); void on_friendadded(Messenger *m, int friendnumber);
void init_window_status(); void init_window_status();
ToxWindow * init_windows(); ToxWindow *init_windows();
void draw_active_window(Messenger * m); void draw_active_window(Messenger *m);
int add_window(Messenger *m, ToxWindow w, int n); int add_window(Messenger *m, ToxWindow w, int n);
void del_window(ToxWindow *w, int f_num); void del_window(ToxWindow *w, int f_num);
void set_active_window(int ch); void set_active_window(int ch);