1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-01 16:57:45 +02:00
toxic/src/prompt.c

719 lines
19 KiB
C
Raw Normal View History

/*
2013-09-02 19:19:25 +02:00
* Toxic -- Tox Curses Client
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
2013-08-13 00:50:43 +02:00
#include "prompt.h"
extern char *DATA_FILE;
2013-08-23 23:03:44 +02:00
uint8_t pending_requests[MAX_STR_SIZE][TOX_CLIENT_ID_SIZE]; // XXX
2013-08-16 19:11:09 +02:00
uint8_t num_requests = 0; // XXX
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;
/* commands */
2013-09-02 19:19:25 +02:00
void cmd_accept(ToxWindow *, Tox *m, int, char **);
void cmd_add(ToxWindow *, Tox *m, int, char **);
void cmd_clear(ToxWindow *, Tox *m, int, char **);
void cmd_connect(ToxWindow *, Tox *m, int, char **);
void cmd_help(ToxWindow *, Tox *m, int, char **);
void cmd_msg(ToxWindow *, Tox *m, int, char **);
void cmd_myid(ToxWindow *, Tox *m, int, char **);
void cmd_nick(ToxWindow *, Tox *m, int, char **);
void cmd_quit(ToxWindow *, Tox *m, int, char **);
void cmd_status(ToxWindow *, Tox *m, int, char **);
void cmd_note(ToxWindow *, Tox *m, int, char **);
#define NUM_COMMANDS 13
static struct {
2013-08-16 19:11:09 +02:00
char *name;
2013-09-02 19:19:25 +02:00
void (*func)(ToxWindow *, Tox *m, int, char **);
} commands[] = {
2013-09-02 19:19:25 +02:00
{ "accept", cmd_accept },
{ "add", cmd_add },
{ "clear", cmd_clear },
{ "connect", cmd_connect },
{ "exit", cmd_quit },
{ "help", cmd_help },
{ "msg", cmd_msg },
{ "myid", cmd_myid },
{ "nick", cmd_nick },
{ "q", cmd_quit },
{ "quit", cmd_quit },
{ "status", cmd_status },
{ "note", cmd_note },
};
/* Updates own nick in prompt statusbar */
void prompt_update_nick(ToxWindow *prompt, uint8_t *nick)
{
StatusBar *statusbar = (StatusBar *) prompt->stb;
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
}
/* Updates own statusmessage in prompt statusbar */
void prompt_update_statusmessage(ToxWindow *prompt, uint8_t *statusmsg)
{
StatusBar *statusbar = (StatusBar *) prompt->stb;
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
}
/* Updates own status in prompt statusbar */
void prompt_update_status(ToxWindow *prompt, TOX_USERSTATUS status)
{
StatusBar *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 = (StatusBar *) prompt->stb;
2013-09-07 03:37:16 +02:00
statusbar->is_online = is_connected;
}
void prompt_onFriendRequest(ToxWindow *prompt, uint8_t *key, uint8_t *data, uint16_t length)
{
int n = add_req(key);
wprintw(prompt->window, "\nFriend request from:\n");
int i;
for (i = 0; i < KEY_SIZE_BYTES; ++i) {
wprintw(prompt->window, "%02x", key[i] & 0xff);
}
wprintw(prompt->window, "\n\nWith the message: %s\n\n", data);
wprintw(prompt->window, "Type \"accept %d\" to accept it.\n", n);
prompt->blink = true;
beep();
}
// XXX:
2013-08-07 00:27:51 +02:00
int add_req(uint8_t *public_key)
{
2013-08-23 23:03:44 +02:00
memcpy(pending_requests[num_requests], public_key, TOX_CLIENT_ID_SIZE);
2013-08-16 19:11:09 +02:00
++num_requests;
return num_requests - 1;
}
// XXX: FIX
2013-08-07 00:27:51 +02:00
unsigned char *hex_string_to_bin(char hex_string[])
{
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;
int i;
for (i = 0; i < len; ++i, pos += 2)
sscanf(pos, "%2hhx", &val[i]);
return val;
}
2013-09-02 19:19:25 +02:00
/* command functions */
void cmd_accept(ToxWindow *self, Tox *m, int argc, char **argv)
2013-08-07 00:27:51 +02:00
{
2013-09-02 19:19:25 +02:00
int num;
2013-08-16 19:11:09 +02:00
2013-09-02 19:19:25 +02:00
/* check arguments */
if (argc != 1) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
num = atoi(argv[1]);
2013-08-16 19:11:09 +02:00
if (num < 0 || num >= num_requests) {
wprintw(self->window, "No pending request with that number.\n");
2013-08-16 19:11:09 +02:00
return;
}
2013-08-25 01:16:43 +02:00
num = tox_addfriend_norequest(m, pending_requests[num]);
2013-08-16 19:11:09 +02:00
if (num == -1)
wprintw(self->window, "Failed to add friend.\n");
else {
wprintw(self->window, "Friend accepted as: %d.\n", num);
on_friendadded(m, num);
2013-08-16 19:11:09 +02:00
}
}
2013-09-02 19:19:25 +02:00
void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv)
{
if (argc < 1 || argc > 2) {
2013-09-03 07:02:58 +02:00
wprintw(self->window, "Invalid syntax.\n");
return;
}
2013-08-23 23:03:44 +02:00
uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE];
2013-08-16 19:11:09 +02:00
char xx[3];
uint32_t x;
uint8_t *msg;
2013-09-02 19:19:25 +02:00
int i, num;
2013-08-16 19:11:09 +02:00
char *id = argv[1];
if (id == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
if (argc == 2) {
msg = argv[2];
if (msg == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
if (msg[0] != '\"') {
2013-09-03 07:02:58 +02:00
wprintw(self->window, "Messages must be enclosed in quotes.\n");
return;
}
msg[strlen(++msg)-1] = L'\0';
} else
msg = "Let's tox.";
2013-08-16 19:11:09 +02:00
2013-08-23 23:03:44 +02:00
if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) {
2013-08-16 19:11:09 +02:00
wprintw(self->window, "Invalid ID length.\n");
return;
}
2013-08-23 23:03:44 +02:00
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
2013-08-16 19:11:09 +02:00
xx[0] = id[2 * i];
xx[1] = id[2 * i + 1];
xx[2] = '\0';
if (sscanf(xx, "%02x", &x) != 1) {
wprintw(self->window, "Invalid ID.\n");
return;
}
id_bin[i] = x;
}
2013-08-23 23:03:44 +02:00
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
2013-08-16 19:11:09 +02:00
id[i] = toupper(id[i]);
}
num = tox_addfriend(m, id_bin, msg, strlen(msg) + 1);
2013-08-16 19:11:09 +02:00
switch (num) {
2013-08-23 23:03:44 +02:00
case TOX_FAERR_TOOLONG:
2013-08-16 19:11:09 +02:00
wprintw(self->window, "Message is too long.\n");
break;
2013-08-23 23:03:44 +02:00
case TOX_FAERR_NOMESSAGE:
2013-08-16 19:11:09 +02:00
wprintw(self->window, "Please add a message to your request.\n");
break;
2013-08-23 23:03:44 +02:00
case TOX_FAERR_OWNKEY:
2013-08-16 19:11:09 +02:00
wprintw(self->window, "That appears to be your own ID.\n");
break;
2013-08-23 23:03:44 +02:00
case TOX_FAERR_ALREADYSENT:
2013-08-16 19:11:09 +02:00
wprintw(self->window, "Friend request already sent.\n");
break;
2013-08-23 23:03:44 +02:00
case TOX_FAERR_UNKNOWN:
2013-08-16 19:11:09 +02:00
wprintw(self->window, "Undefined error when adding friend.\n");
break;
2013-08-23 23:03:44 +02:00
case TOX_FAERR_BADCHECKSUM:
2013-08-16 19:11:09 +02:00
wprintw(self->window, "Bad checksum in address.\n");
break;
2013-08-23 23:03:44 +02:00
case TOX_FAERR_SETNEWNOSPAM:
2013-08-16 19:11:09 +02:00
wprintw(self->window, "Nospam was different.\n");
break;
default:
wprintw(self->window, "Friend added as %d.\n", num);
on_friendadded(m, num);
2013-08-16 19:11:09 +02:00
break;
}
}
2013-09-02 19:19:25 +02:00
void cmd_clear(ToxWindow *self, Tox *m, int argc, char **argv)
{
2013-08-16 19:11:09 +02:00
wclear(self->window);
wprintw(self->window, "\n\n");
}
2013-09-02 19:19:25 +02:00
void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv)
{
2013-09-02 19:19:25 +02:00
/* check arguments */
if (argc != 3) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
2013-09-08 09:18:34 +02:00
tox_IP_Port dht;
char *ip = argv[1];
char *port = argv[2];
char *key = argv[3];
if (!ip || !port || !key) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
2013-08-16 19:11:09 +02:00
if (atoi(port) == 0) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
dht.port = htons(atoi(port));
uint32_t resolved_address = resolve_addr(ip);
if (resolved_address == 0) {
return;
}
dht.ip.i = resolved_address;
2013-08-23 23:03:44 +02:00
uint8_t *binary_string = hex_string_to_bin(key);
tox_bootstrap(m, dht, binary_string);
2013-08-16 19:11:09 +02:00
free(binary_string);
}
2013-08-07 00:27:51 +02:00
2013-09-02 19:19:25 +02:00
void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv)
{
exit_toxic(m);
}
2013-09-02 19:19:25 +02:00
void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv)
{
2013-08-16 19:11:09 +02:00
wclear(self->window);
wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(self->window, "\n\nCommands:\n");
2013-08-16 19:11:09 +02:00
wattroff(self->window, A_BOLD);
wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
wprintw(self->window, " add <id> <message> : Add friend with optional message\n");
wprintw(self->window, " status <type> <message> : Set your status with optional note\n");
wprintw(self->window, " note <message> : Set a personal note\n");
2013-08-16 19:11:09 +02:00
wprintw(self->window, " nick <nickname> : Set your nickname\n");
wprintw(self->window, " accept <number> : Accept friend request\n");
wprintw(self->window, " myid : Print your ID\n");
2013-09-02 07:14:51 +02:00
wprintw(self->window, " quit/exit : Exit Toxic\n");
2013-08-16 19:11:09 +02:00
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\n");
2013-08-16 19:11:09 +02:00
wattroff(self->window, A_BOLD);
wattroff(self->window, COLOR_PAIR(CYAN));
}
2013-09-02 19:19:25 +02:00
void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv)
{
2013-09-02 19:19:25 +02:00
/* check arguments */
if (argc != 2) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
2013-09-08 09:18:34 +02:00
char *id = argv[1];
uint8_t *msg = argv[2];
if (id == NULL || msg == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
msg[strlen(++msg)-1] = L'\0';
2013-08-16 19:11:09 +02:00
2013-09-08 09:18:34 +02:00
if (tox_sendmessage(m, atoi(id), msg, strlen(msg) + 1) == 0)
wprintw(self->window, "Failed to send message.\n");
2013-08-16 19:11:09 +02:00
else
wprintw(self->window, "Message successfully sent.\n");
}
2013-09-02 19:19:25 +02:00
void cmd_myid(ToxWindow *self, Tox *m, int argc, char **argv)
{
2013-08-23 23:03:44 +02:00
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
2013-08-25 01:16:43 +02:00
tox_getaddress(m, address);
2013-08-16 19:11:09 +02:00
2013-09-08 09:18:34 +02:00
size_t i;
2013-08-23 23:03:44 +02:00
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
2013-08-16 19:11:09 +02:00
char xx[3];
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
}
wprintw(self->window, "%s\n", id);
}
2013-09-02 19:19:25 +02:00
void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv)
{
2013-09-02 19:19:25 +02:00
/* check arguments */
if (argc != 1) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
2013-09-08 09:18:34 +02:00
uint8_t *nick = argv[1];
if (nick == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
int len = strlen(nick);
2013-09-02 19:19:25 +02:00
if (nick[0] == '\"') {
++nick;
2013-09-13 10:01:10 +02:00
len -= 2;
nick[len] = L'\0';
}
if (len > TOXIC_MAX_NAME_LENGTH) {
nick[TOXIC_MAX_NAME_LENGTH] = L'\0';
len = TOXIC_MAX_NAME_LENGTH;
}
tox_setname(m, nick, len+1);
prompt_update_nick(self, nick);
2013-08-18 10:48:36 +02:00
store_data(m, DATA_FILE);
}
2013-08-07 00:27:51 +02:00
2013-09-02 19:19:25 +02:00
void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv)
{
if (argc < 1 || argc > 2) {
wprintw(self->window, "Wrong number of arguments.\n");
return;
2013-09-02 19:19:25 +02:00
}
2013-09-08 09:18:34 +02:00
uint8_t *msg = NULL;
2013-09-03 06:20:17 +02:00
if (argc == 2) {
2013-09-03 06:20:17 +02:00
msg = argv[2];
if (msg == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
2013-09-03 06:20:17 +02:00
if (msg[0] != '\"') {
wprintw(self->window, "Messages must be enclosed in quotes.\n");
return;
}
}
2013-09-08 09:18:34 +02:00
char *status = argv[1];
2013-08-16 19:11:09 +02:00
if (status == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
2013-08-23 23:03:44 +02:00
TOX_USERSTATUS status_kind;
2013-08-16 19:11:09 +02:00
if (!strncmp(status, "online", strlen("online")))
2013-08-23 23:03:44 +02:00
status_kind = TOX_USERSTATUS_NONE;
else if (!strncmp(status, "away", strlen("away")))
2013-08-23 23:03:44 +02:00
status_kind = TOX_USERSTATUS_AWAY;
else if (!strncmp(status, "busy", strlen("busy")))
2013-08-23 23:03:44 +02:00
status_kind = TOX_USERSTATUS_BUSY;
2013-08-16 19:11:09 +02:00
else
wprintw(self->window, "Invalid status.\n");
tox_set_userstatus(m, status_kind);
prompt_update_status(self, status_kind);
if (msg != NULL) {
msg[strlen(++msg)-1] = L'\0'; /* remove opening and closing quotes */
tox_set_statusmessage(m, msg, strlen(msg) + 1);
prompt_update_statusmessage(self, msg);
2013-08-16 19:11:09 +02:00
}
}
void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv)
{
2013-09-03 07:02:58 +02:00
if (argc != 1) {
wprintw(self->window, "Wrong number of arguments.\n");
return;
}
2013-09-08 09:18:34 +02:00
uint8_t *msg = argv[1];
if (msg == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
2013-09-02 19:19:25 +02:00
2013-09-08 09:18:34 +02:00
if (msg[0] != '\"') {
wprintw(self->window, "Messages must be enclosed in quotes.\n");
return;
}
2013-09-02 19:19:25 +02:00
msg[strlen(++msg)-1] = L'\0';
2013-09-02 19:19:25 +02:00
tox_set_statusmessage(m, msg, strlen(msg) + 1);
prompt_update_statusmessage(self, msg);
}
2013-08-23 23:03:44 +02:00
static void execute(ToxWindow *self, Tox *m, char *u_cmd)
{
2013-08-16 19:11:09 +02:00
int newlines = 0;
2013-09-08 09:18:34 +02:00
char cmd[MAX_STR_SIZE] = {'\0'};
2013-08-16 19:11:09 +02:00
int i;
for (i = 0; i < strlen(prompt_buf); ++i) {
if (u_cmd[i] == '\n')
++newlines;
else
cmd[i - newlines] = u_cmd[i];
}
2013-08-16 19:11:09 +02:00
int leading_spc = 0;
for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i)
leading_spc++;
memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc);
int cmd_end = strlen(cmd);
while (cmd_end > 0 && cmd_end--)
if (!isspace(cmd[cmd_end]))
break;
cmd[cmd_end + 1] = '\0';
/* insert \0 at argument boundaries */
int numargs = 0;
for (i = 0; i < MAX_STR_SIZE; i++) {
if (cmd[i] == ' ') {
cmd[i] = '\0';
numargs++;
}
2013-09-02 19:19:25 +02:00
/* skip over strings */
else if (cmd[i] == '\"') {
while (cmd[++i] != '\"') {
if (cmd[i] == '\0') {
2013-09-08 09:18:34 +02:00
wprintw(self->window, "Invalid command: did you forget an opening or closing \"?\n");
2013-09-02 19:19:25 +02:00
return;
}
}
}
2013-08-16 19:11:09 +02:00
}
2013-09-02 19:19:25 +02:00
/* read arguments into array */
char **cmdargs = malloc((numargs + 1) * sizeof(char *));
if (!cmdargs) {
2013-09-09 06:56:47 +02:00
wprintw(self->window, "Invalid command: too many arguments.\n");
return;
}
2013-08-16 19:11:09 +02:00
int pos = 0;
2013-09-02 19:19:25 +02:00
for (i = 0; i < numargs + 1; i++) {
2013-08-16 19:11:09 +02:00
cmdargs[i] = cmd + pos;
pos += strlen(cmdargs[i]) + 1;
2013-09-02 19:19:25 +02:00
/* replace empty strings with NULL for easier error checking */
if (strlen(cmdargs[i]) == 0)
cmdargs[i] = NULL;
2013-08-16 19:11:09 +02:00
}
/* no input */
2013-09-08 09:18:34 +02:00
if (!cmdargs[0]) {
free(cmdargs);
2013-08-16 19:11:09 +02:00
return;
2013-09-08 09:18:34 +02:00
}
2013-08-16 19:11:09 +02:00
/* match input to command list */
for (i = 0; i < NUM_COMMANDS; i++) {
if (!strcmp(cmdargs[0], commands[i].name)) {
2013-09-02 19:19:25 +02:00
(commands[i].func)(self, m, numargs, cmdargs);
2013-09-08 09:18:34 +02:00
free(cmdargs);
2013-08-16 19:11:09 +02:00
return;
}
}
/* no match */
2013-09-08 09:18:34 +02:00
free(cmdargs);
2013-08-16 19:11:09 +02:00
wprintw(self->window, "Invalid command.\n");
}
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)) {
wprintw(self->window, "\nToo Long.\n");
prompt_buf_pos = 0;
prompt_buf[0] = 0;
} else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS)
&& (prompt_buf_pos % (COLS - 3) == 0)) {
prompt_buf[prompt_buf_pos++] = '\n';
} else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
&& ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
prompt_buf[prompt_buf_pos++] = '\n';
}
prompt_buf[prompt_buf_pos++] = key;
prompt_buf[prompt_buf_pos] = 0;
}
2013-08-16 19:11:09 +02:00
/* RETURN key: execute command */
else if (key == '\n') {
wprintw(self->window, "\n");
execute(self, m, prompt_buf);
prompt_buf_pos = 0;
prompt_buf[0] = 0;
}
2013-08-16 19:11:09 +02:00
/* BACKSPACE key: Remove one character from line */
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (prompt_buf_pos != 0)
2013-08-16 19:11:09 +02:00
prompt_buf[--prompt_buf_pos] = 0;
}
}
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;
int i;
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;
}
StatusBar *statusbar = (StatusBar *) self->stb;
werase(statusbar->topline);
2013-09-07 03:37:16 +02:00
if (statusbar->is_online) {
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);
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);
wprintw(statusbar->topline, "\n");
wattron(self->window, COLOR_PAIR(GREEN));
2013-08-16 19:11:09 +02:00
mvwprintw(self->window, y, 0, "# ");
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-08-16 19:11:09 +02:00
wrefresh(self->window);
}
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-02 19:19:25 +02:00
cmd_help(self, m, 0, NULL);
2013-08-16 19:11:09 +02:00
wclrtoeol(self->window);
}
void prompt_init_statusbar(ToxWindow *self, Tox *m)
{
int x, y;
getmaxyx(self->window, y, x);
/* Init statusbar info */
StatusBar *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'};
2013-09-08 09:18:34 +02:00
tox_getselfname(m, nick, TOX_MAX_NAME_LENGTH);
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);
/* Init statusbar subwindow */
statusbar->topline = subwin(self->window, 2, x, 0, 0);
}
ToxWindow new_prompt()
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 = &prompt_onKey;
ret.onDraw = &prompt_onDraw;
ret.onInit = &prompt_onInit;
ret.onFriendRequest = &prompt_onFriendRequest;
strcpy(ret.name, "prompt");
StatusBar *stb = calloc(1, sizeof(StatusBar));
2013-09-12 00:07:26 +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-08-16 19:11:09 +02:00
return ret;
}