#by wiebe @ quakenet
#resolving a host to multiple IPs works only on eggdrops (not windrops)
#add dcc cmd



#dns pub
#bind pub -|- !dns dns:pub

proc dns:pub { nick uhost handle chan text } {
  if { ![validchan $chan] } { return 0 }
  if { ![botonchan $chan] } { return 0 }
  set mask [lindex [split $text] 0]
  if { [string equal $mask ""] } {
    lappend output "Usage: dns <ip>|<host>"
    if { [catch {set x [cnotice $nick $output]} error] || !$x } {
      foreach t $output { puthelp "NOTICE $nick :$t" }
    }
    return 0
  }
  if { ![string match -nocase *CYGWIN* [unames]] && ![regexp {^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$} $mask] && [string match -nocase "* has address *" [set results [split [exec host $mask] \n]]] } {
    set a ""
    foreach r $results {
      if { [string match -nocase "* has address *" $r] } {
        if { [string equal $a ""] } {
          set a [lindex [split $r]  3]
        } else {
          set a "$a    [lindex [split $r]  3]"
        }
      }
    }
    set msg "DNS: result for $mask \017is $a"
    if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } {
      set msg [stripcodes c $msg]
    }
    puthelp "PRIVMSG $chan :$msg"
  } else {
    dnslookup $mask dns:callback $chan $mask
  }
  return 1
}



#dns callback
proc dns:callback { ip host status chan mask } {
  if { ![validchan $chan] } { return 0 }
  if { ![botonchan $chan] } { return 0 }
  set result $ip
  if { [string equal -nocase $mask $result] } {
    set result $host
  }
  if { [string equal $status 0] } {
    set msg "DNS: result for $mask \017is unresolved."
    if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } {
      set msg [stripcodes c $msg]
    }
    puthelp "PRIVMSG $chan :$msg"
  } else {
    set msg "DNS: result for $mask \017is $result"
    if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } {
      set msg [stripcodes c $msg]
    }
    puthelp "PRIVMSG $chan :$msg"
  }
}

