home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Shareware / Comunicatii / nat32 / mail < prev    next >
Text File  |  2004-03-26  |  1KB  |  76 lines

  1. #!tcl
  2.  
  3. #
  4. # Compute the mail server for a Domain or an IP or the Primary IP
  5. #
  6. # Usage: mail [ip|domain]
  7. #
  8.  
  9. # Check args
  10.  
  11. if {$argc > 1} {
  12.     error "Usage: mail [ip|domain]
  13. }
  14.  
  15. # Get the argument
  16.  
  17. if {$argc == 1} {
  18.     set myarg [lindex $argv 0]
  19. } else {
  20.     set myarg [exec ip p]
  21. }
  22.  
  23. # Is myarg a name or an address?
  24. # If it ends with a letter, it must be a name.
  25.  
  26. set t [string match *\[a-z\] $myarg]
  27.  
  28. if {$t == 0} {
  29.     # Convert the IP to its DNS name
  30.  
  31.     set response [exec ns $myarg]
  32.     set t [string first "ERROR: " $response]
  33.     if {$t != -1} {
  34.         set domain [exec setd]
  35.  
  36.         if {$domain == ""} {
  37.             error "ERROR: could not resolve $myarg"
  38.         }
  39.         set host localhost.$domain
  40.     } else {
  41.         set host [lindex $response 1]
  42.     }
  43.  
  44.     # Strip the hostname to get the domain name
  45.  
  46.     set t [string first "." $host]
  47.     if {$t == -1} {
  48.         error "ERROR: $host invalid"
  49.     }
  50.     incr t
  51.     set domain [string range $host $t end]
  52. } else {
  53.     set domain $myarg
  54. }
  55.  
  56. # Get the domain's MX record
  57.  
  58. set response [exec ns -m $domain]
  59. set t [string first "ERROR: " $response]
  60. if {$t != -1} {
  61.     error $response
  62. }
  63.  
  64. # Print the first MX name
  65.  
  66. set t [string first ":MX " $response]
  67. if {$t == -1} {
  68.     error "ERROR: no MX record returned.\n$domain may be a host name, not a domain name."
  69. }
  70. set host [lindex $response 2]
  71.  
  72. exec set mail $host
  73.  
  74. echo $host
  75.  
  76.