From 6dc64ce2302dc22a4b67ec634d782a2a05933904 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 27 Mar 2003 06:47:42 +0000 Subject: [PATCH] Broke AdminCommands out of privmsgs. --- src/AdminCommands.py | 157 ++++++++++++++++++++++ src/privmsgs.py | 308 +------------------------------------------ 2 files changed, 158 insertions(+), 307 deletions(-) create mode 100755 src/AdminCommands.py diff --git a/src/AdminCommands.py b/src/AdminCommands.py new file mode 100755 index 000000000..47b2f5da2 --- /dev/null +++ b/src/AdminCommands.py @@ -0,0 +1,157 @@ +#!/usr/bin/env python + +### +# Copyright (c) 2002, Jeremiah Fincher +# 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. +### + +""" +Add the module docstring here. This will be used by the setup.py script. +""" + +import string + +import conf +import ircdb +import ircmsgs +import privmsgs +import callbacks + +class AdminCommands(privmsgs.CapabilityChecker): + capability = 'admin' + def join(self, irc, msg, args): + """ [ ...] + + Tell the bot to join the whitespace-separated list of channels + you give it. + """ + irc.queueMsg(ircmsgs.joins(args)) + for channel in args: + irc.queueMsg(ircmsgs.who(channel)) + + def nick(self, irc, msg, args): + """ + + Changes the bot's nick to .""" + nick = privmsgs.getArgs(args) + irc.queueMsg(ircmsgs.nick(nick)) + + def part(self, irc, msg, args): + """ [ ...] + + Tells the bot to part the whitespace-separated list of channels + you give it. + """ + irc.queueMsg(ircmsgs.parts(args, msg.nick)) + + def disable(self, irc, msg, args): + """ + + Disables the command for all non-owner users. + """ + command = privmsgs.getArgs(args) + if command in ('enable', 'identify'): + irc.error(msg, 'You can\'t disable %s!' % command) + else: + # This has to know that defaultCapabilties gets turned into a + # dictionary. + if command in conf.defaultCapabilities: + conf.defaultCapabilities.remove(command) + capability = ircdb.makeAntiCapability(command) + conf.defaultCapabilities.add(capability) + irc.reply(msg, conf.replySuccess) + + def enable(self, irc, msg, args): + """ + + Re-enables the command for all non-owner users. + """ + command = privmsgs.getArgs(args) + anticapability = ircdb.makeAntiCapability(command) + if anticapability in conf.defaultCapabilities: + conf.defaultCapabilities.remove(anticapability) + irc.reply(msg, conf.replySuccess) + else: + irc.error(msg, 'That command wasn\'t disabled.') + + def addcapability(self, irc, msg, args): + """ + + Gives the user specified by (or the user to whom + currently maps) the specified capability + """ + (name, capability) = privmsgs.getArgs(args, 2) + # This next check to make sure 'admin's can't hand out 'owner'. + if ircdb.checkCapability(msg.prefix, capability) or \ + '!' in capability: + try: + u = ircdb.users.getUser(name) + u.addCapability(capability) + ircdb.users.setUser(name, u) + irc.reply(msg, conf.replySuccess) + except KeyError: + irc.error(msg, conf.replyNoUser) + else: + s = 'You can\'t add capabilities you don\'t have.' + irc.error(msg, s) + + def removecapability(self, irc, msg, args): + """ + + Takes from the user specified by (or the uswer to whom + currently maps) the specified capability + """ + (name, capability) = privmsgs.getArgs(args, 2) + if ircdb.checkCapability(msg.prefix, capability) or \ + '!' in capability: + try: + u = ircdb.users.getUser(name) + u.addCapability(capability) + ircdb.users.setUser(name, u) + irc.reply(msg, conf.replySuccess) + except KeyError: + irc.error(msg, conf.replyNoUser) + else: + s = 'You can\'t remove capabilities you don\'t have.' + irc.error(msg, s) + + def setprefixchar(self, irc, msg, args): + """ + + Sets the prefix chars by which the bot can be addressed. + """ + s = privmsgs.getArgs(args) + if s.translate(string.ascii, string.ascii_letters) == '': + irc.error(msg, 'Prefixes cannot contain letters.') + else: + conf.prefixChars = s + irc.reply(msg, conf.replySuccess) + + +Class = AdminCommands + +# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: diff --git a/src/privmsgs.py b/src/privmsgs.py index b4413990d..a85b346e5 100644 --- a/src/privmsgs.py +++ b/src/privmsgs.py @@ -113,117 +113,6 @@ class CapabilityChecker(callbacks.Privmsg): irc.error(msg, conf.replyNoCapability % self.capability) -class AdminCommands(CapabilityChecker): - capability = 'admin' - def join(self, irc, msg, args): - """ [ ...] - - Tell the bot to join the whitespace-separated list of channels - you give it. - """ - irc.queueMsg(ircmsgs.joins(args)) - for channel in args: - irc.queueMsg(ircmsgs.who(channel)) - - def nick(self, irc, msg, args): - """ - - Changes the bot's nick to .""" - nick = getArgs(args) - irc.queueMsg(ircmsgs.nick(nick)) - - def part(self, irc, msg, args): - """ [ ...] - - Tells the bot to part the whitespace-separated list of channels - you give it. - """ - irc.queueMsg(ircmsgs.parts(args, msg.nick)) - - def disable(self, irc, msg, args): - """ - - Disables the command for all non-owner users. - """ - command = getArgs(args) - if command in ('enable', 'identify'): - irc.error(msg, 'You can\'t disable %s!' % command) - else: - # This has to know that defaultCapabilties gets turned into a - # dictionary. - if command in conf.defaultCapabilities: - conf.defaultCapabilities.remove(command) - capability = ircdb.makeAntiCapability(command) - conf.defaultCapabilities.add(capability) - irc.reply(msg, conf.replySuccess) - - def enable(self, irc, msg, args): - """ - - Re-enables the command for all non-owner users. - """ - command = getArgs(args) - anticapability = ircdb.makeAntiCapability(command) - if anticapability in conf.defaultCapabilities: - conf.defaultCapabilities.remove(anticapability) - irc.reply(msg, conf.replySuccess) - else: - irc.error(msg, 'That command wasn\'t disabled.') - - def addcapability(self, irc, msg, args): - """ - - Gives the user specified by (or the user to whom - currently maps) the specified capability - """ - (name, capability) = getArgs(args, 2) - # This next check to make sure 'admin's can't hand out 'owner'. - if ircdb.checkCapability(msg.prefix, capability) or \ - '!' in capability: - try: - u = ircdb.users.getUser(name) - u.addCapability(capability) - ircdb.users.setUser(name, u) - irc.reply(msg, conf.replySuccess) - except KeyError: - irc.error(msg, conf.replyNoUser) - else: - s = 'You can\'t add capabilities you don\'t have.' - irc.error(msg, s) - - def removecapability(self, irc, msg, args): - """ - - Takes from the user specified by (or the uswer to whom - currently maps) the specified capability - """ - (name, capability) = getArgs(args, 2) - if ircdb.checkCapability(msg.prefix, capability) or \ - '!' in capability: - try: - u = ircdb.users.getUser(name) - u.addCapability(capability) - ircdb.users.setUser(name, u) - irc.reply(msg, conf.replySuccess) - except KeyError: - irc.error(msg, conf.replyNoUser) - else: - s = 'You can\'t remove capabilities you don\'t have.' - irc.error(msg, s) - - def setprefixchar(self, irc, msg, args): - """ - - Sets the prefix chars by which the bot can be addressed. - """ - s = getArgs(args) - if s.translate(string.ascii, string.ascii_letters) == '': - irc.error(msg, 'Prefixes cannot contain letters.') - else: - conf.prefixChars = s - irc.reply(msg, conf.replySuccess) - - class OwnerCommands(CapabilityChecker): capability = 'owner' def __init__(self): @@ -422,202 +311,7 @@ class OwnerCommands(CapabilityChecker): irc.error(msg, 'There was no callback %s' % name) -class UserCommands(callbacks.Privmsg): - def _checkNotChannel(self, irc, msg, password=' '): - if password and ircutils.isChannel(msg.args[0]): - irc.error(msg, conf.replyRequiresPrivacy) - def register(self, irc, msg, args): - """ - - Registers with the given password and the current - hostmask of the person registering. - """ - (name, password) = getArgs(args, optional=1) - self._checkNotChannel(irc, msg, password) - if ircutils.isChannel(msg.args[0]): - irc.error(msg, conf.replyRequiresPrivacy) - if ircdb.users.hasUser(name): - irc.error(msg, 'That name is already registered.') - if ircutils.isUserHostmask(name): - irc.error(msg, 'Hostmasks aren\'t valid usernames.') - user = ircdb.IrcUser() - user.setPassword(password) - user.addHostmask(msg.prefix) - ircdb.users.setUser(name, user) - irc.reply(msg, conf.replySuccess) - - def addhostmask(self, irc, msg, args): - """ [] - - Adds the hostmask to the user specified by . The - may only be required if the user is not recognized by his - hostmask. - """ - (name, hostmask, password) = getArgs(args, 2, 1) - self._checkNotChannel(irc, msg, password) - s = hostmask.translate(string.ascii, '!@*?') - if len(s) < 10: - s = 'Hostmask must be more than 10 non-wildcard characters.' - irc.error(msg, s) - try: - user = ircdb.users.getUser(name) - except KeyError: - irc.error(msg, conf.replyNoUser) - try: - name = ircdb.users.getUserName(hostmask) - s = 'That hostmask is already registered to %s.' % name - irc.error(msg, s) - except KeyError: - pass - if user.checkHostmask(msg.prefix) or user.checkPassword(password): - user.addHostmask(hostmask) - ircdb.users.setUser(name, user) - irc.reply(msg, conf.replySuccess) - else: - irc.error(msg, conf.replyIncorrectAuth) - - def delhostmask(self, irc, msg, args): - """ [] - - Deletes the hostmask from the record of the user specified - by . The may only be required if the user is not - recognized by his hostmask. - """ - (name, hostmask, password) = getArgs(args, 2, 1) - self._checkNotChannel(irc, msg, password) - try: - user = ircdb.users.getUser(name) - except KeyError: - irc.error(msg, conf.replyNoUser) - if user.checkHostmask(msg.prefix) or user.checkPassword(password): - user.removeHostmask(hostmask) - ircdb.users.setUser(name, user) - irc.reply(msg, conf.replySuccess) - else: - irc.error(msg, conf.replyIncorrectAuth) - - def setpassword(self, irc, msg, args): - """ - - Sets the new password for the user specified by to - . - """ - (name, oldpassword, newpassword) = getArgs(args, 3) - self._checkNotChannel(irc, msg, oldpassword+newpassword) - try: - user = ircdb.users.getUser(name) - except KeyError: - irc.error(msg, conf.replyNoUser) - if user.checkPassword(oldpassword): - user.setPassword(newpassword) - ircdb.users.setUser(name, user) - irc.reply(msg, conf.replySuccess) - else: - irc.error(msg, conf.replyIncorrectAuth) - - def username(self, irc, msg, args): - """ - - Returns the username of the user specified by or if - the user is registered. - """ - hostmask = getArgs(args) - if not ircutils.isUserHostmask(hostmask): - try: - hostmask = irc.state.nickToHostmask(hostmask) - except KeyError: - irc.error(msg, conf.replyNoUser) - try: - name = ircdb.users.getUserName(hostmask) - irc.reply(msg, name) - except KeyError: - irc.error(msg, conf.replyNoUser) - - def hostmasks(self, irc, msg, args): - """[] - - Returns the hostmasks of the user specified by ; if isn't - specified, returns the hostmasks of the user calling the command. - """ - if not args: - name = msg.prefix - else: - name = getArgs(args) - try: - user = ircdb.users.getUser(name) - irc.reply(msg, repr(user.hostmasks)) - except KeyError: - irc.error(msg, conf.replyNoUser) - - def capabilities(self, irc, msg, args): - """[] - - Returns the capabilities of the user specified by ; if - isn't specified, returns the hostmasks of the user calling the command. - """ - if not args: - try: - name = ircdb.users.getUserName(msg.prefix) - except KeyError: - irc.error(msg, conf.replyNoUser) - return - else: - name = getArgs(args) - try: - user = ircdb.users.getUser(name) - irc.reply(msg, '[%s]' % ', '.join(user.capabilities)) - except KeyError: - irc.error(msg, conf.replyNoUser) - - def identify(self, irc, msg, args): - """ - - Identifies the user as . - """ - (name, password) = getArgs(args, 2) - self._checkNotChannel(irc, msg) - try: - u = ircdb.users.getUser(name) - except KeyError: - irc.error(msg, conf.replyNoUser) - return - if u.checkPassword(password): - u.setAuth(msg.prefix) - ircdb.users.setUser(name, u) - irc.reply(msg, conf.replySuccess) - else: - irc.error(msg, conf.replyIncorrectAuth) - - def unidentify(self, irc, msg, args): - """takes no arguments - - Un-identifies the user. - """ - try: - u = ircdb.users.getUser(msg.prefix) - name = ircdb.users.getUserName(msg.prefix) - except KeyError: - irc.error(msg, conf.replyNoUser) - return - u.unsetAuth() - ircdb.users.setUser(name, u) - irc.reply(msg, conf.replySuccess) - - def whoami(self, irc, msg, args): - """takes no arguments - - Returns the name of the user calling the command. - """ - try: - name = ircdb.users.getUserName(msg.prefix) - irc.reply(msg, name) - except KeyError: - irc.error(msg, 'I can\'t find you in my database') - - -standardPrivmsgModules = (OwnerCommands, - AdminCommands, - UserCommands,) +standardPrivmsgModules = [OwnerCommands] # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: