2003-10-01 13:12:06 +02:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-10-01 13:12:06 +02:00
|
|
|
# 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.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
2004-09-12 07:46:18 +02:00
|
|
|
Includes commands for connecting, disconnecting, and reconnecting to multiple
|
|
|
|
networks, as well as several other utility functions related to IRC networks.
|
2003-10-01 13:12:06 +02:00
|
|
|
"""
|
|
|
|
|
2004-09-12 07:46:18 +02:00
|
|
|
import supybot
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
2004-09-12 07:46:18 +02:00
|
|
|
__author__ = supybot.authors.jemfinch
|
2003-11-25 09:23:47 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-10-01 13:12:06 +02:00
|
|
|
|
2004-09-12 07:46:18 +02:00
|
|
|
import time
|
2003-10-01 13:12:06 +02:00
|
|
|
|
2004-09-12 07:46:18 +02:00
|
|
|
import supybot.conf as conf
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.utils as utils
|
2004-09-12 07:46:18 +02:00
|
|
|
import supybot.world as world
|
2004-12-07 07:40:57 +01:00
|
|
|
from supybot.commands import *
|
2004-09-12 07:46:18 +02:00
|
|
|
import supybot.ircmsgs as ircmsgs
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.privmsgs as privmsgs
|
2004-09-12 07:46:18 +02:00
|
|
|
import supybot.registry as registry
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.callbacks as callbacks
|
2003-10-01 13:12:06 +02:00
|
|
|
|
|
|
|
class Network(callbacks.Privmsg):
|
2004-09-12 07:46:18 +02:00
|
|
|
_whois = {}
|
2004-09-13 02:29:39 +02:00
|
|
|
_latency = {}
|
2004-09-12 07:46:18 +02:00
|
|
|
def _getIrc(self, network):
|
2004-12-16 09:10:46 +01:00
|
|
|
irc = world.getIrc(network)
|
|
|
|
if irc:
|
|
|
|
return irc
|
|
|
|
else:
|
|
|
|
raise callbacks.Error, \
|
|
|
|
'I\'m not currently connected to %s.' % network
|
2003-10-01 13:12:06 +02:00
|
|
|
|
2004-12-17 20:51:09 +01:00
|
|
|
def connect(self, irc, msg, args, network, server, password):
|
|
|
|
"""<network> [<host[:port]>] [<password>]
|
2004-09-12 07:46:18 +02:00
|
|
|
|
2004-10-08 20:51:50 +02:00
|
|
|
Connects to another network (which will be represented by the name
|
|
|
|
provided in <network>) at <host:port>. If port is not provided, it
|
2004-12-17 20:51:09 +01:00
|
|
|
defaults to 6667, the default port for IRC. If password is
|
|
|
|
provided, it will be sent to the server in a PASS command.
|
2003-10-01 13:12:06 +02:00
|
|
|
"""
|
2004-09-12 07:46:18 +02:00
|
|
|
try:
|
|
|
|
otherIrc = self._getIrc(network)
|
|
|
|
irc.error('I\'m already connected to %s.' % network, Raise=True)
|
2004-12-07 07:40:57 +01:00
|
|
|
except callbacks.Error:
|
2004-09-12 07:46:18 +02:00
|
|
|
pass
|
|
|
|
if server:
|
|
|
|
if ':' in server:
|
|
|
|
(server, port) = server.split(':')
|
|
|
|
port = int(port)
|
2003-10-01 13:12:06 +02:00
|
|
|
else:
|
2004-09-12 07:46:18 +02:00
|
|
|
port = 6667
|
|
|
|
serverPort = (server, port)
|
2003-10-01 13:12:06 +02:00
|
|
|
else:
|
|
|
|
try:
|
2004-09-12 07:46:18 +02:00
|
|
|
serverPort = conf.supybot.networks.get(network).servers()[0]
|
|
|
|
except (registry.NonExistentRegistryEntry, IndexError):
|
|
|
|
irc.error('A server must be provided if the network is not '
|
|
|
|
'already registered.')
|
|
|
|
return
|
|
|
|
Owner = irc.getCallback('Owner')
|
|
|
|
newIrc = Owner._connect(network, serverPort=serverPort)
|
|
|
|
conf.supybot.networks().add(network)
|
|
|
|
assert newIrc.callbacks is irc.callbacks, 'callbacks list is different'
|
|
|
|
irc.replySuccess('Connection to %s initiated.' % network)
|
2004-12-17 20:51:09 +01:00
|
|
|
connect = wrap(connect, ['owner', 'something', additional('something'),
|
|
|
|
additional('something', '')])
|
2004-09-12 07:46:18 +02:00
|
|
|
|
2004-12-07 07:40:57 +01:00
|
|
|
def disconnect(self, irc, msg, args, otherIrc, quitMsg):
|
2004-09-12 07:46:18 +02:00
|
|
|
"""[<network>] [<quit message>]
|
2003-10-01 13:12:06 +02:00
|
|
|
|
2004-09-12 07:46:18 +02:00
|
|
|
Disconnects from the network represented by the network <network>.
|
|
|
|
If <quit message> is given, quits the network with the given quit
|
|
|
|
message. <network> is only necessary if the network is different
|
|
|
|
from the network the command is sent on.
|
2003-10-01 13:12:06 +02:00
|
|
|
"""
|
2004-12-10 07:15:35 +01:00
|
|
|
quitMsg = quitMsg or conf.supybot.plugins.Owner.quitMsg() or msg.nick
|
2004-09-12 07:46:18 +02:00
|
|
|
otherIrc.queueMsg(ircmsgs.quit(quitMsg))
|
|
|
|
otherIrc.die()
|
2004-12-10 16:07:37 +01:00
|
|
|
conf.supybot.networks().discard(otherIrc.network)
|
2004-12-30 19:07:33 +01:00
|
|
|
irc.replySuccess('Disconnection to %s initiated.' % otherIrc.network)
|
2004-12-07 07:40:57 +01:00
|
|
|
disconnect = wrap(disconnect, ['owner', 'networkIrc', additional('text')])
|
2004-09-12 07:46:18 +02:00
|
|
|
|
2004-12-07 07:40:57 +01:00
|
|
|
def reconnect(self, irc, msg, args, otherIrc, quitMsg):
|
|
|
|
"""[<network>] [<quit message>]
|
2004-09-12 07:46:18 +02:00
|
|
|
|
|
|
|
Disconnects and then reconnects to <network>. If no network is given,
|
|
|
|
disconnects and then reconnects to the network the command was given
|
2004-12-07 07:40:57 +01:00
|
|
|
on. If no quit message is given, uses the configured one
|
|
|
|
(supybot.plugins.Owner.quitMsg) or the nick of the person giving the
|
|
|
|
command.
|
2004-09-12 07:46:18 +02:00
|
|
|
"""
|
2004-12-07 07:40:57 +01:00
|
|
|
quitMsg = quitMsg or conf.supybot.plugins.Owner.quitMsg() or msg.nick
|
|
|
|
otherIrc.queueMsg(ircmsgs.quit(quitMsg))
|
|
|
|
if otherIrc != irc:
|
|
|
|
# No need to reply if we're reconnecting ourselves.
|
|
|
|
irc.replySuccess()
|
|
|
|
reconnect = wrap(reconnect, ['owner', 'networkIrc', additional('text')])
|
2004-09-12 07:46:18 +02:00
|
|
|
|
2004-12-07 08:12:47 +01:00
|
|
|
def command(self, irc, msg, args, otherIrc, commandAndArgs):
|
2004-09-12 07:46:18 +02:00
|
|
|
"""<network> <command> [<arg> ...]
|
|
|
|
|
|
|
|
Gives the bot <command> (with its associated <arg>s) on <network>.
|
|
|
|
"""
|
2004-12-07 08:12:47 +01:00
|
|
|
self.Proxy(otherIrc, msg, commandAndArgs)
|
|
|
|
command = wrap(command, ['admin', ('networkIrc', True), many('something')])
|
2004-09-12 07:46:18 +02:00
|
|
|
|
|
|
|
###
|
|
|
|
# whois command-related stuff.
|
|
|
|
###
|
|
|
|
def do311(self, irc, msg):
|
|
|
|
nick = ircutils.toLower(msg.args[1])
|
|
|
|
if (irc, nick) not in self._whois:
|
2003-10-01 13:12:06 +02:00
|
|
|
return
|
2004-02-17 09:08:18 +01:00
|
|
|
else:
|
2004-09-12 07:46:18 +02:00
|
|
|
self._whois[(irc, nick)][-1][msg.command] = msg
|
|
|
|
|
|
|
|
# These are all sent by a WHOIS response.
|
|
|
|
do301 = do311
|
|
|
|
do312 = do311
|
|
|
|
do317 = do311
|
|
|
|
do319 = do311
|
|
|
|
do320 = do311
|
|
|
|
|
|
|
|
def do318(self, irc, msg):
|
|
|
|
nick = msg.args[1]
|
|
|
|
loweredNick = ircutils.toLower(nick)
|
|
|
|
if (irc, loweredNick) not in self._whois:
|
2004-02-24 22:52:53 +01:00
|
|
|
return
|
2004-09-12 07:46:18 +02:00
|
|
|
(replyIrc, replyMsg, d) = self._whois[(irc, loweredNick)]
|
|
|
|
hostmask = '@'.join(d['311'].args[2:4])
|
|
|
|
user = d['311'].args[-1]
|
|
|
|
if '319' in d:
|
|
|
|
channels = d['319'].args[-1].split()
|
|
|
|
ops = []
|
|
|
|
voices = []
|
|
|
|
normal = []
|
|
|
|
halfops = []
|
|
|
|
for channel in channels:
|
|
|
|
if channel.startswith('@'):
|
|
|
|
ops.append(channel[1:])
|
|
|
|
elif channel.startswith('%'):
|
|
|
|
halfops.append(channel[1:])
|
|
|
|
elif channel.startswith('+'):
|
|
|
|
voices.append(channel[1:])
|
|
|
|
else:
|
|
|
|
normal.append(channel)
|
|
|
|
L = []
|
|
|
|
if ops:
|
|
|
|
L.append('is an op on %s' % utils.commaAndify(ops))
|
|
|
|
if halfops:
|
|
|
|
L.append('is a halfop on %s' % utils.commaAndify(halfops))
|
|
|
|
if voices:
|
|
|
|
L.append('is voiced on %s' % utils.commaAndify(voices))
|
|
|
|
if normal:
|
|
|
|
if L:
|
|
|
|
L.append('is also on %s' % utils.commaAndify(normal))
|
|
|
|
else:
|
|
|
|
L.append('is on %s' % utils.commaAndify(normal))
|
|
|
|
else:
|
|
|
|
L = ['isn\'t on any non-secret channels']
|
|
|
|
channels = utils.commaAndify(L)
|
|
|
|
if '317' in d:
|
|
|
|
idle = utils.timeElapsed(d['317'].args[2])
|
2004-12-10 07:21:53 +01:00
|
|
|
signon = time.strftime(conf.supybot.reply.format.time(),
|
2004-09-12 07:46:18 +02:00
|
|
|
time.localtime(float(d['317'].args[3])))
|
|
|
|
else:
|
|
|
|
idle = '<unknown>'
|
|
|
|
signon = '<unknown>'
|
|
|
|
if '312' in d:
|
|
|
|
server = d['312'].args[2]
|
|
|
|
else:
|
|
|
|
server = '<unknown>'
|
|
|
|
if '301' in d:
|
|
|
|
away = ' %s is away: %s.' % (nick, d['301'].args[2])
|
|
|
|
else:
|
|
|
|
away = ''
|
|
|
|
if '320' in d:
|
|
|
|
if d['320'].args[2]:
|
|
|
|
identify = ' identified'
|
|
|
|
else:
|
|
|
|
identify = ''
|
|
|
|
else:
|
|
|
|
identify = ''
|
|
|
|
s = '%s (%s) has been%s on server %s since %s (idle for %s) and ' \
|
|
|
|
'%s.%s' % (user, hostmask, identify, server, signon, idle,
|
|
|
|
channels, away)
|
|
|
|
replyIrc.reply(s)
|
|
|
|
del self._whois[(irc, loweredNick)]
|
|
|
|
|
|
|
|
def do402(self, irc, msg):
|
|
|
|
nick = msg.args[1]
|
|
|
|
loweredNick = ircutils.toLower(nick)
|
|
|
|
if (irc, loweredNick) not in self._whois:
|
2004-02-24 22:52:53 +01:00
|
|
|
return
|
2004-09-12 07:46:18 +02:00
|
|
|
(replyIrc, replyMsg, d) = self._whois[(irc, loweredNick)]
|
|
|
|
del self._whois[(irc, loweredNick)]
|
2004-12-21 02:03:31 +01:00
|
|
|
s = 'There is no %s on %s.' % (nick, irc.network)
|
2004-09-12 07:46:18 +02:00
|
|
|
replyIrc.reply(s)
|
|
|
|
do401 = do402
|
|
|
|
|
2004-12-07 07:40:57 +01:00
|
|
|
def whois(self, irc, msg, args, otherIrc, nick):
|
2004-09-12 07:46:18 +02:00
|
|
|
"""[<network>] <nick>
|
|
|
|
|
|
|
|
Returns the WHOIS response <network> gives for <nick>. <network> is
|
|
|
|
only necessary if the network is different than the network the command
|
|
|
|
is sent on.
|
|
|
|
"""
|
|
|
|
# The double nick here is necessary because single-nick WHOIS only works
|
|
|
|
# if the nick is on the same server (*not* the same network) as the user
|
|
|
|
# giving the command. Yeah, it made me say wtf too.
|
2004-12-20 07:05:17 +01:00
|
|
|
nick = ircutils.toLower(nick)
|
2004-09-12 07:46:18 +02:00
|
|
|
otherIrc.queueMsg(ircmsgs.whois(nick, nick))
|
|
|
|
self._whois[(otherIrc, nick)] = (irc, msg, {})
|
2004-12-07 07:40:57 +01:00
|
|
|
whois = wrap(whois, ['networkIrc', 'nick'])
|
2004-09-12 07:46:18 +02:00
|
|
|
|
|
|
|
def networks(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the networks to which the bot is currently connected.
|
|
|
|
"""
|
|
|
|
L = ['%s: %s' % (ircd.network, ircd.server) for ircd in world.ircs]
|
|
|
|
utils.sortBy(str.lower, L)
|
|
|
|
irc.reply(utils.commaAndify(L))
|
2004-12-07 07:40:57 +01:00
|
|
|
networks = wrap(networks)
|
2004-09-13 02:29:39 +02:00
|
|
|
|
|
|
|
def doPong(self, irc, msg):
|
|
|
|
now = time.time()
|
|
|
|
if irc in self._latency:
|
|
|
|
(replyIrc, when) = self._latency.pop(irc)
|
2004-09-13 15:45:19 +02:00
|
|
|
replyIrc.reply('%.2f seconds.' % (now-when))
|
2004-09-13 02:29:39 +02:00
|
|
|
|
2004-12-07 07:40:57 +01:00
|
|
|
def latency(self, irc, msg, args, otherIrc):
|
2004-09-13 02:29:39 +02:00
|
|
|
"""[<network>]
|
|
|
|
|
|
|
|
Returns the current latency to <network>. <network> is only necessary
|
|
|
|
if the message isn't sent on the network to which this command is to
|
|
|
|
apply.
|
|
|
|
"""
|
2004-09-13 15:45:19 +02:00
|
|
|
otherIrc.queueMsg(ircmsgs.ping('Latency check (from %s).' % msg.nick))
|
2004-09-13 02:29:39 +02:00
|
|
|
self._latency[otherIrc] = (irc, time.time())
|
2004-10-03 10:57:45 +02:00
|
|
|
irc.noReply()
|
2004-12-07 07:40:57 +01:00
|
|
|
latency = wrap(latency, ['networkIrc'])
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2003-10-01 13:12:06 +02:00
|
|
|
|
|
|
|
Class = Network
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|