diff options
Diffstat (limited to 'Manpages/plugin.py.old')
-rw-r--r-- | Manpages/plugin.py.old | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/Manpages/plugin.py.old b/Manpages/plugin.py.old new file mode 100644 index 0000000..b60b4dd --- /dev/null +++ b/Manpages/plugin.py.old @@ -0,0 +1,98 @@ +### +# Copyright (c) 2021, B. Watson +# All rights reserved. +# +# +### + +import os +import subprocess +import sys +import re + +from supybot import utils, plugins, ircutils, callbacks +from supybot.commands import * +import supybot.utils.minisix as minisix + +try: + from supybot.i18n import PluginInternationalization + _ = PluginInternationalization('Manpages') +except ImportError: + # Placeholder that allows to run the plugin on a bot + # without the i18n module + _ = lambda x: x + + +# The regex stuff is to turn this: +# ls (1) - list directory contents +# into this: +# ls(1) - list directory contents + +class Manpages(callbacks.Plugin): + """Provides Unix manpage and whereis lookups""" + threaded = True + + re_sub_spaces = re.compile(r"\s\s+") + re_sub_paren = re.compile(r"\s\(") + + def Command(self, irc, msg, args): + try: + with open(os.devnull) as null: + child = subprocess.Popen(args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=null) + except OSError as e: + irc.error('Man page lookup failed.', Raise=True) + (out, err) = child.communicate() + child.wait() + out = out + err + if minisix.PY3: + lines = [i.decode('utf-8').rstrip() for i in out.splitlines()] + lines = list(map(str, lines)) + else: + lines = out.splitlines() + lines = list(map(str.rstrip, lines)) + lines = filter(None, lines) + return lines + + # man() is a *very* limited subset of what the real + # man command does. Should maybe be called whatis instead, + # use the Aka plugin to create an alias: + # load Aka + # aka add whatis man $* + def man(self, irc, msg, args, sect_or_page, page): + """ Usage: man <section> <page>, or man <page> for all sections """ + + cmd = [ '/usr/bin/man', '-f' ] + + try: + sect = int(sect_or_page) + if page is None: + irc.error("Missing <page>", Raise=True) + else: + cmd.append("-s") + cmd.append(sect_or_page) + cmd.append(page) + except ValueError: + if page is None: + cmd.append(sect_or_page) + else: + irc.error("Invalid <section>", Raise=True) + + lines = self.Command(irc, msg, cmd) + + lines = [self.re_sub_spaces.sub(' ', i) for i in lines] + lines = [self.re_sub_paren.sub('(', i) for i in lines] + irc.replies(lines, joiner=' | ') + + man = thread(wrap(man, ['somethingWithoutSpaces', optional('somethingWithoutSpaces')])) + + # whereis() is a limited subset of the real whereis. + def whereis(self, irc, msg, args, name): + """ <name> """ + lines = self.Command(irc, msg, [ '/usr/bin/whereis', name ]) + irc.replies(lines, joiner=' | ') + whereis = thread(wrap(whereis, ['somethingWithoutSpaces'])) + +Class = Manpages |