site-josuah

/usr/josuah
Log | Files | Refs

commit ec8b7da2bf81ef9c5b36dbfd5bd4414689dc412d
parent 992f5c824e386f36ac77a0b7d44c71eeac005ab0
Author: Josuah Demangeon <me@josuah.net>
Date:   Sun, 19 Apr 2020 05:26:43 +0200

wiki/jj: update the scripts

Diffstat:
Awiki/jj/jj-join | 14++++++++++++++
Mwiki/jj/jj-log | 174++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mwiki/jj/jj-tail | 12+++---------
3 files changed, 110 insertions(+), 90 deletions(-)

diff --git a/wiki/jj/jj-join b/wiki/jj/jj-join @@ -0,0 +1,14 @@ +#!/bin/sh -e +# re-join all channels for which there is a log + +IRC_HOST=${IRC_HOST:-$1} +IRC_HOST=${IRC_HOST:?} +IRC_DIR=${IRC_DIR:-/var/irc} + +cd "$IRC_DIR/$IRC_HOST" +[ -p "in" ] || exit 1 +for IRC_CHANNEL in "channels/#"*; do + IRC_CHANNEL=${IRC_CHANNEL##*/} + IRC_CHANNEL=${IRC_CHANNEL%.log} + echo "JOIN $IRC_CHANNEL" >"in" +done diff --git a/wiki/jj/jj-log b/wiki/jj/jj-log @@ -2,144 +2,156 @@ function isleap(year) { - return (year % 4 == 0) \ - && (year % 100 != 0) \ - || (year % 400 == 0) + return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0) } function mdays(mon, year) { - return (mon == 2) \ - ? (28 + isleap(year)) \ - : (30 + (mon + (mon > 7)) % 2) + return (mon == 2) ? (28 + isleap(year)) : (30 + (mon + (mon > 7)) % 2) } # Split the time in seconds since epoch into a table, with fields named # as with gmtime(3): tm["year"], tm["mon"], tm["mday"], tm["hour"], # tm["min"], tm["sec"]. + function gmtime(sec, tm) { - tm["year"]=1970 + tm["year"] = 1970 while (sec >= (s = 86400 * (365 + isleap(tm["year"])))) { tm["year"]++ - sec-=s + sec -= s } - tm["mon"]=1 + tm["mon"] = 1 while (sec >= (s = 86400 * mdays(tm["mon"], tm["year"]))) { tm["mon"]++ - sec-=s + sec -= s } - tm["mday"]=1 + tm["mday"] = 1 while (sec >= (s = 86400)) { tm["mday"]++ - sec-=s + sec -= s } - tm["hour"]=0 + tm["hour"] = 0 while (sec >= 3600) { tm["hour"]++ - sec-=3600 + sec -= 3600 } - tm["min"]=0 + tm["min"] = 0 while (sec >= 60) { tm["min"]++ - sec-=60 + sec -= 60 } - tm["sec"]=sec -} - -# Handle tail's "==> filename <==" headers -$1 == "==>" { - sub("/channels/", " ") - sub(".log <==", "") - sub(".*/", "") - sub(" ", "/") - printf "\n%s", $0 - next + tm["sec"] = sec } -function tz( cmd, date, h, m) +function init_timezone( \ + cmd, date, h, m) { - cmd="date +%z 2>/dev/null" + cmd = "date +%z" cmd | getline date close(cmd) - h=substr(date, 2, 2) - m=substr(date, 4, 2) - tzoffset=substr(date, 1, 1) (h * 3600 + m * 60) + h = substr(date, 2, 2) + m = substr(date, 4, 2) + TZOFFSET = substr(date, 1, 1) (h * 3600 + m * 60) +} + +function parse_msg(s, msg, + nickend, text) +{ + nickend = index($2, ">") + gmtime($1 + TZOFFSET, TM) + msg["type"] = substr($2, nickend + 1) + msg["text"] = substr($0, index($0, ">") + 2 + length(msg["type"])) + msg["nick"] = substr($2, 2, nickend - 2) + msg["date"] = sprintf("%04d/%02d/%02d", TM["year"], TM["mon"], TM["mday"]) + msg["time"] = sprintf("%02d:%02d", TM["hour"], TM["min"]) + +} + +function parse_bold(s, + tail) +{ + tail = s + s = "" + while (match(tail, "\002[^\002]*\002")) { + s = s substr(tail, 1, RSTART - 1) "\033[1m" + s = s substr(tail, RSTART + 1, RLENGTH - 2) "\033[m" + tail = substr(tail, RSTART + RLENGTH) + } + return s tail } BEGIN { - FS=" " - tz() -} { + FS = " " + init_timezone() +} + +# Handle tail's "==> filename <==" headers. + +!NF { + next +} + +$1 == "==>" { + sub(".log <==", "") + sub("/channels/", " ") + sub("/server$", " server") + sub(".*/", "") + sub(" ", "/") + printf("\n== %s ==", $0) + fflush() + next +} + +{ if (NF < 2) { - printf "\n%s", $0 + printf("\n%s", $0) + fflush() next } - gmtime($1 + tzoffset, tm) - date=sprintf("%04d/%02d/%02d", tm["year"], tm["mon"], tm["mday"]) - time=sprintf("%02d:%02d", tm["hour"], tm["min"]) + parse_msg(s, msg) + msg["text"] = parse_bold(msg["text"]) # Display date only when it actually changed. - if (lastdate != date) { - lastdate=date - printf "%s\033[37m-- %s --\033[0m", - (NR != 1 ? "\n\n" : ""), date + if (LASTDATE != msg["date"]) { + LASTDATE = msg["date"] + printf("\n\n-- " msg["date"] " --") } - nickend=index($2, ">") - type=substr($2, nickend+1) - msg=substr($0, index($0, ">") + 2 + length(type)) - nick=substr($2, 2, nickend-2) - # Set the date color for highlights. - if (index(type, "!")) { - printf "\a" - dcolor="33" + if (index(msg["type"], "!")) { + printf("\a") + dcolor = "33" } else { - dcolor="38;5;8" - } - - # When JJ_SERVERLOG is set, print messages in the default color. - if (ENVIRON["JJ_SERVERLOG"] != "") { - printf "\n\033[%sm%s\033[0m %s", dcolor, time, msg - next + dcolor = "38;5;8" } # Status message. - if (nick == "-") { - printf "\n\033[%sm%s \033[%sm%s\033[0m", - dcolor, time, "38;5;8", msg + if (msg["nick"] == "-") { + printf("\n\033[%sm%s \033[%sm%s\033[0m", + dcolor, msg["time"], "38;5;8", msg["text"]) + fflush() next } - isme=index(type, "*") - - # Alternating nick colors for better readability. - if (!isme && lastnick != nick) { - alt=!alt - lastnick=nick - } - - if (isme) - ncolor="37" - else if (alt) - ncolor="31" - else - ncolor="0" + ncolor = (index(msg["type"], "*") ? "37" : "31") # Handle ACTION. - if (index(type, "a")) { - msg=nick " " msg - nick="*" + if (index(msg["type"], "a")) { + msg["text"] = msg["nick"] " " msg["text"] + msg["nick"] = "*" } - printf "\n\033[%sm%s\033[0m \033[%sm%10s\033[0m: %s", - dcolor, time, ncolor, nick, msg -} END { print "" } + printf("\n\033[%sm%s\033[0m \033[%sm%10s\033[0m: %s", + dcolor, msg["time"], ncolor, msg["nick"], msg["text"]) + fflush() + + delete msg +} diff --git a/wiki/jj/jj-tail b/wiki/jj/jj-tail @@ -7,14 +7,8 @@ for x in "$@"; do shift set -- "$@" -o -path "*$x*.log" done +shift # remove the leading -o -# remove the leading -o -shift - -# First print the history -find "/var/irc" "$@" -exec retail {} + \ - | jj-log - -# Then print the live log -exec find "/var/irc" "$@" -exec tail -f -n 0 {} + \ +# First print the history then print the live log +exec find "/var/irc" "$@" -exec retail {} + -exec tail -f -n 0 {} + \ | exec jj-log