2013-07-30 01:32:39 +02:00
|
|
|
/*
|
2013-09-02 19:19:25 +02:00
|
|
|
* Toxic -- Tox Curses Client
|
|
|
|
*/
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-23 09:50:04 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 01:32:39 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2013-08-13 00:50:43 +02:00
|
|
|
#include "prompt.h"
|
2013-09-19 23:03:22 +02:00
|
|
|
#include "commands.h"
|
2013-08-17 11:59:28 +02:00
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
|
|
|
|
uint8_t num_frnd_requests = 0;
|
|
|
|
|
2013-09-16 06:28:28 +02:00
|
|
|
uint8_t pending_grp_requests[MAX_GROUPCHAT_NUM][TOX_CLIENT_ID_SIZE];
|
2013-09-15 22:38:38 +02:00
|
|
|
uint8_t num_grp_requests = 0;
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-09-08 09:18:34 +02:00
|
|
|
static char prompt_buf[MAX_STR_SIZE] = {'\0'};
|
2013-08-07 00:27:51 +02:00
|
|
|
static int prompt_buf_pos = 0;
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-09-07 01:59:45 +02:00
|
|
|
/* Updates own nick in prompt statusbar */
|
2013-09-18 23:30:35 +02:00
|
|
|
void prompt_update_nick(ToxWindow *prompt, uint8_t *nick, uint16_t len)
|
2013-09-07 01:59:45 +02:00
|
|
|
{
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) prompt->stb;
|
2013-09-07 01:59:45 +02:00
|
|
|
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
|
2013-09-18 23:30:35 +02:00
|
|
|
statusbar->nick_len = len;
|
2013-09-07 01:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Updates own statusmessage in prompt statusbar */
|
2013-09-18 23:30:35 +02:00
|
|
|
void prompt_update_statusmessage(ToxWindow *prompt, uint8_t *statusmsg, uint16_t len)
|
2013-09-07 01:59:45 +02:00
|
|
|
{
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) prompt->stb;
|
2013-09-07 01:59:45 +02:00
|
|
|
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
2013-09-18 23:30:35 +02:00
|
|
|
statusbar->statusmsg_len = len;
|
2013-09-07 01:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Updates own status in prompt statusbar */
|
|
|
|
void prompt_update_status(ToxWindow *prompt, TOX_USERSTATUS status)
|
|
|
|
{
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) prompt->stb;
|
2013-09-07 01:59:45 +02:00
|
|
|
statusbar->status = status;
|
|
|
|
}
|
|
|
|
|
2013-09-09 06:56:47 +02:00
|
|
|
/* Updates own connection status in prompt statusbar */
|
2013-09-07 03:37:16 +02:00
|
|
|
void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected)
|
|
|
|
{
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) prompt->stb;
|
2013-09-07 03:37:16 +02:00
|
|
|
statusbar->is_online = is_connected;
|
|
|
|
}
|
|
|
|
|
2013-09-16 06:28:28 +02:00
|
|
|
/* Adds friend request to pending friend requests.
|
|
|
|
Returns friend number on success, -1 if queue is full or other error. */
|
2013-09-15 22:38:38 +02:00
|
|
|
int add_friend_req(uint8_t *public_key)
|
2013-09-06 08:51:10 +02:00
|
|
|
{
|
2013-09-16 06:28:28 +02:00
|
|
|
if (num_frnd_requests < MAX_FRIENDS_NUM) {
|
|
|
|
memcpy(pending_frnd_requests[num_frnd_requests++], public_key, TOX_CLIENT_ID_SIZE);
|
|
|
|
return num_frnd_requests - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2013-09-06 08:51:10 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 06:28:28 +02:00
|
|
|
/* Adds group chat invite to pending group chat requests.
|
|
|
|
Returns group number on success, -1 if queue is full or other error. */
|
2013-09-15 22:38:38 +02:00
|
|
|
int add_group_req(uint8_t *group_pub_key)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-09-16 06:28:28 +02:00
|
|
|
if (num_grp_requests < MAX_GROUPCHAT_NUM) {
|
|
|
|
memcpy(pending_grp_requests[num_grp_requests++], group_pub_key, TOX_CLIENT_ID_SIZE);
|
|
|
|
return num_grp_requests - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: FIX
|
2013-08-07 00:27:51 +02:00
|
|
|
unsigned char *hex_string_to_bin(char hex_string[])
|
2013-07-30 01:32:39 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
size_t len = strlen(hex_string);
|
|
|
|
unsigned char *val = malloc(len);
|
2013-09-12 00:07:26 +02:00
|
|
|
|
|
|
|
if (val == NULL) {
|
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "malloc() failed. Aborting...\n");
|
2013-09-12 00:07:26 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
char *pos = hex_string;
|
2013-09-12 19:31:10 +02:00
|
|
|
size_t i;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
for (i = 0; i < len; ++i, pos += 2)
|
|
|
|
sscanf(pos, "%2hhx", &val[i]);
|
|
|
|
|
|
|
|
return val;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-09-19 23:03:22 +02:00
|
|
|
void print_prompt_help(ToxWindow *self)
|
2013-09-15 22:38:38 +02:00
|
|
|
{
|
2013-09-19 23:03:22 +02:00
|
|
|
wclear(self->window);
|
|
|
|
wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD);
|
|
|
|
wprintw(self->window, "\n\nCommands:\n");
|
|
|
|
wattroff(self->window, A_BOLD);
|
|
|
|
|
|
|
|
wprintw(self->window, " /add <id> <message> : Add friend with optional message\n");
|
|
|
|
wprintw(self->window, " /accept <n> : Accept friend request\n");
|
|
|
|
wprintw(self->window, " /status <type> <message> : Set your status with optional note\n");
|
|
|
|
wprintw(self->window, " /note <message> : Set a personal note\n");
|
|
|
|
wprintw(self->window, " /nick <nickname> : Set your nickname\n");
|
|
|
|
wprintw(self->window, " /join <n> : Join a group chat\n");
|
|
|
|
wprintw(self->window, " /invite <nickname> <n> : Invite friend to a groupchat\n");
|
|
|
|
wprintw(self->window, " /groupchat : Create a group chat\n");
|
|
|
|
wprintw(self->window, " /myid : Print your ID\n");
|
|
|
|
wprintw(self->window, " /quit or /exit : Exit Toxic\n");
|
|
|
|
wprintw(self->window, " /help : Print this message again\n");
|
|
|
|
wprintw(self->window, " /clear : Clear this window\n");
|
|
|
|
|
|
|
|
wattron(self->window, A_BOLD);
|
|
|
|
wprintw(self->window, " * Messages must be enclosed in quotation marks.\n");
|
|
|
|
wprintw(self->window, " * Use the TAB key to navigate through the tabs.\n");
|
|
|
|
wattroff(self->window, A_BOLD);
|
|
|
|
|
|
|
|
wattroff(self->window, COLOR_PAIR(CYAN));
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
/* Add printable characters to line */
|
|
|
|
if (isprint(key)) {
|
|
|
|
if (prompt_buf_pos == (sizeof(prompt_buf) - 1)) {
|
2013-09-18 23:30:35 +02:00
|
|
|
return;
|
2013-08-16 19:11:09 +02:00
|
|
|
} else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS)
|
|
|
|
&& (prompt_buf_pos % (COLS - 3) == 0)) {
|
2013-09-18 23:30:35 +02:00
|
|
|
wprintw(self->window, "\n");
|
2013-08-16 19:11:09 +02:00
|
|
|
prompt_buf[prompt_buf_pos++] = '\n';
|
|
|
|
} else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
|
|
|
|
&& ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
|
2013-09-18 23:30:35 +02:00
|
|
|
wprintw(self->window, "\n");
|
2013-08-16 19:11:09 +02:00
|
|
|
prompt_buf[prompt_buf_pos++] = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt_buf[prompt_buf_pos++] = key;
|
|
|
|
prompt_buf[prompt_buf_pos] = 0;
|
2013-08-06 12:16:17 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
/* RETURN key: execute command */
|
|
|
|
else if (key == '\n') {
|
|
|
|
wprintw(self->window, "\n");
|
2013-09-19 23:03:22 +02:00
|
|
|
|
|
|
|
if (!strncmp(prompt_buf, "/help", strlen("/help")))
|
|
|
|
print_prompt_help(self);
|
|
|
|
else
|
|
|
|
execute(self->window, self, m, prompt_buf, prompt_buf_pos);
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
prompt_buf_pos = 0;
|
|
|
|
prompt_buf[0] = 0;
|
2013-07-30 01:32:39 +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) {
|
2013-09-07 01:59:45 +02:00
|
|
|
if (prompt_buf_pos != 0)
|
2013-08-16 19:11:09 +02:00
|
|
|
prompt_buf[--prompt_buf_pos] = 0;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void prompt_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;
|
2013-09-12 19:31:10 +02:00
|
|
|
size_t i;
|
2013-09-07 01:59:45 +02:00
|
|
|
getyx(self->window, y, x);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
|
|
|
for (i = 0; i < (strlen(prompt_buf)); ++i) {
|
|
|
|
if ((prompt_buf[i] == '\n') && (y != 0))
|
|
|
|
--y;
|
|
|
|
}
|
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) self->stb;
|
2013-09-07 01:59:45 +02:00
|
|
|
|
|
|
|
werase(statusbar->topline);
|
|
|
|
|
2013-09-07 03:37:16 +02:00
|
|
|
if (statusbar->is_online) {
|
2013-09-07 01:59:45 +02:00
|
|
|
int colour = WHITE;
|
|
|
|
char *status_text = "Unknown";
|
|
|
|
|
|
|
|
switch(statusbar->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;
|
|
|
|
}
|
|
|
|
|
|
|
|
wattron(statusbar->topline, A_BOLD);
|
2013-09-09 06:56:47 +02:00
|
|
|
wprintw(statusbar->topline, " %s ", statusbar->nick);
|
2013-09-07 01:59:45 +02:00
|
|
|
wattron(statusbar->topline, A_BOLD);
|
|
|
|
wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
|
|
|
|
wprintw(statusbar->topline, "[%s]", status_text);
|
|
|
|
wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
|
|
|
|
} else {
|
|
|
|
wattron(statusbar->topline, A_BOLD);
|
|
|
|
wprintw(statusbar->topline, "%s ", statusbar->nick);
|
|
|
|
wattroff(statusbar->topline, A_BOLD);
|
|
|
|
wprintw(statusbar->topline, "[Offline]");
|
|
|
|
}
|
|
|
|
|
2013-09-08 09:18:34 +02:00
|
|
|
wattron(statusbar->topline, A_BOLD);
|
2013-09-09 06:56:47 +02:00
|
|
|
wprintw(statusbar->topline, " | %s |", statusbar->statusmsg);
|
2013-09-08 09:18:34 +02:00
|
|
|
wattroff(statusbar->topline, A_BOLD);
|
2013-09-07 01:59:45 +02:00
|
|
|
|
|
|
|
wprintw(statusbar->topline, "\n");
|
|
|
|
|
2013-09-05 06:47:33 +02:00
|
|
|
wattron(self->window, COLOR_PAIR(GREEN));
|
2013-08-16 19:11:09 +02:00
|
|
|
mvwprintw(self->window, y, 0, "# ");
|
2013-09-05 06:47:33 +02:00
|
|
|
wattroff(self->window, COLOR_PAIR(GREEN));
|
2013-08-16 19:11:09 +02:00
|
|
|
mvwprintw(self->window, y, 2, "%s", prompt_buf);
|
|
|
|
wclrtoeol(self->window);
|
2013-09-07 01:59:45 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
wrefresh(self->window);
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void prompt_onInit(ToxWindow *self, Tox *m)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
scrollok(self->window, 1);
|
2013-09-19 23:03:22 +02:00
|
|
|
print_prompt_help(self);
|
2013-08-16 19:11:09 +02:00
|
|
|
wclrtoeol(self->window);
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16_t length)
|
|
|
|
{
|
|
|
|
int n = add_friend_req(key);
|
2013-09-16 06:28:28 +02:00
|
|
|
|
|
|
|
if (n == -1) {
|
|
|
|
wprintw(self->window, "Friend request queue is full. Discarding request.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
wprintw(self->window, "\nFriend request from:\n");
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < KEY_SIZE_BYTES; ++i) {
|
|
|
|
wprintw(self->window, "%02x", key[i] & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
wprintw(self->window, "\n\nWith the message: %s\n\n", data);
|
2013-09-19 12:37:42 +02:00
|
|
|
wprintw(self->window, "Type \"/accept %d\" to accept it.\n", n);
|
2013-09-15 22:38:38 +02:00
|
|
|
|
|
|
|
self->blink = true;
|
|
|
|
beep();
|
|
|
|
}
|
|
|
|
|
|
|
|
void prompt_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key)
|
|
|
|
{
|
2013-09-18 01:11:23 +02:00
|
|
|
if (friendnumber < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'};
|
|
|
|
|
|
|
|
if (tox_getname(m, friendnumber, name) == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
name[TOXIC_MAX_NAME_LENGTH] = '\0'; /* enforce client max name length */
|
|
|
|
wprintw(self->window, "\nGroup chat invite from %s.\n", name);
|
2013-09-16 06:28:28 +02:00
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
int ngc = get_num_groupchats();
|
2013-09-16 06:28:28 +02:00
|
|
|
|
|
|
|
if (ngc < 0 || ngc > MAX_GROUPCHAT_NUM) {
|
2013-09-15 22:38:38 +02:00
|
|
|
wprintw(self->window, "\nMaximum number of group chats has been reached. Discarding invite.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int n = add_group_req(group_pub_key);
|
|
|
|
|
2013-09-16 06:28:28 +02:00
|
|
|
if (n == -1) {
|
|
|
|
wprintw(self->window, "\nGroup chat queue is full. Discarding invite.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-19 12:37:42 +02:00
|
|
|
wprintw(self->window, "Type \"/join %d\" to join the chat.\n", n);
|
2013-09-15 22:38:38 +02:00
|
|
|
|
|
|
|
self->blink = true;
|
|
|
|
beep();
|
|
|
|
}
|
|
|
|
|
2013-09-07 01:59:45 +02:00
|
|
|
void prompt_init_statusbar(ToxWindow *self, Tox *m)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getmaxyx(self->window, y, x);
|
|
|
|
|
|
|
|
/* Init statusbar info */
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *statusbar = (StatusBar *) self->stb;
|
2013-09-07 01:59:45 +02:00
|
|
|
statusbar->status = TOX_USERSTATUS_NONE;
|
2013-09-07 03:39:22 +02:00
|
|
|
statusbar->is_online = false;
|
2013-09-07 01:59:45 +02:00
|
|
|
|
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
|
2013-09-08 09:18:34 +02:00
|
|
|
tox_getselfname(m, nick, TOX_MAX_NAME_LENGTH);
|
2013-09-07 01:59:45 +02:00
|
|
|
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
|
|
|
|
|
2013-09-08 09:18:34 +02:00
|
|
|
/* temporary until statusmessage saving works */
|
|
|
|
uint8_t *statusmsg = "Toxing on Toxic v0.2.0";
|
2013-09-09 06:56:47 +02:00
|
|
|
m_set_statusmessage(m, statusmsg, strlen(statusmsg) + 1);
|
2013-09-08 09:18:34 +02:00
|
|
|
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
2013-09-07 01:59:45 +02:00
|
|
|
|
|
|
|
/* Init statusbar subwindow */
|
|
|
|
statusbar->topline = subwin(self->window, 2, x, 0, 0);
|
|
|
|
}
|
|
|
|
|
2013-09-19 23:03:22 +02:00
|
|
|
ToxWindow new_prompt(void)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
ToxWindow ret;
|
|
|
|
memset(&ret, 0, sizeof(ret));
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
ret.onKey = &prompt_onKey;
|
|
|
|
ret.onDraw = &prompt_onDraw;
|
|
|
|
ret.onInit = &prompt_onInit;
|
2013-09-06 08:51:10 +02:00
|
|
|
ret.onFriendRequest = &prompt_onFriendRequest;
|
2013-09-15 22:38:38 +02:00
|
|
|
ret.onGroupInvite = &prompt_onGroupInvite;
|
|
|
|
|
2013-09-06 00:24:58 +02:00
|
|
|
strcpy(ret.name, "prompt");
|
2013-09-07 01:59:45 +02:00
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
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)
|
|
|
|
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
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
return ret;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|