####################################################
# by wiebe @ QuakeNet
#
####################################################


####################################################
# dns:help:pubm
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} help dns" dns:help:pubm
proc dns:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "dns: usage dns <ip>|<host>"
  lappend o "dns: performs dns lookup and returns the result."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "dns: " } }
  putloglev c $c "help: $n $u $h $c dns"
  return 1
}


####################################################
# dns:help:msg
####################################################
bind msgm fvlomn|fvlomn "help dns" dns:help:msgm
proc dns:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "dns: usage dns <ip>|<host>"
  lappend o "dns: performs dns lookup and returns the result."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "dns: " } }
  putcmdlog "($n!$u) !$h! help dns"
  return 1
}


####################################################
# dns:pub
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} dns" dns:pubm
bind pubm fvlomn|fvlomn "% ${botnet-nick} dns *" dns:pubm
proc dns:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  set t [join [lrange [split $t] 2 end]]
  if {[string equal $t ""]} {
    lappend o "dns: usage dns <ip>|<host>"
  } else {
    set o [dns:dns $t $h $c $n]; set m [lindex [split [getchanmode $c]] 0]
    if {![string match *m* $m] || [botisop $c] || [botishalfop $c] || [botisvoice $c]} {
      if {[string match *c* $m] } { set o [stripcodes bcru $o] }
      if {[string equal [info procs privmsg] ""]} { foreach l $o { puthelp "PRIVMSG $c :$l" }
      } else { foreach l $o { privmsg $c $l puthelp "dns: " } }
      set o ""
    }
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "dns: " } }
  putloglev c $c "dns: $n $u $h $c $t"
  return 1
}


####################################################
# dns:msg
####################################################
bind msg fvlomn|fvlomn dns dns:msg
proc dns:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} { lappend o "dns: usage dns <ip>|<host>"
  } elseif {![onchan $n]} {  lappend o "dns: you are not on any of my channels"
  } else { set o [dns:dns $t $h $n] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "dns: " } }
  return 1
}


####################################################
# dns:dcc
####################################################
bind dcc -|- dns dns:dcc
proc dns:dcc { h i t } {
  if {[string equal $t ""]} {
    lappend o "dns: usage dns <ip>|<host>"
    lappend o "dns: performs dns lookup and returns the result."
  } else { set o [dns:dns $t $h $i] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# dns:dns
####################################################
proc dns:dns { i a p {n ""} } {
  set i [lindex [split $i] 0]; set o ""
  if {![catch {set s [split [exec host $i] \n]} s]} {
    set r ""; set a ""
    foreach h $s {
      if {[string match -nocase "* has address *" $h]} {
        lappend r "[lindex [split $h]  3]"
      } elseif {[string match -nocase "* domain name pointer *" $h]} {
        lappend r "[string range [lindex [split $h]  4] 0 end-1]"
      } elseif {[string match -nocase "* is an alias for *" $h]} {
        set h [string range [lindex [split $h] 5] 0 end-1]
        if {[string equal [lsearch -exact $a $h] "-1"]} { lappend a $h }
      }
    }
    if {[string equal $r$a ""]} { lappend o "dns: unable to resolve $i" }
    if {![string equal $r ""]} { lappend o "dns: result for $i is: [join $r "    "]" }
    if {![string equal $a ""]} { lappend o "dns: $i is an alias for: [join $a "   "]" }
  } else { dnslookup $i dns:callback $i $a $p $n }
  return $o
}


####################################################
# dns:callback
####################################################
proc dns:callback { i h s t a p n } {
  if {$s} {
    set r $i; if {[string equal -nocase $t $i]} { set r $h }
    lappend o "dns: result for $t\017 is: $r"
  } else { lappend o "dns: unable to resolve $t" }
  if {[valididx $p]} { foreach l $o { putidx $p $l }
  } elseif {[onchan $p] && [string equal [nick2hand $p] $a]} {
    if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $p :$l" }
    } else { foreach l $o { cnotice $p $l puthelp "dns: " } }
  } elseif {[validchan $p]} {
    set m [lindex [split [getchanmode $p]] 0]
    if {![string match "*m*" $m] || [botisop $p] || [botishalfop $p] || [botisvoice $p]} {
      if {[string match *c* $m]} { set o [stripcodes bcru $o] }
      if {[string equal [info procs privmsg] ""]} { foreach l $o { puthelp "PRIVMSG $p :$l" }
      } else { foreach l $o { privmsg $p $l puthelp "dns: " } }
    } elseif {[onchan $n] && [string equal [nick2hand $n] $a]} {
      if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
      } else { foreach l $o { cnotice $n $l puthelp "dns: " } }
    }
  }
}


####################################################
# dns:validip
####################################################
proc dns:validip { ip } {
  if {[regexp {^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$} $ip]} { return 1 }
  return 0
}


set scriptdb(dns) {
  "provides dns command to perform dns lookup"
}

