From e112e32d714aba6b37b2f7390200fa24de657748 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Wed, 26 Mar 2003 08:39:50 +0000 Subject: [PATCH] Still buggy, but more featureful --- plugins/Relay.py | 133 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 1 deletion(-) diff --git a/plugins/Relay.py b/plugins/Relay.py index 8600f802c..a52ff9aa9 100644 --- a/plugins/Relay.py +++ b/plugins/Relay.py @@ -29,14 +29,145 @@ # POSSIBILITY OF SUCH DAMAGE. ### +""" +Handles relaying between networks. + +Commands include: + startrelay + relayconnect + relaydisconnect + relayjoin + relaypart +""" + from baseplugin import * +import ircdb +import debug +import irclib +import ircmsgs +import ircutils import privmsgs import callbacks +import asyncoreDrivers +class IdDict(dict): + def __setitem__(self, key, value): + dict.__setitem__(self, id(key), value) + + def __getitem__(self, key): + return dict.__getitem__(self, id(key)) + + def __contains__(self, key): + return dict.__contains__(self, id(key)) class Relay(callbacks.Privmsg): + def __init__(self): + callbacks.Privmsg.__init__(self) + self.ircs = [] + self.started = False + self.abbreviations = {} #IdDict({}) + + def startrelay(self, irc, msg, args): + "" + 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') + def relayconnect(self, irc, msg, args): - " (port defaults to 6667)" + " (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): + "" pass + + def relayjoin(self, irc, msg, args): + "" + 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): + "" + 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') + + 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] + debug.printf('self.abbreviations = %s' % self.abbreviations) + debug.printf('self.ircs = %s' % self.ircs) + debug.printf('irc = %s' % irc) + abbreviation = self.abbreviations[irc] + s = '<%s@%s> %s' % (msg.nick, abbreviation, msg.args[1]) + for otherIrc in self.ircs: + debug.printf('otherIrc = %s' % otherIrc) + if otherIrc != irc: + debug.printf('otherIrc != irc') + debug.printf('id(irc) = %s, id(otherIrc) = %s' % \ + (id(irc), id(otherIrc))) + 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)) + +Class = Relay + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: