1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-06-26 19:36:45 +02:00

Compare commits

...

30 Commits
0.2.6 ... 0.2.7

Author SHA1 Message Date
7f38c3c6e7 add prompt logging support 2014-03-01 18:06:35 -05:00
7109b8fa18 refactor logging functions to only handle chatlog pointers 2014-03-01 07:10:44 -05:00
1e503b1080 fix 2014-02-27 23:38:15 -05:00
4fb82cceaa save logging preference for friend chats and improve log command message 2014-02-27 23:33:00 -05:00
46b046a209 make C-e and C-aa work like they do in bash and fix/format help messages 2014-02-27 18:55:18 -05:00
6ee1f1ed0f fix 2014-02-26 21:30:27 -05:00
044b731089 Fix bug 2014-02-26 21:00:35 -05:00
d83ef1d8be update help messages 2014-02-26 19:45:11 -05:00
459eb64dc4 Merge branch 'master' of https://github.com/Tox/toxic 2014-02-26 19:03:15 -05:00
af5627b050 update to 0.2.7 2014-02-26 19:02:22 -05:00
9b57c05648 add command to turn logs on/off 2014-02-26 19:00:13 -05:00
817f763589 give groupchat logs unique names 2014-02-26 17:15:34 -05:00
8e4db369bc log events 2014-02-26 06:35:19 -05:00
a61f5f6a6d properly close windows on exit 2014-02-26 05:23:11 -05:00
5ff7065744 basic logging for groupchats 2014-02-26 03:51:26 -05:00
831d8e5f24 implement chat logging 2014-02-26 01:51:06 -05:00
6896292c49 Update README.md 2014-02-25 03:07:03 -05:00
b6613a015f add license info to source files 2014-02-25 02:28:24 -05:00
2d9f4facf7 connect to limited number of nodes on init instead of all of them 2014-02-24 20:08:51 -05:00
e7920d1da7 fix connection error codes 2014-02-24 19:41:02 -05:00
eb09fceed7 fix bug and some cleanup 2014-02-24 19:18:43 -05:00
b308e19e6b Merge pull request #80 from viric/dhtservers
Fallback to loading /usr/share/toxic/DHTservers.
2014-02-24 02:17:14 -05:00
5867f1a672 Merge pull request #82 from kl4ng/master
down arrow returns empty string if at end of history
2014-02-23 12:49:20 -08:00
5187861b69 down arrow returns empty string if at end of history 2014-02-23 14:22:45 -05:00
0013dae552 Merge branch 'master' of https://github.com/Tox/toxic 2014-02-23 05:40:21 -05:00
b018aa384e small fix and don't show typing alert for /commands 2014-02-23 05:38:44 -05:00
ad8f99dae4 Revert that 2014-02-23 02:19:22 -08:00
5b9d3f6f62 fix segfault 2014-02-23 05:15:48 -05:00
93bcecde70 Update README.md 2014-02-23 02:11:51 -08:00
6be1c907d9 Fallback to loading /usr/share/toxic/DHTservers.
First try ~/.config/tox/DHTservers, and fallback to the /usr/share
one if the former fails.

This should allow people just starting toxic for the first time to
connect to the DHT.
2014-02-23 09:52:07 +00:00
28 changed files with 991 additions and 182 deletions

View File

