AmendHub

Download:

jcs

/

subtext

/

amendments

/

27

ansi: Move ANSI output stuff to dedicated files

Start tallying sessions to be stored in the future

jcs made amendment 27 over 2 years ago
--- ansi.c Sat Dec 11 10:34:13 2021 +++ ansi.c Sat Dec 11 10:34:13 2021 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 joshua stein <jcs@jcs.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "ansi.h" +#include "session.h" + +char * +ansi(struct session *s, unsigned char attr) +{ + switch (attr) { + case ANSI_RESET: + if (s->ansi) + return "\33[0m"; + break; + case ANSI_BOLD: + if (s->ansi) + return "\33[1m"; + break; + } + + return ""; +} --- ansi.h Sat Dec 11 10:37:36 2021 +++ ansi.h Sat Dec 11 10:37:36 2021 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 joshua stein <jcs@jcs.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef __ANSI_H__ +#define __ANSI_H + +#define ANSI_RESET 0 +#define ANSI_BOLD 1 + +char *ansi(struct session *s, unsigned char attr); + +#endif /* __ANSI_H__ */ --- console.c Fri Dec 10 16:44:42 2021 +++ console.c Sun Dec 12 13:14:48 2021 @@ -110,7 +110,7 @@ console_idle(struct console *console) short n, len, cursor, iac = 0; short hints[100]; short nhints = 0; - + if (session->obuflen == 0) return; --- main.c Fri Dec 10 15:58:05 2021 +++ main.c Sun Dec 12 13:07:43 2021 @@ -85,16 +85,15 @@ main(void) if (!db) ExitToShell(); - - console_init(&cur_console); + if (db->config.telnet_port) telnet_init(); - + iters = 0; while (!quitting) { if (db->config.telnet_port) telnet_idle(); - + uthread_coordinate(); if (++iters % 10 == 0) { @@ -105,6 +104,8 @@ main(void) switch (event.what) { case nullEvent: + if (cur_console == NULL) + console_init(&cur_console); break; case keyDown: case autoKey: --- session.c Fri Dec 10 16:44:14 2021 +++ session.c Sun Dec 12 13:10:28 2021 @@ -19,17 +19,21 @@ #include <stdlib.h> #include <string.h> +#include "ansi.h" #include "subtext.h" #include "session.h" #include "user.h" #include "uthread.h" #include "util.h" +#include "console.h" + struct session **sessions = NULL; short nsessions = 0; char session_tbuf[512]; -unsigned short sessions_today = 0; +unsigned short sessions_tally = 0; +char sessions_tally_day[9] = { 0 }; void session_run(struct uthread *uthread, void *arg); short session_login(struct session *s); @@ -49,11 +53,12 @@ session_create(char *node, struct node_funcs *node_fun session->state = SESSION_STATE_INIT; session->node_funcs = node_funcs; strlcpy(session->node, node, sizeof(session->node)); + strlcpy(session->log.node, node, sizeof(session->log.node)); nsessions++; sessions = xreallocarray(sessions, nsessions, sizeof(struct session)); sessions[nsessions - 1] = session; - + return session; } @@ -62,6 +67,8 @@ session_run(struct uthread *uthread, void *arg) { struct session *s = (struct session *)arg; Handle h; + char date[9]; + struct tm *date_tm; char *view, done = 0; unsigned char c; size_t len; @@ -83,18 +90,31 @@ session_run(struct uthread *uthread, void *arg) "Welcome to %s (%s)\r\n" "\r\n", db->config.name, s->node); } - + if (session_login(s) != AUTH_USER_OK) { session_output(s, "Thanks for playing\r\n"); session_close(&s); + return; } + + /* update session log */ + if (s->user) + strlcpy(s->log.username, + s->user->username, + sizeof(s->log.username)); + s->log.logged_on_at = Time; - /* TODO: persist this per-day */ - sessions_today++; + date_tm = localtime((time_t *)&Time); + strftime(date, sizeof(date), "%Y%m%d", date_tm); + if (strcmp(date, sessions_tally_day) != 0) { + strlcpy(sessions_tally_day, date, sizeof(sessions_tally_day)); + sessions_tally = 0; + } + sessions_tally++; - session_output(s, "Welcome, %s%s%s, you are the %s caller today.\r\n", - session_ansi(s, ANSI_BOLD), s->user->username, - session_ansi(s, ANSI_RESET), ordinal(sessions_today)); + session_output(s, "Welcome, %s%s%s, you are the %d%s caller today.\r\n", + ansi(s, ANSI_BOLD), s->user->username, ansi(s, ANSI_RESET), + sessions_tally, ordinal(sessions_tally)); session_output(s, "\r\n[ menu goes here ]\r\n\r\n"); @@ -175,7 +195,7 @@ session_output_formatted(struct session *session, cons size_t len) { size_t chunk, olen = len, stroff = 0; - + while (len) { chunk = (sizeof(session->obuf) - session->obuflen); if (chunk == 0) { @@ -403,21 +423,4 @@ session_load_view(struct session *session, short id, c } return retpos; -} - -char * -session_ansi(struct session *s, unsigned char attr) -{ - switch (attr) { - case ANSI_RESET: - if (s->ansi) - return "\33[0m"; - break; - case ANSI_BOLD: - if (s->ansi) - return "\33[1m"; - break; - } - - return ""; -} +} --- session.h Fri Dec 10 16:34:11 2021 +++ session.h Sat Dec 11 11:01:29 2021 @@ -17,6 +17,8 @@ #ifndef __SESSION_H__ #define __SESSION_H__ +#include <stdlib.h> + #include "uthread.h" enum session_state { @@ -38,33 +40,39 @@ struct node_funcs { void (*close)(struct session *session); }; +struct session_log { + char username[32]; + char node[10]; + unsigned long logged_on_at; + unsigned long logged_off_at; + unsigned long ip_address; +}; + struct session { char node[10]; unsigned char obuf[256]; /* telnet.obuf must be double this */ unsigned char ibuf[64]; - enum session_state state; - enum session_input_state input_state; short obuflen; short ibuflen; + enum session_state state; + enum session_input_state input_state; unsigned char last_input; unsigned short terminal_cols; unsigned short terminal_rows; char terminal_type[20]; - void *cookie; unsigned char ansi; unsigned char color; unsigned char cp437; + struct session_log log; + struct user *user; + void *cookie; struct node_funcs *node_funcs; struct uthread *uthread; - struct user *user; }; extern struct session **sessions; extern short nsessions; -#define ANSI_RESET 0 -#define ANSI_BOLD 1 - struct session *session_create(char *node, struct node_funcs *node_funcs); void session_close(struct session **session); void session_idle(struct session *session); @@ -75,6 +83,5 @@ 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); -char *session_ansi(struct session *s, unsigned char attr); #endif /* __SESSION_H__ */ --- telnet.c Fri Dec 10 13:53:54 2021 +++ telnet.c Sat Dec 11 10:51:45 2021 @@ -215,6 +215,7 @@ telnet_idle(void) node->state = TELNET_PB_STATE_CONNECTED; node->session = session_create(tty, &telnet_node_funcs); node->session->cookie = (void *)node; + node->session->log.ip_address = telnet_status_pb.remoteHost; /* start up a new socket for listening */ telnet_listener_node = NULL;