2003-03-12 07:26:59 +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-08-02 08:22:13 +02:00
|
|
|
"""
|
|
|
|
This module provides the basic IrcMsg object used throughout the bot to
|
|
|
|
represent the actual messages. It also provides several helper functions to
|
|
|
|
construct such messages in an easier way than the constructor for the IrcMsg
|
|
|
|
object (which, as you'll read later, is quite...full-featured :))
|
|
|
|
"""
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
from fix import *
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
import debug
|
2003-03-12 07:26:59 +01:00
|
|
|
import ircutils
|
|
|
|
|
|
|
|
###
|
|
|
|
# IrcMsg class -- used for representing IRC messages acquired from a network.
|
|
|
|
###
|
2003-04-04 08:06:33 +02:00
|
|
|
|
2003-09-17 20:21:35 +02:00
|
|
|
class MalformedIrcMsg(ValueError):
|
|
|
|
pass
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
class IrcMsg(object):
|
|
|
|
"""Class to represent an IRC message.
|
2003-08-02 08:22:13 +02:00
|
|
|
|
|
|
|
As usual, ignore attributes that begin with an underscore. They simply
|
|
|
|
don't exist. Instances of this class are *not* to be modified, since they
|
|
|
|
are hashable. Public attributes of this class are .prefix, .command,
|
|
|
|
.args, .nick, .user, and .host.
|
|
|
|
|
|
|
|
The constructor for this class is pretty intricate. It's designed to take
|
|
|
|
any of three major (sets of) arguments.
|
|
|
|
|
|
|
|
Called with no keyword arguments, it takes a single string that is a raw
|
|
|
|
IRC message (such as one taken straight from the network.
|
|
|
|
|
|
|
|
Called with keyword arguments, it *requires* a command parameter. Args is
|
|
|
|
optional, but with most commands will be necessary. Prefix is obviously
|
|
|
|
optional, since clients aren't allowed (well, technically, they are, but
|
|
|
|
only in a completely useless way) to send prefixes to the server.
|
|
|
|
|
|
|
|
Since this class isn't to be modified, the constructor also accepts a 'msg'
|
|
|
|
keyword argument representing a message from which to take all the
|
|
|
|
attributes not provided otherwise as keyword arguments. So, for instance,
|
|
|
|
if a programmer wanted to take a PRIVMSG he'd gotten and simply redirect it
|
|
|
|
to a different source, he could do this:
|
|
|
|
|
|
|
|
IrcMsg(prefix='', args=(newSource, otherMsg.args[1]), msg=otherMsg)
|
2003-03-12 07:26:59 +01:00
|
|
|
"""
|
2003-08-11 08:57:17 +02:00
|
|
|
__slots__ = ('args', 'command', 'host', 'nick', 'prefix', 'user',
|
2003-08-01 16:23:54 +02:00
|
|
|
'_hash', '_str', '_repr', '_len')
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self, s='', command='', args=None, prefix='', msg=None):
|
2003-09-17 09:53:11 +02:00
|
|
|
assert not (msg and s), 'Ircmsg.__init__ cannot accept both s and msg'
|
2003-04-05 12:28:30 +02:00
|
|
|
if not s and not command and not msg:
|
2003-09-17 20:21:35 +02:00
|
|
|
raise MalformedIrcMsg, 'IRC messages require a command.'
|
2003-08-01 16:23:54 +02:00
|
|
|
self._str = None
|
|
|
|
self._repr = None
|
|
|
|
self._hash = None
|
|
|
|
self._len = None
|
2003-09-17 09:53:11 +02:00
|
|
|
if s:
|
2003-09-17 20:21:35 +02:00
|
|
|
originalString = s
|
|
|
|
try:
|
|
|
|
self._str = s
|
|
|
|
if s[0] == ':':
|
|
|
|
self.prefix, s = s[1:].split(None, 1)
|
|
|
|
else:
|
|
|
|
self.prefix = ''
|
|
|
|
if ' :' in s:
|
|
|
|
s, last = s.split(' :', 1)
|
|
|
|
self.args = s.split()
|
|
|
|
self.args.append(last.rstrip('\r\n'))
|
|
|
|
else:
|
|
|
|
self.args = s.split()
|
|
|
|
self.command = self.args.pop(0)
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
raise MalformedIrcMsg, repr(originalString)
|
2003-09-17 09:53:11 +02:00
|
|
|
else:
|
|
|
|
if msg is not None:
|
|
|
|
if prefix:
|
|
|
|
self.prefix = prefix
|
|
|
|
else:
|
|
|
|
self.prefix = msg.prefix
|
|
|
|
if command:
|
|
|
|
self.command = command
|
|
|
|
else:
|
|
|
|
self.command = msg.command
|
|
|
|
if args:
|
|
|
|
self.args = args
|
|
|
|
else:
|
|
|
|
self.args = msg.args
|
|
|
|
else:
|
|
|
|
self.prefix = prefix
|
|
|
|
self.command = command
|
|
|
|
assert all(ircutils.isValidArgument, args)
|
|
|
|
self.args = args
|
|
|
|
self.args = tuple(self.args)
|
2003-08-11 08:57:17 +02:00
|
|
|
if ircutils.isUserHostmask(self.prefix):
|
|
|
|
(self.nick,self.user,self.host)=ircutils.splitHostmask(self.prefix)
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-08-12 21:12:44 +02:00
|
|
|
(self.nick, self.user, self.host) = (self.prefix,)*3
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def __str__(self):
|
2003-08-01 16:23:54 +02:00
|
|
|
if self._str is not None:
|
|
|
|
return self._str
|
2003-03-12 07:26:59 +01:00
|
|
|
if self.prefix:
|
|
|
|
if len(self.args) > 1:
|
2003-08-11 08:57:17 +02:00
|
|
|
self._str = ':%s %s %s :%s\r\n' % \
|
|
|
|
(self.prefix, self.command,
|
|
|
|
' '.join(self.args[:-1]), self.args[-1])
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-08-11 07:08:33 +02:00
|
|
|
if self.args:
|
2003-08-11 08:57:17 +02:00
|
|
|
self._str = ':%s %s :%s\r\n' % \
|
|
|
|
(self.prefix, self.command, self.args[0])
|
2003-08-11 07:08:33 +02:00
|
|
|
else:
|
2003-08-11 08:57:17 +02:00
|
|
|
self._str = ':%s %s\r\n' % (self.prefix, self.command)
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-08-11 07:08:33 +02:00
|
|
|
if len(self.args) > 1:
|
2003-08-11 08:57:17 +02:00
|
|
|
self._str = '%s %s :%s\r\n' % \
|
|
|
|
(self.command,
|
|
|
|
' '.join(self.args[:-1]), self.args[-1])
|
2003-08-11 07:08:33 +02:00
|
|
|
else:
|
|
|
|
if self.args:
|
2003-08-11 08:57:17 +02:00
|
|
|
self._str = '%s :%s\r\n' % (self.command, self.args[0])
|
2003-08-11 07:08:33 +02:00
|
|
|
else:
|
2003-08-11 08:57:17 +02:00
|
|
|
self._str = '%s\r\n' % self.command
|
|
|
|
return self._str
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
# This might not take into account the length of the prefix, but leaves
|
|
|
|
# some room for variation.
|
2003-08-01 16:23:54 +02:00
|
|
|
if self._len is not None:
|
|
|
|
return self._len
|
2003-08-11 08:57:17 +02:00
|
|
|
self._len = 0
|
2003-03-12 07:26:59 +01:00
|
|
|
if self.prefix:
|
2003-08-11 08:57:17 +02:00
|
|
|
self._len += len(self.prefix)
|
2003-08-26 17:03:34 +02:00
|
|
|
## else:
|
|
|
|
## self._len += 42
|
2003-08-11 08:57:17 +02:00
|
|
|
self._len += len(self.command)
|
2003-03-12 07:26:59 +01:00
|
|
|
if self.args:
|
|
|
|
for arg in self.args:
|
2003-08-11 08:57:17 +02:00
|
|
|
self._len += len(arg) + 1 # Remember space prior to the arg.
|
|
|
|
self._len += 2 # For colon before the prefix and before the last arg.
|
|
|
|
return self._len
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def __eq__(self, other):
|
2003-04-04 00:06:24 +02:00
|
|
|
return hash(self) == hash(other) and \
|
|
|
|
self.command == other.command and \
|
|
|
|
self.prefix == other.prefix and \
|
2003-03-12 07:26:59 +01:00
|
|
|
self.args == other.args
|
|
|
|
|
2003-04-04 11:12:00 +02:00
|
|
|
def __ne__(self, other):
|
|
|
|
return not (self == other)
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def __hash__(self):
|
2003-08-01 16:23:54 +02:00
|
|
|
if self._hash is not None:
|
2003-04-10 09:56:41 +02:00
|
|
|
return self._hash
|
2003-09-08 22:29:02 +02:00
|
|
|
self._hash = hash(self.command) ^ \
|
|
|
|
hash(self.prefix) ^ \
|
2003-08-01 16:23:54 +02:00
|
|
|
hash(self.args)
|
|
|
|
return self._hash
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
2003-08-01 16:23:54 +02:00
|
|
|
if self._repr is not None:
|
|
|
|
return self._repr
|
|
|
|
self._repr = 'IrcMsg(prefix=%r, command=%r, args=%r)' % \
|
|
|
|
(self.prefix, self.command, self.args)
|
|
|
|
return self._repr
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-05 12:40:36 +02:00
|
|
|
def __getstate__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
def __setstate__(self, s):
|
|
|
|
self.__init__(s)
|
|
|
|
|
2003-08-12 21:12:44 +02:00
|
|
|
## try:
|
|
|
|
## import _ircmsg
|
|
|
|
## IrcMsg = _ircmsg.IrcMsg
|
|
|
|
## except:
|
|
|
|
## pass
|
2003-08-11 19:16:03 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def isAction(msg):
|
2003-08-02 08:22:13 +02:00
|
|
|
"""A predicate returning true if the PRIVMSG in question is an ACTION"""
|
2003-03-12 07:26:59 +01:00
|
|
|
return msg.command == 'PRIVMSG' and \
|
|
|
|
msg.args[1].startswith('\x01ACTION') and \
|
|
|
|
msg.args[1].endswith('\x01')
|
|
|
|
|
2003-04-04 00:06:24 +02:00
|
|
|
_unactionre = re.compile(r'^\x01ACTION (.*)\x01$')
|
2003-03-12 07:26:59 +01:00
|
|
|
def unAction(msg):
|
2003-08-02 08:22:13 +02:00
|
|
|
"""Returns the payload (i.e., non-ACTION text) of an ACTION msg."""
|
2003-03-12 07:26:59 +01:00
|
|
|
return _unactionre.match(msg.args[1]).group(1)
|
|
|
|
|
|
|
|
def prettyPrint(msg, addRecipients=False):
|
2003-08-02 08:22:13 +02:00
|
|
|
"""Provides a client-friendly string form for messages.
|
|
|
|
|
|
|
|
IIRC, I copied BitchX's (or was it xchat's?) format for messages.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
def nickorprefix():
|
|
|
|
return msg.nick or msg.prefix
|
|
|
|
def nick():
|
|
|
|
if addRecipients:
|
|
|
|
return '%s/%s' % (msg.nick, msg.args[0])
|
|
|
|
else:
|
|
|
|
return msg.nick
|
|
|
|
if msg.command == 'PRIVMSG':
|
|
|
|
m = _unactionre.match(msg.args[1])
|
|
|
|
if m:
|
|
|
|
s = '* %s %s' % (nick(), m.group(1))
|
|
|
|
else:
|
|
|
|
s = '<%s> %s' % (nick(), msg.args[1])
|
|
|
|
elif msg.command == 'NOTICE':
|
|
|
|
s = '-%s- %s' % (nick(), msg.args[1])
|
|
|
|
elif msg.command == 'JOIN':
|
|
|
|
s = '*** %s has joined %s' % (msg.nick, msg.args[0])
|
|
|
|
elif msg.command == 'PART':
|
|
|
|
if len(msg.args) > 1:
|
|
|
|
partmsg = ' (%s)' % msg.args[1]
|
|
|
|
else:
|
|
|
|
partmsg = ''
|
|
|
|
s = '*** %s has parted %s%s' % (msg.nick, msg.args[0], partmsg)
|
|
|
|
elif msg.command == 'KICK':
|
|
|
|
if len(msg.args) > 2:
|
|
|
|
kickmsg = ' (%s)' % msg.args[1]
|
|
|
|
else:
|
|
|
|
kickmsg = ''
|
|
|
|
s = '*** %s was kicked by %s%s' % (msg.args[1], msg.nick, kickmsg)
|
|
|
|
elif msg.command == 'MODE':
|
|
|
|
s = '*** %s sets mode: %s' % (nickorprefix(), ' '.join(msg.args))
|
|
|
|
elif msg.command == 'QUIT':
|
|
|
|
if msg.args:
|
|
|
|
quitmsg = ' (%s)' % msg.args[0]
|
|
|
|
else:
|
|
|
|
quitmsg = ''
|
|
|
|
s = '*** %s has quit IRC%s' % quitmsg
|
|
|
|
elif msg.command == 'TOPIC':
|
|
|
|
s = '*** %s changes topic to %s' % (nickorprefix(), msg.args[1])
|
|
|
|
return s
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
###
|
|
|
|
# Various IrcMsg functions
|
|
|
|
###
|
|
|
|
|
|
|
|
MODE = 'MODE'
|
|
|
|
|
2003-04-04 00:06:24 +02:00
|
|
|
isNick = ircutils.isNick
|
|
|
|
isChannel = ircutils.isChannel
|
|
|
|
isUserHostmask = ircutils.isUserHostmask
|
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def pong(payload, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Takes a payload and returns the proper PONG IrcMsg."""
|
|
|
|
assert payload, 'PONG requires a payload'
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='PONG', args=(payload,))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def ping(payload, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Takes a payload and returns the proper PING IrcMsg."""
|
|
|
|
assert payload, 'PING requires a payload'
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='PING', args=(payload,))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def op(channel, nick, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to op nick on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
|
|
|
assert isNick(nick), repr(nick)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '+o', nick))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def ops(channel, nicks, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to op each of nicks on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isNick, nicks), nicks
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE,
|
2003-03-12 07:26:59 +01:00
|
|
|
args=(channel, '+' + ('o'*len(nicks)), nicks))
|
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def deop(channel, nick, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to deop nick on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
|
|
|
assert isNick(nick), repr(nick)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-o', nick))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def deops(channel, nicks, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to deop each of nicks on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isNick, nicks), nicks
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE,
|
|
|
|
args=(channel, '-' + ('o'*len(nicks)), nicks))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def halfop(channel, nick, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to halfop nick on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
|
|
|
assert isNick(nick), repr(nick)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '+h', nick))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def halfops(channel, nicks, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to halfop each of nicks on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isNick, nicks), nicks
|
2003-04-21 08:12:57 +02:00
|
|
|
return IrcMsg(prefix=prefix,
|
|
|
|
command=MODE,
|
|
|
|
args=(channel, '+' + ('h'*len(nicks)), nicks))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def dehalfop(channel, nick, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to dehalfop nick on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
|
|
|
assert isNick(nick), repr(nick)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-h', nick))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def dehalfops(channel, nicks, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to dehalfop each of nicks on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isNick, nicks), nicks
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE,
|
|
|
|
args=(channel, '-' + ('h'*len(nicks)), nicks))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def voice(channel, nick, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to voice nick on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
|
|
|
assert isNick(nick), repr(nick)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '+v', nick))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def voices(channel, nicks, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to voice each of nicks on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isNick, nicks)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE,
|
|
|
|
args=(channel, '+' + ('v'*len(nicks)), nicks))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def devoice(channel, nick, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to devoice nick on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
|
|
|
assert isNick(nick), repr(nick)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-v', nick))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def devoices(channel, nicks, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to devoice each of nicks on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isNick, nicks), nicks
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE,
|
|
|
|
args=(channel, '-' + ('v'*len(nicks)), nicks))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-06 14:26:35 +02:00
|
|
|
def ban(channel, hostmask, exception='', prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to ban nick on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
|
|
|
assert isUserHostmask(hostmask), repr(hostmask)
|
2003-04-06 14:26:35 +02:00
|
|
|
modes = [('+b', hostmask)]
|
|
|
|
if exception:
|
|
|
|
modes.append(('+e', exception))
|
|
|
|
return IrcMsg(prefix=prefix, command=MODE,
|
|
|
|
args=[channel] + ircutils.joinModes(modes))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-06 14:26:35 +02:00
|
|
|
def bans(channel, hostmasks, exceptions=(), prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to ban each of nicks on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isUserHostmask, hostmasks), hostmasks
|
2003-04-06 14:26:35 +02:00
|
|
|
modes = [('+b', s) for s in hostmasks] + [('+e', s) for s in exceptions]
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE,
|
2003-04-06 14:26:35 +02:00
|
|
|
args=[channel] + ircutils.joinModes(modes))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def unban(channel, hostmask, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to unban nick on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
|
|
|
assert isUserHostmask(hostmask), repr(hostmask)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-b', hostmask))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def unbans(channel, hostmasks, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a MODE to unban each of nicks on channel."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isUserHostmask, hostmasks), hostmasks
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command=MODE,
|
2003-03-12 07:26:59 +01:00
|
|
|
args=(channel, '-' + ('b'*len(hostmasks)), hostmasks))
|
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def kick(channel, nick, msg='', prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a KICK to kick nick from channel with the message msg."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
|
|
|
assert isNick(nick), repr(nick)
|
2003-03-12 07:26:59 +01:00
|
|
|
if msg:
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='KICK', args=(channel, nick, msg))
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='KICK', args=(channel, nick))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def kicks(channel, nicks, msg='', prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a KICK to kick each of nicks from channel with the message msg.
|
|
|
|
"""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isNick, nicks), nicks
|
2003-03-12 07:26:59 +01:00
|
|
|
if msg:
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='KICK',
|
|
|
|
args=(channel, ','.join(nicks), msg))
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='KICK',
|
|
|
|
args=(channel, ','.join(nicks)))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def privmsg(recipient, msg, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a PRIVMSG to recipient with the message msg."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert (isChannel(recipient) or isNick(recipient)), repr(recipient)
|
2003-04-14 07:13:21 +02:00
|
|
|
assert msg, 'msg must not be empty.'
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='PRIVMSG', args=(recipient, msg))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def action(recipient, msg, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a PRIVMSG ACTION to recipient with the message msg."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert (isChannel(recipient) or isNick(recipient)), repr(recipient)
|
2003-04-14 07:13:21 +02:00
|
|
|
assert msg, 'msg must not be empty.'
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='PRIVMSG',
|
|
|
|
args=(recipient,'\x01ACTION %s\x01'% msg))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def notice(recipient, msg, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a NOTICE to recipient with the message msg."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert (isChannel(recipient) or isNick(recipient)), repr(recipient)
|
2003-04-14 07:13:21 +02:00
|
|
|
assert msg, 'msg must not be empty.'
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='NOTICE', args=(recipient, msg))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-10 09:56:41 +02:00
|
|
|
def join(channel, key=None, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a JOIN to a channel"""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-04-10 09:56:41 +02:00
|
|
|
if key is None:
|
|
|
|
return IrcMsg(prefix=prefix, command='JOIN', args=(channel,))
|
|
|
|
else:
|
2003-04-20 19:28:26 +02:00
|
|
|
assert key.translate(string.ascii, string.ascii[128:]) == key and \
|
2003-04-20 09:17:40 +02:00
|
|
|
'\x00' not in key and \
|
|
|
|
'\r' not in key and \
|
|
|
|
'\n' not in key and \
|
|
|
|
'\f' not in key and \
|
|
|
|
'\t' not in key and \
|
|
|
|
'\v' not in key and \
|
|
|
|
' ' not in key
|
2003-04-10 09:56:41 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='JOIN', args=(channel, key))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-10 09:56:41 +02:00
|
|
|
def joins(channels, keys=None, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a JOIN to each of channels."""
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isChannel, channels), channels
|
2003-04-10 09:56:41 +02:00
|
|
|
if keys is None:
|
|
|
|
keys = []
|
|
|
|
assert len(keys) <= len(channels)
|
|
|
|
if not keys:
|
|
|
|
return IrcMsg(prefix=prefix,
|
|
|
|
command='JOIN',
|
|
|
|
args=(','.join(channels),))
|
|
|
|
else:
|
2003-04-20 09:17:40 +02:00
|
|
|
for key in keys:
|
2003-04-20 19:28:26 +02:00
|
|
|
assert key.translate(string.ascii, string.ascii[128:]) == key and \
|
2003-04-20 09:17:40 +02:00
|
|
|
'\x00' not in key and \
|
|
|
|
'\r' not in key and \
|
|
|
|
'\n' not in key and \
|
|
|
|
'\f' not in key and \
|
|
|
|
'\t' not in key and \
|
|
|
|
'\v' not in key and \
|
|
|
|
' ' not in key
|
2003-04-10 09:56:41 +02:00
|
|
|
return IrcMsg(prefix=prefix,
|
|
|
|
command='JOIN',
|
|
|
|
args=(','.join(channels), ','.join(keys)))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def part(channel, msg='', prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a PART from channel with the message msg."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-03-12 07:26:59 +01:00
|
|
|
if msg:
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='PART', args=(channel, msg))
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='PART', args=(channel,))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def parts(channels, msg='', prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a PART from each of channels with the message msg."""
|
2003-08-23 14:11:46 +02:00
|
|
|
assert all(isChannel, channels), channels
|
2003-03-12 07:26:59 +01:00
|
|
|
if msg:
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='PART',
|
|
|
|
args=(','.join(channels), msg,))
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='PART',
|
|
|
|
args=(','.join(channels),))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def quit(msg='', prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a QUIT with the message msg."""
|
2003-03-12 07:26:59 +01:00
|
|
|
if msg:
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='QUIT', args=(msg,))
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='QUIT')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-09-06 03:28:12 +02:00
|
|
|
def topic(channel, topic=None, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a TOPIC for channel with the topic topic."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isChannel(channel), repr(channel)
|
2003-09-06 03:28:12 +02:00
|
|
|
if topic is None:
|
|
|
|
return IrcMsg(prefix=prefix, command='TOPIC', args=(channel,))
|
|
|
|
else:
|
|
|
|
return IrcMsg(prefix=prefix, command='TOPIC', args=(channel, topic))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def nick(nick, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a NICK with nick nick."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isNick(nick), repr(nick)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='NICK', args=(nick,))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-06 14:26:35 +02:00
|
|
|
def user(ident, user, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a USER with ident ident and user user."""
|
2003-04-20 19:57:16 +02:00
|
|
|
assert '\x00' not in ident and \
|
|
|
|
'\r' not in ident and \
|
|
|
|
'\n' not in ident and \
|
|
|
|
' ' not in ident and \
|
|
|
|
'@' not in ident
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='USER', args=(ident, '0', '*', user))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def who(hostmaskOrChannel, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a WHO for the hostmask or channel hostmaskOrChannel."""
|
2003-04-14 07:13:21 +02:00
|
|
|
assert isChannel(hostmaskOrChannel) or isUserHostmask(hostmaskOrChannel), \
|
2003-09-09 22:44:58 +02:00
|
|
|
repr(hostmaskOrChannel)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='WHO', args=(hostmaskOrChannel,))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-08-21 18:53:19 +02:00
|
|
|
def whois(nick, mask='', prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns a WHOIS for nick."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isNick(nick), repr(nick)
|
2003-08-21 18:53:19 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='WHOIS', args=(nick, mask))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:06:33 +02:00
|
|
|
def invite(channel, nick, prefix=''):
|
2003-04-04 00:06:24 +02:00
|
|
|
"""Returns an INVITE for nick."""
|
2003-09-09 22:44:58 +02:00
|
|
|
assert isNick(nick), repr(nick)
|
2003-04-04 08:06:33 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='INVITE', args=(channel, nick))
|
2003-03-31 11:27:15 +02:00
|
|
|
|
2003-04-06 14:26:35 +02:00
|
|
|
def password(password, prefix=''):
|
2003-08-02 08:22:13 +02:00
|
|
|
"""Returns a PASS command for accessing a server."""
|
2003-04-14 07:13:21 +02:00
|
|
|
assert password, 'password must not be empty.'
|
2003-04-06 14:26:35 +02:00
|
|
|
return IrcMsg(prefix=prefix, command='PASS', args=(password,))
|
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|