diff --git a/chat.c b/chat.c index a90bb2a..72fa5d4 100644 --- a/chat.c +++ b/chat.c @@ -26,6 +26,8 @@ typedef struct { } ChatContext; extern void fix_name(uint8_t* name); +void print_help(ChatContext* self); +void execute(ToxWindow* self, ChatContext* ctx, char* cmd); static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { ChatContext* ctx = (ChatContext*) self->x; @@ -92,24 +94,19 @@ int string_is_empty(char *string) static void chat_onKey(ToxWindow* self, int key) { ChatContext* ctx = (ChatContext*) self->x; - time_t now; - time(&now); - struct tm * timeinfo; - timeinfo = localtime(&now); - + /* PRINTABLE characters: Add to line */ if(isprint(key)) { - if(ctx->pos != sizeof(ctx->line)-1) { ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = '\0'; } } + /* RETURN key: Execute command or print line */ else if(key == '\n') { - if(!string_is_empty(ctx->line)) { - /* make sure the string has at least non-space character */ - wattron(ctx->history, COLOR_PAIR(2)); - wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + if (ctx->line[0] == '/') + execute(self, ctx, ctx->line); + else { wattron(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "you: ", ctx->line); wattroff(ctx->history, COLOR_PAIR(1)); @@ -120,18 +117,65 @@ static void chat_onKey(ToxWindow* self, int key) { wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(3)); } - - ctx->line[0] = '\0'; - ctx->pos = 0; } + ctx->line[0] = '\0'; + ctx->pos = 0; } + /* BACKSPACE key: Remove one character from line */ else if(key == 0x107 || key == 0x8 || key == 0x7f) { if(ctx->pos != 0) { ctx->line[--ctx->pos] = '\0'; } } +} +void execute(ToxWindow* self, ChatContext* ctx, char* cmd) +{ + if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { + wclear(self->window); + wclear(ctx->history); + } + else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) + print_help(ctx); + else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { + endwin(); + exit(0); + } + else if (!strncmp(cmd, "/status ", strlen("/status "))) { + char* msg; + msg = strchr(cmd, ' '); + if(msg == NULL) { + wprintw(ctx->history, "Invalid syntax.\n"); + return; + } + msg++; + m_set_userstatus((uint8_t*) msg, strlen(msg)+1); + wprintw(ctx->history, "Status set to: %s\n", msg); + } + else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { + char* nick; + nick = strchr(cmd, ' '); + if(nick == NULL) { + wprintw(ctx->history, "Invalid syntax.\n"); + return; + } + nick++; + setname((uint8_t*) nick, strlen(nick)+1); + wprintw(ctx->history, "Nickname set to: %s\n", nick); + } + else if(!strcmp(cmd, "/myid")) { + char id[32*2 + 1] = {0}; + int i; + for (i = 0; i < 32; i++) { + char xx[3]; + snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); + strcat(id, xx); + } + wprintw(ctx->history, "Your ID: %s\n", id); + } + else + wprintw(ctx->history, "Invalid command.\n"); } static void chat_onDraw(ToxWindow* self) { @@ -164,6 +208,21 @@ static void chat_onInit(ToxWindow* self) { ctx->linewin = subwin(self->window, 2, x, y - 3, 0); } +void print_help(ChatContext* self) { + wattron(self->history, COLOR_PAIR(2) | A_BOLD); + wprintw(self->history, "\nCommands:\n"); + wattroff(self->history, A_BOLD); + + wprintw(self->history, " /status : Set your status\n"); + wprintw(self->history, " /nick : Set your nickname\n"); + wprintw(self->history, " /myid : Print your ID\n"); + wprintw(self->history, " /clear : Clear the screen\n"); + wprintw(self->history, " /quit or /exit : Exit program\n"); + wprintw(self->history, " /help : Print this message again\n\n"); + + wattroff(self->history, COLOR_PAIR(2)); +} + ToxWindow new_chat(int friendnum) { ToxWindow ret; diff --git a/prompt.c b/prompt.c index a47238a..365aee4 100644 --- a/prompt.c +++ b/prompt.c @@ -148,7 +148,7 @@ static void execute(ToxWindow* self, char* cmd) { wprintw(self->window, "Friend request already sent.\n"); break; case -5: - wprintw(self->window, "[i] Undefined error when adding friend.\n"); + wprintw(self->window, "Undefined error when adding friend.\n"); break; default: wprintw(self->window, "Friend added as %d.\n", num); @@ -178,12 +178,13 @@ static void execute(ToxWindow* self, char* cmd) { nick = strchr(cmd, ' '); if(nick == NULL) { + wprintw(self->window, "Invalid syntax.\n"); return; } nick++; setname((uint8_t*) nick, strlen(nick)+1); - wprintw(self->window, "Nickname set to: %s.\n", nick); + wprintw(self->window, "Nickname set to: %s\n", nick); } else if(!strcmp(cmd, "myid")) { char id[32*2 + 1] = {0}; @@ -195,7 +196,7 @@ static void execute(ToxWindow* self, char* cmd) { strcat(id, xx); } - wprintw(self->window, "%s\n", id); + wprintw(self->window, "Your ID: %s\n", id); } else if(!strncmp(cmd, "accept ", strlen("accept "))) { char* id; @@ -256,7 +257,7 @@ static void execute(ToxWindow* self, char* cmd) { wclear(self->window); } else { - wprintw(self->window, "Invalid syntax.\n"); + wprintw(self->window, "Invalid command.\n"); } }