##########################################
##  nob0dy Menu Utama :D                ##
##  by Vrs-hCk@nob0dy.DALnet  ##
##########################################

bind pub - !cmd commands

proc commands {nick uhost hand chan text} {
 puthelp "NOTICE $nick :8,4 !pc | !ip | !dns | !dwhois | !state "
}

####################
##  DNS Resolver  ##
####################
set dnshost(cmdchar) "!"
bind pub - [string trim $dnshost(cmdchar)]dns dns:res
#bind pub - [string trim $dnshost(cmdchar)]host pub:host
#bind pub - [string trim $dnshost(cmdchar)]dnsver pub:ver
#bind pub - [string trim $dnshost(cmdchar)]dnsnick dns:nick
#bind raw * 311 raw:host
#bind raw * 401 raw:fail
set dns_chan ""
set dns_host ""
set dns_nick ""
set dns_bynick ""
proc pub:host {nick uhost hand chan arg} {
  global dns_chan
  set dns_chan "$chan"
  putserv "WHOIS [lindex $arg 0]"
}
proc raw:host {from signal arg} {
  global dns_chan dns_nick dns_host dns_bynick
  set dns_nick "[lindex $arg 1]"
  set dns_host "*!*[lindex $arg 2]@[lindex $arg 3]"
  foreach dns_say $dns_chan { puthelp "PRIVMSG $dns_say :\[4nob0dy-DNS\] User@Host dari $dns_nick adalah $dns_host" }
  if {$dns_bynick == "oui"} {
    set hostip [split [lindex $arg 3] ]
    dnslookup $hostip resolve_rep $dns_chan $hostip
    set dns_bynick "non"
  }
}
proc raw:fail {from signal arg} {
  global dns_chan
  set arg "[lindex $arg 1]"
  foreach dns_say $dns_chan { puthelp "PRIVMSG $dns_say :\[4nob0dy-DNS\] Gak ada tuh Nick $arg" }
}
proc pub:ver {nick uhost hand chan text} {
  putserv "PRIVMSG $chan : 4nob0dy-DNS 7by 3Vaksin13"
}
proc dns:res {nick uhost hand chan text} {
  if {$text == ""} {
    puthelp "privmsg $chan :Syntax: [string trim $dnshost(cmdchar)]dns <host or ip>"
    } else {
    set hostip [split $text]
    dnslookup $hostip resolve_rep $chan $hostip
  }
}
proc dns:nick {nick uhost hand chan arg} {
  global dns_chan dns_bynick dnshost
  if {$arg == ""} {
    puthelp "privmsg $chan :Syntax: [string trim $dnshost(cmdchar)]dnsnick <nick>"
    } else {
    set dns_chan "$chan"
    set dns_bynick "oui"
    putserv "WHOIS [lindex $arg 0]"
  }
}
proc resolve_rep {ip host status chan hostip} {
  if {!$status} {
    puthelp "privmsg $chan  :\[4nob0dy-DNS\] $hostip gak bisa di resolve."
    } elseif {[regexp -nocase -- $ip $hostip]} {
    puthelp "privmsg $chan  :\[4nob0dy-DNS\] $ip resolved to $host"
    } else {
    puthelp "privmsg $chan  :\[4nob0dy-DNS\] $host resolved to $ip"
  }
}
putlog "+++ BHT DNS Resolver TCL Loaded"

###########################
##  nob0dy Domain Whois  ##
###########################
bind pub - !dwhois dwhois
proc dwhois {nick uhost hand chan text} {
  set server "isfree.schlundtec.com"
  set port 80
  set l 14
  set i 0
  set path "/cgi-bin/isfree.cgi?nodesign=1&domain=[lindex $text 0]"
  set sockdw [socket $server $port]
  puts $sockdw "GET $path HTTP/1.0"
  puts $sockdw "User.Agent:Mozilla"
  puts $sockdw "Host: $server"
  puts $sockdw ""
  flush $sockdw
  while {$i <= $l} {
    gets $sockdw linedw
    putlog $linedw
    if {[string match "*Domain*frei*" $linedw]} {
      putserv "PRIVMSG $chan :15\[nob0dy-DwhOiS4!15\] Web 4[lindex $text 0] not found."
      close $sockdw
      return 0
    }
    if {[string match "*Domain*registriert*" $linedw]} {
      gets $sockdw
      putserv "PRIVMSG $chan :15\[nob0dy-DwhOiS4!15\] 4Info for 12[lindex $text 0]: [html [gets $sockdw]], [html [gets $sockdw]], [html [gets $sockdw]], [html [gets $sockdw]]"
      close $sockdw
      return 0
    }
    incr i
  }
  close $sockdw
}
proc html { text } {
  regsub -all "</TD>" $text "" text
  regsub -all "</FONT>" $text "" text
  regsub -all "	" $text "" text
  regsub -all "&uuml;" $text "ü" text
  regsub -all "&ouml;" $text "ö" text
  regsub -all "&auml;" $text "ä" text
  regsub -all "&Uuml;" $text "Ü" text
  regsub -all "&Ouml;" $text "Ö" text
  regsub -all "&Auml;" $text "Ä" text
  regsub -all "&szlig;" $text "ß" text
  regsub -all "&quot;" $text "\"" text
  regsub -all "<tb>" $text "" text
  regsub -all "<font" $text "" text
  regsub -all "size=\"2\"" $text "" text
  regsub -all "face=\"Verdana,Arial,Helvetica,Geneva\">" $text "" text
  regsub -all "<br>" $text "" text
  regsub -all "&nbsp;" $text "" text
  regsub -all "</font>" $text "" text
  regsub -all "<td>" $text "" text
  regsub -all "</td>" $text "" text
  regsub -all "<b>" $text "" text
  regsub -all "</b>" $text "" text
  regsub -all "</pre>" $text "" text
  return $text
}
putlog "+++ BHT Domain Whois TCL Loaded"

