From 85d15bb45f86a9b31f6d4dc3693eee96c83703e7 Mon Sep 17 00:00:00 2001 From: notadecent Date: Fri, 23 Aug 2013 18:52:30 +0200 Subject: [PATCH 01/82] Update README.md Spelling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index de575f8..94515a9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## Toxic - console client for [Tox](http://tox.im) -The client formerly resided in the [Tox core repository](https://github.com/irungentoo/ProjectTox-Core) and is now available as a standalone verion. +The client formerly resided in the [Tox core repository](https://github.com/irungentoo/ProjectTox-Core) and is now available as a standalone version. To compile, first generate the configure script by running the ```autoreconf -i``` command. From 9ab34ca5dd7b4328f218fad07e3e548da95ea732 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 23 Aug 2013 17:25:08 -0400 Subject: [PATCH 02/82] resolve_addr() was removed from core --- src/main.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/main.c b/src/main.c index 119ab99..688cf5c 100644 --- a/src/main.c +++ b/src/main.c @@ -91,6 +91,46 @@ static Messenger *init_tox() return m; } +/* + resolve_addr(): + address should represent IPv4 or a hostname with A record + + returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i + returns 0 on failure + + TODO: Fix ipv6 support +*/ +uint32_t resolve_addr(const char *address) +{ + struct addrinfo *server = NULL; + struct addrinfo hints; + int rc; + uint32_t addr; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; // IPv4 only right now. + hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. + + rc = getaddrinfo(address, "echo", &hints, &server); + + // Lookup failed. + if (rc != 0) { + return 0; + } + + // IPv4 records only.. + if (server->ai_family != AF_INET) { + freeaddrinfo(server); + return 0; + } + + + addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; + + freeaddrinfo(server); + return addr; +} + #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ #define MINLINE 70 #define MAXSERVERS 50 From baadd95b458e9cba041446282c67ede73199de5e Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Fri, 23 Aug 2013 23:03:44 +0200 Subject: [PATCH 03/82] Use new public api --- src/chat.c | 41 ++++++++-------- src/chat.h | 2 +- src/dhtstatus.c | 19 ++++---- src/friendlist.c | 21 ++++---- src/friendlist.h | 2 +- src/main.c | 92 +++++++++++++++++++++++++++--------- src/prompt.c | 113 +++++++++++++++++++++----------------------- src/toxic_windows.h | 28 ++++++----- src/windows.c | 23 +++++---- 9 files changed, 195 insertions(+), 146 deletions(-) diff --git a/src/chat.c b/src/chat.c index 30b8d95..9490233 100644 --- a/src/chat.c +++ b/src/chat.c @@ -13,7 +13,6 @@ #include #include -#include "Messenger.h" #include "network.h" #include "toxic_windows.h" @@ -31,7 +30,7 @@ typedef struct { } ChatContext; void print_help(ChatContext *self); -void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd); +void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd); struct tm *get_time(void) { @@ -42,10 +41,10 @@ struct tm *get_time(void) return timeinfo; } -static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len) +static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) { ChatContext *ctx = (ChatContext *) self->x; - uint8_t nick[MAX_NAME_LENGTH] = {0}; + uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; struct tm *timeinfo = get_time(); if (ctx->friendnum != num) @@ -53,7 +52,7 @@ static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, getname(m, num, (uint8_t *) &nick); msg[len - 1] = '\0'; - nick[MAX_NAME_LENGTH - 1] = '\0'; + nick[TOX_MAX_NAME_LENGTH - 1] = '\0'; wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); @@ -67,7 +66,7 @@ static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, beep(); } -static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len) +static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len) { ChatContext *ctx = (ChatContext *) self->x; struct tm *timeinfo = get_time(); @@ -176,7 +175,7 @@ static char *wc_to_char(wchar_t ch) return ret; } -static void chat_onKey(ToxWindow *self, Messenger *m, wint_t key) +static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) { ChatContext *ctx = (ChatContext *) self->x; struct tm *timeinfo = get_time(); @@ -222,7 +221,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, wint_t key) else { /* make sure the string has at least non-space character */ if (!string_is_empty(line)) { - uint8_t selfname[MAX_NAME_LENGTH]; + uint8_t selfname[TOX_MAX_NAME_LENGTH]; getself_name(m, selfname, sizeof(selfname)); wattron(ctx->history, COLOR_PAIR(2)); @@ -247,7 +246,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, wint_t key) } } -void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) +void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { wclear(self->window); @@ -281,7 +280,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) 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]; + uint8_t selfname[TOX_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); @@ -308,20 +307,20 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) } status++; - USERSTATUS status_kind; + TOX_USERSTATUS status_kind; if (!strncmp(status, "online", strlen("online"))) { - status_kind = USERSTATUS_NONE; + status_kind = TOX_USERSTATUS_NONE; status_text = "ONLINE"; } else if (!strncmp(status, "away", strlen("away"))) { - status_kind = USERSTATUS_AWAY; + status_kind = TOX_USERSTATUS_AWAY; status_text = "AWAY"; } else if (!strncmp(status, "busy", strlen("busy"))) { - status_kind = USERSTATUS_BUSY; + status_kind = TOX_USERSTATUS_BUSY; status_text = "BUSY"; } @@ -358,12 +357,12 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) } else if (!strcmp(cmd, "/myid")) { - char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; + char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; int i; - uint8_t address[FRIEND_ADDRESS_SIZE]; + uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; getaddress(m, address); - for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { + for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) { char xx[3]; snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); strcat(id, xx); @@ -383,7 +382,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) wprintw(ctx->history, "Invalid command.\n"); } -static void chat_onDraw(ToxWindow *self, Messenger *m) +static void chat_onDraw(ToxWindow *self, Tox *m) { curs_set(1); int x, y; @@ -394,7 +393,7 @@ static void chat_onDraw(ToxWindow *self, Messenger *m) wrefresh(self->window); } -static void chat_onInit(ToxWindow *self, Messenger *m) +static void chat_onInit(ToxWindow *self, Tox *m) { int x, y; ChatContext *ctx = (ChatContext *) self->x; @@ -424,7 +423,7 @@ void print_help(ChatContext *self) wattroff(self->history, COLOR_PAIR(2)); } -ToxWindow new_chat(Messenger *m, int friendnum) +ToxWindow new_chat(Tox *m, int friendnum) { ToxWindow ret; memset(&ret, 0, sizeof(ret)); @@ -437,7 +436,7 @@ ToxWindow new_chat(Messenger *m, int friendnum) ret.onStatusChange = &chat_onStatusChange; ret.onAction = &chat_onAction; - uint8_t nick[MAX_NAME_LENGTH] = {0}; + uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; getname(m, friendnum, (uint8_t *) &nick); snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); diff --git a/src/chat.h b/src/chat.h index 7599d46..d222b0d 100644 --- a/src/chat.h +++ b/src/chat.h @@ -1,6 +1,6 @@ #ifndef CHAT_H_6489PZ13 #define CHAT_H_6489PZ13 -ToxWindow new_chat(Messenger *m, int friendnum); +ToxWindow new_chat(Tox *m, int friendnum); #endif /* end of include guard: CHAT_H_6489PZ13 */ diff --git a/src/dhtstatus.c b/src/dhtstatus.c index 8bd98b7..5df5666 100644 --- a/src/dhtstatus.c +++ b/src/dhtstatus.c @@ -4,30 +4,28 @@ #include "dhtstatus.h" #include "string.h" -#include "network.h" -#include "DHT.h" typedef uint8_t ipbuf[3 * 4 + 3 + 1]; static int num_selected = 0; -static void printip(ipbuf buf, IP ip) +static void printip(ipbuf buf, tox_IP ip) { 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, wint_t key) +static void dhtstatus_onKey(ToxWindow *self, Tox *m, wint_t key) { switch (key) { case KEY_UP: case 'k': if (--num_selected < 0) - num_selected = CLIENT_ID_SIZE - 1; + num_selected = TOX_CLIENT_ID_SIZE - 1; break; case KEY_DOWN: case 'j': - num_selected = (num_selected + 1) % CLIENT_ID_SIZE; + num_selected = (num_selected + 1) % TOX_CLIENT_ID_SIZE; break; case '\n': @@ -38,9 +36,10 @@ static void dhtstatus_onKey(ToxWindow *self, Messenger *m, wint_t key) } } -static void dhtstatus_onDraw(ToxWindow *self, Messenger *m) +static void dhtstatus_onDraw(ToxWindow *self, Tox *m) { - Client_data *close_clientlist = DHT_get_close_list(m->dht); + /* + Client_data *close_clientlist = DHT_get_close_list(m->dht); curs_set(0); werase(self->window); @@ -52,6 +51,7 @@ static void dhtstatus_onDraw(ToxWindow *self, Messenger *m) now); for (i = 0; i < 32; i++) { /*Number of nodes in closelist*/ + /* Client_data *client = close_clientlist + i; if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); @@ -82,9 +82,10 @@ static void dhtstatus_onDraw(ToxWindow *self, Messenger *m) } wrefresh(self->window); + */ } -static void dhtstatus_onInit(ToxWindow *self, Messenger *m) +static void dhtstatus_onInit(ToxWindow *self, Tox *m) { } diff --git a/src/friendlist.c b/src/friendlist.c index 548c222..ec6d970 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -10,15 +10,14 @@ #include #include -#include "Messenger.h" -#include "network.h" +#include "tox.h" #include "friendlist.h" typedef struct { - uint8_t name[MAX_NAME_LENGTH]; - uint8_t status[MAX_STATUSMESSAGE_LENGTH]; + uint8_t name[TOX_MAX_NAME_LENGTH]; + uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH]; int num; int chatwin; } friend_t; @@ -28,7 +27,7 @@ static int num_friends = 0; static int num_selected = 0; -void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len) +void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) { if (num >= num_friends) return; @@ -40,7 +39,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { - if (len >= MAX_NAME_LENGTH || num >= num_friends) + if (len >= TOX_MAX_NAME_LENGTH || num >= num_friends) return; memcpy((char *) &friends[num].name, (char *) str, len); @@ -49,14 +48,14 @@ void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t le void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { - if (len >= MAX_STATUSMESSAGE_LENGTH || num >= num_friends) + if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends) return; memcpy((char *) &friends[num].status, (char *) str, len); friends[num].status[len] = 0; } -int friendlist_onFriendAdded(Messenger *m, int num) +int friendlist_onFriendAdded(Tox *m, int num) { if (num_friends == MAX_FRIENDS_NUM) return -1; @@ -69,7 +68,7 @@ int friendlist_onFriendAdded(Messenger *m, int num) return 0; } -static void friendlist_onKey(ToxWindow *self, Messenger *m, wint_t key) +static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key) { if (key == KEY_UP) { if (--num_selected < 0) @@ -87,7 +86,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, wint_t key) } } -static void friendlist_onDraw(ToxWindow *self, Messenger *m) +static void friendlist_onDraw(ToxWindow *self, Tox *m) { curs_set(0); werase(self->window); @@ -125,7 +124,7 @@ void disable_chatwin(int f_num) friends[f_num].chatwin = -1; } -static void friendlist_onInit(ToxWindow *self, Messenger *m) +static void friendlist_onInit(ToxWindow *self, Tox *m) { } diff --git a/src/friendlist.h b/src/friendlist.h index 3a5d393..26c9106 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -5,7 +5,7 @@ #include "chat.h" ToxWindow new_friendlist(); -int friendlist_onFriendAdded(Messenger *m, int num); +int friendlist_onFriendAdded(Tox *m, int num); void disable_chatwin(int f_num); #endif /* end of include guard: FRIENDLIST_H_53I41IM */ diff --git a/src/main.c b/src/main.c index 688cf5c..b3005cb 100644 --- a/src/main.c +++ b/src/main.c @@ -14,16 +14,18 @@ #include #include #include +#include +#include #ifdef _win32 #include #else #include #include +#include #endif -#include "Messenger.h" -#include "network.h" +#include "tox.h" #include "configdir.h" #include "toxic_windows.h" @@ -34,6 +36,50 @@ char *DATA_FILE = NULL; char *SRVLIST_FILE = NULL; +static Tox *tox; + + +/* +resolve_addr(): +address should represent IPv4 or a hostname with A record + +returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i +returns 0 on failure + +TODO: Fix ipv6 support +*/ + +uint32_t resolve_addr(const char *address) +{ + struct addrinfo *server = NULL; + struct addrinfo hints; + int rc; + uint32_t addr; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; // IPv4 only right now. + hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. + + rc = getaddrinfo(address, "echo", &hints, &server); + + // Lookup failed. + if (rc != 0) { + return 0; + } + + // IPv4 records only.. + if (server->ai_family != AF_INET) { + freeaddrinfo(server); + return 0; + } + + + addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; + + freeaddrinfo(server); + return addr; +} + void on_window_resize(int sig) { endwin(); @@ -68,10 +114,10 @@ static void init_term() refresh(); } -static Messenger *init_tox() +static Tox *init_tox() { /* Init core */ - Messenger *m = initMessenger(); + Tox *m = tox_new(); /* Callbacks */ m_callback_friendrequest(m, on_request, NULL); @@ -136,11 +182,11 @@ uint32_t resolve_addr(const char *address) #define MAXSERVERS 50 /* Connects to a random DHT server listed in the DHTservers file */ -int init_connection(Messenger *m) +int init_connection(Tox *m) { FILE *fp = NULL; - if (DHT_isconnected(m->dht)) + if (tox_isconnected(m)) return 0; fp = fopen(SRVLIST_FILE, "r"); @@ -172,7 +218,7 @@ int init_connection(Messenger *m) if (!ip || !port || !key) return 3; - IP_Port dht; + tox_IP_Port dht; dht.port = htons(atoi(port)); uint32_t resolved_address = resolve_addr(ip); @@ -180,19 +226,19 @@ int init_connection(Messenger *m) return 0; dht.ip.i = resolved_address; - unsigned char *binary_string = hex_string_to_bin(key); - DHT_bootstrap(m->dht, dht, binary_string); + uint8_t *binary_string = hex_string_to_bin(key); + tox_bootstrap(m, dht, binary_string); free(binary_string); return 0; } -static void do_tox(Messenger *m, ToxWindow *prompt) +static void do_tox(Tox *m, ToxWindow *prompt) { static int conn_try = 0; static int conn_err = 0; static bool dht_on = false; - if (!dht_on && !DHT_isconnected(m->dht) && !(conn_try++ % 100)) { + if (!dht_on && !tox_isconnected(m) && !(conn_try++ % 100)) { if (!conn_err) { conn_err = init_connection(m); wprintw(prompt->window, "\nEstablishing connection...\n"); @@ -200,15 +246,15 @@ static void do_tox(Messenger *m, ToxWindow *prompt) if (conn_err) wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err); } - } else if (!dht_on && DHT_isconnected(m->dht)) { + } else if (!dht_on && tox_isconnected(m)) { dht_on = true; wprintw(prompt->window, "\nDHT connected.\n"); - } else if (dht_on && !DHT_isconnected(m->dht)) { + } else if (dht_on && !tox_isconnected(m)) { dht_on = false; wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); } - doMessenger(m); + tox_do(m); } int f_loadfromfile; @@ -220,7 +266,7 @@ int f_loadfromfile; * Return 2 opening path failed * Return 3 fwrite failed */ -int store_data(Messenger *m, char *path) +int store_data(Tox *m, char *path) { if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ return 0; @@ -229,14 +275,14 @@ int store_data(Messenger *m, char *path) size_t len; uint8_t *buf; - len = Messenger_size(m); + len = tox_size(m); buf = malloc(len); if (buf == NULL) { return 1; } - Messenger_save(m, buf); + tox_save(m, buf); fd = fopen(path, "w"); @@ -256,7 +302,7 @@ int store_data(Messenger *m, char *path) return 0; } -static void load_data(Messenger *m, char *path) +static void load_data(Tox *m, char *path) { if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ return; @@ -287,12 +333,14 @@ static void load_data(Messenger *m, char *path) exit(1); } - Messenger_load(m, buf, len); + tox_load(m, buf, len); uint32_t i; - for (i = 0; i < m->numfriends; i++) { + char name[TOX_MAX_NAME_LENGTH]; + while (tox_getname(m, i, (uint8_t *)name) != -1) { on_friendadded(m, i); + i++; } free(buf); @@ -354,7 +402,7 @@ int main(int argc, char *argv[]) free(user_config_dir); init_term(); - Messenger *m = init_tox(); + Tox *m = init_tox(); ToxWindow *prompt = init_windows(m); if (f_loadfromfile) @@ -382,7 +430,7 @@ int main(int argc, char *argv[]) draw_active_window(m); } - cleanupMessenger(m); + tox_kill(m); free(DATA_FILE); free(SRVLIST_FILE); return 0; diff --git a/src/prompt.c b/src/prompt.c index 887c486..50e0d4c 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -10,40 +10,37 @@ #include #include -#include "Messenger.h" -#include "network.h" - #include "prompt.h" extern char *DATA_FILE; -extern int store_data(Messenger *m, char *path); +extern int store_data(Tox *m, char *path); -uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX +uint8_t pending_requests[MAX_STR_SIZE][TOX_CLIENT_ID_SIZE]; // XXX uint8_t num_requests = 0; // XXX static char prompt_buf[MAX_STR_SIZE] = {0}; static int prompt_buf_pos = 0; /* commands */ -void cmd_accept(ToxWindow *, Messenger *m, char **); -void cmd_add(ToxWindow *, Messenger *m, char **); -void cmd_clear(ToxWindow *, Messenger *m, char **); -void cmd_connect(ToxWindow *, Messenger *m, char **); -void cmd_help(ToxWindow *, Messenger *m, char **); -void cmd_msg(ToxWindow *, Messenger *m, char **); -void cmd_myid(ToxWindow *, Messenger *m, char **); -void cmd_nick(ToxWindow *, Messenger *m, char **); -void cmd_mynick(ToxWindow *, Messenger *m, char **); -void cmd_quit(ToxWindow *, Messenger *m, char **); -void cmd_status(ToxWindow *, Messenger *m, char **); -void cmd_statusmsg(ToxWindow *, Messenger *m, char **); +void cmd_accept(ToxWindow *, Tox *m, char **); +void cmd_add(ToxWindow *, Tox *m, char **); +void cmd_clear(ToxWindow *, Tox *m, char **); +void cmd_connect(ToxWindow *, Tox *m, char **); +void cmd_help(ToxWindow *, Tox *m, char **); +void cmd_msg(ToxWindow *, Tox *m, char **); +void cmd_myid(ToxWindow *, Tox *m, char **); +void cmd_nick(ToxWindow *, Tox *m, char **); +void cmd_mynick(ToxWindow *, Tox *m, char **); +void cmd_quit(ToxWindow *, Tox *m, char **); +void cmd_status(ToxWindow *, Tox *m, char **); +void cmd_statusmsg(ToxWindow *, Tox *m, char **); #define NUM_COMMANDS 14 static struct { char *name; int numargs; - void (*func)(ToxWindow *, Messenger *m, char **); + void (*func)(ToxWindow *, Tox *m, char **); } commands[] = { { "accept", 1, cmd_accept }, { "add", 1, cmd_add }, @@ -64,7 +61,7 @@ static struct { // XXX: int add_req(uint8_t *public_key) { - memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); + memcpy(pending_requests[num_requests], public_key, TOX_CLIENT_ID_SIZE); ++num_requests; return num_requests - 1; } @@ -83,7 +80,7 @@ unsigned char *hex_string_to_bin(char hex_string[]) return val; } -void cmd_accept(ToxWindow *self, Messenger *m, char **args) +void cmd_accept(ToxWindow *self, Tox *m, char **args) { int num = atoi(args[1]); @@ -102,9 +99,9 @@ void cmd_accept(ToxWindow *self, Messenger *m, char **args) } } -void cmd_add(ToxWindow *self, Messenger *m, char **args) +void cmd_add(ToxWindow *self, Tox *m, char **args) { - uint8_t id_bin[FRIEND_ADDRESS_SIZE]; + uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE]; char xx[3]; uint32_t x; char *id = args[1]; @@ -118,14 +115,14 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args) if (!msg) msg = ""; - if (strlen(id) != 2 * FRIEND_ADDRESS_SIZE) { + if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) { wprintw(self->window, "Invalid ID length.\n"); return; } int i; - for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) { + for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) { xx[0] = id[2 * i]; xx[1] = id[2 * i + 1]; xx[2] = '\0'; @@ -138,38 +135,38 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args) id_bin[i] = x; } - for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { + for (i = 0; i < TOX_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: + case TOX_FAERR_TOOLONG: wprintw(self->window, "Message is too long.\n"); break; - case FAERR_NOMESSAGE: + case TOX_FAERR_NOMESSAGE: wprintw(self->window, "Please add a message to your request.\n"); break; - case FAERR_OWNKEY: + case TOX_FAERR_OWNKEY: wprintw(self->window, "That appears to be your own ID.\n"); break; - case FAERR_ALREADYSENT: + case TOX_FAERR_ALREADYSENT: wprintw(self->window, "Friend request already sent.\n"); break; - case FAERR_UNKNOWN: + case TOX_FAERR_UNKNOWN: wprintw(self->window, "Undefined error when adding friend.\n"); break; - case FAERR_BADCHECKSUM: + case TOX_FAERR_BADCHECKSUM: wprintw(self->window, "Bad checksum in address.\n"); break; - case FAERR_SETNEWNOSPAM: + case TOX_FAERR_SETNEWNOSPAM: wprintw(self->window, "Nospam was different.\n"); break; @@ -180,14 +177,14 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args) } } -void cmd_clear(ToxWindow *self, Messenger *m, char **args) +void cmd_clear(ToxWindow *self, Tox *m, char **args) { wclear(self->window); } -void cmd_connect(ToxWindow *self, Messenger *m, char **args) +void cmd_connect(ToxWindow *self, Tox *m, char **args) { - IP_Port dht; + tox_IP_Port dht; char *ip = args[1]; char *port = args[2]; char *key = args[3]; @@ -205,18 +202,18 @@ void cmd_connect(ToxWindow *self, Messenger *m, char **args) } dht.ip.i = resolved_address; - unsigned char *binary_string = hex_string_to_bin(key); - DHT_bootstrap(m->dht, dht, binary_string); + uint8_t *binary_string = hex_string_to_bin(key); + tox_bootstrap(m, dht, binary_string); free(binary_string); } -void cmd_quit(ToxWindow *self, Messenger *m, char **args) +void cmd_quit(ToxWindow *self, Tox *m, char **args) { endwin(); exit(0); } -void cmd_help(ToxWindow *self, Messenger *m, char **args) +void cmd_help(ToxWindow *self, Tox *m, char **args) { wclear(self->window); wattron(self->window, COLOR_PAIR(2) | A_BOLD); @@ -242,7 +239,7 @@ void cmd_help(ToxWindow *self, Messenger *m, char **args) wattroff(self->window, COLOR_PAIR(2)); } -void cmd_msg(ToxWindow *self, Messenger *m, char **args) +void cmd_msg(ToxWindow *self, Tox *m, char **args) { char *id = args[1]; char *msg = args[2]; @@ -253,14 +250,14 @@ void cmd_msg(ToxWindow *self, Messenger *m, char **args) wprintw(self->window, "Message successfully sent.\n"); } -void cmd_myid(ToxWindow *self, Messenger *m, char **args) +void cmd_myid(ToxWindow *self, Tox *m, char **args) { - char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; + char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; size_t i; - uint8_t address[FRIEND_ADDRESS_SIZE]; + uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; getaddress(m, address); - for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) { + for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) { char xx[3]; snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); strcat(id, xx); @@ -269,7 +266,7 @@ void cmd_myid(ToxWindow *self, Messenger *m, char **args) wprintw(self->window, "%s\n", id); } -void cmd_nick(ToxWindow *self, Messenger *m, char **args) +void cmd_nick(ToxWindow *self, Tox *m, char **args) { char *nick = args[1]; setname(m, (uint8_t *) nick, strlen(nick) + 1); @@ -280,29 +277,29 @@ void cmd_nick(ToxWindow *self, Messenger *m, char **args) } } -void cmd_mynick(ToxWindow *self, Messenger *m, char **args) +void cmd_mynick(ToxWindow *self, Tox *m, char **args) { - uint8_t *nick = malloc(m->name_length); - getself_name(m, nick, m->name_length); + uint8_t *nick = malloc(TOX_MAX_NAME_LENGTH); + tox_getselfname(m, nick, TOX_MAX_NAME_LENGTH); wprintw(self->window, "Current nickname: %s\n", nick); free(nick); } -void cmd_status(ToxWindow *self, Messenger *m, char **args) +void cmd_status(ToxWindow *self, Tox *m, char **args) { char *status = args[1]; char *status_text; - USERSTATUS status_kind; + TOX_USERSTATUS status_kind; if (!strncmp(status, "online", strlen("online"))) { - status_kind = USERSTATUS_NONE; + status_kind = TOX_USERSTATUS_NONE; status_text = "ONLINE"; } else if (!strncmp(status, "away", strlen("away"))) { - status_kind = USERSTATUS_AWAY; + status_kind = TOX_USERSTATUS_AWAY; status_text = "AWAY"; } else if (!strncmp(status, "busy", strlen("busy"))) { - status_kind = USERSTATUS_BUSY; + status_kind = TOX_USERSTATUS_BUSY; status_text = "BUSY"; } else { wprintw(self->window, "Invalid status.\n"); @@ -321,14 +318,14 @@ void cmd_status(ToxWindow *self, Messenger *m, char **args) } } -void cmd_statusmsg(ToxWindow *self, Messenger *m, char **args) +void cmd_statusmsg(ToxWindow *self, Tox *m, char **args) { char *msg = args[1]; m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); wprintw(self->window, "Status set to: %s\n", msg); } -static void execute(ToxWindow *self, Messenger *m, char *u_cmd) +static void execute(ToxWindow *self, Tox *m, char *u_cmd) { int newlines = 0; char cmd[MAX_STR_SIZE] = {0}; @@ -436,7 +433,7 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd) wprintw(self->window, "Invalid command.\n"); } -static void prompt_onKey(ToxWindow *self, Messenger *m, wint_t key) +static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) { /* Add printable characters to line */ if (isprint(key)) { @@ -472,7 +469,7 @@ static void prompt_onKey(ToxWindow *self, Messenger *m, wint_t key) } } -static void prompt_onDraw(ToxWindow *self, Messenger *m) +static void prompt_onDraw(ToxWindow *self, Tox *m) { curs_set(1); int x, y; @@ -493,7 +490,7 @@ static void prompt_onDraw(ToxWindow *self, Messenger *m) wrefresh(self->window); } -static void prompt_onInit(ToxWindow *self, Messenger *m) +static void prompt_onInit(ToxWindow *self, Tox *m) { scrollok(self->window, 1); cmd_help(self, m, NULL); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 4b1abf1..34edf6f 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -9,7 +9,9 @@ #include #include #include -#include "Messenger.h" + +#include "tox.h" + #define MAX_WINDOWS_NUM 32 #define MAX_FRIENDS_NUM 100 #define MAX_STR_SIZE 256 @@ -25,14 +27,14 @@ typedef struct ToxWindow_ ToxWindow; struct ToxWindow_ { - void(*onKey)(ToxWindow *, Messenger *, wint_t); - void(*onDraw)(ToxWindow *, Messenger *); - void(*onInit)(ToxWindow *, Messenger *); + void(*onKey)(ToxWindow *, Tox *, wint_t); + void(*onDraw)(ToxWindow *, Tox *); + void(*onInit)(ToxWindow *, Tox *); void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); - void(*onMessage)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t); + void(*onMessage)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onNickChange)(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 *, Tox *, int, uint8_t *, uint16_t); char title[256]; void *x; @@ -42,14 +44,14 @@ struct ToxWindow_ { }; 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_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_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); -void on_friendadded(Messenger *m, int friendnumber); +void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); +void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); +void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); +void on_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); +void on_friendadded(Tox *m, int friendnumber); ToxWindow *init_windows(); -void draw_active_window(Messenger *m); -int add_window(Messenger *m, ToxWindow w); +void draw_active_window(Tox *m); +int add_window(Tox *m, ToxWindow w); void del_window(ToxWindow *w); void set_active_window(int ch); #endif diff --git a/src/windows.c b/src/windows.c index 6ce234e..d0852e3 100644 --- a/src/windows.c +++ b/src/windows.c @@ -2,18 +2,21 @@ #include "config.h" #endif +#include +#include + #include "friendlist.h" #include "prompt.h" #include "dhtstatus.h" #include "toxic_windows.h" extern char *DATA_FILE; -extern int store_data(Messenger *m, char *path); +extern int store_data(Tox *m, char *path); static ToxWindow windows[MAX_WINDOWS_NUM]; static ToxWindow *active_window; static ToxWindow *prompt; -static Messenger *m; +static Tox *m; /* CALLBACKS START */ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) @@ -36,7 +39,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(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { int i; @@ -46,7 +49,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(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { int i; @@ -56,7 +59,7 @@ void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, } } -void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) +void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); int i; @@ -67,7 +70,7 @@ void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t len } } -void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) +void on_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); int i; @@ -78,17 +81,17 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l } } -void on_friendadded(Messenger *m, int friendnumber) +void on_friendadded(Tox *m, int friendnumber) { friendlist_onFriendAdded(m, friendnumber); if (store_data(m, DATA_FILE)) { - wprintw(prompt->window, "\nCould not store Messenger data\n"); + wprintw(prompt->window, "\nCould not store Tox data\n"); } } /* CALLBACKS END */ -int add_window(Messenger *m, ToxWindow w) +int add_window(Tox *m, ToxWindow w) { if (LINES < 2) return -1; @@ -226,7 +229,7 @@ void prepare_window(WINDOW *w) wresize(w, LINES - 2, COLS); } -void draw_active_window(Messenger *m) +void draw_active_window(Tox *m) { ToxWindow *a = active_window; From 59c628e4b1a1d1979d60e8b612e0fea996177f5b Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Fri, 23 Aug 2013 23:52:07 +0200 Subject: [PATCH 04/82] Remove duplicated function resolve_addr --- src/main.c | 42 ------------------------------------------ 1 file changed, 42 deletions(-) diff --git a/src/main.c b/src/main.c index b3005cb..d02b86a 100644 --- a/src/main.c +++ b/src/main.c @@ -38,48 +38,6 @@ char *SRVLIST_FILE = NULL; static Tox *tox; - -/* -resolve_addr(): -address should represent IPv4 or a hostname with A record - -returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i -returns 0 on failure - -TODO: Fix ipv6 support -*/ - -uint32_t resolve_addr(const char *address) -{ - struct addrinfo *server = NULL; - struct addrinfo hints; - int rc; - uint32_t addr; - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; // IPv4 only right now. - hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. - - rc = getaddrinfo(address, "echo", &hints, &server); - - // Lookup failed. - if (rc != 0) { - return 0; - } - - // IPv4 records only.. - if (server->ai_family != AF_INET) { - freeaddrinfo(server); - return 0; - } - - - addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; - - freeaddrinfo(server); - return addr; -} - void on_window_resize(int sig) { endwin(); From 0f97e37fa30271bd0371c68a5ea5dd6f02636fe0 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Fri, 23 Aug 2013 23:52:35 +0200 Subject: [PATCH 05/82] Remove unused global variable --- src/main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.c b/src/main.c index d02b86a..a63411e 100644 --- a/src/main.c +++ b/src/main.c @@ -36,8 +36,6 @@ char *DATA_FILE = NULL; char *SRVLIST_FILE = NULL; -static Tox *tox; - void on_window_resize(int sig) { endwin(); From 2db69f0fd60ae476df7bebaaebc604c10c48d8a2 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Sat, 24 Aug 2013 00:21:01 +0200 Subject: [PATCH 06/82] Use tox_ instead of m_ functions --- src/chat.c | 18 ++++++++---------- src/friendlist.c | 2 +- src/main.c | 18 +++++++++--------- src/prompt.c | 12 ++++++------ 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/chat.c b/src/chat.c index 9490233..8e4ae66 100644 --- a/src/chat.c +++ b/src/chat.c @@ -13,8 +13,6 @@ #include #include -#include "network.h" - #include "toxic_windows.h" #include "friendlist.h" #include "chat.h" @@ -50,7 +48,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 if (ctx->friendnum != num) return; - getname(m, num, (uint8_t *) &nick); + tox_getname(m, num, (uint8_t *) &nick); msg[len - 1] = '\0'; nick[TOX_MAX_NAME_LENGTH - 1] = '\0'; @@ -232,7 +230,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wattroff(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "%s\n", line); - if (m_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { + if (tox_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(3)); @@ -289,7 +287,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) wprintw(ctx->history, msg); wattroff(ctx->history, COLOR_PAIR(5)); - if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) { + if (tox_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)); @@ -332,12 +330,12 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) msg = strchr(status, ' '); if (msg == NULL) { - m_set_userstatus(m, status_kind); + tox_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); + tox_set_userstatus(m, status_kind); + tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); } } @@ -352,7 +350,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) } nick++; - setname(m, (uint8_t *) nick, strlen(nick) + 1); + tox_setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(ctx->history, "Nickname set to: %s\n", nick); } @@ -437,7 +435,7 @@ ToxWindow new_chat(Tox *m, int friendnum) ret.onAction = &chat_onAction; uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; - getname(m, friendnum, (uint8_t *) &nick); + tox_getname(m, friendnum, (uint8_t *) &nick); snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); diff --git a/src/friendlist.c b/src/friendlist.c index ec6d970..b198656 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -61,7 +61,7 @@ int friendlist_onFriendAdded(Tox *m, int num) return -1; friends[num_friends].num = num; - getname(m, num, friends[num_friends].name); + tox_getname(m, num, friends[num_friends].name); strcpy((char *) friends[num_friends].name, "unknown"); strcpy((char *) friends[num_friends].status, "unknown"); friends[num_friends++].chatwin = -1; diff --git a/src/main.c b/src/main.c index a63411e..390099a 100644 --- a/src/main.c +++ b/src/main.c @@ -76,19 +76,19 @@ static Tox *init_tox() Tox *m = tox_new(); /* Callbacks */ - m_callback_friendrequest(m, on_request, NULL); - m_callback_friendmessage(m, on_message, NULL); - m_callback_namechange(m, on_nickchange, NULL); - m_callback_statusmessage(m, on_statuschange, NULL); - m_callback_action(m, on_action, NULL); + tox_callback_friendrequest(m, on_request, NULL); + tox_callback_friendmessage(m, on_message, NULL); + tox_callback_namechange(m, on_nickchange, NULL); + tox_callback_statusmessage(m, on_statuschange, NULL); + tox_callback_action(m, on_action, NULL); #ifdef __linux__ - setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy")); + tox_setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy")); #elif defined(WIN32) - setname(m, (uint8_t *) "I should install GNU/Linux", sizeof("I should install GNU/Linux")); + tox_setname(m, (uint8_t *) "I should install GNU/Linux", sizeof("I should install GNU/Linux")); #elif defined(__APPLE__) - setname(m, (uint8_t *) "Hipster", sizeof("Hipster")); //This used to users of other Unixes are hipsters + tox_setname(m, (uint8_t *) "Hipster", sizeof("Hipster")); //This used to users of other Unixes are hipsters #else - setname(m, (uint8_t *) "Registered Minix user #4", sizeof("Registered Minix user #4")); + tox_setname(m, (uint8_t *) "Registered Minix user #4", sizeof("Registered Minix user #4")); #endif return m; } diff --git a/src/prompt.c b/src/prompt.c index 50e0d4c..94dbe52 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -244,7 +244,7 @@ void cmd_msg(ToxWindow *self, Tox *m, char **args) char *id = args[1]; char *msg = args[2]; - if (m_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0) + if (tox_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0) wprintw(self->window, "Error occurred while sending message.\n"); else wprintw(self->window, "Message successfully sent.\n"); @@ -269,7 +269,7 @@ void cmd_myid(ToxWindow *self, Tox *m, char **args) void cmd_nick(ToxWindow *self, Tox *m, char **args) { char *nick = args[1]; - setname(m, (uint8_t *) nick, strlen(nick) + 1); + tox_setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(self->window, "Nickname set to: %s\n", nick); if (store_data(m, DATA_FILE)) { @@ -309,11 +309,11 @@ void cmd_status(ToxWindow *self, Tox *m, char **args) char *msg = args[2]; if (msg == NULL) { - m_set_userstatus(m, status_kind); + tox_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); + tox_set_userstatus(m, status_kind); + tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); wprintw(self->window, "Status set to: %s, %s\n", status_text, msg); } } @@ -321,7 +321,7 @@ void cmd_status(ToxWindow *self, Tox *m, char **args) void cmd_statusmsg(ToxWindow *self, Tox *m, char **args) { char *msg = args[1]; - m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); + tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); wprintw(self->window, "Status set to: %s\n", msg); } From 6f9813ceeeeed64ca896676ce29cec7e92a901f4 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Sat, 24 Aug 2013 00:21:39 +0200 Subject: [PATCH 07/82] Use tox.h and tox_new for toxcore discovery --- configure.ac | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 35b17cd..2226f9f 100644 --- a/configure.ac +++ b/configure.ac @@ -252,7 +252,7 @@ CPPFLAGS_SAVE="$CPPFLAGS" if test -n "$LIBTOXCORE_SEARCH_HEADERS"; then CFLAGS="$CFLAGS -I$LIBTOXCORE_SEARCH_HEADERS $LIBSODIUM_CFLAGS" CPPFLAGS="$CPPFLAGS -I$LIBTOXCORE_SEARCH_HEADERS $LIBSODIUM_CFLAGS" - AC_CHECK_HEADER([Messenger.h], + AC_CHECK_HEADER([tox.h], [ LIBTOXCORE_CFLAGS="-I$LIBTOXCORE_SEARCH_HEADERS" ], @@ -263,7 +263,7 @@ if test -n "$LIBTOXCORE_SEARCH_HEADERS"; then else CFLAGS="$CFLAGS $LIBSODIUM_CFLAGS" CPPFLAGS="$CPPFLAGS $LIBSODIUM_CFLAGS" - AC_CHECK_HEADER([Messenger.h], + AC_CHECK_HEADER([tox.h], [], [ AC_MSG_ERROR([headers for the toxcore library were not found on your system]) @@ -279,7 +279,7 @@ LIBTOXCORE_LDFLAGS= LDFLAGS_SAVE="$LDFLAGS" if test -n "$LIBTOXCORE_SEARCH_LIBS"; then LDFLAGS="$LDFLAGS $LIBSODIUM_LDFLAGS -L$LIBTOXCORE_SEARCH_LIBS $LIBSODIUM_LIBS" - AC_CHECK_LIB([toxcore], [initMessenger], + AC_CHECK_LIB([toxcore], [tox_new], [ LIBTOXCORE_LDFLAGS="-L$LIBTOXCORE_SEARCH_LIBS" LIBTOXCORE_LIBS="-ltoxcore" @@ -290,7 +290,7 @@ if test -n "$LIBTOXCORE_SEARCH_LIBS"; then ) else LDFLAGS="$LDFLAGS $LIBSODIUM_LDFLAGS $LIBSODIUM_LIBS" - AC_CHECK_LIB([toxcore], [initMessenger], + AC_CHECK_LIB([toxcore], [tox_new], [], [ AC_MSG_ERROR([required library toxcore was not found on your system]) From a2c85f74ea7b9b3a7b81acd28c16f8467f280c27 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Fri, 23 Aug 2013 15:41:32 -0700 Subject: [PATCH 08/82] Added travis integration --- .travis.yml | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ec74082 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,48 @@ +language: c +compiler: + - gcc + - clang + +before_script: +# installing libsodium, needed for Core + - git clone git://github.com/jedisct1/libsodium.git + - cd libsodium + - git checkout tags/0.4.2 + - ./autogen.sh + - ./configure && make check -j3 + - sudo make install + - cd .. +# installing libconfig, needed for DHT_bootstrap_daemon + - wget http://www.hyperrealm.com/libconfig/libconfig-1.4.9.tar.gz + - tar -xvzf libconfig-1.4.9.tar.gz + - cd libconfig-1.4.9 + - ./configure && make -j3 + - sudo make install + - cd .. +# creating librarys' links and updating cache + - sudo ldconfig +# installing sphinx, needed for documentation + - sudo apt-get install python-sphinx +# installing check, needed for unit tests + - sudo apt-get install check + + - git clone https://github.com/irungentoo/ProjectTox-Core.git core + - cd core + - mkdir build + - cmake .. + - make -j2 + - cd ../.. + - mv core /tmp +#Need an absolute path for autotools +script: + - autoreconf -i + - ./configure --with-libtoxcore-headers=/tmp/core --with-libtoxcore-libs=/tmp/core/build/core + - make -j2 +notifications: + email: false + + irc: + channels: + - "chat.freenode.net#tox-dev" + on_success: always + on_failure: always From 77da25553c9dd069e7e2439a768c405f2276a7d8 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Fri, 23 Aug 2013 15:45:59 -0700 Subject: [PATCH 09/82] Fixed travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index ec74082..b04748c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,6 +29,7 @@ before_script: - git clone https://github.com/irungentoo/ProjectTox-Core.git core - cd core - mkdir build + - cd build - cmake .. - make -j2 - cd ../.. From f2cdf5d9e531693029e87629ab22be827e0de139 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Fri, 23 Aug 2013 15:54:53 -0700 Subject: [PATCH 10/82] Hopefully fixed travis --- .travis.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index b04748c..0ac1d29 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,11 +33,10 @@ before_script: - cmake .. - make -j2 - cd ../.. - - mv core /tmp -#Need an absolute path for autotools + - mv core $TRAVIS_BUILD_DIR/ script: - autoreconf -i - - ./configure --with-libtoxcore-headers=/tmp/core --with-libtoxcore-libs=/tmp/core/build/core + - ./configure --with-libtoxcore-headers=$TRAVIS_BUILD_DIR/core --with-libtoxcore-libs=$TRAVIS_BUILD_DIR/core/build/core - make -j2 notifications: email: false From 66deefe86f2e20f1f84bc827c842f5ba431c43df Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Fri, 23 Aug 2013 15:58:52 -0700 Subject: [PATCH 11/82] Okay, now If fixed travis. --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0ac1d29..eaf4f36 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,7 +33,6 @@ before_script: - cmake .. - make -j2 - cd ../.. - - mv core $TRAVIS_BUILD_DIR/ script: - autoreconf -i - ./configure --with-libtoxcore-headers=$TRAVIS_BUILD_DIR/core --with-libtoxcore-libs=$TRAVIS_BUILD_DIR/core/build/core From 416432c39f91df99b3d8e766c564203dd555ca1d Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Fri, 23 Aug 2013 16:03:54 -0700 Subject: [PATCH 12/82] Okay, I'm an idiot. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index eaf4f36..703594a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,7 +35,7 @@ before_script: - cd ../.. script: - autoreconf -i - - ./configure --with-libtoxcore-headers=$TRAVIS_BUILD_DIR/core --with-libtoxcore-libs=$TRAVIS_BUILD_DIR/core/build/core + - ./configure --with-libtoxcore-headers=$TRAVIS_BUILD_DIR/core/core --with-libtoxcore-libs=$TRAVIS_BUILD_DIR/core/build/core - make -j2 notifications: email: false From 628bb9472b3683b4a3d1c61d3fc528b9b7e59d99 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Fri, 23 Aug 2013 16:05:58 -0700 Subject: [PATCH 13/82] Actually fixed travis. --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 703594a..e32267f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,8 +26,8 @@ before_script: # installing check, needed for unit tests - sudo apt-get install check - - git clone https://github.com/irungentoo/ProjectTox-Core.git core - - cd core + - git clone https://github.com/irungentoo/ProjectTox-Core.git toxcore + - cd toxcore - mkdir build - cd build - cmake .. @@ -35,7 +35,7 @@ before_script: - cd ../.. script: - autoreconf -i - - ./configure --with-libtoxcore-headers=$TRAVIS_BUILD_DIR/core/core --with-libtoxcore-libs=$TRAVIS_BUILD_DIR/core/build/core + - ./configure --with-libtoxcore-headers=$TRAVIS_BUILD_DIR/toxcore/core --with-libtoxcore-libs=$TRAVIS_BUILD_DIR/toxcore/build/core - make -j2 notifications: email: false From 1e1ddddb6b50a0be691e8acfd74697f53584be6e Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Fri, 23 Aug 2013 16:47:36 -0700 Subject: [PATCH 14/82] Travis now only compiles toxcore --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e32267f..c3f9aed 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,7 +31,7 @@ before_script: - mkdir build - cd build - cmake .. - - make -j2 + - make toxcore -j2 - cd ../.. script: - autoreconf -i From 2a46d48cdf1a7547724530abaf0b241eb1ec8fcf Mon Sep 17 00:00:00 2001 From: Sergey 'Jin' Bostandzhyan Date: Sat, 24 Aug 2013 05:15:15 +0300 Subject: [PATCH 15/82] Sync with new libtoxcore build system First try to use pkg-config to find libtoxcore, fall back to manual checks if pkg-config is not available. Improve ncurses search, should now find everything on OSX. --- configure.ac | 281 +++++++++++++++++++++++++++----------------- src/friendlist.c | 2 +- src/main.c | 2 +- src/toxic_windows.h | 2 +- 4 files changed, 173 insertions(+), 114 deletions(-) diff --git a/configure.ac b/configure.ac index 2226f9f..fd2b89b 100644 --- a/configure.ac +++ b/configure.ac @@ -20,6 +20,10 @@ LIBTOXCORE_SEARCH_LIBS= LIBSODIUM_SEARCH_HEADERS= LIBSODIUM_SEARCH_LIBS= +LIBTOXCORE_FOUND="no" +NCURSES_FOUND="no" +NCURSES_WIDECHAR_SUPPORT="no" + AC_ARG_WITH(dependency-search, AC_HELP_STRING([--with-dependency-search=DIR], [search for dependencies in DIR, i.e. look for libraries in @@ -38,7 +42,7 @@ fi AC_ARG_WITH(libtoxcore-headers, AC_HELP_STRING([--with-libtoxcore-headers=DIR], - [search for libtoxcore header files in DIR]), + [search for libtoxcore header files in DIR/tox]), [ LIBTOXCORE_SEARCH_HEADERS="$withval" AC_MSG_NOTICE([Will search for libtoxcore header files in $withval]) @@ -108,17 +112,19 @@ AC_CHECK_FUNCS( # pkg-config based tests PKG_PROG_PKG_CONFIG -NCURSES_WIDECHAR_SUPPORT="yes" if test -n "$PKG_CONFIG"; then if test "$WIN32" != "xyes"; then PKG_CHECK_MODULES([NCURSES], [ncursesw], - [], + [ + NCURSES_FOUND="yes" + NCURSES_WIDECHAR_SUPPORT="yes" + ], [ NCURSES_WIDECHAR_SUPPORT="no" PKG_CHECK_MODULES([NCURSES], [ncurses], [], [ - AC_MSG_ERROR([required library ncursesw was not found on your system: $NCURSES_PKG_ERRORS]) + AC_MSG_WARN([$NCURSES_PKG_ERRORS]) ]) ]) fi @@ -126,8 +132,77 @@ else AC_MSG_WARN([pkg-config was not found on your sytem]) fi +if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then + AC_PATH_PROG([CURSES_CONFIG], [ncursesw5-config], [no]) + if test "x$CURSES_CONFIG" != "xno"; then + AC_MSG_CHECKING(ncurses cflags) + NCURSES_CFLAGS=`${CURSES_CONFIG} --cflags` + AC_MSG_RESULT($NCURSES_CFLAGS) -if (test -z "$PKG_CONFIG") || (test "x$WIN32" = "xyes"); then + AC_MSG_CHECKING(ncurses libraries) + NCURSES_LIBS=`${CURSES_CONFIG} --libs` + AC_MSG_RESULT($NCURSES_LIBS) + + AC_SUBST(NCURSES_CFLAGS) + AC_SUBST(NCURSES_LIBS) + NCURSES_FOUND="yes" + NCURSES_WIDECHAR_SUPPORT="yes" + fi +fi + +if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then + AC_PATH_PROG([CURSES_CONFIG], [ncursesw5.4-config], [no]) + if test "x$CURSES_CONFIG" != "xno"; then + AC_MSG_CHECKING(ncurses cflags) + NCURSES_CFLAGS=`${CURSES_CONFIG} --cflags` + AC_MSG_RESULT($NCURSES_CFLAGS) + + AC_MSG_CHECKING(ncurses libraries) + NCURSES_LIBS=`${CURSES_CONFIG} --libs` + AC_MSG_RESULT($NCURSES_LIBS) + + AC_SUBST(NCURSES_CFLAGS) + AC_SUBST(NCURSES_LIBS) + NCURSES_FOUND="yes" + NCURSES_WIDECHAR_SUPPORT="yes" + fi +fi + +if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then + AC_PATH_PROG([CURSES_CONFIG], [ncurses5-config], [no]) + if test "x$CURSES_CONFIG" != "xno"; then + AC_MSG_CHECKING(ncurses cflags) + NCURSES_CFLAGS=`${CURSES_CONFIG} --cflags` + AC_MSG_RESULT($NCURSES_CFLAGS) + + AC_MSG_CHECKING(ncurses libraries) + NCURSES_LIBS=`${CURSES_CONFIG} --libs` + AC_MSG_RESULT($NCURSES_LIBS) + + AC_SUBST(NCURSES_CFLAGS) + AC_SUBST(NCURSES_LIBS) + NCURSES_FOUND="yes" + fi +fi + +if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then + AC_PATH_PROG([CURSES_CONFIG], [ncurses5.4-config], [no]) + if test "x$CURSES_CONFIG" != "xno"; then + AC_MSG_CHECKING(ncurses cflags) + NCURSES_CFLAGS=`${CURSES_CONFIG} --cflags` + AC_MSG_RESULT($NCURSES_CFLAGS) + + AC_MSG_CHECKING(ncurses libraries) + NCURSES_LIBS=`${CURSES_CONFIG} --libs` + AC_MSG_RESULT($NCURSES_LIBS) + + AC_SUBST(NCURSES_CFLAGS) + AC_SUBST(NCURSES_LIBS) + NCURSES_FOUND="yes" + fi +fi + +if test "x$NCURSES_FOUND" = "xno"; then AC_CHECK_HEADER([curses.h], [], [ @@ -156,11 +231,15 @@ if (test -z "$PKG_CONFIG") || (test "x$WIN32" = "xyes"); then ) else AC_CHECK_LIB([ncursesw], [get_wch], - [], + [ + NCURSES_WIDECHAR_SUPPORT="yes" + ], [ unset ac_cv_lib_ncursesw_get_wch AC_CHECK_LIB([ncursesw], [get_wch], - [], + [ + NCURSES_WIDECHAR_SUPPORT="yes" + ], [ NCURSES_WIDECHAR_SUPPORT="no" AC_CHECK_LIB([ncurses], [clear], @@ -188,120 +267,100 @@ if (test -z "$PKG_CONFIG") || (test "x$WIN32" = "xyes"); then fi fi -# sodium is included by Tox headers so we kind of need to know where it is -LIBSODIUM_CFLAGS= -CFLAGS_SAVE="$CFLAGS" -CPPFLAGS_SAVE="$CPPFLAGS" -if test -n "$LIBSODIUM_SEARCH_HEADERS"; then - CFLAGS="$CFLAGS -I$LIBSODIUM_SEARCH_HEADERS" - CPPFLAGS="$CFLAGS -I$LIBSODIUM_SEARCH_HEADERS" - AC_CHECK_HEADER(sodium.h, +if test -n "$PKG_CONFIG"; then + PKG_CHECK_MODULES(LIBTOXCORE, [libtoxcore], [ - LIBSODIUM_CFLAGS="-I$LIBSODIUM_SEARCH_HEADERS" + LIBTOXCORE_FOUND="yes" ], [ - AC_MSG_ERROR([header files for required library libsodium was not found in requested location $LIBSODIUM_SEARCH_HEADERS]) - ] - ) -else - AC_CHECK_HEADER(sodium.h, - [], - [ - AC_MSG_ERROR([header files for required library libsodium was not found on your system, please check http://download.libsodium.org/libsodium/releases/]) - ] - ) -fi -CFLAGS="$CFLAGS_SAVE" -CPPFLAGS="$CPPFLAGS_SAVE" -AC_SUBST(LIBSODIUM_CFLAGS) - - -LIBSODIUM_LIBS= -LIBSODIUM_LDFLAGS= -LDFLAGS_SAVE="$LDFLAGS" -if test -n "$LIBSODIUM_SEARCH_LIBS"; then - LDFLAGS="$LDFLAGS -L$LIBSODIUM_SEARCH_LIBS" - AC_CHECK_LIB(sodium, randombytes_random, - [ - LIBSODIUM_LDFLAGS="-L$LIBSODIUM_SEARCH_LIBS" - LIBSODIUM_LIBS="-lsodium" - ], - [ - AC_MSG_ERROR([required library libsodium was not found in requested location $LIBSODIUM_SEARCH_LIBS]) - ] - ) -else - AC_CHECK_LIB(sodium, randombytes_random, - [], - [ - AC_MSG_ERROR([required library libsodium was not found on your system, please check http://download.libsodium.org/libsodium/releases/]) - ] - ) + AC_MSG_WARN([required library libsodium was not found in requested location $LIBSODIUM_SEARCH_LIBS]) + ]) fi -LDFLAGS="$LDFLAGS_SAVE" -AC_SUBST(LIBSODIUM_LIBS) -AC_SUBST(LIBSODIUM_LDFLAGS) +if test "x$LIBTOXCORE_FOUND" = "xno"; then + LIBSODIUM_LIBS= + LIBSODIUM_LDFLAGS= + LDFLAGS_SAVE="$LDFLAGS" + if test -n "$LIBSODIUM_SEARCH_LIBS"; then + LDFLAGS="$LDFLAGS -L$LIBSODIUM_SEARCH_LIBS" + AC_CHECK_LIB(sodium, randombytes_random, + [ + LIBSODIUM_LDFLAGS="-L$LIBSODIUM_SEARCH_LIBS" + LIBSODIUM_LIBS="-lsodium" + ], + [ + AC_MSG_ERROR([required library libsodium was not found in requested location $LIBSODIUM_SEARCH_LIBS]) + ] + ) + else + AC_CHECK_LIB(sodium, randombytes_random, + [], + [ + AC_MSG_ERROR([required library libsodium was not found on your system, please check http://download.libsodium.org/libsodium/releases/]) + ] + ) + fi + + LDFLAGS="$LDFLAGS_SAVE" + AC_SUBST(LIBSODIUM_LIBS) + AC_SUBST(LIBSODIUM_LDFLAGS) -LIBTOXCORE_CFLAGS= -CFLAGS_SAVE="$CFLAGS" -CPPFLAGS_SAVE="$CPPFLAGS" + LIBTOXCORE_CFLAGS= + CFLAGS_SAVE="$CFLAGS" + CPPFLAGS_SAVE="$CPPFLAGS" -if test -n "$LIBTOXCORE_SEARCH_HEADERS"; then - CFLAGS="$CFLAGS -I$LIBTOXCORE_SEARCH_HEADERS $LIBSODIUM_CFLAGS" - CPPFLAGS="$CPPFLAGS -I$LIBTOXCORE_SEARCH_HEADERS $LIBSODIUM_CFLAGS" - AC_CHECK_HEADER([tox.h], - [ - LIBTOXCORE_CFLAGS="-I$LIBTOXCORE_SEARCH_HEADERS" - ], - [ - AC_MSG_ERROR([headers for the toxcore library were not found on your system]) - ] - ) -else - CFLAGS="$CFLAGS $LIBSODIUM_CFLAGS" - CPPFLAGS="$CPPFLAGS $LIBSODIUM_CFLAGS" - AC_CHECK_HEADER([tox.h], - [], - [ - AC_MSG_ERROR([headers for the toxcore library were not found on your system]) - ] - ) + if test -n "$LIBTOXCORE_SEARCH_HEADERS"; then + CFLAGS="$CFLAGS -I$LIBTOXCORE_SEARCH_HEADERS" + CPPFLAGS="$CPPFLAGS -I$LIBTOXCORE_SEARCH_HEADERS" + AC_CHECK_HEADER([tox/tox.h], + [ + LIBTOXCORE_CFLAGS="-I$LIBTOXCORE_SEARCH_HEADERS" + ], + [ + AC_MSG_ERROR([headers for the toxcore library were not found on your system]) + ] + ) + else + AC_CHECK_HEADER([tox/tox.h], + [], + [ + AC_MSG_ERROR([headers for the toxcore library were not found on your system]) + ], + ) + fi + CFLAGS="$CFLAGS_SAVE" + CPPFLAGS="$CPPFLAGS_SAVE" + AC_SUBST(LIBTOXCORE_CFLAGS) + + LIBTOXCORE_LIBS= + LIBTOXCORE_LDFLAGS= + LDFLAGS_SAVE="$LDFLAGS" + if test -n "$LIBTOXCORE_SEARCH_LIBS"; then + LDFLAGS="$LDFLAGS $LIBSODIUM_LDFLAGS -L$LIBTOXCORE_SEARCH_LIBS $LIBSODIUM_LIBS" + AC_CHECK_LIB([toxcore], [tox_new], + [ + LIBTOXCORE_LDFLAGS="-L$LIBTOXCORE_SEARCH_LIBS" + LIBTOXCORE_LIBS="-ltoxcore" + ], + [ + AC_MSG_ERROR([required library toxcore was not found on your system]) + ] + ) + else + LDFLAGS="$LDFLAGS $LIBSODIUM_LDFLAGS $LIBSODIUM_LIBS" + AC_CHECK_LIB([toxcore], [tox_new], + [], + [ + AC_MSG_ERROR([required library toxcore was not found on your system]) + ] + ) + fi + LDFLAGS="$LDFLAGS_SAVE" + AC_SUBST(LIBTOXCORE_LIBS) + AC_SUBST(LIBTOXCORE_LDFLAGS) fi -CFLAGS="$CFLAGS_SAVE" -CPPFLAGS="$CPPFLAGS_SAVE" -AC_SUBST(LIBTOXCORE_CFLAGS) - -LIBTOXCORE_LIBS= -LIBTOXCORE_LDFLAGS= -LDFLAGS_SAVE="$LDFLAGS" -if test -n "$LIBTOXCORE_SEARCH_LIBS"; then - LDFLAGS="$LDFLAGS $LIBSODIUM_LDFLAGS -L$LIBTOXCORE_SEARCH_LIBS $LIBSODIUM_LIBS" - AC_CHECK_LIB([toxcore], [tox_new], - [ - LIBTOXCORE_LDFLAGS="-L$LIBTOXCORE_SEARCH_LIBS" - LIBTOXCORE_LIBS="-ltoxcore" - ], - [ - AC_MSG_ERROR([required library toxcore was not found on your system]) - ] - ) -else - LDFLAGS="$LDFLAGS $LIBSODIUM_LDFLAGS $LIBSODIUM_LIBS" - AC_CHECK_LIB([toxcore], [tox_new], - [], - [ - AC_MSG_ERROR([required library toxcore was not found on your system]) - ] - ) -fi -LDFLAGS="$LDFLAGS_SAVE" -AC_SUBST(LIBTOXCORE_LIBS) -AC_SUBST(LIBTOXCORE_LDFLAGS) - - TOXIC_VERSION="$PACKAGE_VERSION" AC_PATH_PROG([GIT], [git], [no]) diff --git a/src/friendlist.c b/src/friendlist.c index b198656..768ab7f 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -10,7 +10,7 @@ #include #include -#include "tox.h" +#include #include "friendlist.h" diff --git a/src/main.c b/src/main.c index 390099a..7c8e1ac 100644 --- a/src/main.c +++ b/src/main.c @@ -25,7 +25,7 @@ #include #endif -#include "tox.h" +#include #include "configdir.h" #include "toxic_windows.h" diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 34edf6f..f93b775 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -10,7 +10,7 @@ #include #include -#include "tox.h" +#include #define MAX_WINDOWS_NUM 32 #define MAX_FRIENDS_NUM 100 From f5071e403805b66d5cc95df9813546b87faf54f3 Mon Sep 17 00:00:00 2001 From: Sergey 'Jin' Bostandzhyan Date: Sat, 24 Aug 2013 05:45:08 +0300 Subject: [PATCH 16/82] Fixed travis build --- .travis.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index c3f9aed..8b25d83 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,14 +28,13 @@ before_script: - git clone https://github.com/irungentoo/ProjectTox-Core.git toxcore - cd toxcore - - mkdir build - - cd build - - cmake .. - - make toxcore -j2 - - cd ../.. + - autoreconf -i + - ./configure + - make -j2 + - sudo make install script: - autoreconf -i - - ./configure --with-libtoxcore-headers=$TRAVIS_BUILD_DIR/toxcore/core --with-libtoxcore-libs=$TRAVIS_BUILD_DIR/toxcore/build/core + - ./configure - make -j2 notifications: email: false From 08f21e01892cb3f76d6531fd5fc5835718fa6625 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Fri, 23 Aug 2013 19:56:10 -0700 Subject: [PATCH 17/82] Added a missing cd --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 8b25d83..25ee6e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,6 +32,7 @@ before_script: - ./configure - make -j2 - sudo make install + - cd .. script: - autoreconf -i - ./configure From 732d05c04e65502a65a2371d6387c97f519cbd29 Mon Sep 17 00:00:00 2001 From: Sergey 'Jin' Bostandzhyan Date: Sat, 24 Aug 2013 20:47:06 +0300 Subject: [PATCH 18/82] Fix configure script for mingw32 Note: toxic itself will still not build because the code does not have the appropriate ifdefs and a WIN32 specific implementation. --- configure.ac | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index fd2b89b..d92b769 100644 --- a/configure.ac +++ b/configure.ac @@ -113,7 +113,7 @@ AC_CHECK_FUNCS( PKG_PROG_PKG_CONFIG if test -n "$PKG_CONFIG"; then - if test "$WIN32" != "xyes"; then + if test "x$WIN32" != "xyes"; then PKG_CHECK_MODULES([NCURSES], [ncursesw], [ NCURSES_FOUND="yes" @@ -132,7 +132,7 @@ else AC_MSG_WARN([pkg-config was not found on your sytem]) fi -if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then +if (test "x$NCURSES_FOUND" = "xno") && (test "x$WIN32" != "xyes"); then AC_PATH_PROG([CURSES_CONFIG], [ncursesw5-config], [no]) if test "x$CURSES_CONFIG" != "xno"; then AC_MSG_CHECKING(ncurses cflags) @@ -150,7 +150,8 @@ if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then fi fi -if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then +if (test "x$NCURSES_FOUND" = "xno") && (test "x$WIN32" != "xyes"); then + unset ac_cv_path_CURSES_CONFIG AC_PATH_PROG([CURSES_CONFIG], [ncursesw5.4-config], [no]) if test "x$CURSES_CONFIG" != "xno"; then AC_MSG_CHECKING(ncurses cflags) @@ -168,7 +169,8 @@ if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then fi fi -if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then +if (test "x$NCURSES_FOUND" = "xno") && (test "x$WIN32" != "xyes"); then + unset ac_cv_path_CURSES_CONFIG AC_PATH_PROG([CURSES_CONFIG], [ncurses5-config], [no]) if test "x$CURSES_CONFIG" != "xno"; then AC_MSG_CHECKING(ncurses cflags) @@ -185,7 +187,8 @@ if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then fi fi -if (test "x$NCURSES_FOUND" = "xno") && (test "$WIN32" != "xyes"); then +if (test "x$NCURSES_FOUND" = "xno") && (test "x$WIN32" != "xyes"); then + unset ac_cv_path_CURSES_CONFIG AC_PATH_PROG([CURSES_CONFIG], [ncurses5.4-config], [no]) if test "x$CURSES_CONFIG" != "xno"; then AC_MSG_CHECKING(ncurses cflags) @@ -346,6 +349,9 @@ if test "x$LIBTOXCORE_FOUND" = "xno"; then ], [ AC_MSG_ERROR([required library toxcore was not found on your system]) + ], + [ + $WINSOCK2_LIBS ] ) else @@ -354,6 +360,9 @@ if test "x$LIBTOXCORE_FOUND" = "xno"; then [], [ AC_MSG_ERROR([required library toxcore was not found on your system]) + ], + [ + $WINSOCK2_LIBS ] ) fi From cecc3ceea0c111ea11daa073bf048b9a161d54f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Arg=C3=BCelles?= Date: Sat, 24 Aug 2013 18:41:06 -0400 Subject: [PATCH 19/82] Fix configure script for ncurses without ncursesw On debian systems it is possible not to have libncursesw5-dev and still have ncursesw5-config pointing to where it should be, ncursesw5-config is provided by libncursesw5. --- configure.ac | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index d92b769..5fd25ef 100644 --- a/configure.ac +++ b/configure.ac @@ -122,7 +122,9 @@ if test -n "$PKG_CONFIG"; then [ NCURSES_WIDECHAR_SUPPORT="no" PKG_CHECK_MODULES([NCURSES], [ncurses], - [], + [ + NCURSES_FOUND="yes" + ], [ AC_MSG_WARN([$NCURSES_PKG_ERRORS]) ]) From 12af26b6668ff6b4a7c3c34c610bd57b0e17cf53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Arg=C3=BCelles?= Date: Sat, 24 Aug 2013 18:59:57 -0400 Subject: [PATCH 20/82] Add check for setlocale() In wide char mode it is important to be sure that setlocale() function succeed. --- src/main.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 91e8963..dc52753 100644 --- a/src/main.c +++ b/src/main.c @@ -50,7 +50,13 @@ static void init_term() { /* Setup terminal */ signal(SIGWINCH, on_window_resize); - setlocale(LC_ALL, ""); +#if HAVE_WIDECHAR + if (setlocale(LC_ALL, "") == NULL) { + printf("Could not set your locale, plese check your locale settings or" + "disable wide char support\n"); + exit(1); + } +#endif initscr(); cbreak(); keypad(stdscr, 1); From e6a13feae8877339dd1ce4f95500355c53e5e809 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 24 Aug 2013 19:16:43 -0400 Subject: [PATCH 21/82] Fixed some warnings. --- src/chat.c | 6 +++--- src/main.c | 5 ++++- src/prompt.c | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/chat.c b/src/chat.c index 8e4ae66..0054f90 100644 --- a/src/chat.c +++ b/src/chat.c @@ -220,7 +220,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) /* make sure the string has at least non-space character */ if (!string_is_empty(line)) { uint8_t selfname[TOX_MAX_NAME_LENGTH]; - getself_name(m, selfname, sizeof(selfname)); + tox_getselfname(m, selfname, sizeof(selfname)); wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); @@ -279,7 +279,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) wattroff(ctx->history, COLOR_PAIR(2)); uint8_t selfname[TOX_MAX_NAME_LENGTH]; - int len = getself_name(m, selfname, sizeof(selfname)); + int len = tox_getselfname(m, selfname, sizeof(selfname)); char msg[MAX_STR_SIZE - len - 4]; snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action); @@ -358,7 +358,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; int i; uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; - getaddress(m, address); + tox_getaddress(m, address); for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) { char xx[3]; diff --git a/src/main.c b/src/main.c index 7c8e1ac..91e8963 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,9 @@ #include "prompt.h" #include "friendlist.h" +#ifndef PACKAGE_DATADIR +#define PACKAGE_DATADIR "." +#endif /* Export for use in Callbacks */ char *DATA_FILE = NULL; char *SRVLIST_FILE = NULL; @@ -291,7 +294,7 @@ static void load_data(Tox *m, char *path) tox_load(m, buf, len); - uint32_t i; + uint32_t i = 0; char name[TOX_MAX_NAME_LENGTH]; while (tox_getname(m, i, (uint8_t *)name) != -1) { diff --git a/src/prompt.c b/src/prompt.c index 94dbe52..5fb4334 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -89,7 +89,7 @@ void cmd_accept(ToxWindow *self, Tox *m, char **args) return; } - num = m_addfriend_norequest(m, pending_requests[num]); + num = tox_addfriend_norequest(m, pending_requests[num]); if (num == -1) wprintw(self->window, "Failed to add friend.\n"); @@ -139,7 +139,7 @@ void cmd_add(ToxWindow *self, Tox *m, char **args) id[i] = toupper(id[i]); } - int num = m_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1); + int num = tox_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1); switch (num) { case TOX_FAERR_TOOLONG: @@ -255,7 +255,7 @@ void cmd_myid(ToxWindow *self, Tox *m, char **args) char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; size_t i; uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; - getaddress(m, address); + tox_getaddress(m, address); for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) { char xx[3]; From c7dcf042487e4651b695a140795044633e08e066 Mon Sep 17 00:00:00 2001 From: Sergey 'Jin' Bostandzhyan Date: Mon, 26 Aug 2013 14:05:14 +0300 Subject: [PATCH 22/82] Fix configure for Free BSD * seems that /usr/local is not in the default ld path on FreeBSD, so add it manually * search for wget_wch instead of get_wch, on FreeBSD get_wch seems to be available as a macro, which does not automatically mean that wide char support is really available --- configure.ac | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index 5fd25ef..8de91a0 100644 --- a/configure.ac +++ b/configure.ac @@ -82,9 +82,13 @@ case $host_os in *mingw*) WIN32="yes" ;; + *freebsd*) + LDFLAGS="$LDFLAGS -L/usr/local/lib" + CFLAGS="$CFLAGS -I/usr/local/include" + CPPFLAGS="$CPPFLAGS -I/usr/local/include" + ;; esac - # Checks for programs. AC_PROG_CC AM_PROG_CC_C_O @@ -235,13 +239,13 @@ if test "x$NCURSES_FOUND" = "xno"; then ] ) else - AC_CHECK_LIB([ncursesw], [get_wch], + AC_CHECK_LIB([ncursesw], [wget_wch], [ NCURSES_WIDECHAR_SUPPORT="yes" ], [ - unset ac_cv_lib_ncursesw_get_wch - AC_CHECK_LIB([ncursesw], [get_wch], + unset ac_cv_lib_ncursesw_wget_wch + AC_CHECK_LIB([ncursesw], [wget_wch], [ NCURSES_WIDECHAR_SUPPORT="yes" ], @@ -250,7 +254,7 @@ if test "x$NCURSES_FOUND" = "xno"; then AC_CHECK_LIB([ncurses], [clear], [], [ - unset ac_cv_lib_ncursesw_get_wch + unset ac_cv_lib_ncurses_clear AC_CHECK_LIB([ncurses], [clear], [], [ From ad1c9f0cae7c0fbc6171a284f3341d9acd547e1f Mon Sep 17 00:00:00 2001 From: Sergey 'Jin' Bostandzhyan Date: Mon, 26 Aug 2013 17:03:04 +0300 Subject: [PATCH 23/82] Fix for the "bad character" when doing backspace in chat window MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backspace was printing 'ć' instead of actually backspacing in a chat window when widechar support was enabled. --- src/windows.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/windows.c b/src/windows.c index d0852e3..39e0539 100644 --- a/src/windows.c +++ b/src/windows.c @@ -242,7 +242,7 @@ void draw_active_window(Tox *m) /* Handle input */ #ifdef HAVE_WIDECHAR - get_wch(&ch); + wget_wch(a->window, &ch); #else ch = getch(); #endif From bb8a2b770043838f666056ef867e69537a366a9f Mon Sep 17 00:00:00 2001 From: jin-eld Date: Tue, 27 Aug 2013 10:45:21 +0300 Subject: [PATCH 24/82] Make sure toxic compiles on MinGW/Win32 again The config dir stuff is simply broken and needs to be fixed. So for now disabled it, until someone has time to look into it. --- configure.ac | 2 ++ src/configdir.c | 21 ++++++++++++++------- src/main.c | 31 ++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/configure.ac b/configure.ac index 8de91a0..a764e18 100644 --- a/configure.ac +++ b/configure.ac @@ -238,6 +238,8 @@ if test "x$NCURSES_FOUND" = "xno"; then AC_MSG_ERROR([required library winsock2 was not found on the system, please check your MinGW installation]) ] ) + AC_DEFINE([_WIN32_WINNT], [0x501], + [enable getaddrinfo/freeaddrinfo on XP and higher]) else AC_CHECK_LIB([ncursesw], [wget_wch], [ diff --git a/src/configdir.c b/src/configdir.c index a032546..193b162 100644 --- a/src/configdir.c +++ b/src/configdir.c @@ -49,7 +49,10 @@ char *get_user_config_dir(void) { char *user_config_dir; -#ifdef WIN32 +#ifdef __WIN32__ +#warning Please fix configdir for Win32 + return NULL; +#if 0 char appdata[MAX_PATH]; BOOL ok; @@ -62,6 +65,7 @@ char *get_user_config_dir(void) user_config_dir = strdup(appdata); return user_config_dir; +#endif #else /* WIN32 */ @@ -126,11 +130,10 @@ char *get_user_config_dir(void) */ int create_user_config_dir(char *path) { - - int mkdir_err; - -#ifdef WIN32 - +#ifdef __WIN32__ +#warning Please fix configdir for Win32 + return -1; +#if 0 char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); strcpy(fullpath, path); strcat(fullpath, CONFIGDIR); @@ -143,7 +146,11 @@ int create_user_config_dir(char *path) return -1; } + free(fullpath); +#endif + #else + int mkdir_err; mkdir_err = mkdir(path, 0700); struct stat buf; @@ -163,7 +170,7 @@ int create_user_config_dir(char *path) return -1; } -#endif free(fullpath); return 0; +#endif } diff --git a/src/main.c b/src/main.c index dc52753..3d70c7a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,10 @@ #include "config.h" #endif +#ifndef SIGWINCH + #define SIGWINCH 28 +#endif + #include #include #include @@ -15,11 +19,13 @@ #include #include #include -#include -#ifdef _win32 -#include +#ifdef WIN32 + #include + #include + #include #else +#include #include #include #include @@ -122,16 +128,32 @@ uint32_t resolve_addr(const char *address) hints.ai_family = AF_INET; // IPv4 only right now. hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. +#ifdef __WIN32__ + int res; + WSADATA wsa_data; + + res = WSAStartup(MAKEWORD(2, 2), &wsa_data); + if (res != 0) + { + return 0; + } +#endif rc = getaddrinfo(address, "echo", &hints, &server); // Lookup failed. if (rc != 0) { +#ifdef __WIN32__ + WSACleanup(); +#endif return 0; } // IPv4 records only.. if (server->ai_family != AF_INET) { freeaddrinfo(server); +#ifdef __WIN32__ + WSACleanup(); +#endif return 0; } @@ -139,6 +161,9 @@ uint32_t resolve_addr(const char *address) addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; freeaddrinfo(server); +#ifdef __WIN32__ + WSACleanup(); +#endif return addr; } From 894dc28c6fe75bccbf96c659994b799d8a37e82b Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 27 Aug 2013 04:58:30 -0400 Subject: [PATCH 25/82] Hiding DHT tab since it doesn't work currently --- src/main.c | 3 --- src/windows.c | 3 ++- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index dc52753..86c3a66 100644 --- a/src/main.c +++ b/src/main.c @@ -151,9 +151,6 @@ int init_connection(Tox *m) { FILE *fp = NULL; - if (tox_isconnected(m)) - return 0; - fp = fopen(SRVLIST_FILE, "r"); if (!fp) diff --git a/src/windows.c b/src/windows.c index 39e0539..f2ddefb 100644 --- a/src/windows.c +++ b/src/windows.c @@ -169,7 +169,8 @@ ToxWindow *init_windows() if (n_prompt == -1 || add_window(m, new_friendlist()) == -1 - || add_window(m, new_dhtstatus()) == -1) { + //|| add_window(m, new_dhtstatus()) == -1 + ) { fprintf(stderr, "add_window() failed.\n"); endwin(); exit(1); From 5cf982dfa141d3d6f36c725ba5d5a3dcc553c756 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 27 Aug 2013 18:30:38 -0400 Subject: [PATCH 26/82] Removed dht window because it's not supported by the new public api --- build/Makefile.am | 4 +- src/dhtstatus.c | 104 ---------------------------------------------- src/dhtstatus.h | 8 ---- src/windows.c | 6 +-- 4 files changed, 2 insertions(+), 120 deletions(-) delete mode 100644 src/dhtstatus.c delete mode 100644 src/dhtstatus.h diff --git a/build/Makefile.am b/build/Makefile.am index 4e747ec..dc6a588 100644 --- a/build/Makefile.am +++ b/build/Makefile.am @@ -11,9 +11,7 @@ toxic_SOURCES = $(top_srcdir)/src/main.c \ $(top_srcdir)/src/friendlist.h \ $(top_srcdir)/src/friendlist.c \ $(top_srcdir)/src/toxic_windows.h \ - $(top_srcdir)/src/windows.c \ - $(top_srcdir)/src/dhtstatus.h \ - $(top_srcdir)/src/dhtstatus.c + $(top_srcdir)/src/windows.c toxic_CFLAGS = -I$(top_srcdir) \ $(NCURSES_CFLAGS) \ diff --git a/src/dhtstatus.c b/src/dhtstatus.c deleted file mode 100644 index 5df5666..0000000 --- a/src/dhtstatus.c +++ /dev/null @@ -1,104 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "dhtstatus.h" -#include "string.h" - -typedef uint8_t ipbuf[3 * 4 + 3 + 1]; -static int num_selected = 0; - -static void printip(ipbuf buf, tox_IP ip) -{ - 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, Tox *m, wint_t key) -{ - switch (key) { - case KEY_UP: - case 'k': - if (--num_selected < 0) - num_selected = TOX_CLIENT_ID_SIZE - 1; - - break; - - case KEY_DOWN: - case 'j': - num_selected = (num_selected + 1) % TOX_CLIENT_ID_SIZE; - break; - - case '\n': - break; - - default: - break; - } -} - -static void dhtstatus_onDraw(ToxWindow *self, Tox *m) -{ - /* - Client_data *close_clientlist = DHT_get_close_list(m->dht); - curs_set(0); - werase(self->window); - - uint64_t now = unix_time(); - uint32_t i, j; - ipbuf ipbuf; - 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*/ - /* - Client_data *client = close_clientlist + i; - - if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); - - wprintw(self->window, "[%02i] ", i); - uint16_t port = ntohs(client->ip_port.port); - - if (port) { - for (j = 0; j < CLIENT_ID_SIZE; j++) - wprintw(self->window, "%02hhx", client->client_id[j]); - - printip(ipbuf, client->ip_port.ip); - wprintw(self->window, " %15s %5u ", ipbuf, port); - 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)); - } - - wrefresh(self->window); - */ -} - -static void dhtstatus_onInit(ToxWindow *self, Tox *m) -{ - -} - -ToxWindow new_dhtstatus() -{ - ToxWindow ret; - memset(&ret, 0, sizeof(ret)); - - ret.onKey = &dhtstatus_onKey; - ret.onDraw = &dhtstatus_onDraw; - ret.onInit = &dhtstatus_onInit; - - strcpy(ret.title, "[dht status]"); - return ret; -} diff --git a/src/dhtstatus.h b/src/dhtstatus.h deleted file mode 100644 index 3c4a55c..0000000 --- a/src/dhtstatus.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef _dhtstatus_h -#define _dhtstatus_h - -#include "toxic_windows.h" - -ToxWindow new_dhtstatus(); - -#endif diff --git a/src/windows.c b/src/windows.c index f2ddefb..ffa71f4 100644 --- a/src/windows.c +++ b/src/windows.c @@ -7,7 +7,6 @@ #include "friendlist.h" #include "prompt.h" -#include "dhtstatus.h" #include "toxic_windows.h" extern char *DATA_FILE; @@ -167,10 +166,7 @@ ToxWindow *init_windows() { int n_prompt = add_window(m, new_prompt()); - if (n_prompt == -1 - || add_window(m, new_friendlist()) == -1 - //|| add_window(m, new_dhtstatus()) == -1 - ) { + if (n_prompt == -1 || add_window(m, new_friendlist()) == -1) { fprintf(stderr, "add_window() failed.\n"); endwin(); exit(1); From f58614e9dce0b3348a14d9379e2d84669fc00cb0 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Tue, 27 Aug 2013 19:38:05 -0700 Subject: [PATCH 27/82] Removed DHT Bootstrap servers that can not be bootstrapped from. --- misc/DHTservers | 2 -- 1 file changed, 2 deletions(-) diff --git a/misc/DHTservers b/misc/DHTservers index bf2d272..28fe107 100644 --- a/misc/DHTservers +++ b/misc/DHTservers @@ -2,8 +2,6 @@ 66.175.223.88 33445 AC4112C975240CAD260BB2FCD134266521FAAF0A5D159C5FD3201196191E4F5D 192.184.81.118 33445 5CD7EB176C19A2FD840406CD56177BB8E75587BB366F7BB3004B19E3EDC04143 192.210.149.121 33445 F404ABAA1C99A9D37D61AB54898F56793E1DEF8BD46B1038B9D822E8460FAB67 -81.224.34.47 443 48F0D94C0D54EB1995A2ECEDE7DB6BDD5E05D81704B2F3D1BB9FE43AC97B7269 198.46.136.167 33445 728925473812C7AAC482BE7250BCCAD0B8CB9F737BF3D42ABD34459C1768F854 95.47.140.214 33445 F4BF7C5A9D0EF4CB684090C38DE937FAE1612021F21FEA4DCBFAC6AAFEF58E68 54.215.145.71 33445 6EDDEE2188EF579303C0766B4796DCBA89C93058B6032FEA51593DCD42FB746C -66.74.30.125 33445 7155386A691E7BD3C4C0589D70ACDA191D488634772885CCED5DD7B3F7E6310D From f24eebaeed5ac6096744916298a94472e20cefce Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 28 Aug 2013 05:46:09 -0400 Subject: [PATCH 28/82] implemented friend deletion --- src/chat.c | 1 - src/friendlist.c | 113 +++++++++++++++++++++++++++++++++++++---------- src/windows.c | 2 +- 3 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/chat.c b/src/chat.c index 0054f90..9359095 100644 --- a/src/chat.c +++ b/src/chat.c @@ -119,7 +119,6 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1 wattroff(ctx->history, COLOR_PAIR(2)); status[len - 1] = '\0'; - 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); diff --git a/src/friendlist.c b/src/friendlist.c index 768ab7f..5d0e18d 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -9,17 +9,21 @@ #include #include #include +#include #include #include "friendlist.h" +extern char *DATA_FILE; +extern int store_data(Tox *m, char *path); typedef struct { uint8_t name[TOX_MAX_NAME_LENGTH]; uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH]; int num; int chatwin; + bool active; } friend_t; static friend_t friends[MAX_FRIENDS_NUM]; @@ -33,7 +37,7 @@ void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16 return; if (friends[num].chatwin == -1) { - friends[num].chatwin = add_window(m, new_chat(m, num)); + friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); } } @@ -60,30 +64,87 @@ int friendlist_onFriendAdded(Tox *m, int num) if (num_friends == MAX_FRIENDS_NUM) return -1; - friends[num_friends].num = num; - tox_getname(m, num, friends[num_friends].name); - strcpy((char *) friends[num_friends].name, "unknown"); - strcpy((char *) friends[num_friends].status, "unknown"); - friends[num_friends++].chatwin = -1; - return 0; + int i; + + for (i = 0; i <= num_friends; ++i) { + if (!friends[i].active) { + friends[i].num = num; + friends[i].active = true; + friends[i].chatwin = -1; + tox_getname(m, num, friends[i].name); + strcpy((char *) friends[i].name, "unknown"); + strcpy((char *) friends[i].status, "unknown"); + + if (i == num_friends) + ++num_friends; + + return 0; + } + } + + return -1; +} + +static void select_friend(wint_t key) +{ + if (num_friends < 1) + return; + + int n = num_selected; + + if (key == KEY_UP) { + while (true) { + if (--n < 0) + n = num_friends-1; + if (friends[n].active) { + num_selected = n; + return; + } + } + } else if (key == KEY_DOWN) { + while (true) { + n = (n + 1) % num_friends; + if (friends[n].active) { + num_selected = n; + return; + } + } + } +} + +static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) +{ + tox_delfriend(m, f_num); + memset(&(friends[f_num]), 0, sizeof(friend_t)); + friends[f_num].active = false; + + int i; + + for (i = num_friends; i != 0; --i) { + if (friends[i-1].active == true) + break; + } + + if (store_data(m, DATA_FILE)) + wprintw(self->window, "\nFailed to store messenger data\n"); + + num_friends = i; + select_friend(KEY_DOWN); } static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key) { - if (key == KEY_UP) { - if (--num_selected < 0) - num_selected = num_friends - 1; - } else if (key == KEY_DOWN) { - if (num_friends != 0) - num_selected = (num_selected + 1) % num_friends; + if (key == KEY_UP || key == KEY_DOWN) { + select_friend(key); } else if (key == '\n') { /* Jump to chat window if already open */ if (friends[num_selected].chatwin != -1) { set_active_window(friends[num_selected].chatwin); } else { - friends[num_selected].chatwin = add_window(m, new_chat(m, num_selected)); + friends[num_selected].chatwin = add_window(m, new_chat(m, friends[num_selected].num)); } - } + } else if (key == 0x107 || key == 0x8 || key == 0x7f) + delete_friend(m, self, num_selected, key); } static void friendlist_onDraw(ToxWindow *self, Tox *m) @@ -95,25 +156,31 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) 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"); + wprintw(self->window, " * Open chat with up/down keys and enter. "); + wprintw(self->window, "Delete friends with the backspace key\n"); wattroff(self->window, COLOR_PAIR(2) | A_BOLD); } wprintw(self->window, "\n"); + int i; for (i = 0; i < num_friends; ++i) { - if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); + if (friends[i].active == true) { + if (i == num_selected) + wattron(self->window, COLOR_PAIR(3)); - wprintw(self->window, " [#%d] ", friends[i].num); + wprintw(self->window, " > ", 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); - wprintw(self->window, "%s ", friends[i].name); - attroff(A_BOLD); + attron(A_BOLD); + wprintw(self->window, "%s ", friends[i].name); + attroff(A_BOLD); - wprintw(self->window, "(%s)\n", friends[i].status); + wprintw(self->window, "(%s)\n", friends[i].status); + } } wrefresh(self->window); diff --git a/src/windows.c b/src/windows.c index ffa71f4..48b7fc2 100644 --- a/src/windows.c +++ b/src/windows.c @@ -195,7 +195,7 @@ static void draw_bar() int i; - for (i = 0; i < (MAX_WINDOWS_NUM); ++i) { + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i].window) { if (windows + i == active_window) attron(A_BOLD); From c08639b8e1083e7d69a249023b8ed9cfcb7a9ba2 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 28 Aug 2013 05:53:47 -0400 Subject: [PATCH 29/82] small fixes --- src/friendlist.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index 5d0e18d..35c6295 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -73,7 +73,7 @@ int friendlist_onFriendAdded(Tox *m, int num) friends[i].chatwin = -1; tox_getname(m, num, friends[i].name); strcpy((char *) friends[i].name, "unknown"); - strcpy((char *) friends[i].status, "unknown"); + strcpy((char *) friends[i].status, "Offline"); if (i == num_friends) ++num_friends; @@ -121,7 +121,7 @@ static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) int i; for (i = num_friends; i != 0; --i) { - if (friends[i-1].active == true) + if (friends[i-1].active) break; } @@ -166,11 +166,11 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) int i; for (i = 0; i < num_friends; ++i) { - if (friends[i].active == true) { + if (friends[i].active) { if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); - wprintw(self->window, " > ", friends[i].num); + wprintw(self->window, " > "); if (i == num_selected) wattroff(self->window, COLOR_PAIR(3)); From 4616aef0709e391480bccced8288f6f2ecca88d3 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Wed, 28 Aug 2013 10:03:21 -0700 Subject: [PATCH 30/82] Improved msvc compatibility --- src/configdir.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/configdir.c b/src/configdir.c index 193b162..d8bb9ed 100644 --- a/src/configdir.c +++ b/src/configdir.c @@ -29,7 +29,7 @@ #include #include -#ifdef WIN32 +#ifdef _WIN32 #include #include #else /* WIN32 */ @@ -49,7 +49,7 @@ char *get_user_config_dir(void) { char *user_config_dir; -#ifdef __WIN32__ +#ifdef _WIN32 #warning Please fix configdir for Win32 return NULL; #if 0 @@ -130,7 +130,7 @@ char *get_user_config_dir(void) */ int create_user_config_dir(char *path) { -#ifdef __WIN32__ +#ifdef _WIN32 #warning Please fix configdir for Win32 return -1; #if 0 From e6835483ce6a4b98195058f1b3e26d41dc2276b0 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Wed, 28 Aug 2013 10:04:54 -0700 Subject: [PATCH 31/82] Improved msvc compatibility --- src/main.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 69450ce..f20ce76 100644 --- a/src/main.c +++ b/src/main.c @@ -20,7 +20,7 @@ #include #include -#ifdef WIN32 +#ifdef _WIN32 #include #include #include @@ -98,7 +98,7 @@ static Tox *init_tox() tox_callback_action(m, on_action, NULL); #ifdef __linux__ tox_setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy")); -#elif defined(WIN32) +#elif defined(_WIN32) tox_setname(m, (uint8_t *) "I should install GNU/Linux", sizeof("I should install GNU/Linux")); #elif defined(__APPLE__) tox_setname(m, (uint8_t *) "Hipster", sizeof("Hipster")); //This used to users of other Unixes are hipsters @@ -128,7 +128,7 @@ uint32_t resolve_addr(const char *address) hints.ai_family = AF_INET; // IPv4 only right now. hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. -#ifdef __WIN32__ +#ifdef _WIN32 int res; WSADATA wsa_data; @@ -142,7 +142,7 @@ uint32_t resolve_addr(const char *address) // Lookup failed. if (rc != 0) { -#ifdef __WIN32__ +#ifdef _WIN32 WSACleanup(); #endif return 0; @@ -151,7 +151,7 @@ uint32_t resolve_addr(const char *address) // IPv4 records only.. if (server->ai_family != AF_INET) { freeaddrinfo(server); -#ifdef __WIN32__ +#ifdef _WIN32 WSACleanup(); #endif return 0; @@ -161,7 +161,7 @@ uint32_t resolve_addr(const char *address) addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; freeaddrinfo(server); -#ifdef __WIN32__ +#ifdef _WIN32 WSACleanup(); #endif return addr; From 01401ea3caee4d9f964c82e570f2747f72931e35 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Wed, 28 Aug 2013 10:16:35 -0700 Subject: [PATCH 32/82] Adjusted it to compile -wall, -c89 --- Makefile.am | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile.am b/Makefile.am index 630a1b0..1d4a528 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,3 +2,4 @@ SUBDIRS = build misc ACLOCAL_AMFLAGS = -I m4 +AM_CXXFLAGS = -wall -c89 From 4f2e80ef28b80b1d7c1c83e0963f6eb526a5ce58 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 28 Aug 2013 14:47:47 -0400 Subject: [PATCH 33/82] added infinite loop check --- src/friendlist.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index 35c6295..6e420bf 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -10,6 +10,7 @@ #include #include #include +#include #include @@ -61,7 +62,7 @@ void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t int friendlist_onFriendAdded(Tox *m, int num) { - if (num_friends == MAX_FRIENDS_NUM) + if (num_friends < 0 || num_friends >= MAX_FRIENDS_NUM) return -1; int i; @@ -91,15 +92,19 @@ static void select_friend(wint_t key) return; int n = num_selected; + int f_inf = num_selected; if (key == KEY_UP) { while (true) { - if (--n < 0) - n = num_friends-1; + if (--n < 0) n = num_friends-1; if (friends[n].active) { num_selected = n; return; } + if (n == f_inf) { + endwin(); + exit(2); + } } } else if (key == KEY_DOWN) { while (true) { @@ -108,6 +113,10 @@ static void select_friend(wint_t key) num_selected = n; return; } + if (n == f_inf) { + endwin(); + exit(2); + } } } } @@ -120,7 +129,7 @@ static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) int i; - for (i = num_friends; i != 0; --i) { + for (i = num_friends; i > 0; --i) { if (friends[i-1].active) break; } From 0c3115e784597ad38cbe4bd3e01520883f74719f Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Wed, 28 Aug 2013 19:13:37 -0700 Subject: [PATCH 34/82] Updated platos's bootstrap server key --- misc/DHTservers | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/DHTservers b/misc/DHTservers index 28fe107..b5da5ee 100644 --- a/misc/DHTservers +++ b/misc/DHTservers @@ -1,5 +1,5 @@ 192.81.133.111 33445 8CD5A9BF0A6CE358BA36F7A653F99FA6B258FF756E490F52C1F98CC420F78858 -66.175.223.88 33445 AC4112C975240CAD260BB2FCD134266521FAAF0A5D159C5FD3201196191E4F5D +66.175.223.88 33445 B24E2FB924AE66D023FE1E42A2EE3B432010206F751A2FFD3E297383ACF1572E 192.184.81.118 33445 5CD7EB176C19A2FD840406CD56177BB8E75587BB366F7BB3004B19E3EDC04143 192.210.149.121 33445 F404ABAA1C99A9D37D61AB54898F56793E1DEF8BD46B1038B9D822E8460FAB67 198.46.136.167 33445 728925473812C7AAC482BE7250BCCAD0B8CB9F737BF3D42ABD34459C1768F854 From 9ac42daf2b5159a7938810f15b9c240918afd88b Mon Sep 17 00:00:00 2001 From: Giuliano Schneider Date: Thu, 29 Aug 2013 17:06:05 +0200 Subject: [PATCH 35/82] fixed "free(): invalid pointer" when XDG_CONFIG_HOME is set --- src/configdir.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/configdir.c b/src/configdir.c index d8bb9ed..8f43bc9 100644 --- a/src/configdir.c +++ b/src/configdir.c @@ -107,7 +107,8 @@ char *get_user_config_dir(void) snprintf(user_config_dir, len, "%s/Library/Application Support", home); # else /* __APPLE__ */ - if (!(user_config_dir = getenv("XDG_CONFIG_HOME"))) { + const char *tmp; + if (!(tmp = getenv("XDG_CONFIG_HOME"))) { len = strlen(home) + strlen("/.config") + 1; user_config_dir = malloc(len); @@ -116,6 +117,8 @@ char *get_user_config_dir(void) } snprintf(user_config_dir, len, "%s/.config", home); + } else { + user_config_dir = strdup(tmp); } # endif /* __APPLE__ */ From ec7e458800439d2440b158013e7fccc4930bc6be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Arg=C3=BCelles?= Date: Thu, 29 Aug 2013 21:56:27 -0500 Subject: [PATCH 36/82] Fix blocking while waiting for key The timeout() for input is set on main window (stdscr), since it seems that for some ncurses implementations get_wch and getch are the same (non widechar) wget_wch is used instead. The window passed must the the main one, the others doesn't have the timeout settings. Another option is to set wtimeout() on every window. --- src/windows.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/windows.c b/src/windows.c index ffa71f4..1795c4b 100644 --- a/src/windows.c +++ b/src/windows.c @@ -239,7 +239,7 @@ void draw_active_window(Tox *m) /* Handle input */ #ifdef HAVE_WIDECHAR - wget_wch(a->window, &ch); + wget_wch(stdscr, &ch); #else ch = getch(); #endif From 3d062ca15b15fe7d8b9fc601034bf37fdfce5892 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 29 Aug 2013 23:14:09 -0400 Subject: [PATCH 37/82] potential seg fault --- src/friendlist.c | 5 +---- src/windows.c | 4 ---- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index 6e420bf..ab49b05 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -125,7 +125,6 @@ static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) { tox_delfriend(m, f_num); memset(&(friends[f_num]), 0, sizeof(friend_t)); - friends[f_num].active = false; int i; @@ -166,12 +165,10 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) } else { wattron(self->window, COLOR_PAIR(2) | A_BOLD); wprintw(self->window, " * Open chat with up/down keys and enter. "); - wprintw(self->window, "Delete friends with the backspace key\n"); + wprintw(self->window, "Delete friends with the backspace key\n\n"); wattroff(self->window, COLOR_PAIR(2) | A_BOLD); } - wprintw(self->window, "\n"); - int i; for (i = 0; i < num_friends; ++i) { diff --git a/src/windows.c b/src/windows.c index 48b7fc2..4319c00 100644 --- a/src/windows.c +++ b/src/windows.c @@ -121,10 +121,6 @@ void del_window(ToxWindow *w) { active_window = windows; // Go to prompt screen delwin(w->window); - - if (w->x) - free(w->x); - w->window = NULL; memset(w, 0, sizeof(ToxWindow)); clear(); From b99ce9ce46b40f86d63dcd4511d656b71780c74f Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 30 Aug 2013 20:13:29 -0400 Subject: [PATCH 38/82] proper fix for segfault, credit to manuel-arguelles --- src/chat.c | 32 +++++++++++++++++++------------- src/windows.c | 3 ++- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/chat.c b/src/chat.c index 9359095..2dd6950 100644 --- a/src/chat.c +++ b/src/chat.c @@ -212,10 +212,18 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); + bool close_win = false; - if (line[0] == '/') - execute(self, ctx, m, line); - else { + if (line[0] == '/') { + if (close_win = !strncmp(line, "/close", strlen("/close"))) { + int f_num = ctx->friendnum; + delwin(ctx->linewin); + del_window(self); + disable_chatwin(f_num); + } else { + execute(self, ctx, m, line); + } + } else { /* make sure the string has at least non-space character */ if (!string_is_empty(line)) { uint8_t selfname[TOX_MAX_NAME_LENGTH]; @@ -237,8 +245,13 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) } } - ctx->line[0] = L'\0'; - ctx->pos = 0; + if (close_win) + free(ctx); + else { + ctx->line[0] = L'\0'; + ctx->pos = 0; + } + free(line); } } @@ -368,13 +381,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) wprintw(ctx->history, "%s\n", id); } - else if (strcmp(cmd, "/close") == 0) { - int f_num = ctx->friendnum; - delwin(ctx->linewin); - del_window(self); - disable_chatwin(f_num); - } - else wprintw(ctx->history, "Invalid command.\n"); } @@ -439,7 +445,7 @@ ToxWindow new_chat(Tox *m, int friendnum) snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); ChatContext *x = calloc(1, sizeof(ChatContext)); + ret.x = x; x->friendnum = friendnum; - ret.x = (void *) x; return ret; } diff --git a/src/windows.c b/src/windows.c index 4319c00..e477928 100644 --- a/src/windows.c +++ b/src/windows.c @@ -120,9 +120,10 @@ int add_window(Tox *m, ToxWindow w) void del_window(ToxWindow *w) { active_window = windows; // Go to prompt screen + delwin(w->window); - w->window = NULL; memset(w, 0, sizeof(ToxWindow)); + clear(); refresh(); } From bfeea477748f35e53b508767017c90526fa709c8 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 31 Aug 2013 02:22:07 -0400 Subject: [PATCH 39/82] fix buffer overflows and format issues --- src/chat.c | 15 ++++++++------- src/windows.c | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/chat.c b/src/chat.c index 2dd6950..7ec3631 100644 --- a/src/chat.c +++ b/src/chat.c @@ -72,6 +72,9 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin if (ctx->friendnum != num) return; + uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; + tox_getname(m, num, (uint8_t *) &nick); + action[len - 1] = '\0'; wattron(ctx->history, COLOR_PAIR(2)); @@ -79,7 +82,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin wattroff(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(5)); - wprintw(ctx->history, "%s\n", action); + wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(5)); self->blink = true; @@ -187,7 +190,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) #else if (isprint(key)) { #endif - if (ctx->pos != sizeof(ctx->line) - 1) { + if (ctx->pos < MAX_STR_SIZE) { mvwaddstr(self->window, y, x, wc_to_char(key)); ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = L'\0'; @@ -291,15 +294,13 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) wattroff(ctx->history, COLOR_PAIR(2)); uint8_t selfname[TOX_MAX_NAME_LENGTH]; - int len = tox_getselfname(m, selfname, sizeof(selfname)); - char msg[MAX_STR_SIZE - len - 4]; - snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action); + tox_getselfname(m, selfname, sizeof(selfname)); wattron(ctx->history, COLOR_PAIR(5)); - wprintw(ctx->history, msg); + wprintw(ctx->history, "* %s %s\n", selfname, action); wattroff(ctx->history, COLOR_PAIR(5)); - if (tox_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) { + if (tox_sendaction(m, ctx->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(3)); diff --git a/src/windows.c b/src/windows.c index e477928..ec6c04a 100644 --- a/src/windows.c +++ b/src/windows.c @@ -120,7 +120,7 @@ int add_window(Tox *m, ToxWindow w) void del_window(ToxWindow *w) { active_window = windows; // Go to prompt screen - + delwin(w->window); memset(w, 0, sizeof(ToxWindow)); @@ -187,7 +187,7 @@ static void draw_bar() move(LINES - 1, 0); attron(COLOR_PAIR(4) | A_BOLD); - printw(" TOXIC " TOXICVER "|"); + printw(" TOXIC " TOXICVER " |"); attroff(COLOR_PAIR(4) | A_BOLD); int i; From fba685695c3c5175baf84aac81741d18533f7e1d Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Sun, 1 Sep 2013 16:32:01 -0700 Subject: [PATCH 40/82] Fixed the flags --- Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile.am b/Makefile.am index 1d4a528..5cc1266 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,4 +2,4 @@ SUBDIRS = build misc ACLOCAL_AMFLAGS = -I m4 -AM_CXXFLAGS = -wall -c89 +AM_CFLAGS = -wall -c89 -werror From c5b9677fc0edf5921826faa12332980a41c29808 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 1 Sep 2013 22:11:47 -0400 Subject: [PATCH 41/82] properly implemented statuses --- src/friendlist.c | 47 +++++++++++++++++++++++++++++++++++------------ src/prompt.c | 6 +++--- src/windows.c | 3 +-- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index ab49b05..cb51cbb 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -24,7 +24,7 @@ typedef struct { uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH]; int num; int chatwin; - bool active; + bool active; } friend_t; static friend_t friends[MAX_FRIENDS_NUM]; @@ -72,9 +72,8 @@ int friendlist_onFriendAdded(Tox *m, int num) friends[i].num = num; friends[i].active = true; friends[i].chatwin = -1; - tox_getname(m, num, friends[i].name); + //tox_getname(m, num, friends[i].name); strcpy((char *) friends[i].name, "unknown"); - strcpy((char *) friends[i].status, "Offline"); if (i == num_friends) ++num_friends; @@ -173,19 +172,43 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) for (i = 0; i < num_friends; ++i) { if (friends[i].active) { - if (i == num_selected) - wattron(self->window, COLOR_PAIR(3)); - - wprintw(self->window, " > "); + bool is_online = tox_friendstatus(m, i) == TOX_FRIEND_ONLINE; + TOX_USERSTATUS status = tox_get_userstatus(m, i); if (i == num_selected) - wattroff(self->window, COLOR_PAIR(3)); + wprintw(self->window, " > "); + else + wprintw(self->window, " "); - attron(A_BOLD); - wprintw(self->window, "%s ", friends[i].name); - attroff(A_BOLD); + if (is_online) { + int colour; - wprintw(self->window, "(%s)\n", friends[i].status); + switch(status) { + case TOX_USERSTATUS_NONE: + colour = 1; + break; + + case TOX_USERSTATUS_AWAY: + colour = 5; + break; + + case TOX_USERSTATUS_BUSY: + case TOX_USERSTATUS_INVALID: + default: + colour = 3; + break; + } + + wattron(self->window, COLOR_PAIR(colour)); + attron(A_BOLD); + wprintw(self->window, "%s \n", friends[i].name); + attroff(A_BOLD); + wattroff(self->window, COLOR_PAIR(colour)); + } else { + attron(A_BOLD); + wprintw(self->window, "%s \n", friends[i].name); + attroff(A_BOLD); + } } } diff --git a/src/prompt.c b/src/prompt.c index 5fb4334..521f624 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -294,13 +294,13 @@ void cmd_status(ToxWindow *self, Tox *m, char **args) if (!strncmp(status, "online", strlen("online"))) { status_kind = TOX_USERSTATUS_NONE; - status_text = "ONLINE"; + status_text = "Online"; } else if (!strncmp(status, "away", strlen("away"))) { status_kind = TOX_USERSTATUS_AWAY; - status_text = "AWAY"; + status_text = "Away"; } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = TOX_USERSTATUS_BUSY; - status_text = "BUSY"; + status_text = "Busy"; } else { wprintw(self->window, "Invalid status.\n"); return; diff --git a/src/windows.c b/src/windows.c index 4fcb653..83d60df 100644 --- a/src/windows.c +++ b/src/windows.c @@ -84,9 +84,8 @@ void on_friendadded(Tox *m, int friendnumber) { friendlist_onFriendAdded(m, friendnumber); - if (store_data(m, DATA_FILE)) { + if (store_data(m, DATA_FILE)) wprintw(prompt->window, "\nCould not store Tox data\n"); - } } /* CALLBACKS END */ From 73d6fba055869063b0a105188776561758b523d7 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 2 Sep 2013 00:18:20 -0400 Subject: [PATCH 42/82] display status messages in friends list --- src/chat.c | 6 +++--- src/friendlist.c | 20 ++++++++++---------- src/friendlist.h | 2 ++ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/chat.c b/src/chat.c index 7ec3631..75f68a3 100644 --- a/src/chat.c +++ b/src/chat.c @@ -322,17 +322,17 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) if (!strncmp(status, "online", strlen("online"))) { status_kind = TOX_USERSTATUS_NONE; - status_text = "ONLINE"; + status_text = "Online"; } else if (!strncmp(status, "away", strlen("away"))) { status_kind = TOX_USERSTATUS_AWAY; - status_text = "AWAY"; + status_text = "Away"; } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = TOX_USERSTATUS_BUSY; - status_text = "BUSY"; + status_text = "Busy"; } else { diff --git a/src/friendlist.c b/src/friendlist.c index cb51cbb..6d48b0f 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -21,7 +21,7 @@ extern int store_data(Tox *m, char *path); typedef struct { uint8_t name[TOX_MAX_NAME_LENGTH]; - uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH]; + uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; int num; int chatwin; bool active; @@ -56,8 +56,8 @@ void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends) return; - memcpy((char *) &friends[num].status, (char *) str, len); - friends[num].status[len] = 0; + memcpy((char *) &friends[num].statusmsg, (char *) str, len); + friends[num].statusmsg[len] = 0; } int friendlist_onFriendAdded(Tox *m, int num) @@ -74,6 +74,7 @@ int friendlist_onFriendAdded(Tox *m, int num) friends[i].chatwin = -1; //tox_getname(m, num, friends[i].name); strcpy((char *) friends[i].name, "unknown"); + strcpy((char *) friends[i].statusmsg, NOSTATUSMSG); if (i == num_friends) ++num_friends; @@ -173,7 +174,6 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) for (i = 0; i < num_friends; ++i) { if (friends[i].active) { bool is_online = tox_friendstatus(m, i) == TOX_FRIEND_ONLINE; - TOX_USERSTATUS status = tox_get_userstatus(m, i); if (i == num_selected) wprintw(self->window, " > "); @@ -181,6 +181,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wprintw(self->window, " "); if (is_online) { + TOX_USERSTATUS status = tox_get_userstatus(m, i); int colour; switch(status) { @@ -200,14 +201,13 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) } wattron(self->window, COLOR_PAIR(colour)); - attron(A_BOLD); - wprintw(self->window, "%s \n", friends[i].name); - attroff(A_BOLD); + wprintw(self->window, "%s ", friends[i].name); wattroff(self->window, COLOR_PAIR(colour)); + + if (strncmp(friends[i].statusmsg, NOSTATUSMSG, strlen(NOSTATUSMSG))) + wprintw(self->window, "(%s)\n", friends[i].statusmsg); } else { - attron(A_BOLD); - wprintw(self->window, "%s \n", friends[i].name); - attroff(A_BOLD); + wprintw(self->window, "%s (Offline)\n", friends[i].name); } } } diff --git a/src/friendlist.h b/src/friendlist.h index 26c9106..6b21bac 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -4,6 +4,8 @@ #include "toxic_windows.h" #include "chat.h" +#define NOSTATUSMSG "NOSTATUSMSG" /* Friends' default status message */ + ToxWindow new_friendlist(); int friendlist_onFriendAdded(Tox *m, int num); void disable_chatwin(int f_num); From 713f2347bc4a464876aa233c0418d2b3298c1030 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 2 Sep 2013 01:14:51 -0400 Subject: [PATCH 43/82] add statusmsg command to chat windows --- src/chat.c | 10 +++++++++- src/prompt.c | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/chat.c b/src/chat.c index 75f68a3..db98c58 100644 --- a/src/chat.c +++ b/src/chat.c @@ -353,6 +353,13 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) } } + else if (!strncmp(cmd, "/statusmsg ", strlen("/statusmsg "))) { + char *msg = strchr(cmd, ' '); + msg++; + wprintw(ctx->history, "Status message set to: %s\n", msg); + tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); + } + else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { char *nick; nick = strchr(cmd, ' '); @@ -416,12 +423,13 @@ void print_help(ChatContext *self) wattroff(self->history, A_BOLD); wprintw(self->history, " /status : Set your status\n"); + wprintw(self->history, " /statusmsg : Set your status message\n"); wprintw(self->history, " /nick : Set your nickname\n"); wprintw(self->history, " /me : Do an action\n"); wprintw(self->history, " /myid : Print your ID\n"); wprintw(self->history, " /clear : Clear the screen\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 Toxic\n"); wprintw(self->history, " /help : Print this message again\n\n"); wattroff(self->history, COLOR_PAIR(2)); diff --git a/src/prompt.c b/src/prompt.c index 521f624..97dcd9b 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -228,7 +228,7 @@ void cmd_help(ToxWindow *self, Tox *m, char **args) wprintw(self->window, " mynick : Print your current nickname\n"); wprintw(self->window, " accept : Accept friend request\n"); wprintw(self->window, " myid : Print your ID\n"); - wprintw(self->window, " quit/exit : Exit program\n"); + wprintw(self->window, " quit/exit : Exit Toxic\n"); wprintw(self->window, " help : Print this message again\n"); wprintw(self->window, " clear : Clear this window\n"); From ed683504243313df432f97d2757f30eb61469056 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 2 Sep 2013 05:15:29 -0400 Subject: [PATCH 44/82] format fixes --- src/chat.c | 11 +++-------- src/friendlist.c | 9 +++++---- src/prompt.c | 8 ++++---- src/windows.c | 4 ++-- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/chat.c b/src/chat.c index db98c58..b885197 100644 --- a/src/chat.c +++ b/src/chat.c @@ -104,9 +104,7 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t nick[len - 1] = '\0'; 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) @@ -123,10 +121,7 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1 status[len - 1] = '\0'; - wattron(ctx->history, COLOR_PAIR(3)); - wprintw(ctx->history, "* Your partner changed status to '%s'\n", status); - wattroff(ctx->history, COLOR_PAIR(3)); - + wprintw(ctx->history, "* Your partner changed status message to '%s'\n", status); } /* check that the string has one non-space character */ @@ -344,12 +339,12 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) if (msg == NULL) { tox_set_userstatus(m, status_kind); - wprintw(ctx->history, "Status set to: %s\n", status_text); + wprintw(ctx->history, "Status message set to: %s\n", status_text); } else { msg++; tox_set_userstatus(m, status_kind); tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); - wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); + wprintw(ctx->history, "Status message set to: %s, %s\n", status_text, msg); } } diff --git a/src/friendlist.c b/src/friendlist.c index 6d48b0f..817513d 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -173,7 +173,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) for (i = 0; i < num_friends; ++i) { if (friends[i].active) { - bool is_online = tox_friendstatus(m, i) == TOX_FRIEND_ONLINE; + bool is_online = tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE; if (i == num_selected) wprintw(self->window, " > "); @@ -181,8 +181,8 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wprintw(self->window, " "); if (is_online) { - TOX_USERSTATUS status = tox_get_userstatus(m, i); - int colour; + TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num); + int colour = 3; switch(status) { case TOX_USERSTATUS_NONE: @@ -206,8 +206,9 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) if (strncmp(friends[i].statusmsg, NOSTATUSMSG, strlen(NOSTATUSMSG))) wprintw(self->window, "(%s)\n", friends[i].statusmsg); + } else { - wprintw(self->window, "%s (Offline)\n", friends[i].name); + wprintw(self->window, "(Offline)\n"); } } } diff --git a/src/prompt.c b/src/prompt.c index 97dcd9b..ef0b2d6 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -223,7 +223,7 @@ void cmd_help(ToxWindow *self, Tox *m, char **args) wprintw(self->window, " connect : Connect to DHT server\n"); wprintw(self->window, " add : Add friend\n"); wprintw(self->window, " status : Set your status\n"); - wprintw(self->window, " statusmsg : Set your status\n"); + wprintw(self->window, " statusmsg : Set your status message\n"); wprintw(self->window, " nick : Set your nickname\n"); wprintw(self->window, " mynick : Print your current nickname\n"); wprintw(self->window, " accept : Accept friend request\n"); @@ -310,11 +310,11 @@ void cmd_status(ToxWindow *self, Tox *m, char **args) if (msg == NULL) { tox_set_userstatus(m, status_kind); - wprintw(self->window, "Status set to: %s\n", status_text); + wprintw(self->window, "Status message set to: %s\n", status_text); } else { tox_set_userstatus(m, status_kind); tox_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 message set to: %s, %s\n", status_text, msg); } } @@ -322,7 +322,7 @@ void cmd_statusmsg(ToxWindow *self, Tox *m, char **args) { char *msg = args[1]; tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); - wprintw(self->window, "Status set to: %s\n", msg); + wprintw(self->window, "Status message set to: %s\n", msg); } static void execute(ToxWindow *self, Tox *m, char *u_cmd) diff --git a/src/windows.c b/src/windows.c index 83d60df..2ae9806 100644 --- a/src/windows.c +++ b/src/windows.c @@ -60,7 +60,7 @@ void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void void on_nickchange(Tox *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(nick change) %d: %s\n", friendnumber, string); int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { @@ -71,7 +71,7 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v void on_statuschange(Tox *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(message change) %d: %s\n", friendnumber, string); int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { From dbe84898b9d802ffec7c764de2bd7e0eeffab4a4 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 2 Sep 2013 05:16:54 -0400 Subject: [PATCH 45/82] oops oops oops --- src/friendlist.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index 817513d..c4dd3cf 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -182,7 +182,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) if (is_online) { TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num); - int colour = 3; + int colour; switch(status) { case TOX_USERSTATUS_NONE: @@ -208,7 +208,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wprintw(self->window, "(%s)\n", friends[i].statusmsg); } else { - wprintw(self->window, "(Offline)\n"); + wprintw(self->window, "%s (Offline)\n", friends[i].name); } } } From a0e3b7dfd108042da04e5d1f26a7b097081dbae7 Mon Sep 17 00:00:00 2001 From: Luke Champine Date: Mon, 2 Sep 2013 13:19:25 -0400 Subject: [PATCH 46/82] refactor command argument parsing --- src/prompt.c | 246 ++++++++++++++++++++++++++++----------------------- 1 file changed, 133 insertions(+), 113 deletions(-) diff --git a/src/prompt.c b/src/prompt.c index 5fb4334..4b06477 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -1,6 +1,6 @@ /* -* Toxic -- Tox Curses Client -*/ + * Toxic -- Tox Curses Client + */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -22,40 +22,39 @@ static char prompt_buf[MAX_STR_SIZE] = {0}; static int prompt_buf_pos = 0; /* commands */ -void cmd_accept(ToxWindow *, Tox *m, char **); -void cmd_add(ToxWindow *, Tox *m, char **); -void cmd_clear(ToxWindow *, Tox *m, char **); -void cmd_connect(ToxWindow *, Tox *m, char **); -void cmd_help(ToxWindow *, Tox *m, char **); -void cmd_msg(ToxWindow *, Tox *m, char **); -void cmd_myid(ToxWindow *, Tox *m, char **); -void cmd_nick(ToxWindow *, Tox *m, char **); -void cmd_mynick(ToxWindow *, Tox *m, char **); -void cmd_quit(ToxWindow *, Tox *m, char **); -void cmd_status(ToxWindow *, Tox *m, char **); -void cmd_statusmsg(ToxWindow *, Tox *m, char **); +void cmd_accept(ToxWindow *, Tox *m, int, char **); +void cmd_add(ToxWindow *, Tox *m, int, char **); +void cmd_clear(ToxWindow *, Tox *m, int, char **); +void cmd_connect(ToxWindow *, Tox *m, int, char **); +void cmd_help(ToxWindow *, Tox *m, int, char **); +void cmd_msg(ToxWindow *, Tox *m, int, char **); +void cmd_myid(ToxWindow *, Tox *m, int, char **); +void cmd_nick(ToxWindow *, Tox *m, int, char **); +void cmd_mynick(ToxWindow *, Tox *m, int, char **); +void cmd_quit(ToxWindow *, Tox *m, int, char **); +void cmd_status(ToxWindow *, Tox *m, int, char **); +void cmd_statusmsg(ToxWindow *, Tox *m, int, char **); #define NUM_COMMANDS 14 static struct { char *name; - int numargs; - void (*func)(ToxWindow *, Tox *m, char **); + void (*func)(ToxWindow *, Tox *m, int, char **); } commands[] = { - { "accept", 1, cmd_accept }, - { "add", 1, cmd_add }, - { "clear", 0, cmd_clear }, - { "connect", 3, cmd_connect }, - { "exit", 0, cmd_quit }, - { "help", 0, cmd_help }, - { "msg", 2, cmd_msg }, - { "myid", 0, cmd_myid }, - { "nick", 1, cmd_nick }, - { "mynick", 0, cmd_mynick }, - { "q", 0, cmd_quit }, - { "quit", 0, cmd_quit }, - { "status", 2, cmd_status }, - { "statusmsg", 1, cmd_statusmsg }, + { "accept", cmd_accept }, + { "add", cmd_add }, + { "clear", cmd_clear }, + { "connect", cmd_connect }, + { "exit", cmd_quit }, + { "help", cmd_help }, + { "msg", cmd_msg }, + { "myid", cmd_myid }, + { "nick", cmd_nick }, + { "mynick", cmd_mynick }, + { "q", cmd_quit }, + { "quit", cmd_quit }, + { "status", cmd_status }, + { "statusmsg", cmd_statusmsg }, }; // XXX: @@ -80,9 +79,18 @@ unsigned char *hex_string_to_bin(char hex_string[]) return val; } -void cmd_accept(ToxWindow *self, Tox *m, char **args) +/* command functions */ +void cmd_accept(ToxWindow *self, Tox *m, int argc, char **argv) { - int num = atoi(args[1]); + int num; + + /* check arguments */ + if (argc != 1) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + + num = atoi(argv[1]); if (num >= num_requests) { wprintw(self->window, "Invalid syntax.\n"); @@ -99,29 +107,29 @@ void cmd_accept(ToxWindow *self, Tox *m, char **args) } } -void cmd_add(ToxWindow *self, Tox *m, char **args) +void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv) { uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE]; char xx[3]; uint32_t x; - char *id = args[1]; - char *msg = args[2]; + char *id; + char *msg; + int i, num; - if (!id) { - wprintw(self->window, "Invalid command: add expected at least one argument.\n"); + /* check arguments */ + if (argc != 1 && argc != 2) { + wprintw(self->window, "Invalid syntax.\n"); return; } - if (!msg) - msg = ""; + id = argv[1]; + msg = (argc == 2) ? argv[2] : ""; if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) { wprintw(self->window, "Invalid ID length.\n"); return; } - int i; - for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) { xx[0] = id[2 * i]; xx[1] = id[2 * i + 1]; @@ -139,7 +147,7 @@ void cmd_add(ToxWindow *self, Tox *m, char **args) id[i] = toupper(id[i]); } - int num = tox_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1); + num = tox_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1); switch (num) { case TOX_FAERR_TOOLONG: @@ -177,17 +185,25 @@ void cmd_add(ToxWindow *self, Tox *m, char **args) } } -void cmd_clear(ToxWindow *self, Tox *m, char **args) +void cmd_clear(ToxWindow *self, Tox *m, int argc, char **argv) { wclear(self->window); } -void cmd_connect(ToxWindow *self, Tox *m, char **args) +void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv) { tox_IP_Port dht; - char *ip = args[1]; - char *port = args[2]; - char *key = args[3]; + char *ip, *port, *key; + + /* check arguments */ + if (argc != 3) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + + ip = argv[1]; + port = argv[2]; + key = argv[3]; if (atoi(port) == 0) { wprintw(self->window, "Invalid syntax.\n"); @@ -207,13 +223,13 @@ void cmd_connect(ToxWindow *self, Tox *m, char **args) free(binary_string); } -void cmd_quit(ToxWindow *self, Tox *m, char **args) +void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv) { endwin(); exit(0); } -void cmd_help(ToxWindow *self, Tox *m, char **args) +void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) { wclear(self->window); wattron(self->window, COLOR_PAIR(2) | A_BOLD); @@ -239,10 +255,18 @@ void cmd_help(ToxWindow *self, Tox *m, char **args) wattroff(self->window, COLOR_PAIR(2)); } -void cmd_msg(ToxWindow *self, Tox *m, char **args) +void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) { - char *id = args[1]; - char *msg = args[2]; + char *id, *msg; + + /* check arguments */ + if (argc != 2) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + + id = argv[1]; + msg = argv[2]; if (tox_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0) wprintw(self->window, "Error occurred while sending message.\n"); @@ -250,7 +274,7 @@ void cmd_msg(ToxWindow *self, Tox *m, char **args) wprintw(self->window, "Message successfully sent.\n"); } -void cmd_myid(ToxWindow *self, Tox *m, char **args) +void cmd_myid(ToxWindow *self, Tox *m, int argc, char **argv) { char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; size_t i; @@ -266,9 +290,18 @@ void cmd_myid(ToxWindow *self, Tox *m, char **args) wprintw(self->window, "%s\n", id); } -void cmd_nick(ToxWindow *self, Tox *m, char **args) +void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv) { - char *nick = args[1]; + char *nick; + + /* check arguments */ + if (argc != 1) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + + nick = argv[1]; + tox_setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(self->window, "Nickname set to: %s\n", nick); @@ -277,7 +310,7 @@ void cmd_nick(ToxWindow *self, Tox *m, char **args) } } -void cmd_mynick(ToxWindow *self, Tox *m, char **args) +void cmd_mynick(ToxWindow *self, Tox *m, int argc, char **argv) { uint8_t *nick = malloc(TOX_MAX_NAME_LENGTH); tox_getselfname(m, nick, TOX_MAX_NAME_LENGTH); @@ -285,10 +318,17 @@ void cmd_mynick(ToxWindow *self, Tox *m, char **args) free(nick); } -void cmd_status(ToxWindow *self, Tox *m, char **args) +void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) { - char *status = args[1]; - char *status_text; + char *status, *status_text, *msg; + + /* check arguments */ + if (argc != 1 && argc != 2) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + + status = argv[1]; TOX_USERSTATUS status_kind; @@ -306,7 +346,7 @@ void cmd_status(ToxWindow *self, Tox *m, char **args) return; } - char *msg = args[2]; + msg = argv[2]; if (msg == NULL) { tox_set_userstatus(m, status_kind); @@ -318,9 +358,18 @@ void cmd_status(ToxWindow *self, Tox *m, char **args) } } -void cmd_statusmsg(ToxWindow *self, Tox *m, char **args) +void cmd_statusmsg(ToxWindow *self, Tox *m, int argc, char **argv) { - char *msg = args[1]; + char *msg; + + /* check arguments */ + if (argc != 1) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + + msg = argv[1]; + tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); wprintw(self->window, "Status set to: %s\n", msg); } @@ -355,76 +404,47 @@ static void execute(ToxWindow *self, Tox *m, char *u_cmd) /* insert \0 at argument boundaries */ int numargs = 0; - for (i = 0; i < MAX_STR_SIZE; i++) { - char quote_chr; - if (cmd[i] == '\"' || cmd[i] == '\'') { - quote_chr = cmd[i]; - while (cmd[++i] != quote_chr && i < MAX_STR_SIZE); /* skip over strings */ - /* Check if got qoute character */ - if (cmd[i] != quote_chr) { - wprintw(self->window, "Missing terminating %c character\n", quote_chr); - return; - } - } - if (cmd[i] == ' ') { cmd[i] = '\0'; - - int j = i; - - while (++j < MAX_STR_SIZE && isspace(cmd[j])); - - i = j - 1; - numargs++; } + /* skip over strings */ + else if (cmd[i] == '\"') { + while (cmd[++i] != '\"') { + if (cmd[i] == '\n') { + wprintw(self->window, "Invalid command: did you forget a closing \"?\n"); + return; + } + } + } } - /* excessive arguments */ - if (numargs > 3) { - wprintw(self->window, "Invalid command: too many arguments.\n"); - return; + /* read arguments into array */ + char **cmdargs = malloc((numargs + 1) * sizeof(char *)); + if (!cmdargs) { + 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++) { + + for (i = 0; i < numargs + 1; i++) { cmdargs[i] = cmd + pos; pos += strlen(cmdargs[i]) + 1; - - while (isspace(cmd[pos]) && pos < MAX_STR_SIZE) - ++pos; + /* replace empty strings with NULL for easier error checking */ + if (strlen(cmdargs[i]) == 0) + cmdargs[i] = NULL; } /* no input */ - if (strlen(cmdargs[0]) == 0) + if (!cmdargs[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); + (commands[i].func)(self, m, numargs, cmdargs); return; } } @@ -493,7 +513,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) static void prompt_onInit(ToxWindow *self, Tox *m) { scrollok(self->window, 1); - cmd_help(self, m, NULL); + cmd_help(self, m, 0, NULL); wclrtoeol(self->window); } From 82dee36729a98fcb568923e1fa30b01c30a19a14 Mon Sep 17 00:00:00 2001 From: Luke Champine Date: Mon, 2 Sep 2013 17:48:16 -0400 Subject: [PATCH 47/82] Add documentation/error messages for missing quotation marks --- src/prompt.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/prompt.c b/src/prompt.c index 4b06477..fe0deb7 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -92,8 +92,8 @@ void cmd_accept(ToxWindow *self, Tox *m, int argc, char **argv) num = atoi(argv[1]); - if (num >= num_requests) { - wprintw(self->window, "Invalid syntax.\n"); + if (num < 0 || num >= num_requests) { + wprintw(self->window, "No pending request with that number.\n"); return; } @@ -117,6 +117,10 @@ void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv) int i, num; /* check arguments */ + if (argv[2] && argv[2][0] != '\"') { + wprintw(self->window, "Strings must be enclosed in quotes.\n"); + return; + } if (argc != 1 && argc != 2) { wprintw(self->window, "Invalid syntax.\n"); return; @@ -249,6 +253,7 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) wprintw(self->window, " clear : Clear this window\n"); wattron(self->window, A_BOLD); + wprintw(self->window, "NOTE: Strings must be enclosed in quotation marks.\n"); wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); wattroff(self->window, A_BOLD); @@ -323,9 +328,13 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) char *status, *status_text, *msg; /* check arguments */ + if (argv[2] && argv[2][0] != '\"') { + wprintw(self->window, "Strings must be enclosed in quotes.\n"); + return; + } if (argc != 1 && argc != 2) { - wprintw(self->window, "Invalid syntax.\n"); - return; + wprintw(self->window, "Wrong number of arguments.\n"); + return; } status = argv[1]; @@ -363,9 +372,13 @@ void cmd_statusmsg(ToxWindow *self, Tox *m, int argc, char **argv) char *msg; /* check arguments */ + if (argv[1] && argv[1][0] != '\"') { + wprintw(self->window, "Strings must be enclosed in quotes.\n"); + return; + } if (argc != 1) { - wprintw(self->window, "Invalid syntax.\n"); - return; + wprintw(self->window, "Wrong number of arguments.\n"); + return; } msg = argv[1]; @@ -412,7 +425,7 @@ static void execute(ToxWindow *self, Tox *m, char *u_cmd) /* skip over strings */ else if (cmd[i] == '\"') { while (cmd[++i] != '\"') { - if (cmd[i] == '\n') { + if (cmd[i] == '\0') { wprintw(self->window, "Invalid command: did you forget a closing \"?\n"); return; } From e5b5155c3ee19601a2f44d174537ebd665f03d75 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 2 Sep 2013 23:27:34 -0400 Subject: [PATCH 48/82] Change statusmsg to note for less confusion --- src/chat.c | 28 +++++++++++++--------------- src/friendlist.c | 8 ++++---- src/main.c | 2 +- src/prompt.c | 37 ++++++++++++++++++------------------- src/toxic_windows.h | 4 ++-- src/windows.c | 8 ++++---- 6 files changed, 42 insertions(+), 45 deletions(-) diff --git a/src/chat.c b/src/chat.c index b885197..5b26165 100644 --- a/src/chat.c +++ b/src/chat.c @@ -104,10 +104,10 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t nick[len - 1] = '\0'; snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); - wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick); + wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick); } -static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) +static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) { ChatContext *ctx = (ChatContext *) self->x; struct tm *timeinfo = get_time(); @@ -121,7 +121,7 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1 status[len - 1] = '\0'; - wprintw(ctx->history, "* Your partner changed status message to '%s'\n", status); + wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status); } /* check that the string has one non-space character */ @@ -335,23 +335,21 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) return; } - msg = strchr(status, ' '); + wprintw(ctx->history, "Status set to: %s\n", status_text); + tox_set_userstatus(m, status_kind); - if (msg == NULL) { - tox_set_userstatus(m, status_kind); - wprintw(ctx->history, "Status message set to: %s\n", status_text); - } else { + msg = strchr(status, ' '); + if (msg != NULL) { msg++; - tox_set_userstatus(m, status_kind); tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); - wprintw(ctx->history, "Status message set to: %s, %s\n", status_text, msg); + wprintw(ctx->history, "Personal note set to: %s\n", msg); } } - else if (!strncmp(cmd, "/statusmsg ", strlen("/statusmsg "))) { + else if (!strncmp(cmd, "/note ", strlen("/note "))) { char *msg = strchr(cmd, ' '); msg++; - wprintw(ctx->history, "Status message set to: %s\n", msg); + wprintw(ctx->history, "Personal note set to: %s\n", msg); tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); } @@ -417,8 +415,8 @@ void print_help(ChatContext *self) wprintw(self->history, "Commands:\n"); wattroff(self->history, A_BOLD); - wprintw(self->history, " /status : Set your status\n"); - wprintw(self->history, " /statusmsg : Set your status message\n"); + wprintw(self->history, " /status : Set your status with optional note\n"); + wprintw(self->history, " /note : Set a personal note\n"); wprintw(self->history, " /nick : Set your nickname\n"); wprintw(self->history, " /me : Do an action\n"); wprintw(self->history, " /myid : Print your ID\n"); @@ -440,7 +438,7 @@ ToxWindow new_chat(Tox *m, int friendnum) ret.onInit = &chat_onInit; ret.onMessage = &chat_onMessage; ret.onNickChange = &chat_onNickChange; - ret.onStatusChange = &chat_onStatusChange; + ret.onStatusMessageChange = &chat_onStatusMessageChange; ret.onAction = &chat_onAction; uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; diff --git a/src/friendlist.c b/src/friendlist.c index c4dd3cf..af30e0e 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -51,7 +51,7 @@ void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t le friends[num].name[len] = 0; } -void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) +void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends) return; @@ -164,8 +164,8 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) 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 and enter. "); - wprintw(self->window, "Delete friends with the backspace key\n\n"); + wprintw(self->window, " * Open chat with up/down keys and enter.\n"); + wprintw(self->window, " * Delete friends with the backspace key.\n\n"); wattroff(self->window, COLOR_PAIR(2) | A_BOLD); } @@ -237,7 +237,7 @@ ToxWindow new_friendlist() ret.onMessage = &friendlist_onMessage; ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message ret.onNickChange = &friendlist_onNickChange; - ret.onStatusChange = &friendlist_onStatusChange; + ret.onStatusMessageChange = &friendlist_onStatusMessageChange; strcpy(ret.title, "[friends]"); return ret; diff --git a/src/main.c b/src/main.c index f20ce76..eaf3618 100644 --- a/src/main.c +++ b/src/main.c @@ -94,7 +94,7 @@ static Tox *init_tox() tox_callback_friendrequest(m, on_request, NULL); tox_callback_friendmessage(m, on_message, NULL); tox_callback_namechange(m, on_nickchange, NULL); - tox_callback_statusmessage(m, on_statuschange, NULL); + tox_callback_statusmessage(m, on_statusmessagechange, NULL); tox_callback_action(m, on_action, NULL); #ifdef __linux__ tox_setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy")); diff --git a/src/prompt.c b/src/prompt.c index 6efb2ff..50a92ec 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -33,7 +33,7 @@ void cmd_nick(ToxWindow *, Tox *m, int, char **); void cmd_mynick(ToxWindow *, Tox *m, int, char **); void cmd_quit(ToxWindow *, Tox *m, int, char **); void cmd_status(ToxWindow *, Tox *m, int, char **); -void cmd_statusmsg(ToxWindow *, Tox *m, int, char **); +void cmd_note(ToxWindow *, Tox *m, int, char **); #define NUM_COMMANDS 14 @@ -54,7 +54,7 @@ static struct { { "q", cmd_quit }, { "quit", cmd_quit }, { "status", cmd_status }, - { "statusmsg", cmd_statusmsg }, + { "note", cmd_note }, }; // XXX: @@ -118,7 +118,7 @@ void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv) /* check arguments */ if (argv[2] && argv[2][0] != '\"') { - wprintw(self->window, "Strings must be enclosed in quotes.\n"); + wprintw(self->window, "Messages must be enclosed in quotes.\n"); return; } if (argc != 1 && argc != 2) { @@ -241,9 +241,9 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) wattroff(self->window, A_BOLD); wprintw(self->window, " connect : Connect to DHT server\n"); - wprintw(self->window, " add : Add friend\n"); - wprintw(self->window, " status : Set your status\n"); - wprintw(self->window, " statusmsg : Set your status message\n"); + wprintw(self->window, " add : Add friend with optional message\n"); + wprintw(self->window, " status : Set your status with optional note\n"); + wprintw(self->window, " note : Set a personal note\n"); wprintw(self->window, " nick : Set your nickname\n"); wprintw(self->window, " mynick : Print your current nickname\n"); wprintw(self->window, " accept : Accept friend request\n"); @@ -253,8 +253,8 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) wprintw(self->window, " clear : Clear this window\n"); wattron(self->window, A_BOLD); - wprintw(self->window, "NOTE: Strings must be enclosed in quotation marks.\n"); - wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); + wprintw(self->window, " * Messages must be enclosed in quotation marks.\n"); + wprintw(self->window, " * Use the TAB key to navigate through the tabs.\n\n"); wattroff(self->window, A_BOLD); wattroff(self->window, COLOR_PAIR(2)); @@ -274,7 +274,7 @@ void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) msg = argv[2]; if (tox_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0) - wprintw(self->window, "Error occurred while sending message.\n"); + wprintw(self->window, "Failed to send message.\n"); else wprintw(self->window, "Message successfully sent.\n"); } @@ -329,7 +329,7 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) /* check arguments */ if (argv[2] && argv[2][0] != '\"') { - wprintw(self->window, "Strings must be enclosed in quotes.\n"); + wprintw(self->window, "Messages must be enclosed in quotes.\n"); return; } if (argc != 1 && argc != 2) { @@ -355,25 +355,24 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) return; } + wprintw(self->window, "Status set to: %s\n", status_text); + tox_set_userstatus(m, status_kind); + msg = argv[2]; - if (msg == NULL) { - tox_set_userstatus(m, status_kind); - wprintw(self->window, "Status message set to: %s\n", status_text); - } else { - tox_set_userstatus(m, status_kind); + if (msg != NULL) { tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); - wprintw(self->window, "Status message set to: %s, %s\n", status_text, msg); + wprintw(self->window, "Personal note set to: %s\n", msg); } } -void cmd_statusmsg(ToxWindow *self, Tox *m, int argc, char **argv) +void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv) { char *msg; /* check arguments */ if (argv[1] && argv[1][0] != '\"') { - wprintw(self->window, "Strings must be enclosed in quotes.\n"); + wprintw(self->window, "Messages must be enclosed in quotes.\n"); return; } if (argc != 1) { @@ -384,7 +383,7 @@ void cmd_statusmsg(ToxWindow *self, Tox *m, int argc, char **argv) msg = argv[1]; tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); - wprintw(self->window, "Status message set to: %s\n", msg); + wprintw(self->window, "Personal note set to: %s\n", msg); } static void execute(ToxWindow *self, Tox *m, char *u_cmd) diff --git a/src/toxic_windows.h b/src/toxic_windows.h index f93b775..ecff396 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -33,7 +33,7 @@ struct ToxWindow_ { void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); void(*onMessage)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t); - void(*onStatusChange)(ToxWindow *, int, uint8_t *, uint16_t); + void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); char title[256]; @@ -47,7 +47,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); -void on_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); +void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_friendadded(Tox *m, int friendnumber); ToxWindow *init_windows(); void draw_active_window(Tox *m); diff --git a/src/windows.c b/src/windows.c index 2ae9806..1e28667 100644 --- a/src/windows.c +++ b/src/windows.c @@ -69,14 +69,14 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v } } -void on_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) +void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { - wprintw(prompt->window, "\n(message change) %d: %s\n", friendnumber, string); + wprintw(prompt->window, "\n(note change) %d: %s\n", friendnumber, string); int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { - if (windows[i].onStatusChange != NULL) - windows[i].onStatusChange(&windows[i], friendnumber, string, length); + if (windows[i].onStatusMessageChange != NULL) + windows[i].onStatusMessageChange(&windows[i], friendnumber, string, length); } } From db309936ab642fcc289219b7b22173c453b4a3ea Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 2 Sep 2013 23:39:33 -0400 Subject: [PATCH 49/82] Remove opening/closing quotes from strings --- src/prompt.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/prompt.c b/src/prompt.c index 50a92ec..99dd1ea 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -127,7 +127,12 @@ void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv) } id = argv[1]; - msg = (argc == 2) ? argv[2] : ""; + + if (argc == 2) { + msg = argv[2]; + msg[strlen(++msg)-1] = L'\0'; + } else + msg = ""; if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) { wprintw(self->window, "Invalid ID length.\n"); @@ -272,6 +277,7 @@ void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) id = argv[1]; msg = argv[2]; + msg[strlen(++msg)-1] = L'\0'; if (tox_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0) wprintw(self->window, "Failed to send message.\n"); @@ -361,6 +367,7 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) msg = argv[2]; if (msg != NULL) { + msg[strlen(++msg)-1] = L'\0'; /* remove opening and closing quotes */ tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); wprintw(self->window, "Personal note set to: %s\n", msg); } @@ -381,6 +388,7 @@ void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv) } msg = argv[1]; + msg[strlen(++msg)-1] = L'\0'; tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); wprintw(self->window, "Personal note set to: %s\n", msg); From 094311785e30c1b62d612727dbfc621c0aa29ba7 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 3 Sep 2013 00:20:17 -0400 Subject: [PATCH 50/82] fixed potential segfault --- src/prompt.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/prompt.c b/src/prompt.c index 99dd1ea..6e598c3 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -331,18 +331,24 @@ void cmd_mynick(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) { - char *status, *status_text, *msg; - - /* check arguments */ - if (argv[2] && argv[2][0] != '\"') { - wprintw(self->window, "Messages must be enclosed in quotes.\n"); - return; - } if (argc != 1 && argc != 2) { wprintw(self->window, "Wrong number of arguments.\n"); return; } + char *status, *status_text; + char *msg = NULL; + + /* check arguments */ + if (argc == 2) { + msg = argv[2]; + if (msg[0] != '\"') { + wprintw(self->window, "Messages must be enclosed in quotes.\n"); + return; + } + } + + status = argv[1]; TOX_USERSTATUS status_kind; @@ -364,8 +370,6 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) wprintw(self->window, "Status set to: %s\n", status_text); tox_set_userstatus(m, status_kind); - msg = argv[2]; - if (msg != NULL) { msg[strlen(++msg)-1] = L'\0'; /* remove opening and closing quotes */ tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); From 577f42c075ca59d429bf8d8bc93df8018ea64eb9 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 3 Sep 2013 01:02:58 -0400 Subject: [PATCH 51/82] fix more potential segfaults --- src/prompt.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/prompt.c b/src/prompt.c index 6e598c3..0444494 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -109,6 +109,11 @@ void cmd_accept(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv) { + if (argc != 1 && argc != 2) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE]; char xx[3]; uint32_t x; @@ -116,19 +121,14 @@ void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv) char *msg; int i, num; - /* check arguments */ - if (argv[2] && argv[2][0] != '\"') { - wprintw(self->window, "Messages must be enclosed in quotes.\n"); - return; - } - if (argc != 1 && argc != 2) { - wprintw(self->window, "Invalid syntax.\n"); - return; - } - id = argv[1]; if (argc == 2) { + if (argv[2][0] != '\"') { + wprintw(self->window, "Messages must be enclosed in quotes.\n"); + return; + } + msg = argv[2]; msg[strlen(++msg)-1] = L'\0'; } else @@ -379,6 +379,11 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv) { + if (argc != 1) { + wprintw(self->window, "Wrong number of arguments.\n"); + return; + } + char *msg; /* check arguments */ @@ -386,10 +391,6 @@ void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv) wprintw(self->window, "Messages must be enclosed in quotes.\n"); return; } - if (argc != 1) { - wprintw(self->window, "Wrong number of arguments.\n"); - return; - } msg = argv[1]; msg[strlen(++msg)-1] = L'\0'; From aa6e205ee848b316f7d902022815c28317006e97 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 3 Sep 2013 21:31:50 -0400 Subject: [PATCH 52/82] Show offline friends names and some cosmetic changes --- src/chat.c | 4 ++-- src/friendlist.c | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/chat.c b/src/chat.c index 5b26165..b6cb639 100644 --- a/src/chat.c +++ b/src/chat.c @@ -102,7 +102,7 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t wattroff(ctx->history, COLOR_PAIR(2)); nick[len - 1] = '\0'; - snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); + snprintf(self->title, sizeof(self->title), "[%s]", nick); wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick); } @@ -444,7 +444,7 @@ ToxWindow new_chat(Tox *m, int friendnum) uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; tox_getname(m, friendnum, (uint8_t *) &nick); - snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); + snprintf(ret.title, sizeof(ret.title), "[%s]", nick); ChatContext *x = calloc(1, sizeof(ChatContext)); ret.x = x; diff --git a/src/friendlist.c b/src/friendlist.c index af30e0e..b5078e4 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -72,8 +72,10 @@ int friendlist_onFriendAdded(Tox *m, int num) friends[i].num = num; friends[i].active = true; friends[i].chatwin = -1; - //tox_getname(m, num, friends[i].name); - strcpy((char *) friends[i].name, "unknown"); + + if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0') + strcpy((char *) friends[i].name, "unknown"); + strcpy((char *) friends[i].statusmsg, NOSTATUSMSG); if (i == num_friends) @@ -164,8 +166,8 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) 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 and enter.\n"); - wprintw(self->window, " * Delete friends with the backspace key.\n\n"); + wprintw(self->window, " Open chat with up/down keys and enter.\n"); + wprintw(self->window, " Delete friends with the backspace key.\n\n"); wattroff(self->window, COLOR_PAIR(2) | A_BOLD); } @@ -182,33 +184,31 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) if (is_online) { TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num); - int colour; + int colour = 7; /* Invalid or other errors default to black */ switch(status) { case TOX_USERSTATUS_NONE: colour = 1; break; - case TOX_USERSTATUS_AWAY: colour = 5; break; - case TOX_USERSTATUS_BUSY: - case TOX_USERSTATUS_INVALID: - default: colour = 3; break; } + wprintw(self->window, "["); wattron(self->window, COLOR_PAIR(colour)); - wprintw(self->window, "%s ", friends[i].name); + wprintw(self->window, "O"); wattroff(self->window, COLOR_PAIR(colour)); + wprintw(self->window, "] %s ", friends[i].name); if (strncmp(friends[i].statusmsg, NOSTATUSMSG, strlen(NOSTATUSMSG))) wprintw(self->window, "(%s)\n", friends[i].statusmsg); } else { - wprintw(self->window, "%s (Offline)\n", friends[i].name); + wprintw(self->window, "[O] %s (Offline)\n", friends[i].name); } } } From 0be84ecbb30df7a36735b8d162654454b7812004 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 3 Sep 2013 22:05:36 -0400 Subject: [PATCH 53/82] notes/status messages no longer forced --- src/friendlist.c | 13 +++++++------ src/friendlist.h | 2 -- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index b5078e4..3849082 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -76,7 +76,7 @@ int friendlist_onFriendAdded(Tox *m, int num) if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0') strcpy((char *) friends[i].name, "unknown"); - strcpy((char *) friends[i].statusmsg, NOSTATUSMSG); + tox_set_statusmessage(m, "\0", strlen("\0")); if (i == num_friends) ++num_friends; @@ -202,13 +202,14 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wattron(self->window, COLOR_PAIR(colour)); wprintw(self->window, "O"); wattroff(self->window, COLOR_PAIR(colour)); - wprintw(self->window, "] %s ", friends[i].name); - - if (strncmp(friends[i].statusmsg, NOSTATUSMSG, strlen(NOSTATUSMSG))) - wprintw(self->window, "(%s)\n", friends[i].statusmsg); + wprintw(self->window, "] %s", friends[i].name); + if (friends[i].statusmsg[0] != '\0') + wprintw(self->window, " (%s)\n", friends[i].statusmsg); + else + wprintw(self->window, "\n"); } else { - wprintw(self->window, "[O] %s (Offline)\n", friends[i].name); + wprintw(self->window, "[O] %s\n", friends[i].name); } } } diff --git a/src/friendlist.h b/src/friendlist.h index 6b21bac..26c9106 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -4,8 +4,6 @@ #include "toxic_windows.h" #include "chat.h" -#define NOSTATUSMSG "NOSTATUSMSG" /* Friends' default status message */ - ToxWindow new_friendlist(); int friendlist_onFriendAdded(Tox *m, int num); void disable_chatwin(int f_num); From 20d1ad9842bdea3d01f24520be2ca24604d85e97 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 4 Sep 2013 00:58:23 -0400 Subject: [PATCH 54/82] save nicks and exit tox properly --- src/chat.c | 1 + src/prompt.c | 1 + src/windows.c | 5 ++++- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/chat.c b/src/chat.c index b6cb639..dbc53c5 100644 --- a/src/chat.c +++ b/src/chat.c @@ -270,6 +270,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { endwin(); + tox_kill(m); exit(0); } diff --git a/src/prompt.c b/src/prompt.c index 0444494..19ab274 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -235,6 +235,7 @@ void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv) { endwin(); + tox_kill(m); exit(0); } diff --git a/src/windows.c b/src/windows.c index 1e28667..229e224 100644 --- a/src/windows.c +++ b/src/windows.c @@ -64,8 +64,11 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { - if (windows[i].onNickChange != NULL) + if (windows[i].onNickChange != NULL) { windows[i].onNickChange(&windows[i], friendnumber, string, length); + if (store_data(m, DATA_FILE)) + wprintw(prompt->window, "\nCould not store Tox data\n"); + } } } From f93af40f28971274892a388b63752d2dd350ea93 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 4 Sep 2013 02:05:36 -0400 Subject: [PATCH 55/82] Save messenger data on exit --- src/chat.c | 4 ++++ src/friendlist.c | 3 +-- src/prompt.c | 1 + src/windows.c | 5 +---- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/chat.c b/src/chat.c index dbc53c5..5a4e283 100644 --- a/src/chat.c +++ b/src/chat.c @@ -19,6 +19,9 @@ #define CURS_Y_OFFSET 3 +extern char *DATA_FILE; +extern int store_data(Tox *m, char *path); + typedef struct { int friendnum; wchar_t line[MAX_STR_SIZE]; @@ -270,6 +273,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { endwin(); + store_data(m, DATA_FILE); tox_kill(m); exit(0); } diff --git a/src/friendlist.c b/src/friendlist.c index 3849082..3a349ef 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -37,9 +37,8 @@ void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16 if (num >= num_friends) return; - if (friends[num].chatwin == -1) { + if (friends[num].chatwin == -1) friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); - } } void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) diff --git a/src/prompt.c b/src/prompt.c index 19ab274..2865654 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -235,6 +235,7 @@ void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv) { endwin(); + store_data(m, DATA_FILE); tox_kill(m); exit(0); } diff --git a/src/windows.c b/src/windows.c index 229e224..1e28667 100644 --- a/src/windows.c +++ b/src/windows.c @@ -64,11 +64,8 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { - if (windows[i].onNickChange != NULL) { + if (windows[i].onNickChange != NULL) windows[i].onNickChange(&windows[i], friendnumber, string, length); - if (store_data(m, DATA_FILE)) - wprintw(prompt->window, "\nCould not store Tox data\n"); - } } } From 2f93081a42b590117c0e85650661bbc9ba3b3da0 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 4 Sep 2013 17:22:16 -0400 Subject: [PATCH 56/82] slight refactor of select_friend() and fix mistake --- src/friendlist.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index 3a349ef..9073420 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -75,8 +75,6 @@ int friendlist_onFriendAdded(Tox *m, int num) if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0') strcpy((char *) friends[i].name, "unknown"); - tox_set_statusmessage(m, "\0", strlen("\0")); - if (i == num_friends) ++num_friends; @@ -87,39 +85,34 @@ int friendlist_onFriendAdded(Tox *m, int num) return -1; } -static void select_friend(wint_t key) +static void select_friend(Tox *m, wint_t key) { if (num_friends < 1) return; int n = num_selected; - int f_inf = num_selected; if (key == KEY_UP) { - while (true) { - if (--n < 0) n = num_friends-1; + while (--n != num_selected) { + if (n < 0) n = num_friends - 1; if (friends[n].active) { num_selected = n; return; } - if (n == f_inf) { - endwin(); - exit(2); - } } } else if (key == KEY_DOWN) { - while (true) { - n = (n + 1) % num_friends; + while ((n = (n + 1) % num_friends) != num_selected) { if (friends[n].active) { num_selected = n; return; } - if (n == f_inf) { - endwin(); - exit(2); - } } - } + } else return; /* Bad key input */ + + /* If we reach this something is wrong */ + endwin(); + tox_kill(m); + exit(2); } static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) @@ -138,13 +131,13 @@ static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) wprintw(self->window, "\nFailed to store messenger data\n"); num_friends = i; - select_friend(KEY_DOWN); + select_friend(m, KEY_DOWN); } static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key) { if (key == KEY_UP || key == KEY_DOWN) { - select_friend(key); + select_friend(m, key); } else if (key == '\n') { /* Jump to chat window if already open */ if (friends[num_selected].chatwin != -1) { @@ -174,14 +167,12 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) for (i = 0; i < num_friends; ++i) { if (friends[i].active) { - bool is_online = tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE; - if (i == num_selected) wprintw(self->window, " > "); else wprintw(self->window, " "); - if (is_online) { + if (tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE) { TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num); int colour = 7; /* Invalid or other errors default to black */ @@ -203,7 +194,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wattroff(self->window, COLOR_PAIR(colour)); wprintw(self->window, "] %s", friends[i].name); - if (friends[i].statusmsg[0] != '\0') + if (friends[i].statusmsg[0]) wprintw(self->window, " (%s)\n", friends[i].statusmsg); else wprintw(self->window, "\n"); @@ -212,7 +203,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) } } } - + wrefresh(self->window); } From f5695a4b3e5980a5143e811417a0f638a54ead8f Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 4 Sep 2013 21:25:59 -0400 Subject: [PATCH 57/82] implemented status and connectionstatus callbacks --- src/chat.c | 82 ++++++++++++++++++++++++++++++++++++++++----- src/friendlist.c | 39 +++++++++++++++++---- src/main.c | 2 ++ src/prompt.c | 1 + src/toxic_windows.h | 4 +++ src/windows.c | 72 +++++++++++++++++++++++++++++++++++++-- 6 files changed, 183 insertions(+), 17 deletions(-) diff --git a/src/chat.c b/src/chat.c index 5a4e283..5b99f70 100644 --- a/src/chat.c +++ b/src/chat.c @@ -52,8 +52,8 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 return; tox_getname(m, num, (uint8_t *) &nick); - msg[len - 1] = '\0'; - nick[TOX_MAX_NAME_LENGTH - 1] = '\0'; + msg[len-1] = '\0'; + nick[TOX_MAX_NAME_LENGTH-1] = '\0'; wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); @@ -67,6 +67,24 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 beep(); } +void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) +{ + 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)); + + if (status == 1) + wprintw(ctx->history, "* Chat partner has come online\n"); + else + wprintw(ctx->history, "* Chat partner went offline\n"); +} + static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len) { ChatContext *ctx = (ChatContext *) self->x; @@ -110,7 +128,7 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick); } -static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) +static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) { ChatContext *ctx = (ChatContext *) self->x; struct tm *timeinfo = get_time(); @@ -122,9 +140,47 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wattroff(ctx->history, COLOR_PAIR(2)); + switch(status) { + + case TOX_USERSTATUS_NONE: + wprintw(ctx->history, "* Chat partner set status to "); + wattron(ctx->history, COLOR_PAIR(1)); + wprintw(ctx->history, "[Online]\n"); + wattroff(ctx->history, COLOR_PAIR(1)); + break; + + case TOX_USERSTATUS_BUSY: + wprintw(ctx->history, "* Chat partner set status to "); + wattron(ctx->history, COLOR_PAIR(3)); + wprintw(ctx->history, "[Busy]\n"); + wattroff(ctx->history, COLOR_PAIR(3)); + break; + + case TOX_USERSTATUS_AWAY: + wprintw(ctx->history, "* Chat partner set status to "); + wattron(ctx->history, COLOR_PAIR(5)); + wprintw(ctx->history, "[Away]\n"); + wattroff(ctx->history, COLOR_PAIR(5)); + break; + } +} + +static void chat_onStatusMessageChange(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; + status[len - 1] = '\0'; - wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status); + if (strncmp(status, "Online", strlen("status"))) { /* Ignore default "Online" message */ + 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)); + wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status); + } } /* check that the string has one non-space character */ @@ -322,17 +378,26 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) if (!strncmp(status, "online", strlen("online"))) { status_kind = TOX_USERSTATUS_NONE; - status_text = "Online"; + wprintw(ctx->history, "Status set to "); + wattron(ctx->history, COLOR_PAIR(1)); + wprintw(ctx->history, "[Online]\n"); + wattroff(ctx->history, COLOR_PAIR(1)); } else if (!strncmp(status, "away", strlen("away"))) { status_kind = TOX_USERSTATUS_AWAY; - status_text = "Away"; + wprintw(ctx->history, "Status set to "); + wattron(ctx->history, COLOR_PAIR(5)); + wprintw(ctx->history, "[Away]\n"); + wattroff(ctx->history, COLOR_PAIR(5)); } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = TOX_USERSTATUS_BUSY; - status_text = "Busy"; + wprintw(ctx->history, "Status set to "); + wattron(ctx->history, COLOR_PAIR(3)); + wprintw(ctx->history, "[Busy]\n"); + wattroff(ctx->history, COLOR_PAIR(3)); } else { @@ -340,7 +405,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) return; } - wprintw(ctx->history, "Status set to: %s\n", status_text); tox_set_userstatus(m, status_kind); msg = strchr(status, ' '); @@ -442,7 +506,9 @@ ToxWindow new_chat(Tox *m, int friendnum) ret.onDraw = &chat_onDraw; ret.onInit = &chat_onInit; ret.onMessage = &chat_onMessage; + ret.onConnectionChange = &chat_onConnectionChange; ret.onNickChange = &chat_onNickChange; + ret.onStatusChange = &chat_onStatusChange; ret.onStatusMessageChange = &chat_onStatusMessageChange; ret.onAction = &chat_onAction; diff --git a/src/friendlist.c b/src/friendlist.c index 9073420..a5befb4 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -24,7 +24,9 @@ typedef struct { uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; int num; int chatwin; - bool active; + bool active; + bool online; + TOX_USERSTATUS status; } friend_t; static friend_t friends[MAX_FRIENDS_NUM]; @@ -34,25 +36,44 @@ static int num_selected = 0; void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) { - if (num >= num_friends) + if (num < 0 || num >= num_friends) return; if (friends[num].chatwin == -1) friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); } +void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) +{ + if (num < 0 || num >= num_friends) + return; + + if (status == 1) + friends[num].online = true; + else + friends[num].online = false; +} + void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { - if (len >= TOX_MAX_NAME_LENGTH || num >= num_friends) + if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= num_friends) return; memcpy((char *) &friends[num].name, (char *) str, len); friends[num].name[len] = 0; } +void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) +{ + if (num < 0 || num >= num_friends) + return; + + friends[num].status = status; +} + void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { - if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends) + if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends) return; memcpy((char *) &friends[num].statusmsg, (char *) str, len); @@ -71,6 +92,8 @@ int friendlist_onFriendAdded(Tox *m, int num) friends[i].num = num; friends[i].active = true; friends[i].chatwin = -1; + friends[i].online = false; + friends[i].status = TOX_USERSTATUS_INVALID; if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0') strcpy((char *) friends[i].name, "unknown"); @@ -172,8 +195,8 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) else wprintw(self->window, " "); - if (tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE) { - TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num); + if (friends[i].online) { + TOX_USERSTATUS status = friends[i].status; int colour = 7; /* Invalid or other errors default to black */ switch(status) { @@ -203,7 +226,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) } } } - + wrefresh(self->window); } @@ -226,8 +249,10 @@ ToxWindow new_friendlist() ret.onDraw = &friendlist_onDraw; ret.onInit = &friendlist_onInit; ret.onMessage = &friendlist_onMessage; + ret.onConnectionChange = &friendlist_onConnectionChange; ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message ret.onNickChange = &friendlist_onNickChange; + ret.onStatusChange = &friendlist_onStatusChange; ret.onStatusMessageChange = &friendlist_onStatusMessageChange; strcpy(ret.title, "[friends]"); diff --git a/src/main.c b/src/main.c index eaf3618..77bcee2 100644 --- a/src/main.c +++ b/src/main.c @@ -91,9 +91,11 @@ static Tox *init_tox() Tox *m = tox_new(); /* Callbacks */ + tox_callback_connectionstatus(m, on_connectionchange, NULL); tox_callback_friendrequest(m, on_request, NULL); tox_callback_friendmessage(m, on_message, NULL); tox_callback_namechange(m, on_nickchange, NULL); + tox_callback_userstatus(m, on_statuschange, NULL); tox_callback_statusmessage(m, on_statusmessagechange, NULL); tox_callback_action(m, on_action, NULL); #ifdef __linux__ diff --git a/src/prompt.c b/src/prompt.c index 2865654..cc62a1d 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -314,6 +314,7 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv) } nick = argv[1]; + nick[strlen(++nick)-1] = L'\0'; tox_setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(self->window, "Nickname set to: %s\n", nick); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index ecff396..ec188cb 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -31,8 +31,10 @@ struct ToxWindow_ { void(*onDraw)(ToxWindow *, Tox *); void(*onInit)(ToxWindow *, Tox *); void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); + void(*onConnectionChange)(ToxWindow *, Tox *, int, uint8_t); void(*onMessage)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t); + void(*onStatusChange)(ToxWindow *, Tox *, int, TOX_USERSTATUS); void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); char title[256]; @@ -44,9 +46,11 @@ struct ToxWindow_ { }; void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata); +void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata); void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); +void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *userdata); void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_friendadded(Tox *m, int friendnumber); ToxWindow *init_windows(); diff --git a/src/windows.c b/src/windows.c index 1e28667..71feecd 100644 --- a/src/windows.c +++ b/src/windows.c @@ -17,6 +17,8 @@ static ToxWindow *active_window; static ToxWindow *prompt; static Tox *m; +#define unknown_name "Unknown" + /* CALLBACKS START */ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { @@ -38,6 +40,27 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd } } +void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata) +{ + uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; + tox_getname(m, friendnumber, (uint8_t *) &nick); + + if (!nick[0]) + snprintf(nick, sizeof(nick), "%s", unknown_name); + + if (status == 1) + wprintw(prompt->window, "\n%s has come online\n", nick, friendnumber); + else + wprintw(prompt->window, "\n%s went offline\n", nick, friendnumber); + + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onConnectionChange != NULL) + windows[i].onConnectionChange(&windows[i], m, friendnumber, status); + } +} + void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { int i; @@ -60,7 +83,6 @@ void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { - wprintw(prompt->window, "\n(nick change) %d: %s\n", friendnumber, string); int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { @@ -71,7 +93,17 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { - wprintw(prompt->window, "\n(note change) %d: %s\n", friendnumber, string); + /* Don't show default "Online" status message */ + if (strncmp(string, "Online", strlen(string))) { + uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; + tox_getname(m, friendnumber, (uint8_t *) &nick); + + if (!nick[0]) + snprintf(nick, sizeof(nick), "%s", unknown_name); + + wprintw(prompt->window, "\n%s set note to: %s\n", nick, string); + } + int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { @@ -80,6 +112,42 @@ void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t } } +void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *userdata) +{ + uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; + tox_getname(m, friendnumber, (uint8_t *) &nick); + + if (!nick[0]) + snprintf(nick, sizeof(nick), "%s", unknown_name); + + switch(status) { + case TOX_USERSTATUS_NONE: + /* Disabled because it spams a second messages when user comes online */ + break; + + case TOX_USERSTATUS_BUSY: + wprintw(prompt->window, "\n%s set status to ", nick); + wattron(prompt->window, COLOR_PAIR(3)); + wprintw(prompt->window, "[Busy]\n"); + wattroff(prompt->window, COLOR_PAIR(3)); + break; + + case TOX_USERSTATUS_AWAY: + wprintw(prompt->window, "\n%s set status to ", nick); + wattron(prompt->window, COLOR_PAIR(5)); + wprintw(prompt->window, "[Away]\n"); + wattroff(prompt->window, COLOR_PAIR(5)); + break; + } + + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onStatusChange != NULL) + windows[i].onStatusChange(&windows[i], m, friendnumber, status); + } +} + void on_friendadded(Tox *m, int friendnumber) { friendlist_onFriendAdded(m, friendnumber); From f7d96b0779c05a0fd5b0adead9f186be058b298a Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 5 Sep 2013 00:47:33 -0400 Subject: [PATCH 58/82] define curses colours and replace magic numbers --- src/chat.c | 80 ++++++++++++++++++++++----------------------- src/friendlist.c | 12 +++---- src/main.c | 10 +++--- src/prompt.c | 8 ++--- src/toxic_windows.h | 10 ++++++ src/windows.c | 28 ++++++++-------- 6 files changed, 79 insertions(+), 69 deletions(-) diff --git a/src/chat.c b/src/chat.c index 5b99f70..e13ba74 100644 --- a/src/chat.c +++ b/src/chat.c @@ -55,9 +55,9 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 msg[len-1] = '\0'; nick[TOX_MAX_NAME_LENGTH-1] = '\0'; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); 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(CYAN)); wattron(ctx->history, COLOR_PAIR(4)); wprintw(ctx->history, "%s: ", nick); wattroff(ctx->history, COLOR_PAIR(4)); @@ -75,9 +75,9 @@ void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) if (ctx->friendnum != num) return; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); 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(CYAN)); if (status == 1) wprintw(ctx->history, "* Chat partner has come online\n"); @@ -98,13 +98,13 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin action[len - 1] = '\0'; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); 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(CYAN)); - wattron(ctx->history, COLOR_PAIR(5)); + wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "* %s %s\n", nick, action); - wattroff(ctx->history, COLOR_PAIR(5)); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); self->blink = true; beep(); @@ -118,9 +118,9 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t if (ctx->friendnum != num) return; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); 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(CYAN)); nick[len - 1] = '\0'; snprintf(self->title, sizeof(self->title), "[%s]", nick); @@ -136,31 +136,31 @@ static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS if (ctx->friendnum != num) return; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); 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(CYAN)); switch(status) { case TOX_USERSTATUS_NONE: wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(1)); + wattron(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "[Online]\n"); - wattroff(ctx->history, COLOR_PAIR(1)); + wattroff(ctx->history, COLOR_PAIR(GREEN)); break; case TOX_USERSTATUS_BUSY: wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(3)); + wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, "[Busy]\n"); - wattroff(ctx->history, COLOR_PAIR(3)); + wattroff(ctx->history, COLOR_PAIR(RED)); break; case TOX_USERSTATUS_AWAY: wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(5)); + wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "[Away]\n"); - wattroff(ctx->history, COLOR_PAIR(5)); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); break; } } @@ -176,9 +176,9 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status status[len - 1] = '\0'; if (strncmp(status, "Online", strlen("status"))) { /* Ignore default "Online" message */ - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); 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(CYAN)); wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status); } } @@ -286,18 +286,18 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) uint8_t selfname[TOX_MAX_NAME_LENGTH]; tox_getselfname(m, selfname, sizeof(selfname)); - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); 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)); + wattroff(ctx->history, COLOR_PAIR(CYAN)); + wattron(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "%s: ", selfname); - wattroff(ctx->history, COLOR_PAIR(1)); + wattroff(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "%s\n", line); if (tox_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { - wattron(ctx->history, COLOR_PAIR(3)); + wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send message.\n"); - wattroff(ctx->history, COLOR_PAIR(3)); + wattroff(ctx->history, COLOR_PAIR(RED)); } } } @@ -345,21 +345,21 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) action++; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); 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(CYAN)); uint8_t selfname[TOX_MAX_NAME_LENGTH]; tox_getselfname(m, selfname, sizeof(selfname)); - wattron(ctx->history, COLOR_PAIR(5)); + wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "* %s %s\n", selfname, action); - wattroff(ctx->history, COLOR_PAIR(5)); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); if (tox_sendaction(m, ctx->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { - wattron(ctx->history, COLOR_PAIR(3)); + wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send action\n"); - wattroff(ctx->history, COLOR_PAIR(3)); + wattroff(ctx->history, COLOR_PAIR(RED)); } } @@ -379,25 +379,25 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) if (!strncmp(status, "online", strlen("online"))) { status_kind = TOX_USERSTATUS_NONE; wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(1)); + wattron(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "[Online]\n"); - wattroff(ctx->history, COLOR_PAIR(1)); + wattroff(ctx->history, COLOR_PAIR(GREEN)); } else if (!strncmp(status, "away", strlen("away"))) { status_kind = TOX_USERSTATUS_AWAY; wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(5)); + wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "[Away]\n"); - wattroff(ctx->history, COLOR_PAIR(5)); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = TOX_USERSTATUS_BUSY; wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(3)); + wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, "[Busy]\n"); - wattroff(ctx->history, COLOR_PAIR(3)); + wattroff(ctx->history, COLOR_PAIR(RED)); } else { @@ -480,7 +480,7 @@ static void chat_onInit(ToxWindow *self, Tox *m) void print_help(ChatContext *self) { - wattron(self->history, COLOR_PAIR(2) | A_BOLD); + wattron(self->history, COLOR_PAIR(CYAN) | A_BOLD); wprintw(self->history, "Commands:\n"); wattroff(self->history, A_BOLD); @@ -494,7 +494,7 @@ void print_help(ChatContext *self) wprintw(self->history, " /quit or /exit : Exit Toxic\n"); wprintw(self->history, " /help : Print this message again\n\n"); - wattroff(self->history, COLOR_PAIR(2)); + wattroff(self->history, COLOR_PAIR(CYAN)); } ToxWindow new_chat(Tox *m, int friendnum) diff --git a/src/friendlist.c b/src/friendlist.c index a5befb4..f79267b 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -180,10 +180,10 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) if (num_friends == 0) { wprintw(self->window, "Empty. Add some friends! :-)\n"); } else { - wattron(self->window, COLOR_PAIR(2) | A_BOLD); + wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD); wprintw(self->window, " Open chat with up/down keys and enter.\n"); wprintw(self->window, " Delete friends with the backspace key.\n\n"); - wattroff(self->window, COLOR_PAIR(2) | A_BOLD); + wattroff(self->window, COLOR_PAIR(CYAN) | A_BOLD); } int i; @@ -197,17 +197,17 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) if (friends[i].online) { TOX_USERSTATUS status = friends[i].status; - int colour = 7; /* Invalid or other errors default to black */ + int colour = WHITE; switch(status) { case TOX_USERSTATUS_NONE: - colour = 1; + colour = GREEN; break; case TOX_USERSTATUS_AWAY: - colour = 5; + colour = YELLOW; break; case TOX_USERSTATUS_BUSY: - colour = 3; + colour = RED; break; } diff --git a/src/main.c b/src/main.c index 77bcee2..c74cb34 100644 --- a/src/main.c +++ b/src/main.c @@ -71,6 +71,7 @@ static void init_term() if (has_colors()) { start_color(); + init_pair(0, COLOR_WHITE, COLOR_BLACK); init_pair(1, COLOR_GREEN, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK); init_pair(3, COLOR_RED, COLOR_BLACK); @@ -79,7 +80,6 @@ static void init_term() init_pair(6, COLOR_MAGENTA, COLOR_BLACK); init_pair(7, COLOR_BLACK, COLOR_BLACK); init_pair(8, COLOR_BLACK, COLOR_WHITE); - } refresh(); @@ -398,17 +398,17 @@ int main(int argc, char *argv[]) load_data(m, DATA_FILE); if (f_flag == -1) { - attron(COLOR_PAIR(3) | A_BOLD); + attron(COLOR_PAIR(RED) | 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); + attroff(COLOR_PAIR(RED) | A_BOLD); } if (config_err) { - attron(COLOR_PAIR(3) | A_BOLD); + attron(COLOR_PAIR(RED) | A_BOLD); wprintw(prompt->window, "Unable to determine configuration directory.\n" "defaulting to 'data' for a keyfile...\n"); - attroff(COLOR_PAIR(3) | A_BOLD); + attroff(COLOR_PAIR(RED) | A_BOLD); } while (true) { diff --git a/src/prompt.c b/src/prompt.c index cc62a1d..913c169 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -243,7 +243,7 @@ void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) { wclear(self->window); - wattron(self->window, COLOR_PAIR(2) | A_BOLD); + wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD); wprintw(self->window, "Commands:\n"); wattroff(self->window, A_BOLD); @@ -264,7 +264,7 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) wprintw(self->window, " * Use the TAB key to navigate through the tabs.\n\n"); wattroff(self->window, A_BOLD); - wattroff(self->window, COLOR_PAIR(2)); + wattroff(self->window, COLOR_PAIR(CYAN)); } void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) @@ -530,9 +530,9 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) --y; } - wattron(self->window, COLOR_PAIR(1)); + wattron(self->window, COLOR_PAIR(GREEN)); mvwprintw(self->window, y, 0, "# "); - wattroff(self->window, COLOR_PAIR(1)); + wattroff(self->window, COLOR_PAIR(GREEN)); mvwprintw(self->window, y, 2, "%s", prompt_buf); wclrtoeol(self->window); wrefresh(self->window); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index ec188cb..23aa4ae 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -24,6 +24,16 @@ #define TOXICVER "NOVER" //Use the -D flag to set this #endif +/* Curses foreground colours (background is black) */ +#define WHITE 0 +#define GREEN 1 +#define CYAN 2 +#define RED 3 +#define BLUE 4 +#define YELLOW 5 +#define MAGENTA 6 +#define BLACK 7 + typedef struct ToxWindow_ ToxWindow; struct ToxWindow_ { diff --git a/src/windows.c b/src/windows.c index 71feecd..a184148 100644 --- a/src/windows.c +++ b/src/windows.c @@ -17,7 +17,7 @@ static ToxWindow *active_window; static ToxWindow *prompt; static Tox *m; -#define unknown_name "Unknown" +#define UNKNOWN_NAME "Unknown" /* CALLBACKS START */ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) @@ -46,7 +46,7 @@ void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdat tox_getname(m, friendnumber, (uint8_t *) &nick); if (!nick[0]) - snprintf(nick, sizeof(nick), "%s", unknown_name); + snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); if (status == 1) wprintw(prompt->window, "\n%s has come online\n", nick, friendnumber); @@ -99,7 +99,7 @@ void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t tox_getname(m, friendnumber, (uint8_t *) &nick); if (!nick[0]) - snprintf(nick, sizeof(nick), "%s", unknown_name); + snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); wprintw(prompt->window, "\n%s set note to: %s\n", nick, string); } @@ -118,7 +118,7 @@ void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *user tox_getname(m, friendnumber, (uint8_t *) &nick); if (!nick[0]) - snprintf(nick, sizeof(nick), "%s", unknown_name); + snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); switch(status) { case TOX_USERSTATUS_NONE: @@ -127,16 +127,16 @@ void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *user case TOX_USERSTATUS_BUSY: wprintw(prompt->window, "\n%s set status to ", nick); - wattron(prompt->window, COLOR_PAIR(3)); + wattron(prompt->window, COLOR_PAIR(RED)); wprintw(prompt->window, "[Busy]\n"); - wattroff(prompt->window, COLOR_PAIR(3)); + wattroff(prompt->window, COLOR_PAIR(RED)); break; case TOX_USERSTATUS_AWAY: wprintw(prompt->window, "\n%s set status to ", nick); - wattron(prompt->window, COLOR_PAIR(5)); + wattron(prompt->window, COLOR_PAIR(YELLOW)); wprintw(prompt->window, "[Away]\n"); - wattroff(prompt->window, COLOR_PAIR(5)); + wattroff(prompt->window, COLOR_PAIR(YELLOW)); break; } @@ -247,15 +247,15 @@ static void draw_bar() static int odd = 0; int blinkrate = 30; - attron(COLOR_PAIR(4)); + attron(COLOR_PAIR(BLUE)); mvhline(LINES - 2, 0, '_', COLS); - attroff(COLOR_PAIR(4)); + attroff(COLOR_PAIR(BLUE)); move(LINES - 1, 0); - attron(COLOR_PAIR(4) | A_BOLD); + attron(COLOR_PAIR(BLUE) | A_BOLD); printw(" TOXIC " TOXICVER " |"); - attroff(COLOR_PAIR(4) | A_BOLD); + attroff(COLOR_PAIR(BLUE) | A_BOLD); int i; @@ -267,13 +267,13 @@ static void draw_bar() odd = (odd + 1) % blinkrate; if (windows[i].blink && (odd < (blinkrate / 2))) - attron(COLOR_PAIR(3)); + attron(COLOR_PAIR(RED)); clrtoeol(); printw(" %s", windows[i].title); if (windows[i].blink && (odd < (blinkrate / 2))) - attroff(COLOR_PAIR(3)); + attroff(COLOR_PAIR(RED)); if (windows + i == active_window) { attroff(A_BOLD); From 369233ba04012caf5b022c9e72e1f530e66a6ca7 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 5 Sep 2013 01:34:23 -0400 Subject: [PATCH 59/82] remove superfluous prompt alerts and some visual changes --- src/chat.c | 59 ++++++++++++++++++++++-------------------------- src/friendlist.c | 11 ++++----- src/prompt.c | 37 ++++++++++++++++++++---------- src/windows.c | 55 +++++++++++--------------------------------- 4 files changed, 70 insertions(+), 92 deletions(-) diff --git a/src/chat.c b/src/chat.c index e13ba74..da97cda 100644 --- a/src/chat.c +++ b/src/chat.c @@ -136,32 +136,27 @@ static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS if (ctx->friendnum != num) return; - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); + char *status_msg = NULL; + int colour = 0; - switch(status) { + if (status == TOX_USERSTATUS_BUSY) { + status_msg = "[Busy]"; + colour = RED; + } + else if (status == TOX_USERSTATUS_AWAY) { + status_msg = "[Away]"; + colour = YELLOW; + } - case TOX_USERSTATUS_NONE: - wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(GREEN)); - wprintw(ctx->history, "[Online]\n"); - wattroff(ctx->history, COLOR_PAIR(GREEN)); - break; + if (status_msg != NULL) { + wattron(ctx->history, COLOR_PAIR(CYAN)); + wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattroff(ctx->history, COLOR_PAIR(CYAN)); - case TOX_USERSTATUS_BUSY: - wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, "[Busy]\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); - break; - - case TOX_USERSTATUS_AWAY: - wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(YELLOW)); - wprintw(ctx->history, "[Away]\n"); - wattroff(ctx->history, COLOR_PAIR(YELLOW)); - break; + wprintw(ctx->history, "* Chat partner set status to: "); + wattron(ctx->history, COLOR_PAIR(colour) | A_BOLD); + wprintw(ctx->history, "%s\n", status_msg); + wattroff(ctx->history, COLOR_PAIR(colour) | A_BOLD); } } @@ -378,26 +373,26 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) if (!strncmp(status, "online", strlen("online"))) { status_kind = TOX_USERSTATUS_NONE; - wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(GREEN)); + wprintw(ctx->history, "Status set to: "); + wattron(ctx->history, COLOR_PAIR(GREEN) | A_BOLD); wprintw(ctx->history, "[Online]\n"); - wattroff(ctx->history, COLOR_PAIR(GREEN)); + wattroff(ctx->history, COLOR_PAIR(GREEN) | A_BOLD); } else if (!strncmp(status, "away", strlen("away"))) { status_kind = TOX_USERSTATUS_AWAY; - wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(YELLOW)); + wprintw(ctx->history, "Status set to: "); + wattron(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD); wprintw(ctx->history, "[Away]\n"); - wattroff(ctx->history, COLOR_PAIR(YELLOW)); + wattroff(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD); } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = TOX_USERSTATUS_BUSY; - wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(RED)); + wprintw(ctx->history, "Status set to: "); + wattron(ctx->history, COLOR_PAIR(RED) | A_BOLD); wprintw(ctx->history, "[Busy]\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); + wattroff(ctx->history, COLOR_PAIR(RED) | A_BOLD); } else { diff --git a/src/friendlist.c b/src/friendlist.c index f79267b..3488d9f 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -93,7 +93,7 @@ int friendlist_onFriendAdded(Tox *m, int num) friends[i].active = true; friends[i].chatwin = -1; friends[i].online = false; - friends[i].status = TOX_USERSTATUS_INVALID; + friends[i].status = TOX_USERSTATUS_NONE; if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0') strcpy((char *) friends[i].name, "unknown"); @@ -150,10 +150,9 @@ static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) break; } - if (store_data(m, DATA_FILE)) - wprintw(self->window, "\nFailed to store messenger data\n"); - num_friends = i; + + store_data(m, DATA_FILE); select_friend(m, KEY_DOWN); } @@ -212,9 +211,9 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) } wprintw(self->window, "["); - wattron(self->window, COLOR_PAIR(colour)); + wattron(self->window, COLOR_PAIR(colour) | A_BOLD); wprintw(self->window, "O"); - wattroff(self->window, COLOR_PAIR(colour)); + wattroff(self->window, COLOR_PAIR(colour) | A_BOLD); wprintw(self->window, "] %s", friends[i].name); if (friends[i].statusmsg[0]) diff --git a/src/prompt.c b/src/prompt.c index 913c169..552c8d5 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -314,7 +314,8 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv) } nick = argv[1]; - nick[strlen(++nick)-1] = L'\0'; + if (nick[0] == '\"') + nick[strlen(++nick)-1] = L'\0'; tox_setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(self->window, "Nickname set to: %s\n", nick); @@ -358,19 +359,31 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) if (!strncmp(status, "online", strlen("online"))) { status_kind = TOX_USERSTATUS_NONE; - status_text = "Online"; - } else if (!strncmp(status, "away", strlen("away"))) { - status_kind = TOX_USERSTATUS_AWAY; - status_text = "Away"; - } else if (!strncmp(status, "busy", strlen("busy"))) { - status_kind = TOX_USERSTATUS_BUSY; - status_text = "Busy"; - } else { - wprintw(self->window, "Invalid status.\n"); - return; + wprintw(self->window, "Status set to: "); + wattron(self->window, COLOR_PAIR(GREEN) | A_BOLD); + wprintw(self->window, "[Online]\n"); + wattroff(self->window, COLOR_PAIR(GREEN) | A_BOLD); } - wprintw(self->window, "Status set to: %s\n", status_text); + else if (!strncmp(status, "away", strlen("away"))) { + status_kind = TOX_USERSTATUS_AWAY; + wprintw(self->window, "Status set to: "); + wattron(self->window, COLOR_PAIR(YELLOW) | A_BOLD); + wprintw(self->window, "[Away]\n"); + wattroff(self->window, COLOR_PAIR(YELLOW) | A_BOLD); + } + + else if (!strncmp(status, "busy", strlen("busy"))) { + status_kind = TOX_USERSTATUS_BUSY; + wprintw(self->window, "Status set to: "); + wattron(self->window, COLOR_PAIR(RED) | A_BOLD); + wprintw(self->window, "[Busy]\n"); + wattroff(self->window, COLOR_PAIR(RED) | A_BOLD); + } + + else + wprintw(self->window, "Invalid status.\n"); + tox_set_userstatus(m, status_kind); if (msg != NULL) { diff --git a/src/windows.c b/src/windows.c index a184148..42a66cd 100644 --- a/src/windows.c +++ b/src/windows.c @@ -32,7 +32,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd } wprintw(prompt->window, "\nWith the message: %s\n", data); - wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); + wprintw(prompt->window, "Type \"accept %d\" to accept it.\n", n); for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i].onFriendRequest != NULL) @@ -48,10 +48,18 @@ void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdat if (!nick[0]) snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); - if (status == 1) - wprintw(prompt->window, "\n%s has come online\n", nick, friendnumber); - else - wprintw(prompt->window, "\n%s went offline\n", nick, friendnumber); + if (status == 1) { + wattron(prompt->window, A_BOLD); + wprintw(prompt->window, "\n%s ", nick); + wattroff(prompt->window, A_BOLD); + wprintw(prompt->window, "has come online\n"); + } else { + wattron(prompt->window, A_BOLD); + wprintw(prompt->window, "\n%s ", nick); + wattroff(prompt->window, A_BOLD); + wprintw(prompt->window, "has gone offline\n"); + + } int i; @@ -93,17 +101,6 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { - /* Don't show default "Online" status message */ - if (strncmp(string, "Online", strlen(string))) { - uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; - tox_getname(m, friendnumber, (uint8_t *) &nick); - - if (!nick[0]) - snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); - - wprintw(prompt->window, "\n%s set note to: %s\n", nick, string); - } - int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { @@ -114,32 +111,6 @@ void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *userdata) { - uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; - tox_getname(m, friendnumber, (uint8_t *) &nick); - - if (!nick[0]) - snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); - - switch(status) { - case TOX_USERSTATUS_NONE: - /* Disabled because it spams a second messages when user comes online */ - break; - - case TOX_USERSTATUS_BUSY: - wprintw(prompt->window, "\n%s set status to ", nick); - wattron(prompt->window, COLOR_PAIR(RED)); - wprintw(prompt->window, "[Busy]\n"); - wattroff(prompt->window, COLOR_PAIR(RED)); - break; - - case TOX_USERSTATUS_AWAY: - wprintw(prompt->window, "\n%s set status to ", nick); - wattron(prompt->window, COLOR_PAIR(YELLOW)); - wprintw(prompt->window, "[Away]\n"); - wattroff(prompt->window, COLOR_PAIR(YELLOW)); - break; - } - int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { From 05c7727fb892ab1e821cfdd5a90ffaa368873ef7 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 5 Sep 2013 18:24:58 -0400 Subject: [PATCH 60/82] Added a statusbar to chat windows and removed spammy messages --- src/chat.c | 206 +++++++++++++++++++++++++------------------- src/friendlist.c | 7 +- src/prompt.c | 2 +- src/toxic_windows.h | 6 +- src/windows.c | 5 +- 5 files changed, 129 insertions(+), 97 deletions(-) diff --git a/src/chat.c b/src/chat.c index da97cda..fec6d02 100644 --- a/src/chat.c +++ b/src/chat.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -23,13 +24,20 @@ extern char *DATA_FILE; extern int store_data(Tox *m, char *path); typedef struct { - int friendnum; wchar_t line[MAX_STR_SIZE]; size_t pos; WINDOW *history; WINDOW *linewin; } ChatContext; +typedef struct { + WINDOW *topline; + uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; + TOX_USERSTATUS status; + bool is_online; + int max_len; /* set equal to the window's max x coordinate */ +} StatusBar; + void print_help(ChatContext *self); void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd); @@ -44,16 +52,15 @@ struct tm *get_time(void) static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) { - ChatContext *ctx = (ChatContext *) self->x; - uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; - struct tm *timeinfo = get_time(); - - if (ctx->friendnum != num) + if (self->friendnum != num) return; + ChatContext *ctx = (ChatContext *) self->x; + + struct tm *timeinfo = get_time(); + + uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_getname(m, num, (uint8_t *) &nick); - msg[len-1] = '\0'; - nick[TOX_MAX_NAME_LENGTH-1] = '\0'; wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); @@ -69,35 +76,25 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) { - ChatContext *ctx = (ChatContext *) self->x; - struct tm *timeinfo = get_time(); - - if (ctx->friendnum != num) + if (self->friendnum != num) return; - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); + StatusBar *statusbar = (StatusBar *) self->s; + statusbar->is_online = status == 1 ? true : false; - if (status == 1) - wprintw(ctx->history, "* Chat partner has come online\n"); - else - wprintw(ctx->history, "* Chat partner went offline\n"); } static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len) { + if (self->friendnum != num) + return; + ChatContext *ctx = (ChatContext *) self->x; struct tm *timeinfo = get_time(); - if (ctx->friendnum != num) - return; - - uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; + uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_getname(m, num, (uint8_t *) &nick); - action[len - 1] = '\0'; - wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wattroff(ctx->history, COLOR_PAIR(CYAN)); @@ -112,70 +109,30 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin 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) + if (self->friendnum != num) return; - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); - - nick[len - 1] = '\0'; - snprintf(self->title, sizeof(self->title), "[%s]", nick); - - wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick); + snprintf(self->name, sizeof(self->name), "%s", nick); } static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) { - ChatContext *ctx = (ChatContext *) self->x; - struct tm *timeinfo = get_time(); - - if (ctx->friendnum != num) + if (self->friendnum != num) return; - char *status_msg = NULL; - int colour = 0; - - if (status == TOX_USERSTATUS_BUSY) { - status_msg = "[Busy]"; - colour = RED; - } - else if (status == TOX_USERSTATUS_AWAY) { - status_msg = "[Away]"; - colour = YELLOW; - } - - if (status_msg != NULL) { - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); - - wprintw(ctx->history, "* Chat partner set status to: "); - wattron(ctx->history, COLOR_PAIR(colour) | A_BOLD); - wprintw(ctx->history, "%s\n", status_msg); - wattroff(ctx->history, COLOR_PAIR(colour) | A_BOLD); - } + StatusBar *statusbar = (StatusBar *) self->s; + statusbar->status = status; } static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) { - ChatContext *ctx = (ChatContext *) self->x; - struct tm *timeinfo = get_time(); - - if (ctx->friendnum != num) + if (self->friendnum != num) return; - status[len - 1] = '\0'; + StatusBar *statusbar = (StatusBar *) self->s; - if (strncmp(status, "Online", strlen("status"))) { /* Ignore default "Online" message */ - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status); - } + if (strncmp(status, "Online", strlen(status))) /* Ignore default "Online" message */ + snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", status); } /* check that the string has one non-space character */ @@ -227,6 +184,8 @@ static char *wc_to_char(wchar_t ch) static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) { ChatContext *ctx = (ChatContext *) self->x; + StatusBar *statusbar = (StatusBar *) self->s; + struct tm *timeinfo = get_time(); int x, y, y2, x2; @@ -239,7 +198,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) #else if (isprint(key)) { #endif - if (ctx->pos < MAX_STR_SIZE) { + if (ctx->pos < (MAX_STR_SIZE-1)) { mvwaddstr(self->window, y, x, wc_to_char(key)); ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = L'\0'; @@ -268,8 +227,9 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) if (line[0] == '/') { if (close_win = !strncmp(line, "/close", strlen("/close"))) { - int f_num = ctx->friendnum; + int f_num = self->friendnum; delwin(ctx->linewin); + delwin(statusbar->topline); del_window(self); disable_chatwin(f_num); } else { @@ -289,7 +249,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wattroff(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "%s\n", line); - if (tox_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { + if (tox_sendmessage(m, self->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(RED)); @@ -297,9 +257,10 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) } } - if (close_win) + if (close_win) { free(ctx); - else { + free(statusbar); + } else { ctx->line[0] = L'\0'; ctx->pos = 0; } @@ -313,6 +274,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { wclear(self->window); wclear(ctx->history); + wprintw(ctx->history, "\n\n"); int x, y; getmaxyx(self->window, y, x); (void) x; @@ -351,7 +313,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) wprintw(ctx->history, "* %s %s\n", selfname, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); - if (tox_sendaction(m, ctx->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { + if (tox_sendaction(m, self->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(RED)); @@ -432,7 +394,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) } else if (!strcmp(cmd, "/myid")) { - char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; + char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {'\0'}; int i; uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; tox_getaddress(m, address); @@ -453,9 +415,58 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) static void chat_onDraw(ToxWindow *self, Tox *m) { curs_set(1); + int x, y; getmaxyx(self->window, y, x); - (void) y; + + /* Draw status bar */ + StatusBar *statusbar = (StatusBar *) self->s; + mvwhline(statusbar->topline, 1, 0, '-', x); + wmove(statusbar->topline, 0, 0); + + /* Draw name, status and note in statusbar */ + uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; + snprintf(nick, sizeof(nick), "%s", self->name); + + if (statusbar->is_online) { + char *status_text = "Unknown"; + int colour = WHITE; + + TOX_USERSTATUS status = statusbar->status; + + switch(status) { + case TOX_USERSTATUS_NONE: + status_text = "Online"; + colour = GREEN; + break; + case TOX_USERSTATUS_AWAY: + status_text = "Away"; + colour = YELLOW; + break; + case TOX_USERSTATUS_BUSY: + status_text = "Busy"; + colour = RED; + break; + } + wattron(statusbar->topline, A_BOLD); + wprintw(statusbar->topline, "%s", nick); + wattroff(statusbar->topline, A_BOLD); + wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); + wprintw(statusbar->topline, " [%s]", status_text); + wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); + + } else { + wattron(statusbar->topline, A_BOLD); + wprintw(statusbar->topline, "%s", nick); + wattroff(statusbar->topline, A_BOLD); + wprintw(statusbar->topline, " [Offline]"); + } + + if (statusbar->statusmsg[0]) + wprintw(statusbar->topline, " | %s", statusbar->statusmsg); + + wprintw(statusbar->topline, "\n"); + ChatContext *ctx = (ChatContext *) self->x; mvwhline(ctx->linewin, 0, 0, '_', x); wrefresh(self->window); @@ -464,11 +475,25 @@ static void chat_onDraw(ToxWindow *self, Tox *m) static void chat_onInit(ToxWindow *self, Tox *m) { int x, y; - ChatContext *ctx = (ChatContext *) self->x; getmaxyx(self->window, y, x); - ctx->history = subwin(self->window, y - 4, x, 0, 0); + + /* Init statusbar info */ + StatusBar *statusbar = (StatusBar *) self->s; + statusbar->status = tox_get_userstatus(m, self->friendnum); + statusbar->is_online = tox_friendstatus(m, self->friendnum) == TOX_FRIEND_ONLINE; + statusbar->max_len = x; + + char statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'}; + tox_copy_statusmessage(m, self->friendnum, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); + if (strncmp(statusmsg, "Online", strlen(statusmsg))) + snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); + + /* Init subwindows */ + ChatContext *ctx = (ChatContext *) self->x; + statusbar->topline = subwin(self->window, 2, x, 0, 0); + ctx->history = subwin(self->window, y-3, x, 0, 0); scrollok(ctx->history, 1); - ctx->linewin = subwin(self->window, 2, x, y - 4, 0); + ctx->linewin = subwin(self->window, 0, x, y-4, 0); print_help(ctx); wmove(self->window, y - CURS_Y_OFFSET, 0); } @@ -507,13 +532,16 @@ ToxWindow new_chat(Tox *m, int friendnum) ret.onStatusMessageChange = &chat_onStatusMessageChange; ret.onAction = &chat_onAction; - uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; - tox_getname(m, friendnum, (uint8_t *) &nick); + uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'}; + tox_getname(m, friendnum, (uint8_t *) &name); - snprintf(ret.title, sizeof(ret.title), "[%s]", nick); + snprintf(ret.name, sizeof(ret.name), "%s", name); ChatContext *x = calloc(1, sizeof(ChatContext)); + StatusBar *s = calloc(1, sizeof(StatusBar)); ret.x = x; - x->friendnum = friendnum; + ret.s = s; + ret.friendnum = friendnum; + return ret; } diff --git a/src/friendlist.c b/src/friendlist.c index 3488d9f..c487689 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -166,6 +166,7 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key) set_active_window(friends[num_selected].chatwin); } else { friends[num_selected].chatwin = add_window(m, new_chat(m, friends[num_selected].num)); + set_active_window(friends[num_selected].chatwin); } } else if (key == 0x107 || key == 0x8 || key == 0x7f) delete_friend(m, self, num_selected, key); @@ -214,14 +215,14 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wattron(self->window, COLOR_PAIR(colour) | A_BOLD); wprintw(self->window, "O"); wattroff(self->window, COLOR_PAIR(colour) | A_BOLD); - wprintw(self->window, "] %s", friends[i].name); + wprintw(self->window, "]%s", friends[i].name); if (friends[i].statusmsg[0]) wprintw(self->window, " (%s)\n", friends[i].statusmsg); else wprintw(self->window, "\n"); } else { - wprintw(self->window, "[O] %s\n", friends[i].name); + wprintw(self->window, "[O]%s\n", friends[i].name); } } } @@ -254,6 +255,6 @@ ToxWindow new_friendlist() ret.onStatusChange = &friendlist_onStatusChange; ret.onStatusMessageChange = &friendlist_onStatusMessageChange; - strcpy(ret.title, "[friends]"); + strcpy(ret.name, "friends"); return ret; } diff --git a/src/prompt.c b/src/prompt.c index 552c8d5..aff1b68 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -565,6 +565,6 @@ ToxWindow new_prompt() ret.onKey = &prompt_onKey; ret.onDraw = &prompt_onDraw; ret.onInit = &prompt_onInit; - strcpy(ret.title, "[prompt]"); + strcpy(ret.name, "prompt"); return ret; } diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 23aa4ae..834941b 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -47,9 +47,13 @@ struct ToxWindow_ { void(*onStatusChange)(ToxWindow *, Tox *, int, TOX_USERSTATUS); void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); - char title[256]; + char name[TOX_MAX_NAME_LENGTH]; + + int friendnum; void *x; + void *s; + bool blink; WINDOW *window; diff --git a/src/windows.c b/src/windows.c index 42a66cd..75cb10e 100644 --- a/src/windows.c +++ b/src/windows.c @@ -58,7 +58,6 @@ void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdat wprintw(prompt->window, "\n%s ", nick); wattroff(prompt->window, A_BOLD); wprintw(prompt->window, "has gone offline\n"); - } int i; @@ -147,7 +146,7 @@ int add_window(Tox *m, ToxWindow w) windows[i] = w; w.onInit(&w, m); - active_window = windows + i; + //active_window = windows + i; return i; } @@ -241,7 +240,7 @@ static void draw_bar() attron(COLOR_PAIR(RED)); clrtoeol(); - printw(" %s", windows[i].title); + printw(" [%s]", windows[i].name); if (windows[i].blink && (odd < (blinkrate / 2))) attroff(COLOR_PAIR(RED)); From 2040368dd94f65282565dc05542d3bccb8cb8771 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 5 Sep 2013 18:26:03 -0400 Subject: [PATCH 61/82] changed version to 0.2.0 --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index a764e18..f4e8ab8 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.65]) -AC_INIT([toxic], [0.1.1], [http://tox.im/]) +AC_INIT([toxic], [0.2.0], [http://tox.im/]) AC_CONFIG_AUX_DIR(configure_aux) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([config.h]) From 3ddae51998a5fb6e2f1bc1bdce1153be8fef0cb1 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 5 Sep 2013 18:36:46 -0400 Subject: [PATCH 62/82] small fix --- src/chat.c | 2 +- src/windows.c | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/chat.c b/src/chat.c index fec6d02..2c8a436 100644 --- a/src/chat.c +++ b/src/chat.c @@ -494,6 +494,7 @@ static void chat_onInit(ToxWindow *self, Tox *m) ctx->history = subwin(self->window, y-3, x, 0, 0); scrollok(ctx->history, 1); ctx->linewin = subwin(self->window, 0, x, y-4, 0); + wprintw(ctx->history, "\n\n"); print_help(ctx); wmove(self->window, y - CURS_Y_OFFSET, 0); } @@ -534,7 +535,6 @@ ToxWindow new_chat(Tox *m, int friendnum) uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_getname(m, friendnum, (uint8_t *) &name); - snprintf(ret.name, sizeof(ret.name), "%s", name); ChatContext *x = calloc(1, sizeof(ChatContext)); diff --git a/src/windows.c b/src/windows.c index 75cb10e..5849c1d 100644 --- a/src/windows.c +++ b/src/windows.c @@ -146,7 +146,6 @@ int add_window(Tox *m, ToxWindow w) windows[i] = w; w.onInit(&w, m); - //active_window = windows + i; return i; } From ba7d01d3c1dcf3d44e7e84c3a8e63e7a297b38d5 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 6 Sep 2013 00:56:55 -0400 Subject: [PATCH 63/82] bug fix and remove redundant code --- src/chat.c | 12 ++++-------- src/friendlist.c | 3 ++- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/chat.c b/src/chat.c index 2c8a436..03bc2e3 100644 --- a/src/chat.c +++ b/src/chat.c @@ -81,7 +81,6 @@ void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) StatusBar *statusbar = (StatusBar *) self->s; statusbar->is_online = status == 1 ? true : false; - } static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len) @@ -425,9 +424,6 @@ static void chat_onDraw(ToxWindow *self, Tox *m) wmove(statusbar->topline, 0, 0); /* Draw name, status and note in statusbar */ - uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; - snprintf(nick, sizeof(nick), "%s", self->name); - if (statusbar->is_online) { char *status_text = "Unknown"; int colour = WHITE; @@ -449,17 +445,17 @@ static void chat_onDraw(ToxWindow *self, Tox *m) break; } wattron(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, "%s", nick); + wprintw(statusbar->topline, " %s ", self->name); wattroff(statusbar->topline, A_BOLD); wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); - wprintw(statusbar->topline, " [%s]", status_text); + wprintw(statusbar->topline, "[%s]", status_text); wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); } else { wattron(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, "%s", nick); + wprintw(statusbar->topline, " %s ", self->name); wattroff(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, " [Offline]"); + wprintw(statusbar->topline, "[Offline]"); } if (statusbar->statusmsg[0]) diff --git a/src/friendlist.c b/src/friendlist.c index c487689..501081a 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -124,7 +124,8 @@ static void select_friend(Tox *m, wint_t key) } } } else if (key == KEY_DOWN) { - while ((n = (n + 1) % num_friends) != num_selected) { + while (++n != num_selected) { + n = n % num_friends; if (friends[n].active) { num_selected = n; return; From 9c7cad0d550bba1ac0306019450b2650a0ff64b9 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 6 Sep 2013 02:51:10 -0400 Subject: [PATCH 64/82] made prompt window beep/blink on friend request --- src/chat.c | 8 -------- src/friendlist.c | 5 +++-- src/prompt.c | 19 +++++++++++++++++++ src/toxic_windows.h | 8 ++++++++ src/windows.c | 16 +++++----------- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/chat.c b/src/chat.c index 03bc2e3..0e00b60 100644 --- a/src/chat.c +++ b/src/chat.c @@ -30,14 +30,6 @@ typedef struct { WINDOW *linewin; } ChatContext; -typedef struct { - WINDOW *topline; - uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; - TOX_USERSTATUS status; - bool is_online; - int max_len; /* set equal to the window's max x coordinate */ -} StatusBar; - void print_help(ChatContext *self); void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd); diff --git a/src/friendlist.c b/src/friendlist.c index 501081a..755536e 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -76,8 +76,9 @@ void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, ui if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends) return; - memcpy((char *) &friends[num].statusmsg, (char *) str, len); - friends[num].statusmsg[len] = 0; + /* Ignore default "Online" status message */ + if (strncmp(str, "Online", strlen(str))) + memcpy((char *) &friends[num].statusmsg, (char *) str, len); } int friendlist_onFriendAdded(Tox *m, int num) diff --git a/src/prompt.c b/src/prompt.c index aff1b68..4a06aa4 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -57,6 +57,24 @@ static struct { { "note", cmd_note }, }; +void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16_t length) +{ + int n = add_req(key); + wprintw(self->window, "\nFriend request from:\n"); + + int i; + + for (i = 0; i < KEY_SIZE_BYTES; ++i) { + wprintw(self->window, "%02x", key[i] & 0xff); + } + + wprintw(self->window, "\n\nWith the message: %s\n\n", data); + wprintw(self->window, "Type \"accept %d\" to accept it.\n", n); + + self->blink = true; + beep(); +} + // XXX: int add_req(uint8_t *public_key) { @@ -565,6 +583,7 @@ ToxWindow new_prompt() ret.onKey = &prompt_onKey; ret.onDraw = &prompt_onDraw; ret.onInit = &prompt_onInit; + ret.onFriendRequest = &prompt_onFriendRequest; strcpy(ret.name, "prompt"); return ret; } diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 834941b..8958270 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -59,6 +59,14 @@ struct ToxWindow_ { WINDOW *window; }; +typedef struct { + WINDOW *topline; + uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; + TOX_USERSTATUS status; + bool is_online; + int max_len; /* set to the window's max x coordinate */ +} StatusBar; + void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata); void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata); void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); diff --git a/src/windows.c b/src/windows.c index 5849c1d..f298b71 100644 --- a/src/windows.c +++ b/src/windows.c @@ -22,18 +22,8 @@ static Tox *m; /* CALLBACKS START */ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { - int n = add_req(public_key); - wprintw(prompt->window, "\nFriend request from:\n"); - 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); - wprintw(prompt->window, "Type \"accept %d\" to accept it.\n", n); - + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i].onFriendRequest != NULL) windows[i].onFriendRequest(&windows[i], public_key, data, length); @@ -49,15 +39,19 @@ void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdat snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); if (status == 1) { + wattron(prompt->window, COLOR_PAIR(GREEN)); wattron(prompt->window, A_BOLD); wprintw(prompt->window, "\n%s ", nick); wattroff(prompt->window, A_BOLD); wprintw(prompt->window, "has come online\n"); + wattroff(prompt->window, COLOR_PAIR(GREEN)); } else { + wattron(prompt->window, COLOR_PAIR(RED)); wattron(prompt->window, A_BOLD); wprintw(prompt->window, "\n%s ", nick); wattroff(prompt->window, A_BOLD); wprintw(prompt->window, "has gone offline\n"); + wattroff(prompt->window, COLOR_PAIR(RED)); } int i; From 261310b091a5e81e8d669d6af91430d4b5b01604 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 6 Sep 2013 19:59:45 -0400 Subject: [PATCH 65/82] added status bar to prompt and fixed some bugs --- src/chat.c | 29 +++--- src/chat.h | 2 +- src/friendlist.c | 6 +- src/main.c | 5 +- src/prompt.c | 211 +++++++++++++++++++++++++++++++------------- src/prompt.h | 1 + src/toxic_windows.h | 4 +- 7 files changed, 183 insertions(+), 75 deletions(-) diff --git a/src/chat.c b/src/chat.c index 0e00b60..d6a9c65 100644 --- a/src/chat.c +++ b/src/chat.c @@ -230,7 +230,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) /* make sure the string has at least non-space character */ if (!string_is_empty(line)) { uint8_t selfname[TOX_MAX_NAME_LENGTH]; - tox_getselfname(m, selfname, sizeof(selfname)); + tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH); wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); @@ -313,8 +313,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) else if (!strncmp(cmd, "/status ", strlen("/status "))) { char *status = strchr(cmd, ' '); - char *msg; - char *status_text; + uint8_t *msg; if (status == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); @@ -354,24 +353,27 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) } tox_set_userstatus(m, status_kind); + prompt_update_status(self->prompt, status_kind); msg = strchr(status, ' '); if (msg != NULL) { msg++; - tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); + tox_set_statusmessage(m, msg, strlen(msg) + 1); + prompt_update_statusmessage(self->prompt, msg); wprintw(ctx->history, "Personal note set to: %s\n", msg); } } else if (!strncmp(cmd, "/note ", strlen("/note "))) { - char *msg = strchr(cmd, ' '); + uint8_t *msg = strchr(cmd, ' '); msg++; + tox_set_statusmessage(m, msg, strlen(msg) + 1); + prompt_update_statusmessage(self->prompt, msg); wprintw(ctx->history, "Personal note set to: %s\n", msg); - tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); } else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { - char *nick; + uint8_t *nick; nick = strchr(cmd, ' '); if (nick == NULL) { @@ -380,7 +382,8 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) } nick++; - tox_setname(m, (uint8_t *) nick, strlen(nick) + 1); + tox_setname(m, nick, strlen(nick) + 1); + prompt_update_nick(self->prompt, nick); wprintw(ctx->history, "Nickname set to: %s\n", nick); } @@ -436,6 +439,7 @@ static void chat_onDraw(ToxWindow *self, Tox *m) colour = RED; break; } + wattron(statusbar->topline, A_BOLD); wprintw(statusbar->topline, " %s ", self->name); wattroff(statusbar->topline, A_BOLD); @@ -450,8 +454,11 @@ static void chat_onDraw(ToxWindow *self, Tox *m) wprintw(statusbar->topline, "[Offline]"); } - if (statusbar->statusmsg[0]) + if (statusbar->statusmsg[0]) { + wattron(statusbar->topline, A_BOLD); wprintw(statusbar->topline, " | %s", statusbar->statusmsg); + wattroff(statusbar->topline, A_BOLD); + } wprintw(statusbar->topline, "\n"); @@ -506,7 +513,7 @@ void print_help(ChatContext *self) wattroff(self->history, COLOR_PAIR(CYAN)); } -ToxWindow new_chat(Tox *m, int friendnum) +ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum) { ToxWindow ret; memset(&ret, 0, sizeof(ret)); @@ -529,6 +536,8 @@ ToxWindow new_chat(Tox *m, int friendnum) StatusBar *s = calloc(1, sizeof(StatusBar)); ret.x = x; ret.s = s; + + ret.prompt = prompt; ret.friendnum = friendnum; return ret; diff --git a/src/chat.h b/src/chat.h index d222b0d..25e64b7 100644 --- a/src/chat.h +++ b/src/chat.h @@ -1,6 +1,6 @@ #ifndef CHAT_H_6489PZ13 #define CHAT_H_6489PZ13 -ToxWindow new_chat(Tox *m, int friendnum); +ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum); #endif /* end of include guard: CHAT_H_6489PZ13 */ diff --git a/src/friendlist.c b/src/friendlist.c index 755536e..ec2b81e 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -19,6 +19,8 @@ extern char *DATA_FILE; extern int store_data(Tox *m, char *path); +extern ToxWindow *prompt; + typedef struct { uint8_t name[TOX_MAX_NAME_LENGTH]; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; @@ -40,7 +42,7 @@ void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16 return; if (friends[num].chatwin == -1) - friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); + friends[num].chatwin = add_window(m, new_chat(m, prompt, friends[num].num)); } void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) @@ -167,7 +169,7 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key) if (friends[num_selected].chatwin != -1) { set_active_window(friends[num_selected].chatwin); } else { - friends[num_selected].chatwin = add_window(m, new_chat(m, friends[num_selected].num)); + friends[num_selected].chatwin = add_window(m, new_chat(m, prompt, friends[num_selected].num)); set_active_window(friends[num_selected].chatwin); } } else if (key == 0x107 || key == 0x8 || key == 0x7f) diff --git a/src/main.c b/src/main.c index c74cb34..8d71e4d 100644 --- a/src/main.c +++ b/src/main.c @@ -44,6 +44,7 @@ /* Export for use in Callbacks */ char *DATA_FILE = NULL; char *SRVLIST_FILE = NULL; +ToxWindow *prompt = NULL; void on_window_resize(int sig) { @@ -392,7 +393,7 @@ int main(int argc, char *argv[]) init_term(); Tox *m = init_tox(); - ToxWindow *prompt = init_windows(m); + prompt = init_windows(m); if (f_loadfromfile) load_data(m, DATA_FILE); @@ -411,6 +412,8 @@ int main(int argc, char *argv[]) attroff(COLOR_PAIR(RED) | A_BOLD); } + prompt_init_statusbar(prompt, m); + while (true) { /* Update tox */ do_tox(m, prompt); diff --git a/src/prompt.c b/src/prompt.c index 4a06aa4..6852e49 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -10,6 +10,7 @@ #include #include +#include "toxic_windows.h" #include "prompt.h" extern char *DATA_FILE; @@ -30,12 +31,11 @@ void cmd_help(ToxWindow *, Tox *m, int, char **); void cmd_msg(ToxWindow *, Tox *m, int, char **); void cmd_myid(ToxWindow *, Tox *m, int, char **); void cmd_nick(ToxWindow *, Tox *m, int, char **); -void cmd_mynick(ToxWindow *, Tox *m, int, char **); void cmd_quit(ToxWindow *, Tox *m, int, char **); void cmd_status(ToxWindow *, Tox *m, int, char **); void cmd_note(ToxWindow *, Tox *m, int, char **); -#define NUM_COMMANDS 14 +#define NUM_COMMANDS 13 static struct { char *name; @@ -50,28 +50,48 @@ static struct { { "msg", cmd_msg }, { "myid", cmd_myid }, { "nick", cmd_nick }, - { "mynick", cmd_mynick }, { "q", cmd_quit }, { "quit", cmd_quit }, { "status", cmd_status }, { "note", cmd_note }, }; -void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16_t length) +/* Updates own nick in prompt statusbar */ +void prompt_update_nick(ToxWindow *prompt, uint8_t *nick) +{ + StatusBar *statusbar = (StatusBar *) prompt->s; + snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick); +} + +/* Updates own statusmessage in prompt statusbar */ +void prompt_update_statusmessage(ToxWindow *prompt, uint8_t *statusmsg) +{ + StatusBar *statusbar = (StatusBar *) prompt->s; + snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); +} + +/* Updates own status in prompt statusbar */ +void prompt_update_status(ToxWindow *prompt, TOX_USERSTATUS status) +{ + StatusBar *statusbar = (StatusBar *) prompt->s; + statusbar->status = status; +} + +void prompt_onFriendRequest(ToxWindow *prompt, uint8_t *key, uint8_t *data, uint16_t length) { int n = add_req(key); - wprintw(self->window, "\nFriend request from:\n"); + wprintw(prompt->window, "\nFriend request from:\n"); int i; for (i = 0; i < KEY_SIZE_BYTES; ++i) { - wprintw(self->window, "%02x", key[i] & 0xff); + wprintw(prompt->window, "%02x", key[i] & 0xff); } - wprintw(self->window, "\n\nWith the message: %s\n\n", data); - wprintw(self->window, "Type \"accept %d\" to accept it.\n", n); + wprintw(prompt->window, "\n\nWith the message: %s\n\n", data); + wprintw(prompt->window, "Type \"accept %d\" to accept it.\n", n); - self->blink = true; + prompt->blink = true; beep(); } @@ -215,6 +235,7 @@ void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_clear(ToxWindow *self, Tox *m, int argc, char **argv) { wclear(self->window); + wprintw(self->window, "\n\n"); } void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv) @@ -262,7 +283,7 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) { wclear(self->window); wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(self->window, "Commands:\n"); + wprintw(self->window, "\n\nCommands:\n"); wattroff(self->window, A_BOLD); wprintw(self->window, " connect : Connect to DHT server\n"); @@ -270,7 +291,6 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) wprintw(self->window, " status : Set your status with optional note\n"); wprintw(self->window, " note : Set a personal note\n"); wprintw(self->window, " nick : Set your nickname\n"); - wprintw(self->window, " mynick : Print your current nickname\n"); wprintw(self->window, " accept : Accept friend request\n"); wprintw(self->window, " myid : Print your ID\n"); wprintw(self->window, " quit/exit : Exit Toxic\n"); @@ -297,6 +317,12 @@ void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) id = argv[1]; msg = argv[2]; + + if (id == NULL || msg == NULL) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + msg[strlen(++msg)-1] = L'\0'; if (tox_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0) @@ -323,7 +349,7 @@ void cmd_myid(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv) { - char *nick; + uint8_t *nick; /* check arguments */ if (argc != 1) { @@ -332,82 +358,74 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv) } nick = argv[1]; + + if (nick == NULL) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + if (nick[0] == '\"') nick[strlen(++nick)-1] = L'\0'; - tox_setname(m, (uint8_t *) nick, strlen(nick) + 1); - wprintw(self->window, "Nickname set to: %s\n", nick); + tox_setname(m, nick, strlen(nick) + 1); + prompt_update_nick(self, nick); - if (store_data(m, DATA_FILE)) { - wprintw(self->window, "\nCould not store Messenger data\n"); - } -} - -void cmd_mynick(ToxWindow *self, Tox *m, int argc, char **argv) -{ - uint8_t *nick = malloc(TOX_MAX_NAME_LENGTH); - tox_getselfname(m, nick, TOX_MAX_NAME_LENGTH); - wprintw(self->window, "Current nickname: %s\n", nick); - free(nick); + store_data(m, DATA_FILE); } void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) { - if (argc != 1 && argc != 2) { + char *status, *status_text; + uint8_t *msg = NULL; + + if (argc < 1 || argc > 2) { wprintw(self->window, "Wrong number of arguments.\n"); return; } - char *status, *status_text; - char *msg = NULL; - - /* check arguments */ if (argc == 2) { + msg = argv[2]; + + if (msg == NULL) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + if (msg[0] != '\"') { wprintw(self->window, "Messages must be enclosed in quotes.\n"); return; } } - status = argv[1]; + if (status == NULL) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + TOX_USERSTATUS status_kind; - if (!strncmp(status, "online", strlen("online"))) { + if (!strncmp(status, "online", strlen("online"))) status_kind = TOX_USERSTATUS_NONE; - wprintw(self->window, "Status set to: "); - wattron(self->window, COLOR_PAIR(GREEN) | A_BOLD); - wprintw(self->window, "[Online]\n"); - wattroff(self->window, COLOR_PAIR(GREEN) | A_BOLD); - } - else if (!strncmp(status, "away", strlen("away"))) { + else if (!strncmp(status, "away", strlen("away"))) status_kind = TOX_USERSTATUS_AWAY; - wprintw(self->window, "Status set to: "); - wattron(self->window, COLOR_PAIR(YELLOW) | A_BOLD); - wprintw(self->window, "[Away]\n"); - wattroff(self->window, COLOR_PAIR(YELLOW) | A_BOLD); - } - else if (!strncmp(status, "busy", strlen("busy"))) { + else if (!strncmp(status, "busy", strlen("busy"))) status_kind = TOX_USERSTATUS_BUSY; - wprintw(self->window, "Status set to: "); - wattron(self->window, COLOR_PAIR(RED) | A_BOLD); - wprintw(self->window, "[Busy]\n"); - wattroff(self->window, COLOR_PAIR(RED) | A_BOLD); - } else wprintw(self->window, "Invalid status.\n"); tox_set_userstatus(m, status_kind); + prompt_update_status(self, status_kind); if (msg != NULL) { msg[strlen(++msg)-1] = L'\0'; /* remove opening and closing quotes */ - tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); - wprintw(self->window, "Personal note set to: %s\n", msg); + tox_set_statusmessage(m, msg, strlen(msg) + 1); + prompt_update_statusmessage(self, msg); } } @@ -418,19 +436,22 @@ void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv) return; } - char *msg; + if (argv[1] == NULL) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } - /* check arguments */ - if (argv[1] && argv[1][0] != '\"') { + if (argv[1][0] != '\"') { wprintw(self->window, "Messages must be enclosed in quotes.\n"); return; } + uint8_t *msg; msg = argv[1]; msg[strlen(++msg)-1] = L'\0'; - tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); - wprintw(self->window, "Personal note set to: %s\n", msg); + tox_set_statusmessage(m, msg, strlen(msg) + 1); + prompt_update_statusmessage(self, msg); } static void execute(ToxWindow *self, Tox *m, char *u_cmd) @@ -542,9 +563,8 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) /* BACKSPACE key: Remove one character from line */ else if (key == 0x107 || key == 0x8 || key == 0x7f) { - if (prompt_buf_pos != 0) { + if (prompt_buf_pos != 0) prompt_buf[--prompt_buf_pos] = 0; - } } } @@ -552,20 +572,64 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) { curs_set(1); int x, y; - getyx(self->window, y, x); - (void) x; int i; + getyx(self->window, y, x); for (i = 0; i < (strlen(prompt_buf)); ++i) { if ((prompt_buf[i] == '\n') && (y != 0)) --y; } + StatusBar *statusbar = (StatusBar *) self->s; + + werase(statusbar->topline); + + if (tox_isconnected(m)) { + int colour = WHITE; + char *status_text = "Unknown"; + + switch(statusbar->status) { + case TOX_USERSTATUS_NONE: + status_text = "Online"; + colour = GREEN; + break; + case TOX_USERSTATUS_AWAY: + status_text = "Away"; + colour = YELLOW; + break; + case TOX_USERSTATUS_BUSY: + status_text = "Busy"; + colour = RED; + break; + } + + wattron(statusbar->topline, A_BOLD); + wprintw(statusbar->topline, "%s ", statusbar->nick); + wattron(statusbar->topline, A_BOLD); + wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); + wprintw(statusbar->topline, "[%s]", status_text); + wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); + } else { + wattron(statusbar->topline, A_BOLD); + wprintw(statusbar->topline, "%s ", statusbar->nick); + wattroff(statusbar->topline, A_BOLD); + wprintw(statusbar->topline, "[Offline]"); + } + + if (statusbar->statusmsg[0]) { + wattron(statusbar->topline, A_BOLD); + wprintw(statusbar->topline, " | %s", statusbar->statusmsg); + wattroff(statusbar->topline, A_BOLD); + } + + wprintw(statusbar->topline, "\n"); + wattron(self->window, COLOR_PAIR(GREEN)); mvwprintw(self->window, y, 0, "# "); wattroff(self->window, COLOR_PAIR(GREEN)); mvwprintw(self->window, y, 2, "%s", prompt_buf); wclrtoeol(self->window); + wrefresh(self->window); } @@ -576,6 +640,29 @@ static void prompt_onInit(ToxWindow *self, Tox *m) wclrtoeol(self->window); } +void prompt_init_statusbar(ToxWindow *self, Tox *m) +{ + int x, y; + getmaxyx(self->window, y, x); + + /* Init statusbar info */ + StatusBar *statusbar = (StatusBar *) self->s; + statusbar->status = TOX_USERSTATUS_NONE; + statusbar->max_len = x; + + uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; + tox_getselfname(m, (uint8_t *) &nick, TOX_MAX_NAME_LENGTH); + snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick); + + uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; + tox_copy_self_statusmessage(m, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); + if (strncmp(statusmsg, "Online", strlen(statusmsg))) + snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); + + /* Init statusbar subwindow */ + statusbar->topline = subwin(self->window, 2, x, 0, 0); +} + ToxWindow new_prompt() { ToxWindow ret; @@ -585,5 +672,9 @@ ToxWindow new_prompt() ret.onInit = &prompt_onInit; ret.onFriendRequest = &prompt_onFriendRequest; strcpy(ret.name, "prompt"); + + StatusBar *s = calloc(1, sizeof(StatusBar)); + ret.s = s; + return ret; } diff --git a/src/prompt.h b/src/prompt.h index b42f6fe..9c68831 100644 --- a/src/prompt.h +++ b/src/prompt.h @@ -6,6 +6,7 @@ ToxWindow new_prompt(); int add_req(uint8_t *public_key); unsigned char *hex_string_to_bin(char hex_string[]); +void prompt_init_statusbar(ToxWindow *self, Tox *m); #endif /* end of include guard: PROMPT_H_UZYGWFFL */ diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 8958270..7df1f1c 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -49,10 +49,11 @@ struct ToxWindow_ { void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); char name[TOX_MAX_NAME_LENGTH]; - int friendnum; + void *x; void *s; + void *prompt; bool blink; @@ -62,6 +63,7 @@ struct ToxWindow_ { typedef struct { WINDOW *topline; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; + uint8_t nick[TOX_MAX_NAME_LENGTH]; TOX_USERSTATUS status; bool is_online; int max_len; /* set to the window's max x coordinate */ From 9bf9dbe875fd8e07f313d881a7b721e9762dcd64 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 6 Sep 2013 21:37:16 -0400 Subject: [PATCH 66/82] better way to check connection status --- src/main.c | 2 ++ src/prompt.c | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 8d71e4d..68808a7 100644 --- a/src/main.c +++ b/src/main.c @@ -238,9 +238,11 @@ static void do_tox(Tox *m, ToxWindow *prompt) } } else if (!dht_on && tox_isconnected(m)) { dht_on = true; + prompt_update_connectionstatus(prompt, dht_on); wprintw(prompt->window, "\nDHT connected.\n"); } else if (dht_on && !tox_isconnected(m)) { dht_on = false; + prompt_update_connectionstatus(prompt, dht_on); wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); } diff --git a/src/prompt.c b/src/prompt.c index 6852e49..40165d2 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -77,6 +77,13 @@ void prompt_update_status(ToxWindow *prompt, TOX_USERSTATUS status) statusbar->status = status; } +/* Updates own connection status */ +void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected) +{ + StatusBar *statusbar = (StatusBar *) prompt->s; + statusbar->is_online = is_connected; +} + void prompt_onFriendRequest(ToxWindow *prompt, uint8_t *key, uint8_t *data, uint16_t length) { int n = add_req(key); @@ -584,7 +591,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) werase(statusbar->topline); - if (tox_isconnected(m)) { + if (statusbar->is_online) { int colour = WHITE; char *status_text = "Unknown"; From dd6a558a6257b556132a4190711ac34dddadc267 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 6 Sep 2013 21:39:22 -0400 Subject: [PATCH 67/82] forgot to init --- src/prompt.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/prompt.c b/src/prompt.c index 40165d2..e246b20 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -655,6 +655,7 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) /* Init statusbar info */ StatusBar *statusbar = (StatusBar *) self->s; statusbar->status = TOX_USERSTATUS_NONE; + statusbar->is_online = false; statusbar->max_len = x; uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; From 08d87cc996fe709fa02df8802399255348aef653 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 7 Sep 2013 00:48:21 -0400 Subject: [PATCH 68/82] fix potential segfaul and added default friend request msg --- src/prompt.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/prompt.c b/src/prompt.c index e246b20..c914df2 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -154,7 +154,7 @@ void cmd_accept(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv) { - if (argc != 1 && argc != 2) { + if (argc < 1 || argc > 2) { wprintw(self->window, "Invalid syntax.\n"); return; } @@ -162,22 +162,33 @@ void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv) uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE]; char xx[3]; uint32_t x; - char *id; - char *msg; + uint8_t *msg; int i, num; - id = argv[1]; + char *id = argv[1]; + + if (id == NULL) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } if (argc == 2) { - if (argv[2][0] != '\"') { + msg = argv[2]; + + if (msg == NULL) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + + if (msg[0] != '\"') { wprintw(self->window, "Messages must be enclosed in quotes.\n"); return; } - msg = argv[2]; msg[strlen(++msg)-1] = L'\0'; + } else - msg = ""; + msg = "Let's tox."; if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) { wprintw(self->window, "Invalid ID length.\n"); @@ -201,7 +212,7 @@ void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv) id[i] = toupper(id[i]); } - num = tox_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1); + num = tox_addfriend(m, id_bin, msg, strlen(msg) + 1); switch (num) { case TOX_FAERR_TOOLONG: From 9798dd6b95fffc4a03e3bcf1d21599099aa6e2db Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 8 Sep 2013 03:18:34 -0400 Subject: [PATCH 69/82] code cleanup/bug fixes --- src/chat.c | 27 +++++++---------- src/friendlist.c | 2 +- src/prompt.c | 75 ++++++++++++++++++++++++------------------------ src/windows.c | 2 +- 4 files changed, 50 insertions(+), 56 deletions(-) diff --git a/src/chat.c b/src/chat.c index d6a9c65..4f941e3 100644 --- a/src/chat.c +++ b/src/chat.c @@ -121,9 +121,7 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status return; StatusBar *statusbar = (StatusBar *) self->s; - - if (strncmp(status, "Online", strlen(status))) /* Ignore default "Online" message */ - snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", status); + snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", status); } /* check that the string has one non-space character */ @@ -298,7 +296,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) wattroff(ctx->history, COLOR_PAIR(CYAN)); uint8_t selfname[TOX_MAX_NAME_LENGTH]; - tox_getselfname(m, selfname, sizeof(selfname)); + tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH); wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "* %s %s\n", selfname, action); @@ -313,7 +311,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) else if (!strncmp(cmd, "/status ", strlen("/status "))) { char *status = strchr(cmd, ' '); - uint8_t *msg; if (status == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); @@ -355,7 +352,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) tox_set_userstatus(m, status_kind); prompt_update_status(self->prompt, status_kind); - msg = strchr(status, ' '); + uint8_t *msg = strchr(status, ' '); if (msg != NULL) { msg++; tox_set_statusmessage(m, msg, strlen(msg) + 1); @@ -373,8 +370,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) } else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { - uint8_t *nick; - nick = strchr(cmd, ' '); + uint8_t *nick = strchr(cmd, ' '); if (nick == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); @@ -454,11 +450,9 @@ static void chat_onDraw(ToxWindow *self, Tox *m) wprintw(statusbar->topline, "[Offline]"); } - if (statusbar->statusmsg[0]) { - wattron(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, " | %s", statusbar->statusmsg); - wattroff(statusbar->topline, A_BOLD); - } + wattron(statusbar->topline, A_BOLD); + wprintw(statusbar->topline, " | %s", statusbar->statusmsg); + wattroff(statusbar->topline, A_BOLD); wprintw(statusbar->topline, "\n"); @@ -475,13 +469,12 @@ static void chat_onInit(ToxWindow *self, Tox *m) /* Init statusbar info */ StatusBar *statusbar = (StatusBar *) self->s; statusbar->status = tox_get_userstatus(m, self->friendnum); - statusbar->is_online = tox_friendstatus(m, self->friendnum) == TOX_FRIEND_ONLINE; + statusbar->is_online = tox_get_friend_connectionstatus(m, self->friendnum) == 1; statusbar->max_len = x; - char statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'}; + uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'}; tox_copy_statusmessage(m, self->friendnum, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); - if (strncmp(statusmsg, "Online", strlen(statusmsg))) - snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); + snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); /* Init subwindows */ ChatContext *ctx = (ChatContext *) self->x; diff --git a/src/friendlist.c b/src/friendlist.c index ec2b81e..e0bb309 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -198,7 +198,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wprintw(self->window, " > "); else wprintw(self->window, " "); - + if (friends[i].online) { TOX_USERSTATUS status = friends[i].status; int colour = WHITE; diff --git a/src/prompt.c b/src/prompt.c index c914df2..d82c188 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -19,7 +19,7 @@ extern int store_data(Tox *m, char *path); uint8_t pending_requests[MAX_STR_SIZE][TOX_CLIENT_ID_SIZE]; // XXX uint8_t num_requests = 0; // XXX -static char prompt_buf[MAX_STR_SIZE] = {0}; +static char prompt_buf[MAX_STR_SIZE] = {'\0'}; static int prompt_buf_pos = 0; /* commands */ @@ -258,18 +258,21 @@ void cmd_clear(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv) { - tox_IP_Port dht; - char *ip, *port, *key; - /* check arguments */ if (argc != 3) { wprintw(self->window, "Invalid syntax.\n"); return; } - ip = argv[1]; - port = argv[2]; - key = argv[3]; + tox_IP_Port dht; + char *ip = argv[1]; + char *port = argv[2]; + char *key = argv[3]; + + if (!ip || !port || !key) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } if (atoi(port) == 0) { wprintw(self->window, "Invalid syntax.\n"); @@ -325,16 +328,14 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) { - char *id, *msg; - /* check arguments */ if (argc != 2) { wprintw(self->window, "Invalid syntax.\n"); return; } - id = argv[1]; - msg = argv[2]; + char *id = argv[1]; + uint8_t *msg = argv[2]; if (id == NULL || msg == NULL) { wprintw(self->window, "Invalid syntax.\n"); @@ -343,7 +344,7 @@ void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) msg[strlen(++msg)-1] = L'\0'; - if (tox_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0) + if (tox_sendmessage(m, atoi(id), msg, strlen(msg) + 1) == 0) wprintw(self->window, "Failed to send message.\n"); else wprintw(self->window, "Message successfully sent.\n"); @@ -352,10 +353,11 @@ void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_myid(ToxWindow *self, Tox *m, int argc, char **argv) { char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; - size_t i; uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; tox_getaddress(m, address); + size_t i; + for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) { char xx[3]; snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); @@ -367,15 +369,13 @@ void cmd_myid(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv) { - uint8_t *nick; - /* check arguments */ if (argc != 1) { wprintw(self->window, "Invalid syntax.\n"); return; } - nick = argv[1]; + uint8_t *nick = argv[1]; if (nick == NULL) { wprintw(self->window, "Invalid syntax.\n"); @@ -393,14 +393,13 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) { - char *status, *status_text; - uint8_t *msg = NULL; - if (argc < 1 || argc > 2) { wprintw(self->window, "Wrong number of arguments.\n"); return; } + uint8_t *msg = NULL; + if (argc == 2) { msg = argv[2]; @@ -416,7 +415,7 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) } } - status = argv[1]; + char *status = argv[1]; if (status == NULL) { wprintw(self->window, "Invalid syntax.\n"); @@ -454,18 +453,18 @@ void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv) return; } - if (argv[1] == NULL) { + uint8_t *msg = argv[1]; + + if (msg == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } - if (argv[1][0] != '\"') { + if (msg[0] != '\"') { wprintw(self->window, "Messages must be enclosed in quotes.\n"); return; } - uint8_t *msg; - msg = argv[1]; msg[strlen(++msg)-1] = L'\0'; tox_set_statusmessage(m, msg, strlen(msg) + 1); @@ -475,7 +474,7 @@ void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv) static void execute(ToxWindow *self, Tox *m, char *u_cmd) { int newlines = 0; - char cmd[MAX_STR_SIZE] = {0}; + char cmd[MAX_STR_SIZE] = {'\0'}; int i; for (i = 0; i < strlen(prompt_buf); ++i) { @@ -511,7 +510,7 @@ static void execute(ToxWindow *self, Tox *m, char *u_cmd) else if (cmd[i] == '\"') { while (cmd[++i] != '\"') { if (cmd[i] == '\0') { - wprintw(self->window, "Invalid command: did you forget a closing \"?\n"); + wprintw(self->window, "Invalid command: did you forget an opening or closing \"?\n"); return; } } @@ -536,18 +535,22 @@ static void execute(ToxWindow *self, Tox *m, char *u_cmd) } /* no input */ - if (!cmdargs[0]) + if (!cmdargs[0]) { + free(cmdargs); return; + } /* match input to command list */ for (i = 0; i < NUM_COMMANDS; i++) { if (!strcmp(cmdargs[0], commands[i].name)) { (commands[i].func)(self, m, numargs, cmdargs); + free(cmdargs); return; } } /* no match */ + free(cmdargs); wprintw(self->window, "Invalid command.\n"); } @@ -634,11 +637,9 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) wprintw(statusbar->topline, "[Offline]"); } - if (statusbar->statusmsg[0]) { - wattron(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, " | %s", statusbar->statusmsg); - wattroff(statusbar->topline, A_BOLD); - } + wattron(statusbar->topline, A_BOLD); + wprintw(statusbar->topline, " | %s", statusbar->statusmsg); + wattroff(statusbar->topline, A_BOLD); wprintw(statusbar->topline, "\n"); @@ -670,13 +671,13 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) statusbar->max_len = x; uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; - tox_getselfname(m, (uint8_t *) &nick, TOX_MAX_NAME_LENGTH); + tox_getselfname(m, nick, TOX_MAX_NAME_LENGTH); snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick); - uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; - tox_copy_self_statusmessage(m, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); - if (strncmp(statusmsg, "Online", strlen(statusmsg))) - snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); + /* temporary until statusmessage saving works */ + uint8_t *statusmsg = "Toxing on Toxic v0.2.0"; + // tox_copy_self_statusmessage(m, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); + snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); /* Init statusbar subwindow */ statusbar->topline = subwin(self->window, 2, x, 0, 0); diff --git a/src/windows.c b/src/windows.c index f298b71..6ccc70a 100644 --- a/src/windows.c +++ b/src/windows.c @@ -32,7 +32,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata) { - uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; + uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_getname(m, friendnumber, (uint8_t *) &nick); if (!nick[0]) From fde8059a4cf489d4576f9720f3c6328bfaaa632f Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 9 Sep 2013 00:56:47 -0400 Subject: [PATCH 70/82] tweaks and fixes --- src/chat.c | 3 ++- src/friendlist.c | 16 ++++------------ src/main.c | 33 +++++++++++++++++++++------------ src/prompt.c | 13 +++++++------ src/toxic_windows.h | 2 +- src/windows.c | 5 ++++- 6 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/chat.c b/src/chat.c index 4f941e3..f841f45 100644 --- a/src/chat.c +++ b/src/chat.c @@ -98,7 +98,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin beep(); } -static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) +static void chat_onNickChange(ToxWindow *self, Tox *m, int num, uint8_t *nick, uint16_t len) { if (self->friendnum != num) return; @@ -276,6 +276,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { endwin(); store_data(m, DATA_FILE); + free(DATA_FILE); tox_kill(m); exit(0); } diff --git a/src/friendlist.c b/src/friendlist.c index e0bb309..6926626 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -56,13 +56,12 @@ void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t sta friends[num].online = false; } -void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) +void friendlist_onNickChange(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) { if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= num_friends) return; memcpy((char *) &friends[num].name, (char *) str, len); - friends[num].name[len] = 0; } void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) @@ -78,9 +77,7 @@ void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, ui if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends) return; - /* Ignore default "Online" status message */ - if (strncmp(str, "Online", strlen(str))) - memcpy((char *) &friends[num].statusmsg, (char *) str, len); + memcpy((char *) &friends[num].statusmsg, (char *) str, len); } int friendlist_onFriendAdded(Tox *m, int num) @@ -219,14 +216,9 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wattron(self->window, COLOR_PAIR(colour) | A_BOLD); wprintw(self->window, "O"); wattroff(self->window, COLOR_PAIR(colour) | A_BOLD); - wprintw(self->window, "]%s", friends[i].name); - - if (friends[i].statusmsg[0]) - wprintw(self->window, " (%s)\n", friends[i].statusmsg); - else - wprintw(self->window, "\n"); + wprintw(self->window, "]%s (%s)\n", friends[i].name, friends[i].statusmsg); } else { - wprintw(self->window, "[O]%s\n", friends[i].name); + wprintw(self->window, "[O]%s\n", friends[i].name, friends[i].statusmsg); } } } diff --git a/src/main.c b/src/main.c index 68808a7..3f0a2a8 100644 --- a/src/main.c +++ b/src/main.c @@ -41,6 +41,7 @@ #ifndef PACKAGE_DATADIR #define PACKAGE_DATADIR "." #endif + /* Export for use in Callbacks */ char *DATA_FILE = NULL; char *SRVLIST_FILE = NULL; @@ -254,15 +255,19 @@ int f_loadfromfile; /* * Store Messenger to given location * Return 0 stored successfully - * Return 1 malloc failed - * Return 2 opening path failed - * Return 3 fwrite failed + * Return 1 file path is NULL + * Return 2 malloc failed + * Return 3 opening path failed + * Return 4 fwrite failed */ int store_data(Tox *m, char *path) { if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ return 0; + if (path == NULL) + return 1; + FILE *fd; size_t len; uint8_t *buf; @@ -271,7 +276,7 @@ int store_data(Tox *m, char *path) buf = malloc(len); if (buf == NULL) { - return 1; + return 2; } tox_save(m, buf); @@ -280,13 +285,13 @@ int store_data(Tox *m, char *path) if (fd == NULL) { free(buf); - return 2; + return 3; } if (fwrite(buf, len, 1, fd) != 1) { free(buf); fclose(fd); - return 3; + return 4; } free(buf); @@ -380,14 +385,18 @@ int main(int argc, char *argv[]) SRVLIST_FILE = strdup(PACKAGE_DATADIR "/DHTservers"); } 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"); + if (DATA_FILE != NULL) { + strcpy(DATA_FILE, user_config_dir); + strcat(DATA_FILE, CONFIGDIR); + strcat(DATA_FILE, "data"); + } SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1); - strcpy(SRVLIST_FILE, user_config_dir); - strcat(SRVLIST_FILE, CONFIGDIR); - strcat(SRVLIST_FILE, "DHTservers"); + if (SRVLIST_FILE != NULL) { + strcpy(SRVLIST_FILE, user_config_dir); + strcat(SRVLIST_FILE, CONFIGDIR); + strcat(SRVLIST_FILE, "DHTservers"); + } } } diff --git a/src/prompt.c b/src/prompt.c index d82c188..c7cfeec 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -77,7 +77,7 @@ void prompt_update_status(ToxWindow *prompt, TOX_USERSTATUS status) statusbar->status = status; } -/* Updates own connection status */ +/* Updates own connection status in prompt statusbar */ void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected) { StatusBar *statusbar = (StatusBar *) prompt->s; @@ -296,6 +296,7 @@ void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv) { endwin(); store_data(m, DATA_FILE); + free(DATA_FILE); tox_kill(m); exit(0); } @@ -520,8 +521,8 @@ static void execute(ToxWindow *self, Tox *m, char *u_cmd) /* read arguments into array */ char **cmdargs = malloc((numargs + 1) * sizeof(char *)); if (!cmdargs) { - wprintw(self->window, "Invalid command: too many arguments.\n"); - return; + wprintw(self->window, "Invalid command: too many arguments.\n"); + return; } int pos = 0; @@ -625,7 +626,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) } wattron(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, "%s ", statusbar->nick); + wprintw(statusbar->topline, " %s ", statusbar->nick); wattron(statusbar->topline, A_BOLD); wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); wprintw(statusbar->topline, "[%s]", status_text); @@ -638,7 +639,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) } wattron(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, " | %s", statusbar->statusmsg); + wprintw(statusbar->topline, " | %s |", statusbar->statusmsg); wattroff(statusbar->topline, A_BOLD); wprintw(statusbar->topline, "\n"); @@ -676,7 +677,7 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) /* temporary until statusmessage saving works */ uint8_t *statusmsg = "Toxing on Toxic v0.2.0"; - // tox_copy_self_statusmessage(m, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); + m_set_statusmessage(m, statusmsg, strlen(statusmsg) + 1); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); /* Init statusbar subwindow */ diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 7df1f1c..269ce2b 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -43,7 +43,7 @@ struct ToxWindow_ { void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); void(*onConnectionChange)(ToxWindow *, Tox *, int, uint8_t); void(*onMessage)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); - void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t); + void(*onNickChange)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onStatusChange)(ToxWindow *, Tox *, int, TOX_USERSTATUS); void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); diff --git a/src/windows.c b/src/windows.c index 6ccc70a..bd490c9 100644 --- a/src/windows.c +++ b/src/windows.c @@ -88,8 +88,11 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i].onNickChange != NULL) - windows[i].onNickChange(&windows[i], friendnumber, string, length); + windows[i].onNickChange(&windows[i], m, friendnumber, string, length); } + + if (store_data(m, DATA_FILE)) + wprintw(prompt->window, "\nCould not store Tox data\n"); } void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) From 1b5bcb4ffa14be0898b9ed077f8f1862a57a41f3 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 9 Sep 2013 01:08:06 -0400 Subject: [PATCH 71/82] tweaks and fixes --- src/chat.c | 2 +- src/friendlist.c | 4 ++-- src/toxic_windows.h | 2 +- src/windows.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/chat.c b/src/chat.c index f841f45..4fa0cf7 100644 --- a/src/chat.c +++ b/src/chat.c @@ -98,7 +98,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin beep(); } -static void chat_onNickChange(ToxWindow *self, Tox *m, int num, uint8_t *nick, uint16_t len) +static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) { if (self->friendnum != num) return; diff --git a/src/friendlist.c b/src/friendlist.c index 6926626..c8a9169 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -56,7 +56,7 @@ void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t sta friends[num].online = false; } -void friendlist_onNickChange(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) +void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= num_friends) return; @@ -218,7 +218,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wattroff(self->window, COLOR_PAIR(colour) | A_BOLD); wprintw(self->window, "]%s (%s)\n", friends[i].name, friends[i].statusmsg); } else { - wprintw(self->window, "[O]%s\n", friends[i].name, friends[i].statusmsg); + wprintw(self->window, "[O]%s\n", friends[i].name); } } } diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 269ce2b..7df1f1c 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -43,7 +43,7 @@ struct ToxWindow_ { void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); void(*onConnectionChange)(ToxWindow *, Tox *, int, uint8_t); void(*onMessage)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); - void(*onNickChange)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); + void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onStatusChange)(ToxWindow *, Tox *, int, TOX_USERSTATUS); void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); diff --git a/src/windows.c b/src/windows.c index bd490c9..17980ca 100644 --- a/src/windows.c +++ b/src/windows.c @@ -88,7 +88,7 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i].onNickChange != NULL) - windows[i].onNickChange(&windows[i], m, friendnumber, string, length); + windows[i].onNickChange(&windows[i], friendnumber, string, length); } if (store_data(m, DATA_FILE)) From 4fc063d5fe07d18c89f8a91b6810c27fc7936e19 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 9 Sep 2013 06:54:36 -0400 Subject: [PATCH 72/82] api changed return on function --- src/friendlist.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index c8a9169..545ac09 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -95,8 +95,8 @@ int friendlist_onFriendAdded(Tox *m, int num) friends[i].online = false; friends[i].status = TOX_USERSTATUS_NONE; - if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0') - strcpy((char *) friends[i].name, "unknown"); + if (tox_getname(m, num, friends[i].name) == -1 || friends[i].name[0] == '\0') + strcpy((char *) friends[i].name, UNKNOWN_NAME); if (i == num_friends) ++num_friends; From f50f93ee81ba7c2120ad7dc69cf08e0a7711780c Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 9 Sep 2013 15:33:15 -0400 Subject: [PATCH 73/82] move define --- src/main.c | 7 +++---- src/toxic_windows.h | 2 ++ src/windows.c | 2 -- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 3f0a2a8..9018257 100644 --- a/src/main.c +++ b/src/main.c @@ -275,9 +275,8 @@ int store_data(Tox *m, char *path) len = tox_size(m); buf = malloc(len); - if (buf == NULL) { + if (buf == NULL) return 2; - } tox_save(m, buf); @@ -334,8 +333,8 @@ static void load_data(Tox *m, char *path) uint32_t i = 0; - char name[TOX_MAX_NAME_LENGTH]; - while (tox_getname(m, i, (uint8_t *)name) != -1) { + uint8_t name[TOX_MAX_NAME_LENGTH]; + while (tox_getname(m, i, name) != -1) { on_friendadded(m, i); i++; } diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 7df1f1c..a1c1269 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -20,6 +20,8 @@ /* number of permanent default windows */ #define N_DEFAULT_WINS 3 +#define UNKNOWN_NAME "Unknown" + #ifndef TOXICVER #define TOXICVER "NOVER" //Use the -D flag to set this #endif diff --git a/src/windows.c b/src/windows.c index 17980ca..f24e892 100644 --- a/src/windows.c +++ b/src/windows.c @@ -17,8 +17,6 @@ static ToxWindow *active_window; static ToxWindow *prompt; static Tox *m; -#define UNKNOWN_NAME "Unknown" - /* CALLBACKS START */ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { From 9e8fa921093ad083667cfda624812d9bcbb6dfbf Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 10 Sep 2013 04:04:13 -0400 Subject: [PATCH 74/82] truncate friends' notes if they're too long --- src/chat.c | 359 +++++++++++++++++++++++--------------------- src/friendlist.c | 16 +- src/prompt.c | 1 - src/toxic_windows.h | 2 +- 4 files changed, 200 insertions(+), 178 deletions(-) diff --git a/src/chat.c b/src/chat.c index 4fa0cf7..9dc6dc2 100644 --- a/src/chat.c +++ b/src/chat.c @@ -30,9 +30,6 @@ typedef struct { WINDOW *linewin; } ChatContext; -void print_help(ChatContext *self); -void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd); - struct tm *get_time(void) { struct tm *timeinfo; @@ -121,7 +118,8 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status return; StatusBar *statusbar = (StatusBar *) self->s; - snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", status); + statusbar->statusmsg_len = len; + snprintf(statusbar->statusmsg, len, "%s", status); } /* check that the string has one non-space character */ @@ -170,6 +168,173 @@ static char *wc_to_char(wchar_t ch) return ret; } +static void print_help(ChatContext *self) +{ + wattron(self->history, COLOR_PAIR(CYAN) | A_BOLD); + wprintw(self->history, "Commands:\n"); + wattroff(self->history, A_BOLD); + + wprintw(self->history, " /status : Set your status with optional note\n"); + wprintw(self->history, " /note : Set a personal note\n"); + wprintw(self->history, " /nick : Set your nickname\n"); + wprintw(self->history, " /me : Do an action\n"); + wprintw(self->history, " /myid : Print your ID\n"); + wprintw(self->history, " /clear : Clear the screen\n"); + wprintw(self->history, " /close : Close the current chat window\n"); + wprintw(self->history, " /quit or /exit : Exit Toxic\n"); + wprintw(self->history, " /help : Print this message again\n\n"); + + wattroff(self->history, COLOR_PAIR(CYAN)); +} + +static void execute(ToxWindow *self, ChatContext *ctx, StatusBar *statusbar, Tox *m, char *cmd) +{ + if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { + wclear(self->window); + wclear(ctx->history); + wprintw(ctx->history, "\n\n"); + 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(); + store_data(m, DATA_FILE); + free(DATA_FILE); + tox_kill(m); + exit(0); + } + + else if (!strncmp(cmd, "/me ", strlen("/me "))) { + struct tm *timeinfo = get_time(); + char *action = strchr(cmd, ' '); + + if (action == NULL) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + + action++; + + wattron(ctx->history, COLOR_PAIR(CYAN)); + wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattroff(ctx->history, COLOR_PAIR(CYAN)); + + uint8_t selfname[TOX_MAX_NAME_LENGTH]; + tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH); + + wattron(ctx->history, COLOR_PAIR(YELLOW)); + wprintw(ctx->history, "* %s %s\n", selfname, action); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); + + if (!statusbar->is_online + || tox_sendaction(m, self->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { + wattron(ctx->history, COLOR_PAIR(RED)); + wprintw(ctx->history, " * Failed to send action\n"); + wattroff(ctx->history, COLOR_PAIR(RED)); + } + } + + else if (!strncmp(cmd, "/status ", strlen("/status "))) { + char *status = strchr(cmd, ' '); + + if (status == NULL) { + wprintw(ctx->history, "Invalid syntax.\n"); + return; + } + + status++; + TOX_USERSTATUS status_kind; + + if (!strncmp(status, "online", strlen("online"))) { + status_kind = TOX_USERSTATUS_NONE; + wprintw(ctx->history, "Status set to: "); + wattron(ctx->history, COLOR_PAIR(GREEN) | A_BOLD); + wprintw(ctx->history, "[Online]\n"); + wattroff(ctx->history, COLOR_PAIR(GREEN) | A_BOLD); + } + + else if (!strncmp(status, "away", strlen("away"))) { + status_kind = TOX_USERSTATUS_AWAY; + wprintw(ctx->history, "Status set to: "); + wattron(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD); + wprintw(ctx->history, "[Away]\n"); + wattroff(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD); + } + + else if (!strncmp(status, "busy", strlen("busy"))) { + status_kind = TOX_USERSTATUS_BUSY; + wprintw(ctx->history, "Status set to: "); + wattron(ctx->history, COLOR_PAIR(RED) | A_BOLD); + wprintw(ctx->history, "[Busy]\n"); + wattroff(ctx->history, COLOR_PAIR(RED) | A_BOLD); + } + + else { + wprintw(ctx->history, "Invalid status.\n"); + return; + } + + tox_set_userstatus(m, status_kind); + prompt_update_status(self->prompt, status_kind); + + uint8_t *msg = strchr(status, ' '); + if (msg != NULL) { + msg++; + uint16_t len = strlen(msg) + 1; + tox_set_statusmessage(m, msg, len); + prompt_update_statusmessage(self->prompt, msg, len); + wprintw(ctx->history, "Personal note set to: %s\n", msg); + } + } + + else if (!strncmp(cmd, "/note ", strlen("/note "))) { + uint8_t *msg = strchr(cmd, ' '); + msg++; + uint16_t len = strlen(msg) + 1; + tox_set_statusmessage(m, msg, len); + prompt_update_statusmessage(self->prompt, msg, len); + wprintw(ctx->history, "Personal note set to: %s\n", msg); + } + + else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { + uint8_t *nick = strchr(cmd, ' '); + + if (nick == NULL) { + wprintw(ctx->history, "Invalid syntax.\n"); + return; + } + + nick++; + tox_setname(m, nick, strlen(nick) + 1); + prompt_update_nick(self->prompt, nick); + wprintw(ctx->history, "Nickname set to: %s\n", nick); + } + + else if (!strcmp(cmd, "/myid")) { + char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {'\0'}; + int i; + uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; + tox_getaddress(m, address); + + for (i = 0; i < TOX_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 + wprintw(ctx->history, "Invalid command.\n"); +} + static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) { ChatContext *ctx = (ChatContext *) self->x; @@ -221,9 +386,8 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) delwin(statusbar->topline); del_window(self); disable_chatwin(f_num); - } else { - execute(self, ctx, m, line); - } + } else + execute(self, ctx, statusbar, m, line); } else { /* make sure the string has at least non-space character */ if (!string_is_empty(line)) { @@ -238,7 +402,8 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wattroff(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "%s\n", line); - if (tox_sendmessage(m, self->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { + if (!statusbar->is_online + || tox_sendmessage(m, self->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(RED)); @@ -258,151 +423,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) } } -void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) -{ - if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { - wclear(self->window); - wclear(ctx->history); - wprintw(ctx->history, "\n\n"); - 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(); - store_data(m, DATA_FILE); - free(DATA_FILE); - tox_kill(m); - exit(0); - } - - else if (!strncmp(cmd, "/me ", strlen("/me "))) { - struct tm *timeinfo = get_time(); - char *action = strchr(cmd, ' '); - - if (action == NULL) { - wprintw(self->window, "Invalid syntax.\n"); - return; - } - - action++; - - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); - - uint8_t selfname[TOX_MAX_NAME_LENGTH]; - tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH); - - wattron(ctx->history, COLOR_PAIR(YELLOW)); - wprintw(ctx->history, "* %s %s\n", selfname, action); - wattroff(ctx->history, COLOR_PAIR(YELLOW)); - - if (tox_sendaction(m, self->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, " * Failed to send action\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); - } - } - - else if (!strncmp(cmd, "/status ", strlen("/status "))) { - char *status = strchr(cmd, ' '); - - if (status == NULL) { - wprintw(ctx->history, "Invalid syntax.\n"); - return; - } - - status++; - TOX_USERSTATUS status_kind; - - if (!strncmp(status, "online", strlen("online"))) { - status_kind = TOX_USERSTATUS_NONE; - wprintw(ctx->history, "Status set to: "); - wattron(ctx->history, COLOR_PAIR(GREEN) | A_BOLD); - wprintw(ctx->history, "[Online]\n"); - wattroff(ctx->history, COLOR_PAIR(GREEN) | A_BOLD); - } - - else if (!strncmp(status, "away", strlen("away"))) { - status_kind = TOX_USERSTATUS_AWAY; - wprintw(ctx->history, "Status set to: "); - wattron(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD); - wprintw(ctx->history, "[Away]\n"); - wattroff(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD); - } - - else if (!strncmp(status, "busy", strlen("busy"))) { - status_kind = TOX_USERSTATUS_BUSY; - wprintw(ctx->history, "Status set to: "); - wattron(ctx->history, COLOR_PAIR(RED) | A_BOLD); - wprintw(ctx->history, "[Busy]\n"); - wattroff(ctx->history, COLOR_PAIR(RED) | A_BOLD); - } - - else { - wprintw(ctx->history, "Invalid status.\n"); - return; - } - - tox_set_userstatus(m, status_kind); - prompt_update_status(self->prompt, status_kind); - - uint8_t *msg = strchr(status, ' '); - if (msg != NULL) { - msg++; - tox_set_statusmessage(m, msg, strlen(msg) + 1); - prompt_update_statusmessage(self->prompt, msg); - wprintw(ctx->history, "Personal note set to: %s\n", msg); - } - } - - else if (!strncmp(cmd, "/note ", strlen("/note "))) { - uint8_t *msg = strchr(cmd, ' '); - msg++; - tox_set_statusmessage(m, msg, strlen(msg) + 1); - prompt_update_statusmessage(self->prompt, msg); - wprintw(ctx->history, "Personal note set to: %s\n", msg); - } - - else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { - uint8_t *nick = strchr(cmd, ' '); - - if (nick == NULL) { - wprintw(ctx->history, "Invalid syntax.\n"); - return; - } - - nick++; - tox_setname(m, nick, strlen(nick) + 1); - prompt_update_nick(self->prompt, nick); - wprintw(ctx->history, "Nickname set to: %s\n", nick); - } - - else if (!strcmp(cmd, "/myid")) { - char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {'\0'}; - int i; - uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; - tox_getaddress(m, address); - - for (i = 0; i < TOX_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 - wprintw(ctx->history, "Invalid command.\n"); -} - static void chat_onDraw(ToxWindow *self, Tox *m) { curs_set(1); @@ -410,6 +430,8 @@ static void chat_onDraw(ToxWindow *self, Tox *m) int x, y; getmaxyx(self->window, y, x); + ChatContext *ctx = (ChatContext *) self->x; + /* Draw status bar */ StatusBar *statusbar = (StatusBar *) self->s; mvwhline(statusbar->topline, 1, 0, '-', x); @@ -451,13 +473,19 @@ static void chat_onDraw(ToxWindow *self, Tox *m) wprintw(statusbar->topline, "[Offline]"); } + /* Truncate note if it doesn't fit in statusbar */ + uint16_t maxlen = x - getcurx(statusbar->topline) - 5; + if (statusbar->statusmsg_len > maxlen) { + statusbar->statusmsg[maxlen] = '\0'; + statusbar->statusmsg_len = maxlen; + } + wattron(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, " | %s", statusbar->statusmsg); + wprintw(statusbar->topline, " | %s |", statusbar->statusmsg); wattroff(statusbar->topline, A_BOLD); wprintw(statusbar->topline, "\n"); - ChatContext *ctx = (ChatContext *) self->x; mvwhline(ctx->linewin, 0, 0, '_', x); wrefresh(self->window); } @@ -471,11 +499,11 @@ static void chat_onInit(ToxWindow *self, Tox *m) StatusBar *statusbar = (StatusBar *) self->s; statusbar->status = tox_get_userstatus(m, self->friendnum); statusbar->is_online = tox_get_friend_connectionstatus(m, self->friendnum) == 1; - statusbar->max_len = x; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'}; tox_copy_statusmessage(m, self->friendnum, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); + statusbar->statusmsg_len = tox_get_statusmessage_size(m, self->friendnum); /* Init subwindows */ ChatContext *ctx = (ChatContext *) self->x; @@ -488,25 +516,6 @@ static void chat_onInit(ToxWindow *self, Tox *m) wmove(self->window, y - CURS_Y_OFFSET, 0); } -void print_help(ChatContext *self) -{ - wattron(self->history, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(self->history, "Commands:\n"); - wattroff(self->history, A_BOLD); - - wprintw(self->history, " /status : Set your status with optional note\n"); - wprintw(self->history, " /note : Set a personal note\n"); - wprintw(self->history, " /nick : Set your nickname\n"); - wprintw(self->history, " /me : Do an action\n"); - wprintw(self->history, " /myid : Print your ID\n"); - wprintw(self->history, " /clear : Clear the screen\n"); - wprintw(self->history, " /close : Close the current chat window\n"); - wprintw(self->history, " /quit or /exit : Exit Toxic\n"); - wprintw(self->history, " /help : Print this message again\n\n"); - - wattroff(self->history, COLOR_PAIR(CYAN)); -} - ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum) { ToxWindow ret; diff --git a/src/friendlist.c b/src/friendlist.c index 545ac09..a68823f 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -24,6 +24,7 @@ extern ToxWindow *prompt; typedef struct { uint8_t name[TOX_MAX_NAME_LENGTH]; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; + uint16_t statusmsg_len; int num; int chatwin; bool active; @@ -77,6 +78,7 @@ void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, ui if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends) return; + friends[num].statusmsg_len = len; memcpy((char *) &friends[num].statusmsg, (char *) str, len); } @@ -216,7 +218,19 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wattron(self->window, COLOR_PAIR(colour) | A_BOLD); wprintw(self->window, "O"); wattroff(self->window, COLOR_PAIR(colour) | A_BOLD); - wprintw(self->window, "]%s (%s)\n", friends[i].name, friends[i].statusmsg); + wprintw(self->window, "]%s (", friends[i].name); + + /* Truncate note if it doesn't fit on one line */ + int x, y; + getmaxyx(self->window, y, x); + uint16_t maxlen = x - getcurx(self->window) - 2; + + if (friends[i].statusmsg_len > maxlen) { + friends[i].statusmsg[maxlen] = '\0'; + friends[i].statusmsg_len = maxlen; + } + + wprintw(self->window, "%s)\n", friends[i].statusmsg); } else { wprintw(self->window, "[O]%s\n", friends[i].name); } diff --git a/src/prompt.c b/src/prompt.c index c7cfeec..bd26010 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -669,7 +669,6 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) StatusBar *statusbar = (StatusBar *) self->s; statusbar->status = TOX_USERSTATUS_NONE; statusbar->is_online = false; - statusbar->max_len = x; uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_getselfname(m, nick, TOX_MAX_NAME_LENGTH); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index a1c1269..12e83bf 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -65,10 +65,10 @@ struct ToxWindow_ { typedef struct { WINDOW *topline; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; + uint16_t statusmsg_len; uint8_t nick[TOX_MAX_NAME_LENGTH]; TOX_USERSTATUS status; bool is_online; - int max_len; /* set to the window's max x coordinate */ } StatusBar; void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata); From af537380a1f39638e2d34daf963f6c1d7679bd2c Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Tue, 10 Sep 2013 11:53:34 -0700 Subject: [PATCH 75/82] Removed file that may confuse people --- README | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 README diff --git a/README b/README deleted file mode 100644 index e69de29..0000000 From a8e73f1532484ede735df5ed079131629f52c599 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Tue, 10 Sep 2013 12:13:58 -0700 Subject: [PATCH 76/82] Needed for compile --- README | 1 + 1 file changed, 1 insertion(+) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/README @@ -0,0 +1 @@ + From c9c9592685abacf398d9e9f0bfa6e60b36c1c130 Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Wed, 11 Sep 2013 01:34:29 +0200 Subject: [PATCH 77/82] if tox_new() fails, don't crash and leave the terminal in a broken state --- src/main.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main.c b/src/main.c index 9018257..beef19b 100644 --- a/src/main.c +++ b/src/main.c @@ -91,6 +91,8 @@ static Tox *init_tox() { /* Init core */ Tox *m = tox_new(); + if (!m) + return NULL; /* Callbacks */ tox_callback_connectionstatus(m, on_connectionchange, NULL); @@ -403,6 +405,12 @@ int main(int argc, char *argv[]) init_term(); Tox *m = init_tox(); + if (!m) { + endwin(); + fprintf(stderr, "Failed to initialize network. Aborting...\n"); + exit(1); + } + prompt = init_windows(m); if (f_loadfromfile) From 40dcfc82d2b39b601dfa233c5f25ff02e8e3e86c Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 11 Sep 2013 00:02:27 -0400 Subject: [PATCH 78/82] made error handling more consistent and added exit function --- src/friendlist.c | 5 ++--- src/main.c | 21 +++++++++++---------- src/prompt.c | 4 +--- src/prompt.h | 2 -- src/toxic_windows.h | 4 +++- src/windows.c | 8 ++++---- 6 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index a68823f..b831959 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -17,8 +17,6 @@ #include "friendlist.h" extern char *DATA_FILE; -extern int store_data(Tox *m, char *path); - extern ToxWindow *prompt; typedef struct { @@ -136,9 +134,10 @@ static void select_friend(Tox *m, wint_t key) } else return; /* Bad key input */ /* If we reach this something is wrong */ + fprintf(stderr, "select_friend() failed. Aborting...\n"); endwin(); tox_kill(m); - exit(2); + exit(EXIT_FAILURE); } static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) diff --git a/src/main.c b/src/main.c index beef19b..11bc1c9 100644 --- a/src/main.c +++ b/src/main.c @@ -60,9 +60,9 @@ static void init_term() signal(SIGWINCH, on_window_resize); #if HAVE_WIDECHAR if (setlocale(LC_ALL, "") == NULL) { - printf("Could not set your locale, plese check your locale settings or" + fprintf(stderr, "Could not set your locale, plese check your locale settings or" "disable wide char support\n"); - exit(1); + exit(EXIT_FAILURE); } #endif initscr(); @@ -91,7 +91,7 @@ static Tox *init_tox() { /* Init core */ Tox *m = tox_new(); - if (!m) + if (m == NULL) return NULL; /* Callbacks */ @@ -184,7 +184,7 @@ int init_connection(Tox *m) fp = fopen(SRVLIST_FILE, "r"); - if (!fp) + if (fp == NULL) return 1; char servers[MAXSERVERS][MAXLINE]; @@ -208,7 +208,7 @@ int init_connection(Tox *m) char *port = strtok(NULL, " "); char *key = strtok(NULL, " "); - if (!ip || !port || !key) + if (ip == NULL || port == NULL || key == NULL) return 3; tox_IP_Port dht; @@ -320,7 +320,7 @@ static void load_data(Tox *m, char *path) fprintf(stderr, "malloc() failed.\n"); fclose(fd); endwin(); - exit(1); + exit(EXIT_FAILURE); } if (fread(buf, len, 1, fd) != 1) { @@ -328,7 +328,7 @@ static void load_data(Tox *m, char *path) free(buf); fclose(fd); endwin(); - exit(1); + exit(EXIT_FAILURE); } tox_load(m, buf, len); @@ -349,7 +349,7 @@ static void load_data(Tox *m, char *path) if ((st = store_data(m, path)) != 0) { fprintf(stderr, "Store messenger failed with return code: %d\n", st); endwin(); - exit(1); + exit(EXIT_FAILURE); } } } @@ -405,10 +405,11 @@ int main(int argc, char *argv[]) init_term(); Tox *m = init_tox(); - if (!m) { + + if (m == NULL) { endwin(); fprintf(stderr, "Failed to initialize network. Aborting...\n"); - exit(1); + exit(EXIT_FAILURE); } prompt = init_windows(m); diff --git a/src/prompt.c b/src/prompt.c index bd26010..3fa10d6 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -10,11 +10,9 @@ #include #include -#include "toxic_windows.h" #include "prompt.h" extern char *DATA_FILE; -extern int store_data(Tox *m, char *path); uint8_t pending_requests[MAX_STR_SIZE][TOX_CLIENT_ID_SIZE]; // XXX uint8_t num_requests = 0; // XXX @@ -298,7 +296,7 @@ void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv) store_data(m, DATA_FILE); free(DATA_FILE); tox_kill(m); - exit(0); + exit(EXIT_SUCCESS); } void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) diff --git a/src/prompt.h b/src/prompt.h index 9c68831..c4d51df 100644 --- a/src/prompt.h +++ b/src/prompt.h @@ -9,5 +9,3 @@ unsigned char *hex_string_to_bin(char hex_string[]); void prompt_init_statusbar(ToxWindow *self, Tox *m); #endif /* end of include guard: PROMPT_H_UZYGWFFL */ - - diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 12e83bf..41f6b14 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -21,6 +21,9 @@ #define N_DEFAULT_WINS 3 #define UNKNOWN_NAME "Unknown" + +#define EXIT_SUCCESS 0 +#define EXIT_FAILURE 1 #ifndef TOXICVER #define TOXICVER "NOVER" //Use the -D flag to set this @@ -85,4 +88,3 @@ int add_window(Tox *m, ToxWindow w); void del_window(ToxWindow *w); void set_active_window(int ch); #endif - diff --git a/src/windows.c b/src/windows.c index f24e892..e0583c4 100644 --- a/src/windows.c +++ b/src/windows.c @@ -10,7 +10,6 @@ #include "toxic_windows.h" extern char *DATA_FILE; -extern int store_data(Tox *m, char *path); static ToxWindow windows[MAX_WINDOWS_NUM]; static ToxWindow *active_window; @@ -177,7 +176,8 @@ void set_next_window(int ch) if (active_window == inf) { // infinite loop check endwin(); - exit(2); + fprintf(stderr, "set_next_window() failed. Aborting...\n"); + exit(EXIT_FAILURE); } } } @@ -195,9 +195,9 @@ ToxWindow *init_windows() int n_prompt = add_window(m, new_prompt()); if (n_prompt == -1 || add_window(m, new_friendlist()) == -1) { - fprintf(stderr, "add_window() failed.\n"); + fprintf(stderr, "add_window() failed. Aborting...\n"); endwin(); - exit(1); + exit(EXIT_FAILURE); } prompt = &windows[n_prompt]; From f004a4ba82be5c1dc39be289f9de04b0676fe7d7 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 11 Sep 2013 00:12:03 -0400 Subject: [PATCH 79/82] added exit_toxic function (for real this time) --- src/chat.c | 6 +----- src/main.c | 19 ++++++++++++++++--- src/prompt.c | 6 +----- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/chat.c b/src/chat.c index 9dc6dc2..ec6063f 100644 --- a/src/chat.c +++ b/src/chat.c @@ -203,11 +203,7 @@ static void execute(ToxWindow *self, ChatContext *ctx, StatusBar *statusbar, Tox print_help(ctx); else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { - endwin(); - store_data(m, DATA_FILE); - free(DATA_FILE); - tox_kill(m); - exit(0); + exit_toxic(m); } else if (!strncmp(cmd, "/me ", strlen("/me "))) { diff --git a/src/main.c b/src/main.c index 11bc1c9..16c3e7a 100644 --- a/src/main.c +++ b/src/main.c @@ -354,6 +354,21 @@ static void load_data(Tox *m, char *path) } } +void exit_toxic(Tox *m) +{ + store_data(m, DATA_FILE); + + if (DATA_FILE != NULL) + free(DATA_FILE); + + if (SRVLIST_FILE != NULL) + free(SRVLIST_FILE); + + tox_kill(m); + endwin(); + exit(EXIT_SUCCESS); +} + int main(int argc, char *argv[]) { char *user_config_dir = get_user_config_dir(); @@ -441,8 +456,6 @@ int main(int argc, char *argv[]) draw_active_window(m); } - tox_kill(m); - free(DATA_FILE); - free(SRVLIST_FILE); + exit_toxic(m); return 0; } diff --git a/src/prompt.c b/src/prompt.c index 3fa10d6..6b14045 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -292,11 +292,7 @@ void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv) { - endwin(); - store_data(m, DATA_FILE); - free(DATA_FILE); - tox_kill(m); - exit(EXIT_SUCCESS); + exit_toxic(m); } void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) From 052f9f9936b21fe51a7d7c18e96bdb75a1c6fb37 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 11 Sep 2013 18:07:26 -0400 Subject: [PATCH 80/82] more error handling --- src/chat.c | 28 +++++++++++++++++++++++----- src/main.c | 25 ++++++++++++------------- src/prompt.c | 16 +++++++++++++++- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/chat.c b/src/chat.c index ec6063f..78ae720 100644 --- a/src/chat.c +++ b/src/chat.c @@ -142,12 +142,22 @@ static char *wcs_to_char(wchar_t *string) if (len != (size_t) -1) { len++; ret = malloc(len); - wcstombs(ret, string, len); + if (ret != NULL) + wcstombs(ret, string, len); } else { ret = malloc(2); - ret[0] = ' '; - ret[1] = '\0'; + if (ret != NULL) { + ret[0] = ' '; + ret[1] = '\0'; + } } + + if (ret == NULL) { + fprintf(stderr, "malloc() failed. Aborting...\n"); + endwin(); + exit(EXIT_FAILURE); + } + return ret; } @@ -533,8 +543,16 @@ ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum) ChatContext *x = calloc(1, sizeof(ChatContext)); StatusBar *s = calloc(1, sizeof(StatusBar)); - ret.x = x; - ret.s = s; + + + if (s != NULL && x != NULL) { + ret.x = x; + ret.s = s; + } else { + fprintf(stderr, "calloc() failed. Aborting...\n"); + endwin(); + exit(EXIT_FAILURE); + } ret.prompt = prompt; ret.friendnum = friendnum; diff --git a/src/main.c b/src/main.c index 16c3e7a..4555789 100644 --- a/src/main.c +++ b/src/main.c @@ -317,14 +317,14 @@ static void load_data(Tox *m, char *path) buf = malloc(len); if (buf == NULL) { - fprintf(stderr, "malloc() failed.\n"); + fprintf(stderr, "malloc() failed. Aborting...\n"); fclose(fd); endwin(); exit(EXIT_FAILURE); } if (fread(buf, len, 1, fd) != 1) { - fprintf(stderr, "fread() failed.\n"); + fprintf(stderr, "fread() failed. Aborting...\n"); free(buf); fclose(fd); endwin(); @@ -357,13 +357,9 @@ static void load_data(Tox *m, char *path) void exit_toxic(Tox *m) { store_data(m, DATA_FILE); - - if (DATA_FILE != NULL) - free(DATA_FILE); - - if (SRVLIST_FILE != NULL) - free(SRVLIST_FILE); - + free(DATA_FILE); + free(SRVLIST_FILE); + free(prompt->s); tox_kill(m); endwin(); exit(EXIT_SUCCESS); @@ -401,17 +397,20 @@ int main(int argc, char *argv[]) SRVLIST_FILE = strdup(PACKAGE_DATADIR "/DHTservers"); } else { DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); - if (DATA_FILE != NULL) { + SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1); + + if (DATA_FILE != NULL && SRVLIST_FILE != NULL) { strcpy(DATA_FILE, user_config_dir); strcat(DATA_FILE, CONFIGDIR); strcat(DATA_FILE, "data"); - } - SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1); - if (SRVLIST_FILE != NULL) { strcpy(SRVLIST_FILE, user_config_dir); strcat(SRVLIST_FILE, CONFIGDIR); strcat(SRVLIST_FILE, "DHTservers"); + } else { + fprintf(stderr, "malloc() failed. Aborting...\n"); + endwin(); + exit(EXIT_FAILURE); } } } diff --git a/src/prompt.c b/src/prompt.c index 6b14045..e581f55 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -113,6 +113,13 @@ unsigned char *hex_string_to_bin(char hex_string[]) { size_t len = strlen(hex_string); unsigned char *val = malloc(len); + + if (val == NULL) { + fprintf(stderr, "malloc() failed. Aborting...\n"); + endwin(); + exit(EXIT_FAILURE); + } + char *pos = hex_string; int i; @@ -688,7 +695,14 @@ ToxWindow new_prompt() strcpy(ret.name, "prompt"); StatusBar *s = calloc(1, sizeof(StatusBar)); - ret.s = s; + + if (s != NULL) + ret.s = s; + else { + fprintf(stderr, "calloc() failed. Aborting...\n"); + endwin(); + exit(EXIT_FAILURE); + } return ret; } From c06189526614232af74d43bc18cf2431da72708d Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 11 Sep 2013 21:44:39 -0400 Subject: [PATCH 81/82] Unnecessary casting --- src/chat.c | 17 ++++++++--------- src/windows.c | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/chat.c b/src/chat.c index 78ae720..488c31a 100644 --- a/src/chat.c +++ b/src/chat.c @@ -49,7 +49,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 struct tm *timeinfo = get_time(); uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; - tox_getname(m, num, (uint8_t *) &nick); + tox_getname(m, num, nick); wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); @@ -81,7 +81,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin struct tm *timeinfo = get_time(); uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; - tox_getname(m, num, (uint8_t *) &nick); + tox_getname(m, num, nick); wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); @@ -133,7 +133,7 @@ int string_is_empty(char *string) } /* convert wide characters to null terminated string */ -static char *wcs_to_char(wchar_t *string) +static uint8_t *wcs_to_char(wchar_t *string) { size_t len = 0; char *ret = NULL; @@ -218,7 +218,7 @@ static void execute(ToxWindow *self, ChatContext *ctx, StatusBar *statusbar, Tox else if (!strncmp(cmd, "/me ", strlen("/me "))) { struct tm *timeinfo = get_time(); - char *action = strchr(cmd, ' '); + uint8_t *action = strchr(cmd, ' '); if (action == NULL) { wprintw(self->window, "Invalid syntax.\n"); @@ -239,7 +239,7 @@ static void execute(ToxWindow *self, ChatContext *ctx, StatusBar *statusbar, Tox wattroff(ctx->history, COLOR_PAIR(YELLOW)); if (!statusbar->is_online - || tox_sendaction(m, self->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { + || tox_sendaction(m, self->friendnum, action, strlen(action) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(RED)); @@ -379,7 +379,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) /* RETURN key: Execute command or print line */ else if (key == '\n') { - char *line = wcs_to_char(ctx->line); + uint8_t *line = wcs_to_char(ctx->line); wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); @@ -409,7 +409,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wprintw(ctx->history, "%s\n", line); if (!statusbar->is_online - || tox_sendmessage(m, self->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { + || tox_sendmessage(m, self->friendnum, line, strlen(line) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(RED)); @@ -538,13 +538,12 @@ ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum) ret.onAction = &chat_onAction; uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'}; - tox_getname(m, friendnum, (uint8_t *) &name); + tox_getname(m, friendnum, name); snprintf(ret.name, sizeof(ret.name), "%s", name); ChatContext *x = calloc(1, sizeof(ChatContext)); StatusBar *s = calloc(1, sizeof(StatusBar)); - if (s != NULL && x != NULL) { ret.x = x; ret.s = s; diff --git a/src/windows.c b/src/windows.c index e0583c4..e36a9af 100644 --- a/src/windows.c +++ b/src/windows.c @@ -30,7 +30,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata) { uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; - tox_getname(m, friendnumber, (uint8_t *) &nick); + tox_getname(m, friendnumber, nick); if (!nick[0]) snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); From 35cd2a2914b36b6aea6ccfab693bb8ec6d4206c0 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 12 Sep 2013 01:33:41 -0400 Subject: [PATCH 82/82] endwin needs to come first --- src/chat.c | 4 ++-- src/friendlist.c | 2 +- src/main.c | 8 ++++---- src/prompt.c | 4 ++-- src/windows.c | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/chat.c b/src/chat.c index 488c31a..fd1b885 100644 --- a/src/chat.c +++ b/src/chat.c @@ -153,8 +153,8 @@ static uint8_t *wcs_to_char(wchar_t *string) } if (ret == NULL) { - fprintf(stderr, "malloc() failed. Aborting...\n"); endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); exit(EXIT_FAILURE); } @@ -548,8 +548,8 @@ ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum) ret.x = x; ret.s = s; } else { - fprintf(stderr, "calloc() failed. Aborting...\n"); endwin(); + fprintf(stderr, "calloc() failed. Aborting...\n"); exit(EXIT_FAILURE); } diff --git a/src/friendlist.c b/src/friendlist.c index b831959..79f91bf 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -134,9 +134,9 @@ static void select_friend(Tox *m, wint_t key) } else return; /* Bad key input */ /* If we reach this something is wrong */ - fprintf(stderr, "select_friend() failed. Aborting...\n"); endwin(); tox_kill(m); + fprintf(stderr, "select_friend() failed. Aborting...\n"); exit(EXIT_FAILURE); } diff --git a/src/main.c b/src/main.c index 4555789..c5d9d83 100644 --- a/src/main.c +++ b/src/main.c @@ -317,17 +317,17 @@ static void load_data(Tox *m, char *path) buf = malloc(len); if (buf == NULL) { - fprintf(stderr, "malloc() failed. Aborting...\n"); fclose(fd); endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); exit(EXIT_FAILURE); } if (fread(buf, len, 1, fd) != 1) { - fprintf(stderr, "fread() failed. Aborting...\n"); free(buf); fclose(fd); endwin(); + fprintf(stderr, "fread() failed. Aborting...\n"); exit(EXIT_FAILURE); } @@ -347,8 +347,8 @@ static void load_data(Tox *m, char *path) int st; if ((st = store_data(m, path)) != 0) { - fprintf(stderr, "Store messenger failed with return code: %d\n", st); endwin(); + fprintf(stderr, "Store messenger failed with return code: %d\n", st); exit(EXIT_FAILURE); } } @@ -408,8 +408,8 @@ int main(int argc, char *argv[]) strcat(SRVLIST_FILE, CONFIGDIR); strcat(SRVLIST_FILE, "DHTservers"); } else { - fprintf(stderr, "malloc() failed. Aborting...\n"); endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); exit(EXIT_FAILURE); } } diff --git a/src/prompt.c b/src/prompt.c index e581f55..420bfef 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -115,8 +115,8 @@ unsigned char *hex_string_to_bin(char hex_string[]) unsigned char *val = malloc(len); if (val == NULL) { - fprintf(stderr, "malloc() failed. Aborting...\n"); endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); exit(EXIT_FAILURE); } @@ -699,8 +699,8 @@ ToxWindow new_prompt() if (s != NULL) ret.s = s; else { - fprintf(stderr, "calloc() failed. Aborting...\n"); endwin(); + fprintf(stderr, "calloc() failed. Aborting...\n"); exit(EXIT_FAILURE); } diff --git a/src/windows.c b/src/windows.c index e36a9af..7ff5c8d 100644 --- a/src/windows.c +++ b/src/windows.c @@ -195,8 +195,8 @@ ToxWindow *init_windows() int n_prompt = add_window(m, new_prompt()); if (n_prompt == -1 || add_window(m, new_friendlist()) == -1) { - fprintf(stderr, "add_window() failed. Aborting...\n"); endwin(); + fprintf(stderr, "add_window() failed. Aborting...\n"); exit(EXIT_FAILURE); }