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

484 lines
14 KiB
C
Raw Normal View History

2014-02-25 08:28:24 +01:00
/* global_commands.c
*
*
* Copyright (C) 2014 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
* Toxic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toxic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
2013-11-10 03:43:56 +01:00
*/
#include <stdlib.h>
#include <string.h>
2014-06-22 03:41:38 +02:00
#include <arpa/inet.h>
2013-11-10 03:43:56 +01:00
#include "toxic.h"
#include "windows.h"
2013-11-10 03:43:56 +01:00
#include "misc_tools.h"
2013-11-30 00:52:21 +01:00
#include "friendlist.h"
2014-03-25 13:25:10 +01:00
#include "log.h"
2014-03-25 08:17:22 +01:00
#include "line_info.h"
2014-06-17 03:22:26 +02:00
#include "dns.h"
2014-06-22 03:41:38 +02:00
#include "groupchat.h"
#include "prompt.h"
2013-11-10 03:43:56 +01:00
extern char *DATA_FILE;
extern ToxWindow *prompt;
2013-11-10 03:43:56 +01:00
2013-11-30 00:52:21 +01:00
extern ToxicFriend friends[MAX_FRIENDS_NUM];
2013-11-10 03:43:56 +01:00
extern uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
extern uint8_t num_frnd_requests;
2013-11-30 00:52:21 +01:00
2013-11-10 03:43:56 +01:00
/* command functions */
void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
2014-03-25 08:17:22 +01:00
uint8_t *msg;
2013-11-10 03:43:56 +01:00
if (argc != 1) {
2014-03-25 08:17:22 +01:00
msg = "Invalid syntax.";
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
return;
2013-11-10 03:43:56 +01:00
}
2013-11-12 08:41:55 +01:00
int req = atoi(argv[1]);
2013-11-10 03:43:56 +01:00
if ((req == 0 && strcmp(argv[1], "0")) || req >= MAX_FRIENDS_NUM) {
2014-03-25 08:17:22 +01:00
msg = "No pending friend request with that number.";
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
return;
}
2013-11-12 08:41:55 +01:00
if (!strlen(pending_frnd_requests[req])) {
2014-03-25 08:17:22 +01:00
msg = "No pending friend request with that number.";
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
return;
}
2014-03-19 08:14:08 +01:00
int32_t friendnum = tox_add_friend_norequest(m, pending_frnd_requests[req]);
2013-11-10 03:43:56 +01:00
if (friendnum == -1)
2014-03-25 08:17:22 +01:00
msg = "Failed to add friend.";
2013-11-10 03:43:56 +01:00
else {
2014-03-25 08:17:22 +01:00
msg = "Friend request accepted.";
on_friendadded(m, friendnum, true);
2013-11-10 03:43:56 +01:00
}
2013-11-12 08:41:55 +01:00
memset(&pending_frnd_requests[req], 0, TOX_CLIENT_ID_SIZE);
2013-11-10 03:43:56 +01:00
int i;
for (i = num_frnd_requests; i > 0; --i) {
if (!strlen(pending_frnd_requests[i - 1]))
2013-11-10 03:43:56 +01:00
break;
}
num_frnd_requests = i;
2014-03-25 08:17:22 +01:00
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
}
2014-06-18 01:10:25 +02:00
void cmd_add_helper(ToxWindow *self, Tox *m, uint8_t *id_bin, uint8_t *msg)
{
uint8_t *errmsg;
int32_t f_num = tox_add_friend(m, id_bin, msg, strlen(msg));
switch (f_num) {
case TOX_FAERR_TOOLONG:
errmsg = "Message is too long.";
break;
case TOX_FAERR_NOMESSAGE:
errmsg = "Please add a message to your request.";
break;
case TOX_FAERR_OWNKEY:
errmsg = "That appears to be your own ID.";
break;
case TOX_FAERR_ALREADYSENT:
errmsg = "Friend request has already been sent.";
break;
case TOX_FAERR_UNKNOWN:
errmsg = "Undefined error when adding friend.";
break;
case TOX_FAERR_BADCHECKSUM:
errmsg = "Bad checksum in address.";
break;
case TOX_FAERR_SETNEWNOSPAM:
errmsg = "Nospam was different.";
break;
default:
errmsg = "Friend request sent.";
on_friendadded(m, f_num, true);
break;
}
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
}
void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
2014-03-25 08:17:22 +01:00
uint8_t *errmsg;
2013-11-10 03:43:56 +01:00
if (argc < 1) {
2014-03-25 08:17:22 +01:00
errmsg = "Invalid syntax.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
return;
}
char *id = argv[1];
uint8_t msg[MAX_STR_SIZE];
2013-11-10 03:43:56 +01:00
if (argc > 1) {
uint8_t *temp = argv[2];
2013-11-10 03:43:56 +01:00
if (temp[0] != '\"') {
2014-03-25 08:17:22 +01:00
errmsg = "Message must be enclosed in quotes.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
return;
}
temp[strlen(++temp) - 1] = L'\0';
snprintf(msg, sizeof(msg), "%s", temp);
} else {
uint8_t selfname[TOX_MAX_NAME_LENGTH];
uint16_t n_len = tox_get_self_name(m, selfname);
selfname[n_len] = '\0';
snprintf(msg, sizeof(msg), "Hello, my name is %s. Care to Tox?", selfname);
}
2013-11-10 03:43:56 +01:00
2014-06-17 03:22:26 +02:00
uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE] = {0};
uint16_t id_len = strlen(id);
2013-11-10 03:43:56 +01:00
2014-06-17 03:22:26 +02:00
/* try to add tox ID */
if (id_len == 2 * TOX_FRIEND_ADDRESS_SIZE) {
size_t i;
char xx[3];
uint32_t x;
2013-11-10 03:43:56 +01:00
2014-06-17 03:22:26 +02:00
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
xx[0] = id[2 * i];
xx[1] = id[2 * i + 1];
xx[2] = '\0';
2013-11-10 03:43:56 +01:00
2014-06-17 03:22:26 +02:00
if (sscanf(xx, "%02x", &x) != 1) {
errmsg = "Invalid ID.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
return;
}
2013-11-10 03:43:56 +01:00
2014-06-17 03:22:26 +02:00
id_bin[i] = x;
}
2013-11-10 03:43:56 +01:00
2014-06-18 01:10:25 +02:00
cmd_add_helper(self, m, id_bin, msg);
} else { /* assume id is a username@domain address and do DNS lookup */
dns3_lookup(self, m, id_bin, id, msg);
2013-11-10 03:43:56 +01:00
}
}
void cmd_clear(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
2014-03-26 02:43:49 +01:00
line_info_clear(self->chatwin->hst);
2014-04-23 03:16:35 +02:00
wclear(window);
2013-11-10 03:43:56 +01:00
}
void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
2014-03-25 08:17:22 +01:00
uint8_t *errmsg;
2013-11-10 03:43:56 +01:00
/* check arguments */
if (argc != 3) {
errmsg = "Invalid syntax.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
return;
2013-11-10 03:43:56 +01:00
}
char *ip = argv[1];
char *port = argv[2];
char *key = argv[3];
if (atoi(port) == 0) {
2014-03-25 08:17:22 +01:00
errmsg = "Invalid syntax.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
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 *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
2014-03-25 08:17:22 +01:00
uint8_t *errmsg;
2014-03-07 01:39:57 +01:00
if (get_num_active_windows() >= MAX_WINDOWS_NUM) {
2014-03-25 08:17:22 +01:00
errmsg = " * Warning: Too many windows are open.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED);
2013-11-10 03:43:56 +01:00
return;
}
int groupnum = tox_add_groupchat(m);
if (groupnum == -1) {
2014-03-25 08:17:22 +01:00
errmsg = "Group chat instance failed to initialize.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
return;
}
2014-02-27 01:45:11 +01:00
if (init_groupchat_win(prompt, m, groupnum) == -1) {
2014-03-25 08:17:22 +01:00
errmsg = "Group chat window failed to initialize.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
tox_del_groupchat(m, groupnum);
return;
}
2014-03-25 08:17:22 +01:00
uint8_t msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "Group chat created as %d.", groupnum);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
}
2014-02-27 01:00:13 +01:00
void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
2014-03-25 08:17:22 +01:00
uint8_t *msg;
struct chatlog *log = self->chatwin->log;
2014-02-27 01:00:13 +01:00
if (argc == 0) {
2014-03-25 08:17:22 +01:00
if (log->log_on)
msg = "Logging for this window is ON. Type \"/log off\" to disable.";
else
msg = "Logging for this window is OFF. Type \"/log on\" to enable.";
2014-02-27 01:45:11 +01:00
2014-03-25 08:17:22 +01:00
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2014-02-27 01:00:13 +01:00
return;
}
uint8_t *swch = argv[1];
if (!strcmp(swch, "1") || !strcmp(swch, "on")) {
2014-03-02 00:06:35 +01:00
if (self->is_chat) {
friends[self->num].logging_on = true;
log_enable(self->name, friends[self->num].pub_key, log);
2014-03-02 00:06:35 +01:00
} else if (self->is_prompt) {
uint8_t myid[TOX_FRIEND_ADDRESS_SIZE];
tox_get_address(m, myid);
2014-03-26 02:43:49 +01:00
log_enable(self->name, myid, log);
2014-03-02 00:06:35 +01:00
} else if (self->is_groupchat) {
log_enable(self->name, NULL, log);
}
2014-02-27 01:00:13 +01:00
2014-03-25 08:17:22 +01:00
msg = "Logging enabled";
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2014-02-27 01:00:13 +01:00
return;
} else if (!strcmp(swch, "0") || !strcmp(swch, "off")) {
if (self->is_chat)
friends[self->num].logging_on = false;
log_disable(log);
2014-02-27 01:00:13 +01:00
2014-03-25 08:17:22 +01:00
msg = "Logging disabled";
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2014-02-27 01:00:13 +01:00
return;
}
2014-03-25 08:17:22 +01:00
msg = "Invalid option. Use \"/log on\" and \"/log off\" to toggle logging.";
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2014-02-27 01:00:13 +01:00
}
void cmd_myid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
2013-11-29 16:38:43 +01:00
tox_get_address(m, address);
2013-11-10 03:43:56 +01:00
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);
}
2014-03-25 08:17:22 +01:00
line_info_add(self, NULL, NULL, NULL, id, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
}
void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
2014-03-25 08:17:22 +01:00
uint8_t *errmsg;
2013-11-10 03:43:56 +01:00
/* check arguments */
2013-11-26 23:39:11 +01:00
if (argc < 1) {
errmsg = "Invalid name.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
return;
2013-11-10 03:43:56 +01:00
}
uint8_t *nick = argv[1];
int len = strlen(nick);
if (nick[0] == '\"') {
++nick;
len -= 2;
nick[len] = L'\0';
}
2013-11-28 08:53:43 +01:00
if (!valid_nick(nick)) {
2014-03-25 08:17:22 +01:00
errmsg = "Invalid name.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-28 08:53:43 +01:00
return;
2013-11-28 01:34:15 +01:00
}
len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1);
2013-11-10 03:43:56 +01:00
nick[len] = L'\0';
tox_set_name(m, nick, len);
prompt_update_nick(prompt, nick, len);
2013-11-10 03:43:56 +01:00
store_data(m, DATA_FILE);
}
void cmd_note(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
2014-03-25 08:17:22 +01:00
uint8_t *errmsg;
2013-11-10 03:43:56 +01:00
if (argc < 1) {
2014-03-25 08:17:22 +01:00
errmsg = "Wrong number of arguments.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
return;
}
uint8_t *msg = argv[1];
if (msg[0] != '\"') {
2014-03-25 08:17:22 +01:00
errmsg = "Note must be enclosed in quotes.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
return;
}
msg[strlen(++msg) - 1] = L'\0';
uint16_t len = strlen(msg);
tox_set_status_message(m, msg, len);
2013-11-10 03:43:56 +01:00
prompt_update_statusmessage(prompt, msg, len);
}
void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
2014-03-27 00:14:28 +01:00
struct history *hst = self->chatwin->hst;
line_info_clear(hst);
struct line_info *start = hst->line_start;
2014-03-25 08:17:22 +01:00
uint8_t *msg = "Global commands:";
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN);
#ifdef _SUPPORT_AUDIO
#define NUMLINES 14
2014-03-25 08:17:22 +01:00
#else
#define NUMLINES 12
2014-03-25 08:17:22 +01:00
#endif
uint8_t lines[NUMLINES][MAX_STR_SIZE] = {
{ " /add <id> <msg> : Add friend with optional message" },
{ " /accept <n> : Accept friend request" },
{ " /connect <ip> <port> <key> : Manually connect to a DHT node" },
{ " /status <type> <msg> : Set status with optional note" },
{ " /note <msg> : Set a personal note" },
{ " /nick <nick> : Set your nickname" },
{ " /log <on> or <off> : Enable/disable logging" },
{ " /groupchat : Create a group chat" },
{ " /myid : Print your ID" },
{ " /help : Print this message again" },
{ " /clear : Clear window history" },
{ " /quit or /exit : Exit Toxic" },
2014-03-08 16:36:42 +01:00
#ifdef _SUPPORT_AUDIO
{ " /lsdev <type> : List devices where type: in|out" },
{ " /sdev <type> <id> : Set active device" },
2014-03-08 16:36:42 +01:00
#endif /* _SUPPORT_AUDIO */
2014-03-25 08:17:22 +01:00
};
2014-03-25 08:17:22 +01:00
int i;
for (i = 0; i < NUMLINES; ++i)
line_info_add(self, NULL, NULL, NULL, lines[i], SYS_MSG, 0, 0);
2014-06-14 07:43:59 +02:00
msg = " * Argument messages must be enclosed in quotation marks.";
2014-03-25 08:17:22 +01:00
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN);
2014-06-14 07:43:59 +02:00
msg = " * Use ctrl-o and ctrl-p to navigate through the tabs.";
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN);
line_info_add(self, NULL, NULL, NULL, "", SYS_MSG, 0, 0);
2014-03-27 00:14:28 +01:00
hst->line_start = start;
}
void cmd_quit(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
2014-06-18 21:54:05 +02:00
exit_toxic_success(m);
2013-11-10 03:43:56 +01:00
}
void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
uint8_t *msg = NULL;
2014-03-25 08:17:22 +01:00
uint8_t *errmsg;
2013-11-10 03:43:56 +01:00
if (argc >= 2) {
msg = argv[2];
if (msg[0] != '\"') {
2014-03-25 08:17:22 +01:00
errmsg = "Note must be enclosed in quotes.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
return;
}
} else if (argc != 1) {
2014-03-25 08:17:22 +01:00
errmsg = "Wrong number of arguments.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
return;
}
char *status = argv[1];
str_to_lower(status);
2013-11-10 03:43:56 +01:00
int len = strlen(status);
TOX_USERSTATUS status_kind;
if (!strcmp(status, "online"))
2013-11-10 03:43:56 +01:00
status_kind = TOX_USERSTATUS_NONE;
else if (!strcmp(status, "away"))
2013-11-10 03:43:56 +01:00
status_kind = TOX_USERSTATUS_AWAY;
else if (!strcmp(status, "busy"))
2013-11-10 03:43:56 +01:00
status_kind = TOX_USERSTATUS_BUSY;
else {
2014-03-25 08:17:22 +01:00
errmsg = "Invalid status. Valid statuses are: online, busy and away.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
2013-11-10 03:43:56 +01:00
return;
}
2013-12-04 00:06:28 +01:00
tox_set_user_status(m, status_kind);
2013-11-10 03:43:56 +01:00
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);
tox_set_status_message(m, msg, len);
2013-11-10 03:43:56 +01:00
prompt_update_statusmessage(prompt, msg, len);
}
}