FoobarBazbot

BotBot

Feb 16th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.61 KB | None | 0 0
  1. #!/bin/sh
  2. IRCDIR="$HOME/botbotirc"
  3. IRCSERV=irc.freenode.net
  4. IRCPORT=6667
  5. IRCCHANS='##altslashdot ##SlashTest'
  6. #IRCCHANS='##botbottest ##botbottest2'
  7. IRCNAME='Bazbot`s_BotBot'
  8. IRCPASSWD='Bazbot`s_BotBot:xxxxxxxxxxxxxxx'
  9. IRCLOGDIR="$HOME/irclog"
  10.  
  11. RSSURL=http://li694-22.members.linode.com/index.rss
  12. TMPPREFIX="$HOME/.botbot_rss_tmp"
  13.  
  14. mkdir -p $IRCLOGDIR
  15.  
  16. cat >"$TMPPREFIX.xls" <<E_O_F
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  19.   <xsl:output method="text" />
  20.  
  21.   <xsl:template match="node()[local-name()!='item' and local-name()!='RDF']"/>
  22.  
  23.   <xsl:template match="*[local-name()='item']">
  24.     <xsl:value-of select="*[local-name()='creator']" />
  25.     <xsl:text> posted: </xsl:text>
  26.     <xsl:value-of select="*[local-name()='title']" />
  27.     <xsl:text>, from the </xsl:text>
  28.     <xsl:value-of select="*[local-name()='department']" />
  29.     <xsl:text> department
  30. </xsl:text>
  31.   </xsl:template>
  32. </xsl:stylesheet>
  33. E_O_F
  34.  
  35. #force channels lowercase
  36. IRCCHANS="$(echo $IRCCHANS|tr '[A-Z]' '[a-z]')"
  37. echo IRCCHANS=$IRCCHANS
  38.  
  39. #start respawning ii
  40. (while true ; do
  41.   if [ $(ii -h 2>&1 |awk 'NR==1 {print int($NF*1000+0.5)}') -lt 1700 ] ; then
  42.     ii -i "$IRCDIR" -s "$IRCSERV" -p "$IRCPORT" -n "$IRCNAME" -k "$IRCPASSWD" &
  43.     iipid=$!
  44.   else
  45.     export IRCPASSWD=$IRCPASSWD
  46.     ii -i "$IRCDIR" -s "$IRCSERV" -p "$IRCPORT" -n "$IRCNAME" -k IRCPASSWD &
  47.     iipid=$!
  48.   fi
  49.   trap "echo /QUIT >\"$IRCDIR/$IRCSERV/in\"; sleep 1; kill $iipid ;
  50.        for chan in \$IRCCHANS ; do echo cleaning \$chan ; cp \"$IRCDIR/$IRCSERV/\$chan/out\" \"$IRCLOGDIR/\$chan.\$(date -Iseconds)\"; rm \"$IRCDIR/$IRCSERV/\$chan/in\"; done
  51.        rm \"$IRCDIR/$IRCSERV/in\"
  52.        exit" TERM
  53.   echo iipid=$iipid
  54.   timeout=0
  55.   until [ -e "$IRCDIR/$IRCSERV/in" -o $timeout -gt 10 ] ; do
  56.     timeout=`expr $timout + 1`
  57.     sleep 1
  58.   done
  59.   if [ -e "$IRCDIR/$IRCSERV/in" ] ; then
  60.     for chan in $IRCCHANS ; do
  61.       echo joining $chan...
  62.       echo "/j $chan" > "$IRCDIR/$IRCSERV/in"
  63.     done
  64.   fi
  65.   wait $iipid
  66.   for chan in $IRCCHANS ; do
  67.     echo cleaning $chan
  68.     cp "$IRCDIR/$IRCSERV/$chan/out" "$IRCLOGDIR/$chan.$(date -Iseconds)";
  69.     rm "$IRCDIR/$IRCSERV/$chan/in"
  70.   done
  71.   rm "$IRCDIR/$IRCSERV/in"
  72. done) &
  73. subshpid=$!
  74. echo subshpid=$subshpid
  75. trap "kill $subshpid;exit" INT EXIT
  76.  
  77. #watchdog
  78. (while sleep 60; do
  79.   if [ $(find "$IRCDIR/$IRCSERV/" -name out -mmin -10|wc -l) -eq 0 ] ; then
  80.     echo /QUIT >"$IRCDIR/$IRCSERV/in"
  81.   fi
  82. done)&
  83. subshpid=$! $subshpid
  84. echo subshpid=$subshpid
  85. trap "kill $subshpid;exit" INT EXIT
  86.  
  87. #bot reactions
  88. for chan in $IRCCHANS ; do
  89.   (until [ -e "$IRCDIR/$IRCSERV/$chan/in" ] ; do sleep 1 ; done ;
  90.    tail -Fn1 "$IRCDIR/$IRCSERV/$chan/out" |
  91.    awk 'BEGIN {
  92.          action_count=30;
  93.          for(i=0;i<action_count;i+=5) {
  94.            action[i+0]="flickers its indicator lights";
  95.            action[i+1]="whistles softly";
  96.            action[i+2]="stares ominously";
  97.            action[i+3]="computes trajectories";
  98.            action[i+4]="oscillates its antenna";
  99.          }
  100.          action[action_count-1]="kills all humans";
  101.          srand();
  102.        }
  103.        (/'"$IRCNAME"'/ || /[bB][oO][tT][bB][oO][tT]/) && $3!="<'"$IRCNAME"'>" {
  104.          print "\001ACTION " action[int(rand()*action_count)] "\001" > "'"$IRCDIR/$IRCSERV/$chan/in"'";
  105.          close("'"$IRCDIR/$IRCSERV/$chan/in"'");
  106.          if(i>=action_count) i=0;
  107.        }')&
  108.   monpid="$! $monpid"
  109.   echo monpid=$monpid
  110.   trap "for pid in $monpid; do pkill -P \$pid; done; kill $subshpid $monpid;exit" INT EXIT
  111. done
  112.  
  113. #channel monitoring -- good for debugging, but can be removed
  114. color=0
  115. for chan in $IRCCHANS ; do
  116.   #choose colors 1-6, no white or black
  117.   color=$(expr $color % 6 + 1)
  118.   until [ -e "$IRCDIR/$IRCSERV/$chan/in" ] ; do sleep 2 ; done
  119.   tput setaf $color; echo monitoring $chan...; tput sgr0
  120.   (tail -F "$IRCDIR/$IRCSERV/$chan/out"|
  121.     awk "{print \"$(tput setaf $color)\" \$0 \"$(tput sgr0)\"}")&
  122.   tailpid="$! $tailpid"
  123.   echo tailpid=$tailpid
  124.   trap "for pid in $monpid $tailpid; do pkill -P \$pid; done; kill $subshpid $monpid $tailpid;exit" INT EXIT
  125. done
  126.  
  127. sleep 10;
  128. #rss wrangling
  129. while true ; do
  130.   [ $(wc -l <"$TMPPREFIX.new") -gt 1 ] && mv "$TMPPREFIX.new" "$TMPPREFIX.old"
  131.   wget -O - "$RSSURL" |
  132.     sed 's/&#x26;amp;\(#[0-9]\+;\)/\&\1/g' |
  133.     xsltproc $TMPPREFIX.xsl - |
  134.     tee "$TMPPREFIX.new" |
  135.     tee "$TMPPREFIX.$(date -Iseconds)" |
  136.     diff - "$TMPPREFIX.old" |
  137.     awk '/^</{print substr($0,3)}/^>/{quit}' |
  138.     tac |
  139.     tee $(for chan in $IRCCHANS; do echo "$IRCDIR/$IRCSERV/$chan/in"; done)
  140.   sleep 60
  141. done
Add Comment
Please, Sign In to add comment