1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-06-26 20:37:46 +02:00

add command to turn logs on/off

This commit is contained in:
Jfreegman 2014-02-26 19:00:13 -05:00
parent 817f763589
commit 9b57c05648
11 changed files with 106 additions and 11 deletions

View File

@ -33,6 +33,7 @@
#include "misc_tools.h"
#include "friendlist.h"
#include "toxic_strings.h"
#include "chat.h"
extern char *DATA_FILE;
extern int store_data(Tox *m, char *path);
@ -40,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] = {
@ -54,6 +55,7 @@ static const uint8_t chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/help" },
{ "/invite" },
{ "/join" },
{ "/log" },
{ "/myid" },
{ "/nick" },
{ "/note" },
@ -77,7 +79,7 @@ void kill_chat_window(ToxWindow *self)
ChatContext *ctx = self->chatwin;
StatusBar *statusbar = self->stb;
write_to_log(ctx);
chat_disable_log(self);
int f_num = self->num;
delwin(ctx->linewin);
@ -89,6 +91,29 @@ void kill_chat_window(ToxWindow *self)
free(statusbar);
}
void chat_enable_log(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
if (ctx->log.log_on)
return;
ctx->log.log_on = true;
if (!ctx->log.log_path[0])
init_logging_session(self->name, friends[self->num].pub_key, ctx);
}
void chat_disable_log(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
if (ctx->log.log_on) {
write_to_log(ctx);
ctx->log.log_on = false;
}
}
static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len)
{
if (self->num != num)
@ -668,10 +693,8 @@ static void chat_onInit(ToxWindow *self, Tox *m)
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2-CHATBOX_HEIGHT, 0);
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);
ctx->log.log_on = true;
init_logging_session(self->name, friends[self->num].pub_key, ctx);
}
ToxWindow new_chat(Tox *m, int friendnum)

View File

@ -25,6 +25,8 @@
#include "toxic_windows.h"
void chat_enable_log(ToxWindow *self);
void chat_disable_log(ToxWindow *self);
void kill_chat_window(ToxWindow *self);
ToxWindow new_chat(Tox *m, int friendnum);

View File

@ -41,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

@ -21,7 +21,7 @@
*/
#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

@ -220,6 +220,48 @@ 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 (!self->is_chat && !self->is_groupchat) /* remove if prompt logging gets implemented */
return;
ChatContext *ctx = self->chatwin;
if (argc == 0) {
uint8_t *s = ctx->log.log_on ? "enabled" : "disabled";
wprintw(window, "Logging for this chat is currently %s\n", s);
return;
}
uint8_t *swch = argv[1];
if (!strcmp(swch, "1") || !strcmp(swch, "on")) {
if (self->is_chat)
chat_enable_log(self);
else if (self->is_groupchat)
groupchat_enable_log(self);
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)
chat_disable_log(self);
else if (self->is_groupchat)
groupchat_disable_log(self);
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 1 or 0 to enable or disable 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};

View File

@ -25,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

@ -74,8 +74,8 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum)
void kill_groupchat_window(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
write_to_log(ctx);
groupchat_disable_log(self);
delwin(ctx->linewin);
del_window(self);
free(ctx);
@ -102,6 +102,29 @@ static void close_groupchat(ToxWindow *self, Tox *m, int groupnum)
kill_groupchat_window(self);
}
void groupchat_enable_log(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
if (ctx->log.log_on)
return;
ctx->log.log_on = true;
if (!ctx->log.log_path[0])
init_logging_session(self->name, NULL, ctx);
}
void groupchat_disable_log(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
if (ctx->log.log_on) {
write_to_log(ctx);
ctx->log.log_on = false;
}
}
static void print_groupchat_help(ChatContext *ctx)
{
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
@ -599,10 +622,9 @@ static void groupchat_onInit(ToxWindow *self, Tox *m)
ctx->sidebar = subwin(self->window, y-CHATBOX_HEIGHT+1, SIDEBAR_WIDTH, 0, x-SIDEBAR_WIDTH);
print_groupchat_help(ctx);
wmove(self->window, y-CURS_Y_OFFSET, 0);
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);
ctx->log.log_on = true;
init_logging_session(self->name, NULL, ctx);
wmove(self->window, y-CURS_Y_OFFSET, 0);
}
ToxWindow new_group_chat(Tox *m, int groupnum)

View File

@ -32,6 +32,8 @@ typedef struct {
uint8_t *oldpeer_names;
} GroupChat;
void groupchat_enable_log(ToxWindow *self);
void groupchat_disable_log(ToxWindow *self);
void kill_groupchat_window(ToxWindow *self);
int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum);
ToxWindow new_group_chat(Tox *m, int groupnum);

View File

@ -48,6 +48,7 @@ const uint8_t glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/groupchat" },
{ "/help" },
{ "/join" },
{ "/log" },
{ "/myid" },
{ "/nick" },
{ "/note" },

View File

@ -25,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

@ -171,6 +171,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 */