AmendHub

Download:

jcs

/

subtext

/

amendments

/

83

session: Persist session logs to a separate bile db, add recent command

This file will get a lot of activity, so put it in a separate file for
easy rotation and to avoid corruption of the important user database

jcs made amendment 83 over 2 years ago
--- db.c Sat Jan 29 18:43:39 2022 +++ db.c Tue Mar 1 13:32:17 2022 @@ -146,15 +146,32 @@ db_init(Str255 path, short vrefnum, struct bile *bile) if (!was_new) db_config_load(tdb); - + + PtoCstr(fullpath); + strlcat((char *)&fullpath, "-sessions", sizeof(fullpath)); + CtoPstr(fullpath); + + tdb->sessions_bile = bile_open(fullpath, vrefnum); + if (tdb->sessions_bile == NULL) { + tdb->sessions_bile = bile_create(fullpath, vrefnum, + SUBTEXT_CREATOR, SL_TYPE); + if (tdb->sessions_bile == NULL) + panic("Couldn't create %s: %d", PtoCstr(fullpath), + bile_error(NULL)); + } + return tdb; } void db_close(struct db *tdb) { - bile_close(db->bile); - free(db->bile); + bile_close(tdb->bile); + free(tdb->bile); + if (tdb->sessions_bile != NULL) { + bile_close(tdb->sessions_bile); + free(tdb->sessions_bile); + } free(tdb); } --- db.h Fri Jan 28 14:50:34 2022 +++ db.h Tue Mar 1 13:20:42 2022 @@ -46,6 +46,9 @@ #define DB_USERNAME_LENGTH 16 +#define SL_TYPE 'STSL' +#define SL_LOG_RTYPE 'SLOG' + struct config { char name[32]; char phone_number[32]; @@ -78,6 +81,7 @@ struct db { struct config config; struct user_map *user_map; short nusers; + struct bile *sessions_bile; }; struct db * db_open(Str255 file, short vrefnum); --- session.c Mon Feb 21 15:20:58 2022 +++ session.c Tue Mar 1 14:27:04 2022 @@ -100,8 +100,19 @@ session_run(struct uthread *uthread, void *arg) if (s->user) strlcpy(s->log.username, s->user->username, sizeof(s->log.username)); + else + strlcpy(s->log.username, GUEST_USERNAME, sizeof(s->log.username)); + + strlcpy(s->log.via, s->via, sizeof(s->log.via)); + s->log.tspeed = s->tspeed; s->log.logged_on_at = Time; + s->log.id = bile_next_id(db->sessions_bile, SL_LOG_RTYPE); + if (bile_write(db->sessions_bile, SL_LOG_RTYPE, s->log.id, &s->log, + sizeof(s->log)) != sizeof(s->log)) + panic("bile_write of session log failed: %d", + bile_error(db->sessions_bile)); + date_tm = localtime((time_t *)&Time); strftime(date, sizeof(date), "%Y%m%d", date_tm); if (strcmp(date, sessions_tally_day) != 0) { @@ -148,6 +159,10 @@ get_another_char: session_flush(s); done = true; break; + case 'l': + case 'L': + session_recents(s); + break; case 'm': case 'M': mail_menu(s); @@ -191,6 +206,14 @@ session_close(struct session *session) session->ending = 1; } + /* finalize session log */ + session->log.logged_off_at = Time; + + if (bile_write(db->sessions_bile, SL_LOG_RTYPE, session->log.id, + &session->log, sizeof(session->log)) != sizeof(session->log)) + panic("bile_write of session log failed: %d", + bile_error(db->sessions_bile)); + /* close the node */ session->node_funcs->close(session); @@ -975,6 +998,55 @@ session_pause_return(struct session *s, short enforce, } session_output(s, "\r\n", 2); session_flush(s); +} + +void +session_recents(struct session *s) +{ + struct bile_object *slog_obj; + struct session_log slog; + struct tm *date_tm; + unsigned long idle; + size_t scount, rsize; + char sdate[12]; + short printed; + + session_output_template(s, "{{B}}Recent Logins{{/}}\r\n"); + session_output_template(s, + "{{B}}Date Node User Via Speed{{/}}\r\n"); + session_flush(s); + + scount = bile_count_by_type(db->sessions_bile, SL_LOG_RTYPE); + for (printed = 0; printed < 10; printed++) { + slog_obj = bile_get_nth_of_type(db->sessions_bile, + scount - 1 - printed, SL_LOG_RTYPE); + if (slog_obj == NULL) + break; + + rsize = bile_read(db->sessions_bile, SL_LOG_RTYPE, slog_obj->id, + (char *)&slog, sizeof(slog)); + if (rsize != sizeof(slog)) + panic("unexpected bile_read response %d (read %ld, " + "expected %ld)", bile_error(db->sessions_bile), rsize, + sizeof(slog)); + + date_tm = localtime((time_t *)&slog.logged_on_at); + strftime(sdate, sizeof(sdate), "%m/%d", date_tm); + + session_printf(s, "%-7s %-7s %-20s %-7s %-6d\r\n", + sdate, + slog.node, + slog.username, + slog.via, + slog.tspeed); + session_flush(s); + + free(slog_obj); + } + + session_output(s, "\r\n", 2); + session_flush(s); + session_pause_return(s, 0, NULL); } void --- session.h Mon Feb 21 13:50:47 2022 +++ session.h Tue Mar 1 14:06:19 2022 @@ -53,14 +53,16 @@ struct node_funcs { }; struct session_log { + unsigned long id; char username[DB_USERNAME_LENGTH + 1]; char node[10]; + char via[10]; unsigned long logged_on_at; unsigned long logged_off_at; unsigned long ip_address; + unsigned short tspeed; }; - struct session { short ending; char node[10]; @@ -114,6 +116,7 @@ char * session_multiline_input(struct session *session char *initial_body); char * session_bar(struct session *s, char *left_str, char *right_str); void session_pause_return(struct session *s, short enforce, char *msg); +void session_recents(struct session *s); void session_who(struct session *s); #endif /* __SESSION_H__ */