AmendHub

Download:

jcs

/

subtext

/

amendments

/

170

chat: Log when users join and leave, implement /who

Also implement a placeholder for /msg since it needs to be done

jcs made amendment 170 about 1 year ago
--- chat.c Fri Jun 17 15:06:58 2022 +++ chat.c Thu Jun 23 10:48:11 2022 @@ -30,6 +30,7 @@ static char chat_tbuf[256]; void chat_help(struct session *s); +void chat_who(struct session *s); void chat_broadcast(struct session *s, char *str) @@ -155,6 +156,9 @@ chat_start(struct session *s, char *with_node) else s->chatting_with_node[0] = '\0'; + session_log(s, "Entered chat with %s", with_node[0] ? with_node : + "everyone"); + session_printf(s, ansi(s, ANSI_CURSOR_LINE_COL, s->terminal_lines, 1, ANSI_END)); @@ -171,8 +175,6 @@ chat_start(struct session *s, char *with_node) session_printf(s, "%s\r\n", session_bar(s, chat_tbuf, NULL)); session_flush(s); - /* TODO: print other users in chat */ - snprintf(chat_tbuf, sizeof(chat_tbuf), "*** %s%s has joined chat", s->user ? s->user->username : "guest", @@ -180,6 +182,8 @@ chat_start(struct session *s, char *with_node) chat_broadcast(s, chat_tbuf); session_flush(s); + chat_who(s); + for (;;) { input = session_field_input(s, CHAT_MAX_INPUT - 1, s->terminal_columns - 1, NULL, false, 0); @@ -197,6 +201,12 @@ chat_start(struct session *s, char *with_node) break; } else if (strcmp(input, "/help") == 0) { chat_help(s); + } else if (strncmp(input, "/msg ", 5) == 0) { + /* TODO */ + chat_printf_line(s, 1, "*** TODO :("); + session_flush(s); + } else if (strcmp(input, "/who") == 0) { + chat_who(s); } else { chat_printf_line(s, 1, "*** Unknown command: %s", input + 1); @@ -212,6 +222,9 @@ chat_start(struct session *s, char *with_node) free(input); } + session_log(s, "Left chat with %s", with_node[0] ? with_node : + "everyone"); + snprintf(chat_tbuf, sizeof(chat_tbuf), "*** %s%s has left chat", s->user ? s->user->username : "guest", @@ -257,5 +270,36 @@ chat_help(struct session *s) chat_printf_line(s, 1, "*** %s/msg user message%s : " "Send \"message\" only to user \"user\"", ansi(s, ANSI_BOLD, ANSI_END), ansi(s, ANSI_RESET, ANSI_END)); + chat_printf_line(s, 1, "*** %s/who%s : " + "List users in this chat", + ansi(s, ANSI_BOLD, ANSI_END), ansi(s, ANSI_RESET, ANSI_END)); session_flush(s); +} + +void +chat_who(struct session *s) +{ + short n, printed = 0; + size_t len = 0; + + for (n = 0; n < nsessions; n++) { + if (!sessions[n]->chatting) + continue; + if (strcmp((char *)s->chatting_with_node, + (char *)sessions[n]->chatting_with_node) != 0) + continue; + + len += snprintf(chat_tbuf + len, sizeof(chat_tbuf), "[ %c%-16s ]", + sessions[n]->user && sessions[n]->user->is_sysop ? '@' : ' ', + sessions[n]->user ? sessions[n]->user->username : "guest"); + + if (++printed == 3) { + chat_printf_line(s, 1, chat_tbuf); + printed = 0; + len = 0; + } + } + + if (printed) + chat_printf_line(s, 1, chat_tbuf); }