[networking] Dusting module

This commit is contained in:
nemunaire 2015-10-12 06:28:52 +02:00
parent eb95480c8f
commit 2cd8f70cdc
3 changed files with 73 additions and 49 deletions

View File

@ -1,16 +1,13 @@
# coding=utf-8
"""Various network tools (w3m, w3c validator, curl, traceurl, ...)""" """Various network tools (w3m, w3c validator, curl, traceurl, ...)"""
# PYTHON STUFFS #######################################################
import logging import logging
import re import re
from nemubot.exception import IRCException from nemubot.exception import IRCException
from nemubot.hooks import hook from nemubot.hooks import hook
logger = logging.getLogger("nemubot.module.networking")
nemubotversion = 3.4
from more import Response from more import Response
from . import isup from . import isup
@ -19,6 +16,11 @@ from . import w3c
from . import watchWebsite from . import watchWebsite
from . import whois from . import whois
logger = logging.getLogger("nemubot.module.networking")
# LOADING #############################################################
def load(context): def load(context):
for mod in [isup, page, w3c, watchWebsite, whois]: for mod in [isup, page, w3c, watchWebsite, whois]:
mod.add_event = context.add_event mod.add_event = context.add_event
@ -34,11 +36,11 @@ def load(context):
logger.exception("Unable to load netwhois module") logger.exception("Unable to load netwhois module")
def help_full(): # MODULE INTERFACE ####################################################
return "!traceurl /url/: Follow redirections from /url/."
@hook("cmd_hook", "title",
@hook("cmd_hook", "title") help="Retrieve webpage's title",
help_usage={"URL": "Display the title of the given URL"})
def cmd_title(msg): def cmd_title(msg):
if not len(msg.args): if not len(msg.args):
raise IRCException("Indicate the URL to visit.") raise IRCException("Indicate the URL to visit.")
@ -52,7 +54,9 @@ def cmd_title(msg):
return Response("%s: %s" % (url, res.group(1).replace("\n", " ")), channel=msg.channel) return Response("%s: %s" % (url, res.group(1).replace("\n", " ")), channel=msg.channel)
@hook("cmd_hook", "curly") @hook("cmd_hook", "curly",
help="Retrieve webpage's headers",
help_usage={"URL": "Display HTTP headers of the given URL"})
def cmd_curly(msg): def cmd_curly(msg):
if not len(msg.args): if not len(msg.args):
raise IRCException("Indicate the URL to visit.") raise IRCException("Indicate the URL to visit.")
@ -63,7 +67,9 @@ def cmd_curly(msg):
return Response("Entêtes de la page %s : HTTP/%s, statut : %d %s ; headers : %s" % (url, version, status, reason, ", ".join(["\x03\x02" + h + "\x03\x02: " + v for h, v in headers])), channel=msg.channel) return Response("Entêtes de la page %s : HTTP/%s, statut : %d %s ; headers : %s" % (url, version, status, reason, ", ".join(["\x03\x02" + h + "\x03\x02: " + v for h, v in headers])), channel=msg.channel)
@hook("cmd_hook", "curl") @hook("cmd_hook", "curl",
help="Retrieve webpage's body",
help_usage={"URL": "Display raw HTTP body of the given URL"})
def cmd_curl(msg): def cmd_curl(msg):
if not len(msg.args): if not len(msg.args):
raise IRCException("Indicate the URL to visit.") raise IRCException("Indicate the URL to visit.")
@ -74,48 +80,55 @@ def cmd_curl(msg):
return res return res
@hook("cmd_hook", "w3m") @hook("cmd_hook", "w3m",
help="Retrieve and format webpage's content",
help_usage={"URL": "Display and format HTTP content of the given URL"})
def cmd_w3m(msg): def cmd_w3m(msg):
if len(msg.args): if not len(msg.args):
raise IRCException("Indicate the URL to visit.")
res = Response(channel=msg.channel) res = Response(channel=msg.channel)
for line in page.render(" ".join(msg.args)).split("\n"): for line in page.render(" ".join(msg.args)).split("\n"):
res.append_message(line) res.append_message(line)
return res return res
else:
raise IRCException("Indicate the URL to visit.")
@hook("cmd_hook", "traceurl") @hook("cmd_hook", "traceurl",
help="Follow redirections of a given URL and display each step",
help_usage={"URL": "Display redirections steps for the given URL"})
def cmd_traceurl(msg): def cmd_traceurl(msg):
if 0 < len(msg.args) < 5: if not len(msg.args):
raise IRCException("Indicate an URL to trace!")
res = list() res = list()
for url in msg.args: for url in msg.args[:4]:
try: try:
trace = page.traceURL(url) trace = page.traceURL(url)
res.append(Response(trace, channel=msg.channel, title="TraceURL")) res.append(Response(trace, channel=msg.channel, title="TraceURL"))
except: except:
pass pass
return res return res
else:
raise IRCException("Indicate an URL to trace!")
@hook("cmd_hook", "isup") @hook("cmd_hook", "isup",
help="Check if a website is up",
help_usage={"DOMAIN": "Check if a DOMAIN is up"})
def cmd_isup(msg): def cmd_isup(msg):
if 1 < len(msg.args) < 5: if not len(msg.args):
raise IRCException("Indicate an domain name to check!")
res = list() res = list()
for url in msg.args: for url in msg.args[:4]:
rep = isup.isup(url) rep = isup.isup(url)
if rep: if rep:
res.append(Response("%s is up (response time: %ss)" % (url, rep), channel=msg.channel)) res.append(Response("%s is up (response time: %ss)" % (url, rep), channel=msg.channel))
else: else:
res.append(Response("%s is down" % (url), channel=msg.channel)) res.append(Response("%s is down" % (url), channel=msg.channel))
return res return res
else:
return Response("Indicate an URL to check!", channel=msg.channel)
@hook("cmd_hook", "w3c") @hook("cmd_hook", "w3c",
help="Perform a w3c HTML validator check",
help_usage={"URL": "Do W3C HTML validation on the given URL"})
def cmd_w3c(msg): def cmd_w3c(msg):
if not len(msg.args): if not len(msg.args):
raise IRCException("Indicate an URL to validate!") raise IRCException("Indicate an URL to validate!")
@ -136,8 +149,12 @@ def cmd_w3c(msg):
@hook("cmd_hook", "watch", data="diff") @hook("cmd_hook", "watch", data="diff",
@hook("cmd_hook", "updown", data="updown") help="Alert on webpage change",
help_usage={"URL": "Watch the given URL and alert when it changes"})
@hook("cmd_hook", "updown", data="updown",
help="Alert on server availability change",
help_usage={"URL": "Watch the given domain and alert when it availability status changes"})
def cmd_watch(msg, diffType="diff"): def cmd_watch(msg, diffType="diff"):
if not len(msg.args): if not len(msg.args):
raise IRCException("indicate an URL to watch!") raise IRCException("indicate an URL to watch!")
@ -145,7 +162,9 @@ def cmd_watch(msg, diffType="diff"):
return watchWebsite.add_site(msg.args[0], msg.frm, msg.channel, msg.server, diffType) return watchWebsite.add_site(msg.args[0], msg.frm, msg.channel, msg.server, diffType)
@hook("cmd_hook", "listwatch") @hook("cmd_hook", "listwatch",
help="List URL watched for the channel",
help_usage={None: "List URL watched for the channel"})
def cmd_listwatch(msg): def cmd_listwatch(msg):
wl = watchWebsite.watchedon(msg.channel) wl = watchWebsite.watchedon(msg.channel)
if len(wl): if len(wl):
@ -154,9 +173,12 @@ def cmd_listwatch(msg):
return Response("No URL are currently watched. Use !watch URL to watch one.", channel=msg.channel) return Response("No URL are currently watched. Use !watch URL to watch one.", channel=msg.channel)
@hook("cmd_hook", "unwatch") @hook("cmd_hook", "unwatch",
help="Unwatch a previously watched URL",
help_usage={"URL": "Unwatch the given URL"})
def cmd_unwatch(msg): def cmd_unwatch(msg):
if not len(msg.args): if not len(msg.args):
raise IRCException("which URL should I stop watching?") raise IRCException("which URL should I stop watching?")
return watchWebsite.del_site(msg.args[0], msg.frm, msg.channel, msg.frm_owner) for arg in msg.args:
return watchWebsite.del_site(arg, msg.frm, msg.channel, msg.frm_owner)

View File

@ -66,7 +66,7 @@ def fetch(url, onNone=_onNoneDefault):
if req is not None: if req is not None:
return req return req
else: else:
if onNone is not None: if callable(onNone):
return onNone() return onNone()
else: else:
return None return None

View File

@ -21,7 +21,9 @@ def load(CONF, add_hook):
URL_WHOIS = URL_WHOIS % (urllib.parse.quote(CONF.getNode("whoisxmlapi")["username"]), urllib.parse.quote(CONF.getNode("whoisxmlapi")["password"])) URL_WHOIS = URL_WHOIS % (urllib.parse.quote(CONF.getNode("whoisxmlapi")["username"]), urllib.parse.quote(CONF.getNode("whoisxmlapi")["password"]))
import nemubot.hooks import nemubot.hooks
add_hook("cmd_hook", nemubot.hooks.Message(cmd_whois, "netwhois")) add_hook("cmd_hook", nemubot.hooks.Message(cmd_whois, "netwhois",
help="Get whois information about given domains",
help_usage={"DOMAIN": "Return whois information on the given DOMAIN"}))
def extractdate(str): def extractdate(str):