Networking module: add Whois command; related to issue #4
This commit is contained in:
parent
14e2c59064
commit
d549d91aca
@ -1,5 +1,6 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
|
import datetime
|
||||||
import http.client
|
import http.client
|
||||||
import json
|
import json
|
||||||
import socket
|
import socket
|
||||||
@ -11,6 +12,15 @@ nemubotversion = 3.3
|
|||||||
|
|
||||||
def load(context):
|
def load(context):
|
||||||
from hooks import Hook
|
from hooks import Hook
|
||||||
|
|
||||||
|
if not CONF.hasNode("whoisxmlapi") or not CONF.getNode("whoisxmlapi").hasAttribute("username") or not CONF.getNode("whoisxmlapi").hasAttribute("password"):
|
||||||
|
print ("You need a WhoisXML API account in order to use the "
|
||||||
|
"!netwhois feature. Add it to the module configuration file:\n"
|
||||||
|
"<whoisxmlapi username=\"XX\" password=\"XXX\" />\nRegister at "
|
||||||
|
"http://www.whoisxmlapi.com/newaccount.php")
|
||||||
|
else:
|
||||||
|
add_hook("cmd_hook", Hook(cmd_whois, "netwhois"))
|
||||||
|
|
||||||
add_hook("cmd_hook", Hook(cmd_traceurl, "traceurl"))
|
add_hook("cmd_hook", Hook(cmd_traceurl, "traceurl"))
|
||||||
add_hook("cmd_hook", Hook(cmd_isup, "isup"))
|
add_hook("cmd_hook", Hook(cmd_isup, "isup"))
|
||||||
add_hook("cmd_hook", Hook(cmd_curl, "curl"))
|
add_hook("cmd_hook", Hook(cmd_curl, "curl"))
|
||||||
@ -48,7 +58,81 @@ def cmd_traceurl(msg):
|
|||||||
res.append(Response(msg.sender, trace, channel=msg.channel, title="TraceURL"))
|
res.append(Response(msg.sender, trace, channel=msg.channel, title="TraceURL"))
|
||||||
return res
|
return res
|
||||||
else:
|
else:
|
||||||
return Response(msg.sender, "Indiquer une URL a tracer !", channel=msg.channel)
|
return Response(msg.sender, "Indiquer une URL à tracer !", channel=msg.channel)
|
||||||
|
|
||||||
|
|
||||||
|
def extractdate(str):
|
||||||
|
tries = [
|
||||||
|
"%Y-%m-%dT%H:%M:%S%Z",
|
||||||
|
"%Y-%m-%dT%H:%M:%S%z",
|
||||||
|
"%Y-%m-%dT%H:%M:%SZ",
|
||||||
|
"%Y-%m-%dT%H:%M:%S",
|
||||||
|
"%Y-%m-%d %H:%M:%S%Z",
|
||||||
|
"%Y-%m-%d %H:%M:%S%z",
|
||||||
|
"%Y-%m-%d %H:%M:%SZ",
|
||||||
|
"%Y-%m-%d %H:%M:%S",
|
||||||
|
"%Y-%m-%d",
|
||||||
|
"%d/%m/%Y",
|
||||||
|
]
|
||||||
|
|
||||||
|
for t in tries:
|
||||||
|
try:
|
||||||
|
return datetime.datetime.strptime(str, t)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
return datetime.datetime.strptime(str, t)
|
||||||
|
|
||||||
|
def whois_entityformat(entity):
|
||||||
|
ret = ""
|
||||||
|
if "organization" in entity:
|
||||||
|
ret += entity["organization"]
|
||||||
|
if "name" in entity:
|
||||||
|
ret += entity["name"]
|
||||||
|
|
||||||
|
if "country" in entity or "city" in entity or "telephone" in entity or "email" in entity:
|
||||||
|
ret += " (from "
|
||||||
|
if "street1" in entity:
|
||||||
|
ret += entity["street1"] + " "
|
||||||
|
if "city" in entity:
|
||||||
|
ret += entity["city"] + " "
|
||||||
|
if "state" in entity:
|
||||||
|
ret += entity["state"] + " "
|
||||||
|
if "country" in entity:
|
||||||
|
ret += entity["country"] + " "
|
||||||
|
if "telephone" in entity:
|
||||||
|
ret += entity["telephone"] + " "
|
||||||
|
if "email" in entity:
|
||||||
|
ret += entity["email"] + " "
|
||||||
|
ret = ret.rstrip() + ")"
|
||||||
|
|
||||||
|
return ret.lstrip()
|
||||||
|
|
||||||
|
def cmd_whois(msg):
|
||||||
|
if len(msg.cmds) < 2:
|
||||||
|
raise IRCException("Indiquer un domaine ou une IP à whois !")
|
||||||
|
|
||||||
|
dom = msg.cmds[1]
|
||||||
|
|
||||||
|
try:
|
||||||
|
req = urllib.request.Request("http://www.whoisxmlapi.com/whoisserver/WhoisService?rid=1&domainName=%s&outputFormat=json&userName=%s&password=%s" % (urllib.parse.quote(dom), urllib.parse.quote(CONF.getNode("whoisxmlapi")["username"]), urllib.parse.quote(CONF.getNode("whoisxmlapi")["password"])), headers={ 'User-Agent' : "nemubot v3" })
|
||||||
|
raw = urllib.request.urlopen(req, timeout=10)
|
||||||
|
except urllib.error.HTTPError as e:
|
||||||
|
raise IRCException("HTTP error occurs: %s %s" % (e.code, e.reason))
|
||||||
|
|
||||||
|
whois = json.loads(raw.read().decode())["WhoisRecord"]
|
||||||
|
|
||||||
|
res = Response(msg.sender, channel=msg.channel, nomore="No more whois information")
|
||||||
|
|
||||||
|
res.append_message("%s: %s%s%s%s\x03\x02registered by\x03\x02 %s, \x03\x02administrated by\x03\x02 %s, \x03\x02managed by\x03\x02 %s" % (whois["domainName"],
|
||||||
|
whois["status"] + " " if "status" in whois else "",
|
||||||
|
"\x03\x02created on\x03\x02 " + extractdate(whois["createdDate"]).strftime("%c") + ", " if "createdDate" in whois else "",
|
||||||
|
"\x03\x02updated on\x03\x02 " + extractdate(whois["updatedDate"]).strftime("%c") + ", " if "updatedDate" in whois else "",
|
||||||
|
"\x03\x02expires on\x03\x02 " + extractdate(whois["expiresDate"]).strftime("%c") + ", " if "expiresDate" in whois else "",
|
||||||
|
whois_entityformat(whois["registrant"]),
|
||||||
|
whois_entityformat(whois["administrativeContact"]),
|
||||||
|
whois_entityformat(whois["technicalContact"]),
|
||||||
|
))
|
||||||
|
return res
|
||||||
|
|
||||||
def cmd_isup(msg):
|
def cmd_isup(msg):
|
||||||
if 1 < len(msg.cmds) < 6:
|
if 1 < len(msg.cmds) < 6:
|
||||||
|
Loading…
Reference in New Issue
Block a user