commit 1bd759533215da854582b66792316ae180b56e4c
parent a4efdee8d9cb4ed031f1aefdd3edc281313a9580
Author: sshbio <jd@ssh.bio>
Date: Sun, 14 Aug 2016 23:35:16 +0200
irc: Could connect to irc.nixers.net with it
Diffstat:
M | bin/irc | | | 76 | +++++++++++++++++++++++++++++++++++++++++----------------------------------- |
1 file changed, 41 insertions(+), 35 deletions(-)
diff --git a/bin/irc b/bin/irc
@@ -3,15 +3,15 @@
# \/ _____ ____
# /\ / ____\/ ___\
# / // / / /__
-# \/ \/ \____\ - Irc client in 225 lines of shell script
+# \/ \/ \____\ - Irc client in 233 lines of shell script
#===============================================================================
NICK="${NICK-$USER}"
FULLNAME='With netcat and a script as a client'
-DIR="$HOME/.cache/irc"
NICK_LEN=14
NL='
' CR='
'
+DIR="$HOME/.cache/irc"
# Functions
@@ -24,7 +24,7 @@ write()
{
local srv="$1" msg="$2"
- printf '\r%s\n' "$msg" | tee -a ~/log >> "$DIR/$srv/IN"
+ printf '\r%s\n' "$msg" >> "$srv/IN"
}
#
@@ -32,12 +32,12 @@ write()
#
connect()
{
- local srv="$1" user="$(tail "$DIR/NICK")"
+ local srv="$1" user="$(tail "NICK")"
- mkdir -p "$DIR/$srv"
- [ -p "$DIR/$srv/IN" ] || mkfifo "$DIR/$srv/IN"
+ mkdir -p "$srv"
+ [ -p "$srv/IN" ] || mkfifo "$srv/IN"
- tail -f "$DIR/$srv/IN" | nc "$srv" 6667 | output "$srv" &
+ tail -f "$srv/IN" | nc "$srv" 6667 | output "$srv" &
write "$srv" "NICK $user"
write "$srv" "USER $(whoami) +i * :$FULLNAME"
@@ -50,19 +50,23 @@ 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
- user="$(tail "$DIR/NICK")"
- arg="$cmd"
+ user="$(tail "NICK")"
+
+ # Parse command and arguments
if [ -z "${cmd##/*}" ]
- then arg="${arg#* }" cmd="${cmd#/}" cmd="${cmd%% *}"
- else 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"
+ srv="$arg" chan=''
if [ -z "$(ps ax -o args | grep "^nc $srv 6667")" ]
then connect "$srv" &
fi
@@ -90,12 +94,14 @@ input()
'' )
write "$srv" "PRIVMSG $chan :$arg"
print_msg "$user" "$arg" PRIVMSG yes \
- >> "$DIR/$srv/$chan"
+ >> "$srv/$chan"
;;
* )
write "$srv" "$cmd $arg"
;;
esac
+
+ printf ' \033[30;1m%s %s\033[0;1m > \033[0m' "$srv" "$chan" 1>&2
done
}
@@ -118,7 +124,7 @@ input()
#
output()
{
- local srv="$1" user="$(tail "$DIR/NICK")"
+ local srv="$1" user="$(tail "NICK")"
local cmd params mid chan last check
while read msg
@@ -160,8 +166,8 @@ output()
write "$srv" "PONG $trail"
;;
* )
- print_msg "$nick" "$trail" "$cmd" "$sep" \
- >> "$DIR/$srv/$chan"
+ print_msg "$nick" "$trail" "$cmd" "$sep" "$user" \
+ >> "$srv/$chan"
;;
esac
done
@@ -172,7 +178,7 @@ output()
#
print_msg()
{
- local nick="$1" trail="$2" cmd="$3" sep="$4" user="$(tail "$DIR/NICK")"
+ local nick="$1" trail="$2" cmd="$3" sep="$4" user="$5"
case "$cmd" in
QUIT ) cmd='<' ;;
@@ -183,7 +189,7 @@ print_msg()
ERROR ) cmd='X' ;;
MODE ) cmd='+' ;;
NICK ) cmd='='
- [ "$nick" = "$user" ] && printf '%s\n' "$trail" > "$DIR/NICK"
+ [ "$nick" = "$user" ] && printf '%s\n' "$trail" > "NICK"
;;
esac
@@ -203,23 +209,23 @@ print_msg()
#-------------------------------------------------------------------------------
# Kill background jobs and delete named pipes while quitting
-trap 'find "$DIR" -name IN -exec rm {} ";"; kill -9 0' INT EXIT
# Prepare the irc log directory
[ -d "$DIR" ] || mkdir -p "$DIR"
-printf '%s\n' "$NICK" > "$DIR/NICK"
-
-if [ "$(ps ax -o args | grep -F "$DIR/IN" | wc -l)" -le 1 ]
-then
- # Start interactive session
- [ -p "$DIR/IN" ] || mkfifo "$DIR/IN"
- tail -f "$DIR/IN" | input &
- printf '> ' 1>&2
- while read cmd
- do
- printf '> ' 1>&2
- printf '%s\n' "$cmd" >> "$DIR/IN"
- done
-else
- IFS="$NL"; cd "$DIR"; less -R $(find . -mindepth 2 -type f); unset IFS
-fi
+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 -R $(find . -mindepth 2 -type f)
+ unset IFS
+ ;;
+* )
+ printf 'USAGE:\tirc [-i|-o]\n'
+ ;;
+esac