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-03-26 09:39:50 +01:00
|
|
|
"""
|
|
|
|
Handles relaying between networks.
|
|
|
|
|
|
|
|
Commands include:
|
|
|
|
startrelay
|
|
|
|
relayconnect
|
|
|
|
relaydisconnect
|
|
|
|
relayjoin
|
|
|
|
relaypart
|
|
|
|
"""
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
from baseplugin import *
|
|
|
|
|
2003-03-27 09:17:51 +01:00
|
|
|
import re
|
|
|
|
|
2003-03-26 09:39:50 +01:00
|
|
|
import ircdb
|
|
|
|
import debug
|
|
|
|
import irclib
|
|
|
|
import ircmsgs
|
|
|
|
import ircutils
|
2003-03-12 07:26:59 +01:00
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
2003-03-26 09:39:50 +01:00
|
|
|
import asyncoreDrivers
|
|
|
|
|
|
|
|
class IdDict(dict):
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
dict.__setitem__(self, id(key), value)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-03-26 09:39:50 +01:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return dict.__getitem__(self, id(key))
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
return dict.__contains__(self, id(key))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
class Relay(callbacks.Privmsg):
|
2003-03-26 09:39:50 +01:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
|
|
|
self.ircs = []
|
|
|
|
self.started = False
|
|
|
|
self.abbreviations = {} #IdDict({})
|
|
|
|
|
|
|
|
def startrelay(self, irc, msg, args):
|
|
|
|
"<network abbreviation for current server>"
|
|
|
|
if ircdb.checkCapability(msg.prefix, 'owner'):
|
|
|
|
realIrc = irc.getRealIrc()
|
|
|
|
abbreviation = privmsgs.getArgs(args)
|
|
|
|
self.ircs.append(realIrc)
|
|
|
|
self.abbreviations[realIrc] = abbreviation
|
|
|
|
self.started = True
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
irc.error(msg, conf.replyNoCapability % 'owner')
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def relayconnect(self, irc, msg, args):
|
2003-03-26 09:39:50 +01:00
|
|
|
"<network abbreviation> <domain:port> (port defaults to 6667)"
|
|
|
|
if ircdb.checkCapability(msg.prefix, 'owner'):
|
|
|
|
abbreviation, server = privmsgs.getArgs(args, needed=2)
|
|
|
|
if ':' in server:
|
|
|
|
(server, port) = server.split(':')
|
|
|
|
port = int(port)
|
|
|
|
else:
|
|
|
|
port = 6667
|
|
|
|
newIrc = irclib.Irc(irc.nick, callbacks=irc.callbacks)
|
|
|
|
driver = asyncoreDrivers.AsyncoreDriver((server, port))
|
|
|
|
driver.irc = newIrc
|
|
|
|
newIrc.driver = driver
|
|
|
|
self.ircs.append(newIrc)
|
|
|
|
self.abbreviations[newIrc] = abbreviation
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
irc.reply(msg, conf.replyNoCapability % 'owner')
|
|
|
|
|
|
|
|
def relaydisconnect(self, irc, msg, args):
|
|
|
|
"<network>"
|
2003-03-12 07:26:59 +01:00
|
|
|
pass
|
2003-03-26 09:39:50 +01:00
|
|
|
|
|
|
|
def relayjoin(self, irc, msg, args):
|
|
|
|
"<channel>"
|
|
|
|
if ircdb.checkCapability(msg.prefix, 'owner'):
|
|
|
|
channel = privmsgs.getArgs(args)
|
|
|
|
for otherIrc in self.ircs:
|
|
|
|
if channel not in otherIrc.state.channels:
|
|
|
|
otherIrc.queueMsg(ircmsgs.join(channel))
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
irc.reply(msg, conf.replyNoCapability % 'owner')
|
|
|
|
|
|
|
|
def relaypart(self, irc, msg, args):
|
|
|
|
"<channel>"
|
|
|
|
if ircdb.checkCapability(msg.prefix, 'owner'):
|
|
|
|
channel = privmsgs.getArgs(args)
|
|
|
|
for otherIrc in self.ircs:
|
|
|
|
if channel in otherIrc.state.channels:
|
|
|
|
otherIrc.queueMsg(ircmsgs.part(channel))
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
irc.reply(msg, conf.replyNoCapability % 'owner')
|
|
|
|
|
2003-03-27 09:17:51 +01:00
|
|
|
def _formatPrivmsg(self, nick, abbreviation, msg):
|
|
|
|
if ircmsgs.isAction(msg):
|
|
|
|
return '* %s/%s %s' % (nick, abbreviation, ircmsgs.unAction(msg))
|
|
|
|
else:
|
|
|
|
return '<%s@%s> %s' % (nick, abbreviation, msg.args[1])
|
|
|
|
|
2003-03-26 09:39:50 +01:00
|
|
|
def doPrivmsg(self, irc, msg):
|
|
|
|
callbacks.Privmsg.doPrivmsg(self, irc, msg)
|
|
|
|
if not isinstance(irc, irclib.Irc):
|
|
|
|
irc = irc.getRealIrc()
|
|
|
|
if self.started and ircutils.isChannel(msg.args[0]):
|
|
|
|
channel = msg.args[0]
|
2003-03-27 09:17:51 +01:00
|
|
|
#debug.printf('self.abbreviations = %s' % self.abbreviations)
|
|
|
|
#debug.printf('self.ircs = %s' % self.ircs)
|
|
|
|
#debug.printf('irc = %s' % irc)
|
2003-03-26 09:39:50 +01:00
|
|
|
abbreviation = self.abbreviations[irc]
|
2003-03-27 09:17:51 +01:00
|
|
|
s = self._formatPrivmsg(msg.nick, abbreviation, msg)
|
2003-03-26 09:39:50 +01:00
|
|
|
for otherIrc in self.ircs:
|
2003-03-27 09:17:51 +01:00
|
|
|
#debug.printf('otherIrc = %s' % otherIrc)
|
2003-03-26 09:39:50 +01:00
|
|
|
if otherIrc != irc:
|
2003-03-27 09:17:51 +01:00
|
|
|
#debug.printf('otherIrc != irc')
|
|
|
|
#debug.printf('id(irc) = %s, id(otherIrc) = %s' % \
|
|
|
|
# (id(irc), id(otherIrc)))
|
2003-03-26 09:39:50 +01:00
|
|
|
if channel in otherIrc.state.channels:
|
|
|
|
otherIrc.queueMsg(ircmsgs.privmsg(channel, s))
|
|
|
|
|
|
|
|
def doJoin(self, irc, msg):
|
|
|
|
if self.started:
|
|
|
|
channels = msg.args[0].split(',')
|
|
|
|
abbreviation = self.abbreviations[irc]
|
|
|
|
s = '%s has joined on %s' % (msg.nick, abbreviation)
|
|
|
|
for otherIrc in self.ircs:
|
|
|
|
if otherIrc != irc:
|
|
|
|
for channel in channels:
|
|
|
|
if channel in otherIrc.state.channels:
|
|
|
|
otherIrc.queueMsg(ircmsgs.privmsg(channel, s))
|
|
|
|
|
|
|
|
def doPart(self, irc, msg):
|
|
|
|
if self.started:
|
|
|
|
channels = msg.args[0].split(',')
|
|
|
|
abbreviation = self.abbreviations[irc]
|
|
|
|
s = '%s has left on %s' % (msg.nick, abbreviation)
|
|
|
|
for otherIrc in self.ircs:
|
|
|
|
if otherIrc == irc:
|
|
|
|
continue
|
|
|
|
for channel in channels:
|
|
|
|
if channel in otherIrc.state.channels:
|
|
|
|
otherIrc.queueMsg(ircmsgs.privmsg(channel, s))
|
|
|
|
|
2003-03-27 09:17:51 +01:00
|
|
|
def outFilter(self, irc, msg):
|
|
|
|
if msg.command == 'PRIVMSG':
|
|
|
|
abbreviations = self.abbreviations.values()
|
|
|
|
r = re.compile(r'<([^@]+@(?:%s)>' % '|'.join(abbreviations))
|
|
|
|
if r.match(msg.args[1]):
|
|
|
|
channel = msg.args[0]
|
|
|
|
abbreviation = self.abbreviations[irc]
|
|
|
|
s = self._formatPrivmsg(irc.nick, abbreviation, msg)
|
|
|
|
for otherIrc in self.ircs:
|
|
|
|
if otherIrc != irc:
|
|
|
|
if channel in otherIrc.state.channels:
|
|
|
|
msg = ircmsgs.privmsg(channel, s)
|
|
|
|
|
2003-03-26 09:39:50 +01:00
|
|
|
Class = Relay
|
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|