#################################################################
#                   S C R I P T   I N F O                       #
#                                                               #
#  name: spymsg.tcl                                             #
#                                                               #
#  info: This scripts listens for any privmsg sent to the bot   #
#        and if it contains possible invites/spams it reports   #
#        it to the ops using /onotice                           #
#                                                               #
#  tips: I have defined a proxy arrays for schools and netcafés #
#        and if the host matches one of the proxys in the array #
#        the script will suggest a different type of bans.      #
#        Use this script on a undercover bot, that is a NON-    #
#        OPED bot for best results!                             #
#                                                               #
# about: based upon adsing.tcl, probably written by Lasse @     #
#        DALnet, and with inspiration and a few lines of code   #
#        from MC_8's mc.spamcheck.tcl                           #
#                                                               #
#  test: this script has only been tested on eggdrop1.6.10	#
#                                                               #
#################################################################
#
# Written exclusivly for #helsingborg @ DALnet, but I liked it and thought
# you'd like it aswell! :)
#
# Changelog
# v1.0 - First release
# v1.01 - Fixed so that the bot only reports spam from someone that's in the channel.
#
# got bugs? report them to me at ph0x@swipnet.se or send a DALnet memo if using DAL ;p

bind msgm - * spy_msg


# Uncomment if not using proxy.tcl
#set proxys {
#  "*"
#}


# this is the array of words to listen for, I want to listen for everything so I use the array below.
#set spywords {
# *join*
# *http*
# *listen.pls*
# *#*
#}

# Listen for everything
set spywords {
 *
}

# report ops?
set spyconfig(reportops) 1

# which channel should the script be active in?
set spyconfig(chan) "#helsingborg"

# ignore theese userflags
set spyconfig(handle) "f|f"

#ignore this messages. good for using msg commands with the bot.
set spyconfig(ignorewords) "addhost info who ident help voice whois pass op invite go key die jump memory save rehash reset status dent notes"

### TCL CODE BELOW, DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING AND GIVE ME FULL CREDIT! ###

set spyversion "v1.01"

proc spy_msg {nick host handle arg} {
  
  #let's strip of the bold, reverse, underline and colour codes
  set arg [mc:sc:inter:strip:all $arg]
  
  global proxys spywords spyconfig
  
  set return 1
  
  # check the privmsg
  foreach word [string tolower [split $spywords \n]] {
    if {[join $word] == ""} {continue}
    if {[string match [string trim $word] [string tolower $arg]]} {
      set return 0
      break
    }
  }
  
  if {$return} {return 0}
  
  scan $arg %s cmd
  
  # if it matches the ignored words, stop here.
  foreach command [string tolower $spyconfig(ignorewords)] {
    if {[string tolower $cmd] == $command} {return 0}
  }
 
  if [matchattr $handle $spyconfig(handle) $spyconfig(chan)] {return 0}
  if {([isop $nick $spyconfig(chan)]) && ($spyconfig(reportops) == 0)} {return 0}
  if {![onchan $nick $spyconfig(chan)]} {return 0}
  
  set isproxy 0
  foreach proxy $proxys {
    if {[string match *$proxy* $host]} {
      set isproxy 1
    } 
  }
  if {$isproxy == 0} {		
    putserv "NOTICE @$spyconfig(chan) :\002\[OPS ONLY\]\002 Possible spam from \002\037$nick\037\002 ($host) \[\"$arg\"\]!"
  } else {
    putserv "NOTICE @$spyconfig(chan) :\002\[OPS ONLY\]\002Possible spam from \002\037$nick\037\002 ($host) \[\"$arg\"\]! \002Note that this is a host matching the proxylist!\002"
  }
}

# MC_8's striping code
proc mc:sc:inter:strip:color {ar} {
 regsub -all -- {\003([0-9][0-9]?(,[0-9][0-9]?)?)?} $ar "" ar ; return $ar
}
proc mc:sc:inter:strip:bold {ar} {regsub -all -- "\002" $ar "" ar; return $ar}
proc mc:sc:inter:strip:uline {ar} {regsub -all -- "\037" $ar "" ar; return $ar}
proc mc:sc:inter:strip:reverse {ar} {regsub -all -- "\026" $ar "" ar; return $ar}
proc mc:sc:inter:strip:all {ar} {
 return [mc:sc:inter:strip:reverse [mc:sc:inter:strip:uline [mc:sc:inter:strip:bold [mc:sc:inter:strip:color $ar]]]]
}

putlog "spymsg.tcl - $spyversion by ph0x @ DALnet"