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

571 lines
15 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
extern int store_data(Tox *m, char *path);
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-08-08 10:51:58 +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_mynick(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 14
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 },
{ "mynick", cmd_mynick },
{ "q", cmd_quit },
{ "quit", cmd_quit },
{ "status", cmd_status },
{ "note", cmd_note },
};
// 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);
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)
{
2013-09-03 07:02:58 +02:00
if (argc != 1 && argc != 2) {
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;
2013-09-02 19:19:25 +02:00
char *id;
char *msg;
int i, num;
2013-08-16 19:11:09 +02:00
2013-09-02 19:19:25 +02:00
id = argv[1];
if (argc == 2) {
2013-09-03 07:02:58 +02:00
if (argv[2][0] != '\"') {
wprintw(self->window, "Messages must be enclosed in quotes.\n");
return;
}
msg = argv[2];
msg[strlen(++msg)-1] = L'\0';
} else
msg = "";
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]);
}
2013-09-02 19:19:25 +02:00
num = tox_addfriend(m, id_bin, (uint8_t *) 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);
}
2013-09-02 19:19:25 +02:00
void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv)
{
2013-08-23 23:03:44 +02:00
tox_IP_Port dht;
2013-09-02 19:19:25 +02:00
char *ip, *port, *key;
/* check arguments */
if (argc != 3) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
ip = argv[1];
port = argv[2];
key = argv[3];
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)
{
2013-08-16 19:11:09 +02:00
endwin();
2013-09-04 08:05:36 +02:00
store_data(m, DATA_FILE);
2013-09-04 06:58:23 +02:00
tox_kill(m);
2013-08-16 19:11:09 +02:00
exit(0);
}
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);
2013-08-16 19:11:09 +02:00
wprintw(self->window, "Commands:\n");
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, " mynick : Print your current nickname\n");
2013-08-16 19:11:09 +02:00
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
char *id, *msg;
/* check arguments */
if (argc != 2) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
id = argv[1];
msg = argv[2];
msg[strlen(++msg)-1] = L'\0';
2013-08-16 19:11:09 +02:00
2013-08-24 00:21:01 +02:00
if (tox_sendmessage(m, atoi(id), (uint8_t *) 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};
2013-08-16 19:11:09 +02:00
size_t i;
2013-08-23 23:03:44 +02:00
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-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
char *nick;
/* check arguments */
if (argc != 1) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
nick = argv[1];
if (nick[0] == '\"')
nick[strlen(++nick)-1] = L'\0';
2013-09-02 19:19:25 +02:00
2013-08-24 00:21:01 +02:00
tox_setname(m, (uint8_t *) nick, strlen(nick) + 1);
2013-08-16 19:11:09 +02:00
wprintw(self->window, "Nickname set to: %s\n", nick);
2013-08-18 10:48:36 +02:00
if (store_data(m, DATA_FILE)) {
wprintw(self->window, "\nCould not store Messenger data\n");
}
}
2013-09-02 19:19:25 +02:00
void cmd_mynick(ToxWindow *self, Tox *m, int argc, char **argv)
{
2013-08-23 23:03:44 +02:00
uint8_t *nick = malloc(TOX_MAX_NAME_LENGTH);
tox_getselfname(m, nick, TOX_MAX_NAME_LENGTH);
wprintw(self->window, "Current nickname: %s\n", nick);
free(nick);
}
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)
{
2013-09-02 19:19:25 +02:00
if (argc != 1 && argc != 2) {
wprintw(self->window, "Wrong number of arguments.\n");
return;
2013-09-02 19:19:25 +02:00
}
2013-09-03 06:20:17 +02:00
char *status, *status_text;
char *msg = NULL;
/* check arguments */
if (argc == 2) {
msg = argv[2];
if (msg[0] != '\"') {
wprintw(self->window, "Messages must be enclosed in quotes.\n");
return;
}
}
2013-09-02 19:19:25 +02:00
status = argv[1];
2013-08-16 19:11:09 +02:00
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;
wprintw(self->window, "Status set to: ");
wattron(self->window, COLOR_PAIR(GREEN) | A_BOLD);
wprintw(self->window, "[Online]\n");
wattroff(self->window, COLOR_PAIR(GREEN) | A_BOLD);
}
else if (!strncmp(status, "away", strlen("away"))) {
2013-08-23 23:03:44 +02:00
status_kind = TOX_USERSTATUS_AWAY;
wprintw(self->window, "Status set to: ");
wattron(self->window, COLOR_PAIR(YELLOW) | A_BOLD);
wprintw(self->window, "[Away]\n");
wattroff(self->window, COLOR_PAIR(YELLOW) | A_BOLD);
}
else if (!strncmp(status, "busy", strlen("busy"))) {
2013-08-23 23:03:44 +02:00
status_kind = TOX_USERSTATUS_BUSY;
wprintw(self->window, "Status set to: ");
wattron(self->window, COLOR_PAIR(RED) | A_BOLD);
wprintw(self->window, "[Busy]\n");
wattroff(self->window, COLOR_PAIR(RED) | A_BOLD);
2013-08-16 19:11:09 +02:00
}
else
wprintw(self->window, "Invalid status.\n");
tox_set_userstatus(m, status_kind);
if (msg != NULL) {
msg[strlen(++msg)-1] = L'\0'; /* remove opening and closing quotes */
2013-08-24 00:21:01 +02:00
tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
wprintw(self->window, "Personal note set to: %s\n", 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-02 19:19:25 +02:00
char *msg;
/* check arguments */
if (argv[1] && argv[1][0] != '\"') {
wprintw(self->window, "Messages must be enclosed in quotes.\n");
return;
}
2013-09-02 19:19:25 +02:00
msg = argv[1];
msg[strlen(++msg)-1] = L'\0';
2013-09-02 19:19:25 +02:00
2013-08-24 00:21:01 +02:00
tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
wprintw(self->window, "Personal note set to: %s\n", 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;
char cmd[MAX_STR_SIZE] = {0};
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-02 19:19:25 +02:00
wprintw(self->window, "Invalid command: did you forget a closing \"?\n");
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) {
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-02 19:19:25 +02:00
if (!cmdargs[0])
2013-08-16 19:11:09 +02:00
return;
/* 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-08-16 19:11:09 +02:00
return;
}
}
/* no match */
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) {
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;
getyx(self->window, y, x);
(void) x;
int i;
for (i = 0; i < (strlen(prompt_buf)); ++i) {
if ((prompt_buf[i] == '\n') && (y != 0))
--y;
}
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);
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);
}
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;
strcpy(ret.name, "prompt");
2013-08-16 19:11:09 +02:00
return ret;
}