#################
## BitchX v1.1 ##
#################

bind ctcp - version pub_sendctcp
bind ctcp - echo pub_sendctcp
bind ctcp - clientinfo pub_sendctcp
bind ctcp - userinfo pub_sendctcp
bind ctcp - errmsg pub_sendctcp
bind ctcp - finger pub_sendctcp
bind ctcp - utc pub_sendctcp
bind ctcp - unban pub_sendctcp
bind ctcp - ops pub_sendctcp
bind ctcp - op pub_sendctcp
bind ctcp - whoami pub_sendctcp
bind ctcp - invite pub_sendctcp
bind ctcp - ping pub_sendctcp
bind ctcp - time pub_sendctcp
bind ctcp - send pub_sendctcp

 if {[llength [channels]] == 0} {
  set host "whatever"
 }
utimer 15 {do_the_finger_shit}
proc do_the_finger_shit {} {
global botnick ctcp-finger
 set host [lindex [split [getchanhost $botnick [lindex [channels] [rand [llength [channels]]]]] @.] 1]
 set ctcp-finger "$botnick ($botnick@$host) Idle"
}
set system "[exec uname -s -r]"
if {$system==""} { set system "Linux 2.0.30" }
set ctcp-version "BitchX-71p2 by panasync - $system : Keep it to yourself!"
set ctcp-clientinfo "SED VERSION CLIENTINFO USERINFO ERRMSG FINGER TIME ACTION DCC CDCC BDCC XDCC UTC PING INVITE WHOAMI ECHO OPS UNBAN  :Use CLIENTINFO <COMMAND> to get more specific information"
set clientinfo(sed) "SED contains simple_encrypted_data"
set clientinfo(version) "a direct_client_connection"
set clientinfo(cdcc) "CDCC cVERSION shows client type, version and environment"
set clientinfo(clientinfo) "CLIENTINFO gives information about available CTCP commands"
set clientinfo(userinfo) "USERINFO returns user settable information"
set clientinfo(errmsg) "ERRMSG returns error messages"
set clientinfo(finger) "FINGER shows real name, login and idle time of user"
set clientinfo(action) "ACTION contains action descriptions for atmosphere"
set clientinfo(dcc) "DCC requests hecks cdcc info for you"
set clientinfo(bdcc) "BDCC checks cdcc info for you"
set clientinfo(xdcc) "XDCC checks cdcc info for you"
set clientinfo(utc) "UTC substitutes the local timezone"
set clientinfo(ping) "PING returns the arguments it receives"
set clientinfo(invite) "INVITE invite to channel specified"
set clientinfo(whoami) "WHOAMI user list information"
set clientinfo(echo) "ECHO returns the arguments it receives"
set clientinfo(ops) "OPS ops person if on userlist"
set clientinfo(op) "OP ops person if on userlist"
set clientinfo(unban) "UNBAN unbans the person from channel"

## How many ctcps will trigger it.. the higher the worse flood prot..
set ctcps "4"
## How many seconds between clearing timers..
set ctcptime "60"
## How long to ignore..
set ignoretime "15"

