Skip to content

Instantly share code, notes, and snippets.

@cirrusUK
Created October 13, 2015 18:36
Show Gist options
  • Select an option

  • Save cirrusUK/791dc114e5f0ae98407a to your computer and use it in GitHub Desktop.

Select an option

Save cirrusUK/791dc114e5f0ae98407a to your computer and use it in GitHub Desktop.
#!/bin/bash
[ -z "$channel" ] && channel="#cirrus"
[ -z "$server" ] && server="irc.geekshed.net"
[ -z "$port" ] && port="6667"
[ -z "$user" ] && user="cirrusb0t"
[ -z "$message" ] && message="testing script"
function irc_session () {
# Login phase
echo "USER $user 0 * :$user"
echo "NICK $user"
echo "JOIN $channel"
#echo "$message"
# User input
while read message; do
if [ "$message" == ".QUIT." ]; then
break
fi
echo "PRIVMSG $channel :$message testing script"
done
# Termination
echo "QUIT"
}
function irc_read() {
while read -ru 3 line; do
echo "$line"
# Handle PINGs
if expr match "$line" "^PING" >/dev/null; then
servers=`echo $line"" | cut -d: -f2`
echo "PONG $servers" >&4
echo "PONG $servers"
fi
done
}
function finish() {
exec 3>&- 4>&-
kill $SESSION_PID >/dev/null 2>&1
kill $READ_PID >/dev/null 2>&1
kill $NC_PID >/dev/null 2>&1
}
coproc NC {
nc $server $port
}
exec 3>&${NC[0]}
exec 4>&${NC[1]}
trap finish INT
irc_session <&1 >&4 & # Feed nc with user input
SESSION_PID=$!
irc_read <&3 & # Handle pings from IRC
READ_PID=$!
wait $NC_PID
finish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment