1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 01:53:02 +01:00

big refactor for command functionality

This commit is contained in:
Jfreegman 2013-11-09 21:43:56 -05:00
parent 1b041cb089
commit 549fc5ecb6
11 changed files with 732 additions and 180 deletions

View File

@ -11,13 +11,28 @@
#include <time.h> #include <time.h>
#include "toxic_windows.h" #include "toxic_windows.h"
#include "friendlist.h" #include "execute.h"
#include "commands.h"
#include "misc_tools.h" #include "misc_tools.h"
extern char *DATA_FILE; extern char *DATA_FILE;
extern int store_data(Tox *m, char *path); extern int store_data(Tox *m, char *path);
/* One group chat request slot for each friend; slot is
overwritten on subsequent requests by the same friend. */
extern uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
/* Adds group chat invite to pending group chat requests.
Returns friend number on success, -1 if f_num is out of range. */
static int add_group_request(uint8_t *group_pub_key, int f_num)
{
if (f_num >= 0 && f_num < MAX_FRIENDS_NUM) {
memcpy(pending_grp_requests[f_num], group_pub_key, TOX_CLIENT_ID_SIZE);
return f_num;
}
return -1;
}
static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len)
{ {
if (self->num != num) if (self->num != num)
@ -206,129 +221,36 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u
fclose(file_to_save); fclose(file_to_save);
} }
static void chat_groupinvite(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *line) static void chat_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key)
{ {
int groupnum = atoi(line); if (friendnumber < 0)
return;
if (groupnum == 0 && strcmp(line, "0")) { /* atoi returns 0 value on invalid input */ ChatContext *ctx = (ChatContext *) self->chatwin;
wprintw(ctx->history, "Invalid syntax.\n"); uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'};
if (tox_getname(m, friendnumber, name) == -1)
return;
wprintw(ctx->history, "Group chat invite from %s.\n", name);
int ngc = get_num_groupchats();
if (ngc < 0 || ngc > MAX_GROUPCHAT_NUM) {
wprintw(ctx->history, "Maximum number of group chats has been reached. Discarding invite.\n");
return; return;
} }
if (tox_invite_friend(m, self->num, groupnum) == -1) { int n = add_group_request(group_pub_key, friendnumber);
wprintw(ctx->history, "Failed to invite friend.\n");
if (n == -1) {
wprintw(ctx->history, "Something bad happened.\n");
return; return;
} }
wprintw(ctx->history, "Invited friend to group chat %d.\n", groupnum); wprintw(ctx->history, "Type \"/join %d\" to join the chat.\n", n);
} self->blink = true;
beep();
static void chat_sendfile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *path)
{
if (num_file_senders >= MAX_FILES) {
wprintw(ctx->history,"Please wait for some of your outgoing file transfers to complete.\n");
return;
}
int path_len = strlen(path);
if (path_len > MAX_STR_SIZE) {
wprintw(ctx->history, "File path exceeds character limit.\n");
return;
}
FILE *file_to_send = fopen(path, "r");
if (file_to_send == NULL) {
wprintw(ctx->history, "File '%s' not found.\n", path);
return;
}
fseek(file_to_send, 0, SEEK_END);
uint64_t filesize = ftell(file_to_send);
fseek(file_to_send, 0, SEEK_SET);
int friendnum = self->num;
int filenum = tox_new_filesender(m, friendnum, filesize, path, path_len + 1);
if (filenum == -1) {
wprintw(ctx->history, "Error sending file.\n");
return;
}
int i;
for (i = 0; i < MAX_FILES; ++i) {
if (!file_senders[i].active) {
memcpy(file_senders[i].pathname, path, path_len + 1);
file_senders[i].active = true;
file_senders[i].chatwin = ctx->history;
file_senders[i].file = file_to_send;
file_senders[i].filenum = (uint8_t) filenum;
file_senders[i].friendnum = friendnum;
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
tox_filedata_size(m, friendnum), file_to_send);
wprintw(ctx->history, "Sending file: '%s'\n", path);
if (i == num_file_senders)
++num_file_senders;
return;
}
}
}
static void chat_savefile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *num)
{
uint8_t filenum = atoi(num);
if ((filenum == 0 && strcmp(num, "0")) || filenum >= MAX_FILES) {
wprintw(ctx->history, "No pending file transfers with that number.\n");
return;
}
int friendnum = self->num;
if (!friends[friendnum].file_receiver.pending[filenum]) {
wprintw(ctx->history, "No pending file transfers with that number.\n");
return;
}
uint8_t *filename = friends[friendnum].file_receiver.filenames[filenum];
if (tox_file_sendcontrol(m, friendnum, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0))
wprintw(ctx->history, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename);
else
wprintw(ctx->history, "File transfer failed.\n");
friends[friendnum].file_receiver.pending[filenum] = false;
}
static void print_chat_help(ChatContext *ctx)
{
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(ctx->history, "Chat commands:\n");
wattroff(ctx->history, A_BOLD);
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 <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, " /sendfile <filepath> : Send a file\n");
wprintw(ctx->history, " /savefile <n> : Receive a file\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 * Argument messages must be enclosed in quotation marks.\n");
wattroff(ctx->history, A_BOLD);
wattroff(ctx->history, COLOR_PAIR(CYAN));
} }
static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *action) { static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *action) {
@ -401,16 +323,8 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
disable_chatwin(f_num); disable_chatwin(f_num);
} else if (!strncmp(line, "/me ", strlen("/me "))) } else if (!strncmp(line, "/me ", strlen("/me ")))
send_action(self, ctx, m, line + strlen("/me ")); send_action(self, ctx, m, line + strlen("/me "));
else if (!strncmp(line, "/help", strlen("/help")))
print_chat_help(ctx);
else if (!strncmp(line, "/invite", strlen("/invite")))
chat_groupinvite(self, ctx, m, line + strlen("/invite "));
else if(!strncmp(line, "/sendfile ", strlen("/sendfile ")))
chat_sendfile(self, ctx, m, line + strlen("/sendfile "));
else if(!strncmp(line, "/savefile ", strlen("/savefile ")))
chat_savefile(self, ctx, m, line + strlen("/savefile "));
else else
execute(ctx->history, self->prompt, m, line); execute(ctx->history, self->prompt, m, line, CHAT_COMMAND_MODE);
} else { } else {
/* make sure the string has at least non-space character */ /* make sure the string has at least non-space character */
if (!string_is_empty(line)) { if (!string_is_empty(line)) {
@ -549,7 +463,7 @@ static void chat_onInit(ToxWindow *self, Tox *m)
scrollok(ctx->history, 1); scrollok(ctx->history, 1);
ctx->linewin = subwin(self->window, 0, x, y-4, 0); ctx->linewin = subwin(self->window, 0, x, y-4, 0);
wprintw(ctx->history, "\n\n"); wprintw(ctx->history, "\n\n");
print_chat_help(ctx); execute(ctx->history, self->prompt, m, "/help", CHAT_COMMAND_MODE);
wmove(self->window, y - CURS_Y_OFFSET, 0); wmove(self->window, y - CURS_Y_OFFSET, 0);
} }
@ -563,6 +477,7 @@ ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum)
ret.onInit = &chat_onInit; ret.onInit = &chat_onInit;
ret.onMessage = &chat_onMessage; ret.onMessage = &chat_onMessage;
ret.onConnectionChange = &chat_onConnectionChange; ret.onConnectionChange = &chat_onConnectionChange;
ret.onGroupInvite = &chat_onGroupInvite;
ret.onNickChange = &chat_onNickChange; ret.onNickChange = &chat_onNickChange;
ret.onStatusChange = &chat_onStatusChange; ret.onStatusChange = &chat_onStatusChange;
ret.onStatusMessageChange = &chat_onStatusMessageChange; ret.onStatusMessageChange = &chat_onStatusMessageChange;