proc pub_sendctcp { nick uhost hand dest key arg } {
 global ctcps ctcptime ctcp-version ctcp-finger ctcp-finger ctcp-clientinfo botnick lastdest lastping clientinfo ctcptime ignore timerinuse ctcpnum ignoretime curidle
 set dest [string tolower $dest]
 set nick [string tolower $nick]
  if {![info exists lastping]} {
   set lastping "null"
  }
  if {![info exists lastdest]} {
   set lastdest "null"
  }
  if {![info exists ctcpnum]} {
   set ctcpnum "0"
  }
  if {![info exists ignore]} {
   set ignore 0
  }
  if {[expr $ctcpnum + 1] >= $ctcps} {
    if {$ignore == 0} {
     set ignore 1
     putlog "Anti-flood mode activated."
     utimer $ignoretime unignore
    }
  }
  if {$ignore == "1"} {
   return 1
  }
  if {$dest != [string tolower $botnick]} {
    if {$lastdest == $dest} {
      if {$lastping == $nick} {
        if {[botisop $dest]} {
         putserv "KICK $dest {$nick} :Two channel ctcps in a row are NOT allowed"
        } {
         putlog "Couldn't kick {$nick}:( I'm not chop"
        }
      } {
       set lastping $nick
      }
     set lastdest $dest
    }
  }
 set key [string tolower $key]
  if {$key == "echo"} {
   putlog "$nick tried to echo me something.. bahah maybe a flood?"
   set ctcpnum [expr $ctcpnum + 1]
   return 1
  }
  if {$key == "version"} {
   putserv "NOTICE $nick :\001VERSION ${ctcp-version}\001"
   putlog "$nick requested my version."
   set ctcpnum [expr $ctcpnum + 1]
  }
  if {$key == "finger"} {
    if {![info exists curidle]} {
     make_idle
    }
   putserv "NOTICE $nick :\001FINGER ${ctcp-finger} $curidle seconds\001"
   putlog "$nick fingered me."
   set ctcpnum [expr $ctcpnum + 1]
  }
  if {$key == "userinfo"} {
   putserv "NOTICE $nick :\001USERINFO\001"
   putlog "$nick requested my userinfo."
   set ctcpnum [expr $ctcpnum + 1]
  }
  if {$key == "ping"} {
   putserv "NOTICE $nick :\001PING $arg\001"
   putlog "$nick pinged me."
   set ctcpnum [expr $ctcpnum + 1]
  }
  if {$key == "clientinfo"} {
    if {$arg == ""} {
     putserv "NOTICE $nick :\001CLIENTINFO ${ctcp-clientinfo}\001"
     putlog "$nick requested my clientinfo command list"
    }
    if {[info exists clientinfo($arg)]} {
     putserv "NOTICE $nick :\001CLIENTINFO $clientinfo($arg)\001"
     putlog "$nick requested information on $arg"
    } {
      if {$arg != ""} {
       putserv "NOTICE $nick :\001ERRMSG $arg is not a valid function\001"
       putlog "$nick requested information on $arg -- No such command"
      }
    }
   set ctcpnum [expr $ctcpnum + 1]
  }
  if {$key == "time"} {
   putserv "NOTICE $nick :\001TIME [ctime [unixtime]]\001"
   set ctcpnum [expr $ctcpnum + 1]
   putlog "$nick got the time."
  }
  if {($key == "ops") || ($key == "ops") || ($key == "invite") || ($key == "unban")} {
   putserv "NOTICE $nick :BitchX: Access Denied"
   set ctcpnum [expr $ctcpnum + 1]
   putlog "$nick tried a BitchX ctcp command.. haha this is a egg lamer.."
  }
  if {$key == "utc"} {
    if {[llength $arg] >= 1} {
     putserv "NOTICE $nick :Wed Dec 31 19:00:00 1969"
     set ctcpnum [expr $ctcpnum + 1]
     putlog "$nick got the time thru utc.. Weird bitchX thing"
    }
  }
  if {![info exists timerinuse]} {
   set timerinuse 0
  }
  if {$timerinuse == 0} {
   set timerinuse 1
   utimer $ctcptime clear_ctcps
  }
 return 1
}
proc clear_ctcps {} {
global ctcpnum timerinuse ctcptime
  if {$ctcpnum == 0} {
   set timerinuse 0
   return 1
  }
 set ctcpnum "0"
 utimer $ctcptime clear_ctcps
}

proc make_idle {} {
 global curidle
  if {![info exists curidle]} {
   set curidle [rand 30]
   utimer 5 make_idle
   return 1
  }
  if {$curidle >= [expr [rand 300] + 50]} {
   unset curidle
   return 1
  }
 set curidle [expr $curidle + [rand 7]]
 utimer 5 make_idle
}

proc unignore {} {
global ignore ctcpnum
 set ignore 0
 set ctcpnum 0
}

putlog "Loaded BitchX v1.1"

