mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 01:53:02 +01:00
code cleanup/bug fixes
This commit is contained in:
parent
5b2e7bb7d2
commit
9798dd6b95
27
src/chat.c
27
src/chat.c
@ -121,9 +121,7 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
StatusBar *statusbar = (StatusBar *) self->s;
|
StatusBar *statusbar = (StatusBar *) self->s;
|
||||||
|
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", status);
|
||||||
if (strncmp(status, "Online", strlen(status))) /* Ignore default "Online" message */
|
|
||||||
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", status);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check that the string has one non-space character */
|
/* check that the string has one non-space character */
|
||||||
@ -298,7 +296,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
|
|||||||
wattroff(ctx->history, COLOR_PAIR(CYAN));
|
wattroff(ctx->history, COLOR_PAIR(CYAN));
|
||||||
|
|
||||||
uint8_t selfname[TOX_MAX_NAME_LENGTH];
|
uint8_t selfname[TOX_MAX_NAME_LENGTH];
|
||||||
tox_getselfname(m, selfname, sizeof(selfname));
|
tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH);
|
||||||
|
|
||||||
wattron(ctx->history, COLOR_PAIR(YELLOW));
|
wattron(ctx->history, COLOR_PAIR(YELLOW));
|
||||||
wprintw(ctx->history, "* %s %s\n", selfname, action);
|
wprintw(ctx->history, "* %s %s\n", selfname, action);
|
||||||
@ -313,7 +311,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
|
|||||||
|
|
||||||
else if (!strncmp(cmd, "/status ", strlen("/status "))) {
|
else if (!strncmp(cmd, "/status ", strlen("/status "))) {
|
||||||
char *status = strchr(cmd, ' ');
|
char *status = strchr(cmd, ' ');
|
||||||
uint8_t *msg;
|
|
||||||
|
|
||||||
if (status == NULL) {
|
if (status == NULL) {
|
||||||
wprintw(ctx->history, "Invalid syntax.\n");
|
wprintw(ctx->history, "Invalid syntax.\n");
|
||||||
@ -355,7 +352,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
|
|||||||
tox_set_userstatus(m, status_kind);
|
tox_set_userstatus(m, status_kind);
|
||||||
prompt_update_status(self->prompt, status_kind);
|
prompt_update_status(self->prompt, status_kind);
|
||||||
|
|
||||||
msg = strchr(status, ' ');
|
uint8_t *msg = strchr(status, ' ');
|
||||||
if (msg != NULL) {
|
if (msg != NULL) {
|
||||||
msg++;
|
msg++;
|
||||||
tox_set_statusmessage(m, msg, strlen(msg) + 1);
|
tox_set_statusmessage(m, msg, strlen(msg) + 1);
|
||||||
@ -373,8 +370,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
|
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
|
||||||
uint8_t *nick;
|
uint8_t *nick = strchr(cmd, ' ');
|
||||||
nick = strchr(cmd, ' ');
|
|
||||||
|
|
||||||
if (nick == NULL) {
|
if (nick == NULL) {
|
||||||
wprintw(ctx->history, "Invalid syntax.\n");
|
wprintw(ctx->history, "Invalid syntax.\n");
|
||||||
@ -454,11 +450,9 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
|
|||||||
wprintw(statusbar->topline, "[Offline]");
|
wprintw(statusbar->topline, "[Offline]");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (statusbar->statusmsg[0]) {
|
wattron(statusbar->topline, A_BOLD);
|
||||||
wattron(statusbar->topline, A_BOLD);
|
wprintw(statusbar->topline, " | %s", statusbar->statusmsg);
|
||||||
wprintw(statusbar->topline, " | %s", statusbar->statusmsg);
|
wattroff(statusbar->topline, A_BOLD);
|
||||||
wattroff(statusbar->topline, A_BOLD);
|
|
||||||
}
|
|
||||||
|
|
||||||
wprintw(statusbar->topline, "\n");
|
wprintw(statusbar->topline, "\n");
|
||||||
|
|
||||||
@ -475,13 +469,12 @@ static void chat_onInit(ToxWindow *self, Tox *m)
|
|||||||
/* Init statusbar info */
|
/* Init statusbar info */
|
||||||
StatusBar *statusbar = (StatusBar *) self->s;
|
StatusBar *statusbar = (StatusBar *) self->s;
|
||||||
statusbar->status = tox_get_userstatus(m, self->friendnum);
|
statusbar->status = tox_get_userstatus(m, self->friendnum);
|
||||||
statusbar->is_online = tox_friendstatus(m, self->friendnum) == TOX_FRIEND_ONLINE;
|
statusbar->is_online = tox_get_friend_connectionstatus(m, self->friendnum) == 1;
|
||||||
statusbar->max_len = x;
|
statusbar->max_len = x;
|
||||||
|
|
||||||
char statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
|
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
|
||||||
tox_copy_statusmessage(m, self->friendnum, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
|
tox_copy_statusmessage(m, self->friendnum, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
|
||||||
if (strncmp(statusmsg, "Online", strlen(statusmsg)))
|
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
||||||
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
|
||||||
|
|
||||||
/* Init subwindows */
|
/* Init subwindows */
|
||||||
ChatContext *ctx = (ChatContext *) self->x;
|
ChatContext *ctx = (ChatContext *) self->x;
|
||||||
|
75
src/prompt.c
75
src/prompt.c
@ -19,7 +19,7 @@ extern int store_data(Tox *m, char *path);
|
|||||||
uint8_t pending_requests[MAX_STR_SIZE][TOX_CLIENT_ID_SIZE]; // XXX
|
uint8_t pending_requests[MAX_STR_SIZE][TOX_CLIENT_ID_SIZE]; // XXX
|
||||||
uint8_t num_requests = 0; // XXX
|
uint8_t num_requests = 0; // XXX
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
/* commands */
|
/* commands */
|
||||||
@ -258,18 +258,21 @@ void cmd_clear(ToxWindow *self, Tox *m, int argc, char **argv)
|
|||||||
|
|
||||||
void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv)
|
void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||||
{
|
{
|
||||||
tox_IP_Port dht;
|
|
||||||
char *ip, *port, *key;
|
|
||||||
|
|
||||||
/* check arguments */
|
/* check arguments */
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
wprintw(self->window, "Invalid syntax.\n");
|
wprintw(self->window, "Invalid syntax.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ip = argv[1];
|
tox_IP_Port dht;
|
||||||
port = argv[2];
|
char *ip = argv[1];
|
||||||
key = argv[3];
|
char *port = argv[2];
|
||||||
|
char *key = argv[3];
|
||||||
|
|
||||||
|
if (!ip || !port || !key) {
|
||||||
|
wprintw(self->window, "Invalid syntax.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (atoi(port) == 0) {
|
if (atoi(port) == 0) {
|
||||||
wprintw(self->window, "Invalid syntax.\n");
|
wprintw(self->window, "Invalid syntax.\n");
|
||||||
@ -325,16 +328,14 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv)
|
|||||||
|
|
||||||
void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv)
|
void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||||
{
|
{
|
||||||
char *id, *msg;
|
|
||||||
|
|
||||||
/* check arguments */
|
/* check arguments */
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
wprintw(self->window, "Invalid syntax.\n");
|
wprintw(self->window, "Invalid syntax.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
id = argv[1];
|
char *id = argv[1];
|
||||||
msg = argv[2];
|
uint8_t *msg = argv[2];
|
||||||
|
|
||||||
if (id == NULL || msg == NULL) {
|
if (id == NULL || msg == NULL) {
|
||||||
wprintw(self->window, "Invalid syntax.\n");
|
wprintw(self->window, "Invalid syntax.\n");
|
||||||
@ -343,7 +344,7 @@ void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv)
|
|||||||
|
|
||||||
msg[strlen(++msg)-1] = L'\0';
|
msg[strlen(++msg)-1] = L'\0';
|
||||||
|
|
||||||
if (tox_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0)
|
if (tox_sendmessage(m, atoi(id), msg, strlen(msg) + 1) == 0)
|
||||||
wprintw(self->window, "Failed to send message.\n");
|
wprintw(self->window, "Failed to send message.\n");
|
||||||
else
|
else
|
||||||
wprintw(self->window, "Message successfully sent.\n");
|
wprintw(self->window, "Message successfully sent.\n");
|
||||||
@ -352,10 +353,11 @@ void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv)
|
|||||||
void cmd_myid(ToxWindow *self, Tox *m, int argc, char **argv)
|
void cmd_myid(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||||
{
|
{
|
||||||
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
|
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
|
||||||
size_t i;
|
|
||||||
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
||||||
tox_getaddress(m, address);
|
tox_getaddress(m, address);
|
||||||
|
|
||||||
|
size_t i;
|
||||||
|
|
||||||
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
|
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
|
||||||
char xx[3];
|
char xx[3];
|
||||||
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
|
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
|
||||||
@ -367,15 +369,13 @@ void cmd_myid(ToxWindow *self, Tox *m, int argc, char **argv)
|
|||||||
|
|
||||||
void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv)
|
void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||||
{
|
{
|
||||||
uint8_t *nick;
|
|
||||||
|
|
||||||
/* check arguments */
|
/* check arguments */
|
||||||
if (argc != 1) {
|
if (argc != 1) {
|
||||||
wprintw(self->window, "Invalid syntax.\n");
|
wprintw(self->window, "Invalid syntax.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
nick = argv[1];
|
uint8_t *nick = argv[1];
|
||||||
|
|
||||||
if (nick == NULL) {
|
if (nick == NULL) {
|
||||||
wprintw(self->window, "Invalid syntax.\n");
|
wprintw(self->window, "Invalid syntax.\n");
|
||||||
@ -393,14 +393,13 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv)
|
|||||||
|
|
||||||
void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv)
|
void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv)
|
||||||
{
|
{
|
||||||
char *status, *status_text;
|
|
||||||
uint8_t *msg = NULL;
|
|
||||||
|
|
||||||
if (argc < 1 || argc > 2) {
|
if (argc < 1 || argc > 2) {
|
||||||
wprintw(self->window, "Wrong number of arguments.\n");
|
wprintw(self->window, "Wrong number of arguments.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t *msg = NULL;
|
||||||
|
|
||||||
if (argc == 2) {
|
if (argc == 2) {
|
||||||
|
|
||||||
msg = argv[2];
|
msg = argv[2];
|
||||||
@ -416,7 +415,7 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
status = argv[1];
|
char *status = argv[1];
|
||||||
|
|
||||||
if (status == NULL) {
|
if (status == NULL) {
|
||||||
wprintw(self->window, "Invalid syntax.\n");
|
wprintw(self->window, "Invalid syntax.\n");
|
||||||
@ -454,18 +453,18 @@ void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argv[1] == NULL) {
|
uint8_t *msg = argv[1];
|
||||||
|
|
||||||
|
if (msg == NULL) {
|
||||||
wprintw(self->window, "Invalid syntax.\n");
|
wprintw(self->window, "Invalid syntax.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argv[1][0] != '\"') {
|
if (msg[0] != '\"') {
|
||||||
wprintw(self->window, "Messages must be enclosed in quotes.\n");
|
wprintw(self->window, "Messages must be enclosed in quotes.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *msg;
|
|
||||||
msg = argv[1];
|
|
||||||
msg[strlen(++msg)-1] = L'\0';
|
msg[strlen(++msg)-1] = L'\0';
|
||||||
|
|
||||||
tox_set_statusmessage(m, msg, strlen(msg) + 1);
|
tox_set_statusmessage(m, msg, strlen(msg) + 1);
|
||||||
@ -475,7 +474,7 @@ void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv)
|
|||||||
static void execute(ToxWindow *self, Tox *m, char *u_cmd)
|
static void execute(ToxWindow *self, Tox *m, char *u_cmd)
|
||||||
{
|
{
|
||||||
int newlines = 0;
|
int newlines = 0;
|
||||||
char cmd[MAX_STR_SIZE] = {0};
|
char cmd[MAX_STR_SIZE] = {'\0'};
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < strlen(prompt_buf); ++i) {
|
for (i = 0; i < strlen(prompt_buf); ++i) {
|
||||||
@ -511,7 +510,7 @@ static void execute(ToxWindow *self, Tox *m, char *u_cmd)
|
|||||||
else if (cmd[i] == '\"') {
|
else if (cmd[i] == '\"') {
|
||||||
while (cmd[++i] != '\"') {
|
while (cmd[++i] != '\"') {
|
||||||
if (cmd[i] == '\0') {
|
if (cmd[i] == '\0') {
|
||||||
wprintw(self->window, "Invalid command: did you forget a closing \"?\n");
|
wprintw(self->window, "Invalid command: did you forget an opening or closing \"?\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -536,18 +535,22 @@ static void execute(ToxWindow *self, Tox *m, char *u_cmd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* no input */
|
/* no input */
|
||||||
if (!cmdargs[0])
|
if (!cmdargs[0]) {
|
||||||
|
free(cmdargs);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* match input to command list */
|
/* match input to command list */
|
||||||
for (i = 0; i < NUM_COMMANDS; i++) {
|
for (i = 0; i < NUM_COMMANDS; i++) {
|
||||||
if (!strcmp(cmdargs[0], commands[i].name)) {
|
if (!strcmp(cmdargs[0], commands[i].name)) {
|
||||||
(commands[i].func)(self, m, numargs, cmdargs);
|
(commands[i].func)(self, m, numargs, cmdargs);
|
||||||
|
free(cmdargs);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* no match */
|
/* no match */
|
||||||
|
free(cmdargs);
|
||||||
wprintw(self->window, "Invalid command.\n");
|
wprintw(self->window, "Invalid command.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -634,11 +637,9 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
|
|||||||
wprintw(statusbar->topline, "[Offline]");
|
wprintw(statusbar->topline, "[Offline]");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (statusbar->statusmsg[0]) {
|
wattron(statusbar->topline, A_BOLD);
|
||||||
wattron(statusbar->topline, A_BOLD);
|
wprintw(statusbar->topline, " | %s", statusbar->statusmsg);
|
||||||
wprintw(statusbar->topline, " | %s", statusbar->statusmsg);
|
wattroff(statusbar->topline, A_BOLD);
|
||||||
wattroff(statusbar->topline, A_BOLD);
|
|
||||||
}
|
|
||||||
|
|
||||||
wprintw(statusbar->topline, "\n");
|
wprintw(statusbar->topline, "\n");
|
||||||
|
|
||||||
@ -670,13 +671,13 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m)
|
|||||||
statusbar->max_len = x;
|
statusbar->max_len = x;
|
||||||
|
|
||||||
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
|
||||||
tox_getselfname(m, (uint8_t *) &nick, TOX_MAX_NAME_LENGTH);
|
tox_getselfname(m, nick, TOX_MAX_NAME_LENGTH);
|
||||||
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
|
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
|
||||||
|
|
||||||
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
|
/* temporary until statusmessage saving works */
|
||||||
tox_copy_self_statusmessage(m, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
|
uint8_t *statusmsg = "Toxing on Toxic v0.2.0";
|
||||||
if (strncmp(statusmsg, "Online", strlen(statusmsg)))
|
// tox_copy_self_statusmessage(m, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
|
||||||
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
||||||
|
|
||||||
/* Init statusbar subwindow */
|
/* Init statusbar subwindow */
|
||||||
statusbar->topline = subwin(self->window, 2, x, 0, 0);
|
statusbar->topline = subwin(self->window, 2, x, 0, 0);
|
||||||
|
@ -32,7 +32,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd
|
|||||||
|
|
||||||
void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata)
|
void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata)
|
||||||
{
|
{
|
||||||
uint8_t nick[TOX_MAX_NAME_LENGTH] = {0};
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
|
||||||
tox_getname(m, friendnumber, (uint8_t *) &nick);
|
tox_getname(m, friendnumber, (uint8_t *) &nick);
|
||||||
|
|
||||||
if (!nick[0])
|
if (!nick[0])
|
||||||
|
Loading…
Reference in New Issue
Block a user