jcs
/subtext
/amendments
/28
session: Add a "who's online" command
jcs made amendment 28 over 2 years ago
--- console.c Sun Dec 12 13:14:48 2021
+++ console.c Mon Dec 13 09:14:36 2021
@@ -80,10 +80,12 @@ console_init(struct console **cur_console)
ShowWindow(console->win);
- console->session = session_create("console", &console_node_funcs);
+ console->session = session_create("console", "console",
+ &console_node_funcs);
console->session->cookie = (void *)console;
console->session->ansi = 1;
console->session->cp437 = 1;
+ console->session->tspeed = 19200;
*cur_console = console;
@@ -236,6 +238,7 @@ console_key_down(struct console *console, EventRecord
if (console->session->ibuflen >= nitems(console->session->ibuf))
return;
+ console->session->last_input_at = Time;
k = (event->message & charCodeMask);
console->session->ibuf[console->session->ibuflen++] = k;
if (k == '\r')
--- session.c Sun Dec 12 13:10:28 2021
+++ session.c Mon Dec 13 13:04:07 2021
@@ -39,7 +39,7 @@ void session_run(struct uthread *uthread, void *arg);
short session_login(struct session *s);
struct session *
-session_create(char *node, struct node_funcs *node_funcs)
+session_create(char *node, char *via, struct node_funcs *node_funcs)
{
struct session *session;
@@ -54,11 +54,13 @@ session_create(char *node, struct node_funcs *node_fun
session->node_funcs = node_funcs;
strlcpy(session->node, node, sizeof(session->node));
strlcpy(session->log.node, node, sizeof(session->log.node));
-
+ strlcpy(session->via, via, sizeof(session->via));
+ session->last_input_at = Time;
+
nsessions++;
sessions = xreallocarray(sessions, nsessions, sizeof(struct session));
sessions[nsessions - 1] = session;
-
+
return session;
}
@@ -91,7 +93,9 @@ session_run(struct uthread *uthread, void *arg)
"\r\n", db->config.name, s->node);
}
- if (session_login(s) != AUTH_USER_OK) {
+ if (strcmp(s->node, "console") == 0)
+ s->user = user_find(db, "sysop");
+ else if (session_login(s) != AUTH_USER_OK) {
session_output(s, "Thanks for playing\r\n");
session_close(&s);
return;
@@ -99,8 +103,7 @@ session_run(struct uthread *uthread, void *arg)
/* update session log */
if (s->user)
- strlcpy(s->log.username,
- s->user->username,
+ strlcpy(s->log.username, s->user->username,
sizeof(s->log.username));
s->log.logged_on_at = Time;
@@ -122,7 +125,7 @@ session_run(struct uthread *uthread, void *arg)
session_output(s, "Main Menu> ");
c = session_input_char(s);
- session_output_formatted(s, "\r\n", 2);
+ session_output(s, "%c\r\n", c, 2);
switch (c) {
case 'g':
@@ -131,6 +134,12 @@ session_run(struct uthread *uthread, void *arg)
session_output(s, "Goodbye!\r\n");
done = 1;
break;
+ case 'w':
+ /* who's online */
+ session_who(s);
+ break;
+ case '\r':
+ break;
default:
session_output(s, "Invalid option\r\n");
break;
@@ -423,4 +432,60 @@ session_load_view(struct session *session, short id, c
}
return retpos;
-}
+}
+
+void
+session_pause_return(struct session *s, short enforce, char *msg)
+{
+ unsigned char c;
+
+ session_output(s, "%sPress %s<Enter>%s ",
+ ansi(s, ANSI_RESET), ansi(s, ANSI_BOLD), ansi(s, ANSI_RESET));
+ if (msg)
+ session_output_formatted(s, msg, strlen(msg));
+ else
+ session_output(s, "to return to the main menu...");
+
+ for (;;) {
+ c = session_input_char(s);
+ if (!enforce || c == '\r')
+ break;
+ }
+ session_output_formatted(s, "\r\n", 2);
+}
+
+void
+session_who(struct session *s)
+{
+ char idle_s[20];
+ unsigned long idle;
+ short n;
+
+ session_output(s, "%sWho's Online%s\r\n",
+ ansi(s, ANSI_BOLD), ansi(s, ANSI_RESET));
+
+ session_output(s, "%sNode User Via Speed Idle%s\r\n",
+ ansi(s, ANSI_BOLD), ansi(s, ANSI_RESET));
+
+ for (n = 0; n < nsessions; n++) {
+ idle = Time - sessions[n]->last_input_at;
+ if (idle < 60)
+ sprintf(idle_s, "%lds", idle);
+ else if (idle < (60 * 60))
+ sprintf(idle_s, "%ldm", idle / 60);
+ else if (idle < (60 * 60 * 24))
+ sprintf(idle_s, "%ldh", idle / (60 * 60));
+ else
+ sprintf(idle_s, "%ldd", idle / (60 * 60 * 24));
+
+ session_output(s, "%-7s %-20s %-7s %-6d %-6s\r\n",
+ sessions[n]->node,
+ sessions[n]->user ? sessions[n]->user->username : "guest",
+ sessions[n]->via,
+ sessions[n]->tspeed,
+ idle_s);
+ }
+
+ session_output_formatted(s, "\r\n", 2);
+ session_pause_return(s, 0, NULL);
+}
--- session.h Sat Dec 11 11:01:29 2021
+++ session.h Mon Dec 13 12:40:34 2021
@@ -50,6 +50,7 @@ struct session_log {
struct session {
char node[10];
+ char via[10];
unsigned char obuf[256]; /* telnet.obuf must be double this */
unsigned char ibuf[64];
short obuflen;
@@ -57,9 +58,11 @@ struct session {
enum session_state state;
enum session_input_state input_state;
unsigned char last_input;
+ unsigned long last_input_at;
unsigned short terminal_cols;
unsigned short terminal_rows;
char terminal_type[20];
+ unsigned short tspeed;
unsigned char ansi;
unsigned char color;
unsigned char cp437;
@@ -73,7 +76,8 @@ struct session {
extern struct session **sessions;
extern short nsessions;
-struct session *session_create(char *node, struct node_funcs *node_funcs);
+struct session *session_create(char *node, char *via,
+ struct node_funcs *node_funcs);
void session_close(struct session **session);
void session_idle(struct session *session);
char session_input_char(struct session *session);
@@ -83,5 +87,7 @@ size_t session_output_formatted(struct session *sessio
char *session_field_input(struct session *session,
unsigned short len, char mask);
size_t session_load_view(struct session *session, short id, char **ret);
+void session_pause_return(struct session *s, short enforce, char *msg);
+void session_who(struct session *s);
#endif /* __SESSION_H__ */
--- telnet.c Sat Dec 11 10:51:45 2021
+++ telnet.c Mon Dec 13 09:13:45 2021
@@ -213,7 +213,8 @@ telnet_idle(void)
char tty[7];
sprintf(tty, "ttyt%d", n);
node->state = TELNET_PB_STATE_CONNECTED;
- node->session = session_create(tty, &telnet_node_funcs);
+ node->session = session_create(tty, "telnet",
+ &telnet_node_funcs);
node->session->cookie = (void *)node;
node->session->log.ip_address = telnet_status_pb.remoteHost;
@@ -284,6 +285,8 @@ telnet_input(struct session *session)
/* TODO: make this not fatal */
panic("TCPRecv[%d] failed: %d", node->id, error);
+ session->last_input_at = Time;
+
for (n = 0; n < rlen; n++) {
c = node->ibuf[n];