commit f3ddbd2d706ed4015cbcd992276518c64b88cf6d
parent 4f389597a6533b6d9448bc1c887210f88986962c
Author: sshbio <jd@ssh.bio>
Date: Sun, 31 Jul 2016 19:39:14 +0200
Updated and fixed irc
Diffstat:
M | bin/irc | | | 130 | ++++++++++++++++++++----------------------------------------------------------- |
1 file changed, 32 insertions(+), 98 deletions(-)
diff --git a/bin/irc b/bin/irc
@@ -9,10 +9,13 @@
NICK="${NICK-$USER}"
FULLNAME='With netcat and a script as a client'
IRC_DIR="$HOME/.cache/irc"
-FORMAT='(%-20s) %14s %1s %s\n'
+FORMAT='%14s %1s %s\n'
LINES="$(stty size | cut -d ' ' -f 2)"
-#===[ SERVER INTERACTION ]======================================================
+
+# Server interaction
+#-------------------------------------------------------------------------------
+
#
# For each server, the #servers variable contains a list of servers and
# associated pid (NC, TAIL). All element is assumed to not contain any space.
@@ -35,22 +38,22 @@ write()
open_connection()
{
local server="$1"
-
- local in="$IRC_DIR/$server/in"
- local out="$IRC_DIR/$server/out"
+ local in="$IRC_DIR/$server/in" out="$IRC_DIR/$server/out"
mkdir -p "$IRC_DIR/$server"
[ -p "$in" ] || mkfifo "$in"
printf '' > "$IRC_DIR/$server/channel"
printf '' > "$IRC_DIR/$server/channels"
- tail -f "$in" | nc "$server" 6667 | output "$server" >> "$out" &
+ tail -f "$in" | nc "$server" 6667 | output "$server" &
write "NICK $NICK" "$server"
write "USER pipe +i * :$FULLNAME" "$server"
}
-#===[ INPUT ]===================================================================
+
+# Input
+#-------------------------------------------------------------------------------
#
# Manage the user input. The $in file act as an history for the input.
#
@@ -67,44 +70,11 @@ get_char()
}
#
-# Parse the key from the input and append the resulting string in $IRC_DIR/input
-#
-parse_input()
-{
- local input="$IRC_DIR/input"
-
- while key="$(get_char)"
- do
- case "$key" in
- '
')
- parse_command "$(tee < "$IRC_DIR/input")"
- printf '' > "$input"
- ;;
- '') clear 1>&2; exit 0 ;;
- '') sed -i '$ s/.$//' "$input" ;;
- '') sed -i '$ s/[^ ]* *$//' "$input" ;;
- '') printf '' > "$input" ;;
- '') next_server ;;
- '') next_channel ;;
- '') prev_channel ;;
- '') ${PAGER-less -R} "$IRC_DIR/$(get_server)/out" ;;
- *)
- key="$(printf '%s' "$key" | tr -cd '\40-\176\002\037')"
- printf '%s' "$key" >> "$input"
- ;;
- esac
-
- display_input
- done
-}
-
-#
# Parse command from user input and execute corresponding actions.
#
parse_command()
{
- local command="$(tee < "$IRC_DIR/input")" argument
- local server="$(get_server)" channel="${get_channel}"
+ local command="$1" server="$(get_server)" channel="${get_channel}"
if [ -z "${command##/*}" ]
then
@@ -147,7 +117,9 @@ parse_command()
esac
}
-#===[ OUTPUT ]==================================================================
+
+# Output
+#-------------------------------------------------------------------------------
#
# Manages which channel to show and update the interface
#
@@ -182,9 +154,9 @@ output()
local params='' middle='' trailing='' channel=''
case "$message" in
- 'PING *') write "PONG${message#PING}" "$server" ;;
- *)
- printf '%s\n' "$message" 1>&2
+ PING* ) write "PONG${message#PING}" "$server" ;;
+ *'ACTION'* ) printf 'ACTION!!!' 1>&2 ;;
+ * )
if [ -z "${message##:*}" ]
then
prefix="${message%% *}"
@@ -197,12 +169,13 @@ output()
message="${message#* }"
params="${message# *}"
middle="${message%% :*}"
- [ -z "${params%%* :*}" ] && trailing="$(printf '%s' \
- "${params#* :}" | sed -r '
+ [ -z "${params%%* :*}" ] && trailing="$(
+ printf '%s' "${params#* :}" | sed -r '
s/([^]*)/[1m\1[0m/g
s/([^]*)/[4m\1[0m/g
')"
- channel="${middle##* }"
+ channel="${middle%% *}"
+ [ "$channel" = '*' ] && channel='root'
case "$command" in
QUIT ) command='<'; channel="$(get_channel)" ;;
@@ -215,52 +188,16 @@ output()
MODE ) command='+' ;;
esac
- printf '\r\033[K' 1>&2
- printf "$FORMAT" "$channel" "$nick" "$command" "$trailing"
- if [ "$server" = "$(get_server)" \
- -a \( "$channel" = "$(get_channel)" \
- -o -z "$channel" \) ]
- then printf "$FORMAT" "$channel" "$nick" "$command" "$trailing" 1>&2
- fi
-
- display_input
- display_status_bar
+ printf "$FORMAT" "$nick" "$command" "$trailing" \
+ | tee # "$out"
;;
esac
done
}
-#===[ INTERFACE ]===============================================================
-#
-# Display the user input as he type it or update it while new message shift it
-#
-display_input()
-{
- printf '\r\033[K> %s' 1>&2
- tee < "$IRC_DIR/input" 1>&2
-}
-
-#
-# Display a status bar on top
-#
-display_status_bar()
-{
- printf '\033[s\033[1;0f\033[7m\033[K%s > %s \033[0m\033[u' \
- "$(get_server)" "$(get_channel)" 1>&2
-}
-
-#
-# Update the buffer lines of the current server and channel
-#
-display_buffer()
-{
- printf '\r\033[K'
- tail -n "$LINES" "$IRC_DIR/$(get_server)/out"
- display_status_bar
-}
-
-#===[ SERVERS ]=================================================================
+# Servers
+#-------------------------------------------------------------------------------
set_server()
{
@@ -284,11 +221,11 @@ next_server()
local next="$(get_servers | sed -n "/$(get_server)/ { n; p }")"
[ -z "$next" ] && next="$(get_servers | sed 1q)"
set_server "$next"
-
- display_buffer
}
-#===[ CHANNELS ]================================================================
+
+# Channels
+#-------------------------------------------------------------------------------
set_channel()
{
@@ -325,8 +262,6 @@ next_channel()
[ -z "$next" ] && next="$(printf '%s' "$channels" | sed '1q')"
[ -z "$next" ] || set_channel "$next"
fi
-
- display_buffer
}
prev_channel()
@@ -341,11 +276,11 @@ prev_channel()
| sed -n '$ p')"
[ -z "$prev" ] || set_channel "$prev"
fi
-
- display_buffer
}
-#===[ ALGORYTHM ]===============================================================
+
+# Algorythm
+#-------------------------------------------------------------------------------
# Remove irc dir
[ -d "$IRC_DIR" ] || mkdir -p "$IRC_DIR"
@@ -358,5 +293,4 @@ printf '' > "$IRC_DIR/server"
trap 'kill -9 0' INT EXIT
# Start listenning the user input
-display_input
-parse_input
+while read command; do parse_command "$command"; done