AmendHub

Download:

jcs

/

subtext

/

amendments

/

33

session: Use new ansi() interface, add session_bar helper


jcs made amendment 33 over 2 years ago
--- session.c Mon Dec 13 13:04:07 2021 +++ session.c Wed Dec 15 14:36:25 2021 @@ -93,10 +93,14 @@ session_run(struct uthread *uthread, void *arg) "\r\n", db->config.name, s->node); } - 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"); + if (s->autologin_username) { + s->user = user_find(db, s->autologin_username); + if (!s->user) { + session_output(s, "Failed to find autologin user\r\n"); + session_close(&s); + return; + } + } else if (session_login(s) != AUTH_USER_OK) { session_close(&s); return; } @@ -116,7 +120,9 @@ session_run(struct uthread *uthread, void *arg) sessions_tally++; 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), + ansi(s, ANSI_BOLD, NULL), + s->user ? s->user->username : GUEST_USERNAME, + ansi(s, ANSI_RESET, NULL), sessions_tally, ordinal(sessions_tally)); session_output(s, "\r\n[ menu goes here ]\r\n\r\n"); @@ -127,6 +133,7 @@ session_run(struct uthread *uthread, void *arg) c = session_input_char(s); session_output(s, "%c\r\n", c, 2); + /* TODO: make letter->command dynamic from a resource */ switch (c) { case 'g': case 'G': @@ -336,11 +343,12 @@ session_login(struct session *s) continue; } - if (strcmp(username, "guest") == 0) { - /* TODO */ - } else if (strcmp(username, "signup") == 0 || + if (strcmp(username, GUEST_USERNAME) == 0) + return AUTH_USER_OK; + + if (strcmp(username, "signup") == 0 || strcmp(username, "new") == 0) { - /* TODO */ + /* TODO: check for open signups */ } else { user = user_find(db, username); } @@ -371,6 +379,7 @@ session_login(struct session *s) session_output(s, "Login incorrect\r\n"); } + session_output(s, "Thanks for playing\r\n"); return AUTH_USER_FAILED; } @@ -381,6 +390,50 @@ session_login(struct session *s) } \ } +char * +session_bar(struct session *s, char *left_str, char *right_str) +{ + static char bar[128]; + char *strip; + short len, bold, n, llen, rlen, pad; + + len = sprintf(bar, "\r%s", ansi(s, ANSI_REVERSE, NULL)); + + bold = 0; + llen = strlen(left_str); + rlen = (right_str == NULL ? 0 : strlen(right_str)); + for (n = 0; n < llen; n++) { + if (left_str[n] == '\b') { + if (bold) + len = strlcat(bar, ansi(s, ANSI_RESET, ANSI_REVERSE, NULL), + sizeof(bar)); + else + len = strlcat(bar, ansi(s, ANSI_BOLD, NULL), sizeof(bar)); + + bold = !bold; + } else { + bar[len] = left_str[n]; + len++; + } + + if (len >= sizeof(bar)) + panic("session_bar: overflow!"); + } + bar[len] = '\0'; + + /* TODO: right_str */ + + pad = s->terminal_cols - ansi_strip(bar, NULL); + for (; pad > 0 && len < sizeof(bar); pad--) + bar[len++] = ' '; + bar[len] = '\0'; + len = strlcat(bar, ansi(s, ANSI_RESET, NULL), sizeof(bar)); + if (len > sizeof(bar)) + panic("session_bar: overflow!"); + + return bar; +} + size_t session_load_view(struct session *session, short id, char **ret) { @@ -440,7 +493,8 @@ session_pause_return(struct session *s, short enforce, unsigned char c; session_output(s, "%sPress %s<Enter>%s ", - ansi(s, ANSI_RESET), ansi(s, ANSI_BOLD), ansi(s, ANSI_RESET)); + ansi(s, ANSI_RESET, NULL), ansi(s, ANSI_BOLD, NULL), + ansi(s, ANSI_RESET, NULL)); if (msg) session_output_formatted(s, msg, strlen(msg)); else @@ -462,10 +516,10 @@ session_who(struct session *s) short n; session_output(s, "%sWho's Online%s\r\n", - ansi(s, ANSI_BOLD), ansi(s, ANSI_RESET)); + ansi(s, ANSI_BOLD, NULL), ansi(s, ANSI_RESET, NULL)); session_output(s, "%sNode User Via Speed Idle%s\r\n", - ansi(s, ANSI_BOLD), ansi(s, ANSI_RESET)); + ansi(s, ANSI_BOLD, NULL), ansi(s, ANSI_RESET, NULL)); for (n = 0; n < nsessions; n++) { idle = Time - sessions[n]->last_input_at; @@ -488,4 +542,4 @@ session_who(struct session *s) session_output_formatted(s, "\r\n", 2); session_pause_return(s, 0, NULL); -} +} --- session.h Mon Dec 13 12:40:34 2021 +++ session.h Wed Dec 15 14:33:53 2021 @@ -33,6 +33,8 @@ enum session_input_state { SESSION_INPUT_CHAR }; +#define GUEST_USERNAME "guest" + struct node_funcs { void (*setup)(struct session *session); short (*input)(struct session *session); @@ -67,6 +69,7 @@ struct session { unsigned char color; unsigned char cp437; struct session_log log; + char autologin_username[32]; struct user *user; void *cookie; struct node_funcs *node_funcs; @@ -86,6 +89,7 @@ size_t session_output_formatted(struct session *sessio size_t len); char *session_field_input(struct session *session, unsigned short len, char mask); +char *session_bar(struct session *s, char *left_str, char *right_str); 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);