199
src/chat_commands.c Normal file
View File

@ -0,0 +1,199 @@
/*
* Toxic -- Tox Curses Client
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "toxic_windows.h"
#include "misc_tools.h"
#include "chat_commands.h"
extern uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
void cmd_help(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
wattron(window, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(window, "Chat commands:\n");
wattroff(window, A_BOLD);
wprintw(window, " /status <type> <message> : Set your status with optional note\n");
wprintw(window, " /note <message> : Set a personal note\n");
wprintw(window, " /nick <nickname> : Set your nickname\n");
wprintw(window, " /invite <n> : Invite friend to a groupchat\n");
wprintw(window, " /me <action> : Do an action\n");
wprintw(window, " /myid : Print your ID\n");
wprintw(window, " /clear : Clear the screen\n");
wprintw(window, " /close : Close the current chat window\n");
wprintw(window, " /sendfile <filepath> : Send a file\n");
wprintw(window, " /savefile <n> : Receive a file\n");
wprintw(window, " /quit or /exit : Exit Toxic\n");
wprintw(window, " /help : Print this message again\n");
wattron(window, A_BOLD);
wprintw(window, "\n * Argument messages must be enclosed in quotation marks.\n");
wattroff(window, A_BOLD);
wattroff(window, COLOR_PAIR(CYAN));
}
void cmd_groupinvite(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc < 1) {
wprintw(window, "Invalid syntax.\n");
return;
}
int groupnum = atoi(argv[1]);
if (groupnum == 0 && strcmp(argv[1], "0")) { /* atoi returns 0 value on invalid input */
wprintw(window, "Invalid syntax.\n");
return;
}
if (tox_invite_friend(m, chatwin->num, groupnum) == -1) {
wprintw(window, "Failed to invite friend.\n");
return;
}
wprintw(window, "Invited friend to group chat %d.\n", groupnum);
}
void cmd_join_group(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc != 1) {
wprintw(window, "Invalid syntax.\n");
return;
}
int num = atoi(argv[1]);
if ((num == 0 && strcmp(argv[1], "0")) || num >= MAX_FRIENDS_NUM) {
wprintw(window, "No pending group chat invite with that number.\n");
return;
}
uint8_t *groupkey = pending_grp_requests[num];
if (!strlen(groupkey)) {
wprintw(window, "No pending group chat invite with that number.\n");
return;
}
int groupnum = tox_join_groupchat(m, num, groupkey);
if (groupnum == -1) {
wprintw(window, "Group chat instance 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_savefile(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc != 1) {
wprintw(window, "Invalid syntax.\n");
return;
}
uint8_t filenum = atoi(argv[1]);
if ((filenum == 0 && strcmp(argv[1], "0")) || filenum >= MAX_FILES) {
wprintw(window, "No pending file transfers with that number.\n");
return;
}
int friendnum = chatwin->num;
if (!friends[friendnum].file_receiver.pending[filenum]) {
wprintw(window, "No pending file transfers with that number.\n");
return;
}
uint8_t *filename = friends[friendnum].file_receiver.filenames[filenum];
if (tox_file_sendcontrol(m, friendnum, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0))
wprintw(window, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename);
else
wprintw(window, "File transfer failed.\n");
friends[friendnum].file_receiver.pending[filenum] = false;
}
void cmd_sendfile(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (num_file_senders >= MAX_FILES) {
wprintw(window,"Please wait for some of your outgoing file transfers to complete.\n");
return;
}
if (argc < 1) {
wprintw(window, "Invalid syntax.\n");
return;
}
uint8_t *path = argv[1];
if (path[0] != '\"') {
wprintw(window, "File path must be enclosed in quotes.\n");
return;
}
path[strlen(++path)-1] = L'\0';
int path_len = strlen(path);
if (path_len > MAX_STR_SIZE) {
wprintw(window, "File path exceeds character limit.\n");
return;
}
FILE *file_to_send = fopen(path, "r");
if (file_to_send == NULL) {
wprintw(window, "File '%s' not found.\n", path);
return;
}
fseek(file_to_send, 0, SEEK_END);
uint64_t filesize = ftell(file_to_send);
fseek(file_to_send, 0, SEEK_SET);
int friendnum = chatwin->num;
int filenum = tox_new_filesender(m, friendnum, filesize, path, path_len + 1);
if (filenum == -1) {
wprintw(window, "Error sending file.\n");
return;
}
int i;
for (i = 0; i < MAX_FILES; ++i) {
if (!file_senders[i].active) {
memcpy(file_senders[i].pathname, path, path_len + 1);
file_senders[i].active = true;
file_senders[i].chatwin = window;
file_senders[i].file = file_to_send;
file_senders[i].filenum = (uint8_t) filenum;
file_senders[i].friendnum = friendnum;
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
tox_filedata_size(m, friendnum), file_to_send);
wprintw(window, "Sending file: '%s'\n", path);
if (i == num_file_senders)
++num_file_senders;
return;
}
}
}

