mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-13 02:33:03 +01:00
Merge pull request #42 from JFreegman/master
Refactored prompt command parser to work with all window types and moved command stuff to separate files
This commit is contained in:
commit
39f71c3819
@ -13,7 +13,9 @@ toxic_SOURCES = $(top_srcdir)/src/main.c \
|
||||
$(top_srcdir)/src/toxic_windows.h \
|
||||
$(top_srcdir)/src/windows.c \
|
||||
$(top_srcdir)/src/groupchat.c \
|
||||
$(top_srcdir)/src/groupchat.h
|
||||
$(top_srcdir)/src/groupchat.h \
|
||||
$(top_srcdir)/src/commands.c \
|
||||
$(top_srcdir)/src/commands.h
|
||||
|
||||
toxic_CFLAGS = -I$(top_srcdir) \
|
||||
$(NCURSES_CFLAGS) \
|
||||
|
@ -2,7 +2,7 @@
|
||||
# Process this file with autoconf to produce a configure script.
|
||||
|
||||
AC_PREREQ([2.65])
|
||||
AC_INIT([toxic], [0.2.0], [http://tox.im/])
|
||||
AC_INIT([toxic], [0.2.1], [http://tox.im/])
|
||||
AC_CONFIG_AUX_DIR(configure_aux)
|
||||
AC_CONFIG_SRCDIR([src/main.c])
|
||||
AC_CONFIG_HEADERS([config.h])
|
||||
|
210
src/chat.c
210
src/chat.c
@ -17,6 +17,7 @@
|
||||
#include "toxic_windows.h"
|
||||
#include "friendlist.h"
|
||||
#include "chat.h"
|
||||
#include "commands.h"
|
||||
|
||||
extern char *DATA_FILE;
|
||||
extern int store_data(Tox *m, char *path);
|
||||
@ -168,173 +169,54 @@ char *wc_to_char(wchar_t ch)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void print_help(ChatContext *self)
|
||||
static void print_chat_help(ChatContext *ctx)
|
||||
{
|
||||
wattron(self->history, COLOR_PAIR(CYAN) | A_BOLD);
|
||||
wprintw(self->history, "Commands:\n");
|
||||
wattroff(self->history, A_BOLD);
|
||||
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
||||
wprintw(ctx->history, "Chat commands:\n");
|
||||
wattroff(ctx->history, A_BOLD);
|
||||
|
||||
wprintw(self->history, " /status <type> <message> : Set your status with optional note\n");
|
||||
wprintw(self->history, " /note <message> : Set a personal note\n");
|
||||
wprintw(self->history, " /nick <nickname> : Set your nickname\n");
|
||||
wprintw(self->history, " /me <action> : Do an action\n");
|
||||
wprintw(self->history, " /myid : Print your ID\n");
|
||||
wprintw(self->history, " /clear : Clear the screen\n");
|
||||
wprintw(self->history, " /close : Close the current chat window\n");
|
||||
wprintw(self->history, " /quit or /exit : Exit Toxic\n");
|
||||
wprintw(self->history, " /help : Print this message again\n\n");
|
||||
|
||||
wattroff(self->history, COLOR_PAIR(CYAN));
|
||||
wprintw(ctx->history, " /status <type> <message> : Set your status with optional note\n");
|
||||
wprintw(ctx->history, " /note <message> : Set a personal note\n");
|
||||
wprintw(ctx->history, " /nick <nickname> : Set your nickname\n");
|
||||
wprintw(ctx->history, " /invite <nickname> <n> : Invite friend to a groupchat\n");
|
||||
wprintw(ctx->history, " /me <action> : Do an action\n");
|
||||
wprintw(ctx->history, " /myid : Print your ID\n");
|
||||
wprintw(ctx->history, " /clear : Clear the screen\n");
|
||||
wprintw(ctx->history, " /close : Close the current chat window\n");
|
||||
wprintw(ctx->history, " /quit or /exit : Exit Toxic\n");
|
||||
wprintw(ctx->history, " /help : Print this message again\n");
|
||||
|
||||
wattron(ctx->history, A_BOLD);
|
||||
wprintw(ctx->history, "\n * Messages must be enclosed in quotation marks.\n");
|
||||
wattroff(ctx->history, A_BOLD);
|
||||
|
||||
wattroff(ctx->history, COLOR_PAIR(CYAN));
|
||||
}
|
||||
|
||||
static void execute(ToxWindow *self, ChatContext *ctx, StatusBar *statusbar, Tox *m, char *cmd)
|
||||
{
|
||||
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
|
||||
wclear(self->window);
|
||||
wclear(ctx->history);
|
||||
wprintw(ctx->history, "\n\n");
|
||||
int x, y;
|
||||
getmaxyx(self->window, y, x);
|
||||
(void) x;
|
||||
wmove(self->window, y - CURS_Y_OFFSET, 0);
|
||||
static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *action) {
|
||||
struct tm *timeinfo = get_time();
|
||||
|
||||
if (action == NULL) {
|
||||
wprintw(ctx->history, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
|
||||
print_help(ctx);
|
||||
wattron(ctx->history, COLOR_PAIR(CYAN));
|
||||
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
||||
wattroff(ctx->history, COLOR_PAIR(CYAN));
|
||||
|
||||
else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
|
||||
exit_toxic(m);
|
||||
uint8_t selfname[TOX_MAX_NAME_LENGTH];
|
||||
tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH);
|
||||
|
||||
wattron(ctx->history, COLOR_PAIR(YELLOW));
|
||||
wprintw(ctx->history, "* %s %s\n", selfname, action);
|
||||
wattroff(ctx->history, COLOR_PAIR(YELLOW));
|
||||
|
||||
if (tox_sendaction(m, self->num, action, strlen(action) + 1) == 0) {
|
||||
wattron(ctx->history, COLOR_PAIR(RED));
|
||||
wprintw(ctx->history, " * Failed to send action\n");
|
||||
wattroff(ctx->history, COLOR_PAIR(RED));
|
||||
}
|
||||
|
||||
else if (!strncmp(cmd, "/me ", strlen("/me "))) {
|
||||
struct tm *timeinfo = get_time();
|
||||
uint8_t *action = strchr(cmd, ' ');
|
||||
|
||||
if (action == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
action++;
|
||||
|
||||
wattron(ctx->history, COLOR_PAIR(CYAN));
|
||||
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
||||
wattroff(ctx->history, COLOR_PAIR(CYAN));
|
||||
|
||||
uint8_t selfname[TOX_MAX_NAME_LENGTH];
|
||||
tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH);
|
||||
|
||||
wattron(ctx->history, COLOR_PAIR(YELLOW));
|
||||
wprintw(ctx->history, "* %s %s\n", selfname, action);
|
||||
wattroff(ctx->history, COLOR_PAIR(YELLOW));
|
||||
|
||||
if (!statusbar->is_online
|
||||
|| tox_sendaction(m, self->num, action, strlen(action) + 1) == 0) {
|
||||
wattron(ctx->history, COLOR_PAIR(RED));
|
||||
wprintw(ctx->history, " * Failed to send action\n");
|
||||
wattroff(ctx->history, COLOR_PAIR(RED));
|
||||
}
|
||||
}
|
||||
|
||||
else if (!strncmp(cmd, "/status ", strlen("/status "))) {
|
||||
char *status = strchr(cmd, ' ');
|
||||
|
||||
if (status == NULL) {
|
||||
wprintw(ctx->history, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
status++;
|
||||
TOX_USERSTATUS status_kind;
|
||||
|
||||
if (!strncmp(status, "online", strlen("online"))) {
|
||||
status_kind = TOX_USERSTATUS_NONE;
|
||||
wprintw(ctx->history, "Status set to: ");
|
||||
wattron(ctx->history, COLOR_PAIR(GREEN) | A_BOLD);
|
||||
wprintw(ctx->history, "[Online]\n");
|
||||
wattroff(ctx->history, COLOR_PAIR(GREEN) | A_BOLD);
|
||||
}
|
||||
|
||||
else if (!strncmp(status, "away", strlen("away"))) {
|
||||
status_kind = TOX_USERSTATUS_AWAY;
|
||||
wprintw(ctx->history, "Status set to: ");
|
||||
wattron(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD);
|
||||
wprintw(ctx->history, "[Away]\n");
|
||||
wattroff(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD);
|
||||
}
|
||||
|
||||
else if (!strncmp(status, "busy", strlen("busy"))) {
|
||||
status_kind = TOX_USERSTATUS_BUSY;
|
||||
wprintw(ctx->history, "Status set to: ");
|
||||
wattron(ctx->history, COLOR_PAIR(RED) | A_BOLD);
|
||||
wprintw(ctx->history, "[Busy]\n");
|
||||
wattroff(ctx->history, COLOR_PAIR(RED) | A_BOLD);
|
||||
}
|
||||
|
||||
else {
|
||||
wprintw(ctx->history, "Invalid status.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
tox_set_userstatus(m, status_kind);
|
||||
prompt_update_status(self->prompt, status_kind);
|
||||
|
||||
uint8_t *msg = strchr(status, ' ');
|
||||
if (msg != NULL) {
|
||||
msg++;
|
||||
uint16_t len = strlen(msg) + 1;
|
||||
tox_set_statusmessage(m, msg, len);
|
||||
prompt_update_statusmessage(self->prompt, msg, len);
|
||||
wprintw(ctx->history, "Personal note set to: %s\n", msg);
|
||||
}
|
||||
}
|
||||
|
||||
else if (!strncmp(cmd, "/note ", strlen("/note "))) {
|
||||
uint8_t *msg = strchr(cmd, ' ');
|
||||
msg++;
|
||||
uint16_t len = strlen(msg) + 1;
|
||||
tox_set_statusmessage(m, msg, len);
|
||||
prompt_update_statusmessage(self->prompt, msg, len);
|
||||
wprintw(ctx->history, "Personal note set to: %s\n", msg);
|
||||
}
|
||||
|
||||
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
|
||||
uint8_t *nick = strchr(cmd, ' ');
|
||||
|
||||
if (nick == NULL) {
|
||||
wprintw(ctx->history, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int len = strlen(++nick);
|
||||
|
||||
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->prompt, nick, len+1);
|
||||
wprintw(ctx->history, "Nickname set to: %s\n", nick);
|
||||
}
|
||||
|
||||
else if (!strcmp(cmd, "/myid")) {
|
||||
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {'\0'};
|
||||
size_t i;
|
||||
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
||||
tox_getaddress(m, address);
|
||||
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
|
||||
char xx[3];
|
||||
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
|
||||
strcat(id, xx);
|
||||
}
|
||||
|
||||
wprintw(ctx->history, "%s\n", id);
|
||||
}
|
||||
|
||||
else
|
||||
wprintw(ctx->history, "Invalid command.\n");
|
||||
}
|
||||
|
||||
static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
|
||||
@ -388,8 +270,12 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
|
||||
delwin(statusbar->topline);
|
||||
del_window(self);
|
||||
disable_chatwin(f_num);
|
||||
} else
|
||||
execute(self, ctx, statusbar, m, line);
|
||||
} else if (!strncmp(line, "/me ", strlen("/me ")))
|
||||
send_action(self, ctx, m, line+4);
|
||||
else if (!strncmp(line, "/help", strlen("/help")))
|
||||
print_chat_help(ctx);
|
||||
else
|
||||
execute(ctx->history, self->prompt, m, line, ctx->pos);
|
||||
} else {
|
||||
/* make sure the string has at least non-space character */
|
||||
if (!string_is_empty(line)) {
|
||||
@ -524,7 +410,7 @@ static void chat_onInit(ToxWindow *self, Tox *m)
|
||||
scrollok(ctx->history, 1);
|
||||
ctx->linewin = subwin(self->window, 0, x, y-4, 0);
|
||||
wprintw(ctx->history, "\n\n");
|
||||
print_help(ctx);
|
||||
print_chat_help(ctx);
|
||||
wmove(self->window, y - CURS_Y_OFFSET, 0);
|
||||
}
|
||||
|
||||
|
516
src/commands.c
Normal file
516
src/commands.c
Normal file
@ -0,0 +1,516 @@
|
||||
/*
|
||||
* Toxic -- Tox Curses Client
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "toxic_windows.h"
|
||||
#include "prompt.h"
|
||||
#include "commands.h"
|
||||
|
||||
extern char *DATA_FILE;
|
||||
|
||||
extern uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
|
||||
extern uint8_t num_frnd_requests;
|
||||
|
||||
extern uint8_t pending_grp_requests[MAX_GROUPCHAT_NUM][TOX_CLIENT_ID_SIZE];
|
||||
extern uint8_t num_grp_requests;
|
||||
|
||||
/* command functions */
|
||||
void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
/* check arguments */
|
||||
if (argc != 1) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int num = atoi(argv[1]);
|
||||
|
||||
if (num < 0 || num >= num_frnd_requests) {
|
||||
wprintw(window, "No pending friend request with that number.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int friendnum = tox_addfriend_norequest(m, pending_frnd_requests[num]);
|
||||
|
||||
if (friendnum == -1)
|
||||
wprintw(window, "Failed to add friend.\n");
|
||||
else {
|
||||
wprintw(window, "Friend request accepted.\n");
|
||||
on_friendadded(m, friendnum);
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_add(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc < 1 || argc > 2) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *id = argv[1];
|
||||
|
||||
if (id == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *msg;
|
||||
|
||||
if (argc == 2) {
|
||||
msg = argv[2];
|
||||
|
||||
if (msg == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg[0] != '\"') {
|
||||
wprintw(window, "Messages must be enclosed in quotes.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
msg[strlen(++msg)-1] = L'\0';
|
||||
|
||||
} else
|
||||
msg = "Let's tox.";
|
||||
|
||||
if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) {
|
||||
wprintw(window, "Invalid ID length.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
size_t i;
|
||||
char xx[3];
|
||||
uint32_t x;
|
||||
uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE];
|
||||
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
|
||||
xx[0] = id[2 * i];
|
||||
xx[1] = id[2 * i + 1];
|
||||
xx[2] = '\0';
|
||||
|
||||
if (sscanf(xx, "%02x", &x) != 1) {
|
||||
wprintw(window, "Invalid ID.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
id_bin[i] = x;
|
||||
}
|
||||
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
|
||||
id[i] = toupper(id[i]);
|
||||
}
|
||||
|
||||
int num = tox_addfriend(m, id_bin, msg, strlen(msg) + 1);
|
||||
|
||||
switch (num) {
|
||||
case TOX_FAERR_TOOLONG:
|
||||
wprintw(window, "Message is too long.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_NOMESSAGE:
|
||||
wprintw(window, "Please add a message to your request.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_OWNKEY:
|
||||
wprintw(window, "That appears to be your own ID.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_ALREADYSENT:
|
||||
wprintw(window, "Friend request already sent.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_UNKNOWN:
|
||||
wprintw(window, "Undefined error when adding friend.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_BADCHECKSUM:
|
||||
wprintw(window, "Bad checksum in address.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_SETNEWNOSPAM:
|
||||
wprintw(window, "Nospam was different.\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
wprintw(window, "Friend added as %d.\n", num);
|
||||
on_friendadded(m, num);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_clear(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
wclear(window);
|
||||
wprintw(window, "\n\n");
|
||||
}
|
||||
|
||||
void cmd_connect(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
/* check arguments */
|
||||
if (argc != 3) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
tox_IP_Port dht;
|
||||
char *ip = argv[1];
|
||||
char *port = argv[2];
|
||||
char *key = argv[3];
|
||||
|
||||
if (ip == NULL || port == NULL || key == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (atoi(port) == 0) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *binary_string = hex_string_to_bin(key);
|
||||
tox_bootstrap_from_address(m, ip, TOX_ENABLE_IPV6_DEFAULT,
|
||||
htons(atoi(port)), binary_string);
|
||||
free(binary_string);
|
||||
}
|
||||
|
||||
void cmd_groupchat(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
int ngc = get_num_groupchats();
|
||||
|
||||
if (ngc < 0 || ngc > MAX_GROUPCHAT_NUM) {
|
||||
wprintw(window, "\nMaximum number of group chats has been reached.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int groupnum = tox_add_groupchat(m);
|
||||
|
||||
if (groupnum == -1) {
|
||||
wprintw(window, "Group chat failed to initialize.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (init_groupchat_win(prompt, m, groupnum) == -1) {
|
||||
wprintw(window, "Group chat failed to initialize.\n");
|
||||
tox_del_groupchat(m, groupnum);
|
||||
return;
|
||||
}
|
||||
|
||||
wprintw(window, "Group chat created as %d.\n", groupnum);
|
||||
}
|
||||
|
||||
void cmd_invite(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (argv[1] == NULL || argv[2] == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *friendname = argv[1];
|
||||
int groupnum = atoi(argv[2]);
|
||||
|
||||
if (friendname[0] == '\"')
|
||||
friendname[strlen(++friendname)-1] = L'\0';
|
||||
|
||||
int friendnum = get_friendnum(friendname);
|
||||
|
||||
if (friendnum == -1) {
|
||||
wprintw(window, "Friend '%s' not found.\n", friendname);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tox_invite_friend(m, friendnum, groupnum) == -1) {
|
||||
wprintw(window, "Failed to invite friend.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
wprintw(window, "Invited friend %s to group chat %d.\n", friendname, groupnum);
|
||||
}
|
||||
|
||||
void cmd_join(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (argv[1] == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int num = atoi(argv[1]);
|
||||
|
||||
if (num < 0 || num >= num_grp_requests) {
|
||||
wprintw(window, "No pending group chat invites with that number.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int groupnum = tox_join_groupchat(m, num, pending_grp_requests[num]);
|
||||
|
||||
if (groupnum == -1) {
|
||||
wprintw(window, "Group chat failed to initialize.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (init_groupchat_win(prompt, m, groupnum) == -1) {
|
||||
wprintw(window, "Group chat window failed to initialize.\n");
|
||||
tox_del_groupchat(m, groupnum);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_msg(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
/* check arguments */
|
||||
if (argc != 2) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *id = argv[1];
|
||||
uint8_t *msg = argv[2];
|
||||
|
||||
if (id == NULL || msg == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
msg[strlen(++msg)-1] = L'\0';
|
||||
|
||||
if (tox_sendmessage(m, atoi(id), msg, strlen(msg) + 1) == 0)
|
||||
wprintw(window, "Failed to send message.\n");
|
||||
else
|
||||
wprintw(window, "Message successfully sent.\n");
|
||||
}
|
||||
|
||||
void cmd_myid(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
|
||||
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
||||
tox_getaddress(m, address);
|
||||
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
|
||||
char xx[3];
|
||||
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
|
||||
strcat(id, xx);
|
||||
}
|
||||
|
||||
wprintw(window, "%s\n", id);
|
||||
}
|
||||
|
||||
void cmd_nick(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
/* check arguments */
|
||||
if (argc != 1) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *nick = argv[1];
|
||||
|
||||
if (nick == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int len = strlen(nick);
|
||||
|
||||
if (nick[0] == '\"') {
|
||||
++nick;
|
||||
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(prompt, nick, len+1);
|
||||
|
||||
store_data(m, DATA_FILE);
|
||||
}
|
||||
|
||||
void cmd_note(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
wprintw(window, "Wrong number of arguments.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *msg = argv[1];
|
||||
|
||||
if (msg == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg[0] != '\"') {
|
||||
wprintw(window, "Messages must be enclosed in quotes.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
msg[strlen(++msg)-1] = L'\0';
|
||||
uint16_t len = strlen(msg) + 1;
|
||||
tox_set_statusmessage(m, msg, len);
|
||||
prompt_update_statusmessage(prompt, msg, len);
|
||||
}
|
||||
|
||||
void cmd_quit(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
exit_toxic(m);
|
||||
}
|
||||
|
||||
void cmd_status(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
uint8_t *msg = NULL;
|
||||
|
||||
if (argc == 2) {
|
||||
|
||||
msg = argv[2];
|
||||
|
||||
if (msg == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg[0] != '\"') {
|
||||
wprintw(window, "Messages must be enclosed in quotes.\n");
|
||||
return;
|
||||
}
|
||||
} else if (argc != 1) {
|
||||
wprintw(window, "Wrong number of arguments.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *status = argv[1];
|
||||
|
||||
if (status == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
TOX_USERSTATUS status_kind;
|
||||
|
||||
if (!strncmp(status, "online", strlen("online")))
|
||||
status_kind = TOX_USERSTATUS_NONE;
|
||||
|
||||
else if (!strncmp(status, "away", strlen("away")))
|
||||
status_kind = TOX_USERSTATUS_AWAY;
|
||||
|
||||
else if (!strncmp(status, "busy", strlen("busy")))
|
||||
status_kind = TOX_USERSTATUS_BUSY;
|
||||
|
||||
else
|
||||
wprintw(window, "Invalid status.\n");
|
||||
|
||||
tox_set_userstatus(m, status_kind);
|
||||
prompt_update_status(prompt, status_kind);
|
||||
|
||||
if (msg != NULL) {
|
||||
msg[strlen(++msg)-1] = L'\0'; /* remove opening and closing quotes */
|
||||
uint16_t len = strlen(msg) + 1;
|
||||
tox_set_statusmessage(m, msg, len);
|
||||
prompt_update_statusmessage(prompt, msg, len);
|
||||
}
|
||||
}
|
||||
|
||||
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len)
|
||||
{
|
||||
int newlines = 0;
|
||||
char cmd[MAX_STR_SIZE] = {'\0'};
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < buf_len; ++i) {
|
||||
if (u_cmd[i] == '\n')
|
||||
++newlines;
|
||||
else
|
||||
cmd[i - newlines] = u_cmd[i];
|
||||
}
|
||||
|
||||
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++;
|
||||
}
|
||||
/* skip over strings */
|
||||
else if (cmd[i] == '\"') {
|
||||
while (cmd[++i] != '\"') {
|
||||
if (cmd[i] == '\0') {
|
||||
wprintw(window, "Invalid command: did you forget an opening or closing \"?\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* read arguments into array */
|
||||
char **cmdargs = malloc((numargs + 1) * sizeof(char *));
|
||||
if (!cmdargs) {
|
||||
wprintw(window, "Invalid command: too many arguments.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int pos = 0;
|
||||
|
||||
for (i = 0; i < numargs + 1; i++) {
|
||||
cmdargs[i] = cmd + pos;
|
||||
pos += strlen(cmdargs[i]) + 1;
|
||||
/* replace empty strings with NULL for easier error checking */
|
||||
if (strlen(cmdargs[i]) == 0)
|
||||
cmdargs[i] = NULL;
|
||||
}
|
||||
|
||||
/* no input */
|
||||
if (!cmdargs[0]) {
|
||||
free(cmdargs);
|
||||
return;
|
||||
}
|
||||
|
||||
/* match input to command list */
|
||||
for (i = 0; i < NUM_COMMANDS; i++) {
|
||||
if (!strcmp(cmdargs[0], commands[i].name)) {
|
||||
(commands[i].func)(window, prompt, m, numargs, cmdargs);
|
||||
free(cmdargs);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* no match */
|
||||
free(cmdargs);
|
||||
wprintw(window, "Invalid command.\n");
|
||||
}
|
43
src/commands.h
Normal file
43
src/commands.h
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Toxic -- Tox Curses Client
|
||||
*/
|
||||
|
||||
/* commands */
|
||||
void cmd_accept(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_add(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_clear(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_connect(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_groupchat(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_invite(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_join(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_msg(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_myid(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_nick(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_note(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_quit(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_status(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
|
||||
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len);
|
||||
|
||||
#define NUM_COMMANDS 15
|
||||
|
||||
static struct {
|
||||
char *name;
|
||||
void (*func)(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
} commands[] = {
|
||||
{ "/accept", cmd_accept },
|
||||
{ "/add", cmd_add },
|
||||
{ "/clear", cmd_clear },
|
||||
{ "/connect", cmd_connect },
|
||||
{ "/exit", cmd_quit },
|
||||
{ "/groupchat", cmd_groupchat },
|
||||
{ "/invite", cmd_invite },
|
||||
{ "/join", cmd_join },
|
||||
{ "/msg", cmd_msg },
|
||||
{ "/myid", cmd_myid },
|
||||
{ "/nick", cmd_nick },
|
||||
{ "/note", cmd_note },
|
||||
{ "/q", cmd_quit },
|
||||
{ "/quit", cmd_quit },
|
||||
{ "/status", cmd_status },
|
||||
};
|
@ -15,6 +15,7 @@
|
||||
|
||||
#include "toxic_windows.h"
|
||||
#include "chat.h"
|
||||
#include "commands.h"
|
||||
|
||||
static GroupChat groupchats[MAX_GROUPCHAT_NUM];
|
||||
static int group_chat_index = 0;
|
||||
@ -71,6 +72,31 @@ static void close_groupchatwin(Tox *m, int groupnum)
|
||||
group_chat_index = i;
|
||||
}
|
||||
|
||||
static void print_groupchat_help(ChatContext *ctx)
|
||||
{
|
||||
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
||||
wprintw(ctx->history, "Group chat commands:\n");
|
||||
wattroff(ctx->history, A_BOLD);
|
||||
|
||||
wprintw(ctx->history, " /add <id> <message> : Add friend with optional message\n");
|
||||
wprintw(ctx->history, " /status <type> <message> : Set your status with optional note\n");
|
||||
wprintw(ctx->history, " /note <message> : Set a personal note\n");
|
||||
wprintw(ctx->history, " /nick <nickname> : Set your nickname\n");
|
||||
wprintw(ctx->history, " /invite <nickname> <n> : Invite friend to a groupchat\n");
|
||||
wprintw(ctx->history, " /groupchat : Create a group chat\n");
|
||||
wprintw(ctx->history, " /myid : Print your ID\n");
|
||||
wprintw(ctx->history, " /clear : Clear the screen\n");
|
||||
wprintw(ctx->history, " /close : Close the current group chat\n");
|
||||
wprintw(ctx->history, " /quit or /exit : Exit Toxic\n");
|
||||
wprintw(ctx->history, " /help : Print this message again\n");
|
||||
|
||||
wattron(ctx->history, A_BOLD);
|
||||
wprintw(ctx->history, "\n * Messages must be enclosed in quotation marks.\n");
|
||||
wattroff(ctx->history, A_BOLD);
|
||||
|
||||
wattroff(ctx->history, COLOR_PAIR(CYAN));
|
||||
}
|
||||
|
||||
static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t *msg, uint16_t len)
|
||||
{
|
||||
if (self->num != groupnum)
|
||||
@ -138,14 +164,16 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
|
||||
bool close_win = false;
|
||||
|
||||
if (line[0] == '/') {
|
||||
if (close_win = !strncmp(line, "/close", strlen("/close"))) {
|
||||
if (close_win = strncmp(line, "/close", strlen(line)) == 0) {
|
||||
set_active_window(0);
|
||||
int groupnum = self->num;
|
||||
delwin(ctx->linewin);
|
||||
del_window(self);
|
||||
close_groupchatwin(m, groupnum);
|
||||
} //else
|
||||
//execute(self, ctx, statusbar, m, line);
|
||||
} else if (strncmp(line, "/help", strlen("/help")) == 0)
|
||||
print_groupchat_help(ctx);
|
||||
else
|
||||
execute(ctx->history, self->prompt, m, line, ctx->pos);
|
||||
} else {
|
||||
/* make sure the string has at least non-space character */
|
||||
if (!string_is_empty(line)) {
|
||||
@ -197,7 +225,7 @@ static void groupchat_onInit(ToxWindow *self, Tox *m)
|
||||
ctx->history = subwin(self->window, y-3, x, 0, 0);
|
||||
scrollok(ctx->history, 1);
|
||||
ctx->linewin = subwin(self->window, 2, x, y-4, 0);
|
||||
// print_help(ctx);
|
||||
print_groupchat_help(ctx);
|
||||
wmove(self->window, y - CURS_Y_OFFSET, 0);
|
||||
}
|
||||
|
||||
|
588
src/prompt.c
588
src/prompt.c
@ -11,9 +11,7 @@
|
||||
#include <ctype.h>
|
||||
|
||||
#include "prompt.h"
|
||||
#include "groupchat.h"
|
||||
|
||||
extern char *DATA_FILE;
|
||||
#include "commands.h"
|
||||
|
||||
uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
|
||||
uint8_t num_frnd_requests = 0;
|
||||
@ -24,46 +22,6 @@ uint8_t num_grp_requests = 0;
|
||||
static char prompt_buf[MAX_STR_SIZE] = {'\0'};
|
||||
static int prompt_buf_pos = 0;
|
||||
|
||||
/* commands */
|
||||
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_groupchat(ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_help(ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_invite(ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_join(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 16
|
||||
|
||||
static struct {
|
||||
char *name;
|
||||
void (*func)(ToxWindow *, Tox *m, int, char **);
|
||||
} commands[] = {
|
||||
{ "accept", cmd_accept },
|
||||
{ "add", cmd_add },
|
||||
{ "clear", cmd_clear },
|
||||
{ "connect", cmd_connect },
|
||||
{ "exit", cmd_quit },
|
||||
{ "groupchat", cmd_groupchat },
|
||||
{ "help", cmd_help },
|
||||
{ "invite", cmd_invite },
|
||||
{ "join", cmd_join },
|
||||
{ "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, uint16_t len)
|
||||
{
|
||||
@ -139,528 +97,34 @@ unsigned char *hex_string_to_bin(char hex_string[])
|
||||
return val;
|
||||
}
|
||||
|
||||
/* command functions */
|
||||
void cmd_accept(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
/* check arguments */
|
||||
if (argc != 1) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int num = atoi(argv[1]);
|
||||
|
||||
if (num < 0 || num >= num_frnd_requests) {
|
||||
wprintw(self->window, "No pending friend request with that number.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int friendnum = tox_addfriend_norequest(m, pending_frnd_requests[num]);
|
||||
|
||||
if (friendnum == -1)
|
||||
wprintw(self->window, "Failed to add friend.\n");
|
||||
else {
|
||||
wprintw(self->window, "Friend request accepted.\n");
|
||||
on_friendadded(m, friendnum);
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc < 1 || argc > 2) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *id = argv[1];
|
||||
|
||||
if (id == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *msg;
|
||||
|
||||
if (argc == 2) {
|
||||
msg = argv[2];
|
||||
|
||||
if (msg == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg[0] != '\"') {
|
||||
wprintw(self->window, "Messages must be enclosed in quotes.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
msg[strlen(++msg)-1] = L'\0';
|
||||
|
||||
} else
|
||||
msg = "Let's tox.";
|
||||
|
||||
if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) {
|
||||
wprintw(self->window, "Invalid ID length.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
size_t i;
|
||||
char xx[3];
|
||||
uint32_t x;
|
||||
uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE];
|
||||
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
|
||||
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;
|
||||
}
|
||||
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
|
||||
id[i] = toupper(id[i]);
|
||||
}
|
||||
|
||||
int num = tox_addfriend(m, id_bin, msg, strlen(msg) + 1);
|
||||
|
||||
switch (num) {
|
||||
case TOX_FAERR_TOOLONG:
|
||||
wprintw(self->window, "Message is too long.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_NOMESSAGE:
|
||||
wprintw(self->window, "Please add a message to your request.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_OWNKEY:
|
||||
wprintw(self->window, "That appears to be your own ID.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_ALREADYSENT:
|
||||
wprintw(self->window, "Friend request already sent.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_UNKNOWN:
|
||||
wprintw(self->window, "Undefined error when adding friend.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_BADCHECKSUM:
|
||||
wprintw(self->window, "Bad checksum in address.\n");
|
||||
break;
|
||||
|
||||
case TOX_FAERR_SETNEWNOSPAM:
|
||||
wprintw(self->window, "Nospam was different.\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
wprintw(self->window, "Friend added as %d.\n", num);
|
||||
on_friendadded(m, num);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_clear(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
wclear(self->window);
|
||||
wprintw(self->window, "\n\n");
|
||||
}
|
||||
|
||||
void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
/* check arguments */
|
||||
if (argc != 3) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
tox_IP_Port dht;
|
||||
char *ip = argv[1];
|
||||
char *port = argv[2];
|
||||
char *key = argv[3];
|
||||
|
||||
if (ip == NULL || port == NULL || key == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (atoi(port) == 0) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *binary_string = hex_string_to_bin(key);
|
||||
tox_bootstrap_from_address(m, ip, TOX_ENABLE_IPV6_DEFAULT,
|
||||
htons(atoi(port)), binary_string);
|
||||
free(binary_string);
|
||||
}
|
||||
|
||||
void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
exit_toxic(m);
|
||||
}
|
||||
|
||||
void cmd_groupchat(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
int ngc = get_num_groupchats();
|
||||
|
||||
if (ngc < 0 || ngc > MAX_GROUPCHAT_NUM) {
|
||||
wprintw(self->window, "\nMaximum number of group chats has been reached.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int groupnum = tox_add_groupchat(m);
|
||||
|
||||
if (groupnum == -1) {
|
||||
wprintw(self->window, "Group chat failed to initialize.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (init_groupchat_win(self, m, groupnum) == -1) {
|
||||
wprintw(self->window, "Group chat failed to initialize.\n");
|
||||
tox_del_groupchat(m, groupnum);
|
||||
return;
|
||||
}
|
||||
|
||||
wprintw(self->window, "Group chat created as %d.\n", groupnum);
|
||||
}
|
||||
|
||||
void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
void print_prompt_help(ToxWindow *self)
|
||||
{
|
||||
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, " connect <ip> <port> <key> : Connect to DHT server\n");
|
||||
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/exit : Exit Toxic\n");
|
||||
wprintw(self->window, " help : Print this message again\n");
|
||||
wprintw(self->window, " clear : Clear this window\n");
|
||||
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\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));
|
||||
}
|
||||
|
||||
void cmd_invite(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (argv[1] == NULL || argv[2] == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *friendname = argv[1];
|
||||
int groupnum = atoi(argv[2]);
|
||||
|
||||
if (friendname[0] == '\"')
|
||||
friendname[strlen(++friendname)-1] = L'\0';
|
||||
|
||||
int friendnum = get_friendnum(friendname);
|
||||
|
||||
if (friendnum == -1) {
|
||||
wprintw(self->window, "Friend '%s' not found.\n", friendname);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tox_invite_friend(m, friendnum, groupnum) == -1) {
|
||||
wprintw(self->window, "Failed to invite friend.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
wprintw(self->window, "Invited friend %s to group chat %d.\n", friendname, groupnum);
|
||||
}
|
||||
|
||||
void cmd_join(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (argv[1] == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int num = atoi(argv[1]);
|
||||
|
||||
if (num < 0 || num >= num_grp_requests) {
|
||||
wprintw(self->window, "No pending group chat invites with that number.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int groupnum = tox_join_groupchat(m, num, pending_grp_requests[num]);
|
||||
|
||||
if (groupnum == -1) {
|
||||
wprintw(self->window, "Group chat failed to initialize.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (init_groupchat_win(self, m, groupnum) == -1) {
|
||||
wprintw(self->window, "Group chat failed to initialize.\n");
|
||||
tox_del_groupchat(m, groupnum);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
/* check arguments */
|
||||
if (argc != 2) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
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';
|
||||
|
||||
if (tox_sendmessage(m, atoi(id), msg, strlen(msg) + 1) == 0)
|
||||
wprintw(self->window, "Failed to send message.\n");
|
||||
else
|
||||
wprintw(self->window, "Message successfully sent.\n");
|
||||
}
|
||||
|
||||
void cmd_myid(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
|
||||
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
||||
tox_getaddress(m, address);
|
||||
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
|
||||
char xx[3];
|
||||
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
|
||||
strcat(id, xx);
|
||||
}
|
||||
|
||||
wprintw(self->window, "%s\n", id);
|
||||
}
|
||||
|
||||
void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
/* check arguments */
|
||||
if (argc != 1) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *nick = argv[1];
|
||||
|
||||
if (nick == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int len = strlen(nick);
|
||||
|
||||
if (nick[0] == '\"') {
|
||||
++nick;
|
||||
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, len+1);
|
||||
|
||||
store_data(m, DATA_FILE);
|
||||
}
|
||||
|
||||
void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
uint8_t *msg = NULL;
|
||||
|
||||
if (argc == 2) {
|
||||
|
||||
msg = argv[2];
|
||||
|
||||
if (msg == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg[0] != '\"') {
|
||||
wprintw(self->window, "Messages must be enclosed in quotes.\n");
|
||||
return;
|
||||
}
|
||||
} else if (argc != 1) {
|
||||
wprintw(self->window, "Wrong number of arguments.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *status = argv[1];
|
||||
|
||||
if (status == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
TOX_USERSTATUS status_kind;
|
||||
|
||||
if (!strncmp(status, "online", strlen("online")))
|
||||
status_kind = TOX_USERSTATUS_NONE;
|
||||
|
||||
else if (!strncmp(status, "away", strlen("away")))
|
||||
status_kind = TOX_USERSTATUS_AWAY;
|
||||
|
||||
else if (!strncmp(status, "busy", strlen("busy")))
|
||||
status_kind = TOX_USERSTATUS_BUSY;
|
||||
|
||||
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 */
|
||||
uint16_t len = strlen(msg) + 1;
|
||||
tox_set_statusmessage(m, msg, len);
|
||||
prompt_update_statusmessage(self, msg, len);
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
wprintw(self->window, "Wrong number of arguments.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *msg = argv[1];
|
||||
|
||||
if (msg == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg[0] != '\"') {
|
||||
wprintw(self->window, "Messages must be enclosed in quotes.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
msg[strlen(++msg)-1] = L'\0';
|
||||
uint16_t len = strlen(msg) + 1;
|
||||
tox_set_statusmessage(m, msg, len);
|
||||
prompt_update_statusmessage(self, msg, len);
|
||||
}
|
||||
|
||||
static void execute(ToxWindow *self, Tox *m, char *u_cmd)
|
||||
{
|
||||
int newlines = 0;
|
||||
char cmd[MAX_STR_SIZE] = {'\0'};
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < strlen(prompt_buf); ++i) {
|
||||
if (u_cmd[i] == '\n')
|
||||
++newlines;
|
||||
else
|
||||
cmd[i - newlines] = u_cmd[i];
|
||||
}
|
||||
|
||||
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++;
|
||||
}
|
||||
/* skip over strings */
|
||||
else if (cmd[i] == '\"') {
|
||||
while (cmd[++i] != '\"') {
|
||||
if (cmd[i] == '\0') {
|
||||
wprintw(self->window, "Invalid command: did you forget an opening or closing \"?\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* read arguments into array */
|
||||
char **cmdargs = malloc((numargs + 1) * sizeof(char *));
|
||||
if (!cmdargs) {
|
||||
wprintw(self->window, "Invalid command: too many arguments.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int pos = 0;
|
||||
|
||||
for (i = 0; i < numargs + 1; i++) {
|
||||
cmdargs[i] = cmd + pos;
|
||||
pos += strlen(cmdargs[i]) + 1;
|
||||
/* replace empty strings with NULL for easier error checking */
|
||||
if (strlen(cmdargs[i]) == 0)
|
||||
cmdargs[i] = NULL;
|
||||
}
|
||||
|
||||
/* no input */
|
||||
if (!cmdargs[0]) {
|
||||
free(cmdargs);
|
||||
return;
|
||||
}
|
||||
|
||||
/* match input to command list */
|
||||
for (i = 0; i < NUM_COMMANDS; i++) {
|
||||
if (!strcmp(cmdargs[0], commands[i].name)) {
|
||||
(commands[i].func)(self, m, numargs, cmdargs);
|
||||
free(cmdargs);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* no match */
|
||||
free(cmdargs);
|
||||
wprintw(self->window, "Invalid command.\n");
|
||||
}
|
||||
|
||||
static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
|
||||
{
|
||||
/* Add printable characters to line */
|
||||
@ -684,7 +148,12 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
|
||||
/* RETURN key: execute command */
|
||||
else if (key == '\n') {
|
||||
wprintw(self->window, "\n");
|
||||
execute(self, m, prompt_buf);
|
||||
|
||||
if (!strncmp(prompt_buf, "/help", strlen("/help")))
|
||||
print_prompt_help(self);
|
||||
else
|
||||
execute(self->window, self, m, prompt_buf, prompt_buf_pos);
|
||||
|
||||
prompt_buf_pos = 0;
|
||||
prompt_buf[0] = 0;
|
||||
}
|
||||
@ -762,7 +231,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
|
||||
static void prompt_onInit(ToxWindow *self, Tox *m)
|
||||
{
|
||||
scrollok(self->window, 1);
|
||||
cmd_help(self, m, 0, NULL);
|
||||
print_prompt_help(self);
|
||||
wclrtoeol(self->window);
|
||||
}
|
||||
|
||||
@ -784,7 +253,7 @@ void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16
|
||||
}
|
||||
|
||||
wprintw(self->window, "\n\nWith the message: %s\n\n", data);
|
||||
wprintw(self->window, "Type \"accept %d\" to accept it.\n", n);
|
||||
wprintw(self->window, "Type \"/accept %d\" to accept it.\n", n);
|
||||
|
||||
self->blink = true;
|
||||
beep();
|
||||
@ -817,7 +286,7 @@ void prompt_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *gr
|
||||
return;
|
||||
}
|
||||
|
||||
wprintw(self->window, "Type \"join %d\" to join the chat.\n", n);
|
||||
wprintw(self->window, "Type \"/join %d\" to join the chat.\n", n);
|
||||
|
||||
self->blink = true;
|
||||
beep();
|
||||
@ -838,7 +307,12 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m)
|
||||
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
|
||||
|
||||
/* temporary until statusmessage saving works */
|
||||
uint8_t *statusmsg = "Toxing on Toxic v0.2.0";
|
||||
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
|
||||
char toxic_ver[strlen(TOXICVER)+1];
|
||||
strcpy(toxic_ver, TOXICVER);
|
||||
char *L = strchr(toxic_ver, '_');
|
||||
toxic_ver[L-toxic_ver] = '\0';
|
||||
snprintf(statusmsg, sizeof(statusmsg), "Toxing on Toxic v.%s", toxic_ver);
|
||||
m_set_statusmessage(m, statusmsg, strlen(statusmsg) + 1);
|
||||
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
||||
|
||||
@ -846,7 +320,7 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m)
|
||||
statusbar->topline = subwin(self->window, 2, x, 0, 0);
|
||||
}
|
||||
|
||||
ToxWindow new_prompt()
|
||||
ToxWindow new_prompt(void)
|
||||
{
|
||||
ToxWindow ret;
|
||||
memset(&ret, 0, sizeof(ret));
|
||||
|
@ -1,9 +1,13 @@
|
||||
/*
|
||||
* Toxic -- Tox Curses Client
|
||||
*/
|
||||
|
||||
#ifndef PROMPT_H_UZYGWFFL
|
||||
#define PROMPT_H_UZYGWFFL
|
||||
|
||||
#include "toxic_windows.h"
|
||||
|
||||
ToxWindow new_prompt();
|
||||
ToxWindow new_prompt(void);
|
||||
int add_req(uint8_t *public_key);
|
||||
unsigned char *hex_string_to_bin(char hex_string[]);
|
||||
void prompt_init_statusbar(ToxWindow *self, Tox *m);
|
||||
|
Loading…
Reference in New Issue
Block a user