2013-07-30 01:32:39 +02:00
|
|
|
/*
|
2013-08-06 12:16:17 +02:00
|
|
|
* Toxic -- Tox Curses Client
|
|
|
|
*/
|
2013-07-30 01:32:39 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <curses.h>
|
|
|
|
|
|
|
|
#include "../../core/Messenger.h"
|
|
|
|
#include "../../core/network.h"
|
|
|
|
|
|
|
|
#include "windows.h"
|
2013-08-13 00:50:43 +02:00
|
|
|
#include "prompt.h"
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-08 10:51:58 +02:00
|
|
|
uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX
|
2013-07-30 01:32:39 +02:00
|
|
|
uint8_t num_requests=0; // XXX
|
|
|
|
|
2013-08-15 12:11:48 +02:00
|
|
|
static friendAddedFn *on_friendadded_cb;
|
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;
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-09 16:31:29 +02:00
|
|
|
/* commands */
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_accept(ToxWindow *, Messenger *m, char **);
|
|
|
|
void cmd_add(ToxWindow *, Messenger *m, char **);
|
|
|
|
void cmd_clear(ToxWindow *, Messenger *m, char **);
|
|
|
|
void cmd_connect(ToxWindow *, Messenger *m, char **);
|
|
|
|
void cmd_help(ToxWindow *, Messenger *m, char **);
|
|
|
|
void cmd_msg(ToxWindow *, Messenger *m, char **);
|
|
|
|
void cmd_myid(ToxWindow *, Messenger *m, char **);
|
|
|
|
void cmd_nick(ToxWindow *, Messenger *m, char **);
|
|
|
|
void cmd_quit(ToxWindow *, Messenger *m, char **);
|
|
|
|
void cmd_status(ToxWindow *, Messenger *m, char **);
|
|
|
|
void cmd_statusmsg(ToxWindow *, Messenger *m, char **);
|
2013-08-09 16:31:29 +02:00
|
|
|
|
|
|
|
#define NUM_COMMANDS 13
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
char *name;
|
|
|
|
int numargs;
|
2013-08-11 05:24:11 +02:00
|
|
|
void (*func)(ToxWindow *, Messenger *m, char **);
|
2013-08-09 16:31:29 +02:00
|
|
|
} commands[] = {
|
|
|
|
{ "accept", 1, cmd_accept },
|
2013-08-11 02:10:03 +02:00
|
|
|
{ "add", 1, cmd_add },
|
2013-08-09 16:31:29 +02:00
|
|
|
{ "clear", 0, cmd_clear },
|
|
|
|
{ "connect", 3, cmd_connect },
|
|
|
|
{ "exit", 0, cmd_quit },
|
|
|
|
{ "help", 0, cmd_help },
|
|
|
|
{ "msg", 2, cmd_msg },
|
|
|
|
{ "myid", 0, cmd_myid },
|
|
|
|
{ "nick", 1, cmd_nick },
|
|
|
|
{ "q", 0, cmd_quit },
|
|
|
|
{ "quit", 0, cmd_quit },
|
|
|
|
{ "status", 2, cmd_status },
|
|
|
|
{ "statusmsg", 1, cmd_statusmsg },
|
|
|
|
};
|
|
|
|
|
2013-07-30 01:32:39 +02:00
|
|
|
// XXX:
|
2013-08-07 00:27:51 +02:00
|
|
|
int add_req(uint8_t *public_key)
|
|
|
|
{
|
2013-07-30 01:32:39 +02:00
|
|
|
memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
|
|
|
|
++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-07-30 01:32:39 +02:00
|
|
|
{
|
2013-08-07 00:27:51 +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-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_accept(ToxWindow *self, Messenger *m, char **args)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-09 16:31:29 +02:00
|
|
|
int num = atoi(args[1]);
|
|
|
|
if (num >= num_requests) {
|
|
|
|
wprintw(self->window, "Invalid syntax.\n");
|
|
|
|
return;
|
2013-08-07 00:27:51 +02:00
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
num = m_addfriend_norequest(m, pending_requests[num]);
|
2013-08-09 16:31:29 +02:00
|
|
|
if (num == -1)
|
|
|
|
wprintw(self->window, "Failed to add friend.\n");
|
|
|
|
else {
|
|
|
|
wprintw(self->window, "Friend accepted as: %d.\n", num);
|
2013-08-15 12:11:48 +02:00
|
|
|
on_friendadded_cb(m, num);
|
2013-08-07 09:24:37 +02:00
|
|
|
}
|
2013-08-09 16:31:29 +02:00
|
|
|
}
|
2013-08-07 08:25:20 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_add(ToxWindow *self, Messenger *m, char **args)
|
2013-08-09 16:31:29 +02:00
|
|
|
{
|
2013-08-13 15:32:31 +02:00
|
|
|
uint8_t id_bin[FRIEND_ADDRESS_SIZE];
|
2013-08-09 16:31:29 +02:00
|
|
|
char xx[3];
|
|
|
|
uint32_t x;
|
|
|
|
char *id = args[1];
|
|
|
|
char *msg = args[2];
|
|
|
|
|
|
|
|
if (!id) {
|
|
|
|
wprintw(self->window, "Invalid command: add expected at least one argument.\n");
|
|
|
|
return;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
2013-08-09 16:31:29 +02:00
|
|
|
if (!msg)
|
|
|
|
msg = "";
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-13 15:32:31 +02:00
|
|
|
if (strlen(id) != 2*FRIEND_ADDRESS_SIZE) {
|
2013-08-09 16:31:29 +02:00
|
|
|
wprintw(self->window, "Invalid ID length.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int i;
|
2013-08-13 15:32:31 +02:00
|
|
|
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
|
2013-08-09 16:31:29 +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");
|
2013-07-30 01:32:39 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-08-09 16:31:29 +02:00
|
|
|
id_bin[i] = x;
|
|
|
|
}
|
2013-08-14 12:23:38 +02:00
|
|
|
|
|
|
|
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
|
|
|
|
id[i] = toupper(id[i]);
|
|
|
|
}
|
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
int num = m_addfriend(m, id_bin, (uint8_t*) msg, strlen(msg)+1);
|
2013-08-09 16:31:29 +02:00
|
|
|
switch (num) {
|
|
|
|
case FAERR_TOOLONG:
|
|
|
|
wprintw(self->window, "Message is too long.\n");
|
|
|
|
break;
|
|
|
|
case FAERR_NOMESSAGE:
|
|
|
|
wprintw(self->window, "Please add a message to your request.\n");
|
|
|
|
break;
|
|
|
|
case FAERR_OWNKEY:
|
|
|
|
wprintw(self->window, "That appears to be your own ID.\n");
|
|
|
|
break;
|
|
|
|
case FAERR_ALREADYSENT:
|
|
|
|
wprintw(self->window, "Friend request already sent.\n");
|
|
|
|
break;
|
|
|
|
case FAERR_UNKNOWN:
|
|
|
|
wprintw(self->window, "Undefined error when adding friend.\n");
|
|
|
|
break;
|
2013-08-14 11:16:02 +02:00
|
|
|
case FAERR_BADCHECKSUM:
|
|
|
|
wprintw(self->window, "Bad checksum in address.\n");
|
|
|
|
break;
|
|
|
|
case FAERR_SETNEWNOSPAM:
|
|
|
|
wprintw(self->window, "Nospam was different.\n");
|
|
|
|
break;
|
2013-08-09 16:31:29 +02:00
|
|
|
default:
|
|
|
|
wprintw(self->window, "Friend added as %d.\n", num);
|
2013-08-15 12:11:48 +02:00
|
|
|
on_friendadded_cb(m, num);
|
2013-08-09 16:31:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_clear(ToxWindow *self, Messenger *m, char **args)
|
2013-08-09 16:31:29 +02:00
|
|
|
{
|
|
|
|
wclear(self->window);
|
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_connect(ToxWindow *self, Messenger *m, char **args)
|
2013-08-09 16:31:29 +02:00
|
|
|
{
|
|
|
|
IP_Port dht;
|
|
|
|
char *ip = args[1];
|
|
|
|
char *port = args[2];
|
|
|
|
char *key = args[3];
|
|
|
|
|
|
|
|
if (atoi(port) == 0) {
|
|
|
|
wprintw(self->window, "Invalid syntax.\n");
|
|
|
|
return;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-09 16:31:29 +02:00
|
|
|
dht.port = htons(atoi(port));
|
|
|
|
uint32_t resolved_address = resolve_addr(ip);
|
|
|
|
if (resolved_address == 0) {
|
|
|
|
return;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-09 16:31:29 +02:00
|
|
|
dht.ip.i = resolved_address;
|
|
|
|
unsigned char *binary_string = hex_string_to_bin(key);
|
|
|
|
DHT_bootstrap(dht, binary_string);
|
|
|
|
free(binary_string);
|
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_quit(ToxWindow *self, Messenger *m, char **args)
|
2013-08-09 16:31:29 +02:00
|
|
|
{
|
|
|
|
endwin();
|
|
|
|
exit(0);
|
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_help(ToxWindow *self, Messenger *m, char **args)
|
2013-08-09 16:31:29 +02:00
|
|
|
{
|
|
|
|
wclear(self->window);
|
|
|
|
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
|
|
|
|
wprintw(self->window, "Commands:\n");
|
|
|
|
wattroff(self->window, A_BOLD);
|
2013-08-07 09:14:10 +02:00
|
|
|
|
2013-08-09 16:31:29 +02:00
|
|
|
wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
|
|
|
|
wprintw(self->window, " add <id> <message> : Add friend\n");
|
|
|
|
wprintw(self->window, " status <type> <message> : Set your status\n");
|
|
|
|
wprintw(self->window, " statusmsg <message> : Set your status\n");
|
|
|
|
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");
|
|
|
|
wprintw(self->window, " quit/exit : Exit program\n");
|
|
|
|
wprintw(self->window, " help : Print this message again\n");
|
|
|
|
wprintw(self->window, " clear : Clear this window\n");
|
2013-08-11 05:24:11 +02:00
|
|
|
|
2013-08-09 16:31:29 +02:00
|
|
|
wattron(self->window, A_BOLD);
|
|
|
|
wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");
|
|
|
|
wattroff(self->window, A_BOLD);
|
2013-08-07 09:14:10 +02:00
|
|
|
|
2013-08-09 16:31:29 +02:00
|
|
|
wattroff(self->window, COLOR_PAIR(2));
|
|
|
|
}
|
2013-08-07 09:14:10 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_msg(ToxWindow *self, Messenger *m, char **args)
|
2013-08-09 16:31:29 +02:00
|
|
|
{
|
|
|
|
char *id = args[1];
|
|
|
|
char *msg = args[2];
|
2013-08-11 05:24:11 +02:00
|
|
|
if (m_sendmessage(m, atoi(id), (uint8_t*) msg, strlen(msg)+1) == 0)
|
2013-08-09 16:31:29 +02:00
|
|
|
wprintw(self->window, "Error occurred while sending message.\n");
|
|
|
|
else
|
|
|
|
wprintw(self->window, "Message successfully sent.\n");
|
|
|
|
}
|
2013-08-07 09:14:10 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_myid(ToxWindow *self, Messenger *m, char **args)
|
2013-08-09 16:31:29 +02:00
|
|
|
{
|
2013-08-13 15:32:31 +02:00
|
|
|
char id[FRIEND_ADDRESS_SIZE*2 + 1] = {0};
|
2013-08-09 16:31:29 +02:00
|
|
|
size_t i;
|
2013-08-13 15:32:31 +02:00
|
|
|
uint8_t address[FRIEND_ADDRESS_SIZE];
|
|
|
|
getaddress(m, address);
|
|
|
|
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
|
2013-08-09 16:31:29 +02:00
|
|
|
char xx[3];
|
2013-08-13 15:32:31 +02:00
|
|
|
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
|
2013-08-09 16:31:29 +02:00
|
|
|
strcat(id, xx);
|
2013-08-07 09:14:10 +02:00
|
|
|
}
|
2013-08-14 22:02:21 +02:00
|
|
|
wprintw(self->window, "%s\n", id);
|
2013-08-09 16:31:29 +02:00
|
|
|
}
|
2013-08-07 09:14:10 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_nick(ToxWindow *self, Messenger *m, char **args)
|
2013-08-09 16:31:29 +02:00
|
|
|
{
|
|
|
|
char *nick = args[1];
|
2013-08-11 05:24:11 +02:00
|
|
|
setname(m, (uint8_t*) nick, strlen(nick)+1);
|
2013-08-09 16:31:29 +02:00
|
|
|
wprintw(self->window, "Nickname set to: %s\n", nick);
|
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_status(ToxWindow *self, Messenger *m, char **args)
|
2013-08-09 16:31:29 +02:00
|
|
|
{
|
|
|
|
char *status = args[1];
|
|
|
|
char *status_text;
|
|
|
|
|
|
|
|
USERSTATUS status_kind;
|
|
|
|
if (!strncmp(status, "online", strlen("online"))) {
|
|
|
|
status_kind = USERSTATUS_NONE;
|
|
|
|
status_text = "ONLINE";
|
|
|
|
}
|
|
|
|
else if (!strncmp(status, "away", strlen("away"))) {
|
|
|
|
status_kind = USERSTATUS_AWAY;
|
|
|
|
status_text = "AWAY";
|
|
|
|
}
|
|
|
|
else if (!strncmp(status, "busy", strlen("busy"))) {
|
|
|
|
status_kind = USERSTATUS_BUSY;
|
|
|
|
status_text = "BUSY";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wprintw(self->window, "Invalid status.\n");
|
|
|
|
return;
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-09 16:31:29 +02:00
|
|
|
char *msg = args[2];
|
|
|
|
if (msg == NULL) {
|
2013-08-11 05:24:11 +02:00
|
|
|
m_set_userstatus(m, status_kind);
|
2013-08-09 16:31:29 +02:00
|
|
|
wprintw(self->window, "Status set to: %s\n", status_text);
|
|
|
|
}
|
|
|
|
else {
|
2013-08-11 05:24:11 +02:00
|
|
|
m_set_userstatus(m, status_kind);
|
|
|
|
m_set_statusmessage(m, (uint8_t*) msg, strlen(msg)+1);
|
2013-08-09 16:31:29 +02:00
|
|
|
wprintw(self->window, "Status set to: %s, %s\n", status_text, msg);
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
void cmd_statusmsg(ToxWindow *self, Messenger *m, char **args)
|
2013-08-09 16:31:29 +02:00
|
|
|
{
|
|
|
|
char *msg = args[1];
|
2013-08-11 05:24:11 +02:00
|
|
|
m_set_statusmessage(m, (uint8_t*) msg, strlen(msg)+1);
|
2013-08-09 16:31:29 +02:00
|
|
|
wprintw(self->window, "Status set to: %s\n", msg);
|
|
|
|
}
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
|
2013-08-09 16:31:29 +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-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-09 16:31:29 +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';
|
|
|
|
|
2013-08-11 02:10:03 +02:00
|
|
|
/* insert \0 at argument boundaries */
|
|
|
|
int numargs = 0;
|
|
|
|
for (i = 0; i < MAX_STR_SIZE; i++) {
|
|
|
|
if (cmd[i] == '\"')
|
|
|
|
while (cmd[++i] != '\"'); /* skip over strings */
|
|
|
|
if (cmd[i] == ' ') {
|
|
|
|
cmd[i] = '\0';
|
|
|
|
numargs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* excessive arguments */
|
|
|
|
if (numargs > 3) {
|
|
|
|
wprintw(self->window, "Invalid command: too many arguments.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read arguments into array */
|
|
|
|
char *cmdargs[5];
|
|
|
|
int pos = 0;
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
cmdargs[i] = cmd + pos;
|
|
|
|
pos += strlen(cmdargs[i]) + 1;
|
|
|
|
}
|
2013-08-09 16:31:29 +02:00
|
|
|
|
|
|
|
/* no input */
|
2013-08-11 02:10:03 +02:00
|
|
|
if (strlen(cmdargs[0]) == 0)
|
2013-08-09 16:31:29 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* match input to command list */
|
|
|
|
for (i = 0; i < NUM_COMMANDS; i++) {
|
2013-08-11 02:10:03 +02:00
|
|
|
if (!strcmp(cmdargs[0], commands[i].name)) {
|
|
|
|
/* check for missing arguments */
|
2013-08-09 16:31:29 +02:00
|
|
|
int j;
|
2013-08-11 02:10:03 +02:00
|
|
|
for (j = 0; j <= commands[i].numargs; j++) {
|
|
|
|
if (strlen(cmdargs[j]) == 0) {
|
2013-08-09 16:31:29 +02:00
|
|
|
wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n",
|
|
|
|
commands[i].name, commands[i].numargs, j - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-08-11 05:24:11 +02:00
|
|
|
/* check for excess arguments */
|
2013-08-11 02:10:03 +02:00
|
|
|
if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) {
|
2013-08-09 16:31:29 +02:00
|
|
|
wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* pass arguments to command function */
|
2013-08-11 05:24:11 +02:00
|
|
|
(commands[i].func)(self, m, cmdargs);
|
2013-07-30 01:32:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-08-09 16:31:29 +02:00
|
|
|
|
|
|
|
/* no match */
|
|
|
|
wprintw(self->window, "Invalid command.\n");
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
static void prompt_onKey(ToxWindow *self, Messenger *m, int key)
|
2013-08-07 00:27:51 +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';
|
2013-08-06 12:16:17 +02:00
|
|
|
}
|
2013-08-07 00:27:51 +02:00
|
|
|
else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
|
|
|
|
&& ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
|
|
|
|
prompt_buf[prompt_buf_pos++] = '\n';
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
prompt_buf[prompt_buf_pos++] = key;
|
|
|
|
prompt_buf[prompt_buf_pos] = 0;
|
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
/* RETURN key: execute command */
|
|
|
|
else if (key == '\n') {
|
2013-07-30 01:32:39 +02:00
|
|
|
wprintw(self->window, "\n");
|
2013-08-11 05:24:11 +02:00
|
|
|
execute(self, m, prompt_buf);
|
2013-07-30 01:32:39 +02:00
|
|
|
prompt_buf_pos = 0;
|
|
|
|
prompt_buf[0] = 0;
|
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
/* BACKSPACE key: Remove one character from line */
|
|
|
|
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
|
|
|
|
if (prompt_buf_pos != 0) {
|
2013-07-30 01:32:39 +02:00
|
|
|
prompt_buf[--prompt_buf_pos] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
static void prompt_onDraw(ToxWindow *self)
|
|
|
|
{
|
|
|
|
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(1));
|
|
|
|
mvwprintw(self->window, y, 0, "# ");
|
|
|
|
wattroff(self->window, COLOR_PAIR(1));
|
|
|
|
mvwprintw(self->window, y, 2, "%s", prompt_buf);
|
|
|
|
wclrtoeol(self->window);
|
|
|
|
wrefresh(self->window);
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-11 05:24:11 +02:00
|
|
|
static void prompt_onInit(ToxWindow *self, Messenger *m)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-07-30 01:32:39 +02:00
|
|
|
scrollok(self->window, 1);
|
2013-08-11 05:24:11 +02:00
|
|
|
cmd_help(self, m, NULL);
|
2013-07-30 01:32:39 +02:00
|
|
|
wclrtoeol(self->window);
|
|
|
|
}
|
|
|
|
|
2013-08-13 00:50:43 +02:00
|
|
|
ToxWindow new_prompt(friendAddedFn *f)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-15 12:11:48 +02:00
|
|
|
on_friendadded_cb = f;
|
2013-07-30 01:32:39 +02:00
|
|
|
ToxWindow ret;
|
2013-07-31 19:20:03 +02:00
|
|
|
memset(&ret, 0, sizeof(ret));
|
2013-07-30 01:32:39 +02:00
|
|
|
ret.onKey = &prompt_onKey;
|
|
|
|
ret.onDraw = &prompt_onDraw;
|
|
|
|
ret.onInit = &prompt_onInit;
|
|
|
|
strcpy(ret.title, "[prompt]");
|
|
|
|
return ret;
|
|
|
|
}
|