23
src/chat_commands.h Normal file
View File

@ -0,0 +1,23 @@
/*
* Toxic -- Tox Curses Client
*/
/* commands */
void cmd_help(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_groupinvite(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_join_group(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_savefile(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_sendfile(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
#define CHAT_NUM_COMMANDS 5
static struct {
char *name;
void (*func)(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
} chat_commands[] = {
{ "/help", cmd_help },
{ "/invite", cmd_groupinvite },
{ "/join", cmd_join_group },
{ "/savefile", cmd_savefile },
{ "/sendfile", cmd_sendfile },
};

82
src/execute.c Normal file
View File

@ -0,0 +1,82 @@
/*
* Toxic -- Tox Curses Client
*/
#include <stdlib.h>
#include <string.h>
#include "toxic_windows.h"
#include "global_commands.h"
#include "chat_commands.h"
#include "execute.h"
static int parse_command(WINDOW *window, char *cmd, char (*args)[MAX_STR_SIZE])
{
int num_args = 0;
bool cmd_end = false; // flags when we get to the end of cmd
char *end; // points to the end of the current arg
/* Put arguments into args array (characters wrapped in double quotes count as one arg) */
while (!cmd_end && num_args < MAX_NUM_ARGS) {
if (*cmd == '\"') {
end = strchr(cmd+1, '\"');
if (end++ == NULL) { /* Increment past the end quote */
wprintw(window, "Invalid argument. Did you forget a closing \"?\n");
return;
}
cmd_end = *end == '\0';
} else {
end = strchr(cmd, ' ');
cmd_end = end == NULL;
}
if (!cmd_end)
*end++ = '\0'; /* mark end of current argument */
/* Copy from start of current arg to where we just inserted the null byte */
strcpy(args[num_args++], cmd);
cmd = end;
}
return num_args;
}
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *cmd, int mode)
{
if (string_is_empty(cmd))
return;
char args[MAX_NUM_ARGS][MAX_STR_SIZE] = {0};
int num_args = parse_command(window, cmd, args);
/* Attempt to match input to command functions. If non-global command mode is specified,
try the specified mode's commands first, then upon failure try global commands.
TODO: Generalize command matching loop in a separate function */
int i;
switch(mode) {
case CHAT_COMMAND_MODE:
for (i = 0; i < CHAT_NUM_COMMANDS; ++i) {
if (strcmp(args[0], chat_commands[i].name) == 0) {
(chat_commands[i].func)(window, prompt, m, num_args-1, args);
return;
}
}
break;
case GROUPCHAT_COMMAND_MODE:
break;
}
for (i = 0; i < GLOBAL_NUM_COMMANDS; ++i) {
if (strcmp(args[0], global_commands[i].name) == 0) {
(global_commands[i].func)(window, prompt, m, num_args-1, args);
return;
}
}
wprintw(window, "Invalid command.\n");
}

10
src/execute.h Normal file
View File

@ -0,0 +1,10 @@
/*
* Toxic -- Tox Curses Client
*/
#define MAX_NUM_ARGS 4 /* Includes command */
#define GLOBAL_COMMAND_MODE 0
#define CHAT_COMMAND_MODE 1
#define GROUPCHAT_COMMAND_MODE 2
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *cmd, int mode);

View File

@ -143,6 +143,15 @@ static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8
friends[num].chatwin = add_window(m, new_chat(m, prompt, friends[num].num)); friends[num].chatwin = add_window(m, new_chat(m, prompt, friends[num].num));
} }
static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int num, uint8_t *group_pub_key)
{
if (num < 0 || num >= max_friends_index)
return;
if (friends[num].chatwin == -1)
friends[num].chatwin = add_window(m, new_chat(m, prompt, friends[num].num));
}
static void select_friend(Tox *m, wint_t key) static void select_friend(Tox *m, wint_t key)
{ {
if (num_friends < 1) if (num_friends < 1)
@ -265,7 +274,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
} else { } else {
wprintw(self->window, "[O]%s\n", friends[f].name); wprintw(self->window, "[O]%s\n", friends[f].name);
} }
} }
} }
wrefresh(self->window); wrefresh(self->window);
@ -296,6 +305,7 @@ ToxWindow new_friendlist(void)
ret.onStatusChange = &friendlist_onStatusChange; ret.onStatusChange = &friendlist_onStatusChange;
ret.onStatusMessageChange = &friendlist_onStatusMessageChange; ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
ret.onFileSendRequest = &friendlist_onFileSendRequest; ret.onFileSendRequest = &friendlist_onFileSendRequest;
ret.onGroupInvite = &friendlist_onGroupInvite;
strcpy(ret.name, "friends"); strcpy(ret.name, "friends");
return ret; return ret;

