2013-07-31 20:20:16 +02:00
|
|
|
/*
|
|
|
|
* Toxic -- Tox Curses Client
|
|
|
|
*/
|
|
|
|
|
2013-08-23 09:50:04 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-31 19:20:03 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <ctype.h>
|
2013-08-03 01:44:32 +02:00
|
|
|
#include <time.h>
|
2013-08-21 23:19:35 +02:00
|
|
|
#include <limits.h>
|
2013-07-31 19:20:03 +02:00
|
|
|
|
2013-08-23 00:18:53 +02:00
|
|
|
#include "toxic_windows.h"
|
2013-08-13 00:50:43 +02:00
|
|
|
#include "friendlist.h"
|
|
|
|
#include "chat.h"
|
2013-07-31 19:20:03 +02:00
|
|
|
|
2013-08-09 06:25:45 +02:00
|
|
|
#define CURS_Y_OFFSET 3
|
|
|
|
|
2013-07-31 19:20:03 +02:00
|
|
|
typedef struct {
|
2013-08-16 19:11:09 +02:00
|
|
|
int friendnum;
|
2013-08-21 23:19:35 +02:00
|
|
|
wchar_t line[MAX_STR_SIZE];
|
2013-08-16 19:11:09 +02:00
|
|
|
size_t pos;
|
|
|
|
WINDOW *history;
|
|
|
|
WINDOW *linewin;
|
2013-07-31 19:20:03 +02:00
|
|
|
} ChatContext;
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
void print_help(ChatContext *self);
|
2013-08-23 23:03:44 +02:00
|
|
|
void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd);
|
2013-07-31 19:20:03 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
struct tm *get_time(void)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
struct tm *timeinfo;
|
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
timeinfo = localtime(&now);
|
|
|
|
return timeinfo;
|
2013-08-13 02:28:43 +02:00
|
|
|
}
|
2013-07-31 19:20:03 +02:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
ChatContext *ctx = (ChatContext *) self->x;
|
2013-08-23 23:03:44 +02:00
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {0};
|
2013-08-16 19:11:09 +02:00
|
|
|
struct tm *timeinfo = get_time();
|
|
|
|
|
|
|
|
if (ctx->friendnum != num)
|
|
|
|
return;
|
|
|
|
|
2013-08-24 00:21:01 +02:00
|
|
|
tox_getname(m, num, (uint8_t *) &nick);
|
2013-08-16 19:11:09 +02:00
|
|
|
msg[len - 1] = '\0';
|
2013-08-23 23:03:44 +02:00
|
|
|
nick[TOX_MAX_NAME_LENGTH - 1] = '\0';
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
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: ", nick);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(4));
|
|
|
|
wprintw(ctx->history, "%s\n", msg);
|
|
|
|
|
|
|
|
self->blink = true;
|
|
|
|
beep();
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len)
|
2013-08-08 21:01:33 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
ChatContext *ctx = (ChatContext *) self->x;
|
|
|
|
struct tm *timeinfo = get_time();
|
2013-08-08 21:01:33 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
if (ctx->friendnum != num)
|
|
|
|
return;
|
2013-08-08 21:01:33 +02:00
|
|
|
|
2013-08-31 08:22:07 +02:00
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {0};
|
|
|
|
tox_getname(m, num, (uint8_t *) &nick);
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
action[len - 1] = '\0';
|
2013-08-08 21:01:33 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
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));
|
2013-08-08 21:01:33 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(5));
|
2013-08-31 08:22:07 +02:00
|
|
|
wprintw(ctx->history, "* %s %s\n", nick, action);
|
2013-08-16 19:11:09 +02:00
|
|
|
wattroff(ctx->history, COLOR_PAIR(5));
|
2013-08-08 21:01:33 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
self->blink = true;
|
|
|
|
beep();
|
2013-08-08 21:01:33 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
|
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
ChatContext *ctx = (ChatContext *) self->x;
|
|
|
|
struct tm *timeinfo = get_time();
|
|
|
|
|
|
|
|
if (ctx->friendnum != num)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wattron(ctx->history, COLOR_PAIR(2));
|
|
|
|
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(2));
|
|
|
|
|
|
|
|
nick[len - 1] = '\0';
|
|
|
|
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));
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
|
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
ChatContext *ctx = (ChatContext *) self->x;
|
|
|
|
struct tm *timeinfo = get_time();
|
|
|
|
|
|
|
|
if (ctx->friendnum != num)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wattron(ctx->history, COLOR_PAIR(2));
|
|
|
|
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(2));
|
|
|
|
|
|
|
|
status[len - 1] = '\0';
|
2013-08-21 01:37:05 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(3));
|
|
|
|
wprintw(ctx->history, "* Your partner changed status to '%s'\n", status);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(3));
|
|
|
|
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-03 23:29:18 +02:00
|
|
|
/* check that the string has one non-space character */
|
|
|
|
int string_is_empty(char *string)
|
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
int rc = 0;
|
|
|
|
char *copy = strdup(string);
|
|
|
|
rc = ((strtok(copy, " ") == NULL) ? 1 : 0);
|
|
|
|
free(copy);
|
|
|
|
return rc;
|
2013-08-03 23:29:18 +02:00
|
|
|
}
|
|
|
|
|
2013-08-21 23:19:35 +02:00
|
|
|
/* convert wide characters to null terminated string */
|
|
|
|
static char *wcs_to_char(wchar_t *string)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
len = wcstombs(NULL, string, 0);
|
|
|
|
if (len != (size_t) -1) {
|
|
|
|
len++;
|
|
|
|
ret = malloc(len);
|
|
|
|
wcstombs(ret, string, len);
|
|
|
|
} else {
|
|
|
|
ret = malloc(2);
|
|
|
|
ret[0] = ' ';
|
|
|
|
ret[1] = '\0';
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert a wide char to null terminated string */
|
|
|
|
static char *wc_to_char(wchar_t ch)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
static char ret[MB_LEN_MAX + 1];
|
|
|
|
|
|
|
|
len = wctomb(ret, ch);
|
|
|
|
if (len == -1) {
|
|
|
|
ret[0] = ' ';
|
|
|
|
ret[1] = '\0';
|
|
|
|
} else {
|
|
|
|
ret[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
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 */
|
2013-08-22 22:57:34 +02:00
|
|
|
#if HAVE_WIDECHAR
|
2013-08-21 23:19:35 +02:00
|
|
|
if (iswprint(key)) {
|
2013-08-22 22:57:34 +02:00
|
|
|
#else
|
|
|
|
if (isprint(key)) {
|
|
|
|
#endif
|
2013-08-31 08:22:07 +02:00
|
|
|
if (ctx->pos < MAX_STR_SIZE) {
|
2013-08-21 23:19:35 +02:00
|
|
|
mvwaddstr(self->window, y, x, wc_to_char(key));
|
2013-08-16 19:11:09 +02:00
|
|
|
ctx->line[ctx->pos++] = key;
|
2013-08-21 23:19:35 +02:00
|
|
|
ctx->line[ctx->pos] = L'\0';
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
/* BACKSPACE key: Remove one character from line */
|
|
|
|
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
|
|
|
|
if (ctx->pos > 0) {
|
2013-08-21 23:19:35 +02:00
|
|
|
ctx->line[--ctx->pos] = L'\0';
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
if (x == 0)
|
|
|
|
mvwdelch(self->window, y - 1, x2 - 1);
|
|
|
|
else
|
|
|
|
mvwdelch(self->window, y, x - 1);
|
|
|
|
}
|
2013-08-09 06:25:45 +02:00
|
|
|
}
|
2013-08-13 01:50:50 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
/* RETURN key: Execute command or print line */
|
|
|
|
else if (key == '\n') {
|
2013-08-21 23:19:35 +02:00
|
|
|
char *line = wcs_to_char(ctx->line);
|
2013-08-16 19:11:09 +02:00
|
|
|
wclear(ctx->linewin);
|
|
|
|
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
|
|
|
|
wclrtobot(self->window);
|
2013-08-31 02:13:29 +02:00
|
|
|
bool close_win = false;
|
|
|
|
|
|
|
|
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 {
|
2013-08-16 19:11:09 +02:00
|
|
|
/* make sure the string has at least non-space character */
|
2013-08-21 23:19:35 +02:00
|
|
|
if (!string_is_empty(line)) {
|
2013-08-23 23:03:44 +02:00
|
|
|
uint8_t selfname[TOX_MAX_NAME_LENGTH];
|
2013-08-25 01:16:43 +02:00
|
|
|
tox_getselfname(m, selfname, sizeof(selfname));
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
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));
|
2013-08-21 23:19:35 +02:00
|
|
|
wprintw(ctx->history, "%s\n", line);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-24 00:21:01 +02:00
|
|
|
if (tox_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) {
|
2013-08-16 19:11:09 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(3));
|
|
|
|
wprintw(ctx->history, " * Failed to send message.\n");
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(3));
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 01:50:50 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-31 02:13:29 +02:00
|
|
|
if (close_win)
|
|
|
|
free(ctx);
|
|
|
|
else {
|
|
|
|
ctx->line[0] = L'\0';
|
|
|
|
ctx->pos = 0;
|
|
|
|
}
|
|
|
|
|
2013-08-21 23:19:35 +02:00
|
|
|
free(line);
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
2013-08-04 10:42:17 +02:00
|
|
|
}
|
2013-08-03 16:00:48 +02:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
|
2013-08-04 10:42:17 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
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);
|
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
|
|
|
|
print_help(ctx);
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
|
|
|
|
endwin();
|
|
|
|
exit(0);
|
2013-08-08 21:01:33 +02:00
|
|
|
}
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
else if (!strncmp(cmd, "/me ", strlen("/me "))) {
|
|
|
|
struct tm *timeinfo = get_time();
|
|
|
|
char *action = strchr(cmd, ' ');
|
2013-08-08 21:01:33 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
if (action == NULL) {
|
|
|
|
wprintw(self->window, "Invalid syntax.\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-08-08 21:01:33 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
action++;
|
2013-08-08 12:22:48 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
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));
|
2013-08-08 12:22:48 +02:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
uint8_t selfname[TOX_MAX_NAME_LENGTH];
|
2013-08-31 08:22:07 +02:00
|
|
|
tox_getselfname(m, selfname, sizeof(selfname));
|
2013-08-08 12:22:48 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(5));
|
2013-08-31 08:22:07 +02:00
|
|
|
wprintw(ctx->history, "* %s %s\n", selfname, action);
|
2013-08-16 19:11:09 +02:00
|
|
|
wattroff(ctx->history, COLOR_PAIR(5));
|
2013-08-08 12:22:48 +02:00
|
|
|
|
2013-08-31 08:22:07 +02:00
|
|
|
if (tox_sendaction(m, ctx->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) {
|
2013-08-16 19:11:09 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(3));
|
|
|
|
wprintw(ctx->history, " * Failed to send action\n");
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(3));
|
|
|
|
}
|
2013-08-08 12:22:48 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
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++;
|
2013-08-23 23:03:44 +02:00
|
|
|
TOX_USERSTATUS status_kind;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
if (!strncmp(status, "online", strlen("online"))) {
|
2013-08-23 23:03:44 +02:00
|
|
|
status_kind = TOX_USERSTATUS_NONE;
|
2013-09-02 06:18:20 +02:00
|
|
|
status_text = "Online";
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (!strncmp(status, "away", strlen("away"))) {
|
2013-08-23 23:03:44 +02:00
|
|
|
status_kind = TOX_USERSTATUS_AWAY;
|
2013-09-02 06:18:20 +02:00
|
|
|
status_text = "Away";
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (!strncmp(status, "busy", strlen("busy"))) {
|
2013-08-23 23:03:44 +02:00
|
|
|
status_kind = TOX_USERSTATUS_BUSY;
|
2013-09-02 06:18:20 +02:00
|
|
|
status_text = "Busy";
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
wprintw(ctx->history, "Invalid status.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = strchr(status, ' ');
|
|
|
|
|
|
|
|
if (msg == NULL) {
|
2013-08-24 00:21:01 +02:00
|
|
|
tox_set_userstatus(m, status_kind);
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(ctx->history, "Status set to: %s\n", status_text);
|
|
|
|
} else {
|
|
|
|
msg++;
|
2013-08-24 00:21:01 +02:00
|
|
|
tox_set_userstatus(m, status_kind);
|
|
|
|
tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
|
|
|
|
}
|
2013-08-08 12:22:48 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
|
|
|
|
char *nick;
|
|
|
|
nick = strchr(cmd, ' ');
|
|
|
|
|
|
|
|
if (nick == NULL) {
|
|
|
|
wprintw(ctx->history, "Invalid syntax.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nick++;
|
2013-08-24 00:21:01 +02:00
|
|
|
tox_setname(m, (uint8_t *) nick, strlen(nick) + 1);
|
2013-08-16 19:11:09 +02:00
|
|
|
wprintw(ctx->history, "Nickname set to: %s\n", nick);
|
2013-08-04 10:42:17 +02:00
|
|
|
}
|
2013-08-13 01:50:50 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
else if (!strcmp(cmd, "/myid")) {
|
2013-08-23 23:03:44 +02:00
|
|
|
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
|
2013-08-16 19:11:09 +02:00
|
|
|
int i;
|
2013-08-23 23:03:44 +02:00
|
|
|
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
2013-08-25 01:16:43 +02:00
|
|
|
tox_getaddress(m, address);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
|
2013-08-16 19:11:09 +02:00
|
|
|
char xx[3];
|
|
|
|
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
|
|
|
|
strcat(id, xx);
|
|
|
|
}
|
|
|
|
|
|
|
|
wprintw(ctx->history, "%s\n", id);
|
2013-08-04 10:42:17 +02:00
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
else
|
|
|
|
wprintw(ctx->history, "Invalid command.\n");
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void chat_onDraw(ToxWindow *self, Tox *m)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
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);
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void chat_onInit(ToxWindow *self, Tox *m)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
int x, y;
|
|
|
|
ChatContext *ctx = (ChatContext *) self->x;
|
|
|
|
getmaxyx(self->window, y, x);
|
|
|
|
ctx->history = subwin(self->window, y - 4, x, 0, 0);
|
|
|
|
scrollok(ctx->history, 1);
|
|
|
|
ctx->linewin = subwin(self->window, 2, x, y - 4, 0);
|
|
|
|
print_help(ctx);
|
|
|
|
wmove(self->window, y - CURS_Y_OFFSET, 0);
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
void print_help(ChatContext *self)
|
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
wattron(self->history, COLOR_PAIR(2) | A_BOLD);
|
|
|
|
wprintw(self->history, "Commands:\n");
|
|
|
|
wattroff(self->history, A_BOLD);
|
|
|
|
|
|
|
|
wprintw(self->history, " /status <type> <message> : Set your status\n");
|
|
|
|
wprintw(self->history, " /nick <nickname> : Set your nickname\n");
|
|
|
|
wprintw(self->history, " /me <action> : 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, " /help : Print this message again\n\n");
|
|
|
|
|
|
|
|
wattroff(self->history, COLOR_PAIR(2));
|
2013-08-04 10:42:17 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
ToxWindow new_chat(Tox *m, int friendnum)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
ToxWindow ret;
|
|
|
|
memset(&ret, 0, sizeof(ret));
|
|
|
|
|
|
|
|
ret.onKey = &chat_onKey;
|
|
|
|
ret.onDraw = &chat_onDraw;
|
|
|
|
ret.onInit = &chat_onInit;
|
|
|
|
ret.onMessage = &chat_onMessage;
|
|
|
|
ret.onNickChange = &chat_onNickChange;
|
|
|
|
ret.onStatusChange = &chat_onStatusChange;
|
|
|
|
ret.onAction = &chat_onAction;
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {0};
|
2013-08-24 00:21:01 +02:00
|
|
|
tox_getname(m, friendnum, (uint8_t *) &nick);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
|
|
|
|
|
|
|
|
ChatContext *x = calloc(1, sizeof(ChatContext));
|
2013-08-31 02:13:29 +02:00
|
|
|
ret.x = x;
|
2013-08-16 19:11:09 +02:00
|
|
|
x->friendnum = friendnum;
|
|
|
|
return ret;
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|