1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-15 02:33:02 +01:00
toxic/src/prompt.c

518 lines
15 KiB
C
Raw Normal View History

2014-02-25 08:28:24 +01:00
/* prompt.c
*
*
* Copyright (C) 2014 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
* Toxic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toxic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
2013-09-02 19:19:25 +02:00
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "toxic_windows.h"
2013-08-13 00:50:43 +02:00
#include "prompt.h"
2013-11-10 03:43:56 +01:00
#include "execute.h"
#include "misc_tools.h"
2013-12-10 09:03:45 +01:00
#include "toxic_strings.h"
#include "line_info.h"
2013-10-16 23:59:56 +02:00
uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0};
2013-09-15 22:38:38 +02:00
uint8_t num_frnd_requests = 0;
extern ToxWindow *prompt;
struct _Winthread Winthread;
/* Array of global command names used for tab completion. */
const uint8_t glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/accept" },
{ "/add" },
{ "/clear" },
{ "/close" }, /* rm /close when groupchats gets its own list */
{ "/connect" },
{ "/exit" },
{ "/groupchat" },
{ "/help" },
{ "/join" },
2014-02-27 01:00:13 +01:00
{ "/log" },
{ "/myid" },
{ "/nick" },
{ "/note" },
{ "/quit" },
{ "/status" },
2014-03-08 01:12:51 +01:00
#ifdef _SUPPORT_AUDIO
{ "/lsdev" },
{ "/sdev" },
#endif /* _SUPPORT_AUDIO */
};
/* Updates own nick in prompt statusbar */
void prompt_update_nick(ToxWindow *prompt, uint8_t *nick, uint16_t len)
{
StatusBar *statusbar = prompt->stb;
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
statusbar->nick_len = len;
}
/* Updates own statusmessage in prompt statusbar */
void prompt_update_statusmessage(ToxWindow *prompt, uint8_t *statusmsg, uint16_t len)
{
StatusBar *statusbar = prompt->stb;
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
statusbar->statusmsg_len = len;
}
/* Updates own status in prompt statusbar */
2014-03-19 02:48:26 +01:00
void prompt_update_status(ToxWindow *prompt, uint8_t status)
{
StatusBar *statusbar = prompt->stb;
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)
{
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.
2013-10-16 23:59:56 +02:00
Returns request number on success, -1 if queue is full or other error. */
static int add_friend_request(uint8_t *public_key)
{
2013-12-01 22:57:05 +01:00
if (num_frnd_requests >= MAX_FRIENDS_NUM)
2013-12-01 23:08:57 +01:00
return -1;
2013-12-01 22:57:05 +01:00
int i;
2013-10-16 23:59:56 +02:00
2013-12-01 22:57:05 +01:00
for (i = 0; i <= num_frnd_requests; ++i) {
if (!strlen(pending_frnd_requests[i])) {
memcpy(pending_frnd_requests[i], public_key, TOX_CLIENT_ID_SIZE);
2013-10-16 23:59:56 +02:00
2013-12-01 22:57:05 +01:00
if (i == num_frnd_requests)
++num_frnd_requests;
2013-10-16 23:59:56 +02:00
2013-12-01 22:57:05 +01:00
return i;
2013-10-16 23:59:56 +02:00
}
2013-09-16 06:28:28 +02:00
}
return -1;
}
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
{
ChatContext *ctx = self->chatwin;
2013-12-04 09:57:03 +01:00
2013-10-23 09:24:08 +02:00
int x, y, y2, x2;
2014-03-25 08:17:22 +01:00
getyx(ctx->history, y, x);
getmaxyx(ctx->history, y2, x2);
/* this is buggy */
//if (key == T_KEY_ESC) { /* ESC key: Toggle history scroll mode */
// bool scroll = ctx->hst->scroll_mode ? false : true;
// line_info_toggle_scroll(self, scroll);
//}
/* If we're in scroll mode ignore rest of function */
if (ctx->hst->scroll_mode) {
line_info_onKey(self, key);
return;
}
2013-10-23 09:24:08 +02:00
/* BACKSPACE key: Remove one character from line */
if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (ctx->pos > 0) {
del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len);
2014-03-25 08:17:22 +01:00
wmove(ctx->history, y, x-1); /* not necessary but fixes a display glitch */
ctx->scroll = false;
2013-12-16 02:52:10 +01:00
} else {
beep();
}
2013-12-04 22:14:33 +01:00
}
2013-10-23 09:24:08 +02:00
2013-12-04 22:14:33 +01:00
else if (key == KEY_DC) { /* DEL key: Remove character at pos */
if (ctx->pos != ctx->len) {
del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len);
ctx->scroll = false;
2013-12-16 02:52:10 +01:00
} else {
beep();
}
2013-12-04 22:14:33 +01:00
}
2013-12-06 04:55:14 +01:00
else if (key == T_KEY_DISCARD) { /* CTRL-U: Delete entire line behind pos */
if (ctx->pos > 0) {
2014-03-25 08:17:22 +01:00
wmove(ctx->history, ctx->orig_y, X_OFST);
wclrtobot(ctx->history);
discard_buf(ctx->line, &ctx->pos, &ctx->len);
2013-12-16 02:52:10 +01:00
} else {
beep();
2013-12-06 05:05:16 +01:00
}
2013-12-06 04:55:14 +01:00
}
else if (key == T_KEY_KILL) { /* CTRL-K: Delete entire line in front of pos */
if (ctx->len != ctx->pos)
kill_buf(ctx->line, &ctx->pos, &ctx->len);
2013-12-16 02:52:10 +01:00
else
beep();
2013-12-06 04:55:14 +01:00
}
else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */
if (ctx->pos != 0)
ctx->pos = 0;
2013-12-04 22:14:33 +01:00
}
else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */
if (ctx->pos != ctx->len)
ctx->pos = ctx->len;
2014-02-20 13:47:19 +01:00
}
2013-12-04 22:14:33 +01:00
else if (key == KEY_LEFT) {
if (ctx->pos > 0)
--ctx->pos;
2013-12-16 02:52:10 +01:00
else
beep();
2013-12-04 22:14:33 +01:00
}
else if (key == KEY_RIGHT) {
if (ctx->pos < ctx->len)
++ctx->pos;
2013-12-16 02:52:10 +01:00
else
beep();
2013-12-11 06:10:09 +01:00
}
else if (key == KEY_UP) { /* fetches previous item in history */
2014-03-25 08:17:22 +01:00
wmove(ctx->history, ctx->orig_y, X_OFST);
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot,
&ctx->hst_pos, MOVE_UP);
2013-12-11 11:41:02 +01:00
2013-12-14 21:38:21 +01:00
/* adjust line y origin appropriately when window scrolls down */
if (ctx->at_bottom && ctx->len >= x2 - X_OFST) {
int px2 = ctx->len >= x2 ? x2 : x2 - X_OFST;
2013-12-14 21:38:21 +01:00
int p_ofst = px2 != x2 ? 0 : X_OFST;
2013-12-11 11:41:02 +01:00
2013-12-14 21:38:21 +01:00
if (px2 <= 0)
return;
2013-12-11 11:41:02 +01:00
int k = ctx->orig_y + ((ctx->len + p_ofst) / px2);
2013-12-11 11:41:02 +01:00
2013-12-14 21:38:21 +01:00
if (k >= y2) {
2014-03-25 08:17:22 +01:00
wprintw(ctx->history, "\n");
--ctx->orig_y;
2013-12-11 10:49:21 +01:00
}
2013-12-11 06:10:09 +01:00
}
}
2013-12-09 00:14:57 +01:00
2013-12-11 06:10:09 +01:00
else if (key == KEY_DOWN) { /* fetches next item in history */
2014-03-25 08:17:22 +01:00
wmove(ctx->history, ctx->orig_y, X_OFST);
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot,
&ctx->hst_pos, MOVE_DOWN);
2013-12-11 06:10:09 +01:00
}
else if (key == '\t') { /* TAB key: completes command */
if (ctx->len > 1 && ctx->line[0] == '/') {
if (complete_line(ctx->line, &ctx->pos, &ctx->len, glob_cmd_list, AC_NUM_GLOB_COMMANDS,
2013-12-16 02:52:10 +01:00
MAX_CMDNAME_SIZE) == -1)
beep();
} else {
beep();
}
2013-12-09 00:14:57 +01:00
}
else
2013-12-04 09:57:03 +01:00
#if HAVE_WIDECHAR
if (iswprint(key))
#else
if (isprint(key))
#endif
{
if (ctx->len < (MAX_STR_SIZE-1)) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key);
ctx->scroll = true;
}
}
2013-08-16 19:11:09 +02:00
/* RETURN key: execute command */
else if (key == '\n') {
2014-03-25 08:17:22 +01:00
wprintw(ctx->history, "\n");
uint8_t line[MAX_STR_SIZE] = {0};
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1)
memset(&line, 0, sizeof(line));
2013-12-11 06:10:09 +01:00
if (!string_is_empty(line))
add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos);
2013-12-11 06:10:09 +01:00
2014-03-25 08:17:22 +01:00
line_info_add(self, NULL, NULL, NULL, line, PROMPT, 0, 0);
execute(ctx->history, self, m, line, GLOBAL_COMMAND_MODE);
reset_buf(ctx->line, &ctx->pos, &ctx->len);
}
}
2013-08-23 23:03:44 +02:00
static void prompt_onDraw(ToxWindow *self, Tox *m)
2013-08-07 00:27:51 +02:00
{
ChatContext *ctx = self->chatwin;
2013-09-28 01:55:11 +02:00
int x, y, x2, y2;
2014-03-25 08:17:22 +01:00
getyx(ctx->history, y, x);
getmaxyx(ctx->history, y2, x2);
line_info_print(self);
2013-08-16 19:11:09 +02:00
/* if len is >= screen width offset max x by X_OFST to account for prompt char */
int px2 = ctx->len >= x2 ? x2 : x2 - X_OFST;
2013-12-05 00:09:51 +01:00
if (px2 <= 0)
return;
2013-12-04 22:14:33 +01:00
/* len offset to account for prompt char (0 if len is < width of screen) */
int p_ofst = px2 != x2 ? 0 : X_OFST;
if (ctx->len > 0) {
uint8_t line[MAX_STR_SIZE];
2013-12-14 02:57:32 +01:00
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1)
reset_buf(ctx->line, &ctx->pos, &ctx->len);
else
2014-03-25 08:17:22 +01:00
mvwprintw(ctx->history, ctx->orig_y, X_OFST, line);
2013-12-05 07:22:02 +01:00
int k = ctx->orig_y + ((ctx->len + p_ofst) / px2);
ctx->at_bottom = k == y2 - 1;
bool botm = k == y2;
bool edge = (ctx->len + p_ofst) % px2 == 0;
2013-12-05 00:09:51 +01:00
2013-12-04 22:14:33 +01:00
/* move point of line origin up when input scrolls screen down */
if (ctx->scroll && edge && botm) {
--ctx->orig_y;
ctx->scroll = false;
}
2013-12-05 00:09:51 +01:00
2013-12-06 04:55:14 +01:00
} else { /* Mark point of origin for new line */
ctx->orig_y = y;
2013-12-04 22:14:33 +01:00
}
2014-03-25 08:17:22 +01:00
wattron(ctx->history, COLOR_PAIR(GREEN));
mvwprintw(ctx->history, ctx->orig_y, 0, "$ ");
wattroff(ctx->history, COLOR_PAIR(GREEN));
2013-12-06 04:55:14 +01:00
StatusBar *statusbar = self->stb;
werase(statusbar->topline);
2013-11-24 05:46:46 +01:00
mvwhline(statusbar->topline, 1, 0, ACS_HLINE, x2);
2013-09-28 01:55:11 +02:00
wmove(statusbar->topline, 0, 0);
2013-09-07 03:37:16 +02:00
if (statusbar->is_online) {
int colour = WHITE;
2014-03-19 08:14:08 +01:00
const uint8_t *status_text = "Unknown";
2013-11-12 07:50:04 +01:00
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;
2014-03-18 10:16:42 +01:00
case TOX_USERSTATUS_INVALID:
status_text = "ERROR";
colour = MAGENTA;
break;
}
wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
wprintw(statusbar->topline, " [%s]", status_text);
wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
wattron(statusbar->topline, A_BOLD);
wprintw(statusbar->topline, " %s", statusbar->nick);
wattroff(statusbar->topline, A_BOLD);
} else {
wprintw(statusbar->topline, "[Offline]");
wattron(statusbar->topline, A_BOLD);
2013-11-19 00:52:46 +01:00
wprintw(statusbar->topline, " %s ", statusbar->nick);
wattroff(statusbar->topline, A_BOLD);
}
if (statusbar->statusmsg[0])
wprintw(statusbar->topline, " - %s", statusbar->statusmsg);
2013-12-04 22:14:33 +01:00
/* put cursor back in correct spot */
int y_m = ctx->orig_y + ((ctx->pos + p_ofst) / px2);
int x_m = (ctx->pos + X_OFST) % x2;
2013-12-04 22:14:33 +01:00
wmove(self->window, y_m, x_m);
}
2014-03-19 02:48:26 +01:00
static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int32_t friendnum , uint8_t status)
2013-09-23 07:22:21 +02:00
{
if (friendnum < 0)
return;
ChatContext *ctx = self->chatwin;
2013-09-23 07:22:21 +02:00
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
if (tox_get_name(m, friendnum, nick) == -1)
2013-09-23 07:22:21 +02:00
return;
if (!nick[0])
snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME);
2014-03-25 08:17:22 +01:00
uint8_t timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt);
uint8_t *msg;
2014-03-02 00:06:35 +01:00
2013-09-23 07:22:21 +02:00
if (status == 1) {
2014-03-04 01:21:52 +01:00
msg = "has come online";
2014-03-25 08:17:22 +01:00
line_info_add(self, timefrmt, nick, NULL, msg, CONNECTION, 0, GREEN);
write_to_log(msg, nick, ctx->log, true);
alert_window(self, WINDOW_ALERT_2, false);
2013-09-23 07:22:21 +02:00
} else {
2014-03-04 01:21:52 +01:00
msg = "has gone offline";
2014-03-25 08:17:22 +01:00
line_info_add(self, timefrmt, nick, NULL, msg, CONNECTION, 0, RED);
write_to_log(msg, nick, ctx->log, true);
2013-09-23 07:22:21 +02:00
}
}
2014-03-19 02:48:26 +01:00
static void prompt_onFriendRequest(ToxWindow *self, Tox *m, uint8_t *key, uint8_t *data, uint16_t length)
2013-09-15 22:38:38 +02:00
{
/* make sure message data is null-terminated */
data[length - 1] = 0;
ChatContext *ctx = self->chatwin;
2014-03-02 00:06:35 +01:00
2014-03-25 08:17:22 +01:00
wprintw(ctx->history, "\n");
print_time(ctx->history);
2014-03-02 00:06:35 +01:00
uint8_t msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data);
2014-03-25 08:17:22 +01:00
wprintw(ctx->history, "%s", msg);
write_to_log(msg, "", ctx->log, true);
2013-09-15 22:38:38 +02:00
2013-10-16 23:59:56 +02:00
int n = add_friend_request(key);
2013-09-15 22:38:38 +02:00
2013-09-22 10:32:51 +02:00
if (n == -1) {
2014-03-19 08:14:08 +01:00
const uint8_t *errmsg = "Friend request queue is full. Discarding request.\n";
2014-03-25 08:17:22 +01:00
wprintw(ctx->history, "%s", errmsg);
write_to_log(errmsg, "", ctx->log, true);
2013-09-22 10:32:51 +02:00
return;
}
2014-03-25 08:17:22 +01:00
wprintw(ctx->history, "Type \"/accept %d\" to accept it.\n", n);
alert_window(self, WINDOW_ALERT_1, true);
2013-09-15 22:38:38 +02:00
}
void prompt_init_statusbar(ToxWindow *self, Tox *m)
{
2014-03-25 08:17:22 +01:00
int x2, y2;
getmaxyx(self->window, y2, x2);
/* Init statusbar info */
StatusBar *statusbar = self->stb;
statusbar->status = TOX_USERSTATUS_NONE;
2013-09-07 03:39:22 +02:00
statusbar->is_online = false;
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
uint8_t statusmsg[MAX_STR_SIZE];
pthread_mutex_lock(&Winthread.lock);
2014-03-19 02:48:26 +01:00
tox_get_self_name(m, nick);
tox_get_self_status_message(m, statusmsg, MAX_STR_SIZE);
2014-03-19 02:48:26 +01:00
uint8_t status = tox_get_self_user_status(m);
pthread_mutex_unlock(&Winthread.lock);
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
2014-03-14 04:30:44 +01:00
/* load prev status message or show toxic version if it has never been set */
uint8_t ver[strlen(TOXICVER) + 1];
strcpy(ver, TOXICVER);
2014-03-19 08:14:08 +01:00
const uint8_t *toxic_ver = strtok(ver, "_");
2014-02-22 10:50:47 +01:00
if ( (!strcmp("Online", statusmsg) || !strncmp("Toxing on Toxic", statusmsg, 15)) && toxic_ver != NULL)
2014-02-22 10:50:47 +01:00
snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic v.%s", toxic_ver);
prompt_update_statusmessage(prompt, statusmsg, strlen(statusmsg) + 1);
prompt_update_status(prompt, status);
/* Init statusbar subwindow */
2014-03-25 08:17:22 +01:00
statusbar->topline = subwin(self->window, 2, x2, 0, 0);
}
static void prompt_onInit(ToxWindow *self, Tox *m)
{
ChatContext *ctx = self->chatwin;
curs_set(1);
int y2, x2;
getmaxyx(self->window, y2, x2);
ctx->history = subwin(self->window, y2, x2, 0, 0);
scrollok(ctx->history, 1);
ctx->log = malloc(sizeof(struct chatlog));
ctx->hst = malloc(sizeof(struct history));
if (ctx->log == NULL || ctx->hst == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
memset(ctx->log, 0, sizeof(struct chatlog));
memset(ctx->hst, 0, sizeof(struct history));
line_info_init(ctx->hst);
execute(ctx->history, self, m, "/help", GLOBAL_COMMAND_MODE);
wmove(ctx->history, y2-1, 2);
}
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
ret.active = true;
2014-03-02 00:06:35 +01:00
ret.is_prompt = true;
2013-08-16 19:11:09 +02:00
ret.onKey = &prompt_onKey;
ret.onDraw = &prompt_onDraw;
ret.onInit = &prompt_onInit;
2013-09-23 07:22:21 +02:00
ret.onConnectionChange = &prompt_onConnectionChange;
ret.onFriendRequest = &prompt_onFriendRequest;
2013-09-15 22:38:38 +02:00
strcpy(ret.name, "prompt");
ChatContext *chatwin = calloc(1, sizeof(ChatContext));
StatusBar *stb = calloc(1, sizeof(StatusBar));
2013-09-12 00:07:26 +02:00
if (stb != NULL && chatwin != NULL) {
ret.chatwin = chatwin;
ret.stb = stb;
2013-12-04 09:57:03 +01:00
} else {
2013-09-12 00:07:26 +02:00
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-08-16 19:11:09 +02:00
return ret;
2013-12-01 22:59:46 +01:00
}