#########################
##  nob0dy Port Check  ##
#########################

set portchk_setting(flag) ""
set portchk_setting(cmd_pub) "!pc"
set portchk_setting(cmd_dcc) "pc"
set portchk_setting(read) 1
set portchk_setting(onjoin) 0
set portchk_setting(ports) "1080 21 22 23 80 8080 31337"
set portchk_setting(autoban_svr) 0
set portchk_setting(autoban_list) 0
set portchk_setting(global) 0
set portchk_setting(bantime) 5
set portchk_setting(onotice) 1
set portchk_setting(bold) 1
set portchk_setting(portchk:) 1

if {![string match 1.6.* $version]} {
	putlog "\002portchk:\002 \002CRITICAL ERROR\002 portchk.tcl requires eggdrop 1.6.x to run."
	die "\002portchk:\002 \002CRITICAL ERROR\002 portchk.tcl requires eggdrop 1.6.x to run."
}
bind pub $portchk_setting(flag) $portchk_setting(cmd_pub) portchk_scan_pub
bind dcc $portchk_setting(flag) $portchk_setting(cmd_dcc) portchk_scan_dcc
bind join - * portchk_onjoin_scan
setudef flag portchk

proc portchk_dopre {} {
	global portchk_setting
	if {!$portchk_setting(portchk:)} {
		return ""
	} elseif {!$portchk_setting(bold)} {
		return "portchk: "
	} else {
		return "\002portchk:\002 "
	}
}
proc portchk_onjoin_scan {nick uhost hand chan} {
	global portchk_setting
	if {($portchk_setting(onjoin)) && ($portchk_setting(ports) != "")} {
		foreach i [channel info $chan] {
			if {([string match "+portchk" $i]) && ([botisop $chan])} {
				set host [lindex [split $uhost @] 1]
				foreach p $portchk_setting(ports) {
					if {![catch {set sock [socket -async $host $p]} error]} {
						set timerid [utimer 15 [list portchk_timeout_join $sock]]
						fileevent $sock writable [list portchk_connected_join $nick $chan $sock $host $p $timerid]
					}
				}
				break
			}
		}
	}
}
proc portchk_scan_pub {nick uhost hand chan text} {
	global portchk_setting
	set host [lindex [split $text] 0]
	set port [lindex [split $text] 1]
	if {$port == ""} {
		putquick "NOTICE $nick :Usage: $portchk_setting(cmd_pub) <host> <port>"
	} else {
		if {[catch {set sock [socket -async $host $port]} error]} {
			putquick "PRIVMSG $chan :15\[nob0dy-PorT4!15\] Connection to $host \($port\) was refused."
		} else {
			set timerid [utimer 15 [list portchk_timeout_pub $chan $sock $host $port]]
			fileevent $sock writable [list portchk_connected_pub $chan $sock $host $port $timerid]
		}
	}
}
proc portchk_scan_dcc {hand idx text} {
	global portchk_setting
	set host [lindex [split $text] 0]
	set port [lindex [split $text] 1]
	if {$port == ""} {
		putdcc $idx "[portchk_dopre]Usage: .$portchk_setting(cmd_dcc) <host> <port>"
	} else {
		if {[catch {set sock [socket -async $host $port]} error]} {
			putdcc $idx "[portchk_dopre]Connection to $host \($port\) was refused."
		} else {
			set timerid [utimer 15 [list portchk_timeout $idx $sock $host $port]]
			fileevent $sock writable [list portchk_connected $idx $sock $host $port $timerid]
		}
	}
}
proc portchk_connected {idx sock host port timerid} {
	killutimer $timerid
	set error [fconfigure $sock -error]
	if {$error != ""} {
		close $sock
		putdcc $idx "[portchk_dopre]Connection to $host \($port\) failed. \([string totitle $error]\)"
	} else {
		fileevent $sock writable {}
		fileevent $sock readable [list portchk_read $idx $sock $host $port]
		putdcc $idx "[portchk_dopre]Connection to $host \($port\) accepted."
	}
}
proc portchk_timeout {idx sock host port} {
	close $sock
	putdcc $idx "[portchk_dopre]Connection to $host \($port\) timed out."
}
proc portchk_read {idx sock host port} {
	global portchk_setting
	if {$portchk_setting(read)} {
		if {[gets $sock read] == -1} {
			putdcc $idx "[portchk_dopre]EOF On Connection To $host \($port\). Socket Closed."
			close $sock
		} else {
			putdcc $idx "[portchk_dopre]$host \($port\) > $read"
		}
	} else {
		close $sock
	}
}
proc portchk_connected_pub {chan sock host port timerid} {
	killutimer $timerid
	set error [fconfigure $sock -error]
	if {$error != ""} {
		close $sock
		putquick "PRIVMSG $chan :15\[nob0dy-PorT4!15\] Connection to $host \($port\) failed. \([string totitle $error]\)"
	} else {
		fileevent $sock writable {}
		fileevent $sock readable [list portchk_read_pub $chan $sock $host $port]
		putquick "PRIVMSG $chan :15\[nob0dy-PorT4!15\] Connection to $host \($port\) accepted."
	}
}
proc portchk_timeout_pub {chan sock host port} {
	close $sock
	putquick "PRIVMSG $chan :15\[nob0dy-PorT4!15\] Connection to $host \($port\) timed out."
}
proc portchk_connected_join {nick chan sock host port timerid} {
	global portchk_setting botnick
	killutimer $timerid
	set error [fconfigure $sock -error]
	if {$error != ""} {
		close $sock
	} else {
		fileevent $sock writable {}
		fileevent $sock readable [list portchk_read_join $sock]
		if {$portchk_setting(onotice)} {
			foreach i [chanlist $chan] {
				if {([isop $i $chan]) && ($i != $botnick)} {
					putserv "NOTICE $i :Port $port was found open on $nick's host. \($host\)"
				}
			}
		}
		if {$portchk_setting(autoban_svr)} {
			putserv "MODE $chan +b *!*@$host"
			putserv "KICK $chan $nick :One of the ports open on your host is banned."
			timer $portchk_setting(bantime) [list portchk_unsvrban $chan $host]
		} elseif {$portchk_setting(autoban_list)} {
			if {$portchk_setting(global)} {
				newban *!*@$host portchk "One of the ports open on your machine is banned." $portchk_setting(bantime)
			} else {
				newchanban $chan *!*@$host portchk "One of the ports open on your machine is banned." $portchk_setting(bantime)
			}
		}
	}
}
proc portchk_timeout_join {sock} {
	close $sock
}
proc portchk_read_join {sock} {
	close $sock
}
proc portchk_read_pub {sock} {
	global portchk_setting
	if {!$portchk_setting(read)} {
		close $sock
	} elseif {[gets $sock read] == -1} {
		putquick "PRIVMSG $chan :EOF On Connection To $host \($port\). Socket Closed."
		close $sock
	}
}
proc portchk_unsvrban {chan host} {
	putserv "MODE $chan -b *!*@$host"
}
putlog "\002portchk:\002 portchk.tcl Version 2.2 by Vaksin13 is loaded."


