aboutsummaryrefslogtreecommitdiff
path: root/Manpages/plugin.py
blob: e3913d1618fd68e78d368a9255f067f5ec4d241b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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