[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, ...)"""
# PYTHON STUFFS #######################################################
import logging
import re
from nemubot.exception import IRCException
from nemubot.hooks import hook
logger = logging.getLogger("nemubot.module.networking")
nemubotversion = 3.4
from more import Response
from . import isup
@ -19,6 +16,11 @@ from . import w3c
from . import watchWebsite
from . import whois
logger = logging.getLogger("nemubot.module.networking")
# LOADING #############################################################
def load(context):
for mod in [isup, page, w3c, watchWebsite, whois]:
mod.add_event = context.add_event
@ -34,11 +36,11 @@ def load(context):
logger.exception("Unable to load netwhois module")
def help_full():
return "!traceurl /url/: Follow redirections from /url/."
# MODULE INTERFACE ####################################################
@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):
if not len(msg.args):
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)
@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):
if not len(msg.args):
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)
@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):
if not len(msg.args):
raise IRCException("Indicate the URL to visit.")
@ -74,48 +80,55 @@ def cmd_curl(msg):
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):
if len(msg.args):
if not len(msg.args):
raise IRCException("Indicate the URL to visit.")
res = Response(channel=msg.channel)
for line in page.render(" ".join(msg.args)).split("\n"):
res.append_message(line)
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):
if 0 < len(msg.args) < 5:
if not len(msg.args):
raise IRCException("Indicate an URL to trace!")
res = list()
for url in msg.args:
for url in msg.args[:4]:
try:
trace = page.traceURL(url)
res.append(Response(trace, channel=msg.channel, title="TraceURL"))
except:
pass
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):
if 1 < len(msg.args) < 5:
if not len(msg.args):
raise IRCException("Indicate an domain name to check!")
res = list()
for url in msg.args:
for url in msg.args[:4]:
rep = isup.isup(url)
if rep:
res.append(Response("%s is up (response time: %ss)" % (url, rep), channel=msg.channel))
else:
res.append(Response("%s is down" % (url), channel=msg.channel))
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):
if not len(msg.args):
raise IRCException("Indicate an URL to validate!")
@ -136,8 +149,12 @@ def cmd_w3c(msg):
@hook("cmd_hook", "watch", data="diff")
@hook("cmd_hook", "updown", data="updown")
@hook("cmd_hook", "watch", data="diff",
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"):
if not len(msg.args):
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)
@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):
wl = watchWebsite.watchedon(msg.channel)
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)
@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):
if not len(msg.args):
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:
return req
else:
if onNone is not None:
if callable(onNone):
return onNone()
else:
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"]))
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):