2005-01-26 08:32:42 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2015-03-26 05:11:36 +01:00
|
|
|
# Copyright (c) 2010,2015 James McCoy
|
2005-01-26 08:32:42 +01: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.
|
|
|
|
###
|
|
|
|
|
|
|
|
import time
|
2013-05-18 14:46:58 +02:00
|
|
|
import functools
|
2005-01-26 08:32:42 +01:00
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.world as world
|
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.registry as registry
|
|
|
|
import supybot.callbacks as callbacks
|
2010-10-16 13:51:27 +02:00
|
|
|
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
|
|
|
_ = PluginInternationalization('Network')
|
2005-01-26 08:32:42 +01:00
|
|
|
|
2005-02-09 08:04:04 +01:00
|
|
|
class Network(callbacks.Plugin):
|
2014-11-30 21:07:41 +01:00
|
|
|
"""Provides network-related commands, such as connecting to multiple networks
|
|
|
|
and checking latency to the server."""
|
2005-01-26 08:32:42 +01:00
|
|
|
_whois = {}
|
|
|
|
_latency = {}
|
|
|
|
def _getIrc(self, network):
|
|
|
|
irc = world.getIrc(network)
|
|
|
|
if irc:
|
|
|
|
return irc
|
|
|
|
else:
|
2014-01-20 15:43:55 +01:00
|
|
|
raise callbacks.Error('I\'m not currently connected to %s.' % network)
|
2005-01-26 08:32:42 +01:00
|
|
|
|
2010-10-16 13:51:27 +02:00
|
|
|
@internationalizeDocstring
|
2005-12-14 05:20:04 +01:00
|
|
|
def connect(self, irc, msg, args, opts, network, server, password):
|
|
|
|
"""[--ssl] <network> [<host[:port]>] [<password>]
|
2005-01-26 08:32:42 +01:00
|
|
|
|
|
|
|
Connects to another network (which will be represented by the name
|
|
|
|
provided in <network>) at <host:port>. If port is not provided, it
|
|
|
|
defaults to 6667, the default port for IRC. If password is
|
2005-12-14 05:20:04 +01:00
|
|
|
provided, it will be sent to the server in a PASS command. If --ssl is
|
|
|
|
provided, an SSL connection will be attempted.
|
2005-01-26 08:32:42 +01:00
|
|
|
"""
|
2014-11-27 03:27:25 +01:00
|
|
|
if '.' in network:
|
|
|
|
irc.error("Network names cannot have a '.' in them. "
|
|
|
|
"Remember, this is the network name, not the actual "
|
|
|
|
"server you plan to connect to.", Raise=True)
|
2005-01-26 08:32:42 +01:00
|
|
|
try:
|
|
|
|
otherIrc = self._getIrc(network)
|
2010-10-16 13:51:27 +02:00
|
|
|
irc.error(_('I\'m already connected to %s.') % network)
|
2005-05-18 16:55:55 +02:00
|
|
|
return # We've gotta return here. This is ugly code, but I'm not
|
|
|
|
# quite sure what to do about it.
|
2005-01-26 08:32:42 +01:00
|
|
|
except callbacks.Error:
|
|
|
|
pass
|
2005-12-29 03:12:32 +01:00
|
|
|
ssl = False
|
|
|
|
for (opt, arg) in opts:
|
|
|
|
if opt == 'ssl':
|
|
|
|
ssl = True
|
2005-01-26 08:32:42 +01:00
|
|
|
if server:
|
|
|
|
if ':' in server:
|
|
|
|
(server, port) = server.split(':')
|
|
|
|
port = int(port)
|
|
|
|
else:
|
|
|
|
port = 6667
|
|
|
|
serverPort = (server, port)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
serverPort = conf.supybot.networks.get(network).servers()[0]
|
|
|
|
except (registry.NonExistentRegistryEntry, IndexError):
|
2010-10-16 13:51:27 +02:00
|
|
|
irc.error(_('A server must be provided if the network is not '
|
|
|
|
'already registered.'))
|
2005-01-26 08:32:42 +01:00
|
|
|
return
|
|
|
|
Owner = irc.getCallback('Owner')
|
|
|
|
newIrc = Owner._connect(network, serverPort=serverPort,
|
2005-12-14 05:20:04 +01:00
|
|
|
password=password, ssl=ssl)
|
2005-01-26 08:32:42 +01:00
|
|
|
conf.supybot.networks().add(network)
|
|
|
|
assert newIrc.callbacks is irc.callbacks, 'callbacks list is different'
|
2010-10-16 13:51:27 +02:00
|
|
|
irc.replySuccess(_('Connection to %s initiated.') % network)
|
2005-12-14 05:20:04 +01:00
|
|
|
connect = wrap(connect, ['owner', getopts({'ssl': ''}), 'something',
|
|
|
|
additional('something'),
|
2005-01-26 08:32:42 +01:00
|
|
|
additional('something', '')])
|
|
|
|
|
2010-10-16 13:51:27 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:32:42 +01:00
|
|
|
def disconnect(self, irc, msg, args, otherIrc, quitMsg):
|
|
|
|
"""[<network>] [<quit message>]
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
quitMsg = quitMsg or conf.supybot.plugins.Owner.quitMsg() or msg.nick
|
|
|
|
otherIrc.queueMsg(ircmsgs.quit(quitMsg))
|
|
|
|
otherIrc.die()
|
|
|
|
conf.supybot.networks().discard(otherIrc.network)
|
|
|
|
if otherIrc != irc:
|
2010-10-16 13:51:27 +02:00
|
|
|
irc.replySuccess(_('Disconnection to %s initiated.') %
|
2005-01-26 08:32:42 +01:00
|
|
|
otherIrc.network)
|
|
|
|
disconnect = wrap(disconnect, ['owner', 'networkIrc', additional('text')])
|
|
|
|
|
2010-10-16 13:51:27 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:32:42 +01:00
|
|
|
def reconnect(self, irc, msg, args, otherIrc, quitMsg):
|
|
|
|
"""[<network>] [<quit message>]
|
|
|
|
|
|
|
|
Disconnects and then reconnects to <network>. If no network is given,
|
|
|
|
disconnects and then reconnects to the network the command was given
|
|
|
|
on. If no quit message is given, uses the configured one
|
|
|
|
(supybot.plugins.Owner.quitMsg) or the nick of the person giving the
|
|
|
|
command.
|
|
|
|
"""
|
|
|
|
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')])
|
|
|
|
|
2010-10-16 13:51:27 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:32:42 +01:00
|
|
|
def command(self, irc, msg, args, otherIrc, commandAndArgs):
|
|
|
|
"""<network> <command> [<arg> ...]
|
|
|
|
|
|
|
|
Gives the bot <command> (with its associated <arg>s) on <network>.
|
|
|
|
"""
|
|
|
|
self.Proxy(otherIrc, msg, commandAndArgs)
|
|
|
|
command = wrap(command, ['admin', ('networkIrc', True), many('something')])
|
2014-02-06 01:06:01 +01:00
|
|
|
|
2014-02-06 00:02:20 +01:00
|
|
|
def cmdall(self, irc, msg, args, commandAndArgs):
|
|
|
|
"""<command> <args>...
|
|
|
|
|
|
|
|
Perform <command> (with its associated <arg>s) on all networks.
|
|
|
|
"""
|
|
|
|
ircs = world.ircs
|
|
|
|
for ircd in ircs:
|
|
|
|
self.Proxy(ircd, msg, commandAndArgs)
|
2014-02-06 01:02:43 +01:00
|
|
|
cmdall = wrap(cmdall, ['admin', many('something')])
|
2014-02-06 01:06:01 +01:00
|
|
|
|
2005-01-26 08:32:42 +01:00
|
|
|
###
|
|
|
|
# whois command-related stuff.
|
|
|
|
###
|
|
|
|
def do311(self, irc, msg):
|
|
|
|
nick = ircutils.toLower(msg.args[1])
|
|
|
|
if (irc, nick) not in self._whois:
|
|
|
|
return
|
2014-05-20 12:49:23 +02:00
|
|
|
elif msg.command == '319':
|
|
|
|
if '319' not in self._whois[(irc, nick)][2]:
|
|
|
|
self._whois[(irc, nick)][2][msg.command] = []
|
|
|
|
self._whois[(irc, nick)][2][msg.command].append(msg)
|
2005-01-26 08:32:42 +01:00
|
|
|
else:
|
2013-05-18 14:46:58 +02:00
|
|
|
self._whois[(irc, nick)][2][msg.command] = msg
|
2005-01-26 08:32:42 +01:00
|
|
|
|
|
|
|
# These are all sent by a WHOIS response.
|
|
|
|
do301 = do311
|
|
|
|
do312 = do311
|
2013-05-18 14:46:58 +02:00
|
|
|
do314 = do311
|
2005-01-26 08:32:42 +01:00
|
|
|
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:
|
|
|
|
return
|
2013-05-18 14:46:58 +02:00
|
|
|
(replyIrc, replyMsg, d, command) = self._whois[(irc, loweredNick)]
|
2015-03-26 05:11:36 +01:00
|
|
|
d['318'] = msg
|
|
|
|
s = ircutils.formatWhois(irc, d, caller=replyMsg.nick,
|
2015-05-15 14:41:08 +02:00
|
|
|
channel=replyMsg.args[0],
|
|
|
|
command=command)
|
2005-01-26 08:32:42 +01:00
|
|
|
replyIrc.reply(s)
|
|
|
|
del self._whois[(irc, loweredNick)]
|
2013-05-18 14:46:58 +02:00
|
|
|
do369 = do318
|
2005-01-26 08:32:42 +01:00
|
|
|
|
|
|
|
def do402(self, irc, msg):
|
|
|
|
nick = msg.args[1]
|
|
|
|
loweredNick = ircutils.toLower(nick)
|
|
|
|
if (irc, loweredNick) not in self._whois:
|
|
|
|
return
|
2013-05-18 14:46:58 +02:00
|
|
|
(replyIrc, replyMsg, d, command) = self._whois[(irc, loweredNick)]
|
2005-01-26 08:32:42 +01:00
|
|
|
del self._whois[(irc, loweredNick)]
|
2013-05-18 14:46:58 +02:00
|
|
|
if command == 'whois':
|
2014-12-10 03:38:21 +01:00
|
|
|
template = _('There is no user %s on %s.')
|
2013-05-18 14:46:58 +02:00
|
|
|
else:
|
2014-12-10 03:38:21 +01:00
|
|
|
template = _('There was no user %s on %s.')
|
2013-05-18 14:46:58 +02:00
|
|
|
s = template % (nick, irc.network)
|
2005-01-26 08:32:42 +01:00
|
|
|
replyIrc.reply(s)
|
|
|
|
do401 = do402
|
2013-05-18 14:46:58 +02:00
|
|
|
do406 = do402
|
2005-01-26 08:32:42 +01:00
|
|
|
|
2010-10-16 13:51:27 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:32:42 +01:00
|
|
|
def whois(self, irc, msg, args, otherIrc, nick):
|
|
|
|
"""[<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.
|
|
|
|
nick = ircutils.toLower(nick)
|
|
|
|
otherIrc.queueMsg(ircmsgs.whois(nick, nick))
|
2013-05-18 14:46:58 +02:00
|
|
|
self._whois[(otherIrc, nick)] = (irc, msg, {}, 'whois')
|
2005-01-26 08:32:42 +01:00
|
|
|
whois = wrap(whois, ['networkIrc', 'nick'])
|
2013-05-18 14:46:58 +02:00
|
|
|
|
|
|
|
@internationalizeDocstring
|
|
|
|
def whowas(self, irc, msg, args, otherIrc, nick):
|
|
|
|
"""[<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.
|
|
|
|
nick = ircutils.toLower(nick)
|
|
|
|
otherIrc.queueMsg(ircmsgs.whowas(nick, nick))
|
|
|
|
self._whois[(otherIrc, nick)] = (irc, msg, {}, 'whowas')
|
|
|
|
whowas = wrap(whowas, ['networkIrc', 'nick'])
|
2005-01-26 08:32:42 +01:00
|
|
|
|
2010-10-16 13:51:27 +02:00
|
|
|
@internationalizeDocstring
|
2014-12-18 03:29:53 +01:00
|
|
|
def networks(self, irc, msg, args, opts):
|
|
|
|
"""[--all]
|
2005-01-26 08:32:42 +01:00
|
|
|
|
|
|
|
Returns the networks to which the bot is currently connected.
|
2014-12-18 03:29:53 +01:00
|
|
|
If --all is given, also includes networks known by the bot,
|
|
|
|
but not connected to.
|
2005-01-26 08:32:42 +01:00
|
|
|
"""
|
2014-12-18 03:29:53 +01:00
|
|
|
opts = dict(opts)
|
2005-01-26 08:32:42 +01:00
|
|
|
L = ['%s: %s' % (ircd.network, ircd.server) for ircd in world.ircs]
|
2014-12-18 03:29:53 +01:00
|
|
|
if 'all' in opts:
|
|
|
|
for net in conf.supybot.networks._children.keys():
|
|
|
|
if net not in [ircd.network for ircd in world.ircs]:
|
|
|
|
L.append('%s: (%s)' % (net, _('disconnected')))
|
2005-02-15 14:57:57 +01:00
|
|
|
utils.sortBy(str.lower, L)
|
2005-01-31 15:52:27 +01:00
|
|
|
irc.reply(format('%L', L))
|
2014-12-18 03:29:53 +01:00
|
|
|
networks = wrap(networks, [getopts({'all': ''})])
|
2005-01-26 08:32:42 +01:00
|
|
|
|
|
|
|
def doPong(self, irc, msg):
|
|
|
|
now = time.time()
|
|
|
|
if irc in self._latency:
|
|
|
|
(replyIrc, when) = self._latency.pop(irc)
|
2010-10-16 13:51:27 +02:00
|
|
|
replyIrc.reply(_('%.2f seconds.') % (now-when))
|
2005-01-26 08:32:42 +01:00
|
|
|
|
2010-10-16 13:51:27 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:32:42 +01:00
|
|
|
def latency(self, irc, msg, args, otherIrc):
|
|
|
|
"""[<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.
|
|
|
|
"""
|
2010-10-16 13:51:27 +02:00
|
|
|
otherIrc.queueMsg(ircmsgs.ping(_('Latency check (from %s).') %
|
|
|
|
msg.nick))
|
2005-01-26 08:32:42 +01:00
|
|
|
self._latency[otherIrc] = (irc, time.time())
|
|
|
|
irc.noReply()
|
|
|
|
latency = wrap(latency, ['networkIrc'])
|
|
|
|
|
2010-10-16 13:51:27 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:32:42 +01:00
|
|
|
def driver(self, irc, msg, args, otherIrc):
|
|
|
|
"""[<network>]
|
|
|
|
|
|
|
|
Returns the current network driver for <network>. <network> is only
|
|
|
|
necessary if the message isn't sent on the network to which this
|
|
|
|
command is to apply.
|
|
|
|
"""
|
|
|
|
irc.reply(otherIrc.driver.__class__.__module__[8:])
|
|
|
|
driver = wrap(driver, ['networkIrc'])
|
|
|
|
|
2012-06-30 02:35:40 +02:00
|
|
|
@internationalizeDocstring
|
|
|
|
def uptime(self, irc, msg, args, otherIrc):
|
|
|
|
"""[<network>]
|
|
|
|
|
|
|
|
Returns the time duration since the connection was established.
|
|
|
|
"""
|
|
|
|
network = otherIrc.network
|
|
|
|
now = time.time()
|
|
|
|
started = otherIrc.startedAt
|
|
|
|
irc.reply(_("I've been connected to %s for %s.") %
|
|
|
|
(network, utils.timeElapsed(now - started)))
|
|
|
|
uptime = wrap(uptime, ['networkIrc'])
|
2005-01-26 08:32:42 +01:00
|
|
|
|
|
|
|
Class = Network
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|