#!/bin/bash export LANG=C Tor_disable stalelock_exclusive () { # {{{ #returns 0 if lock exists and has a running process #which is really wrong, should be 1 so we can do stalelock_exclusive || return 1 if [ -n "${1}" ] ; then [ -e "${1}.lock" ] || return 1 read pid < "${1}.lock" [ -z "${pid}" ] && return 1 [ -e "/proc/${pid}" ] && return 0 rm -f "${1}.lock" return 1 fi } # }}} stalelock_shared () { # {{{ #returns 0 if lock exists and has a running process #which is really wrong, should be 1 so we can do stalelock_shared || return 1 retval=1 if [ -n "${1}" ] ; then for sharedlock in ${1}.shared* ; do [ -f "${sharedlock}" ] || continue read pid < ${sharedlock} if [ -e "/proc/${pid}" ] ; then retval=0 else rm -f ${sharedlock} fi done fi return ${retval} } # }}} getlock_exclusive() { # {{{ if [ -n "${1}" ] ; then stalelock_exclusive "${1}" stalelock_shared "${1}" if [ -f "${1}.lock" ] && [ ! -w "${1}.lock" ] ; then error "Can't write lockfile ${1}.lock!" exit 1 fi if [ -e "${1}.lock" ] ; then read pid < ${1}.lock if [ ${pid} -ne ${$} ] ; then return 1 fi else echo "${$}" >>${1}.lock fi read pid < ${1}.lock if [ ${pid} -eq ${$} ] ; then for x in ${1}.shared* ; do [ -f "${x}" ] || continue return 1 # as long as a shared lock exists, exclusive ones aren't granted done return 0 else return 1 fi fi } # }}} getlock_shared() { # {{{ if [ -n "${1}" ] ; then stalelock_exclusive "${1}" stalelock_shared "${1}" [ -e "${1}.lock" ] && return 1 # no shared locks with an exclusive one in place echo "${$}" >> "${1}.shared_${$}" return 0 fi } # }}} releaselock_shared () { # {{{ if [ -n "${1}" ] ; then [ -e "${1}.shared_${$}" ] || return 0 # no lock, so it's released, no? rm -f "${1}.shared_${$}" return 0 fi } # }}} releaselock_exclusive() { # {{{ if [ -n "${1}" ] ; then [ -e "${1}.lock" ] || return 0 # no lock, so it's released, no? read pid < ${1}.lock if [ ${pid} -eq ${$} ] ; then # our lock. release it rm -f ${1}.lock return 0 else #uh-oh, not our lock... echo "Tried to release lock not our own!" >&2 echo "LockPID: ${pid} Our pid: ${$} File: ${1}" >&2 exit 1 fi fi } # }}} trap 'fuser -k /home/blindcoder/LOGBOT/ii/irc.freenode.net/\#*/out /home/blindcoder/LOGBOT/ii/irc.freenode.net/out; exit' INT unset tails ball_0="Signs point to yes (Zeichen deuten auf ja)" ball_1="Yes (Ja)" ball_2="Without a doubt (Ohne einen Zweifel)" ball_3="As I see it yes (Wie ich es sehe ja)" ball_4="Most likely (Höchstwahrscheinlich)" ball_5="You may rely on it (Darauf kannst du dich verlassen)" ball_6="Yes definitely (Definitiv ja)" ball_7="It is decidedly so (Es ist entschieden so)" ball_8="Outlook good (Gute Aussichten)" ball_9="It is certain (Es ist sicher)" ball_10="My sources say no (Meine Quellen sagen nein)" ball_11="Very doubtful (Sehr zweifelhaft)" ball_12="Don't count on it (Zähl nicht drauf)" ball_13="Outlook not so good (Nicht so gute Aussichten)" ball_14="My reply is no (Meine Antwort ist nein)" ball_15="Reply hazy, try again (Antwort unklar, versuchs nochmal)" ball_16="Concentrate and ask again (Konzentriere dich und frag nochmal)" ball_17="Better not tell you now (Ich sags dir jetzt lieber nicht)" ball_18="Cannot predict now (Kann es jetzt nicht vorhersagen)" ball_19="Ask again later (Frag später nochmal)" gettitle() { # {{{ set -x text="${1}" channel="${2}" IFS=' ' read count title < <( echo "SELECT count, title FROM links WHERE link = '${text}'" | mysql -u rocklogbot --password=rockmyworld --database rocklogbot | tail -n 1 ) if [ -z "${title}" ] ; then curl -m 30 --head "${text}" 2>/dev/null | grep -q text/.\\?html || return tmp="$(mktemp)" if ! wget --timeout 30 -o /dev/null -O - "${text}" | tee "${tmp}_orig" | tr '\n' ' ' | tr -d ' ' > ${tmp} ; then rm -f "${tmp}" continue fi if ! grep -iq '.*' "${tmp}" ; then rm -f "${tmp}" continue fi if grep -q http://blog.fefe.de/ <<< "${text}" ; then read title < <( grep -i '^
  • ' "${tmp}_orig" | sed -e 's,<[^>]*>,,g' | head -n 1 ) else read title < <( sed 's,^.*\(.*\).*$,\1,gi' ${tmp} ) fi title="${title//\'/}" title="$( echo "" | php )"; echo "INSERT INTO links (link, title, count) VALUES ('${text}', '${title}', 1)" | mysql -u rocklogbot --password=rockmyworld --database rocklogbot else echo "UPDATE links SET count=count+1 WHERE link='${text}'" | mysql -u rocklogbot --password=rockmyworld --database rocklogbot fi if [ -n "${title}" ] ; then [ -n "${title:101}" ] && title="${title:0:97}..." [ "${title:0:1}" == "/" ] && title=" ${title}" #[ -n "${text:99}" ] && text="${text:0:95}..." echo "${title}: ${text} ${count:+(posted ${count} times)}" > ${channel} fi rm -f "${tmp}" "${tmp}_orig" } # }}} poker() { # {{{ channel="${1}" shift players=" ${@} " tmp="$(mktemp)" trap 'rm -f ${tmp}' EXIT ./shuffle.pl CARDS > ${tmp} for player in ${players} ; do eval unset CARDS_${player} done while : ; do for player in ${players} ; do read CARD eval "CARDS_${player}=\"\${CARDS_${player}}\${CARDS_${player}:+-}${CARD}\"" read CARD eval "CARDS_${player}=\"\${CARDS_${player}}\${CARDS_${player}:+-}${CARD}\"" #echo /j "${user}" "You have been dealt the ${CARD}" > ii/irc.freenode.net/in eval echo /j "${player}" "Your hand is now: \${CARDS_${player}//-/, }" > ii/irc.freenode.net/in done read CARD common="${CARD}" read CARD common="${common}-${CARD}" read CARD common="${common}-${CARD}" echo "Flop is: ${common//-/, }" > ${channel} for player in ${players} ; do echo "${player}: Do you want to '!fold', '!check' or '!raise'?" > ${channel} while read decision ; do #15:46 !fold if grep -q "<${player}> !fold" <<< "${decision}" ; then players="${players/ ${player} / }" continue 2 elif grep -q "<${player}> !check" <<< "${decision}" ; then continue 2 elif grep -q "<${player}> !raise" <<< "${decision}" ; then echo "not implemented" > ${channel} continue 2 elif grep -q "<${player}> !die" <<< "${decision}" ; then echo "${player} ends the game." > ${channel} exit fi done < <( tail -n 0 -F ${channel%in}out ) done read CARD common="${common}-${CARD}" echo "Turn is: ${CARD}" > ${channel} for player in ${players} ; do echo "${player}: Do you want to '!fold', '!check' or '!raise'?" > ${channel} while read decision ; do #15:46 !fold if grep -q "<${player}> !fold" <<< "${decision}" ; then players="${players/ ${player} / }" continue 2 elif grep -q "<${player}> !check" <<< "${decision}" ; then continue 2 elif grep -q "<${player}> !raise" <<< "${decision}" ; then echo "not implemented" > ${channel} continue 2 elif grep -q "<${player}> !die" <<< "${decision}" ; then echo "${player} ends the game." > ${channel} exit fi done < <( tail -n 0 -F ${channel%in}out ) done read CARD common="${common}-${CARD}" echo "River is: ${CARD}" > ${channel} for player in ${players} ; do echo "${player}: Do you want to '!fold', '!check' or '!raise'?" > ${channel} while read decision ; do #15:46 !fold if grep -q "<${player}> !fold" <<< "${decision}" ; then players="${players/ ${player} / }" continue 2 elif grep -q "<${player}> !check" <<< "${decision}" ; then continue 2 elif grep -q "<${player}> !raise" <<< "${decision}" ; then echo "not implemented" > ${channel} continue 2 elif grep -q "<${player}> !die" <<< "${decision}" ; then echo "${player} ends the game." > ${channel} exit fi done < <( tail -n 0 -F ${channel%in}out ) done echo "Board cards are: ${common//-/, }" > ${channel} for player in ${players} ; do eval echo "${player} has the following cards: \${CARDS_${player}//-/, }" > ${channel} done exit done < ${tmp} } # }}} readchannel() { # {{{ channel=${1%/out}/in tail -F -n 0 ${1} | while read line ; do # 09:24 -!- not-blindy(n=blindcod@pallas.crash-override.net) has quit "Client Quit" # not-blindy quit "Client Quit" #18:10 -!- tc_ changed nick to sr__ # 17:15 -!- owl changed nick to lkd #19:40 -!- sr__(n=sr@host.perpetuum-immobile.net) has quit "Remote closed the connection" if grep -q '^..:.. -!- .* \(left\|joined\|quit\) ' <<< "${line}" ; then read user action chan < <( sed -e 's,^..:.. ... \([^(]*\)(.*\(left\|joined\|quit\) \(.*\)$,\1 \2 \3,g' <<< "${line}" ) if [ "${action}" == "joined" ] ; then chan="#${channel##*/\#}" chan="${chan%/*}" [ -f "onetimegreetings.${chan}" ] || continue # #channel: message eval "grep -q '^${user//\[/\\[}$' onetimegreetings.${chan}.done" && continue read message < "onetimegreetings.${chan}" message="${message/NICK/${user}}" message="${message/CHANNEL/${chan}}" echo /j "${user}" "${message}" > ii/irc.freenode.net/in echo "${user}" >> onetimegreetings.${chan}.done fi [ "${action}" == "quit" ] && chan="${chan// /_}" tmp="`mktemp`" while ! getlock_exclusive /home/blindcoder/LOGBOT/seen.db ; do sleep 1 done grep -iv "^${user//\[/\\[}:" /home/blindcoder/LOGBOT/seen.db > ${tmp} echo "${user}: ${action} ${chan} `date`" >/home/blindcoder/LOGBOT/seen.db cat ${tmp} >> /home/blindcoder/LOGBOT/seen.db rm -f ${tmp} releaselock_exclusive /home/blindcoder/LOGBOT/seen.db continue elif grep -q '^..:.. -!- \([^ ]*\) changed nick to \([^ ]*\)' <<< "${line}" ; then read time bang old changed nick to new <<< "${line}" while ! getlock_exclusive /home/blindcoder/LOGBOT/seen.db ; do sleep 1 done tmp="`mktemp`" grep -iv "^${old//\[/\\[}:" /home/blindcoder/LOGBOT/seen.db > ${tmp} echo "${old}: nick ${new} `date`" >/home/blindcoder/LOGBOT/seen.db cat ${tmp} >> /home/blindcoder/LOGBOT/seen.db rm -f ${tmp} releaselock_exclusive /home/blindcoder/LOGBOT/seen.db continue elif grep -q '^..:.. -!- .* kicked rocklogbot ' <<< "${line}" ; then channelname="${chan##*/#}" channelname="#${channelname%%/*}" grep -q "^${channelname}\$" /home/blindcoder/LOGBOT/ii/channels && \ echo "echo \"/j ${channelname}\" > /home/blindcoder/LOGBOT/ii/irc.freenode.net/in" | at now + 5 minutes continue fi line="$( sed -e 's,^[^<]*<\([^>]*\)> \(.*\),\1 \2,g' <<< "${line}" )" read user text <<< "${line}" [ "${user}" == "rocklogbot" ] && continue [ -z "${text}" ] && continue read command parameters <<< "${text}" [ "${command}" == $'\001'"ACTION" ] && read command parameters <<< "${parameters}" if [ "${command}" == "!wtf" ] ; then # {{{ if [ -z "${parameters}" ] ; then echo "${user}: Usage: !wtf [is] " > ${channel} continue fi message="${parameters}" message="${message//\'/\'\"\'\"\'}" message="${message//\\*/}" eval "wtf -t rock '${message#is }' 2>&1" | sed "s,^,${user}: ,g" > ${channel} # }}} elif [ "${command}" == "!alarm" ] ; then # {{{ read when <<< "$( sed -e 's,".*,,g' <<< "${parameters}" )" read message <<< "$( sed -e 's,^[^"]*",,g' -e 's,"[^"]*$,,g' <<< "${parameters}" )" if [ -z "${when}" ] ; then echo "${user}: Usage: !alarm