diff --git a/chat.c b/chat.c index 8376577..73cb145 100644 --- a/chat.c +++ b/chat.c @@ -27,7 +27,7 @@ extern int active_window; extern void del_window(ToxWindow *w, int f_num); extern void fix_name(uint8_t *name); void print_help(ChatContext *self); -void execute(ToxWindow *self, ChatContext *ctx, char *cmd); +void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo); static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len) { @@ -35,7 +35,7 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len) uint8_t nick[MAX_NAME_LENGTH] = {0}; time_t now; time(&now); - struct tm * timeinfo; + struct tm *timeinfo; timeinfo = localtime(&now); if (ctx->friendnum != num) @@ -59,6 +59,32 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len) beep(); } +static void chat_onAction(ToxWindow *self, int num, uint8_t *action, uint16_t len) +{ + ChatContext *ctx = (ChatContext*) self->x; + time_t now; + time(&now); + struct tm *timeinfo; + timeinfo = localtime(&now); + + if (ctx->friendnum != num) + return; + + action[len-1] = '\0'; + fix_name(action); + + wattron(ctx->history, COLOR_PAIR(2)); + wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattroff(ctx->history, COLOR_PAIR(2)); + + wattron(ctx->history, COLOR_PAIR(4)); + wprintw(ctx->history, "%s\n", action); + wattroff(ctx->history, COLOR_PAIR(4)); + + self->blink = true; + beep(); +} + static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) { ChatContext *ctx = (ChatContext*) self->x; @@ -108,7 +134,7 @@ static void chat_onKey(ToxWindow *self, int key) /* RETURN key: Execute command or print line */ else if (key == '\n') { if (ctx->line[0] == '/') - execute(self, ctx, ctx->line); + execute(self, ctx, ctx->line, timeinfo); else { if (!string_is_empty(ctx->line)) { /* make sure the string has at least non-space character */ @@ -138,7 +164,7 @@ static void chat_onKey(ToxWindow *self, int key) } } -void execute(ToxWindow *self, ChatContext *ctx, char *cmd) +void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { wclear(self->window); @@ -153,6 +179,33 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd) exit(0); } + else if (!strncmp(cmd, "/me ", strlen("/me "))) { + char *action = strchr(cmd, ' '); + if (action == NULL) { + wprintw(self->window, "Invalid syntax.\n"); + return; + } + action++; + + wattron(ctx->history, COLOR_PAIR(2)); + wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattroff(ctx->history, COLOR_PAIR(2)); + + uint8_t selfname[MAX_NAME_LENGTH]; + int len = getself_name(selfname); + char msg[MAX_STR_SIZE-len-4]; + snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action); + + wattron(ctx->history, COLOR_PAIR(1)); + wprintw(ctx->history, msg); + wattroff(ctx->history, COLOR_PAIR(1)); + if (m_sendaction(ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) { + wattron(ctx->history, COLOR_PAIR(3)); + wprintw(ctx->history, " * Failed to send action\n"); + wattroff(ctx->history, COLOR_PAIR(3)); + } + } + else if (!strncmp(cmd, "/status ", strlen("/status "))) { char *status = strchr(cmd, ' '); char *msg; @@ -178,8 +231,7 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd) status_text = "BUSY"; } - else - { + else { wprintw(ctx->history, "Invalid status.\n"); return; } @@ -265,6 +317,7 @@ void print_help(ChatContext *self) wprintw(self->history, " /status : Set your status\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"); @@ -285,6 +338,7 @@ ToxWindow new_chat(int friendnum) ret.onMessage = &chat_onMessage; ret.onNickChange = &chat_onNickChange; ret.onStatusChange = &chat_onStatusChange; + ret.onAction = &chat_onAction; uint8_t nick[MAX_NAME_LENGTH] = {0}; getname(friendnum, (uint8_t*) &nick); diff --git a/friendlist.c b/friendlist.c index 159217b..f2aa1cf 100644 --- a/friendlist.c +++ b/friendlist.c @@ -177,6 +177,7 @@ ToxWindow new_friendlist() { ret.onDraw = &friendlist_onDraw; ret.onInit = &friendlist_onInit; ret.onMessage = &friendlist_onMessage; + ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message ret.onNickChange = &friendlist_onNickChange; ret.onStatusChange = &friendlist_onStatusChange; diff --git a/main.c b/main.c index 7fa9e96..d5999eb 100644 --- a/main.c +++ b/main.c @@ -44,8 +44,9 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length) wprintw(prompt->window, "%02x", public_key[i] & 0xff); } - wprintw(prompt->window, "\n"); - wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n); + wprintw(prompt->window, "\nWith the message: %s\n", data); + wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); + for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { if (windows[i].onFriendRequest != NULL) windows[i].onFriendRequest(&windows[i], public_key, data, length); @@ -54,7 +55,6 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length) void on_message(int friendnumber, uint8_t *string, uint16_t length) { - wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string); int i; for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { if (windows[i].onMessage != NULL) @@ -62,6 +62,15 @@ void on_message(int friendnumber, uint8_t *string, uint16_t length) } } +void on_action(int friendnumber, uint8_t *string, uint16_t length) +{ + int i; + for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { + if (windows[i].onAction != NULL) + windows[i].onAction(&windows[i], friendnumber, string, length); + } +} + void on_nickchange(int friendnumber, uint8_t *string, uint16_t length) { wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); @@ -117,6 +126,7 @@ static void init_tox() m_callback_friendmessage(on_message); m_callback_namechange(on_nickchange); m_callback_statusmessage(on_statuschange); + m_callback_action(on_action); } void init_window_status() @@ -272,13 +282,13 @@ static void draw_bar() attron(A_BOLD); odd = (odd+1) % blinkrate; - if (windows[i].blink && (odd < (blinkrate/2))) { + if (windows[i].blink && (odd < (blinkrate/2))) attron(COLOR_PAIR(3)); - } + printw(" %s", windows[i].title); - if (windows[i].blink && (odd < (blinkrate/2))) { + if (windows[i].blink && (odd < (blinkrate/2))) attroff(COLOR_PAIR(3)); - } + if (i == active_window) { attroff(A_BOLD); } @@ -308,7 +318,6 @@ void set_active_window(int ch) i = (i + 1) % max; if (f_inf++ > max) { // infinite loop check endwin(); - clear(); exit(2); } } @@ -323,7 +332,6 @@ void set_active_window(int ch) if (--i < 0) i = max; if (f_inf++ > max) { endwin(); - clear(); exit(2); } } diff --git a/windows.h b/windows.h index 287e534..c6925ce 100644 --- a/windows.h +++ b/windows.h @@ -24,6 +24,7 @@ struct ToxWindow_ { void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t); void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t); void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t); + void(*onAction)(ToxWindow*, int, uint8_t*, uint16_t); char title[256]; void* x;