#! /bin/bash # Bot en shell # Copyright (c) Tripa, 2002 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along as file 'COPYING'; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # # bash traine habituellement sur RezoSup (irc://irc.rezosup.org) # sur les chans #linux et #botstory VERSION="shellbot alpha[R1] (bash $BASH_VERSION)" # Sortie propre function sigint { echo "QUIT :http://www.gnu.org/software/bash/" } # Initialisation un poil plus propre function init { echo "USER $mynick +i * :shellbot" if [ -n $pass ]; then echo "PASS $pass"; fi echo "NICK $mynick" # ça c'est pas propre mais ça restera pour le moment echo "JOIN #dcc,#botstory" } # Décomposition des lignes de protocole function parse { set ${REPLY%$'\r'} # détection du préfixe if [ ${1:0:1} = : ]; then host=${1:1} nick=${host%\!*} host=${host#$nick\!} user=${host%@*} host=${host#*@} shift else nick=.server. unset user host fi # scission des paramètres command=$1; shift unset parameter; local n=0 while [ $# -ge 1 -a "${1:0:1}" != : ]; do parameter[$n]=$1 shift; let n++ done [ $# -eq 0 ] && return local tmp=$* parameter[$n]=${tmp:1} } # Emission de CTCP function ctcp { local dest=$1; shift echo -e "PRIVMSG $dest :\001$*\001" } function nctcp { local dest=$1; shift echo -e "NOTICE $dest :\001$*\001" } # Emission de messages function msg { local dest=$1; shift echo "PRIVMSG $dest :$*" } function notice { local dest=$1; shift echo "NOTICE $dest :$*" } # Réception de message normal function m_msg { local orig=$1; shift echo "[$nick->${parameter[0]}] - ${parameter[1]}" >&2 # slap à la demande if [ "${parameter[1]% *}" = "$mynick: slappe" ]; then echo -e "PRIVMSG ${parameter[0]} :\001ACTION" \ "slaps ${parameter[1]##* }\001" fi # commandes du propriétaire if [ "${parameter[0]}" = "$mynick" -a "$user" = "tripa" \ -a "$host" = "staff.supelec.rezosup.net" ]; then set ${parameter[1]} case $1 in JOIN|join|PART|part) echo "$1 $2" ;; OP|op) echo "MODE $2 +o $nick" ;; RAW|raw) shift echo $* ;; esac fi } # Réception de CTCP function m_ctcp { echo "[$nick->${parameter[0]}] CTCP - ${parameter[1]}" >&2 local cmd=$1; shift # CTCP VERSION case $cmd in CLIENTINFO) nctcp $nick "CLIENTINFO FINGER PING TIME USERINFO VERSION" ;; FINGER) nctcp $nick "FINGER bash (bash@gnu.org) Idle incalculable" ;; PING) nctcp $nick "PING $*" ;; TIME) nctcp $nick "TIME $(/bin/date)" ;; USERINFO) nctcp $nick 'USERINFO $Id: shellbot.sh,v 1.9 2002/08/25 01:11:51 tripa Exp $' ;; VERSION) nctcp $nick "VERSION $VERSION" ;; esac } # Le gros du soft function bot { init trap ':;sigint' SIGINT # avec l'affreux hack du ':;' :o) while read; do parse case $command in ERROR) echo "${parameter[0]}" >&2 exit ;; PING) echo "PONG ${parameter[0]}" echo "PING/PONG (${parameter[0]})" >&2 ;; JOIN) echo "$nick joins channel ${parameter[0]}" >&2 ;; PART) echo "$nick parts from ${parameter[0]}" >&2 ;; NICK) echo "$nick is now known as ${parameter[0]}" >&2 # pas oublier de tracker ses propres changements if [ $nick = $mynick ]; then mynick=${parameter[0]} fi ;; PRIVMSG) # bug bash? en attendant, c'est bon, ces offsets marchent if [ "${parameter[1]:0:1}" = $'\001' -a \ "${parameter[1]:${#parameter[1]}:1}" = $'\001' ]; then m_ctcp ${parameter[1]:1:${#parameter[1]}-1} else m_msg ${parameter[1]} fi ;; *) echo "[$nick] $command ${parameter[*]}" >&2 ;; esac done } # Début effectif du code set -f # pas de globbing! (l'option qui tue...) shopt -s nocaseglob # patterns non sensibles à la casse (autre option qui tue) if [ $# -lt 2 ]; then echo "Usage: $0 [port] [password]" >&2 exit 1 fi mynick=$1 server=$2 if [ $# -ge 3 ]; then port=$3; else port=6667; fi if [ $# -ge 4 ]; then pass=$4; fi bot 3<>/dev/tcp/$server/$port <&3 >&3