bitbot-3.11-fork/modules/dns.py
jesopo 8c6ab17e57 give an Exports object (actually, ExportsContex object) to each module, to
facilitate things like !set and !channelset without using the events system
2018-09-02 19:54:45 +01:00

22 lines
759 B
Python

import socket
class Module(object):
_name = "DNS"
def __init__(self, bot, events, exports):
events.on("received").on("command").on("dns").hook(
self.dns, min_args=1,
help="Get all addresses for a given hostname (IPv4/IPv6)",
usage="<hostname>")
def dns(self, event):
hostname = event["args_split"][0]
try:
address_info = socket.getaddrinfo(hostname, 1, 0,
socket.SOCK_DGRAM)
except socket.gaierror:
event["stderr"].write("Failed to find hostname")
return
ips = []
for _, _, _, _, address in address_info:
ips.append(address[0])
event["stdout"].write("%s: %s" % (hostname, ", ".join(ips)))