@ -1,6 +1,6 @@
## Toxic - console client for [Tox](http://tox.im)
## Toxic
The client formerly resided in the [Tox core repository](https://github.com/irungentoo/ProjectTox-Core) and is now available as a standalone program. It looks like [this](http://wiki.tox.im/images/b/b6/Ncursesclient1.png).
Toxic is an ncurses based instant messaging client for [Tox](http://tox.im) which formerly resided in the [Tox core repository](https://github.com/irungentoo/ProjectTox-Core) and is now available as a standalone program. It looks like [this](http://i.imgur.com/hL7WhVl.png).
To compile, first generate the configure script by running the ```autoreconf -i``` command.
@ -14,7 +14,7 @@ Then execute the configure script with ./configure (you may need to pass it the
echo '/usr/local/lib/' | sudo tee -a /etc/ld.so.conf.d/locallib.conf
sudo ldconfig
```
If you dont already have them, you might want to install the ncurses libraries, on Debian:
If you dont already have them, you may need to install the ncurses libraries. For Debian based systems:
```
sudo apt-get install libncurses5-dev libncursesw5-dev
```

View File

@ -25,7 +25,9 @@ toxic_SOURCES = $(top_srcdir)/src/main.c \
$(top_srcdir)/src/misc_tools.c \
$(top_srcdir)/src/misc_tools.h \
$(top_srcdir)/src/toxic_strings.c \
$(top_srcdir)/src/toxic_strings.h
$(top_srcdir)/src/toxic_strings.h \
$(top_srcdir)/src/log.c \
$(top_srcdir)/src/log.h
toxic_CFLAGS = -I$(top_srcdir) \
$(NCURSES_CFLAGS) \

View File

@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.65])
AC_INIT([toxic], [0.2.6], [https://tox.im/])
AC_INIT([toxic], [0.2.7], [https://tox.im/])
AC_CONFIG_AUX_DIR(configure_aux)
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* chat.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/>.
*
*/
#ifdef HAVE_CONFIG_H
@ -15,6 +33,7 @@
#include "misc_tools.h"
#include "friendlist.h"
#include "toxic_strings.h"
#include "log.h"
extern char *DATA_FILE;
extern int store_data(Tox *m, char *path);
@ -22,7 +41,7 @@ extern int store_data(Tox *m, char *path);
extern FileSender file_senders[MAX_FILES];
extern ToxicFriend friends[MAX_FRIENDS_NUM];
#define AC_NUM_CHAT_COMMANDS 17
#define AC_NUM_CHAT_COMMANDS 18
/* Array of chat command names used for tab completion. */
static const uint8_t chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = {
@ -36,6 +55,7 @@ static const uint8_t chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/help" },
{ "/invite" },
{ "/join" },
{ "/log" },
{ "/myid" },
{ "/nick" },
{ "/note" },
@ -53,6 +73,25 @@ static void set_typingstatus(ToxWindow *self, Tox *m, bool is_typing)
ctx->self_is_typing = is_typing;
}
void kill_chat_window(ToxWindow *self)
{
set_active_window(0);
ChatContext *ctx = self->chatwin;
StatusBar *statusbar = self->stb;
log_disable(ctx->log);
int f_num = self->num;
delwin(ctx->linewin);
delwin(statusbar->topline);
del_window(self);
disable_chatwin(f_num);
free(ctx->log);
free(ctx);
free(statusbar);
}
static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len)
{
if (self->num != num)
@ -76,6 +115,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1
} else
wprintw(ctx->history, "%s\n", msg);
add_to_log_buf(msg, nick, ctx->log, false);
alert_window(self, WINDOW_ALERT_1, true);
}
@ -119,6 +159,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin
wprintw(ctx->history, "* %s %s\n", nick, action);
wattroff(ctx->history, COLOR_PAIR(YELLOW));
add_to_log_buf(action, nick, ctx->log, true);
alert_window(self, WINDOW_ALERT_1, true);
}
@ -245,7 +286,6 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u
uint8_t *filename = friends[num].file_receiver.filenames[filenum];
FILE *file_to_save = fopen(filename, "a");
// we have a problem here, but don't let it segfault
if (file_to_save == NULL) {
wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, "* Error writing to file.\n");
@ -300,6 +340,8 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *acti
wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, " * Failed to send action\n");
wattroff(ctx->history, COLOR_PAIR(RED));
} else {
add_to_log_buf(action, selfname, ctx->log, true);
}
}
@ -350,14 +392,14 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
beep();
}
else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */
else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */
if (ctx->pos > 0) {
ctx->pos = 0;
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
}
}
else if (key == KEY_END) { /* END key: move cursor to end of line */
else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */
if (ctx->pos != ctx->len) {
ctx->pos = ctx->len;
mv_curs_end(self->window, MAX(0, wcswidth(ctx->line, (CHATBOX_HEIGHT-1)*x2)), y2, x2);
@ -440,7 +482,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
wmove(self->window, y, x + MAX(1, wcwidth(key)));
}
if (!ctx->self_is_typing)
if (!ctx->self_is_typing && ctx->line[0] != '/')
set_typingstatus(self, m, true);
}
/* RETURN key: Execute command or print line */
@ -453,22 +495,22 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
wclear(ctx->linewin);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
wclrtobot(self->window);
bool close_win = false;
if (!string_is_empty(line))
add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos);
if (line[0] == '/') {
if (close_win = !strcmp(line, "/close")) {
int f_num = self->num;
delwin(ctx->linewin);
delwin(statusbar->topline);
del_window(self);
disable_chatwin(f_num);
} else if (strncmp(line, "/me ", strlen("/me ")) == 0)
if (strcmp(line, "/close") == 0) {
if (ctx->self_is_typing)
set_typingstatus(self, m, false);
kill_chat_window(self);
return;
} else if (strncmp(line, "/me ", strlen("/me ")) == 0) {
send_action(self, ctx, m, line + strlen("/me "));
else
} else {
execute(ctx->history, self, m, line, CHAT_COMMAND_MODE);
}
} else if (!string_is_empty(line)) {
uint8_t selfname[TOX_MAX_NAME_LENGTH];
tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH);
@ -489,15 +531,12 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, " * Failed to send message.\n");
wattroff(ctx->history, COLOR_PAIR(RED));
} else {
add_to_log_buf(line, selfname, ctx->log, false);
}
}
if (close_win) {
free(ctx);
free(statusbar);
} else {
reset_buf(ctx->line, &ctx->pos, &ctx->len);
}
reset_buf(ctx->line, &ctx->pos, &ctx->len);
}
if (ctx->len <= 0 && ctx->self_is_typing)
@ -630,8 +669,24 @@ static void chat_onInit(ToxWindow *self, Tox *m)
ctx->history = subwin(self->window, y2-CHATBOX_HEIGHT+1, x2, 0, 0);
scrollok(ctx->history, 1);
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2-CHATBOX_HEIGHT, 0);
ctx->log = malloc(sizeof(struct chatlog));
if (ctx->log == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
memset(ctx->log, 0, sizeof(struct chatlog));
if (friends[self->num].logging_on)
log_enable(self->name, friends[self->num].pub_key, ctx->log);
wprintw(ctx->history, "\n\n");
execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE);
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
}
@ -641,6 +696,7 @@ ToxWindow new_chat(Tox *m, int friendnum)
memset(&ret, 0, sizeof(ret));
ret.active = true;
ret.is_chat = true;
ret.onKey = &chat_onKey;
ret.onDraw = &chat_onDraw;

View File

@ -1,8 +1,31 @@
/* chat.h
*
*
* 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/>.
*
*/
#ifndef CHAT_H_6489PZ13
#define CHAT_H_6489PZ13
#include "toxic_windows.h"
void kill_chat_window(ToxWindow *self);
ToxWindow new_chat(Tox *m, int friendnum);
#endif /* end of include guard: CHAT_H_6489PZ13 */

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* chat_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/>.
*
*/
#ifdef HAVE_CONFIG_H
@ -12,6 +30,7 @@
#include "toxic_windows.h"
#include "misc_tools.h"
#include "friendlist.h"
#include "execute.h"
extern ToxWindow *prompt;
@ -22,23 +41,25 @@ extern uint8_t max_file_senders_index;
void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc == 1) {
if (!strcmp(argv[1], "global")) {
execute(window, self, m, "/help", GLOBAL_COMMAND_MODE);
return;
}
}
wattron(window, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(window, "Chat commands:\n");
wattroff(window, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(window, " /status <type> <msg> : Set your status with optional note\n");
wprintw(window, " /note <msg> : Set a personal note\n");
wprintw(window, " /nick <nick> : Set your nickname\n");
wprintw(window, " /invite <n> : Invite friend to a group chat\n");
wprintw(window, " /me <action> : Do an action\n");
wprintw(window, " /myid : Print your ID\n");
wprintw(window, " /join : Join a pending group chat\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");
wprintw(window, " /invite <n> : Invite friend to a group chat\n");
wprintw(window, " /join : Join a pending group chat\n");
wprintw(window, " /log <on> or <off> : Enable/disable logging\n");
wprintw(window, " /sendfile <filepath> : Send a file\n");
wprintw(window, " /savefile <n> : Receive a file\n");
wprintw(window, " /close : Close the current chat window\n");
wprintw(window, " /help : Print this message again\n");
wprintw(window, " /help global : Show a list of global commands\n");
wattron(window, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(window, " * Argument messages must be enclosed in quotation marks.\n\n");

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* chat_commands.h
*
*
* 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/>.
*
*/
void cmd_chat_help(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);

View File

@ -1,20 +1,22 @@
/*
* Copyright (C) 2013 Tox project All Rights Reserved.
/* configdir.c
*
* This file is part of Tox.
*
* Tox is free software: you can redistribute it and/or modify
* 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.
*
* Tox is distributed in the hope that it will be useful,
* 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 Tox. If not, see <http://www.gnu.org/licenses/>.
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
*/

View File

@ -1,20 +1,22 @@
/*
* Copyright (C) 2013 Tox project All Rights Reserved.
/* configdir.h
*
* This file is part of Tox.
*
* Tox is free software: you can redistribute it and/or modify
* 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.
*
* Tox is distributed in the hope that it will be useful,
* 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 Tox. If not, see <http://www.gnu.org/licenses/>.
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
*/

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* execute.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/>.
*
*/
#include <stdlib.h>
@ -23,6 +41,7 @@ static struct cmd_func global_commands[] = {
{ "/exit", cmd_quit },
{ "/groupchat", cmd_groupchat },
{ "/help", cmd_prompt_help },
{ "/log", cmd_log },
{ "/myid", cmd_myid },
{ "/nick", cmd_nick },
{ "/note", cmd_note },

View File

@ -1,9 +1,27 @@
/*
* Toxic -- Tox Curses Client
/* execute.h
*
*
* 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/>.
*
*/
#define MAX_NUM_ARGS 4 /* Includes command */
#define GLOBAL_NUM_COMMANDS 13
#define GLOBAL_NUM_COMMANDS 14
#define CHAT_NUM_COMMANDS 5
enum {

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* friendlist.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/>.
*
*/
#ifdef HAVE_CONFIG_H

View File

@ -1,3 +1,25 @@
/* friendlist.h
*
*
* 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/>.
*
*/
#ifndef FRIENDLIST_H_53I41IM
#define FRIENDLIST_H_53I41IM
@ -15,6 +37,7 @@ typedef struct {
bool active;
bool online;
bool is_typing;
bool logging_on; /* saves preference for friend irrespective of chat windows */
TOX_USERSTATUS status;
struct FileReceiver file_receiver;
} ToxicFriend;

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* 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/>.
*
*/
#ifdef HAVE_CONFIG_H
@ -202,6 +220,73 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg
wprintw(window, "Group chat created as %d.\n", groupnum);
}
void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc == 0) {
bool on;
if (self->is_chat || self->is_groupchat)
on = self->chatwin->log->log_on;
else if (self->is_prompt)
on = self->promptbuf->log->log_on;
if (on) {
wprintw(window, "Logging for this window is ");
wattron(window, COLOR_PAIR(GREEN) | A_BOLD);
wprintw(window, "[on]");
wattroff(window, COLOR_PAIR(GREEN) | A_BOLD);
wprintw(window, ". Type \"/log off\" to disable.\n");
} else {
wprintw(window, "Logging for this window is ");
wattron(window, COLOR_PAIR(RED) | A_BOLD);
wprintw(window, "[off]");
wattroff(window, COLOR_PAIR(RED) | A_BOLD);
wprintw(window, ". Type \"/log on\" to enable.\n");
}
return;
}
uint8_t *swch = argv[1];
if (!strcmp(swch, "1") || !strcmp(swch, "on")) {
if (self->is_chat) {
friends[self->num].logging_on = true;
log_enable(self->name, friends[self->num].pub_key, self->chatwin->log);
} else if (self->is_prompt) {
uint8_t myid[TOX_FRIEND_ADDRESS_SIZE];
tox_get_address(m, myid);
log_enable(self->name, &myid, self->promptbuf->log);
} else if (self->is_groupchat) {
log_enable(self->name, NULL, self->chatwin->log);
}
wprintw(window, "Logging ");
wattron(window, COLOR_PAIR(GREEN) | A_BOLD);
wprintw(window, "[on]\n");
wattroff(window, COLOR_PAIR(GREEN) | A_BOLD);
return;
} else if (!strcmp(swch, "0") || !strcmp(swch, "off")) {
if (self->is_chat) {
friends[self->num].logging_on = false;
log_disable(self->chatwin->log);
} else if (self->is_prompt) {
log_disable(self->promptbuf->log);
} else if (self->is_groupchat) {
log_disable(self->chatwin->log);
}
wprintw(window, "Logging ");
wattron(window, COLOR_PAIR(RED) | A_BOLD);
wprintw(window, "[off]\n");
wattroff(window, COLOR_PAIR(RED) | A_BOLD);
return;
}
wprintw(window, "Invalid option. Use \"/log on\" and \"/log off\" to toggle logging.\n");
}
void cmd_myid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
@ -280,17 +365,18 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a
wprintw(window, "\n\nGlobal commands:\n");
wattroff(window, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(window, " /add <id> <msg> : Add friend with optional message\n");
wprintw(window, " /accept <n> : Accept friend request\n");
wprintw(window, " /connect <ip> <port> <key> : Manually connect to a DHT server\n");
wprintw(window, " /status <type> <msg> : Set your status with optional note\n");
wprintw(window, " /note <msg> : Set a personal note\n");
wprintw(window, " /nick <nick> : Set your nickname\n");
wprintw(window, " /groupchat : Create a group chat\n");
wprintw(window, " /myid : Print your ID\n");
wprintw(window, " /quit or /exit : Exit Toxic\n");
wprintw(window, " /help : Print this message again\n");
wprintw(window, " /clear : Clear the window\n");
wprintw(window, " /add <id> <msg> : Add friend with optional message\n");
wprintw(window, " /accept <n> : Accept friend request\n");
wprintw(window, " /connect <ip> <port> <key> : Manually connect to a DHT server\n");
wprintw(window, " /status <type> <msg> : Set status with optional note\n");
wprintw(window, " /note <msg> : Set a personal note\n");
wprintw(window, " /nick <nick> : Set your nickname\n");
wprintw(window, " /log <on> or <off> : Enable/disable logging\n");
wprintw(window, " /groupchat : Create a group chat\n");
wprintw(window, " /myid : Print your ID\n");
wprintw(window, " /help : Print this message again\n");
wprintw(window, " /clear : Clear the window\n");
wprintw(window, " /quit or /exit : Exit Toxic\n");
wattron(window, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(window, " * Argument messages must be enclosed in quotation marks.\n");

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* global_commands.h
*
*
* 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/>.
*
*/
void cmd_accept(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
@ -7,6 +25,7 @@ void cmd_add(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE])
void cmd_clear(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_connect(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_groupchat(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_log(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_myid(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_nick(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_note(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* groupchat.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/>.
*
*/
#ifdef HAVE_CONFIG_H
@ -16,6 +34,7 @@
#include "groupchat.h"
#include "prompt.h"
#include "toxic_strings.h"
#include "log.h"
extern char *DATA_FILE;
extern int store_data(Tox *m, char *path);
@ -53,8 +72,20 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum)
return -1;
}
static void close_groupchatwin(Tox *m, int groupnum)
void kill_groupchat_window(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
log_disable(ctx->log);
delwin(ctx->linewin);
del_window(self);
free(ctx->log);
free(ctx);
}
static void close_groupchat(ToxWindow *self, Tox *m, int groupnum)
{
set_active_window(0);
tox_del_groupchat(m, groupnum);
free(groupchats[groupnum].peer_names);
@ -69,6 +100,7 @@ static void close_groupchatwin(Tox *m, int groupnum)
}
max_groupchat_index = i;
kill_groupchat_window(self);
}
static void print_groupchat_help(ChatContext *ctx)
@ -77,16 +109,15 @@ static void print_groupchat_help(ChatContext *ctx)
wprintw(ctx->history, "Group chat commands:\n");
wattroff(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(ctx->history, " /add <id> <msg> : Add friend with optional message\n");
wprintw(ctx->history, " /status <type> <msg>: Set your status with optional note\n");
wprintw(ctx->history, " /note <msg> : Set a personal note\n");
wprintw(ctx->history, " /nick <nick> : Set your nickname\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");
wprintw(ctx->history, " /add <id> <msg> : Add friend with optional message\n");
wprintw(ctx->history, " /status <type> <msg>: Set your status with optional note\n");
wprintw(ctx->history, " /note <msg> : Set a personal note\n");
wprintw(ctx->history, " /nick <nick> : Set your nickname\n");
wprintw(ctx->history, " /groupchat : Create a group chat\n");
wprintw(ctx->history, " /log <on> or <off> : Enable/disable logging\n");
wprintw(ctx->history, " /close : Close the current group chat\n");
wprintw(ctx->history, " /help : Print this message again\n");
wprintw(ctx->history, " /help global : Show a list of global commands\n");
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(ctx->history, " * Argument messages must be enclosed in quotation marks.\n");
@ -136,6 +167,8 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int
} else {
wprintw(ctx->history, "%s\n", msg);
}
add_to_log_buf(msg, nick, ctx->log, false);
}
static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t *action,
@ -170,6 +203,8 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p
wattron(ctx->history, COLOR_PAIR(YELLOW));
wprintw(ctx->history, "* %s %s\n", nick, action);
wattroff(ctx->history, COLOR_PAIR(YELLOW));
add_to_log_buf(action, nick, ctx->log, true);
}
/* Puts two copies of peerlist in chat instance */
@ -234,25 +269,36 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
ChatContext *ctx = self->chatwin;
print_time(ctx->history);
uint8_t *event;
switch (change) {
case TOX_CHAT_CHANGE_PEER_ADD:
event = "has joined the room";
wattron(ctx->history, COLOR_PAIR(GREEN));
wattron(ctx->history, A_BOLD);
wprintw(ctx->history, "* %s", peername);
wattroff(ctx->history, A_BOLD);
wprintw(ctx->history, " has joined the room\n");
wprintw(ctx->history, " %s\n", event);
wattroff(ctx->history, COLOR_PAIR(GREEN));
add_to_log_buf(event, peername, ctx->log, true);
break;
case TOX_CHAT_CHANGE_PEER_DEL:
event = "has left the room";
wattron(ctx->history, A_BOLD);
wprintw(ctx->history, "* %s", oldpeername);
wattroff(ctx->history, A_BOLD);
wprintw(ctx->history, " has left the room\n");
wprintw(ctx->history, " %s\n", event);
if (groupchats[self->num].side_pos > 0)
--groupchats[self->num].side_pos;
add_to_log_buf(event, oldpeername, ctx->log, true);
break;
case TOX_CHAT_CHANGE_PEER_NAME:
wattron(ctx->history, COLOR_PAIR(MAGENTA));
wattron(ctx->history, A_BOLD);
@ -265,6 +311,10 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
wprintw(ctx->history, "%s\n", peername);
wattroff(ctx->history, A_BOLD);
wattroff(ctx->history, COLOR_PAIR(MAGENTA));
uint8_t tmp_event[TOXIC_MAX_NAME_LENGTH + 32];
snprintf(tmp_event, sizeof(tmp_event), "is now known as %s", peername);
add_to_log_buf(tmp_event, oldpeername, ctx->log, true);
break;
}
@ -338,14 +388,14 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
beep();
}
else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */
else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */
if (ctx->pos > 0) {
ctx->pos = 0;
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
}
}
else if (key == KEY_END) { /* END key: move cursor to end of line */
else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */
if (ctx->pos != ctx->len) {
ctx->pos = ctx->len;
mv_curs_end(self->window, MAX(0, wcswidth(ctx->line, (CHATBOX_HEIGHT-1)*x2)), y2, x2);
@ -458,24 +508,25 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
wclear(ctx->linewin);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
wclrtobot(self->window);
bool close_win = false;
if (!string_is_empty(line))
add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos);
if (line[0] == '/') {
if (close_win = strcmp(line, "/close") == 0) {
set_active_window(0);
int groupnum = self->num;
delwin(ctx->linewin);
del_window(self);
close_groupchatwin(m, groupnum);
} else if (strcmp(line, "/help") == 0)
print_groupchat_help(ctx);
else if (strncmp(line, "/me ", strlen("/me ")) == 0)
if (strcmp(line, "/close") == 0) {
close_groupchat(self, m, self->num);
return;
} else if (strcmp(line, "/help") == 0) {
if (strcmp(line, "help global") == 0)
execute(ctx->history, self, m, "/help", GLOBAL_COMMAND_MODE);
else
print_groupchat_help(ctx);
} else if (strncmp(line, "/me ", strlen("/me ")) == 0) {
send_group_action(self, ctx, m, line + strlen("/me "));
else
} else {
execute(ctx->history, self, m, line, GROUPCHAT_COMMAND_MODE);
}
} else if (!string_is_empty(line)) {
if (tox_group_message_send(m, self->num, line, strlen(line) + 1) == -1) {
wattron(ctx->history, COLOR_PAIR(RED));
@ -484,10 +535,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
}
}
if (close_win)
free(ctx);
else
reset_buf(ctx->line, &ctx->pos, &ctx->len);
reset_buf(ctx->line, &ctx->pos, &ctx->len);
}
}
@ -555,7 +603,19 @@ static void groupchat_onInit(ToxWindow *self, Tox *m)
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x, y-CHATBOX_HEIGHT, 0);
ctx->sidebar = subwin(self->window, y-CHATBOX_HEIGHT+1, SIDEBAR_WIDTH, 0, x-SIDEBAR_WIDTH);
ctx->log = malloc(sizeof(struct chatlog));
if (ctx->log == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
memset(ctx->log, 0, sizeof(struct chatlog));
print_groupchat_help(ctx);
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);
wmove(self->window, y-CURS_Y_OFFSET, 0);
}
@ -565,6 +625,7 @@ ToxWindow new_group_chat(Tox *m, int groupnum)
memset(&ret, 0, sizeof(ret));
ret.active = true;
ret.is_groupchat = true;
ret.onKey = &groupchat_onKey;
ret.onDraw = &groupchat_onDraw;

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* groupchat.h
*
*
* 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/>.
*
*/
#define SIDEBAR_WIDTH 16
@ -14,5 +32,6 @@ typedef struct {
uint8_t *oldpeer_names;
} GroupChat;
void kill_groupchat_window(ToxWindow *self);
int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum);
ToxWindow new_group_chat(Tox *m, int groupnum);

138
src/log.c Normal file
View File

@ -0,0 +1,138 @@
/* log.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/>.
*
*/
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "configdir.h"
#include "toxic_windows.h"
#include "misc_tools.h"
/* gets the log path by appending to the config dir the name and a pseudo-unique identity */
void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log)
{
if (!log->log_on)
return;
char *user_config_dir = get_user_config_dir();
int path_len = strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(name);
/* use first 4 digits of key as log ident. If no key use a timestamp */
uint8_t ident[32];
if (key != NULL) {
path_len += (KEY_IDENT_DIGITS * 2 + 5);
sprintf(&ident[0], "%02X", key[0] & 0xff);
sprintf(&ident[2], "%02X", key[2] & 0xff);
ident[KEY_IDENT_DIGITS*2+1] = '\0';
} else {
struct tm *tminfo = get_time();
snprintf(ident, sizeof(ident),
"%04d-%02d-%02d[%02d:%02d:%02d]", tminfo->tm_year+1900,tminfo->tm_mon+1, tminfo->tm_mday,
tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec);
path_len += strlen(ident) + 1;
}
if (path_len > MAX_STR_SIZE) {
log->log_on = false;
return;
}
snprintf(log->log_path, MAX_STR_SIZE, "%s%s%s-%s.log",
user_config_dir, CONFIGDIR, name, ident);
FILE *logfile = fopen(log->log_path, "a");
if (logfile == NULL) {
log->log_on = false;
return;
}
fprintf(logfile, "\n*** NEW SESSION ***\n\n");
fclose(logfile);
free(user_config_dir);
}
/* writes contents from a chatcontext's log buffer to respective log file and resets log pos.
This is triggered when the log buffer is full, but may be forced. */
void write_to_log(struct chatlog *log)
{
if (!log->log_on)
return;
FILE *logfile = fopen(log->log_path, "a");
if (logfile == NULL) {
log->log_on = false;
return;
}
int i;
for (i = 0; i < log->pos; ++i)
fprintf(logfile, "%s", log->log_buf[i]);
log->pos = 0;
fclose(logfile);
}
/* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log.
If event is true, formats line as an event, e.g. * name has gone offline */
void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event)
{
if (!log->log_on)
return;
uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3];
if (event)
snprintf(name_frmt, sizeof(name_frmt), "* %s", name);
else
snprintf(name_frmt, sizeof(name_frmt), "%s:", name);
struct tm *tminfo = get_time();
snprintf(log->log_buf[log->pos], MAX_LOG_LINE_SIZE, "%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n",
tminfo->tm_year + 1900, tminfo->tm_mon + 1, tminfo->tm_mday,
tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name_frmt, msg);
if (++(log->pos) >= MAX_LOG_BUF_LINES)
write_to_log(log);
}
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log)
{
log->log_on = true;
if (!log->log_path[0])
init_logging_session(name, key, log);
}
void log_disable(struct chatlog *log)
{
if (log->log_on) {
write_to_log(log);
log->log_on = false;
}
}

35
src/log.h Normal file
View File

@ -0,0 +1,35 @@
/* log.h
*
*
* 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/>.
*
*/
/* gets the log path by appending to the config dir the name and a pseudo-unique identity */
void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log);
/* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log.
If event is true, formats line as an event, e.g. * name has gone offline */
void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event);
/* writes contents from a chatcontext's log buffer to respective log file and resets log pos.
This is triggered automatically when the log buffer is full, but may be forced. */
void write_to_log(struct chatlog *log);
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log);
void log_disable(struct chatlog *log);

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* main.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/>.
*
*/
#ifdef HAVE_CONFIG_H
@ -47,8 +65,11 @@
/* Export for use in Callbacks */
char *DATA_FILE = NULL;
char *SRVLIST_FILE = NULL;
ToxWindow *prompt = NULL;
static int f_loadfromfile; /* 1 if we want to load from/save the data file, 0 otherwise */
FileSender file_senders[MAX_FILES];
uint8_t max_file_senders_index;
@ -146,11 +167,14 @@ static char servers[MAXSERVERS][SERVERLEN];
static uint16_t ports[MAXSERVERS];
static uint8_t keys[MAXSERVERS][TOX_CLIENT_ID_SIZE];
int serverlist_load(void)
static int serverlist_load(const char *filename)
{
FILE *fp = NULL;
fp = fopen(SRVLIST_FILE, "r");
if (!filename)
return 1;
fp = fopen(filename, "r");
if (fp == NULL)
return 1;
@ -186,10 +210,10 @@ int serverlist_load(void)
return 0;
}
int init_connection_helper(Tox *m, int linenumber)
int init_connection_helper(Tox *m, int line)
{
return tox_bootstrap_from_address(m, servers[linenumber], TOX_ENABLE_IPV6_DEFAULT,
ports[linenumber], keys[linenumber]);
return tox_bootstrap_from_address(m, servers[line], TOX_ENABLE_IPV6_DEFAULT,
ports[line], keys[line]);
}
/* Connects to a random DHT server listed in the DHTservers file
@ -197,40 +221,51 @@ int init_connection_helper(Tox *m, int linenumber)
* return codes:
* 1: failed to open server file
* 2: no line of sufficient length in server file
* 3: (old, removed) failed to split a selected line in the server file
* 4: failed to resolve name to IP
* 5: serverlist file contains no acceptable line
* 3: failed to resolve name to IP
* 4: serverlist file contains no acceptable line
*/
static int init_connection_serverlist_loaded = 0;
static bool srvlist_loaded = false;
#define NUM_INIT_NODES 5
int init_connection(Tox *m)
{
if (linecnt > 0) /* already loaded serverlist */
return init_connection_helper(m, rand() % linecnt) ? 0 : 4;
return init_connection_helper(m, rand() % linecnt) ? 0 : 3;
/* only once:
* - load the serverlist
* - connect to "everyone" inside
*/
if (!init_connection_serverlist_loaded) {
init_connection_serverlist_loaded = 1;
int res = serverlist_load();
if (res)
return res;
if (!srvlist_loaded) {
srvlist_loaded = true;
int res = serverlist_load(SRVLIST_FILE);
if (res) {
/* Fallback on the provided DHTServers in /usr/share or /usr/local/share,
so new starts of toxic will connect to the DHT. */
res = serverlist_load(PACKAGE_DATADIR "/DHTservers");
if (res)
return res;
}
if (!linecnt)
return 4;
return 2;
res = 6;
int linenumber;
for(linenumber = 0; linenumber < linecnt; linenumber++)
if (init_connection_helper(m, linenumber))
res = 3;
int i;
int n = MIN(NUM_INIT_NODES, linecnt);
for(i = 0; i < n; ++i)
if (init_connection_helper(m, rand() % linecnt))
res = 0;
return res;
}
/* empty serverlist file */
return 5;
return 4;
}
static void do_connection(Tox *m, ToxWindow *prompt)
@ -263,8 +298,6 @@ static void do_connection(Tox *m, ToxWindow *prompt)
}
}
int f_loadfromfile;
/*
* Store Messenger to given location
* Return 0 stored successfully
@ -442,9 +475,12 @@ void exit_toxic(Tox *m)
fclose(file_senders[i].file);
}
kill_all_windows();
free(DATA_FILE);
free(SRVLIST_FILE);
free(prompt->stb);
log_disable(prompt->promptbuf->log);
free(prompt->promptbuf->log);
free(prompt->promptbuf);
tox_kill(m);
endwin();
@ -471,7 +507,7 @@ int main(int argc, char *argv[])
int i = 0;
int f_use_ipv4 = 0;
// Make sure all written files are read/writeable only by the current user.
/* Make sure all written files are read/writeable only by the current user. */
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
for (i = 0; i < argc; ++i) {
@ -509,19 +545,15 @@ int main(int argc, char *argv[])
}
}
if (config_err) {
SRVLIST_FILE = strdup(PACKAGE_DATADIR "/DHTservers");
SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1);
if (SRVLIST_FILE != NULL) {
strcpy(SRVLIST_FILE, user_config_dir);
strcat(SRVLIST_FILE, CONFIGDIR);
strcat(SRVLIST_FILE, "DHTservers");
} else {
SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1);
if (SRVLIST_FILE != NULL) {
strcpy(SRVLIST_FILE, user_config_dir);
strcat(SRVLIST_FILE, CONFIGDIR);
strcat(SRVLIST_FILE, "DHTservers");
} else {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
free(user_config_dir);

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* misc_tools.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/>.
*
*/
#include <stdlib.h>
@ -52,8 +70,8 @@ void print_time(WINDOW *window)
wattroff(window,COLOR_PAIR(BLUE));
}
/* Returns 1 if the string is empty, 0 otherwise */
int string_is_empty(char *string)
/* Returns true if the string is empty, false otherwise */
bool string_is_empty(char *string)
{
return string[0] == '\0';
}

View File

@ -1,9 +1,27 @@
/*
* Toxic -- Tox Curses Client
/* misc_tools.h
*
*
* 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/>.
*
*/
// #define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
/* convert a hex string to binary */
unsigned char *hex_string_to_bin(char hex_string[]);
@ -14,8 +32,8 @@ struct tm *get_time(void);
/* Prints the time to given window */
void print_time(WINDOW *window);
/* Returns 1 if the string is empty, 0 otherwise */
int string_is_empty(char *string);
/* Returns true if the string is empty, false otherwise */
bool string_is_empty(char *string);
/* convert a multibyte string to a wide character string (must provide buffer) */
int char_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n);

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* prompt.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/>.
*
*/
#ifdef HAVE_CONFIG_H
@ -30,6 +48,7 @@ const uint8_t glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/groupchat" },
{ "/help" },
{ "/join" },
{ "/log" },
{ "/myid" },
{ "/nick" },
{ "/note" },
@ -154,12 +173,12 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
beep();
}
else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */
else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */
if (prt->pos != 0)
prt->pos = 0;
}
else if (key == KEY_END) { /* END key: move cursor to end of line */
else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */
if (prt->pos != prt->len)
prt->pos = prt->len;
}
@ -341,6 +360,18 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
static void prompt_onInit(ToxWindow *self, Tox *m)
{
scrollok(self->window, true);
PromptBuf *prt = self->promptbuf;
prt->log = malloc(sizeof(struct chatlog));
if (prt->log == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
memset(prt->log, 0, sizeof(struct chatlog));
execute(self->window, self, m, "/help", GLOBAL_COMMAND_MODE);
wclrtoeol(self->window);
}
@ -350,6 +381,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , u
if (friendnum < 0)
return;
PromptBuf *prt = self->promptbuf;
prep_prompt_win();
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
@ -363,22 +395,29 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , u
wprintw(self->window, "\n");
print_time(self->window);
uint8_t *msg;
if (status == 1) {
msg = "has come online\n";
wattron(self->window, COLOR_PAIR(GREEN));
wattron(self->window, A_BOLD);
wprintw(self->window, "* %s ", nick);
wattroff(self->window, A_BOLD);
wprintw(self->window, "has come online\n");
wprintw(self->window, "%s", msg);
wattroff(self->window, COLOR_PAIR(GREEN));
add_to_log_buf(msg, nick, prt->log, true);
alert_window(self, WINDOW_ALERT_2, false);
} else {
msg = "has gone offline\n";
wattron(self->window, COLOR_PAIR(RED));
wattron(self->window, A_BOLD);
wprintw(self->window, "* %s ", nick);
wattroff(self->window, A_BOLD);
wprintw(self->window, "has gone offline\n");
wprintw(self->window, "%s", msg);
wattroff(self->window, COLOR_PAIR(RED));
add_to_log_buf(msg, nick, prt->log, true);
}
}
@ -386,16 +425,23 @@ static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data,
{
// make sure message data is null-terminated
data[length - 1] = 0;
PromptBuf *prt = self->promptbuf;
prep_prompt_win();
wprintw(self->window, "\n");
print_time(self->window);
wprintw(self->window, "Friend request with the message: '%s'\n", data);
uint8_t msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data);
wprintw(self->window, "%s", msg);
add_to_log_buf(msg, "", prt->log, true);
int n = add_friend_request(key);
if (n == -1) {
wprintw(self->window, "Friend request queue is full. Discarding request.\n");
uint8_t *errmsg = "Friend request queue is full. Discarding request.\n";
wprintw(self->window, "%s", errmsg);
add_to_log_buf(errmsg, "", prt->log, true);
return;
}
@ -441,6 +487,7 @@ ToxWindow new_prompt(void)
memset(&ret, 0, sizeof(ret));
ret.active = true;
ret.is_prompt = true;
ret.onKey = &prompt_onKey;
ret.onDraw = &prompt_onDraw;

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* prompt.h
*
*
* 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/>.
*
*/
#ifndef PROMPT_H_UZYGWFFL
@ -7,7 +25,7 @@
#define X_OFST 2 /* offset to account for prompt char */
#define AC_NUM_GLOB_COMMANDS 14
#define AC_NUM_GLOB_COMMANDS 15
ToxWindow new_prompt(void);
void prep_prompt_win(void);

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* toxic_strings.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/>.
*
*/
#include <stdlib.h>
@ -120,19 +138,21 @@ void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZ
}
/* copies history item at hst_pos to buf. Sets pos and len to the len of the history item.
hst_pos is decremented or incremented depending on key_dir. */
hst_pos is decremented or incremented depending on key_dir.
resets buffer if at end of history */
void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_STR_SIZE],
int hst_tot, int *hst_pos, int key_dir)
{
if (key_dir == LN_HIST_MV_UP) {
if (--(*hst_pos) < 0) {
++(*hst_pos);
*hst_pos = 0;
beep();
}
} else {
if (++(*hst_pos) >= hst_tot) {
--(*hst_pos);
beep();
*hst_pos = hst_tot;
reset_buf(buf, pos, len);
return;
}
}

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* toxic_strings.h
*
*
* 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/>.
*
*/
/* Adds char to buffer at pos */

View File

@ -1,5 +1,23 @@
/*
* Toxic -- Tox Curses Client
/* toxic_windows.h
*
*
* 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/>.
*
*/
#ifndef _windows_h
@ -36,6 +54,8 @@
#define T_KEY_DISCARD 0x15 /* ctrl-u */
#define T_KEY_NEXT 0x10 /* ctrl-p */
#define T_KEY_PREV 0x0F /* ctrl-o */
#define T_KEY_C_E 0x05 /* ctrl-e */
#define T_KEY_C_A 0x01 /* ctrl-a */
/* Curses foreground colours (background is black) */
enum {
@ -91,6 +111,11 @@ struct ToxWindow {
bool active;
int x;
/* window type identifiers */
bool is_chat;
bool is_groupchat;
bool is_prompt;
bool alert0;
bool alert1;
bool alert2;
@ -113,6 +138,16 @@ struct StatusBar {
bool is_online;
};
#define MAX_LOG_BUF_LINES 10 /* write log_buf contents to log file after this many lines */
#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 32 /* extra room for timestamp */
struct chatlog {
uint8_t log_path[MAX_STR_SIZE];
uint8_t log_buf[MAX_LOG_BUF_LINES][MAX_LOG_LINE_SIZE];
int pos;
bool log_on; /* specific to current chat window */
};
#define MAX_LINE_HIST 128
/* chat and groupchat window/buffer holder */
@ -127,6 +162,8 @@ struct ChatContext {
bool self_is_typing;
struct chatlog *log;
WINDOW *history;
WINDOW *linewin;
WINDOW *sidebar;
@ -137,6 +174,7 @@ struct PromptBuf {
wchar_t line[MAX_STR_SIZE];
size_t pos;
size_t len;
bool at_bottom; /* true if line end is at bottom of window */
int orig_y; /* y axis point of line origin */
bool scroll; /* used for prompt window hack to determine when to scroll down */
@ -145,6 +183,7 @@ struct PromptBuf {
int hst_pos;
int hst_tot;
struct chatlog *log;
WINDOW *linewin;
};
@ -196,4 +235,7 @@ int add_window(Tox *m, ToxWindow w);
void del_window(ToxWindow *w);
void set_active_window(int ch);
int num_active_windows(void);
/* closes all chat and groupchat windows (should only be called on shutdown) */
void kill_all_windows(void);
#endif

View File

@ -1,3 +1,25 @@
/* windows.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/>.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@ -8,6 +30,7 @@
#include "friendlist.h"
#include "prompt.h"
#include "toxic_windows.h"
#include "groupchat.h"
extern char *DATA_FILE;
@ -387,3 +410,16 @@ int num_active_windows(void)
return count;
}
/* destroys all chat and groupchat windows (should only be called on shutdown) */
void kill_all_windows(void)
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].is_chat)
kill_chat_window(&windows[i]);
else if (windows[i].is_groupchat)
kill_groupchat_window(&windows[i]);
}
}