dot

packages and services management
Log | Files | Refs | README

commit d91423f98a9bcb922504695a8d442be9d2edcd00
parent 5cf7324f4846f06ea8d9a8933342bd812f17dbf9
Author: sshbio <jd@ssh.bio>
Date:   Fri, 12 Aug 2016 19:26:34 +0200

Simplified and fixed irc

Diffstat:
Mbin/irc | 213++++++++++++++++++++++++++++++++++++++++---------------------------------------
Ms-nail/.mailrc | 47+++++++++++++++++++++++++++++------------------
2 files changed, 137 insertions(+), 123 deletions(-)

diff --git a/bin/irc b/bin/irc @@ -6,7 +6,7 @@ # \/ \/ \____\ - Irc client based on nc and a script from http://xero.nu #=============================================================================== -commands_help=' +help=' /c, /connect, /s, /server SERVER Start connection with SERVER @@ -29,11 +29,11 @@ commands_help=' NICK="${NICK-$USER}" FULLNAME='With netcat and a script as a client' IRC_DIR="$HOME/.cache/irc" -NICK_WIDTH=14 -LINES="$(stty size | cut -d ' ' -f 2)" +NICK_LEN=14 NL=' ' + # Server interaction #------------------------------------------------------------------------------- @@ -52,8 +52,7 @@ write() # open_connection() { - local server="$1" - local in="$IRC_DIR/$server/in" + local server="$1" in="$IRC_DIR/$1/in" mkdir -p "$IRC_DIR/$server" [ -p "$in" ] || mkfifo "$in" @@ -71,56 +70,50 @@ input() { local SERVER CHANNEL msg - while read command + while read cmd do - - if [ -z "${command##/*}" ] - then - argument="${command#* }" - command="${command#/}" - command="${command%% *}" - else - argument="$command" - command='' + if [ -z "${cmd##/*}" ] + then arg="${cmd#* }" cmd="${cmd#/}" cmd="${cmd%% *}" + else arg="$cmd" cmd='' fi - command="$(printf %s "$command" | tr [a-z] [A-Z])" - - case "$command" in + cmd="$(printf %s "$cmd" | tr [a-z] [A-Z])" + + case "$cmd" in M | MSG | PRIVMSG ) - write "PRIVMSG $( \ - printf '%s' "$argument" | cut -d ' ' -f 1 \ - ) :$( \ - printf '%s' "$argument" | cut -d ' ' -f 2- \ - )" "$SERVER" + write "PRIVMSG ${arg%% *} :${arg#* }" "$SERVER" ;; J | JOIN ) - CHANNEL="$argument" - write "JOIN $argument" "$SERVER" + CHANNEL="$arg" + write "JOIN $arg" "$SERVER" ;; S | SERVER | C | CONNECT ) + SERVER="$arg" if [ "$(ps ax | grep -F "$SERVER")" ] - then - SERVER="$argument" - open_connection "$SERVER" + then open_connection "$SERVER" fi ;; HELP ) - printf '%s\n' "$commands_help" + printf '%s\n' "$help" ;; QUIT ) - write "QUIT $argument" "$SERVER" + write "QUIT $arg" "$SERVER" printf 'Closing background process\n' kill -9 0 ;; + ME | ACTION ) + C_A="$(printf '\001')" + msg="PRIVMSG $CHANNEL :${C_A}ACTION $arg$C_A" + write "$msg" "$SERVER" + print_message "$SERVER" ":$NICK!localhost $msg$NL" + ;; '' ) - msg="PRIVMSG $CHANNEL :$argument" + msg="PRIVMSG $CHANNEL :$arg" write "$msg" "$SERVER" - printf ':%s!localhost %s\n' "$NICK" "$msg" \ - | output "$SERVER" + print_message "$SERVER" ":$NICK!localhost :$msg$NL" ;; * ) - write "$command $argument" "$SERVER" + write "$cmd $arg" "$SERVER" ;; esac done @@ -128,11 +121,27 @@ input() # -# The extracted message is parsed into the components <prefix>,<command> and -# list of parameters (<params>). -# -# The Augmented BNF representation for this is: -# +# Maintain the connection open and manage the messages. +# +output() +{ + local server="$1" + + while read message + do + message="${message% }" + + case "$message" in + PING* ) write "PONG${message#PING}" "$server" ;; + *'VERSION'* ) write "SYSTEM sshbio CTCP: VERSION netcat" ;; + * ) print_message "$server" "$message" ;; + esac + done +} + +# +# Parse server's message and print formatted message +# # message = [ ":" prefix SPACE ] command [ params ] crlf # prefix = servername / ( nickname [ [ "!" user ] "@" host ] ) # command = 1*letter / 3digit @@ -147,75 +156,66 @@ input() # SPACE = %x20 ; space character # crlf = %x0D %x0A ; "carriage return" "linefeed" # -output() +print_message() { - local SERVER="$1" NICK - - while read message + # Parse server's message + + local server="$1" msg="$2" prefix='' nick='' cmd='' \ + local params='' middle='' trail='' channel='' + + if [ -z "${msg##:*}" ] + then + prefix="${msg%% *}" + prefix="${prefix#:}" + [ -z "${prefix%%*!*}" ] && nick="${prefix%%!*}" + msg="${msg#* }" + fi + + cmd="${msg%% *}" + msg="${msg#* }" + params="${msg# *}" + middle="${msg% }" + middle="${msg%%:*}" + [ -z "${params%%*:*}" ] && trail="$( + printf '%s' "${params#*:}" | sed -r ' + s/([^]*)/\1/g + s/([^]*)/\1/g + s/ACTION(.*)/**\1 **/g + ')" + channel="${middle%% *}" + case "$channel" in + "$NICK" | '*' | '' ) channel=SERVER ;; + esac + + # Print formatted message + + case "$cmd" in + QUIT ) cmd='<' ;; + JOIN ) cmd='>' ;; + PRIVMSG ) cmd='|' ;; + NOTICE ) cmd='!' ;; + PART ) cmd='~' ;; + ERROR ) cmd='X' ;; + MODE ) cmd='+' ;; + NICK ) cmd='=' [ "$nick" = "$NICK" ] && NICK="$trail" ;; + esac + + # One line separator + if [ "$last" != "$nick" -o "$nick" = "$NICK" ] + then printf "%${NICK_LEN}s |\n" >> "$IRC_DIR/$SERVER/$channel" + fi + last="$nick" + + # Print the message + printf '%s\n' "$trail" | fold -s -w $((80 - $NICK_LEN - ${#cmd})) \ + | while read line do - local prefix='' nick='' command='' params='' middle='' - local trailing='' trail='' channel='' - message="${message% }" - - case "$message" in - PING* ) write "PONG${message#PING}" "$SERVER" ;; - *'VERSION'* ) printf 'VERSION!\n' 1>&2 ;; - * ) - if [ -z "${message##:*}" ] - then - prefix="${message%% *}" - prefix="${prefix#:}" - [ -z "${prefix%%*!*}" ] && nick="${prefix%%!*}" - message="${message#* }" - fi - - command="${message%% *}" - message="${message#* }" - params="${message# *}" - middle="${message% }" - middle="${message%%:*}" - [ -z "${params%%*:*}" ] && trailing="$( - printf '%s' "${params#*:}" | sed -r ' - s/([^]*)/\1/g - s/([^]*)/\1/g - ')" - channel="${middle%% *}" - [ \ - "$channel" = '*' \ - -o -z "$channel" \ - -o "$channel" = "$NICK" \ - ] && channel=SERVER - - case "$command" in - QUIT ) command='<' ;; - JOIN ) command='>' ;; - PRIVMSG ) command='|' ;; - NOTICE ) command='!' ;; - PART ) command='~' ;; - ERROR ) command='X' ;; - MODE ) command='+' ;; - NICK ) command='=' - [ "$nick" = "$NICK" ] && NICK="$trailing" - ;; - esac - - while [ "${trailing}" -gt 80 ] - do - trail="$(printf %s "$trail" \ - | cut -c 1-$((80 - $NICK_WIDTH)))" - - printf "%${NICK_WIDTH}s %1s %s\n" \ - "$nick" "$command" "${trail% *}" \ - >> "$IRC_DIR/$SERVER/$channel" - nick='' - done - - ;; - esac + printf "%${NICK_LEN}s %1s %s\n" "$nick" "$cmd" "$line" \ + >> "$IRC_DIR/$SERVER/$channel" + nick='' done } - # Algorythm #------------------------------------------------------------------------------- @@ -228,14 +228,17 @@ output() if [ "$(ps ax | grep -F "$IRC_DIR" | wc -l)" -gt 1 ] then cd "$IRC_DIR" - for file in $(find . -type f); do files="$files ${file#./}"; done + IFS="$NL" + for file in $(find . -type f); do files="$files$NL${file#./}"; done exec less -R $files + unset IFS else tail -f "$IRC_DIR/in" | input & printf 'This is an interactive interpreter. You can send irc commands. You can also send commands by writing them to '"$IRC_DIR/IN"'. For a list of available commands, send "/help". -If you run the `irc` again, you will get all channels displayed in `less -R`\n' +If you run the `irc` again, you will get all channels displayed in `less -R` +\n' while read command do printf '%s\n' "$command" > "$IRC_DIR/in" done diff --git a/s-nail/.mailrc b/s-nail/.mailrc @@ -124,40 +124,51 @@ set pipe-application/pdf="cat > \"${TMPDIR}/${NAIL_FILENAME_GENERATED}\"; \ define filter { # Mailing lists - move (cc "mmh@marmaro.de") ~/Mail/l.mmh - move (to "mmh@marmaro.de") ~/Mail/l.mmh + move (cc "mmh@marmaro.de") ~/Mail/l.mmh + move (to "mmh@marmaro.de") ~/Mail/l.mmh - move (from "@lobste.rs") ~/Mail/l.lobsters + move (from "@lobste.rs") ~/Mail/l.lobsters - move (from "@sfbi.fr") ~/Mail/l.bioinfo - move (to "@sfbi.fr") ~/Mail/l.bioinfo + move (from "@sfbi.fr") ~/Mail/l.bioinfo + move (to "@sfbi.fr") ~/Mail/l.bioinfo - move (from "@suckless.org") ~/Mail/l.suckless - move (to "@suckless.org") ~/Mail/l.suckless + move (from "@suckless.org") ~/Mail/l.suckless + move (to "@suckless.org") ~/Mail/l.suckless - move (from "@fsf.org") ~/Mail/l.fsf + move (from "@fsf.org") ~/Mail/l.fsf # move (from "lists@univ-rennes1.fr") ~/Mail/l.univ-rennes1 # Services - move (from "@reddit.com") ~/Mail/s.reddit + move (from "@reddit") ~/Mail/s.reddit - move (from "@github.com") ~/Mail/s.github + move (from "@github.com") ~/Mail/s.github - move (from "@twitter.com") ~/Mail/s.twitter + move (from "@twitter.com") ~/Mail/s.twitter - move (from "@bandcamp.com") ~/Mail/s.bandcamp + move (from "@bandcamp.com") ~/Mail/s.bandcamp - move (from "@keybase.io") ~/Mail/s.keybase + move (from "@keybase.io") ~/Mail/s.keybase - move (from "@wordpress.com") ~/Mail/s.wordpress + move (from "@wordpress.com") ~/Mail/s.wordpress + + move (from "google.com") ~/Mail/s.google # Billing - move (from "@free-mobile.fr") ~/Mail/b.free + move (from "@free-mobile.fr") ~/Mail/b.free + + move (from "@gandi.net") ~/Mail/b.gandi - move (from "@gandi.net") ~/Mail/b.gandi + move (from "@paypal") ~/Mail/b.paypal - move (from "@amazon") ~/Mail/b.amazon + move (from "@amazon") ~/Mail/b.amazon - move (from "@sncf") ~/Mail/b.transports + move (from "@sncf") ~/Mail/b.transports + move (from "@captaintrain.com") ~/Mail/b.transports + move (from "flixbus") ~/Mail/b.transports + move (from "megabus") ~/Mail/b.transports + move (from "ouigo") ~/Mail/b.transports + move (from "ouibus") ~/Mail/b.transports + move (from "ouibus") ~/Mail/b.transports + move (from "keolis.com") ~/Mail/b.transports }