aboutsummaryrefslogtreecommitdiff
path: root/Manpages/plugin.py.old
blob: b60b4ddeb08c3e13d5e4d1d263b3a1c8e5003c94 (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
###
# 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