Separate curl and w3m functions to use it from others modules

This commit is contained in:
nemunaire 2014-09-20 00:14:19 +02:00
parent b184b27d4f
commit 7a5c2d9786

View File

@ -27,31 +27,27 @@ def load(context):
def help_full(): def help_full():
return "!traceurl /url/: Follow redirections from /url/." return "!traceurl /url/: Follow redirections from /url/."
def w3m(url):
args = ["w3m", "-T", "text/html", "-dump"]
args.append(url)
with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
return proc.stdout.read().decode()
@hook("cmd_hook", "w3m") @hook("cmd_hook", "w3m")
def cmd_w3m(msg): def cmd_w3m(msg):
if len(msg.cmds) > 1: if len(msg.cmds) > 1:
args = ["w3m", "-T", "text/html", "-dump"]
args.append(msg.cmds[1])
res = Response(channel=msg.channel) res = Response(channel=msg.channel)
with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: for line in w3m(" ".join(msg.cmds[1:])).split("\n"):
for line in proc.stdout.read().split(b"\n"): res.append_message(line)
res.append_message(line.decode())
return res return res
else: else:
raise IRCException("Indicate the URL to visit.") raise IRCException("Indicate the URL to visit.")
@hook("cmd_hook", "curl") def curl(url):
def cmd_curl(msg):
if len(msg.cmds) < 2:
raise IRCException("Indicate the URL to visit.")
try: try:
req = web.getURLContent(" ".join(msg.cmds[1:])) req = web.getURLContent(url)
if req is not None: if req is not None:
res = Response(channel=msg.channel) return req
for m in req.split("\n"):
res.append_message(m)
return res
else: else:
raise IRCException("An error occurs when trying to access the page") raise IRCException("An error occurs when trying to access the page")
except socket.timeout: except socket.timeout:
@ -59,6 +55,16 @@ def cmd_curl(msg):
except socket.error as e: except socket.error as e:
raise IRCException(e.strerror) raise IRCException(e.strerror)
@hook("cmd_hook", "curl")
def cmd_curl(msg):
if len(msg.cmds) < 2:
raise IRCException("Indicate the URL to visit.")
res = Response(channel=msg.channel)
for m in curl(" ".join(msg.cmds[1:])).split("\n"):
res.append_message(m)
return res
@hook("cmd_hook", "curly") @hook("cmd_hook", "curly")
def cmd_curly(msg): def cmd_curly(msg):
if len(msg.cmds) < 2: if len(msg.cmds) < 2: