Last active
October 19, 2018 09:53
-
-
Save bdrewery/6736306 to your computer and use it in GitHub Desktop.
IRC bot written in POSIX Shell using netcat
This file contains 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/sh | |
# | |
# Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org> | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions | |
# are met: | |
# 1. Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# 2. Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
# SUCH DAMAGE. | |
set -ef | |
NICK="poudriere" | |
CHANNEL="#poudriere" | |
SERVER="irc.sucks.net" | |
PORT=6667 | |
VERSION="Poudriere build bot (https://fossil.etoilebsd.net/poudriere/)" | |
REALNAME="${VERSION}" | |
STATUS_SOCKET=/tmp/poudriere.sock | |
HOSTNAME="$(hostname -s)" | |
CTCP=$(echo -e "\001") | |
send() { | |
echo "[->] $@" | |
echo "$@" >> ${OUT} | |
} | |
send_ctcp_reply() { | |
local nick keyword msg | |
echo "[CTCP->] $@" | |
nick="$1" | |
keyword="$2" | |
shift 2 | |
msg="$@" | |
notice ${nick} "${CTCP}${keyword} ${msg}${CTCP}" | |
} | |
notice() { | |
local nick msg | |
nick="$1" | |
shift | |
msg="$@" | |
send "NOTICE ${nick} :${msg}" | |
} | |
privmsg() { | |
local nick msg | |
nick="$1" | |
shift | |
msg="$@" | |
send "PRIVMSG ${nick} :${msg}" | |
} | |
handle_ctcp() { | |
local nick msg keyword | |
nick="$1" | |
shift | |
# Trim out \001 | |
msg="${@}" | |
msg="${msg%${CTCP}}" | |
msg="${msg#${CTCP}}" | |
set -- "${msg}" | |
keyword="$1" | |
shift | |
msg="$@" | |
case "${keyword}" in | |
VERSION) | |
send_ctcp_reply ${nick} ${keyword} "${VERSION}" | |
return 0 | |
;; | |
esac | |
} | |
handle_privmsg() { | |
local from msg nick target reply_target | |
from="${1#:}" | |
nick="${from%!*}" | |
target="${3}" | |
shift 3 | |
msg="${@#:}" | |
case "${msg}" in | |
${CTCP}*${CTCP}) | |
handle_ctcp ${nick} "${msg}" | |
return 0 | |
;; | |
esac | |
#[<-] :nick!user@host PRIVMSG #channel :text | |
#[<-] :nick!user@host PRIVMSG nick :text | |
reply_target="${target}" | |
[ "${target}" = "${MYNICK}" ] && reply_target="${nick}" | |
echo "<${from}> ${msg}" | |
case "${msg}" in | |
"!poudriere status") | |
poudriere status | while read line; do | |
privmsg ${reply_target} "${nick}: [${HOSTNAME}] ${line}" | |
done | |
;; | |
*) | |
;; | |
esac | |
} | |
recv() { | |
echo "[<-] $@" | |
# [<-] PING :irc.servercentral.net | |
case $1 in | |
:*) | |
;; | |
PING) | |
send "PONG $2" | |
return 0 | |
;; | |
esac | |
case $2 in | |
PRIVMSG) | |
handle_privmsg "$@" | |
return 0 | |
;; | |
433) # :server 433 * test :Nickname is already in use. | |
send "NICK ${4}_" | |
return 0 | |
;; | |
001) # :server 001 test :Welcome to IRC | |
MYNICK="$3" | |
send "JOIN ${CHANNEL} ${CHANNEL_KEY}" | |
return 0 | |
;; | |
*) | |
;; | |
esac | |
} | |
poudriere_socket() { | |
rm -f ${STATUS_SOCKET} | |
nc -klU ${STATUS_SOCKET} | while read cmd mastername line; do | |
case "$cmd" in | |
status) | |
privmsg "${CHANNEL}" \ | |
"[${HOSTNAME}][${mastername}] Status: ${line}" | |
;; | |
esac | |
done | |
} | |
OUT=$(mktemp) | |
# http://stackoverflow.com/a/15557994/285734 | |
usr1_handler() { | |
exit | |
} | |
exit_handler() { | |
[ -n "${OUT}" ] && rm -f ${OUT} 2>/dev/null | |
# Kill children and nc(1) | |
kill -USR1 -$$ | |
} | |
trap exit_handler EXIT INT TERM | |
trap usr1_handler USR1 | |
main() { | |
[ -n "${PASS}" ] && send "PASS ${PASS}" | |
send "NICK ${NICK}" | |
send "USER ${NICK} localhost ${SERVER} :${REALNAME}" | |
# Start the listen socket for poudriere | |
poudriere_socket & | |
tail -F ${OUT} | nc ${SERVER} ${PORT} | while read raw; do | |
raw="${raw%$'\n'}" | |
raw="${raw%$'\r'}" | |
recv ${raw} | |
done | |
} | |
main |
A root process writes to /tmp/poudriere.sock with nc(1) as well which relays the information to IRC:
echo "status ${MASTERNAME} ${status}" | nc -U /tmp/poudriere.sock
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a prototype for poudriere IRC support. It will be moved to https://fossil.etoilebsd.net/poudriere/.