Project

General

Profile

/dns alias that mimics hexchat's /dns alias

Added by Per Amundsen about 8 years ago

/dns alias that mimics hexchat's /dns alias.

; Lookup a IP address or hostname.
;/dns google.com
;/dns 8.8.8.8

; Override the /dns alias to print the lookup text.
alias dns {
  ; Print lookup text.
  echo -a Looking up $iif($left($1,1) == -,$2,$1) $+ ...

  ; Send the request to the built-in alias.
  .!dns $1-
}

; Listen to the DNS event and print the results from the lookups.
on *:DNS:{     
  ; Check if the input was a valid ip.
  var %isip = $regex($dns(1), /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/)

  ; If the input was hostname, print each ip found.
  if (: !isin $dns(1) && !%isip) {  
    ; Print the resolved to text.
    echo -a Resolved to:

    var %n = $dns(0)  
    while (%n > 0) {
      echo -a $dns(%n).ip
      dec %n
    }
  }
  else {
    ; If the input was a ipv4 or ipv6 address, print the hostname.
    echo -a Resolved to $dns(1).addr
  }
}

Replies (2)

RE: /dns alias that mimics hexchat's /dns alias - Added by S Reject about 8 years ago

The more correct way would be to not override the /dns command but instead only change the dns event, and only when its needed:

on *:DNS:{
  var %end = $dns(0)
  var %get = ip
  var %index = 0
  var %outcmd = echo $color(info) -stg *

  ;; if halted or less than 2 resolved addresses were returned
  ;; let the client's internal handling take over
  if ($halted || %end < 2) {
    return
  }

  ;; if the input address was an ip
  ;; then the host addresses should be returned
  if ($dns(1) == $dns(0).ip) {
    %get = addr
  }

  ;; if the input was a nick output nick and address
  if ($dns(0).nick) {
    %outcmd Dns resolved $v1 $+ ( $+ $dns(1) $+ ) to:
  }

  ;; otherwise just output the address
  else {
    %outcmd Dns resolved $dns(1) to:
  }

  ;; loop of resolved results
  ;; outputting each address in the list
  while (%index < %end) {
    inc %index
    %outcmd $chr(35) $+ %index $+ : $dns(%index). [ $+ [ %get ] ]
  }
  haltdef
}

RE: /dns alias that mimics hexchat's /dns alias - Added by Per Amundsen about 8 years ago

Override /dns was for printing the same text output as hexchat, also was something I quickly threw together, I'm sure yours is better :p

    (1-2/2)