2013-07-31 20:20:16 +02:00
|
|
|
/*
|
|
|
|
* Toxic -- Tox Curses Client
|
|
|
|
*/
|
|
|
|
|
2013-07-31 19:20:03 +02:00
|
|
|
#include <curses.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <ctype.h>
|
2013-08-03 01:44:32 +02:00
|
|
|
#include <time.h>
|
2013-07-31 19:20:03 +02:00
|
|
|
|
|
|
|
#include "../../core/Messenger.h"
|
|
|
|
#include "../../core/network.h"
|
|
|
|
|
|
|
|
#include "windows.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int friendnum;
|
2013-08-08 10:51:58 +02:00
|
|
|
char line[MAX_STR_SIZE];
|
2013-07-31 19:20:03 +02:00
|
|
|
size_t pos;
|
|
|
|
WINDOW* history;
|
|
|
|
WINDOW* linewin;
|
|
|
|
} ChatContext;
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
extern int active_window;
|
|
|
|
|
2013-08-05 07:57:29 +02:00
|
|
|
extern void del_window(ToxWindow *w, int f_num);
|
2013-08-07 00:27:51 +02:00
|
|
|
extern void fix_name(uint8_t *name);
|
|
|
|
void print_help(ChatContext *self);
|
|
|
|
void execute(ToxWindow *self, ChatContext *ctx, char *cmd);
|
2013-07-31 19:20:03 +02:00
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
|
|
|
|
{
|
|
|
|
ChatContext *ctx = (ChatContext*) self->x;
|
2013-07-31 19:20:03 +02:00
|
|
|
uint8_t nick[MAX_NAME_LENGTH] = {0};
|
2013-08-03 01:44:32 +02:00
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
struct tm * timeinfo;
|
|
|
|
timeinfo = localtime(&now);
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
if (ctx->friendnum != num)
|
2013-07-31 19:20:03 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
getname(num, (uint8_t*) &nick);
|
|
|
|
msg[len-1] = '\0';
|
|
|
|
nick[MAX_NAME_LENGTH-1] = '\0';
|
|
|
|
fix_name(msg);
|
|
|
|
fix_name(nick);
|
|
|
|
|
2013-08-03 01:44:32 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(2));
|
2013-08-06 00:38:55 +02:00
|
|
|
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
2013-08-04 12:52:24 +02:00
|
|
|
wattroff(ctx->history, COLOR_PAIR(2));
|
2013-08-03 01:44:32 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(4));
|
2013-07-31 20:20:16 +02:00
|
|
|
wprintw(ctx->history, "%s: ", nick);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(4));
|
|
|
|
wprintw(ctx->history, "%s\n", msg);
|
|
|
|
|
|
|
|
self->blink = true;
|
2013-08-06 00:39:32 +02:00
|
|
|
beep();
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
|
|
|
|
{
|
|
|
|
ChatContext *ctx = (ChatContext*) self->x;
|
|
|
|
if (ctx->friendnum != num)
|
2013-07-31 19:20:03 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
nick[len-1] = '\0';
|
|
|
|
fix_name(nick);
|
2013-08-01 06:32:54 +02:00
|
|
|
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
|
|
|
|
|
2013-07-31 19:20:03 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(3));
|
|
|
|
wprintw(ctx->history, " * Your partner changed nick to '%s'\n", nick);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(3));
|
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
|
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
char *copy = strdup(string);
|
|
|
|
rc = ((strtok(copy, " ") == NULL) ? 1:0);
|
|
|
|
free(copy);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
static void chat_onKey(ToxWindow *self, int key)
|
|
|
|
{
|
|
|
|
ChatContext *ctx = (ChatContext*) self->x;
|
2013-08-04 11:00:16 +02:00
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
struct tm * timeinfo;
|
|
|
|
timeinfo = localtime(&now);
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
/* Add printable characters to line */
|
|
|
|
if (isprint(key)) {
|
|
|
|
if (ctx->pos != sizeof(ctx->line)-1) {
|
2013-07-31 19:20:03 +02:00
|
|
|
ctx->line[ctx->pos++] = key;
|
|
|
|
ctx->line[ctx->pos] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2013-08-03 23:29:18 +02:00
|
|
|
|
2013-08-04 10:42:17 +02:00
|
|
|
/* RETURN key: Execute command or print line */
|
2013-08-07 00:27:51 +02:00
|
|
|
else if (key == '\n') {
|
2013-08-04 10:42:17 +02:00
|
|
|
if (ctx->line[0] == '/')
|
|
|
|
execute(self, ctx, ctx->line);
|
|
|
|
else {
|
2013-08-07 00:27:51 +02:00
|
|
|
if (!string_is_empty(ctx->line)) {
|
2013-08-04 11:00:16 +02:00
|
|
|
/* make sure the string has at least non-space character */
|
|
|
|
wattron(ctx->history, COLOR_PAIR(2));
|
2013-08-05 07:57:29 +02:00
|
|
|
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
2013-08-04 12:52:24 +02:00
|
|
|
wattroff(ctx->history, COLOR_PAIR(2));
|
2013-08-04 11:00:16 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(1));
|
|
|
|
wprintw(ctx->history, "you: ", ctx->line);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(1));
|
|
|
|
wprintw(ctx->history, "%s\n", ctx->line);
|
|
|
|
}
|
2013-08-07 19:51:58 +02:00
|
|
|
if (m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) {
|
2013-08-03 23:29:18 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(3));
|
|
|
|
wprintw(ctx->history, " * Failed to send message.\n");
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(3));
|
|
|
|
}
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
2013-08-04 10:42:17 +02:00
|
|
|
ctx->line[0] = '\0';
|
|
|
|
ctx->pos = 0;
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
2013-08-03 23:29:18 +02:00
|
|
|
|
2013-08-04 10:42:17 +02:00
|
|
|
/* BACKSPACE key: Remove one character from line */
|
2013-08-07 00:27:51 +02:00
|
|
|
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
|
|
|
|
if (ctx->pos != 0) {
|
2013-07-31 20:20:16 +02:00
|
|
|
ctx->line[--ctx->pos] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2013-08-04 10:42:17 +02:00
|
|
|
}
|
2013-08-03 16:00:48 +02:00
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
void execute(ToxWindow *self, ChatContext *ctx, char *cmd)
|
2013-08-04 10:42:17 +02:00
|
|
|
{
|
|
|
|
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
|
|
|
|
wclear(self->window);
|
|
|
|
wclear(ctx->history);
|
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-04 10:42:17 +02:00
|
|
|
else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
|
|
|
|
print_help(ctx);
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-04 10:42:17 +02:00
|
|
|
else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
|
|
|
|
endwin();
|
|
|
|
exit(0);
|
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-04 10:42:17 +02:00
|
|
|
else if (!strncmp(cmd, "/status ", strlen("/status "))) {
|
2013-08-08 12:22:48 +02:00
|
|
|
char *status = strchr(cmd, ' ');
|
2013-08-07 00:27:51 +02:00
|
|
|
char *msg;
|
2013-08-08 12:22:48 +02:00
|
|
|
char *status_text;
|
|
|
|
if (status == NULL) {
|
2013-08-04 10:42:17 +02:00
|
|
|
wprintw(ctx->history, "Invalid syntax.\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-08-08 12:22:48 +02:00
|
|
|
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(status_kind);
|
|
|
|
wprintw(ctx->history, "Status set to: %s\n", status_text);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg++;
|
|
|
|
m_set_userstatus(status_kind);
|
|
|
|
m_set_statusmessage((uint8_t*) msg, strlen(msg)+1);
|
|
|
|
wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
|
|
|
|
}
|
2013-08-04 10:42:17 +02:00
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-04 10:42:17 +02:00
|
|
|
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
|
2013-08-07 00:27:51 +02:00
|
|
|
char *nick;
|
2013-08-04 10:42:17 +02:00
|
|
|
nick = strchr(cmd, ' ');
|
2013-08-07 00:27:51 +02:00
|
|
|
if (nick == NULL) {
|
2013-08-04 10:42:17 +02:00
|
|
|
wprintw(ctx->history, "Invalid syntax.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nick++;
|
|
|
|
setname((uint8_t*) nick, strlen(nick)+1);
|
|
|
|
wprintw(ctx->history, "Nickname set to: %s\n", nick);
|
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
|
|
|
else if (!strcmp(cmd, "/myid")) {
|
2013-08-08 10:51:58 +02:00
|
|
|
char id[KEY_SIZE_BYTES*2+1] = {0};
|
2013-08-04 10:42:17 +02:00
|
|
|
int i;
|
2013-08-08 10:51:58 +02:00
|
|
|
for (i = 0; i < KEY_SIZE_BYTES; i++) {
|
2013-08-04 10:42:17 +02:00
|
|
|
char xx[3];
|
|
|
|
snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
|
|
|
|
strcat(id, xx);
|
|
|
|
}
|
|
|
|
wprintw(ctx->history, "Your ID: %s\n", id);
|
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-05 07:57:29 +02:00
|
|
|
else if (strcmp(ctx->line, "/close") == 0) {
|
2013-08-07 00:27:51 +02:00
|
|
|
active_window = 0; // Go to prompt screen
|
2013-08-05 07:57:29 +02:00
|
|
|
int f_num = ctx->friendnum;
|
|
|
|
delwin(ctx->linewin);
|
|
|
|
del_window(self, f_num);
|
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-04 10:42:17 +02:00
|
|
|
else
|
|
|
|
wprintw(ctx->history, "Invalid command.\n");
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
static void chat_onDraw(ToxWindow *self)
|
|
|
|
{
|
2013-08-03 03:36:01 +02:00
|
|
|
curs_set(1);
|
2013-07-31 19:20:03 +02:00
|
|
|
int x, y;
|
2013-08-07 00:27:51 +02:00
|
|
|
ChatContext *ctx = (ChatContext*) self->x;
|
2013-07-31 19:20:03 +02:00
|
|
|
getmaxyx(self->window, y, x);
|
|
|
|
(void) x;
|
2013-08-07 00:27:51 +02:00
|
|
|
if (y < 3) return;
|
2013-07-31 19:20:03 +02:00
|
|
|
|
|
|
|
wclear(ctx->linewin);
|
|
|
|
mvwhline(ctx->linewin, 0, 0, '_', COLS);
|
2013-08-03 03:36:01 +02:00
|
|
|
mvwprintw(self->window, y-1, 0, "%s\n", ctx->line);
|
2013-07-31 19:20:03 +02:00
|
|
|
wrefresh(self->window);
|
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
static void chat_onInit(ToxWindow *self)
|
|
|
|
{
|
2013-07-31 19:20:03 +02:00
|
|
|
int x, y;
|
2013-08-07 00:27:51 +02:00
|
|
|
ChatContext *ctx = (ChatContext*) self->x;
|
2013-07-31 19:20:03 +02:00
|
|
|
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 - 3, 0);
|
2013-08-05 22:34:55 +02:00
|
|
|
print_help(ctx);
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
void print_help(ChatContext *self)
|
|
|
|
{
|
2013-08-04 10:42:17 +02:00
|
|
|
wattron(self->history, COLOR_PAIR(2) | A_BOLD);
|
2013-08-05 22:34:55 +02:00
|
|
|
wprintw(self->history, "Commands:\n");
|
2013-08-04 10:42:17 +02:00
|
|
|
wattroff(self->history, A_BOLD);
|
|
|
|
|
2013-08-08 12:22:48 +02:00
|
|
|
wprintw(self->history, " /status <type> <message> : Set your status\n");
|
2013-08-04 10:42:17 +02:00
|
|
|
wprintw(self->history, " /nick <nickname> : Set your nickname\n");
|
|
|
|
wprintw(self->history, " /myid : Print your ID\n");
|
|
|
|
wprintw(self->history, " /clear : Clear the screen\n");
|
2013-08-05 22:04:06 +02:00
|
|
|
wprintw(self->history, " /close : Close the current chat window\n");
|
2013-08-04 10:42:17 +02:00
|
|
|
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-07 00:27:51 +02:00
|
|
|
ToxWindow new_chat(int friendnum)
|
|
|
|
{
|
2013-07-31 19:20:03 +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;
|
|
|
|
|
2013-08-01 06:32:54 +02:00
|
|
|
uint8_t nick[MAX_NAME_LENGTH] = {0};
|
|
|
|
getname(friendnum, (uint8_t*) &nick);
|
|
|
|
fix_name(nick);
|
2013-08-03 16:00:48 +02:00
|
|
|
|
2013-08-01 06:32:54 +02:00
|
|
|
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
|
2013-07-31 19:20:03 +02:00
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
ChatContext *x = calloc(1, sizeof(ChatContext));
|
2013-07-31 19:20:03 +02:00
|
|
|
x->friendnum = friendnum;
|
|
|
|
ret.x = (void*) x;
|
|
|
|
return ret;
|
|
|
|
}
|