dot

packages and services management
Log | Files | Refs | README

commit 45084abce57c2de1c31c40f089cd095eaf10bcaa
parent bcec21432ada878ac53c4eb4d17359ebc4b19f40
Author: josuah <mail@josuah.net>
Date:   Fri, 10 Feb 2017 22:41:46 +0100

Added ii filter

Diffstat:
M.config/dvtm/config.h | 2+-
R.local/share/build/build -> .local/share/build/self | 0
M.profile | 31+++++++++++++++++--------------
Mbin/ii-client | 51++++++++++++++++++---------------------------------
Abin/ii-filter | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Abin/ii-filter2 | 40++++++++++++++++++++++++++++++++++++++++
Mbin/ii-server | 61+++++++++++++++++++++++++++----------------------------------
Abin/irc | 280+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 438 insertions(+), 82 deletions(-)

diff --git a/.config/dvtm/config.h b/.config/dvtm/config.h @@ -68,7 +68,7 @@ static const ColorRule colorrules[] = { /* tags ----------------------------------------------------------------------*/ -const char tags[][8] = { "1", "2", "3", "4 mail", "5 irc", "6 music" }; +const char tags[][8] = { "1", "2", "3 doc", "4 mail", "5 irc", "6 music" }; /* curses attributes for the currently selected tags */ #define TAG_SEL (COLOR(DEFAULT) | A_REVERSE) diff --git a/.local/share/build/build b/.local/share/build/self diff --git a/.profile b/.profile @@ -112,11 +112,11 @@ color() extract() { case "$1" in - ( *.tar.gz ) tar xvzf "$1" ;; - ( *.tgz ) tar xvzf "$1" ;; - ( *.tar.bz2 ) tar xvjf "$1" ;; - ( *.tbz2 ) tar xvjf "$1" ;; - ( *.tar ) tar xvf "$1" ;; + ( *.tar.gz ) tar -xzf "$1" ;; + ( *.tgz ) tar -xzf "$1" ;; + ( *.tar.bz2 ) tar -xjf "$1" ;; + ( *.tbz2 ) tar -xjf "$1" ;; + ( *.tar ) tar -xf "$1" ;; ( *.bz2 ) bunzip2 "$1" ;; ( *.rar ) unrar x "$1" ;; ( *.zip ) unzip "$1" ;; @@ -130,9 +130,8 @@ extract() } -# # Sudo, sudo, sudo, sudo, sudo, screen brightness, sudo, sudo... -# + light() { printf '%s\n' "$1" | sudo tee "$( @@ -142,18 +141,14 @@ light() } -# -# Why sudo to mount a pendrive! -# mnt() { sudo mount -o uid="$USER" -o rw "${1-/dev/sdb1}" "${2-/mnt}" } -# # Works only in TTY and with sudo, unfortunately. -# + rotate() { case "$1" in @@ -162,9 +157,8 @@ rotate() } -# # FrameBuffer Video player based on mplayer -# + fbv() { mplayer -vm -vo fbdev2 "$1" -vf scale -zoom -x "$( @@ -185,6 +179,15 @@ screencast() } +rfc() +{ + wget -q -O- http://www.ietf.org/rfc/rfc"$1".txt | + awk -v RS='\n\n[^\n]*\n\f\n[^\n]*\n\n\n' '{ print; }' | + less +} + + + main() { [ "$TERM" = 'linux' ] && diff --git a/bin/ii-client b/bin/ii-client @@ -3,11 +3,11 @@ II_DIR="$HOME/.cache/irc" +E="$(printf '\033')" -# # Read one character from /dev/tty in raw mode. -# + get_char() { stty -echo raw @@ -51,37 +51,20 @@ key() ;; ( "$(printf '\014')" ) # Ctrl-L - less out + ii-filter < out | less -R print_screen ;; esac - print_prompt + prompt return 0 } -# -# Manage characters typed by user. -# -input() -{ - printf '' > input - - print_prompt - - while key="$(get_char)" - do - key "$key" || break - done -} - - -# # Print a prompt with the current input -# -print_prompt() + +prompt() { [ -f input ] && IFS='' read -r input < input @@ -97,9 +80,8 @@ print_prompt() } -# # Update the buffer lines and prompt -# + print_screen() { size="$(stty -F /dev/tty size)" @@ -107,8 +89,8 @@ print_screen() stty sane printf '\033[H\033[J' - [ -f out ] && tail -n "${size#* }" out - print_prompt + [ -f out ] && tail -n "${size#* }" out | ii-filter + prompt stty raw } @@ -116,17 +98,20 @@ print_screen() main() { - trap 'print_screen' WINCH - + printf '' > input print_screen - (tail -n 0 -f out & printf '%s\n' $! > pid) | while IFS='' read -r line + (tail -n 0 -f out & printf '%s\n' $! > pid) | ii-filter | + while IFS='' read -r line do - printf '\a\r\033[K%s\n' "$line" - print_prompt + printf '\r\033[K%s\n' "$line" + prompt done & - input + while key="$(get_char)" + do + key "$key" || break + done read pid < pid rm -f pid input diff --git a/bin/ii-filter b/bin/ii-filter @@ -0,0 +1,55 @@ +#!/bin/sh + +busybox awk -v COLS="${1:-80}" 'BEGIN { + NICK_COL = 14; + TEXT_COL = COLS - NICK_COL - length(" | "); +} + +{ + sub(/[0-9]*-[0-9]*-[0-9]* /, ""); + + date = $0; + sub(/ .*/, "", date); + sub(/^[^ ]* /, "", $0 ); + + if (match($0, /^[-=<#*@+]/)) { + nick = $0; + sub(/ .*/, "", nick); + sub(/^[^ ]* /, "", $0 ); + sub(/</, "", nick); + sub(/>/, "", nick); + } else { + nick = "-!-"; + } + + if (nick != lastnick) + printf("%-" NICK_COL "s │\033[1;30m%" TEXT_COL "s\033[m\n", + "", date); + + printf("%" NICK_COL "s │ ", + (nick != lastnick && nick != "-!-") ? nick : ""); + + for (first = 1; length($0) >= TEXT_COL; first = 0) { + line = substr($0, 1, TEXT_COL); + sub(/ [^ ]*$/, "", line); + $0 = substr($0, length(line) + 2); + + if (!first) + printf("%" NICK_COL "s │ ", ""); + + if (nick == "-!-") + printf("\033[1;30m"); + + printf("%s\033[m\n", line); + } + + if (!first) + printf("%" NICK_COL "s │ ", ""); + + if (nick == "-!-") + printf("\033[1;30m"); + + printf("%s\033[m\n", $0); + + lastnick = nick; +}' diff --git a/bin/ii-filter2 b/bin/ii-filter2 @@ -0,0 +1,40 @@ +#!/bin/sed -rf + +# Remove date +s/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} // + +# Chat events +s/^-!- (.*)\(([^)]*)\) has joined .*/<\1>\x1b[0;1;32m>\x1b[0m \x1b[37m\2\x1b[0m/ +s/^-!- (.*)\(([^)]*)\) has left .*/<\1>\x1b[0;1;31m<\x1b[0m \x1b[37m\2\x1b[0m/ +s/^-!- (.*) changed mode\/.* -> (.*)/<\1>\x1b[0;1;34m=\x1b[0m \2\x1b[0m/ +s/^-!- (.*) changed nick to (.*)/<\1>\x1b[0;1;36m=\x1b[0m \x1b[1m\1\x1b[0m -> \x1b[1m\2\x1b[0m/ +s/^(.*) Nickname is already in use./<\1>\x1b[0;1;31m!\x1b[0m \x1b[37;1m\1\x1b[37m already in use\x1b[0m/ +s/^(.*) Erroneous Nickname/<\1>\x1b[0;1;31m!\x1b[0m \x1b[37m\1\x1b[37m wrong nickname\x1b[0m/ +s/^\+(.*)/ \x1b[0;1;33m+\x1b[0m \1/ +s/^-!- (.*)/ \x1b[0m│\x1b[1;33m \1\x1b[0m/ +s/^\*\*\* (.*)/ \x1b[0m│ \x1b[37m\1\x1b[0m/ + +# Infos +s/^-!- (.*)/ \x1b[0m│ \x1b[37m\1\x1b[0m/ + +# Motd +s/^- (.*)/\x1b[0m │ \x1b[37m\1\x1b[0m/ +s/End of \/MOTD command./\x1b[0m └───────────────┬──────────────────────────────────┤ MOTD/ + +# Names +s/ End of \/NAMES list\./\n\x1b[0m──────────────────┬──────────────────────────────────┤ NAMES/ + +# Right alignment +/<[^>]*>/ { + :x + s/^([^>]{0,16}>.*)$/ \1/g + tx +} + +# Nicks +s/( *)<([^>]*)> .ACTION (.*)/\x1b[0m\1 \x1b[1m\2\x1b[0m ┊ \x1b[35m\3\x1b[0m/ +s/( *)<([^>]*)> ([^ :]*): /\x1b[0m\1 \x1b[1m\2\x1b[0m ┊ \x1b[1;36m\3:\x1b[0m / +s/( *)<([^>]*)> /\x1b[0m\1 \x1b[1m\2\x1b[0m ┊ / + +# Clean remaining < .* > +s/^( *)<([^>]*)>/\x1b[0m\1 \x1b[1m\2\x1b[0m / diff --git a/bin/ii-server b/bin/ii-server @@ -1,35 +1,28 @@ #!/bin/sh -# connect to an irc server with ii and maintain connection active - - -DIR="$HOME/irc" - - -connect() -{ - (exit 2) - while [ "$?" -eq 2 ] - do - nohup ii -i "$DIR" -p 6667 "$@" > "$DIR/errors" - done & -} - - -main() -{ - mkdir -p "$DIR" - - pgrep -fla '^ii ' - printf '\nDo you want to start ii in the background ? [Y/n] ' - - read -r answer - - case "$answer" in - ( y | Y | '' ) - connect "$@" - ;; - esac -} - - -main "$@" +# connect to my irc server and channels + +printf 'Password: ' +IFS='' read -r IIPASS +export IIPASS + +ii -n josuah -k IIPASS -s irc.freenode.net & +tee -a ~/irc/irc.freenode.net/in << EOF +/privmsg nickserv IDENTIFY IIPASS +/j ##networking +/j #2f30 +/j #cat-v +/j #breizh-entropy +EOF + +ii -n josuah -k IIPASS -s irc.oftc.net & +tee -a ~/irc/irc.oftc.net/in << EOF +/privmsg nickserv IDENTIFY IIPASS +/j #suckless +/j #crypto-party +EOF + +ii -n josuah -k IIPASS -s unix.chat -e 6697 & +tee -a ~/irc/unix.chat/in << EOF +/privmsg nickserv IDENTIFY IIPASS +/j #unix +EOF diff --git a/bin/irc b/bin/irc @@ -0,0 +1,280 @@ +#!/bin/sh +# __ +# \/ _____ ____ +# /\ / ____\/ ___\ +# / // / / /__ +# \/ \/ \____\ 2016-09-18 +#=============================================================================== +# Irc client in 300 lines of shell script +# +# With help of http://xero.nu, inspired by http://tools.suckless.org/ii + +NICK="${NICK-$(whoami)}" +FULLNAME='With netcat and a script as a client' +NICK_LEN=12 +NL=' +' CR="$(printf '\015')" C_A="$(printf '\001')" +DIR="$HOME/.cache/irc" + + +# FUNCTIONS +#------------------------------------------------------------------------------- + +# +# Write a message to a server +# +write() +{ + local srv="$1" msg="$2" + + printf '\r%s\n' "$msg" >> "$srv/IN" +} + +# +# Start netcat, identify the client and user, and monitor the response. +# +connect() +{ + local srv="$1" + [ -f "$srv/NICK" ] && user="$(tail "$srv/NICK")" + [ -z "$user" ] && user="$NICK" + + mkdir -p "$srv" + [ -p "$srv/IN" ] || mkfifo "$srv/IN" + + printf "$user" > "$srv/NICK" + + # Connect to irc server, send command from the pipe, parse the messages. + tail -f "$srv/IN" | nc "$srv" 6667 | tee -a ~/log | while IFS= read -r msg + do parse_message "$srv" + done & + + write "$srv" "NICK $user" + write "$srv" "USER $user +i * :$FULLNAME" +} + +# +# Parse command from user input and print IRC messages. +# +input() +{ + local srv chan arg user + + printf 'irc: Logs started in %s\n\n \033[1m>\033[0m ' "$DIR" 1>&2 + + while read cmd + do + [ "$srv" ] && user="$(tail "$srv/NICK")" + + # Parse command and arguments + if [ -z "${cmd##/*}" ] + then arg="${cmd#* }" cmd="${cmd#/}" cmd="${cmd%% *}" + else arg="$cmd" cmd='' + fi + cmd="$(printf %s "$cmd" | tr [a-z] [A-Z])" + + # Write message according to command + case "$cmd" in + C | CONNECT | S | SERVER ) + srv="$arg" chan='' + if [ -z "$(ps ax -o args | grep "^nc $srv 6667")" ] + then connect "$srv" & + fi + ;; + M | MSG | PRIVMSG ) + arg="${arg%% *} :${arg#* }" + write "$srv" "PRIVMSG $arg" + print_msg "$user" "$arg" PRIVMSG "$srv" >> "$srv/$arg" + ;; + J | JOIN ) + chan="$arg" + write "$srv" "JOIN $arg" + ;; + ME | ACTION ) + write "$srv" "PRIVMSG $chan :${C_A}ACTION ${arg}${C_A}" + ;; + TOPIC | LIST | MOTD ) + 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 + write "$srv" "PRIVMSG NickServ :IDENTIFY $arg" + ;; + '' ) + write "$srv" "PRIVMSG $chan :$arg" + print_msg "$user" "$arg" PRIVMSG "$srv" >> "$srv/$chan" + ;; + * ) + write "$srv" "$cmd $arg" + ;; + esac + + printf ' \033[30;1m%s %s\033[0;1m > \033[0m' "$srv" "$chan" 1>&2 + done +} + +# +# Maintain the connection open and print formatted message +# +# message = [ ":" prefix SPACE ] command [ params ] crlf +# prefix = servername / ( nickname [ [ "!" user ] "@" host ] ) +# command = 1*letter / 3digit +# params = *14( SPACE middle ) [ SPACE ":" trail ] +# =/ 14( SPACE middle ) [ SPACE [ ":" ] trail ] +# +# nospcrlfcl = %x01-09 / %x0B-0C / %x0E-1F / %x21-39 / %x3B-FF +# ; any octet except NUL, CR, LF, " " and ":" +# middle = nospcrlfcl *( ":" / nospcrlfcl ) +# trail = *( ":" / " " / nospcrlfcl ) +# +# SPACE = %x20 ; space character +# crlf = %x0D %x0A ; "carriage return" "linefeed" +# +parse_message() +{ + local srv="$1" + + # Parse server message + local prefix='' nick='' cmd='' trail='' sep='' + local params='' middle='' chan='' user="$(tail "$srv/NICK")" + + # Remove the useless '\r' character + msg="${msg#$CR}" msg="${msg%$CR}" + + # Prefix (and nick) + if [ -z "${msg##:*}" ] + then + prefix="${msg%% *}" prefix="${prefix#:}" + [ -z "${prefix%%*!*}" ] && nick="${prefix%%!*}" + msg="${msg#* }" + fi + + # Command + cmd="${msg%% *}" msg="${msg#* }" + + # Parameters + params="${msg# *}" + case "$params" in + :* ) + trail="${params#:}" + ;; + *:* ) + trail="${params#*:}" middle="${params%% :*}" + ;; + * ) + middle="$params" + ;; + esac + + # Channel and extra command action + case "$cmd" in + JOIN | PART ) + chan="${middle:-$trail}" + ;; + PRIVMSG ) + [ "$middle" = "$user" ] && chan="$nick" || chan="$middle" + ;; + NICK ) + chan=SERVER + [ "$nick" = "$user" ] && printf '%s\n' "$trail" > "$srv/NICK" + ;; + QUIT | NOTICE | [0-9][0-9][0-9] | * | '' ) + chan=SERVER + ;; + esac + + # Execute action + case "$cmd" in + PING ) + write "$srv" "PONG $trail" + ;; + QUIT | NICK ) + grep -l -r "$nick" | while read chan + do + chan="${chan#$srv/}" + print_msg "$nick" "$trail" "$cmd" "$srv" "$chan" \ + >> "$srv/$chan" + done + ;; + *) + print_msg "$nick" "$trail" "$cmd" "$srv" "$chan" >> "$srv/$chan" + ;; + esac + + # Bot actions + case "$trail" in + 'VERSION'* ) write "$srv" "PRIVMSG $nick :VERSION nc + sh" ;; + esac +} + +# +# Print formatted message +# +print_msg() +{ + local nick="$1" trail="$2" cmd="$3" srv="$4" chan="$5" + local user="$(tail "$srv/NICK")" last_cmd last_nick + + case "$cmd" in + QUIT ) cmd='<' ;; + JOIN ) cmd='>' ;; + PRIVMSG ) cmd='|' ;; + NOTICE ) cmd='!' ;; + PART ) cmd='~' ;; + ERROR ) cmd='X' ;; + MODE ) cmd='+' ;; + NICK ) cmd='=' ;; + esac + + # 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 $((80 - $NICK_LEN - ${#cmd})) \ + | while read line + do + printf "%${NICK_LEN}s %1s %s\n" "$nick" "$cmd" "$line" + nick='' + done +} + + +# ALGORYTHM +#------------------------------------------------------------------------------- + +# Kill background jobs and delete named pipes while quitting + +# Prepare the irc log directory +[ -d "$DIR" ] || mkdir -p "$DIR" +cd "$DIR" +printf '%s\n' "$NICK" > "NICK" + +case "$1" in +-i ) + trap 'find . -name IN -exec rm {} ";"; kill -9 0' INT EXIT + input + ;; +-o ) + IFS="$NL" + less $(find . -mindepth 2 -type f ! -name NICK) + unset IFS + ;; +* ) + printf 'USAGE:\tirc [-i|-o]\n' + ;; +esac