Limnoria/src/Channel.py

384 lines
16 KiB
Python
Raw Normal View History

2003-03-27 07:28:13 +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
Basic channel management commands. Many of these commands require their caller
to have the <channel>.op capability. This plugin is loaded by default.
2003-03-27 07:28:13 +01:00
"""
import time
import conf
2003-10-15 06:46:19 +02:00
import debug
2003-03-27 07:28:13 +01:00
import ircdb
import utils
2003-03-27 07:28:13 +01:00
import ircmsgs
import schedule
import ircutils
import privmsgs
import callbacks
2003-10-21 08:03:57 +02:00
class Channel(callbacks.Privmsg):
2003-09-29 07:01:41 +02:00
def op(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>]
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will give you ops.
"""
2003-09-29 07:01:41 +02:00
if irc.nick in irc.state.channels[channel].ops:
irc.queueMsg(ircmsgs.op(channel, msg.nick))
2003-03-27 07:28:13 +01:00
else:
2003-09-29 07:01:41 +02:00
irc.error(msg, 'How can I op you? I\'m not opped!')
op = privmsgs.checkChannelCapability(op, 'op')
2003-03-27 07:28:13 +01:00
2003-09-29 07:01:41 +02:00
def halfop(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>]
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.halfop
capability, this will give you halfops.
"""
2003-09-29 07:01:41 +02:00
if irc.nick in irc.state.channels[channel].ops:
irc.queueMsg(ircmsgs.halfop(channel, msg.nick))
2003-03-27 07:28:13 +01:00
else:
2003-09-29 07:01:41 +02:00
irc.error(msg, 'How can I halfop you? I\'m not opped!')
halfop = privmsgs.checkChannelCapability(halfop, 'halfop')
2003-03-27 07:28:13 +01:00
2003-09-29 07:01:41 +02:00
def voice(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>]
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.voice capability,
this will give you voice.
"""
2003-09-29 07:01:41 +02:00
if irc.nick in irc.state.channels[channel].ops:
irc.queueMsg(ircmsgs.voice(channel, msg.nick))
2003-03-27 07:28:13 +01:00
else:
2003-09-29 07:01:41 +02:00
irc.error(msg, 'How can I voice you? I\'m not opped!')
voice = privmsgs.checkChannelCapability(voice, 'voice')
def cycle(self, irc, msg, args, channel):
"""[<channel>] [<key>]
2003-03-27 07:28:13 +01:00
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will cause the bot to "cycle", or PART and then JOIN the channel.
If <key> is given, join the channel using that key.
2003-03-27 07:28:13 +01:00
"""
key = privmsgs.getArgs(args, required=0, optional=1)
if not key:
key = None
2003-09-29 07:01:41 +02:00
irc.queueMsg(ircmsgs.part(channel))
irc.queueMsg(ircmsgs.join(channel, key))
2003-09-29 07:01:41 +02:00
cycle = privmsgs.checkChannelCapability(cycle, 'op')
2003-03-27 07:28:13 +01:00
def kban(self, irc, msg, args):
"""[<channel>] <nick> [<number of seconds to ban>]
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will kickban <nick> for as many seconds as you specify, or else
(if you specify 0 seconds or don't specify a number of seconds) it
will ban the person indefinitely.
"""
channel = privmsgs.getChannel(msg, args)
(bannedNick, length) = privmsgs.getArgs(args, optional=1)
if bannedNick == irc.nick:
irc.error(msg, 'I cowardly refuse to kickban myself.')
return
2003-03-27 07:28:13 +01:00
length = int(length or 0)
try:
bannedHostmask = irc.state.nickToHostmask(bannedNick)
except KeyError:
irc.error(msg, 'I haven\'t seen %s.' % bannedNick)
return
2003-03-27 07:28:13 +01:00
capability = ircdb.makeChannelCapability(channel, 'op')
banmask = ircutils.banmask(bannedHostmask)
if ircutils.hostmaskPatternEqual(banmask, irc.prefix):
banmask = bannedHostmask
if bannedNick == msg.nick or \
(ircdb.checkCapability(msg.prefix, capability) \
and not ircdb.checkCapability(bannedHostmask, capability)):
if irc.nick in irc.state.channels[channel].ops:
irc.queueMsg(ircmsgs.ban(channel, banmask))
irc.queueMsg(ircmsgs.kick(channel, bannedNick, msg.nick))
if length > 0:
def f():
irc.queueMsg(ircmsgs.unban(channel, banmask))
schedule.addEvent(f, time.time() + length)
else:
irc.error(msg, 'How can I do that? I\'m not opped.')
2003-03-27 07:28:13 +01:00
else:
irc.error(msg, conf.replyNoCapability % capability)
2003-10-15 06:46:19 +02:00
def unban(self, irc, msg, args, channel):
"""[<channel>] <hostmask>
Unbans <hostmask> on <channel>. Especially useful for unbanning
yourself when you get unexpectedly (or accidentally) banned from
the channel. <channel> is only necessary if the message isn't sent
in the channel itself.
"""
hostmask = privmsgs.getArgs(args)
if irc.nick in irc.state.channels[channel].ops:
irc.queueMsg(ircmsgs.unban(channel, hostmask))
irc.reply(msg, conf.replySuccess)
else:
irc.error(msg, 'How can I unban someone? I\'m not opped.')
unban = privmsgs.checkChannelCapability(unban, 'op')
2003-09-29 07:01:41 +02:00
def lobotomize(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>]
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will "lobotomize" the bot, making it silent and unanswering to
all requests made in the channel.
"""
2003-09-29 07:01:41 +02:00
ircdb.channels.getChannel(channel).lobotomized = True
irc.reply(msg, conf.replySuccess)
lobotomize = privmsgs.checkChannelCapability(lobotomize, 'op')
2003-03-27 07:28:13 +01:00
2003-09-29 07:01:41 +02:00
def unlobotomize(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>]
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will unlobotomize the bot, making it respond to requests made in
the channel again.
"""
2003-09-29 07:01:41 +02:00
ircdb.channels.getChannel(channel).lobotomized = False
irc.reply(msg, conf.replySuccess)
unlobotomize = privmsgs.checkChannelCapability(unlobotomize, 'op')
2003-03-27 07:28:13 +01:00
2003-09-29 07:01:41 +02:00
def permban(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>] <nick|hostmask>
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will effect a permanent (persistent) ban on the given <hostmask>
(or the current hostmask associated with <nick>.
"""
arg = privmsgs.getArgs(args)
if ircutils.isNick(arg):
banmask = ircutils.banmask(irc.state.nickToHostmask(arg))
2003-09-29 07:01:41 +02:00
elif ircutils.isUserHostmask(arg):
2003-03-27 07:28:13 +01:00
banmask = arg
else:
2003-09-29 07:01:41 +02:00
irc.error(msg, 'That\'s not a valid nick or hostmask.')
return
c = ircdb.channels.getChannel(channel)
c.addBan(banmask)
ircdb.channels.setChannel(channel, c)
irc.reply(msg, conf.replySuccess)
permban = privmsgs.checkChannelCapability(permban, 'op')
2003-03-27 07:28:13 +01:00
2003-09-29 07:01:41 +02:00
def unpermban(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>] <hostmask>
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will remove the permanent ban on <hostmask>.
"""
banmask = privmsgs.getArgs(args)
2003-09-29 07:01:41 +02:00
c = ircdb.channels.getChannel(channel)
c.removeBan(banmask)
ircdb.channels.setChannel(channel, c)
irc.reply(msg, conf.replySuccess)
unpermban = privmsgs.checkChannelCapability(unpermban, 'op')
2003-03-27 07:28:13 +01:00
2003-11-09 15:11:15 +01:00
def ignore(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>] <nick|hostmask>
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will set a permanent (persistent) ignore on <hostmask> or the
hostmask currently associated with <nick>.
"""
arg = privmsgs.getArgs(args)
if ircutils.isNick(arg):
banmask = ircutils.banmask(irc.state.nickToHostmask(arg))
2003-09-29 07:01:41 +02:00
elif ircutils.isUserHostmask(arg):
2003-03-27 07:28:13 +01:00
banmask = arg
else:
2003-09-29 07:01:41 +02:00
irc.error(msg, 'That\'s not a valid nick or hostmask.')
return
c = ircdb.channels.getChannel(channel)
c.addIgnore(banmask)
ircdb.channels.setChannel(channel, c)
irc.reply(msg, conf.replySuccess)
2003-11-09 15:11:15 +01:00
ignore = privmsgs.checkChannelCapability(ignore, 'op')
2003-03-27 07:28:13 +01:00
2003-11-09 15:11:15 +01:00
def unignore(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>] <hostmask>
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will remove the permanent ignore on <hostmask> in the channel.
"""
banmask = privmsgs.getArgs(args)
2003-09-29 07:01:41 +02:00
c = ircdb.channels.getChannel(channel)
c.removeIgnore(banmask)
ircdb.channels.setChannel(channel, c)
irc.reply(msg, conf.replySuccess)
2003-11-09 15:11:15 +01:00
unignore = privmsgs.checkChannelCapability(unignore, 'op')
2003-03-27 07:28:13 +01:00
2003-11-09 15:11:15 +01:00
def ignores(self, irc, msg, args, channel):
"""[<channel>]
Lists the hostmasks that the bot is ignoring on the given channel.
The <channel> argument is only necessary if the message isn't being
sent in the channel itself.
"""
channelarg = privmsgs.getArgs(args, required=0, optional=1)
channel = channelarg or channel
c = ircdb.channels.getChannel(channel)
if len(c.ignores) == 0:
irc.reply(msg, 'I\'m not currently ignoring any hostmasks '
'in %r' % channel)
return
irc.reply(msg, utils.commaAndify(map(repr,c.ignores)))
2003-11-09 15:11:15 +01:00
ignores = privmsgs.checkChannelCapability(ignores, 'op')
2003-11-09 15:11:15 +01:00
def addcapability(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>] <name|hostmask> <capability>
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will give the user currently identified as <name> (or the user
to whom <hostmask> maps) the capability <capability> in the channel.
"""
(name, capability) = privmsgs.getArgs(args, 2)
capability = ircdb.makeChannelCapability(channel, capability)
2003-09-29 07:01:41 +02:00
try:
id = ircdb.users.getUserId(name)
user = ircdb.users.getUser(id)
user.addCapability(capability)
ircdb.users.setUser(id, user)
irc.reply(msg, conf.replySuccess)
except KeyError:
irc.error(msg, conf.replyNoUser)
2003-11-09 15:11:15 +01:00
addcapability = privmsgs.checkChannelCapability(addcapability,'op')
2003-03-27 07:28:13 +01:00
2003-11-09 15:11:15 +01:00
def removecapability(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>] <name|hostmask> <capability>
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will take from the user currently identified as <name> (or the
user to whom <hostmask> maps) the capability <capability> in the
channel.
"""
(name, capability) = privmsgs.getArgs(args, 2)
capability = ircdb.makeChannelCapability(channel, capability)
2003-09-29 07:01:41 +02:00
try:
id = ircdb.users.getUser(name)
user = ircdb.users.getUser(id)
user.removeCapability(capability)
ircdb.users.setUser(id, user)
irc.reply(msg, conf.replySuccess)
except KeyError:
irc.error(msg, conf.replyNoUser)
2003-11-09 15:11:15 +01:00
removecapability = privmsgs.checkChannelCapability(removecapability, 'op')
2003-03-27 07:28:13 +01:00
2003-11-09 15:11:15 +01:00
def setdefaultcapability(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<channel>] <default response to unknown capabilities> <True|False>
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will set the default response to non-power-related (that is,
not {op, halfop, voice} capabilities to be the value you give.
"""
v = privmsgs.getArgs(args)
2003-09-29 07:01:41 +02:00
v = v.capitalize()
2003-10-04 14:57:44 +02:00
c = ircdb.channels.getChannel(channel)
2003-09-29 07:01:41 +02:00
if v == 'True':
c.setDefaultCapability(True)
elif v == 'False':
c.setDefaultCapability(False)
2003-03-27 07:28:13 +01:00
else:
2003-09-29 07:01:41 +02:00
s = 'The default value must be either True or False.'
irc.error(msg, s)
return
ircdb.channels.setChannel(channel, c)
irc.reply(msg, conf.replySuccess)
2003-11-09 15:11:15 +01:00
setdefaultcapability = \
privmsgs.checkChannelCapability(setdefaultcapability, 'op')
2003-09-29 07:01:41 +02:00
2003-11-09 15:11:15 +01:00
def setcapability(self, irc, msg, args, channel):
2003-04-02 13:08:34 +02:00
"""[<channel>] <capability>
2003-03-27 07:28:13 +01:00
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
2003-04-02 13:08:34 +02:00
this will add the channel capability <capability> for all users in the
2003-03-27 07:28:13 +01:00
channel.
"""
2003-09-29 07:01:41 +02:00
capability = privmsgs.getArgs(args)
c = ircdb.channels.getChannel(channel)
c.addCapability(capability)
ircdb.channels.setChannel(channel, c)
irc.reply(msg, conf.replySuccess)
2003-11-09 15:11:15 +01:00
setcapability = privmsgs.checkChannelCapability(setcapability, 'op')
2003-03-27 07:28:13 +01:00
2003-11-09 15:11:15 +01:00
def unsetcapability(self, irc, msg, args, channel):
2003-03-27 07:28:13 +01:00
"""[<chanel>] <capability>
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. If you have the #channel.op capability,
this will unset the channel capability <capability> so each user's
specific capability or the channel default capability will take
precedence.
"""
2003-09-29 07:01:41 +02:00
capability = privmsgs.getArgs(args)
c = ircdb.channels.getChannel(channel)
c.removeCapability(capability)
ircdb.channels.setChannel(channel, c)
irc.reply(msg, conf.replySuccess)
2003-11-09 15:11:15 +01:00
unsetcapability = privmsgs.checkChannelCapability(unsetcapability, 'op')
2003-03-27 07:28:13 +01:00
2003-11-09 15:11:15 +01:00
def capabilities(self, irc, msg, args):
2003-04-02 13:08:34 +02:00
"""[<channel>]
The <channel> argument is only necessary if the message isn't being
sent in the channel itself. Returns the capabilities present on the
<channel>.
"""
channel = privmsgs.getChannel(msg, args)
c = ircdb.channels.getChannel(channel)
irc.reply(msg, ', '.join(c.capabilities))
2003-03-27 07:28:13 +01:00
2003-10-21 08:03:57 +02:00
Class = Channel
2003-03-27 07:28:13 +01:00
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: