Skip to content

Commit 7f38c3c

Browse files
committed
add prompt logging support
1 parent 7109b8f commit 7f38c3c

7 files changed

Lines changed: 67 additions & 26 deletions

File tree

src/chat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ static void chat_onInit(ToxWindow *self, Tox *m)
681681
memset(ctx->log, 0, sizeof(struct chatlog));
682682

683683
if (friends[self->num].logging_on)
684-
log_enable(ctx->log, self->name, friends[self->num].pub_key);
684+
log_enable(self->name, friends[self->num].pub_key, ctx->log);
685685

686686
wprintw(ctx->history, "\n\n");
687687
execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE);

src/global_commands.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,22 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg
222222

223223
void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
224224
{
225-
if (!self->is_chat && !self->is_groupchat) { /* remove if prompt logging gets implemented */
226-
wprintw(window, "Invalid command.\n");
227-
return;
228-
}
225+
if (argc == 0) {
226+
bool on;
229227

230-
ChatContext *ctx = self->chatwin;
228+
if (self->is_chat || self->is_groupchat)
229+
on = self->chatwin->log->log_on;
230+
else if (self->is_prompt)
231+
on = self->promptbuf->log->log_on;
231232

232-
if (argc == 0) {
233-
if (ctx->log->log_on) {
234-
wprintw(window, "Logging for this chat is ");
233+
if (on) {
234+
wprintw(window, "Logging for this window is ");
235235
wattron(window, COLOR_PAIR(GREEN) | A_BOLD);
236236
wprintw(window, "[on]");
237237
wattroff(window, COLOR_PAIR(GREEN) | A_BOLD);
238238
wprintw(window, ". Type \"/log off\" to disable.\n");
239239
} else {
240-
wprintw(window, "Logging for this chat is ");
240+
wprintw(window, "Logging for this window is ");
241241
wattron(window, COLOR_PAIR(RED) | A_BOLD);
242242
wprintw(window, "[off]");
243243
wattroff(window, COLOR_PAIR(RED) | A_BOLD);
@@ -248,26 +248,34 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
248248
}
249249

250250
uint8_t *swch = argv[1];
251-
uint8_t *ident = NULL;
252251

253252
if (!strcmp(swch, "1") || !strcmp(swch, "on")) {
253+
254254
if (self->is_chat) {
255255
friends[self->num].logging_on = true;
256-
ident = friends[self->num].pub_key;
256+
log_enable(self->name, friends[self->num].pub_key, self->chatwin->log);
257+
} else if (self->is_prompt) {
258+
uint8_t myid[TOX_FRIEND_ADDRESS_SIZE];
259+
tox_get_address(m, myid);
260+
log_enable(self->name, &myid, self->promptbuf->log);
261+
} else if (self->is_groupchat) {
262+
log_enable(self->name, NULL, self->chatwin->log);
257263
}
258264

259-
log_enable(ctx->log, self->name, ident);
260-
261265
wprintw(window, "Logging ");
262266
wattron(window, COLOR_PAIR(GREEN) | A_BOLD);
263267
wprintw(window, "[on]\n");
264268
wattroff(window, COLOR_PAIR(GREEN) | A_BOLD);
265269
return;
266270
} else if (!strcmp(swch, "0") || !strcmp(swch, "off")) {
267-
if (self->is_chat)
271+
if (self->is_chat) {
268272
friends[self->num].logging_on = false;
269-
270-
log_disable(ctx->log);
273+
log_disable(self->chatwin->log);
274+
} else if (self->is_prompt) {
275+
log_disable(self->promptbuf->log);
276+
} else if (self->is_groupchat) {
277+
log_disable(self->chatwin->log);
278+
}
271279

272280
wprintw(window, "Logging ");
273281
wattron(window, COLOR_PAIR(RED) | A_BOLD);
@@ -363,6 +371,7 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a
363371
wprintw(window, " /status <type> <msg> : Set status with optional note\n");
364372
wprintw(window, " /note <msg> : Set a personal note\n");
365373
wprintw(window, " /nick <nick> : Set your nickname\n");
374+
wprintw(window, " /log <on> or <off> : Enable/disable logging\n");
366375
wprintw(window, " /groupchat : Create a group chat\n");
367376
wprintw(window, " /myid : Print your ID\n");
368377
wprintw(window, " /help : Print this message again\n");

src/log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* log->c
1+
/* log.c
22
*
33
*
44
* Copyright (C) 2014 Toxic All Rights Reserved.
@@ -121,7 +121,7 @@ void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event
121121
write_to_log(log);
122122
}
123123

124-
void log_enable(struct chatlog *log, uint8_t *name, uint8_t *key)
124+
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log)
125125
{
126126
log->log_on = true;
127127

src/log.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event
3131
This is triggered automatically when the log buffer is full, but may be forced. */
3232
void write_to_log(struct chatlog *log);
3333

34-
void log_enable(struct chatlog *log, uint8_t *name, uint8_t *key);
35-
void log_disable(struct chatlog *log);
34+
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log);
35+
void log_disable(struct chatlog *log);

src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ void exit_toxic(Tox *m)
479479
free(DATA_FILE);
480480
free(SRVLIST_FILE);
481481
free(prompt->stb);
482+
log_disable(prompt->promptbuf->log);
483+
free(prompt->promptbuf->log);
482484
free(prompt->promptbuf);
483485
tox_kill(m);
484486
endwin();

src/prompt.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,18 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
360360
static void prompt_onInit(ToxWindow *self, Tox *m)
361361
{
362362
scrollok(self->window, true);
363+
PromptBuf *prt = self->promptbuf;
364+
365+
prt->log = malloc(sizeof(struct chatlog));
366+
367+
if (prt->log == NULL) {
368+
endwin();
369+
fprintf(stderr, "malloc() failed. Aborting...\n");
370+
exit(EXIT_FAILURE);
371+
}
372+
373+
memset(prt->log, 0, sizeof(struct chatlog));
374+
363375
execute(self->window, self, m, "/help", GLOBAL_COMMAND_MODE);
364376
wclrtoeol(self->window);
365377
}
@@ -369,6 +381,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , u
369381
if (friendnum < 0)
370382
return;
371383

384+
PromptBuf *prt = self->promptbuf;
372385
prep_prompt_win();
373386

374387
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
@@ -382,39 +395,53 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , u
382395
wprintw(self->window, "\n");
383396
print_time(self->window);
384397

398+
uint8_t *msg;
399+
385400
if (status == 1) {
401+
msg = "has come online\n";
386402
wattron(self->window, COLOR_PAIR(GREEN));
387403
wattron(self->window, A_BOLD);
388404
wprintw(self->window, "* %s ", nick);
389405
wattroff(self->window, A_BOLD);
390-
wprintw(self->window, "has come online\n");
406+
wprintw(self->window, "%s", msg);
391407
wattroff(self->window, COLOR_PAIR(GREEN));
392408

409+
add_to_log_buf(msg, nick, prt->log, true);
393410
alert_window(self, WINDOW_ALERT_2, false);
394411
} else {
412+
msg = "has gone offline\n";
395413
wattron(self->window, COLOR_PAIR(RED));
396414
wattron(self->window, A_BOLD);
397415
wprintw(self->window, "* %s ", nick);
398416
wattroff(self->window, A_BOLD);
399-
wprintw(self->window, "has gone offline\n");
417+
wprintw(self->window, "%s", msg);
400418
wattroff(self->window, COLOR_PAIR(RED));
419+
420+
add_to_log_buf(msg, nick, prt->log, true);
401421
}
402422
}
403423

404424
static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16_t length)
405425
{
406426
// make sure message data is null-terminated
407427
data[length - 1] = 0;
408-
428+
PromptBuf *prt = self->promptbuf;
409429
prep_prompt_win();
430+
410431
wprintw(self->window, "\n");
411432
print_time(self->window);
412-
wprintw(self->window, "Friend request with the message: '%s'\n", data);
433+
434+
uint8_t msg[MAX_STR_SIZE];
435+
snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data);
436+
wprintw(self->window, "%s", msg);
437+
add_to_log_buf(msg, "", prt->log, true);
413438

414439
int n = add_friend_request(key);
415440

416441
if (n == -1) {
417-
wprintw(self->window, "Friend request queue is full. Discarding request.\n");
442+
uint8_t *errmsg = "Friend request queue is full. Discarding request.\n";
443+
wprintw(self->window, "%s", errmsg);
444+
add_to_log_buf(errmsg, "", prt->log, true);
418445
return;
419446
}
420447

@@ -460,6 +487,7 @@ ToxWindow new_prompt(void)
460487
memset(&ret, 0, sizeof(ret));
461488

462489
ret.active = true;
490+
ret.is_prompt = true;
463491

464492
ret.onKey = &prompt_onKey;
465493
ret.onDraw = &prompt_onDraw;

src/toxic_windows.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct ToxWindow {
114114
/* window type identifiers */
115115
bool is_chat;
116116
bool is_groupchat;
117+
bool is_prompt;
117118

118119
bool alert0;
119120
bool alert1;
@@ -182,6 +183,7 @@ struct PromptBuf {
182183
int hst_pos;
183184
int hst_tot;
184185

186+
struct chatlog *log;
185187
WINDOW *linewin;
186188
};
187189

0 commit comments

Comments
 (0)