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>
|
2013-08-03 01:44:32 +02:00
|
|
|
#include <time.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"
|
2013-09-19 23:03:22 +02:00
|
|
|
#include "commands.h"
|
2013-09-21 02:35:03 +02:00
|
|
|
#include "misc_tools.h"
|
2013-07-31 19:20:03 +02:00
|
|
|
|
2013-09-04 08:05:36 +02:00
|
|
|
extern char *DATA_FILE;
|
|
|
|
extern int store_data(Tox *m, char *path);
|
|
|
|
|
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-09-15 22:38:38 +02:00
|
|
|
if (self->num != num)
|
2013-09-06 00:24:58 +02:00
|
|
|
return;
|
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-06 00:24:58 +02:00
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
|
2013-09-12 03:44:39 +02:00
|
|
|
tox_getname(m, num, nick);
|
2013-10-19 05:08:37 +02:00
|
|
|
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-10-11 07:11:43 +02:00
|
|
|
print_time(ctx->history);
|
2013-08-16 19:11:09 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(4));
|
|
|
|
wprintw(ctx->history, "%s: ", nick);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(4));
|
2013-09-26 06:33:51 +02:00
|
|
|
|
|
|
|
if (msg[0] == '>') {
|
|
|
|
wattron(ctx->history, COLOR_PAIR(GREEN));
|
|
|
|
wprintw(ctx->history, "%s\n", msg);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(GREEN));
|
|
|
|
} else
|
|
|
|
wprintw(ctx->history, "%s\n", msg);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
self->blink = true;
|
|
|
|
beep();
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-09-21 02:35:03 +02:00
|
|
|
static void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status)
|
2013-09-05 03:25:59 +02:00
|
|
|
{
|
2013-09-15 22:38:38 +02:00
|
|
|
if (self->num != num)
|
2013-09-05 03:25:59 +02:00
|
|
|
return;
|
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) self->stb;
|
2013-09-06 00:24:58 +02:00
|
|
|
statusbar->is_online = status == 1 ? true : false;
|
2013-09-05 03:25:59 +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-09-15 22:38:38 +02:00
|
|
|
if (self->num != num)
|
2013-09-06 00:24:58 +02:00
|
|
|
return;
|
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
2013-08-08 21:01:33 +02:00
|
|
|
|
2013-09-06 00:24:58 +02:00
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
|
2013-09-12 03:44:39 +02:00
|
|
|
tox_getname(m, num, nick);
|
2013-10-19 05:08:37 +02:00
|
|
|
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
|
2013-08-31 08:22:07 +02:00
|
|
|
|
2013-10-11 07:11:43 +02:00
|
|
|
print_time(ctx->history);
|
2013-09-05 06:47:33 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(YELLOW));
|
2013-08-31 08:22:07 +02:00
|
|
|
wprintw(ctx->history, "* %s %s\n", nick, action);
|
2013-09-05 06:47:33 +02:00
|
|
|
wattroff(ctx->history, COLOR_PAIR(YELLOW));
|
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-09-09 07:08:06 +02:00
|
|
|
static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-09-15 22:38:38 +02:00
|
|
|
if (self->num != num)
|
2013-08-16 19:11:09 +02:00
|
|
|
return;
|
|
|
|
|
2013-10-19 05:08:37 +02:00
|
|
|
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
|
|
|
|
len = strlen(nick) + 1;
|
2013-09-23 07:22:21 +02:00
|
|
|
memcpy(self->name, nick, len);
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-09-05 03:25:59 +02:00
|
|
|
static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-09-15 22:38:38 +02:00
|
|
|
if (self->num != num)
|
2013-08-16 19:11:09 +02:00
|
|
|
return;
|
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) self->stb;
|
2013-09-06 00:24:58 +02:00
|
|
|
statusbar->status = status;
|
2013-09-05 03:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
|
|
|
|
{
|
2013-09-15 22:38:38 +02:00
|
|
|
if (self->num != num)
|
2013-09-05 03:25:59 +02:00
|
|
|
return;
|
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) self->stb;
|
2013-09-10 10:04:13 +02:00
|
|
|
statusbar->statusmsg_len = len;
|
2013-09-23 07:22:21 +02:00
|
|
|
memcpy(statusbar->statusmsg, status, len);
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-10-11 10:42:30 +02:00
|
|
|
static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum,
|
2013-10-14 01:09:20 +02:00
|
|
|
uint64_t filesize, uint8_t *pathname, uint16_t path_len)
|
2013-10-11 06:23:39 +02:00
|
|
|
{
|
2013-10-17 12:13:28 +02:00
|
|
|
if (self->num != num)
|
2013-10-11 06:23:39 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
|
|
|
|
2013-10-17 12:13:28 +02:00
|
|
|
/* try to get file name from path */
|
|
|
|
uint8_t *filename = strrchr(pathname, '/'); // Try unix style paths
|
|
|
|
|
|
|
|
if (filename != NULL) {
|
2013-10-18 01:53:29 +02:00
|
|
|
if (!strlen(++filename))
|
|
|
|
filename = pathname;
|
2013-10-17 12:13:28 +02:00
|
|
|
} else {
|
|
|
|
filename = strrchr(pathname, '\\'); // Try windows style paths
|
|
|
|
|
|
|
|
if (filename == NULL)
|
|
|
|
filename = pathname;
|
|
|
|
}
|
|
|
|
|
|
|
|
wprintw(ctx->history, "File transfer request for '%s' (%llu bytes).\n", filename,
|
2013-10-11 10:42:30 +02:00
|
|
|
(long long unsigned int)filesize);
|
2013-10-11 06:23:39 +02:00
|
|
|
|
2013-10-18 07:35:08 +02:00
|
|
|
if (filenum >= MAX_FILES) {
|
2013-10-11 06:23:39 +02:00
|
|
|
wprintw(ctx->history, "Too many pending file requests; discarding.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-18 04:20:40 +02:00
|
|
|
/* Append current time to duplicate file names */
|
|
|
|
FILE *filecheck = NULL;
|
|
|
|
|
|
|
|
if ((filecheck = fopen(filename, "r"))) {
|
|
|
|
struct tm *timeinfo = get_time();
|
|
|
|
uint8_t cur_time[MAX_STR_SIZE];
|
|
|
|
snprintf(cur_time, sizeof(cur_time), ".%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min,
|
|
|
|
timeinfo->tm_sec);
|
2013-10-19 04:18:48 +02:00
|
|
|
strncat(filename, cur_time, MAX_STR_SIZE - strlen(filename));
|
2013-10-18 04:20:40 +02:00
|
|
|
fclose(filecheck);
|
|
|
|
}
|
|
|
|
|
2013-10-14 01:09:20 +02:00
|
|
|
wprintw(ctx->history, "Type '/savefile %d' to accept the file transfer.\n", filenum);
|
2013-10-11 06:23:39 +02:00
|
|
|
|
2013-10-18 07:35:08 +02:00
|
|
|
friends[num].file_receiver.pending[filenum] = true;
|
2013-10-18 01:53:29 +02:00
|
|
|
strcpy(friends[num].file_receiver.filenames[filenum], filename);
|
2013-10-11 06:23:39 +02:00
|
|
|
|
|
|
|
self->blink = true;
|
|
|
|
beep();
|
|
|
|
}
|
|
|
|
|
2013-10-11 10:42:30 +02:00
|
|
|
static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive_send,
|
|
|
|
uint8_t filenum, uint8_t control_type, uint8_t *data, uint16_t length)
|
2013-10-11 06:23:39 +02:00
|
|
|
{
|
|
|
|
if (self->num != num)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
2013-10-18 04:20:40 +02:00
|
|
|
uint8_t *filename;
|
2013-10-11 06:23:39 +02:00
|
|
|
|
2013-10-18 04:20:40 +02:00
|
|
|
if (receive_send == 0)
|
|
|
|
filename = friends[num].file_receiver.filenames[filenum];
|
|
|
|
else
|
|
|
|
filename = file_senders[filenum].pathname;
|
2013-10-18 01:53:29 +02:00
|
|
|
|
2013-10-11 06:23:39 +02:00
|
|
|
switch(control_type) {
|
2013-10-17 12:13:28 +02:00
|
|
|
case TOX_FILECONTROL_ACCEPT:
|
2013-10-18 01:53:29 +02:00
|
|
|
wprintw(ctx->history, "File transfer for '%s' accepted.\n", filename);
|
2013-10-11 06:23:39 +02:00
|
|
|
break;
|
2013-10-17 12:13:28 +02:00
|
|
|
case TOX_FILECONTROL_PAUSE:
|
2013-10-18 01:53:29 +02:00
|
|
|
wprintw(ctx->history, "File transfer for '%s' paused.\n", filename);
|
2013-10-17 12:13:28 +02:00
|
|
|
break;
|
|
|
|
case TOX_FILECONTROL_KILL:
|
2013-10-18 01:53:29 +02:00
|
|
|
wprintw(ctx->history, "File transfer for '%s' failed.\n", filename);
|
2013-10-11 06:23:39 +02:00
|
|
|
break;
|
2013-10-17 12:13:28 +02:00
|
|
|
case TOX_FILECONTROL_FINISHED:
|
2013-10-18 01:53:29 +02:00
|
|
|
wprintw(ctx->history, "File transfer for '%s' complete.\n", filename);
|
2013-10-11 06:23:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->blink = true;
|
|
|
|
beep();
|
|
|
|
}
|
|
|
|
|
2013-10-11 10:42:30 +02:00
|
|
|
static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, uint8_t *data,
|
|
|
|
uint16_t length)
|
2013-10-11 06:23:39 +02:00
|
|
|
{
|
|
|
|
if (self->num != num)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
|
|
|
|
2013-10-18 01:53:29 +02:00
|
|
|
uint8_t *filename = friends[num].file_receiver.filenames[filenum];
|
2013-10-17 12:13:28 +02:00
|
|
|
FILE *file_to_save = fopen(filename, "a");
|
2013-10-11 06:23:39 +02:00
|
|
|
|
|
|
|
if (fwrite(data, length, 1, file_to_save) != 1) {
|
|
|
|
wattron(ctx->history, COLOR_PAIR(RED));
|
|
|
|
wprintw(ctx->history, "* Error writing to file.\n");
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(RED));
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file_to_save);
|
|
|
|
}
|
|
|
|
|
2013-10-14 01:09:20 +02:00
|
|
|
static void chat_groupinvite(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *line)
|
|
|
|
{
|
|
|
|
int groupnum = atoi(line);
|
|
|
|
|
|
|
|
if (groupnum == 0 && strcmp(line, "0")) { /* atoi returns 0 value on invalid input */
|
|
|
|
wprintw(ctx->history, "Invalid syntax.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tox_invite_friend(m, self->num, groupnum) == -1) {
|
|
|
|
wprintw(ctx->history, "Failed to invite friend.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wprintw(ctx->history, "Invited friend to group chat %d.\n", groupnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void chat_sendfile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *path)
|
|
|
|
{
|
2013-10-18 07:35:08 +02:00
|
|
|
if (num_file_senders >= MAX_FILES) {
|
|
|
|
wprintw(ctx->history,"Please wait for some of your outgoing file transfers to complete.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-14 01:09:20 +02:00
|
|
|
int path_len = strlen(path);
|
|
|
|
|
|
|
|
if (path_len > MAX_STR_SIZE) {
|
|
|
|
wprintw(ctx->history, "File path exceeds character limit.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *file_to_send = fopen(path, "r");
|
|
|
|
|
|
|
|
if (file_to_send == NULL) {
|
|
|
|
wprintw(ctx->history, "File '%s' not found.\n", path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(file_to_send, 0, SEEK_END);
|
|
|
|
uint64_t filesize = ftell(file_to_send);
|
|
|
|
fseek(file_to_send, 0, SEEK_SET);
|
|
|
|
|
|
|
|
int friendnum = self->num;
|
|
|
|
int filenum = tox_new_filesender(m, friendnum, filesize, path, path_len + 1);
|
|
|
|
|
|
|
|
if (filenum == -1) {
|
|
|
|
wprintw(ctx->history, "Error sending file.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-18 07:35:08 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_FILES; ++i) {
|
|
|
|
if (!file_senders[i].active) {
|
|
|
|
memcpy(file_senders[i].pathname, path, path_len + 1);
|
|
|
|
file_senders[i].active = true;
|
|
|
|
file_senders[i].chatwin = ctx->history;
|
|
|
|
file_senders[i].file = file_to_send;
|
|
|
|
file_senders[i].filenum = (uint8_t) filenum;
|
|
|
|
file_senders[i].friendnum = friendnum;
|
|
|
|
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
|
|
|
|
tox_filedata_size(m, friendnum), file_to_send);
|
2013-10-14 01:09:20 +02:00
|
|
|
|
2013-10-18 07:35:08 +02:00
|
|
|
wprintw(ctx->history, "Sending file: '%s'\n", path);
|
|
|
|
|
|
|
|
if (i == num_file_senders)
|
|
|
|
++num_file_senders;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-10-14 01:09:20 +02:00
|
|
|
}
|
|
|
|
|
2013-10-18 01:53:29 +02:00
|
|
|
static void chat_savefile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *num)
|
|
|
|
{
|
|
|
|
uint8_t filenum = atoi(num);
|
|
|
|
|
2013-10-18 07:35:08 +02:00
|
|
|
if (filenum < 0 || filenum >= MAX_FILES) {
|
|
|
|
wprintw(ctx->history, "No pending file transfers with that number.\n");
|
2013-10-18 01:53:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int friendnum = self->num;
|
2013-10-18 07:35:08 +02:00
|
|
|
|
|
|
|
if (!friends[friendnum].file_receiver.pending[filenum]) {
|
|
|
|
wprintw(ctx->history, "No pending file transfers with that number.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-18 01:53:29 +02:00
|
|
|
uint8_t *filename = friends[friendnum].file_receiver.filenames[filenum];
|
|
|
|
|
|
|
|
if (tox_file_sendcontrol(m, friendnum, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0))
|
|
|
|
wprintw(ctx->history, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename);
|
|
|
|
else
|
|
|
|
wprintw(ctx->history, "File transfer failed.\n");
|
2013-10-18 07:35:08 +02:00
|
|
|
|
|
|
|
friends[friendnum].file_receiver.pending[filenum] = false;
|
2013-10-18 01:53:29 +02:00
|
|
|
}
|
|
|
|
|
2013-09-19 12:37:42 +02:00
|
|
|
static void print_chat_help(ChatContext *ctx)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-09-19 12:37:42 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
|
|
|
wprintw(ctx->history, "Chat commands:\n");
|
|
|
|
wattroff(ctx->history, A_BOLD);
|
|
|
|
|
|
|
|
wprintw(ctx->history, " /status <type> <message> : Set your status with optional note\n");
|
|
|
|
wprintw(ctx->history, " /note <message> : Set a personal note\n");
|
|
|
|
wprintw(ctx->history, " /nick <nickname> : Set your nickname\n");
|
2013-10-14 01:09:20 +02:00
|
|
|
wprintw(ctx->history, " /invite <n> : Invite friend to a groupchat\n");
|
2013-09-19 12:37:42 +02:00
|
|
|
wprintw(ctx->history, " /me <action> : Do an action\n");
|
|
|
|
wprintw(ctx->history, " /myid : Print your ID\n");
|
|
|
|
wprintw(ctx->history, " /clear : Clear the screen\n");
|
|
|
|
wprintw(ctx->history, " /close : Close the current chat window\n");
|
2013-10-14 01:09:20 +02:00
|
|
|
wprintw(ctx->history, " /sendfile <filepath> : Send a file\n");
|
|
|
|
wprintw(ctx->history, " /savefile <n> : Receive a file\n");
|
2013-09-19 12:37:42 +02:00
|
|
|
wprintw(ctx->history, " /quit or /exit : Exit Toxic\n");
|
|
|
|
wprintw(ctx->history, " /help : Print this message again\n");
|
|
|
|
|
|
|
|
wattron(ctx->history, A_BOLD);
|
2013-09-24 22:18:22 +02:00
|
|
|
wprintw(ctx->history, "\n * Argument messages must be enclosed in quotation marks.\n");
|
2013-09-19 12:37:42 +02:00
|
|
|
wattroff(ctx->history, A_BOLD);
|
|
|
|
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(CYAN));
|
2013-08-04 10:42:17 +02:00
|
|
|
}
|
2013-08-03 16:00:48 +02:00
|
|
|
|
2013-09-19 12:37:42 +02:00
|
|
|
static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *action) {
|
|
|
|
if (action == NULL) {
|
|
|
|
wprintw(ctx->history, "Invalid syntax.\n");
|
|
|
|
return;
|
2013-09-02 07:14:51 +02:00
|
|
|
}
|
|
|
|
|
2013-09-19 12:37:42 +02:00
|
|
|
uint8_t selfname[TOX_MAX_NAME_LENGTH];
|
|
|
|
tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-10-11 07:11:43 +02:00
|
|
|
print_time(ctx->history);
|
2013-09-19 12:37:42 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(YELLOW));
|
|
|
|
wprintw(ctx->history, "* %s %s\n", selfname, action);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(YELLOW));
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-19 12:37:42 +02:00
|
|
|
if (tox_sendaction(m, self->num, action, strlen(action) + 1) == 0) {
|
|
|
|
wattron(ctx->history, COLOR_PAIR(RED));
|
|
|
|
wprintw(ctx->history, " * Failed to send action\n");
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(RED));
|
2013-08-04 10:42:17 +02:00
|
|
|
}
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-09-10 10:04:13 +02:00
|
|
|
static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
|
|
|
|
{
|
2013-09-13 08:02:49 +02:00
|
|
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
|
|
|
StatusBar *statusbar = (StatusBar *) self->stb;
|
2013-09-10 10:04:13 +02:00
|
|
|
|
|
|
|
int x, y, y2, x2;
|
|
|
|
getyx(self->window, y, x);
|
|
|
|
getmaxyx(self->window, y2, x2);
|
2013-09-26 13:52:17 +02:00
|
|
|
/* BACKSPACE key: Remove one character from line */
|
|
|
|
if (key == 0x107 || key == 0x8 || key == 0x7f) {
|
|
|
|
if (ctx->pos > 0) {
|
|
|
|
ctx->line[--ctx->pos] = L'\0';
|
2013-09-10 10:04:13 +02:00
|
|
|
|
2013-09-26 13:52:17 +02:00
|
|
|
if (x == 0)
|
|
|
|
mvwdelch(self->window, y - 1, x2 - 1);
|
|
|
|
else
|
|
|
|
mvwdelch(self->window, y, x - 1);
|
|
|
|
}
|
|
|
|
} else
|
2013-09-10 10:04:13 +02:00
|
|
|
/* Add printable chars to buffer and print on input space */
|
|
|
|
#if HAVE_WIDECHAR
|
|
|
|
if (iswprint(key)) {
|
|
|
|
#else
|
|
|
|
if (isprint(key)) {
|
|
|
|
#endif
|
|
|
|
if (ctx->pos < (MAX_STR_SIZE-1)) {
|
|
|
|
mvwaddstr(self->window, y, x, wc_to_char(key));
|
|
|
|
ctx->line[ctx->pos++] = key;
|
|
|
|
ctx->line[ctx->pos] = L'\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* RETURN key: Execute command or print line */
|
|
|
|
else if (key == '\n') {
|
2013-09-12 03:44:39 +02:00
|
|
|
uint8_t *line = wcs_to_char(ctx->line);
|
2013-10-20 07:27:45 +02:00
|
|
|
line[ctx->pos+1] = L'\0';
|
2013-09-10 10:04:13 +02:00
|
|
|
wclear(ctx->linewin);
|
|
|
|
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
|
|
|
|
wclrtobot(self->window);
|
|
|
|
bool close_win = false;
|
|
|
|
if (line[0] == '/') {
|
|
|
|
if (close_win = !strncmp(line, "/close", strlen("/close"))) {
|
2013-09-15 22:38:38 +02:00
|
|
|
int f_num = self->num;
|
2013-09-10 10:04:13 +02:00
|
|
|
delwin(ctx->linewin);
|
|
|
|
delwin(statusbar->topline);
|
|
|
|
del_window(self);
|
|
|
|
disable_chatwin(f_num);
|
2013-09-19 12:37:42 +02:00
|
|
|
} else if (!strncmp(line, "/me ", strlen("/me ")))
|
2013-10-14 01:09:20 +02:00
|
|
|
send_action(self, ctx, m, line + strlen("/me "));
|
2013-09-19 12:37:42 +02:00
|
|
|
else if (!strncmp(line, "/help", strlen("/help")))
|
|
|
|
print_chat_help(ctx);
|
2013-10-14 01:09:20 +02:00
|
|
|
else if (!strncmp(line, "/invite", strlen("/invite")))
|
|
|
|
chat_groupinvite(self, ctx, m, line + strlen("/invite "));
|
|
|
|
else if(!strncmp(line, "/sendfile ", strlen("/sendfile ")))
|
|
|
|
chat_sendfile(self, ctx, m, line + strlen("/sendfile "));
|
2013-10-18 01:53:29 +02:00
|
|
|
else if(!strncmp(line, "/savefile ", strlen("/savefile ")))
|
|
|
|
chat_savefile(self, ctx, m, line + strlen("/savefile "));
|
2013-09-19 12:37:42 +02:00
|
|
|
else
|
2013-10-20 07:27:45 +02:00
|
|
|
execute(ctx->history, self->prompt, m, line);
|
2013-09-10 10:04:13 +02:00
|
|
|
} else {
|
|
|
|
/* make sure the string has at least non-space character */
|
|
|
|
if (!string_is_empty(line)) {
|
|
|
|
uint8_t selfname[TOX_MAX_NAME_LENGTH];
|
|
|
|
tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH);
|
|
|
|
|
2013-10-11 07:11:43 +02:00
|
|
|
print_time(ctx->history);
|
2013-09-10 10:04:13 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(GREEN));
|
|
|
|
wprintw(ctx->history, "%s: ", selfname);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(GREEN));
|
2013-09-26 06:33:51 +02:00
|
|
|
|
|
|
|
if (line[0] == '>') {
|
|
|
|
wattron(ctx->history, COLOR_PAIR(GREEN));
|
|
|
|
wprintw(ctx->history, "%s\n", line);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(GREEN));
|
|
|
|
} else
|
|
|
|
wprintw(ctx->history, "%s\n", line);
|
2013-09-10 10:04:13 +02:00
|
|
|
|
|
|
|
if (!statusbar->is_online
|
2013-09-15 22:38:38 +02:00
|
|
|
|| tox_sendmessage(m, self->num, line, strlen(line) + 1) == 0) {
|
2013-09-10 10:04:13 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(RED));
|
|
|
|
wprintw(ctx->history, " * Failed to send message.\n");
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(RED));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (close_win) {
|
|
|
|
free(ctx);
|
|
|
|
free(statusbar);
|
|
|
|
} else {
|
|
|
|
ctx->line[0] = L'\0';
|
|
|
|
ctx->pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2013-09-06 00:24:58 +02:00
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
2013-09-10 10:04:13 +02:00
|
|
|
|
2013-09-06 00:24:58 +02:00
|
|
|
/* Draw status bar */
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) self->stb;
|
2013-09-06 00:24:58 +02:00
|
|
|
mvwhline(statusbar->topline, 1, 0, '-', x);
|
|
|
|
wmove(statusbar->topline, 0, 0);
|
|
|
|
|
|
|
|
/* Draw name, status and note in statusbar */
|
|
|
|
if (statusbar->is_online) {
|
|
|
|
char *status_text = "Unknown";
|
|
|
|
int colour = WHITE;
|
|
|
|
|
|
|
|
TOX_USERSTATUS status = statusbar->status;
|
|
|
|
|
|
|
|
switch(status) {
|
|
|
|
case TOX_USERSTATUS_NONE:
|
|
|
|
status_text = "Online";
|
|
|
|
colour = GREEN;
|
|
|
|
break;
|
|
|
|
case TOX_USERSTATUS_AWAY:
|
|
|
|
status_text = "Away";
|
|
|
|
colour = YELLOW;
|
|
|
|
break;
|
|
|
|
case TOX_USERSTATUS_BUSY:
|
|
|
|
status_text = "Busy";
|
|
|
|
colour = RED;
|
|
|
|
break;
|
|
|
|
}
|
2013-09-07 01:59:45 +02:00
|
|
|
|
2013-09-06 00:24:58 +02:00
|
|
|
wattron(statusbar->topline, A_BOLD);
|
2013-09-06 06:56:55 +02:00
|
|
|
wprintw(statusbar->topline, " %s ", self->name);
|
2013-09-06 00:24:58 +02:00
|
|
|
wattroff(statusbar->topline, A_BOLD);
|
|
|
|
wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
|
2013-09-06 06:56:55 +02:00
|
|
|
wprintw(statusbar->topline, "[%s]", status_text);
|
2013-09-06 00:24:58 +02:00
|
|
|
wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
|
|
|
|
} else {
|
|
|
|
wattron(statusbar->topline, A_BOLD);
|
2013-09-06 06:56:55 +02:00
|
|
|
wprintw(statusbar->topline, " %s ", self->name);
|
2013-09-06 00:24:58 +02:00
|
|
|
wattroff(statusbar->topline, A_BOLD);
|
2013-09-06 06:56:55 +02:00
|
|
|
wprintw(statusbar->topline, "[Offline]");
|
2013-09-06 00:24:58 +02:00
|
|
|
}
|
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
/* Reset statusbar->statusmsg on window resize */
|
|
|
|
if (x != self->x) {
|
|
|
|
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
|
2013-09-15 22:38:38 +02:00
|
|
|
tox_copy_statusmessage(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
|
2013-09-13 08:02:49 +02:00
|
|
|
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
2013-09-15 22:38:38 +02:00
|
|
|
statusbar->statusmsg_len = tox_get_statusmessage_size(m, self->num);
|
2013-09-13 08:02:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self->x = x;
|
|
|
|
|
2013-09-10 10:04:13 +02:00
|
|
|
/* Truncate note if it doesn't fit in statusbar */
|
2013-09-13 08:02:49 +02:00
|
|
|
uint16_t maxlen = x - getcurx(statusbar->topline) - 6;
|
2013-09-10 10:04:13 +02:00
|
|
|
if (statusbar->statusmsg_len > maxlen) {
|
|
|
|
statusbar->statusmsg[maxlen] = '\0';
|
|
|
|
statusbar->statusmsg_len = maxlen;
|
|
|
|
}
|
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
if (statusbar->statusmsg[0]) {
|
|
|
|
wattron(statusbar->topline, A_BOLD);
|
|
|
|
wprintw(statusbar->topline, " | %s | ", statusbar->statusmsg);
|
|
|
|
wattroff(statusbar->topline, A_BOLD);
|
|
|
|
}
|
2013-09-06 00:24:58 +02:00
|
|
|
|
|
|
|
wprintw(statusbar->topline, "\n");
|
2013-08-16 19:11:09 +02:00
|
|
|
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;
|
|
|
|
getmaxyx(self->window, y, x);
|
2013-09-13 08:02:49 +02:00
|
|
|
self->x = x;
|
2013-09-06 00:24:58 +02:00
|
|
|
|
|
|
|
/* Init statusbar info */
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) self->stb;
|
2013-09-15 22:38:38 +02:00
|
|
|
statusbar->status = tox_get_userstatus(m, self->num);
|
|
|
|
statusbar->is_online = tox_get_friend_connectionstatus(m, self->num) == 1;
|
2013-09-06 00:24:58 +02:00
|
|
|
|
2013-09-08 09:18:34 +02:00
|
|
|
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
|
2013-09-15 22:38:38 +02:00
|
|
|
tox_copy_statusmessage(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
|
2013-09-08 09:18:34 +02:00
|
|
|
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
2013-09-15 22:38:38 +02:00
|
|
|
statusbar->statusmsg_len = tox_get_statusmessage_size(m, self->num);
|
2013-09-06 00:24:58 +02:00
|
|
|
|
|
|
|
/* Init subwindows */
|
2013-09-13 08:02:49 +02:00
|
|
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
2013-09-06 00:24:58 +02:00
|
|
|
statusbar->topline = subwin(self->window, 2, x, 0, 0);
|
|
|
|
ctx->history = subwin(self->window, y-3, x, 0, 0);
|
2013-08-16 19:11:09 +02:00
|
|
|
scrollok(ctx->history, 1);
|
2013-09-06 00:24:58 +02:00
|
|
|
ctx->linewin = subwin(self->window, 0, x, y-4, 0);
|
2013-09-06 00:36:46 +02:00
|
|
|
wprintw(ctx->history, "\n\n");
|
2013-09-19 12:37:42 +02:00
|
|
|
print_chat_help(ctx);
|
2013-08-16 19:11:09 +02:00
|
|
|
wmove(self->window, y - CURS_Y_OFFSET, 0);
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 01:59:45 +02:00
|
|
|
ToxWindow new_chat(Tox *m, ToxWindow *prompt, 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;
|
2013-09-05 03:25:59 +02:00
|
|
|
ret.onConnectionChange = &chat_onConnectionChange;
|
2013-08-16 19:11:09 +02:00
|
|
|
ret.onNickChange = &chat_onNickChange;
|
2013-09-05 03:25:59 +02:00
|
|
|
ret.onStatusChange = &chat_onStatusChange;
|
2013-09-03 05:27:34 +02:00
|
|
|
ret.onStatusMessageChange = &chat_onStatusMessageChange;
|
2013-08-16 19:11:09 +02:00
|
|
|
ret.onAction = &chat_onAction;
|
2013-10-11 06:23:39 +02:00
|
|
|
ret.onFileSendRequest = &chat_onFileSendRequest;
|
|
|
|
ret.onFileControl = &chat_onFileControl;
|
|
|
|
ret.onFileData = &chat_onFileData;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-06 00:24:58 +02:00
|
|
|
uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'};
|
2013-10-19 05:46:58 +02:00
|
|
|
uint16_t len = tox_getname(m, friendnum, name);
|
|
|
|
memcpy(ret.name, name, len);
|
|
|
|
ret.name[TOXIC_MAX_NAME_LENGTH] = '\0';
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
ChatContext *chatwin = calloc(1, sizeof(ChatContext));
|
|
|
|
StatusBar *stb = calloc(1, sizeof(StatusBar));
|
2013-09-12 00:07:26 +02:00
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
if (stb != NULL && chatwin != NULL) {
|
|
|
|
ret.chatwin = chatwin;
|
|
|
|
ret.stb = stb;
|
2013-09-12 00:07:26 +02:00
|
|
|
} else {
|
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "calloc() failed. Aborting...\n");
|
2013-09-12 00:07:26 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-09-07 01:59:45 +02:00
|
|
|
|
|
|
|
ret.prompt = prompt;
|
2013-09-15 22:38:38 +02:00
|
|
|
ret.num = friendnum;
|
2013-09-06 00:24:58 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
return ret;
|
2013-07-31 19:20:03 +02:00
|
|
|
}
|