#########################
##  nob0dy IP2Country  ##
#########################

set ip2c(trigger) "!ip"
set ip2c(flag) "-"
set ip2c(chan) ""
set ip2c(database) "scripts/ip.csv"
set ip2c(sql) "0"
set ip2c(type) 0
set ip2c(bold) 1
set ip2c(mode) 0
set ip2c(switch) 1

# Host of mysql server 
set mysql(host) "127.0.0.1"

# Port of mysql server
set mysql(port) "3306"

# Database login
set mysql(user) "ip2c"

# Database pass change this
set mysql(pass) "xxxxxxxxxxxxxxxxxxx"

# Database name
set mysql(data) "ip2c"

# Location of MySQL lib for tcl tested with version 2.41 current is 2.5 
# checked the manual and syntax seems ok so I gues it still supports it
# download at http://www.xdobry.de/mysqltcl/
set mysql(libsql) "lib/mysqltcl-2.41/libmysqltcl2.41.so"

# Script version.
set ip2c(vrs) "0.6"

# Script update tag
set ip2c(tag) "1088746257"

# Loading mysql libs and creating a connection.
if {![info exists mysql(conn)] && $ip2c(sql)} {
  if {[file exists $mysql(libsql)]} {
    load "$mysql(libsql)"
    set mysql(conn) [mysqlconnect -host $mysql(host) -port $mysql(port) -user $mysql(user) -password $mysql(pass)]
  } else {
    putlog "Error can't find the mysql lib."
  }
}