322
src/global_commands.c Normal file
View File

@ -0,0 +1,322 @@
/*
* Toxic -- Tox Curses Client
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "toxic_windows.h"
#include "misc_tools.h"
#include "global_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;
/* command functions */
void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
/* check arguments */
if (argc != 1) {
wprintw(window, "Invalid syntax.\n");
return;
}
int num = atoi(argv[1]);
if ((num == 0 && strcmp(argv[1], "0"))|| num >= MAX_FRIENDS_NUM) {
wprintw(window, "No pending friend request with that number.\n");
return;
}
if (!strlen(pending_frnd_requests[num])) {
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);
}
memset(&pending_frnd_requests[num], 0, TOX_CLIENT_ID_SIZE);
int i;
for (i = num_frnd_requests; i > 0; --i) {
if (!strlen(pending_frnd_requests[i-1]))
break;
}
num_frnd_requests = i;
}
void cmd_add(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc < 1) {
wprintw(window, "Invalid syntax.\n");
return;
}
char *id = argv[1];
uint8_t *msg;
if (argc > 1) {
msg = argv[2];
if (msg[0] != '\"') {
wprintw(window, "Message 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 has already been 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 request sent.\n");
on_friendadded(m, num);
break;
}
}
void cmd_clear(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
wclear(window);
wprintw(window, "\n\n");
}
void cmd_connect(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
/* 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 (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)[MAX_STR_SIZE])
{
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 instance 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;
}
wprintw(window, "Group chat created as %d.\n", groupnum);
}
void cmd_myid(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
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)[MAX_STR_SIZE])
{
/* check arguments */
if (argc != 1) {
wprintw(window, "Invalid syntax.\n");
return;
}
uint8_t *nick = argv[1];
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)[MAX_STR_SIZE])
{
if (argc < 1) {
wprintw(window, "Wrong number of arguments.\n");
return;
}
uint8_t *msg = argv[1];
if (msg[0] != '\"') {
wprintw(window, "Note 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)[MAX_STR_SIZE])
{
exit_toxic(m);
}
void cmd_status(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
uint8_t *msg = NULL;
if (argc >= 2) {
msg = argv[2];
if (msg[0] != '\"') {
wprintw(window, "Note must be enclosed in quotes.\n");
return;
}
} else if (argc != 1) {
wprintw(window, "Wrong number of arguments.\n");
return;
}
char *status = argv[1];
int len = strlen(status);
char l_status[len+1];
int i;
for (i = 0; i <= len; ++i)
l_status[i] = tolower(status[i]);
TOX_USERSTATUS status_kind;
if (!strcmp(l_status, "online"))
status_kind = TOX_USERSTATUS_NONE;
else if (!strcmp(l_status, "away"))
status_kind = TOX_USERSTATUS_AWAY;
else if (!strcmp(l_status, "busy"))
status_kind = TOX_USERSTATUS_BUSY;
else {
wprintw(window, "Invalid status. Valid statuses are: online, busy and away.\n");
return;
}
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);
}
}

35
src/global_commands.h Normal file
View File

@ -0,0 +1,35 @@
/*
* Toxic -- Tox Curses Client
*/
/* commands */
void cmd_accept(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_add(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_clear(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_connect(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_groupchat(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_myid(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_nick(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_note(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_quit(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_status(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
#define GLOBAL_NUM_COMMANDS 12
static struct {
char *name;
void (*func)(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
} global_commands[] = {
{ "/accept", cmd_accept },
{ "/add", cmd_add },
{ "/clear", cmd_clear },
{ "/connect", cmd_connect },
{ "/exit", cmd_quit },
{ "/groupchat", cmd_groupchat },
{ "/myid", cmd_myid },
{ "/nick", cmd_nick },
{ "/note", cmd_note },
{ "/q", cmd_quit },
{ "/quit", cmd_quit },
{ "/status", cmd_status },
};

View File

@ -11,7 +11,7 @@
#include <time.h> #include <time.h>
#include "toxic_windows.h" #include "toxic_windows.h"
#include "commands.h" #include "execute.h"
#include "misc_tools.h" #include "misc_tools.h"
static GroupChat groupchats[MAX_GROUPCHAT_NUM]; static GroupChat groupchats[MAX_GROUPCHAT_NUM];
@ -170,7 +170,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
} else if (strncmp(line, "/help", strlen("/help")) == 0) } else if (strncmp(line, "/help", strlen("/help")) == 0)
print_groupchat_help(ctx); print_groupchat_help(ctx);
else else
execute(ctx->history, self->prompt, m, line); execute(ctx->history, self->prompt, m, line, GROUPCHAT_COMMAND_MODE);
} else { } else {
/* make sure the string has at least non-space character */ /* make sure the string has at least non-space character */
if (!string_is_empty(line)) { if (!string_is_empty(line)) {

View File

@ -11,16 +11,12 @@
#include "toxic_windows.h" #include "toxic_windows.h"
#include "prompt.h" #include "prompt.h"
#include "commands.h" #include "execute.h"
#include "misc_tools.h" #include "misc_tools.h"
uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0}; uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0};
uint8_t num_frnd_requests = 0; uint8_t num_frnd_requests = 0;
/* One group chat request slot for each friend; slot is
overwritten on subsequent requests by the same friend. */
uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0};
static char prompt_buf[MAX_STR_SIZE] = {'\0'}; static char prompt_buf[MAX_STR_SIZE] = {'\0'};
static int prompt_buf_pos = 0; static int prompt_buf_pos = 0;
@ -76,18 +72,6 @@ static int add_friend_request(uint8_t *public_key)
return -1; return -1;
} }
/* Adds group chat invite to pending group chat requests.
Returns friend number on success, -1 if f_num is out of range. */
static int add_group_request(uint8_t *group_pub_key, int f_num)
{
if (f_num >= 0 && f_num < MAX_FRIENDS_NUM) {
memcpy(pending_grp_requests[f_num], group_pub_key, TOX_CLIENT_ID_SIZE);
return f_num;
}
return -1;
}
static void print_prompt_help(ToxWindow *self) static void print_prompt_help(ToxWindow *self)
{ {
wclear(self->window); wclear(self->window);
@ -150,7 +134,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
if (!strncmp(prompt_buf, "/help", strlen("/help"))) if (!strncmp(prompt_buf, "/help", strlen("/help")))
print_prompt_help(self); print_prompt_help(self);
else else
execute(self->window, self, m, prompt_buf); execute(self->window, self, m, prompt_buf, GLOBAL_COMMAND_MODE);
prompt_buf_pos = 0; prompt_buf_pos = 0;
prompt_buf[0] = '\0'; prompt_buf[0] = '\0';
@ -286,37 +270,6 @@ static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data,
beep(); beep();
} }
static void prompt_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key)
{
if (friendnumber < 0)
return;
uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'};
if (tox_getname(m, friendnumber, name) == -1)
return;
wprintw(self->window, "\nGroup chat invite from %s.\n", name);
int ngc = get_num_groupchats();
if (ngc < 0 || ngc > MAX_GROUPCHAT_NUM) {
wprintw(self->window, "\nMaximum number of group chats has been reached. Discarding invite.\n");
return;
}
int n = add_group_request(group_pub_key, friendnumber);
if (n == -1) {
wprintw(self->window, "\nSomething bad happened.\n");
return;
}
wprintw(self->window, "Type \"/join %d\" to join the chat.\n", n);
self->blink = true;
beep();
}
void prompt_init_statusbar(ToxWindow *self, Tox *m) void prompt_init_statusbar(ToxWindow *self, Tox *m)
{ {
int x, y; int x, y;
@ -350,7 +303,6 @@ ToxWindow new_prompt(void)
ret.onInit = &prompt_onInit; ret.onInit = &prompt_onInit;
ret.onConnectionChange = &prompt_onConnectionChange; ret.onConnectionChange = &prompt_onConnectionChange;
ret.onFriendRequest = &prompt_onFriendRequest; ret.onFriendRequest = &prompt_onFriendRequest;
ret.onGroupInvite = &prompt_onGroupInvite;
strcpy(ret.name, "prompt"); strcpy(ret.name, "prompt");

View File

@ -16,6 +16,10 @@ static ToxWindow *active_window;
static ToxWindow *prompt; static ToxWindow *prompt;
static Tox *m; static Tox *m;
/* One group chat request slot for each friend; slot is
overwritten on subsequent requests by the same friend. */
uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0};
/* CALLBACKS START */ /* CALLBACKS START */
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
{ {