File: plugin.py

package info (click to toggle)
limnoria 2026.1.16-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,584 kB
  • sloc: python: 50,436; makefile: 49; sh: 14
file content (367 lines) | stat: -rw-r--r-- 15,152 bytes parent folder | download | duplicates (4)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
###
# Copyright (c) 2002-2005, Jeremiah Fincher
# Copyright (c) 2010-2021, Valentin Lorentz
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions, and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions, and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the author of this software nor the name of
#     contributors to this software may be used to endorse or promote products
#     derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###

import sys
import time

import supybot.conf as conf
import supybot.ircdb as ircdb
import supybot.utils as utils
from supybot.commands import *
import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils
import supybot.schedule as schedule
import supybot.callbacks as callbacks
from supybot.i18n import PluginInternationalization, internationalizeDocstring
_ = PluginInternationalization('Admin')

class Admin(callbacks.Plugin):
    """This plugin provides access to administrative commands, such as
    adding capabilities, managing ignore lists, and joining channels.
    This is a core Supybot plugin that should not be removed!"""
    def __init__(self, irc):
        self.__parent = super(Admin, self)
        self.__parent.__init__(irc)
        self.joins = {}
        self.pendingNickChanges = {}

    @internationalizeDocstring
    def do437(self, irc, msg):
        """Nick/channel temporarily unavailable."""
        target = msg.args[0]
        t = time.time() + 30
        if irc.isChannel(target):
            # Let's schedule a rejoin.
            networkGroup = conf.supybot.networks.get(irc.network)
            def rejoin():
                irc.queueMsg(networkGroup.channels.join(target))
                # We don't need to schedule something because we'll get another
                # 437 when we try to join later.
            schedule.addEvent(rejoin, t)
            self.log.info('Scheduling a rejoin to %s at %s; '
                          'Channel temporarily unavailable.', target, t)
        else:
            irc = self.pendingNickChanges.get(irc, None)
            if irc is not None:
                def nick():
                    irc.queueMsg(ircmsgs.nick(target))
                schedule.addEvent(nick, t)
                self.log.info('Scheduling a nick change to %s at %s; '
                              'Nick temporarily unavailable.', target, t)
            else:
                self.log.debug('Got 437 without Admin.nick being called.')

    def do471(self, irc, msg):
        try:
            channel = msg.args[1]
            (irc, msg) = self.joins.pop(channel)
            irc.error(_('Cannot join %s, it\'s full.') % channel)
        except KeyError:
            self.log.debug('Got 471 without Admin.join being called.')

    def do473(self, irc, msg):
        try:
            channel = msg.args[1]
            (irc, msg) = self.joins.pop(channel)
            irc.error(_('Cannot join %s, I was not invited.') % channel)
        except KeyError:
            self.log.debug('Got 473 without Admin.join being called.')

    def do474(self, irc, msg):
        try:
            channel = msg.args[1]
            (irc, msg) = self.joins.pop(channel)
            irc.error(_('Cannot join %s, I am banned.') % channel)
        except KeyError:
            self.log.debug('Got 474 without Admin.join being called.')

    def do475(self, irc, msg):
        try:
            channel = msg.args[1]
            (irc, msg) = self.joins.pop(channel)
            irc.error(_('Cannot join %s, my keyword was wrong.') % channel)
        except KeyError:
            self.log.debug('Got 475 without Admin.join being called.')

    def do477(self, irc, msg):
        try:
            channel = msg.args[1]
            (irc,msg) = self.joins.pop(channel)
            irc.error(_('Cannot join %s, I\'m not identified with '
                      'NickServ.') % channel)
        except KeyError:
            self.log.debug('Got 477 without Admin.join being called.')

    def do515(self, irc, msg):
        try:
            channel = msg.args[1]
            (irc, msg) = self.joins.pop(channel)
            irc.error(_('Cannot join %s, I\'m not identified with '
                      'NickServ.') % channel)
        except KeyError:
            self.log.debug('Got 515 without Admin.join being called.')

    def doJoin(self, irc, msg):
        if msg.prefix == irc.prefix:
            try:
                del self.joins[msg.args[0]]
            except KeyError:
                s = 'Joined a channel without Admin.join being called.'
                self.log.debug(s)

    def doInvite(self, irc, msg):
        channel = msg.args[1]
        if channel not in irc.state.channels:
            if conf.supybot.alwaysJoinOnInvite.get(channel)() or \
               ircdb.checkCapability(msg.prefix, 'admin'):
                self.log.info('Invited to %s by %s.', channel, msg.prefix)
                networkGroup = conf.supybot.networks.get(irc.network)
                irc.queueMsg(networkGroup.channels.join(channel))
                conf.supybot.networks.get(irc.network).channels().add(channel)
            else:
                self.log.warning('Invited to %s by %s, but '
                                 'supybot.alwaysJoinOnInvite was False and '
                                 'the user lacked the "admin" capability.',
                                 channel, msg.prefix)

    @internationalizeDocstring
    def join(self, irc, msg, args, channel, key):
        """<channel> [<key>]

        Tell the bot to join the given channel.  If <key> is given, it is used
        when attempting to join the channel.
        """
        if not irc.isChannel(channel):
            irc.errorInvalid(_('channel'), channel, Raise=True)
        networkGroup = conf.supybot.networks.get(irc.network)
        networkGroup.channels().add(channel)
        if key:
            networkGroup.channels.key.get(channel).setValue(key)
        maxchannels = irc.state.supported.get('maxchannels', sys.maxsize)
        if len(irc.state.channels) + 1 > maxchannels:
            irc.error(_('I\'m already too close to maximum number of '
                      'channels for this network.'), Raise=True)
        irc.queueMsg(networkGroup.channels.join(channel))
        irc.noReply()
        self.joins[channel] = (irc, msg)
    join = wrap(join, ['validChannel', additional('something')])

    @internationalizeDocstring
    def channels(self, irc, msg, args):
        """takes no arguments

        Returns the channels the bot is on.
        """
        L = irc.state.channels.keys()
        if L:
            utils.sortBy(ircutils.toLower, L)
            irc.reply(format('%L', L), private=True)
        else:
            irc.reply(_('I\'m not currently in any channels.'))
    channels = wrap(channels)

    def do484(self, irc, msg):
        irc = self.pendingNickChanges.get(irc, None)
        if irc is not None:
            irc.error(_('My connection is restricted, I can\'t change nicks.'))
        else:
            self.log.debug('Got 484 without Admin.nick being called.')

    def do433(self, irc, msg):
        irc = self.pendingNickChanges.get(irc, None)
        if irc is not None:
            irc.error(_('Someone else is already using that nick.'))
        else:
            self.log.debug('Got 433 without Admin.nick being called.')

    def do435(self, irc, msg):
        irc = self.pendingNickChanges.get(irc, None)
        if irc is not None:
            irc.error(_('I can\'t change nick, I\'m currently banned in %s.') %
                      msg.args[2])
        else:
            self.log.debug('Got 435 without Admin.nick being called.')

    def do438(self, irc, msg):
        irc = self.pendingNickChanges.get(irc, None)
        if irc is not None:
            irc.error(format(_('I can\'t change nicks, the server said %q.'),
                      msg.args[2]), private=True)
        else:
            self.log.debug('Got 438 without Admin.nick being called.')

    def doNick(self, irc, msg):
        if msg.nick == irc.nick or msg.args[0] == irc.nick:
            try:
                del self.pendingNickChanges[irc]
            except KeyError:
                self.log.debug('Got NICK without Admin.nick being called.')

    @internationalizeDocstring
    def nick(self, irc, msg, args, nick, network):
        """[<nick>] [<network>]

        Changes the bot's nick to <nick>.  If no nick is given, returns the
        bot's current nick.
        """
        network = network or irc.network
        if nick:
            group = getattr(conf.supybot.networks, network)
            group.nick.setValue(nick)
            irc.queueMsg(ircmsgs.nick(nick))
            self.pendingNickChanges[irc.getRealIrc()] = irc
        else:
            irc.reply(irc.nick)
    nick = wrap(nick, [additional('nick'), additional('something')])

    class capability(callbacks.Commands):

        @internationalizeDocstring
        def add(self, irc, msg, args, user, capability):
            """<name|hostmask> <capability>

            Gives the user specified by <name> (or the user to whom <hostmask>
            currently maps) the specified capability <capability>
            """
            # Ok, the concepts that are important with capabilities:
            #
            ### 1) No user should be able to elevate their privilege to owner.
            ### 2) Admin users are *not* superior to #channel.ops, and don't
            ###    have God-like powers over channels.
            ### 3) We assume that Admin users are two things: non-malicious and
            ###    and greedy for power.  So they'll try to elevate their
            ###    privilege to owner, but they won't try to crash the bot for
            ###    no reason.

            # Thus, the owner capability can't be given in the bot.  Admin
            # users can only give out capabilities they have themselves (which
            # will depend on supybot.capabilities and its child default) but
            # generally means they can't mess with channel capabilities.
            if ircutils.strEqual(capability, 'owner'):
                irc.error(_('The "owner" capability can\'t be added in the '
                          'bot.  Use the supybot-adduser program (or edit the '
                          'users.conf file yourself) to add an owner '
                          'capability.'))
                return
            if ircdb.isAntiCapability(capability) or \
               ircdb.checkCapability(msg.prefix, capability):
                user.addCapability(capability)
                ircdb.users.setUser(user)
                irc.replySuccess()
            else:
                irc.error(_('You can\'t add capabilities you don\'t have.'))
        add = wrap(add, ['otherUser', 'lowered'])

        @internationalizeDocstring
        def remove(self, irc, msg, args, user, capability):
            """<name|hostmask> <capability>

            Takes from the user specified by <name> (or the user to whom
            <hostmask> currently maps) the specified capability <capability>
            """
            if ircdb.checkCapability(msg.prefix, capability) or \
               ircdb.isAntiCapability(capability):
                try:
                    user.removeCapability(capability)
                    ircdb.users.setUser(user)
                    irc.replySuccess()
                except KeyError:
                    irc.error(_('That user doesn\'t have that capability.'))
            else:
                s = _('You can\'t remove capabilities you don\'t have.')
                irc.error(s)
        remove = wrap(remove, ['otherUser','lowered'])

    class ignore(callbacks.Commands):

        @internationalizeDocstring
        def add(self, irc, msg, args, hostmask, expires):
            """<hostmask|nick> [<expires>]

            This will set a persistent ignore on <hostmask> or the hostmask
            currently associated with <nick>. <expires> is an optional argument
            specifying when (in "seconds from now") the ignore will expire; if
            it isn't given, the ignore will never automatically expire.
            """
            ircdb.ignores.add(hostmask, expires)
            irc.replySuccess()
        add = wrap(add, ['hostmask', additional('expiry', 0)])

        @internationalizeDocstring
        def remove(self, irc, msg, args, hostmask):
            """<hostmask|nick>

            This will remove the persistent ignore on <hostmask> or the
            hostmask currently associated with <nick>.
            """
            try:
                ircdb.ignores.remove(hostmask)
                irc.replySuccess()
            except KeyError:
                irc.error(_('%s wasn\'t in the ignores database.') % hostmask)
        remove = wrap(remove, ['hostmask'])

        @internationalizeDocstring
        def list(self, irc, msg, args):
            """takes no arguments

            Lists the hostmasks that the bot is ignoring.
            """
            # XXX Add the expirations.
            if ircdb.ignores.hostmasks:
                irc.reply(format('%L', (list(map(repr,ircdb.ignores.hostmasks)))))
            else:
                irc.reply(_('I\'m not currently globally ignoring anyone.'))
        list = wrap(list)

    def clearq(self, irc, msg, args):
        """takes no arguments

        Clears the current send queue for this network.
        """
        irc.queue.reset()
        irc.replySuccess()
    clearq = wrap(clearq)

    def acmd(self, irc, msg, args, commandAndArgs):
        """<command> [<arg> ...]

        Perform <command> (with associated <arg>s on all channels on current network."""
        for channel in irc.state.channels:
            msg = ircmsgs.IrcMsg(msg=msg, args=(channel,) + msg.args[1:])
            self.Proxy(irc.getRealIrc(), msg, commandAndArgs)
    acmd = wrap(acmd, ['admin', many('something')])




Class = Admin

# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: