dot

packages and services management
Log | Files | Refs | README

commit 04576535db6b2a950bf8fa9242c03e5c3e7a9fed
parent 7754485d073744257e905be00f3ddacfbdef81ed
Author: josuah <mail@josuah.net>
Date:   Sun, 28 Aug 2016 19:19:14 +0200

irc: Improved parsing

Diffstat:
Mbin/irc | 123+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
Mbin/run | 18++++++++++--------
2 files changed, 87 insertions(+), 54 deletions(-)

diff --git a/bin/irc b/bin/irc @@ -3,7 +3,7 @@ # \/ _____ ____ # /\ / ____\/ ___\ # / // / / /__ -# \/ \/ \____\ - Irc client in 233 lines of shell script +# \/ \/ \____\ - Irc client in 300 lines of shell script #=============================================================================== # # With help of http://xero.nu, inspired by http://tools.suckless.org/ii @@ -34,7 +34,7 @@ write() # connect() { - local srv="$1" user="$(tail "NICK")" + local srv="$1" user="$(tail "$1/NICK" | tee -a ~/log)" mkdir -p "$srv" [ -p "$srv/IN" ] || mkfifo "$srv/IN" @@ -44,6 +44,8 @@ connect() write "$srv" "NICK $user" write "$srv" "USER $(whoami) +i * :$FULLNAME" + + printf "$user" > "$srv/NICK" } # @@ -57,7 +59,7 @@ input() while read cmd do - user="$(tail "NICK")" + [ "$srv" ] && user="$(tail "$srv/NICK")" # Parse command and arguments if [ -z "${cmd##/*}" ] @@ -77,7 +79,7 @@ input() M | MSG | PRIVMSG ) arg="${arg%% *} :${arg#* }" write "$srv" "PRIVMSG $arg" - printf '%s' ":$user!local PRIVMSG $arg" | output "$srv" + print_msg "$user" "$arg" PRIVMSG "$srv" >> "$srv/$arg" ;; J | JOIN ) chan="$arg" @@ -91,15 +93,15 @@ input() write "$srv" "TOPIC ${arg-chan}" ;; ID | IDENTIFY ) - printf '\033[A\r\033[K' 1>&2 - printf ' \033[30;1m%s %s\033[0;1m > \033[0m' "$srv" "$chan" 1>&2 - printf '/IDENTIFY *****\n' 1>&2 + printf '\033[A\r\033[K' 1>&2 + printf ' \033[30;1m%s %s\033[0;1m > \033[0m' \ + "$srv" "$chan" 1>&2 + printf '/IDENTIFY *****\n' 1>&2 write "$srv" "PRIVMSG NickServ :IDENTIFY $arg" ;; '' ) write "$srv" "PRIVMSG $chan :$arg" - print_msg "$user" "$arg" PRIVMSG yes \ - >> "$srv/$chan" + print_msg "$user" "$arg" PRIVMSG "$srv" >> "$srv/$chan" ;; * ) write "$srv" "$cmd $arg" @@ -116,27 +118,28 @@ input() # message = [ ":" prefix SPACE ] command [ params ] crlf # prefix = servername / ( nickname [ [ "!" user ] "@" host ] ) # command = 1*letter / 3digit -# params = *14( SPACE mid ) [ SPACE ":" trailing ] -# =/ 14( SPACE mid ) [ SPACE [ ":" ] trailing ] -# +# params = *14( SPACE middle ) [ SPACE ":" trailing ] +# =/ 14( SPACE middle ) [ SPACE [ ":" ] trailing ] +# # nospcrlfcl = %x01-09 / %x0B-0C / %x0E-1F / %x21-39 / %x3B-FF # ; any octet except NUL, CR, LF, " " and ":" -# mid = nospcrlfcl *( ":" / nospcrlfcl ) +# middle = nospcrlfcl *( ":" / nospcrlfcl ) # trailing = *( ":" / " " / nospcrlfcl ) -# +# # SPACE = %x20 ; space character # crlf = %x0D %x0A ; "carriage return" "linefeed" # output() { - local srv="$1" user="$(tail "NICK")" - local cmd params mid chan last check + local srv="$1" while read msg do # Parse server message + local prefix='' nick='' cmd='' trailing='' sep='' + local params='' middle='' chan='' user="$(tail "$srv/NICK")" - local prefix='' nick='' trail='' sep='' # Reset + # Prefix msg="${msg#$CR}" msg="${msg%$CR}" if [ -z "${msg##:*}" ] then @@ -145,38 +148,59 @@ output() msg="${msg#* }" fi - cmd="${msg%% *}" - msg="${msg#* }" params="$msg" - mid="${msg% }" mid="${msg%%:*}" - [ -z "${params%%*:*}" ] && trail="${params#*:}" - chan="${mid%% *}" - case "$chan" in - *"$srv" | "$user" | '*' | '' ) chan='SERVER' ;; - esac + # Command + cmd="${msg%% *}" msg="${msg#* }" - # Execute action - - # Single line separator at the right place. - [ "$cmd" = PRIVMSG ] && c="$chan $cmd $nick" || c="$chan $cmd" - if [ ! "$(printf %s "$last" | grep -F "$c")" ] - then sep='yes' + # Parameters + [ -z "${msg##* *}" ] && params="${msg# *}" + if [ -z "${msg##:*}" ] + then + trailing="${params#:}" + elif [ -z "${msg##*:*}" ] + then + trailing="${params#*:}" + middle="${params%%:*}" + else + middle="$params" fi - last="$(printf '%s %s ' "$chan" "$cmd" - [ "$cmd" = PRIVMSG ] && printf %s "$nick" - printf '\n' - printf '%s\n' "$last" | grep -vF "$chan")" + # Channel and extra command action + case "$cmd" in + JOIN ) chan="$trailing" ;; + PART ) chan="$middle" ;; + PRIVMSG ) + if [ "$middle" = "$user" ] + then chan="$nick" + else chan="$middle" + fi + ;; + NICK ) chan="SERVER" + if [ "$nick" = "$user" ] + then printf '%s\n' "$trailing" > "$srv/NICK" + fi + ;; + QUIT | NOTICE | [0-9][0-9][0-9] | * ) chan="SERVER" ;; + esac + + # Execute action case "$cmd" in PING ) - write "$srv" "PONG $trail" + write "$srv" "PONG $trailing" ;; - * ) - print_msg "$nick" "$trail" "$cmd" "$sep" "$user" \ + QUIT | NICK ) + for chan in "$(ls "$srv/#"*)" "$srv/SERVER" + do print_msg "$nick" "$trailing" "$cmd" "$srv" \ + >> "$srv/$chan" + done + ;; + *) + print_msg "$nick" "$trailing" "$cmd" "$srv" \ >> "$srv/$chan" ;; esac - case "$trail" in + # Bot actions + case "$trailing" in 'VERSION' ) write "$srv" "PRIVMSG $nick :VERSION nc and a script" @@ -190,7 +214,8 @@ output() # print_msg() { - local nick="$1" trail="$2" cmd="$3" sep="$4" user="$5" + local nick="$1" trailing="$2" cmd="$3" srv="$4" last_cmd last_nick + local user="$(tail "$srv/NICK")" case "$cmd" in QUIT ) cmd='<' ;; @@ -200,15 +225,21 @@ print_msg() PART ) cmd='~' ;; ERROR ) cmd='X' ;; MODE ) cmd='+' ;; - NICK ) cmd='=' - [ "$nick" = "$user" ] && printf '%s\n' "$trail" > "NICK" - ;; + NICK ) cmd='=' ;; esac - [ "$sep" ] && printf "%${NICK_LEN}s |\n" + # Single line separator at the right place. + for n in $(tac "$srv/$chan" | cut -c 1-$NICK_LEN) + do last_nick="$n"; break + done + last_cmd="$(tail -n 1 "$srv/$chan" | cut -c $(($NICK_LEN + 2))-)" + last_cmd="${last_cmd%% *}" + if [ "$cmd" = '|' ] && [ "$last_nick" != "$nick" ] || [ "$cmd" != "$last_cmd" ] + then printf "%${NICK_LEN}s |\n" + fi # Print the message - printf '%s\n' "$trail" | fold -s -w $((78 - $NICK_LEN - ${#cmd})) \ + printf '%s\n' "$trailing" | fold -s -w $((78 - $NICK_LEN - ${#cmd})) \ | while read line do printf "%${NICK_LEN}s %1s %s\n" "$nick" "$cmd" "$line" @@ -227,7 +258,7 @@ print_msg() cd "$DIR" printf '%s\n' "$NICK" > "NICK" -case $1 in +case "$1" in -i ) trap 'find . -name IN -exec rm {} ";"; kill -9 0' INT EXIT input diff --git a/bin/run b/bin/run @@ -31,12 +31,13 @@ printf '$ %s ' "$cmd" # Get command arguments and the session name name="$cmd" case "$cmd" in -'' ) exit 1 ;; -mbsync ) opt='-a' ;; -alsamixer ) opt='-c 1' ;; -ping ) opt='-c 3 www.wikipedia.org' ;; -feeds ) opt='read' ;; -htop | s-nail | irc | rirc | agenda | cmus ) : ;; +'' ) exit 1 ;; +mbsync ) opt='-a' ;; +alsamixer ) opt='-c 1' ;; +ping ) opt='-c 3 www.wikipedia.org' ;; +feeds ) opt='read' ;; +irc ) opt='-i' ;; +htop | s-nail | rirc | agenda | cmus ) : ;; vi | vim | vis | less | ex-vi | ex | ed | emacsclient ) opt="$(find ./ -type f ! -path '*/.git/*' | fzy)" file="$opt" ;; @@ -59,5 +60,6 @@ then fi # Start the command in an abduco session -TERM=screen ABDUCO="$cmd" \ - abduco -e '^z' -A "$(printf '%s%s' "$cmd" "$file" | tr '/' '!')" $cmd $opt +TERM=screen ABDUCO="$cmd" abduco \ + -e '^z' \ + -A "$(printf '%s%s' "$cmd" "$file" | tr '/' '!')" $cmd $opt