2013-08-01 02:20:16 +08:00
|
|
|
/*
|
|
|
|
* Toxic -- Tox Curses Client
|
|
|
|
*/
|
|
|
|
|
2013-08-01 01:20:03 +08:00
|
|
|
#include <curses.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <ctype.h>
|
2013-08-03 07:44:32 +08:00
|
|
|
#include <time.h>
|
2013-08-01 01:20:03 +08:00
|
|
|
|
|
|
|
#include "../../core/Messenger.h"
|
|
|
|
#include "../../core/network.h"
|
|
|
|
|
|
|
|
#include "windows.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int friendnum;
|
|
|
|
|
|
|
|
char line[256];
|
|
|
|
size_t pos;
|
|
|
|
|
|
|
|
WINDOW* history;
|
|
|
|
WINDOW* linewin;
|
|
|
|
|
|
|
|
} ChatContext;
|
|
|
|
|
|
|
|
extern void fix_name(uint8_t* name);
|
|
|
|
|
|
|
|
static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) {
|
|
|
|
ChatContext* ctx = (ChatContext*) self->x;
|
|
|
|
uint8_t nick[MAX_NAME_LENGTH] = {0};
|
|
|
|
|
2013-08-03 07:44:32 +08:00
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
struct tm * timeinfo;
|
|
|
|
timeinfo = localtime(&now);
|
|
|
|
|
2013-08-01 01:20:03 +08:00
|
|
|
if(ctx->friendnum != num)
|
|
|
|
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 07:44:32 +08:00
|
|
|
wattron(ctx->history, COLOR_PAIR(2));
|
2013-08-03 22:26:23 +08:00
|
|
|
wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
2013-08-03 07:44:32 +08:00
|
|
|
wattron(ctx->history, COLOR_PAIR(4));
|
2013-08-01 02:20:16 +08:00
|
|
|
wprintw(ctx->history, "%s: ", nick);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(4));
|
|
|
|
wprintw(ctx->history, "%s\n", msg);
|
|
|
|
|
|
|
|
self->blink = true;
|
2013-08-01 01:20:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) {
|
|
|
|
ChatContext* ctx = (ChatContext*) self->x;
|
|
|
|
|
|
|
|
if(ctx->friendnum != num)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nick[len-1] = '\0';
|
|
|
|
fix_name(nick);
|
|
|
|
|
2013-08-01 12:32:54 +08:00
|
|
|
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
|
|
|
|
|
2013-08-01 01:20:03 +08:00
|
|
|
wattron(ctx->history, COLOR_PAIR(3));
|
|
|
|
wprintw(ctx->history, " * Your partner changed nick to '%s'\n", nick);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-04 05:29:18 +08: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-01 01:20:03 +08:00
|
|
|
static void chat_onKey(ToxWindow* self, int key) {
|
|
|
|
ChatContext* ctx = (ChatContext*) self->x;
|
|
|
|
|
2013-08-03 07:44:32 +08:00
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
struct tm * timeinfo;
|
|
|
|
timeinfo = localtime(&now);
|
|
|
|
|
2013-08-01 01:20:03 +08:00
|
|
|
if(isprint(key)) {
|
|
|
|
|
|
|
|
if(ctx->pos != sizeof(ctx->line)-1) {
|
|
|
|
ctx->line[ctx->pos++] = key;
|
|
|
|
ctx->line[ctx->pos] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2013-08-04 05:29:18 +08:00
|
|
|
|
2013-08-01 01:20:03 +08:00
|
|
|
else if(key == '\n') {
|
2013-08-04 05:29:18 +08:00
|
|
|
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);
|
|
|
|
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);
|
|
|
|
|
|
|
|
if(m_sendmessage(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;
|
2013-08-01 01:20:03 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-04 05:29:18 +08:00
|
|
|
|
2013-08-01 03:22:43 +08:00
|
|
|
else if(key == 0x107 || key == 0x8 || key == 0x7f) {
|
2013-08-01 02:20:16 +08:00
|
|
|
if(ctx->pos != 0) {
|
|
|
|
ctx->line[--ctx->pos] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2013-08-03 22:00:48 +08:00
|
|
|
|
2013-08-01 01:20:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void chat_onDraw(ToxWindow* self) {
|
2013-08-03 09:36:01 +08:00
|
|
|
curs_set(1);
|
2013-08-01 01:20:03 +08:00
|
|
|
int x, y;
|
|
|
|
ChatContext* ctx = (ChatContext*) self->x;
|
|
|
|
|
|
|
|
getmaxyx(self->window, y, x);
|
|
|
|
|
|
|
|
(void) x;
|
|
|
|
if(y < 3)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wclear(ctx->linewin);
|
|
|
|
mvwhline(ctx->linewin, 0, 0, '_', COLS);
|
2013-08-03 09:36:01 +08:00
|
|
|
mvwprintw(self->window, y-1, 0, "%s\n", ctx->line);
|
2013-08-01 01:20:03 +08:00
|
|
|
|
|
|
|
wrefresh(self->window);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void chat_onInit(ToxWindow* self) {
|
|
|
|
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 - 3, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ToxWindow new_chat(int friendnum) {
|
|
|
|
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 12:32:54 +08:00
|
|
|
uint8_t nick[MAX_NAME_LENGTH] = {0};
|
|
|
|
getname(friendnum, (uint8_t*) &nick);
|
|
|
|
fix_name(nick);
|
2013-08-03 22:00:48 +08:00
|
|
|
|
2013-08-01 12:32:54 +08:00
|
|
|
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
|
2013-08-01 01:20:03 +08:00
|
|
|
|
|
|
|
ChatContext* x = calloc(1, sizeof(ChatContext));
|
|
|
|
x->friendnum = friendnum;
|
|
|
|
|
|
|
|
ret.x = (void*) x;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|