2013-01-02 14:49:26 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
"Read manual pages on IRC"
|
|
|
|
|
2013-01-02 14:49:26 +00:00
|
|
|
import subprocess
|
|
|
|
import re
|
2013-01-15 10:15:25 +00:00
|
|
|
import os
|
2013-01-02 14:49:26 +00:00
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
from hooks import hook
|
2013-01-02 14:49:26 +00:00
|
|
|
|
2014-08-13 13:53:55 +00:00
|
|
|
nemubotversion = 3.4
|
2013-01-02 14:49:26 +00:00
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2013-01-02 14:49:26 +00:00
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
def help_full():
|
2013-01-02 14:49:26 +00:00
|
|
|
return "!man [0-9] /what/: gives informations about /what/."
|
|
|
|
|
|
|
|
RGXP_s = re.compile(b'\x1b\\[[0-9]+m')
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "MAN")
|
2013-01-02 14:49:26 +00:00
|
|
|
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])
|
|
|
|
|
2013-01-15 10:15:25 +00:00
|
|
|
os.unsetenv("LANG")
|
2014-09-18 05:57:06 +00:00
|
|
|
res = Response(channel=msg.channel)
|
2014-11-13 01:51:49 +00:00
|
|
|
with subprocess.Popen(args,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE) as proc:
|
2013-01-02 14:49:26 +00:00
|
|
|
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:
|
2014-11-13 01:51:49 +00:00
|
|
|
res.append_message("There is no entry %s in section %d." %
|
|
|
|
(msg.cmds[1], num))
|
2013-01-02 14:49:26 +00:00
|
|
|
else:
|
2014-11-13 01:51:49 +00:00
|
|
|
res.append_message("There is no man page for %s." % msg.cmds[1])
|
2013-01-02 14:49:26 +00:00
|
|
|
|
|
|
|
return res
|
2013-01-15 10:15:25 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "man")
|
2013-01-15 10:15:25 +00:00
|
|
|
def cmd_whatis(msg):
|
|
|
|
args = ["whatis", " ".join(msg.cmds[1:])]
|
|
|
|
|
2014-09-18 05:57:06 +00:00
|
|
|
res = Response(channel=msg.channel)
|
2014-11-13 01:51:49 +00:00
|
|
|
with subprocess.Popen(args,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE) as proc:
|
2013-01-15 10:15:25 +00:00
|
|
|
for line in proc.stdout.read().split(b"\n"):
|
|
|
|
(line, n) = RGXP_s.subn(b'', line)
|
|
|
|
res.append_message(" ".join(line.decode().split()))
|
|
|
|
|
|
|
|
if len(res.messages) <= 0:
|
|
|
|
if num is not None:
|
2014-11-13 01:51:49 +00:00
|
|
|
res.append_message("There is no entry %s in section %d." %
|
|
|
|
(msg.cmds[1], num))
|
2013-01-15 10:15:25 +00:00
|
|
|
else:
|
2014-11-13 01:51:49 +00:00
|
|
|
res.append_message("There is no man page for %s." % msg.cmds[1])
|
2013-01-15 10:15:25 +00:00
|
|
|
|
|
|
|
return res
|