hysteria

ii wrapper script
git clone git://git.2f30.org/hysteria.git
Log | Files | Refs | README | LICENSE

commit 9ce9eea11d50b0f748730c5acb3699e23a958a56
parent 25a5c231e41db16fc2d468c55cfbfa775ba6f522
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Tue Oct 28 21:43:41 +0000

rename hysteria scripts to make it usable system-wide

- use the prefix "hysteria-" for the helper scripts.
- make the default config location "$HOME/.hysteria/config".

Diffstat:
autojoin | 24------------------------
connectserver | 26--------------------------
highlight.c | 175-------------------------------------------------------------------------------
hysteria | 8++++----
hysteria-autojoin | 24++++++++++++++++++++++++
hysteria-connect | 26++++++++++++++++++++++++++
hysteria-highlight.c | 175+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
hysteria-monitor | 42++++++++++++++++++++++++++++++++++++++++++
hysteria-waitfile | 13+++++++++++++
mkfile | 2+-
monitor | 42------------------------------------------
waitfile | 13-------------
12 files changed, 285 insertions(+), 285 deletions(-)
diff --git a/autojoin b/autojoin @@ -1,24 +0,0 @@ -#!/bin/sh - -if [ "$3" = "" ]; then - . ./config -else - . "$3" -fi - -if test $# -lt 2; then - echo "usage: $(basename $0) host channel [config]" 1>&2 - exit 1 -fi - -tmux list-sessions -F '#S' 2>/dev/null | \ - grep "^$session$" >/dev/null 2>/dev/null -if [ $? -eq 1 ]; then - echo "no hysteria session \"$session\" running" 1>&2 - exit 1 -fi - -host="$1" -c="$2" -echo "/join $c" > "$ircdir/$host/in" -./waitfile "$ircdir/$host/$c/out" diff --git a/connectserver b/connectserver @@ -1,26 +0,0 @@ -#!/bin/sh - -if [ "$2" = "" ]; then - . ./config -else - . "$2" -fi - -if test -z $1; then - echo "usage: $(basename $0) host [config]" 1>&2 - exit 1 -fi - -tmux list-sessions -F '#S' 2>/dev/null | \ - grep "^$session$" >/dev/null 2>/dev/null -if [ $? -eq 1 ]; then - echo "no hysteria session \"$session\" running" 1>&2 - exit 1 -fi - -host=$(printf '%s' "$1" | cut -d ':' -f 1) -port=$(printf '%s' "$1" | cut -s -d ':' -f 2) -[ "$port" = "" ] && port="6667" -tmux new-window "ii -i $ircdir -s $host -p $port -n $nick -f $fullname" -tmux rename-window "$host" -./waitfile "$ircdir/$host/out" diff --git a/highlight.c b/highlight.c @@ -1,175 +0,0 @@ -/* - * highlight chat for ii similar in style to irssi. - * - * supports: - * - highlight own nick, words + beep (can set urgent hint). - * - highlight own messages. - * - highlight urls. - * - highlight server messages. - * - align nicknames. - * - ignore words. - * - don't print date-prefix (YYYY-mm-dd). - */ - -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <errno.h> - -#include <sys/types.h> -#include <regex.h> - -/* escape codes: colors, reset */ -static const char *green = "\x1b[1;32m"; -static const char *yellow = "\x1b[1;33m"; -static const char *blue = "\x1b[1;34m"; -static const char *white = "\x1b[1;37m"; -static const char *reset = "\x1b[1;0m"; - -/* highlight / color words */ -static const char *regexword = "(evil_bob|bob|hiltjo)"; -/* match nickname */ -static const char *regexnick = "(evil_bob|bob|hiltjo)"; -/* nickname to match and align */ -static const char *regexnickalign = "^[0-9]{2}:[0-9]{2} <([^>]+)>"; -/* match server messages */ -static const char *regexserver = "^([0-9]{2}:[0-9]{2} -!- .*)$"; -/* skip this prefix for each line (date part) */ -static const char *regextimestamp = "^[0-9]{4}-[0-9]{2}-[0-9]{2} "; -/* ignore message on match */ -static const char *regexignore = ""; -/* highlight / color urls */ -static const char *regexurl = - "(((https?|ftp)|mailto):(//)?[^ <>\"[:blank:]]*|(www|ftp)[0-9]?\\.[-a-z0-9.]+)"; - -static regex_t nick, nickalign, server, timestamp, ignore, url, word; - -static void -eprintf(const char *s) -{ - fprintf(stderr, "%s\n", s); - exit(EXIT_FAILURE); -} - -static void -printalign(const char *s, size_t len, size_t maxlen) -{ - size_t i; - - if(len < maxlen) { - for(i = 0; i < maxlen - len; i++) - putchar(' '); - } - for(i = 0; i < len && i < maxlen; i++) - putchar(*s++); -} - -int -main(void) { - /* buffer: should be atleast 512 bytes (IRC RFC) + datetime prefix, - * assumes line length is lower than sizeof(buffer). */ - char buffer[1024]; - regmatch_t match, matches[3]; - size_t len; - const char *p; - int hasmatch, ret; - - /* compile regex */ - if(regcomp(&nick, regexnick, REG_EXTENDED | REG_ICASE)) - eprintf("invalid regex: nick"); - if(regcomp(&nickalign, regexnickalign, REG_EXTENDED)) - eprintf("invalid regex: nickalign"); - if(regcomp(&server, regexserver, REG_EXTENDED)) - eprintf("invalid regex: server"); - if(regcomp(&timestamp, regextimestamp, REG_EXTENDED)) - eprintf("invalid regex: timestamp"); - if(regcomp(&ignore, regexignore, REG_EXTENDED | REG_ICASE)) - eprintf("invalid regex: ignore"); - if(regcomp(&url, regexurl, REG_EXTENDED | REG_ICASE)) - eprintf("invalid regex: url"); - if(regcomp(&word, regexword, REG_EXTENDED | REG_ICASE)) - eprintf("invalid regex: word"); - - while(fgets(buffer, sizeof(buffer), stdin)) { - p = buffer; - - /* skip date part (YYY-mm-dd) of timestamp */ - if((ret = regexec(&timestamp, p, 1, &match, 0)) != REG_NOMATCH) - p += match.rm_eo - match.rm_so; - - /* highlight / color server messages */ - if((ret = regexec(&server, p, 1, &match, 0)) != REG_NOMATCH) { - fputs(green, stdout); - fputs(p, stdout); - fputs(reset, stdout); - fflush(stdout); - continue; - } - - /* ignore on match */ - if(regexignore[0] && (ret = regexec(&ignore, p, 1, &match, 0)) != REG_NOMATCH) - continue; - - /* align nicknames */ - if((ret = regexec(&nickalign, p, 2, matches, 0)) != REG_NOMATCH) - { - fwrite(p, 1, matches[1].rm_so, stdout); - putchar('@'); - p += matches[1].rm_so; - len = matches[1].rm_eo - matches[1].rm_so; - /* own nick ?: color nickname part white */ - if((ret = regexec(&nick, p, 1, &match, 0)) != REG_NOMATCH && - match.rm_so == 0 && match.rm_eo == len) - { - fputs(white, stdout); - printalign(p, len, 10); - fputs(reset, stdout); - } else { - printalign(p, len, 10); - } - p += len; - } - - hasmatch = 1; - while(hasmatch) { - hasmatch = 0; - /* highlight word */ - if(regexword[0] && - (ret = regexec(&word, p, 1, &match, 0)) != REG_NOMATCH) - { - fwrite(p, 1, match.rm_so, stdout); - fputs(yellow, stdout); - p += match.rm_so; - fwrite(p, 1, match.rm_eo - match.rm_so, stdout); - fputs(reset, stdout); - p += match.rm_eo - match.rm_so; - /* beep, can be set in your terminal to set urgent hint */ - putchar('\a'); - hasmatch = 1; - } - /* highlight url */ - if((ret = regexec(&url, p, 1, &match, 0)) != REG_NOMATCH) { - fwrite(p, 1, match.rm_so, stdout); - fputs(blue, stdout); - p += match.rm_so; - fwrite(p, 1, match.rm_eo - match.rm_so, stdout); - fputs(reset, stdout); - p += match.rm_eo - match.rm_so; - hasmatch = 1; - } - } - fputs(p, stdout); /* rest */ - fflush(stdout); - } - - /* free regex */ - regfree(&word); - regfree(&nick); - regfree(&nickalign); - regfree(&server); - regfree(&timestamp); - regfree(&ignore); - regfree(&url); - - return EXIT_SUCCESS; -} diff --git a/hysteria b/hysteria @@ -1,7 +1,7 @@ #!/bin/sh -x if [ "$1" = "" ]; then - config="./config" + config="$HOME/.hysteria/config" else config=$(readlink -f "$1") fi @@ -9,15 +9,15 @@ fi rm -rf "$ircdir" mkdir -p "$ircdir" -tmux new-session -s "$session" -d "./monitor \"$config\"" +tmux new-session -s "$session" -d "hysteria-monitor \"$config\"" tmux rename-window "monitor" for h in $hosts; do - ./connectserver "$h" "$config" + hysteria-connect "$h" "$config" done for c in $channels; do host=$(printf '%s' "$c" | cut -d ':' -f 1) channel=$(printf '%s' "$c" | cut -s -d ':' -f 2) - ./autojoin "$host" "$channel" "$config" + hysteria-autojoin "$host" "$channel" "$config" done diff --git a/hysteria-autojoin b/hysteria-autojoin @@ -0,0 +1,24 @@ +#!/bin/sh + +if [ "$3" = "" ]; then + . "$HOME/.hysteria/config" +else + . "$3" +fi + +if test $# -lt 2; then + echo "usage: $(basename $0) host channel [config]" 1>&2 + exit 1 +fi + +tmux list-sessions -F '#S' 2>/dev/null | \ + grep "^$session$" >/dev/null 2>/dev/null +if [ $? -eq 1 ]; then + echo "no hysteria session \"$session\" running" 1>&2 + exit 1 +fi + +host="$1" +c="$2" +echo "/join $c" > "$ircdir/$host/in" +hysteria-waitfile "$ircdir/$host/$c/out" diff --git a/hysteria-connect b/hysteria-connect @@ -0,0 +1,26 @@ +#!/bin/sh + +if [ "$2" = "" ]; then + . "$HOME/.hysteria/config" +else + . "$2" +fi + +if test -z $1; then + echo "usage: $(basename $0) host [config]" 1>&2 + exit 1 +fi + +tmux list-sessions -F '#S' 2>/dev/null | \ + grep "^$session$" >/dev/null 2>/dev/null +if [ $? -eq 1 ]; then + echo "no hysteria session \"$session\" running" 1>&2 + exit 1 +fi + +host=$(printf '%s' "$1" | cut -d ':' -f 1) +port=$(printf '%s' "$1" | cut -s -d ':' -f 2) +[ "$port" = "" ] && port="6667" +tmux new-window "ii -i $ircdir -s $host -p $port -n $nick -f $fullname" +tmux rename-window "$host" +hysteria-waitfile "$ircdir/$host/out" diff --git a/hysteria-highlight.c b/hysteria-highlight.c @@ -0,0 +1,175 @@ +/* + * highlight chat for ii similar in style to irssi. + * + * supports: + * - highlight own nick, words + beep (can set urgent hint). + * - highlight own messages. + * - highlight urls. + * - highlight server messages. + * - align nicknames. + * - ignore words. + * - don't print date-prefix (YYYY-mm-dd). + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> + +#include <sys/types.h> +#include <regex.h> + +/* escape codes: colors, reset */ +static const char *green = "\x1b[1;32m"; +static const char *yellow = "\x1b[1;33m"; +static const char *blue = "\x1b[1;34m"; +static const char *white = "\x1b[1;37m"; +static const char *reset = "\x1b[1;0m"; + +/* highlight / color words */ +static const char *regexword = "(evil_bob|bob|hiltjo)"; +/* match nickname */ +static const char *regexnick = "(evil_bob|bob|hiltjo)"; +/* nickname to match and align */ +static const char *regexnickalign = "^[0-9]{2}:[0-9]{2} <([^>]+)>"; +/* match server messages */ +static const char *regexserver = "^([0-9]{2}:[0-9]{2} -!- .*)$"; +/* skip this prefix for each line (date part) */ +static const char *regextimestamp = "^[0-9]{4}-[0-9]{2}-[0-9]{2} "; +/* ignore message on match */ +static const char *regexignore = ""; +/* highlight / color urls */ +static const char *regexurl = + "(((https?|ftp)|mailto):(//)?[^ <>\"[:blank:]]*|(www|ftp)[0-9]?\\.[-a-z0-9.]+)"; + +static regex_t nick, nickalign, server, timestamp, ignore, url, word; + +static void +eprintf(const char *s) +{ + fprintf(stderr, "%s\n", s); + exit(EXIT_FAILURE); +} + +static void +printalign(const char *s, size_t len, size_t maxlen) +{ + size_t i; + + if(len < maxlen) { + for(i = 0; i < maxlen - len; i++) + putchar(' '); + } + for(i = 0; i < len && i < maxlen; i++) + putchar(*s++); +} + +int +main(void) { + /* buffer: should be atleast 512 bytes (IRC RFC) + datetime prefix, + * assumes line length is lower than sizeof(buffer). */ + char buffer[1024]; + regmatch_t match, matches[3]; + size_t len; + const char *p; + int hasmatch, ret; + + /* compile regex */ + if(regcomp(&nick, regexnick, REG_EXTENDED | REG_ICASE)) + eprintf("invalid regex: nick"); + if(regcomp(&nickalign, regexnickalign, REG_EXTENDED)) + eprintf("invalid regex: nickalign"); + if(regcomp(&server, regexserver, REG_EXTENDED)) + eprintf("invalid regex: server"); + if(regcomp(&timestamp, regextimestamp, REG_EXTENDED)) + eprintf("invalid regex: timestamp"); + if(regcomp(&ignore, regexignore, REG_EXTENDED | REG_ICASE)) + eprintf("invalid regex: ignore"); + if(regcomp(&url, regexurl, REG_EXTENDED | REG_ICASE)) + eprintf("invalid regex: url"); + if(regcomp(&word, regexword, REG_EXTENDED | REG_ICASE)) + eprintf("invalid regex: word"); + + while(fgets(buffer, sizeof(buffer), stdin)) { + p = buffer; + + /* skip date part (YYY-mm-dd) of timestamp */ + if((ret = regexec(&timestamp, p, 1, &match, 0)) != REG_NOMATCH) + p += match.rm_eo - match.rm_so; + + /* highlight / color server messages */ + if((ret = regexec(&server, p, 1, &match, 0)) != REG_NOMATCH) { + fputs(green, stdout); + fputs(p, stdout); + fputs(reset, stdout); + fflush(stdout); + continue; + } + + /* ignore on match */ + if(regexignore[0] && (ret = regexec(&ignore, p, 1, &match, 0)) != REG_NOMATCH) + continue; + + /* align nicknames */ + if((ret = regexec(&nickalign, p, 2, matches, 0)) != REG_NOMATCH) + { + fwrite(p, 1, matches[1].rm_so, stdout); + putchar('@'); + p += matches[1].rm_so; + len = matches[1].rm_eo - matches[1].rm_so; + /* own nick ?: color nickname part white */ + if((ret = regexec(&nick, p, 1, &match, 0)) != REG_NOMATCH && + match.rm_so == 0 && match.rm_eo == len) + { + fputs(white, stdout); + printalign(p, len, 10); + fputs(reset, stdout); + } else { + printalign(p, len, 10); + } + p += len; + } + + hasmatch = 1; + while(hasmatch) { + hasmatch = 0; + /* highlight word */ + if(regexword[0] && + (ret = regexec(&word, p, 1, &match, 0)) != REG_NOMATCH) + { + fwrite(p, 1, match.rm_so, stdout); + fputs(yellow, stdout); + p += match.rm_so; + fwrite(p, 1, match.rm_eo - match.rm_so, stdout); + fputs(reset, stdout); + p += match.rm_eo - match.rm_so; + /* beep, can be set in your terminal to set urgent hint */ + putchar('\a'); + hasmatch = 1; + } + /* highlight url */ + if((ret = regexec(&url, p, 1, &match, 0)) != REG_NOMATCH) { + fwrite(p, 1, match.rm_so, stdout); + fputs(blue, stdout); + p += match.rm_so; + fwrite(p, 1, match.rm_eo - match.rm_so, stdout); + fputs(reset, stdout); + p += match.rm_eo - match.rm_so; + hasmatch = 1; + } + } + fputs(p, stdout); /* rest */ + fflush(stdout); + } + + /* free regex */ + regfree(&word); + regfree(&nick); + regfree(&nickalign); + regfree(&server); + regfree(&timestamp); + regfree(&ignore); + regfree(&url); + + return EXIT_SUCCESS; +} diff --git a/hysteria-monitor b/hysteria-monitor @@ -0,0 +1,42 @@ +#!/bin/sh +# +# Monitor for private messages and/or new channels and +# create a new split tmux window + +if [ "$1" = "" ]; then + . "$HOME/.hysteria/config" +else + . "$1" +fi + +while :; do + for n in $(find "$ircdir" -name "in" | sort -r 2>/dev/null); do + title=$(printf '%s' "$n" | awk -F/ '{ \ + if (NF == 4) \ + print $2":"$3; \ + else if (NF == 3) \ + print $2}') + found=0 + for t in $(tmux list-windows -F '#W'); do + if test "$t" = "$title"; then + found=1 + break + fi + done + if test $found -eq 0; then + INFILE="$(readlink -f $n)" + NDIR="$(dirname ${INFILE})" + OUTFILE="${NDIR}/out" + CHAN="$(basename ${NDIR})" + # default inputcmd if not set in config. + if test x"${inputcmd}" = x""; then + inputcmd='cat >> "$INFILE"' + fi + tmux new-window "tail -f '$OUTFILE' | hysteria-highlight" + tmux split-window -v -p 1 "tmux resize-pane -y 4; \ + CHAN='$CHAN' INFILE='$INFILE' OUTFILE='$OUTFILE' eval '$inputcmd'" + tmux rename-window "$title" + fi + done + sleep 1 +done diff --git a/hysteria-waitfile b/hysteria-waitfile @@ -0,0 +1,13 @@ +#!/bin/sh + +if test -z $1; then + echo "usage: $(basename $0) file" 1>&2 + exit 1 +fi + +while :; do + if test -e "$1"; then + break + fi + sleep 1 +done diff --git a/mkfile b/mkfile @@ -2,7 +2,7 @@ CC = cc CFLAGS = -Wall -pedantic -std=c99 -Os LDFLAGS = -s -TARG = highlight +TARG = hysteria-highlight all: $TARG diff --git a/monitor b/monitor @@ -1,42 +0,0 @@ -#!/bin/sh -# -# Monitor for private messages and/or new channels and -# create a new split tmux window - -if [ "$1" = "" ]; then - . ./config -else - . "$1" -fi - -while :; do - for n in $(find "$ircdir" -name "in" | sort -r 2>/dev/null); do - title=$(printf '%s' "$n" | awk -F/ '{ \ - if (NF == 4) \ - print $2":"$3; \ - else if (NF == 3) \ - print $2}') - found=0 - for t in $(tmux list-windows -F '#W'); do - if test "$t" = "$title"; then - found=1 - break - fi - done - if test $found -eq 0; then - INFILE="$(readlink -f $n)" - NDIR="$(dirname ${INFILE})" - OUTFILE="${NDIR}/out" - CHAN="$(basename ${NDIR})" - # default inputcmd if not set in config. - if test x"${inputcmd}" = x""; then - inputcmd='cat >> "$INFILE"' - fi - tmux new-window "tail -f '$OUTFILE' | ./highlight" - tmux split-window -v -p 1 "tmux resize-pane -y 4; \ - CHAN='$CHAN' INFILE='$INFILE' OUTFILE='$OUTFILE' eval '$inputcmd'" - tmux rename-window "$title" - fi - done - sleep 1 -done diff --git a/waitfile b/waitfile @@ -1,13 +0,0 @@ -#!/bin/sh - -if test -z $1; then - echo "usage: $(basename $0) file" 1>&2 - exit 1 -fi - -while :; do - if test -e "$1"; then - break - fi - sleep 1 -done