Limnoria/src/UserCommands.py

285 lines
9.6 KiB
Python
Raw Normal View History

2003-03-27 08:57:27 +01:00
#!/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.
###
"""
2003-09-03 11:50:04 +02:00
Provides commands useful to users in general. This plugin is loaded by default.
2003-03-27 08:57:27 +01:00
"""
import string
import conf
import utils
2003-03-27 08:57:27 +01:00
import ircdb
import ircutils
import privmsgs
import callbacks
class UserCommands(callbacks.Privmsg):
def _checkNotChannel(self, irc, msg, password=' '):
if password and ircutils.isChannel(msg.args[0]):
irc.error(msg, conf.replyRequiresPrivacy)
return False
else:
return True
2003-03-27 08:57:27 +01:00
def register(self, irc, msg, args):
"""<name> <password>
Registers <name> with the given password <password> and the current
hostmask of the person registering.
"""
2003-09-13 15:44:13 +02:00
(name, password) = privmsgs.getArgs(args, needed=2)
if not self._checkNotChannel(irc, msg, password):
return
try:
ircdb.users.getUserId(name)
irc.error(msg, 'That name is alrady assigned to someone.')
2003-04-20 10:54:27 +02:00
return
except KeyError:
pass
2003-03-27 08:57:27 +01:00
if ircutils.isUserHostmask(name):
irc.error(msg, 'Hostmasks aren\'t valid usernames.')
2003-04-20 10:54:27 +02:00
return
(id, user) = ircdb.users.newUser()
user.name = name
2003-03-27 08:57:27 +01:00
user.setPassword(password)
user.addHostmask(msg.prefix)
ircdb.users.setUser(id, user)
2003-03-27 08:57:27 +01:00
irc.reply(msg, conf.replySuccess)
2003-09-13 15:44:13 +02:00
def unregister(self, irc, msg, args):
"""<name> <password>
Unregisters <name> from the user database.
"""
(name, password) = privmsgs.getArgs(args, needed=2)
if not self._checkNotChannel(irc, msg, password):
return
try:
id = ircdb.users.getUserId(name)
user = ircdb.users.getUser(id)
except KeyError:
irc.error(msg, 'That username isn\'t registered.')
return
if user.checkPassword(password):
ircdb.users.delUser(id)
irc.reply(msg, conf.replySuccess)
else:
irc.error(msg, conf.replyIncorrectAuth)
2003-03-27 08:57:27 +01:00
def addhostmask(self, irc, msg, args):
"""<name> <hostmask> [<password>]
Adds the hostmask <hostmask> to the user specified by <name>. The
<password> may only be required if the user is not recognized by his
hostmask.
"""
(name, hostmask, password) = privmsgs.getArgs(args, 2, 1)
if not self._checkNotChannel(irc, msg, password):
return
if not ircutils.isUserHostmask(hostmask):
irc.error(msg, 'That\'s not a valid hostmask.')
return
2003-03-27 08:57:27 +01:00
s = hostmask.translate(string.ascii, '!@*?')
if len(s) < 10:
s = 'Hostmask must contain more than 10 non-wildcard characters.'
2003-03-27 08:57:27 +01:00
irc.error(msg, s)
2003-04-20 10:54:27 +02:00
return
2003-03-27 08:57:27 +01:00
try:
id = ircdb.users.getUserId(name)
user = ircdb.users.getUser(id)
2003-03-27 08:57:27 +01:00
except KeyError:
irc.error(msg, conf.replyNoUser)
2003-04-20 10:54:27 +02:00
return
2003-03-27 08:57:27 +01:00
try:
_ = ircdb.users.getUserId(hostmask)
irc.error(msg, 'That hostmask is already registered.')
2003-04-20 10:54:27 +02:00
return
2003-03-27 08:57:27 +01:00
except KeyError:
pass
if user.checkHostmask(msg.prefix) or user.checkPassword(password):
user.addHostmask(hostmask)
ircdb.users.setUser(id, user)
2003-03-27 08:57:27 +01:00
irc.reply(msg, conf.replySuccess)
else:
irc.error(msg, conf.replyIncorrectAuth)
2003-04-20 10:54:27 +02:00
return
2003-03-27 08:57:27 +01:00
def delhostmask(self, irc, msg, args):
"""<name> <hostmask> [<password>]
Deletes the hostmask <hostmask> from the record of the user specified
by <name>. The <password> may only be required if the user is not
recognized by his hostmask.
"""
(name, hostmask, password) = privmsgs.getArgs(args, 2, 1)
if not self._checkNotChannel(irc, msg, password):
return
2003-03-27 08:57:27 +01:00
try:
id = ircdb.users.getUserId(name)
user = ircdb.users.getUser(id)
2003-03-27 08:57:27 +01:00
except KeyError:
irc.error(msg, conf.replyNoUser)
2003-04-20 10:54:27 +02:00
return
2003-03-27 08:57:27 +01:00
if user.checkHostmask(msg.prefix) or user.checkPassword(password):
user.removeHostmask(hostmask)
ircdb.users.setUser(id, user)
2003-03-27 08:57:27 +01:00
irc.reply(msg, conf.replySuccess)
else:
irc.error(msg, conf.replyIncorrectAuth)
2003-04-20 10:54:27 +02:00
return
2003-03-27 08:57:27 +01:00
def setpassword(self, irc, msg, args):
"""<name> <old password> <new password>
Sets the new password for the user specified by <name> to
<new password>.
"""
(name, oldpassword, newpassword) = privmsgs.getArgs(args, 3)
if not self._checkNotChannel(irc, msg, oldpassword+newpassword):
return
2003-03-27 08:57:27 +01:00
try:
id = ircdb.users.getUserId(name)
user = ircdb.users.getUser(id)
2003-03-27 08:57:27 +01:00
except KeyError:
irc.error(msg, conf.replyNoUser)
2003-04-20 10:54:27 +02:00
return
2003-03-27 08:57:27 +01:00
if user.checkPassword(oldpassword):
user.setPassword(newpassword)
ircdb.users.setUser(id, user)
2003-03-27 08:57:27 +01:00
irc.reply(msg, conf.replySuccess)
else:
irc.error(msg, conf.replyIncorrectAuth)
def username(self, irc, msg, args):
"""<hostmask|nick>
Returns the username of the user specified by <hostmask> or <nick> if
the user is registered.
"""
hostmask = privmsgs.getArgs(args)
if not ircutils.isUserHostmask(hostmask):
try:
hostmask = irc.state.nickToHostmask(hostmask)
except KeyError:
irc.error(msg, conf.replyNoUser)
2003-04-20 10:54:27 +02:00
return
2003-03-27 08:57:27 +01:00
try:
user = ircdb.users.getUser(hostmask)
irc.reply(msg, user.name)
2003-03-27 08:57:27 +01:00
except KeyError:
irc.error(msg, conf.replyNoUser)
def hostmasks(self, irc, msg, args):
"""[<name>]
Returns the hostmasks of the user specified by <name>; if <name> isn't
specified, returns the hostmasks of the user calling the command.
"""
if not args:
name = msg.prefix
else:
name = privmsgs.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):
"""[<name>]
Returns the capabilities of the user specified by <name>; if <name>
isn't specified, returns the hostmasks of the user calling the command.
"""
if not args:
name = msg.prefix
2003-03-27 08:57:27 +01:00
else:
name = privmsgs.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):
"""<name> <password>
Identifies the user as <name>.
"""
(name, password) = privmsgs.getArgs(args, 2)
if not self._checkNotChannel(irc, msg):
return
2003-03-27 08:57:27 +01:00
try:
id = ircdb.users.getUserId(name)
user = ircdb.users.getUser(id)
2003-03-27 08:57:27 +01:00
except KeyError:
irc.error(msg, conf.replyNoUser)
return
if user.checkPassword(password):
user.setAuth(msg.prefix)
ircdb.users.setUser(id, user)
2003-03-27 08:57:27 +01:00
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:
id = ircdb.users.getUserId(msg.prefix)
user = ircdb.users.getUser(id)
2003-03-27 08:57:27 +01:00
except KeyError:
irc.error(msg, conf.replyNoUser)
return
user.unsetAuth()
ircdb.users.setUser(id, user)
2003-03-27 08:57:27 +01:00
irc.reply(msg, conf.replySuccess)
def whoami(self, irc, msg, args):
"""takes no arguments
Returns the name of the user calling the command.
"""
try:
user = ircdb.users.getUser(msg.prefix)
irc.reply(msg, user.name)
2003-03-27 08:57:27 +01:00
except KeyError:
irc.error(msg, conf.replyNotRegistered)
2003-03-27 08:57:27 +01:00
Class = UserCommands
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
2003-04-20 23:56:41 +02:00