AmendHub

Download:

jcs

/

subtext

/

amendments

/

552

session: Add "logged_in_time" template variable


jcs made amendment 552 about 1 year ago
--- GUIDE Thu Nov 9 21:46:01 2023 +++ GUIDE Fri Nov 17 13:41:01 2023 @@ -83,6 +83,9 @@ Supported variables: - {{#}} Stops varible expansion for the rest of the template. Useful when printing untrusted data (mostly used internally). + - {{logged_in_time}} + The amount of time the user has been logged in (still usable in + signoff template), such as "7m20s". - {{new_mail}} If the user has any new, unread mail messages, the count such as "(4 New)". Otherwise it is blank. --- session.c Fri Nov 17 11:32:32 2023 +++ session.c Fri Nov 17 12:27:44 2023 @@ -1448,6 +1448,7 @@ session_expand_var(struct session *session, char *ivar { static char var[128], retval[128]; size_t retsize, retlen, varlen, unread_count; + long elapsed, telapsed; struct tm *now; short count; bool pad = false; @@ -1482,6 +1483,33 @@ session_expand_var(struct session *session, char *ivar } else if (strcmp(var, "#") == 0) { *end_expansion = true; retlen = 0; + } else if (strcmp(var, "logged_in_time") == 0) { + memset(retval, 0, retsize); + retlen = 0; + elapsed = Time - session->log.logged_on_at; + while (elapsed > 0) { + if (elapsed < 60) { + telapsed = elapsed; + retlen += snprintf(retval + retlen, + retsize - retlen, "%lus", telapsed); + } else if (elapsed < (60 * 60)) { + telapsed = elapsed / 60; + retlen += snprintf(retval + retlen, + retsize - retlen, "%lum", telapsed); + telapsed *= 60; + } else if (elapsed < (60 * 60 * 24)) { + telapsed = elapsed / (60 * 60); + retlen += snprintf(retval + retlen, + retsize - retlen, "%luh", telapsed); + telapsed *= (60 * 60); + } else { + telapsed = elapsed / (60 * 60 * 24); + retlen += snprintf(retval + retlen, + retsize - retlen, "%luh", telapsed); + telapsed *= (60 * 60 * 24); + } + elapsed -= telapsed; + } } else if (strcmp(var, "node") == 0) { retlen = strlcpy(retval, session->node, retsize); } else if (strcmp(var, "phone_number") == 0) {