]> jfr.im git - irc/quakenet/qwebirc.git/blobdiff - qwebirc/dns.py
add dynamic configuration support
[irc/quakenet/qwebirc.git] / qwebirc / dns.py
index 4f5e9126f78f202c327b9d151fe2786c246a4007..357a8d9ccb3d83ef4561ea0a9297cf3c9d6c50cb 100644 (file)
@@ -1,4 +1,4 @@
-from twisted.names import client
+from twisted.names import client, dns
 from twisted.internet import reactor, defer
 
 class LookupException(Exception): pass
@@ -8,7 +8,7 @@ TimeoutException = defer.TimeoutError
 def lookupPTR(ip, *args, **kwargs):
   def callback(result):
     answer, auth, add = result
-
+    answer = [x for x in answer if x.type == dns.PTR]
     if len(answer) == 0:
       raise LookupException, "No ANSWERS in PTR response for %s." % repr(ip)
     return str(answer[0].payload.name)
@@ -17,15 +17,17 @@ def lookupPTR(ip, *args, **kwargs):
   return client.lookupPointer(ptr, **kwargs).addCallback(callback)
 
 def expandIPv6(ip):
-  expand_start, expand_end = ["".join(["{:0>4}".format(group)
+  expand_sections = ["".join(["{:0>4}".format(group)
       for group in section.split(":")])
-        for section in ip.split("::")]
-  return expand_start + (32-len(expand_start)-len(expand_end))*"0" + expand_end
+        for section in ip.split("::", 1)]
+  if len(expand_sections) == 1:
+      return expand_sections[0]
+  return expand_sections[0] + "".join((32-sum([len(x) for x in expand_sections]))*"0") + expand_sections[1]
 
 def lookupPTRv6(ip, *args, **kwargs):
   def callback(result):
     answer, auth, add = result
-
+    answer = [x for x in answer if x.type == dns.PTR]
     if len(answer) == 0:
       raise LookupException, "No ANSWERS in PTR response for %s." % repr(ip)
     return str(answer[0].payload.name)
@@ -36,6 +38,7 @@ def lookupPTRv6(ip, *args, **kwargs):
 def lookupAs(hostname, *args, **kwargs):
   def callback(result):
     answer, auth, add = result
+    answer = [x for x in answer if x.type == dns.A]
     if len(answer) == 0:
       raise LookupException, "No ANSWERS in A response for %s." % repr(hostname)
     return [x.payload.dottedQuad() for x in answer]
@@ -45,6 +48,7 @@ def lookupAs(hostname, *args, **kwargs):
 def lookupAAAAs(hostname, *args, **kwargs):
   def callback(result):
     answer, auth, add = result
+    answer = [x for x in answer if x.type == dns.AAAA]
     if len(answer) == 0:
       raise LookupException, "No ANSWERS in AAAA response for %s." % repr(hostname)
     return [expandIPv6(x.payload._address) for x in answer]