New module man that prints man pages on IRC
This commit is contained in:
parent
1b05b965ed
commit
a7fd178a41
46
modules/man.py
Normal file
46
modules/man.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
import re
|
||||||
|
|
||||||
|
nemubotversion = 3.3
|
||||||
|
|
||||||
|
def load(context):
|
||||||
|
from hooks import Hook
|
||||||
|
add_hook("cmd_hook", Hook(cmd_man, "man"))
|
||||||
|
|
||||||
|
def help_tiny ():
|
||||||
|
"""Line inserted in the response to the command !help"""
|
||||||
|
return "Read man on IRC"
|
||||||
|
|
||||||
|
def help_full ():
|
||||||
|
return "!man [0-9] /what/: gives informations about /what/."
|
||||||
|
|
||||||
|
RGXP_s = re.compile(b'\x1b\\[[0-9]+m')
|
||||||
|
|
||||||
|
def cmd_man(msg):
|
||||||
|
args = ["man"]
|
||||||
|
num = None
|
||||||
|
if len(msg.cmds) == 2:
|
||||||
|
args.append(msg.cmds[1])
|
||||||
|
elif len(msg.cmds) >= 3:
|
||||||
|
try:
|
||||||
|
num = int(msg.cmds[1])
|
||||||
|
args.append("%d" % num)
|
||||||
|
args.append(msg.cmds[2])
|
||||||
|
except ValueError:
|
||||||
|
args.append(msg.cmds[1])
|
||||||
|
|
||||||
|
res = Response(msg.sender, channel=msg.channel)
|
||||||
|
with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
|
||||||
|
for line in proc.stdout.read().split(b"\n"):
|
||||||
|
(line, n) = RGXP_s.subn(b'', line)
|
||||||
|
res.append_message(line.decode())
|
||||||
|
|
||||||
|
if len(res.messages) <= 0:
|
||||||
|
if num is not None:
|
||||||
|
res.append_message("Il n'y a pas d'entrée %s dans la section %d du manuel." % (msg.cmds[1], num))
|
||||||
|
else:
|
||||||
|
res.append_message("Il n'y a pas de page de manuel pour %s." % msg.cmds[1])
|
||||||
|
|
||||||
|
return res
|
Loading…
x
Reference in New Issue
Block a user