Last active
April 2, 2019 22:51
-
-
Save madx/2710587 to your computer and use it in GitHub Desktop.
A minimalist IRC client in Bash. Could also be used as a bot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
[ -z "$channel" ] && channel="#bobot" | |
[ -z "$server" ] && server="irc.freenode.net" | |
[ -z "$port" ] && port="6667" | |
[ -z "$user" ] && user=$USER | |
function irc_session () { | |
# Login phase | |
echo "USER $user 0 * :$user" | |
echo "NICK $user" | |
echo "JOIN $channel" | |
# User input | |
while read message; do | |
if [ "$message" == ".QUIT." ]; then | |
break | |
fi | |
echo "PRIVMSG $channel :$message" | |
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