void cmd_mynick(ToxWindow *self, Messenger *m, char **args) { uint8_t *nick = malloc(m->name_length); getself_name(m, nick, m->name_length); wprintw(self->window, "Current nickname: %s\n", nick); free(nick); }
char *format_message(char *message, int friendnum) { char name[MAX_NAME_LENGTH]; if (friendnum != -1) { getname(friendnum, (uint8_t*)name); } else { getself_name((uint8_t*)name); } char *msg = malloc(100+strlen(message)+strlen(name)+1); time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); char* time = asctime(timeinfo); size_t len = strlen(time); time[len-1] = '\0'; if (friendnum != -1) { sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message); } else { // This message came from ourselves sprintf(msg, "%s <%s> %s", time, name, message); } return msg; }
END_TEST START_TEST(test_getself_name) { char *nickname = "testGallop"; int len = strlen(nickname); char nick_check[len]; setname(m, (uint8_t *)nickname, len); getself_name(m, (uint8_t *)nick_check); ck_assert_msg((memcmp(nickname, nick_check, len) == 0), "getself_name failed to return the known name!\n" "known name: %s\nreturned: %s\n", nickname, nick_check); }
/* Get your nickname. * m - The messanger context to use. * name - Pointer to a string for the name. * nlen - The length of the string buffer. * * return length of the name. * return 0 on error. */ uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen) { Messenger *m = tox; return getself_name(m, name, nlen); }
/* Get your nickname. * m - The messenger context to use. * name - Pointer to a string for the name. (must be at least MAX_NAME_LENGTH) * * return length of the name. * return 0 on error. */ uint16_t tox_get_self_name(const Tox *tox, uint8_t *name) { const Messenger *m = tox; return getself_name(m, name); }
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { wclear(self->window); wclear(ctx->history); int x, y; getmaxyx(self->window, y, x); (void) x; wmove(self->window, y - CURS_Y_OFFSET, 0); } else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) print_help(ctx); else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { endwin(); exit(0); } else if (!strncmp(cmd, "/me ", strlen("/me "))) { struct tm *timeinfo = get_time(); 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(m, selfname, sizeof(selfname)); char msg[MAX_STR_SIZE - len - 4]; snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action); wattron(ctx->history, COLOR_PAIR(5)); wprintw(ctx->history, msg); wattroff(ctx->history, COLOR_PAIR(5)); if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(3)); } } else if (!strncmp(cmd, "/status ", strlen("/status "))) { char *status = strchr(cmd, ' '); char *msg; char *status_text; if (status == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } status++; USERSTATUS status_kind; if (!strncmp(status, "online", strlen("online"))) { status_kind = USERSTATUS_NONE; status_text = "ONLINE"; } else if (!strncmp(status, "away", strlen("away"))) { status_kind = USERSTATUS_AWAY; status_text = "AWAY"; } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = USERSTATUS_BUSY; status_text = "BUSY"; } else { wprintw(ctx->history, "Invalid status.\n"); return; } msg = strchr(status, ' '); if (msg == NULL) { m_set_userstatus(m, status_kind); wprintw(ctx->history, "Status set to: %s\n", status_text); } else { msg++; m_set_userstatus(m, status_kind); m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); } } else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { char *nick; nick = strchr(cmd, ' '); if (nick == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } nick++; setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(ctx->history, "Nickname set to: %s\n", nick); } else if (!strcmp(cmd, "/myid")) { char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; int i; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { char xx[3]; snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); strcat(id, xx); } wprintw(ctx->history, "%s\n", id); } else if (strcmp(ctx->line, "/close") == 0) { int f_num = ctx->friendnum; delwin(ctx->linewin); del_window(self); disable_chatwin(f_num); } else wprintw(ctx->history, "Invalid command.\n"); }
static void chat_onKey(ToxWindow *self, Messenger *m, int key) { ChatContext *ctx = (ChatContext *) self->x; struct tm *timeinfo = get_time(); int x, y, y2, x2; getyx(self->window, y, x); getmaxyx(self->window, y2, x2); /* Add printable chars to buffer and print on input space */ if (isprint(key)) { if (ctx->pos != sizeof(ctx->line) - 1) { mvwaddch(self->window, y, x, key); ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = '\0'; } } /* BACKSPACE key: Remove one character from line */ else if (key == 0x107 || key == 0x8 || key == 0x7f) { if (ctx->pos > 0) { ctx->line[--ctx->pos] = '\0'; if (x == 0) mvwdelch(self->window, y - 1, x2 - 1); else mvwdelch(self->window, y, x - 1); } } /* RETURN key: Execute command or print line */ else if (key == '\n') { wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); if (ctx->line[0] == '/') execute(self, ctx, m, ctx->line); else { /* make sure the string has at least non-space character */ if (!string_is_empty(ctx->line)) { uint8_t selfname[MAX_NAME_LENGTH]; getself_name(m, selfname, sizeof(selfname)); fix_name(selfname); wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wattroff(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "%s: ", selfname); wattroff(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "%s\n", ctx->line); if (m_sendmessage(m, ctx->friendnum, (uint8_t *) ctx->line, strlen(ctx->line) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(3)); } } } ctx->line[0] = '\0'; ctx->pos = 0; } }
static void chat_onKey(ToxWindow *self, Messenger *m, wint_t key) { ChatContext *ctx = (ChatContext *) self->x; struct tm *timeinfo = get_time(); int x, y, y2, x2; getyx(self->window, y, x); getmaxyx(self->window, y2, x2); /* Add printable chars to buffer and print on input space */ #if HAVE_WIDECHAR if (iswprint(key)) { #else if (isprint(key)) { #endif if (ctx->pos != sizeof(ctx->line) - 1) { mvwaddstr(self->window, y, x, wc_to_char(key)); ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = L'\0'; } } /* BACKSPACE key: Remove one character from line */ else if (key == 0x107 || key == 0x8 || key == 0x7f) { if (ctx->pos > 0) { ctx->line[--ctx->pos] = L'\0'; if (x == 0) mvwdelch(self->window, y - 1, x2 - 1); else mvwdelch(self->window, y, x - 1); } } /* RETURN key: Execute command or print line */ else if (key == '\n') { char *line = wcs_to_char(ctx->line); wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); if (line[0] == '/') execute(self, ctx, m, line); else { /* make sure the string has at least non-space character */ if (!string_is_empty(line)) { uint8_t selfname[MAX_NAME_LENGTH]; getself_name(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); wattroff(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "%s: ", selfname); wattroff(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "%s\n", line); if (m_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)); } } } ctx->line[0] = L'\0'; ctx->pos = 0; free(line); } } void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { wclear(self->window); wclear(ctx->history); int x, y; getmaxyx(self->window, y, x); (void) x; wmove(self->window, y - CURS_Y_OFFSET, 0); } else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) print_help(ctx); else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { endwin(); exit(0); } else if (!strncmp(cmd, "/me ", strlen("/me "))) { struct tm *timeinfo = get_time(); 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(m, selfname, sizeof(selfname)); char msg[MAX_STR_SIZE - len - 4]; snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action); wattron(ctx->history, COLOR_PAIR(5)); wprintw(ctx->history, msg); wattroff(ctx->history, COLOR_PAIR(5)); if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(3)); } } else if (!strncmp(cmd, "/status ", strlen("/status "))) { char *status = strchr(cmd, ' '); char *msg; char *status_text; if (status == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } status++; USERSTATUS status_kind; if (!strncmp(status, "online", strlen("online"))) { status_kind = USERSTATUS_NONE; status_text = "ONLINE"; } else if (!strncmp(status, "away", strlen("away"))) { status_kind = USERSTATUS_AWAY; status_text = "AWAY"; } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = USERSTATUS_BUSY; status_text = "BUSY"; } else { wprintw(ctx->history, "Invalid status.\n"); return; } msg = strchr(status, ' '); if (msg == NULL) { m_set_userstatus(m, status_kind); wprintw(ctx->history, "Status set to: %s\n", status_text); } else { msg++; m_set_userstatus(m, status_kind); m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); } } else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { char *nick; nick = strchr(cmd, ' '); if (nick == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } nick++; setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(ctx->history, "Nickname set to: %s\n", nick); } else if (!strcmp(cmd, "/myid")) { char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; uint8_t address[FRIEND_ADDRESS_SIZE]; size_t i; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { char xx[3]; snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); strcat(id, xx); } wprintw(ctx->history, "%s\n", id); } else if (strcmp(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"); } static void chat_onDraw(ToxWindow *self, Messenger *m) { curs_set(1); int x, y; getmaxyx(self->window, y, x); (void) y; ChatContext *ctx = (ChatContext *) self->x; mvwhline(ctx->linewin, 0, 0, '_', x); wrefresh(self->window); }
/* Get your nickname. * m - The messanger context to use. * name - Pointer to a string for the name. * nlen - The length of the string buffer. * * return length of the name. * return 0 on error. */ size_t tox_get_self_name(Tox *tox, size_t *name, size_t nlen) { Messenger *m = tox; return getself_name(m, name, nlen); }