bind pub $ip2c(flag) $ip2c(trigger) ip2c:pub
bind raw - 302 raw:302
bind evnt - init-server conn:init

if {[info exists ip2c(bold)]} {
  switch  -- $ip2c(bold) {
    "0" {
      set ip2c(boldset) ""
    }
    "1" {
      set ip2c(boldset) "\002"
    }
  }
} else {
  putlog "Ip-to-Country could contain errors, a required variable couldn't be resolved."
  return 0
}

if {![info exists ip2c(boldset)]} {
  putlog "Ip-to-Country could contain errors, a required variable couldn't be resolved."
  return 0
}

if {[info exists ip2c(type)]} {
  switch  -- $ip2c(type) {
    "0" {
      set ip2c(putserv) "PRIVMSG %CHAN%"
    }
    "1" {
      set ip2c(putserv) "PRIVMSG %NICK%"
    }
    "2" {
      set ip2c(putserv) "NOTICE %NICK%"
    }
  }
} else {
  putlog "Ip-to-Country could contain errors, a required variable couldn't be resolved."
  return 0
}

if {![info exists ip2c(bold)]} {
  putlog "Ip-to-Country could contain errors, a required variable couldn't be resolved."
  return 0
}

proc ip2c:pub {nick host hand chan arg} {
  global ip2c
  set arg [string map {\\ \\\\ \[ \\\[ \] \\\] \{ \\\{ \} \\\} \" \\\"} $arg]
  if {([string match -nocase *$ip2c(chan)* $chan] || [string match -nocase {} $chan]) && [isstatus $nick $chan]} {
    if {[string match -nocase {} [lindex $arg 0]]} {
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Invalid option, $ip2c(boldset)$ip2c(trigger) -help$ip2c(boldset) for a list of options."
    } elseif {![string match -nocase {} [lindex $arg 1]] && ![string match -nocase "-set" [lindex $arg 0]] && ![string match -nocase "-ban" [lindex $arg 0]] && ![string match -nocase "-longip" [lindex $arg 0]] && ![string match -nocase "-ipaddr" [lindex $arg 0]]} {
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Error to mutch variables, usage: $ip2c(trigger) <nick|ipaddr|host>."
    } elseif {[isipaddr [lindex $arg 0]] && $ip2c(switch)} {
      set output [ip2country [lindex $arg 0]]
      set result(0) "[lindex $output 0]"
      set result(1) "[lindex $output 1]"
      set result(2) "[join [lrange $output 2 end]]"
      if {$result(0) != "00"} {
        foreach line [split $result(2) \x20] {
          if {[info exists word]} {
            append word \x20[word_format $line]
          } else {
            set word [word_format $line]
          }
        }
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :15\[nob0dy-IpChk4!15\] Resolved [lindex $arg 0] 7\(6$ip2c(boldset)$result(0), [join [lrange $word 0 end]]$ip2c(boldset)7\)"
      } else {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$result(2)"
      }
    } elseif {[ishost [lindex $arg 0]] && $ip2c(switch)} {
      dnslookup "[string map {\\ \\\\ \[ \\\[ \] \\\] \{ \\\{ \} \\\} \" \\\"} [lindex $arg 0]]" resolve:ipaddrhost "[string map {\\ \\\\ \[ \\\[ \] \\\] \{ \\\{ \} \\\} \" \\\"} $nick]" "[string map {\\ \\\\ \[ \\\[ \] \\\] \{ \\\{ \} \\\} \" \\\"} $chan]"
    } elseif {[string match -nocase "-about" [lindex $arg 0]]} {
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$ip2c(boldset)Ip-to-Country.tcl$ip2c(boldset) Modifed bY Vaksin13 version $ip2c(boldset)$ip2c(vrs)$ip2c(boldset)."
    } elseif {[string match -nocase "-update" [lindex $arg 0]] && [matchattr $nick m]} {
      set vrschk [newupdates]
      if {[string match -nocase "-1" $vrschk]} {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Error could not connect to webserver."
      } else {
        if {$vrschk} {
          putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :You are using the latest version."
	} else {
	  putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :You are not using the latest version check http://www.ofloo.net/ for updates."
	}
      }
    } elseif {[string match -nocase "-help" [lindex $arg 0]]} {
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Ip-to-Country.tcl command list."
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$ip2c(trigger) <nick|host|ipaddr> ::: perfrom lookup."
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$ip2c(trigger) -ban <country code> ::: only for ops bans all users that match the country."
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$ip2c(trigger) -set <on|off> ::: only for ops turns trigger status on or off."
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$ip2c(trigger) -status ::: only for ops shows current script status."
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$ip2c(trigger) -longip <ipaddr> ::: returns longip for ipaddr."
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$ip2c(trigger) -ipaddr <longip> ::: returns ipaddr for longip."
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$ip2c(trigger) -update ::: checks if your using the latest script version."
      putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$ip2c(trigger) -about ::: shows info about this script."
    } elseif {[string match -nocase "-ban" [lindex $arg 0]] && [matchattr $nick o]} {
      if {![string match -nocase {} [lindex $arg 1]] && [string match -nocase {} [lindex $arg 2]]} {
        if {([string bytelength [lindex $arg 1]] == 2) && [regexp -all {[a-zA-Z]} [lindex $arg 1]]} {
          bancountry [lindex $arg 1] $chan
	} else {
	  putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Invalid country code."
	}
      } else {
      	putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Usage: $ip2c(trigger) -ban <2digit country code>."
      }
    } elseif {[string match -nocase "-longip" [lindex $arg 0]]} {
      if {[isipaddr [lindex $arg 1]] && ![string match -nocase {} [lindex $arg 1]] && [string match -nocase {} [lindex $arg 2]]} {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :LongIP for \($ip2c(boldset)[lindex $arg 1]$ip2c(boldset)\) is \($ip2c(boldset)[ip2longip [lindex $arg 1]]$ip2c(boldset)\)"
      } elseif {[string match -nocase {} [lindex $arg 1]] || ![string match -nocase {} [lindex $arg 2]]} {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Usage: $ip2c(trigger) -longip <ipaddr>"
      } else {
      	putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :\($ip2c(boldset)[lindex $arg 1]$ip2c(boldset)\) is not a valid ipaddr."
      }
    } elseif {[string match -nocase "-ipaddr" [lindex $arg 0]]} {
      if {[regexp -all {[0-9]} [lindex $arg 1]] && ![string match -nocase {} [lindex $arg 1]] && [string match -nocase {} [lindex $arg 2]]} {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :IpAddr for \($ip2c(boldset)[lindex $arg 1]$ip2c(boldset)\) is \($ip2c(boldset)[longip2ip [lindex $arg 1]]$ip2c(boldset)\)"
      } elseif {[string match -nocase {} [lindex $arg 1]] || ![string match -nocase {} [lindex $arg 2]]} {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Usage: $ip2c(trigger) -ipaddr <longip>"
      } else {
      	putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :\($ip2c(boldset)[lindex $arg 1]$ip2c(boldset)\) is not a valid longip."
      }
    } elseif {[string match -nocase "-status" [lindex $arg 0]] && [matchattr $nick m]} {
      if {$ip2c(switch)} {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Trigger status is turned \($ip2c(boldset)on$ip2c(boldset)\)."
      } else {
      	putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Trigger status is turned \($ip2c(boldset)off$ip2c(boldset)\)."
      }
    } elseif {[string match -nocase "-set" [lindex $arg 0]] && [matchattr $nick m]} {
      if {[string equal -nocase {} [lindex $arg 1]]} {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] ::Usage: $ip2c(trigger) -set <on|off>."
      } else {
	if {[string equal -nocase "on" [lindex $arg 1]]} {
	  if {!($ip2c(switch))} {
	    set ip2c(switch) 1
	    putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Turned trigger status \($ip2c(boldset)on$ip2c(boldset)\)."
      	  } else {
      	    putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Allready turned on."
      	  }
	} elseif {[string equal -nocase "off" [lindex $arg 1]]} {
	  if {$ip2c(switch)} {
	    set ip2c(switch) 0
	    putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Turned trigger status \($ip2c(boldset)off$ip2c(boldset)\)."
      	  } else {
      	    putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :Allready turned off."
      	  }
	} else {
	  putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] ::Usage: $ip2c(trigger) -set <on|off>."
	}
      }
    } else {
      if {[onchan [lindex $arg 0] $chan] && $ip2c(switch)} {
        dnslookup [lindex [split [getchanhost [lindex $arg 0]] \x40] 1] resolve:ipaddrnick [string map {\\ \\\\ \[ \\\[ \] \\\] \{ \\\{ \} \\\} \" \\\"} [lindex $arg 0]] [string map {\\ \\\\ \[ \\\[ \] \\\] \{ \\\{ \} \\\} \" \\\"} $chan]
      } elseif {$ip2c(switch)} {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :\($ip2c(boldset)[lindex $arg 0]$ip2c(boldset)\) is not on the current channel."
      }
    }
  }
}

proc bancountry {cc chan} {
  global botnick
  foreach line [chanlist $chan] {
    if {![string match -nocase $botnick $line] && ![matchattr $line o]} {
      dnslookup [lindex [split [getchanhost $line] \x40] 1] resolve:countryban $cc $chan
    }
  }
}

proc resolve:countryban {ipaddr hostname status cc chan} {
  if {[string match -nocase $cc [lindex [ip2country $ipaddr] 0]]} {
    pushmode $chan +b *!*@$hostname
  }
}

proc resolve:ipaddrnick {ipaddr hostname status nick chan} {
  global ip2c
  if {[ip2longip $ipaddr]} {
    if {[isipaddr $ipaddr]} {
      set output [ip2country $ipaddr]
      set result(0) "[lindex $output 0]"
      set result(1) "[lindex $output 1]"
      set result(2) "[join [lrange $output 2 end]]"
      if {$result(0) != "00"} {
        foreach line [split $result(2) \x20] {
          if {[info exists word]} {
            append word \x20[word_format $line]
          } else {
            set word [word_format $line]
          }
        }
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :15\[nob0dy-IpChk4!15\] Resolved Host 7\(6$ip2c(boldset)$result(0), [join [lrange $word 0 end]]$ip2c(boldset)7\)"
      } else {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$result(2)"
      }
    }

  } else {
    putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :15\[nob0dy-IpChk4!15\] There iS No IP 7\(6$ip2c(boldset)$hostname$ip2c(boldset)7\) For 7\(6$ip2c(boldset)$nick$ip2c(boldset)7\)."
  }
}

proc resolve:ipaddrhost {ipaddr hostname status nick chan} {
  global ip2c
  if {[ip2longip $ipaddr]} {
    if {[isipaddr $ipaddr]} {
      set output [ip2country $ipaddr]
      set result(0) "[lindex $output 0]"
      set result(1) "[lindex $output 1]"
      set result(2) "[join [lrange $output 2 end]]"
      if {$result(0) != "00"} {
        foreach line [split $result(2) \x20] {
          if {[info exists word]} {
            append word \x20[word_format $line]
          } else {
            set word [word_format $line]
          }
        }
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :15\[nob0dy-IpChk4!15\] $hostname This ip From 7\(6$ip2c(boldset)$result(0), [join [lrange $word 0 end]]$ip2c(boldset)7\)"
      } else {
        putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :$result(2)"
      }
    }

  } else {
    putserv "[string map [list %CHAN% $chan %NICK% $nick] $ip2c(putserv)] :15\[nob0dy-IpChk4!15\] There iS No IP 7\(6$ip2c(boldset)$hostname$ip2c(boldset)7\)."
  }
}

proc reverse_string {arg} {
   set out {}
   foreach line [split $arg {}] {set out $line$out} 
   set out
}

proc ip2longip {ipaddr} {
  foreach ipbyte [split $ipaddr \x2E] { 
    append hexaddr [format {%02x} $ipbyte] 
  } 
  return [format {%u} "0x$hexaddr"] 
}

proc longip2ip {longip} {
  return [expr {$longip>>24&255}]\x2E[expr {$longip>>16&255}]\x2E[expr {$longip>>8&255}]\x2E[expr {$longip&255}]
}

proc isipaddr {ipaddr} {
  if {[string match -nocase {} $ipaddr]} {
    return 0
  }
  foreach line [split $ipaddr \x2E] {
    if {![regexp ^\[0-9\]+$ $line]} {
      return 0
    } elseif {$line > 255} {
      return 0
    }
    if {[info exists count]} {
      incr count
    } else {
      set count 1
    }
  }
  if {$count != 4} {
    return 0
  } else {
    return 1
  }
}

proc isvalidip {ipaddr} {
  if {(([ip2longip $ipaddr] >= 2130706432) && ([ip2longip $ipaddr] <= 2130706687)) || (([ip2longip $ipaddr] >= 167772160) && ([ip2longip $ipaddr] <= 184549375)) || (([ip2longip $ipaddr] >= 2886729728) && ([ip2longip $ipaddr] <= 2887778303)) || (([ip2longip $ipaddr] >= 3232235520) && ([ip2longip $ipaddr] <= 3232301055)) || (([ip2longip $ipaddr] >= 2851995648) && ([ip2longip $ipaddr] <= 2852061183)) || (([ip2longip $ipaddr] >= 3758096384) && ([ip2longip $ipaddr] <= 4026531839))} {
    return 0
  } else {
    return 1
  }
}

proc ishost {host} {
  foreach {a b} [split [reverse_string $host] \x2E] break
  if {[regexp -all {[a-zA-Z]} $a] && [regexp -all {[a-zA-Z0-9]} $b]} {
    if {![string match -nocase {} $a] && ![string match -nocase {} $b]} {
      return 1
    } else {
      return 0
    }
  } else {
    return 0
  }
}

proc word_format {arg} {
  foreach line [split [string tolower $arg] {}] {
    if {[info exists word]} {
      append word $line
    } else {
      set word [string toupper $line]
    }
  }
  return $word
}

proc ip2country {ipaddr} {
  global ip2c mysql
  if {![info exists mysql(conn)] && $ip2c(sql)} {
    set mysql(conn) [mysqlconnect -host $mysql(host) -port $mysql(port) -user $mysql(user) -password $mysql(pass)]
  }
  if {[isvalidip $ipaddr]} {
    if {$ip2c(sql)} {
      if {[ip2longip $ipaddr]} {
        mysqluse $mysql(conn) $mysql(data)
	foreach query [mysqlsel $mysql(conn) "SELECT resolve_start,resolve_end,resolve_code_2,resolve_code_3,resolve_fullname FROM resolve WHERE ([ip2longip $ipaddr] >= resolve_start) and ([ip2longip $ipaddr] <= resolve_end);" -list] {
	  if {![info exists test]} {
	    set location(0) "[lindex $query 2]"
	    set location(1) "[lindex $query 3]"
	    set location(2) "[join [lrange $query 4 end]]"
	  } else {
	    putlog "Error to many results."
	  }
	}
      }
      if {[array exists location]} {
        if {[string match -nocase {} $location(0)]} {
	  set location(0) "00"
        }
	if {[string match -nocase {} $location(1)]} {
	  set location(1) "000"
	}
	if {[string match -nocase {} $location(2)]} {
	  set location(2) "Unknown"
	}
	return "$location(0) $location(1) $location(2)"
      } else {
        return "00 000 Couldn't resolve \($ip2c(boldset)$ipaddr$ip2c(boldset)\) to a country."
      }
    } else {
      if {[file exists $ip2c(database)]} {
        if {[ip2longip $ipaddr]} {
          set rfile [open "$ip2c(database)" r]
          while {![eof $rfile]} {
            gets $rfile line
	    set data [string map {\x22 {} \x2C \x20} $line]
            if {[ip2longip $ipaddr] >= [lindex $data 0]} {
	      if {[ip2longip $ipaddr] <= [lindex $data 1]} {
	        set location(0) [lindex $data 2]
	        set location(1) [lindex $data 3]
	        set location(2) [join [lrange $data 4 end]]
	        break
	      }
            }
          }
          close $rfile
	  if {[array exists location]} {
	    return "$location(0) $location(1) $location(2)"
	  } else {
	    return "00 000 Couldn't resolve \($ip2c(boldset)$ipaddr$ip2c(boldset)\) to a country."
	  }
        }
      } else {
        putlog "Can't find the cvs database."
      }
    }
  } else {
    return "00 000 Bego BaNGed dAH iTUkaN Ip $ip2c(boldset)Localhost$ip2c(boldset) IdiOT!."
  }
}

proc isstatus {nick chan} {
  global ip2c
  if {$ip2c(mode)} {
    if {[isvoice $nick $chan] && ($ip2c(mode) == 1)} {
      return 1
    } elseif {[ishalfop $nick $chan] && ($ip2c(mode) <= 2)} {
      return 1
    } elseif {[isop $nick $chan] && ($ip2c(mode) <= 3)} {
      return 1
    } else {
      return 0
    }
  } else {
    return 1
  }
}

proc newupdates {} {
  global ip2c
  catch {socket update.ofloo.net 80} sock
  if {[string match -nocase "sock??" $sock]} {
    flush $sock
    puts $sock "GET /ip-to-country HTTP/1.1"
    puts $sock "Host: update.ofloo.net"
    puts $sock "Connection: Close"
    puts $sock "User-Agent: Ip-to-Country updater"
    puts $sock "\n\r"
    while {![eof $sock]} {
      flush $sock
      gets $sock line
      if {[string match -nocase "<tag>*</tag>" [lindex $line 0]]} {
        if {[regexp -all {[0-9]} [string map {<tag> {} </tag> {}} [lindex $line 0]]] && ![string match -nocase {} [lindex $line 0]]} {
          if {[string map {<tag> {} </tag> {}} [lindex $line 0]] > $ip2c(tag)} {
            set return "0"
          } else {
            set return "1"
          }
        }
      }
    }
    close $sock
  } else {
    set return "-1"
  }
  return $return
}

proc raw:302 {from key arg} {
  global botnick
  if {[string match -nocase [lindex [split [lindex [split [join [lrange $arg 0 end]] \x3D] 0] \x3A] 1] $botnick]} {
    dnslookup "[lindex [split [join [lrange $arg 0 end]] \x40] 1]" resolve:raw
  }
}

proc resolve:raw {ipaddr hostname status} {
  global MyIP
  set MyIP "$ipaddr"
}

proc MyIP {} {
  global MyIP
  if {[info exists MyIP]} {
  
    if {[string match {} $MyIP] || [ip2longip $MyIP] || [isvalidip $MyIP]} {
      set MyIP {}
      foreach a {a b c d e f g h i j k} { 
        catch { 
          set external [socket $a.root-servers.net 53] 
          set MyIP [lindex [fconfigure $external -sockname] 0] 
          close $external             
        } 
        if { ![string equal $MyIP {}] } { 
	  break 
	} 
      }
      return $MyIP
    } else {
      return $MyIP	    
    }
  } else {
    set MyIP {}
    foreach a {a b c d e f g h i j k} { 
      catch { 
        set external [socket $a.root-servers.net 53] 
        set MyIP [lindex [fconfigure $external -sockname] 0] 
        close $external             
      } 
      if { ![string equal $MyIP {}] } { 
        break 
      } 
    }
    return $MyIP
  }
}

proc conn:init init-server { 
  global botnick
  putserv "USERHOST $botnick"
}

proc location:type0 {} {
  set location [lindex [ip2country [MyIP]] 0]
  if {[regexp -all {[0-9]} $location]} {
    set location "Unknown"
  }
  return $location
}

proc location:type1 {} {
  set location [lindex [ip2country [MyIP]] 1]
  if {[regexp -all {[0-9]} $location]} {
    set location "Unknown"
  }
  return $location
}

proc location:type2 {} {
  foreach line [split [join [lrange [ip2country [MyIP]]] 2 end] \x20] {
    if {[info exists location]} {
      append location \x20[word_format $line]
    } else {
      set location [word_format $line]
    }
  }
  if {[regexp -all {[0-9]} [lindex [ip2country [MyIP]] 0]]} {
    set location "Unknown"
  }
  return $location
}

# this section will be rewritten in the futur
#BOTNET DCC COMMANDS
bind dcc n netip netip:dcc 
bind dcc n netvrs netvrs:dcc
bind dcc n netloc netloc:dcc

#SHOW IP OVER BOTNET
proc netip:dcc {hand idx arg} { 
  global botnick
  putdcc $idx "$ip2c(boldset)$botnick$ip2c(boldset) current ip is: $ip2c(boldset)[MyIP]$ip2c(boldset)"
  putallbots "botdcccmd $idx getip" 
} 

#SHOW SCRIPT VERSION OVER BOTNET
proc netvrs:dcc {hand idx arg} {
  global ip2c botnick
  putdcc $idx "$ip2c(boldset)$botnick$ip2c(boldset) current script version is: $ip2c(boldset)$ip2c(vrs)$ip2c(boldset)"
  putallbots "botdcccmd $idx scriptversion" 
} 

#SHOW LOCATION OVER BOTNET
proc netloc:dcc {hand idx arg} {
  global botnick
  putdcc $idx "$ip2c(boldset)$botnick$ip2c(boldset) location is: $ip2c(boldset)[location:type1]$ip2c(boldset)"
  putallbots "botdcccmd $idx botlocation"
}

#BOTNET ECHO RESULTS
bind bot - result result:bot 

proc result:bot {hand idx arg} { 
  putidx [lindex $arg 0] "$ip2c(boldset)$hand$ip2c(boldset) answered back: $ip2c(boldset)[join [lrange $arg 1 end]]$ip2c(boldset)" 
} 

#BOTNET ANSWER BACK 
bind bot - botdcccmd botdcccmd:bot 

proc botdcccmd:bot {hand idx arg} {
  global ip2c botnick
  switch -exact -- [strlwr [lindex $arg 1]] { 
    "getip" { 
      putbot $hand "result [lindex $arg 0] [MyIP]" 
    } 
    "scriptversion" { 
      putbot $hand "result [lindex $arg 0] $ip2c(vrs)" 
    }
    "botlocation" {
      putbot $hand "result [lindex $arg 0] [location:type1]"
    }
  } 
}

putlog "\002Ip-to-Country.tcl\002 Modifed bY Vaksin13 \002$ip2c(vrs)\002."


