diff options
Diffstat (limited to 'Manpages/plugin.py')
| -rw-r--r-- | Manpages/plugin.py | 99 | 
1 files changed, 99 insertions, 0 deletions
| diff --git a/Manpages/plugin.py b/Manpages/plugin.py new file mode 100644 index 0000000..e3913d1 --- /dev/null +++ b/Manpages/plugin.py @@ -0,0 +1,99 @@ +### +# Copyright (c) 2021, B. Watson +# All rights reserved. +# +# +### + +import os +import subprocess +import sys +import re +import sqlite3 + +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 + + +class Manpages(callbacks.Plugin): +    """Provides Unix manpage lookups""" +    threaded = True + +    db = None + +    def InitDB(self): +        if self.db is None: +            filename = self.registryValue('dbpath') +            if(os.path.exists(filename)): +                self.db = sqlite3.connect(filename) +        return self.db + +    # 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): +        """ [<section>|-k] <page> + +        Look up man pages and show their summaries. Without -k, searches +        man pages by name (same as the OS "whatis" or "man -f" commands). +        With -k, also searches descriptions (same as OS "man -k" or "apropos"). +        With <section>, limits search to a single man section (otherwise all +        sections are searched). +        """ + +        db = self.InitDB(); +        if db is None: +            irc.error("manpage database doesn't exist", Raise=True) + +        maxresults = self.registryValue('maxresults') +        if msg.channel is None: +            # private message, increase limit +            maxresults *= 5 + +        cursor = db.cursor() + +        errmsg = "No manual entry for " + +        selectfrom = "select page, section, desc from whatis " +        orderby = " order by section " +        limit = " limit " + str(maxresults + 1) + " " + +        if page is None: +            cursor.execute(selectfrom + "where page glob ?" + orderby + limit, (sect_or_page,)) +            errmsg = errmsg + sect_or_page +        else: +            if sect_or_page == "-k": +                glob = "*" + page + "*" +                cursor.execute(selectfrom + "where page glob ? or desc glob ? " + orderby + limit, (glob, glob)) +                errmsg = "No man pages matching " + page +            else: +                cursor.execute(selectfrom + "where section=? and page glob ? " + orderby + limit, (sect_or_page, page)) +                errmsg = errmsg + page + " in section " + sect_or_page + +        result = cursor.fetchall() + +        lines = [] + +        if len(result) == 0: +            irc.reply(errmsg) +        else: +            for (page, section, desc) in result: +                lines.append(ircutils.bold(page + "(" + section + ")") + ": " + desc) +            if(len(result) > maxresults): +                lines.append("[too many results, only showing first " + str(maxresults) + "]") +            irc.replies(lines, joiner=' | ') + +    man = thread(wrap(man, ['somethingWithoutSpaces', optional('somethingWithoutSpaces')])) + +Class = Manpages | 
