Updated to allow different networks.

This commit is contained in:
Jeremy Fincher 2004-07-20 05:57:58 +00:00
parent b88dd0f649
commit aad1d2dc41
4 changed files with 102 additions and 60 deletions

View File

@ -123,18 +123,12 @@ if __name__ == '__main__':
parser.add_option('-n', '--nick', action='store', parser.add_option('-n', '--nick', action='store',
dest='nick', default='', dest='nick', default='',
help='nick the bot should use') help='nick the bot should use')
parser.add_option('-s', '--server', action='store',
dest='server', default='',
help='server to connect to')
parser.add_option('-u', '--user', action='store', parser.add_option('-u', '--user', action='store',
dest='user', default='', dest='user', default='',
help='full username the bot should use') help='full username the bot should use')
parser.add_option('-i', '--ident', action='store', parser.add_option('-i', '--ident', action='store',
dest='ident', default='', dest='ident', default='',
help='ident the bot should use') help='ident the bot should use')
parser.add_option('-p', '--password', action='store',
dest='password', default='',
help='server password the bot should use')
parser.add_option('-d', '--daemon', action='store_true', parser.add_option('-d', '--daemon', action='store_true',
dest='daemon', dest='daemon',
help='Determines whether the bot will daemonize. ' help='Determines whether the bot will daemonize. '
@ -209,9 +203,12 @@ if __name__ == '__main__':
nick = options.nick or conf.supybot.nick() nick = options.nick or conf.supybot.nick()
user = options.user or conf.supybot.user() user = options.user or conf.supybot.user()
ident = options.ident or conf.supybot.ident() ident = options.ident or conf.supybot.ident()
password = options.password or conf.supybot.password()
server = options.server or conf.supybot.server() # We could add an option for this, but we'll wait until it's requested.
defaultNetwork = conf.supybot.networks.default()
network = conf.supybot.networks.get(defaultNetwork)
server = network.server()
password = network.password()
if ':' in server: if ':' in server:
serverAndPort = server.split(':', 1) serverAndPort = server.split(':', 1)
serverAndPort[1] = int(serverAndPort[1]) serverAndPort[1] = int(serverAndPort[1])
@ -220,6 +217,7 @@ if __name__ == '__main__':
server = (server, 6667) server = (server, 6667)
if options.optimize: if options.optimize:
# This doesn't work anymore.
__builtins__.__debug__ = False __builtins__.__debug__ = False
if options.optimize > 1: if options.optimize > 1:
try: try:
@ -253,7 +251,8 @@ if __name__ == '__main__':
import callbacks import callbacks
import Owner import Owner
irc = irclib.Irc(nick, user=user, ident=ident, password=password) irc = irclib.Irc(nick, user=user, ident=ident,
network=defaultNetwork, password=password)
callback = Owner.Class() callback = Owner.Class()
irc.addCallback(callback) irc.addCallback(callback)
driver = drivers.newDriver(server, irc) driver = drivers.newDriver(server, irc)

View File

@ -257,13 +257,26 @@ def main():
output("""Now we're going to ask you things that actually relate to the output("""Now we're going to ask you things that actually relate to the
bot you'll be running.""") bot you'll be running.""")
# conf.supybot.server network = None
# Force the user into specifying a server if he didn't have one already while not network:
try: output("""First, we need to know the name of the network you'd like to
defaultServer = registry._cache['supybot.server'] connect to. Not the server host, mind you, but the name of the
defaultServer = utils.safeEval(defaultServer) network. If you plan to connect to irc.freenode.net, for instance, you
defaultServer = defaultServer.split(':')[0] should answer this question with 'freenode' (without the quotes).""")
except KeyError: network = something('What IRC network will you be connecting to?')
if '.' in network:
output("""There shouldn't be a '.' in the network name. Remember,
this is the network name, not the actual server you plan to connect
to.""")
network = None
elif not isValidRegistryName:
output("""That's not a valid name for one reason or another. Please
pick a simpler name, one more likely to be valid.""")
network = None
conf.supybot.networks.default.set(network)
network = conf.registerNetwork(network)
defaultServer = None defaultServer = None
server = None server = None
while not server: while not server:
@ -274,8 +287,9 @@ def main():
ip = socket.gethostbyname(serverString) ip = socket.gethostbyname(serverString)
except: except:
output("""Sorry, I couldn't find that server. Perhaps you output("""Sorry, I couldn't find that server. Perhaps you
misspelled it?""") misspelled it? Also, be sure not to put the port in the server's
continue name -- we'll ask you about that later.""")
output("""Found %s (%s).""" % (serverString, ip)) output("""Found %s (%s).""" % (serverString, ip))
output("""IRC Servers almost always accept connections on port output("""IRC Servers almost always accept connections on port
6667. They can, however, accept connections anywhere their admin 6667. They can, however, accept connections anywhere their admin
@ -294,8 +308,8 @@ def main():
port = 0 port = 0
else: else:
port = 6667 port = 6667
server = ':'.join(map(str, [serverString, port])) server = ':'.join([serverString, str(port)])
conf.supybot.server.set(server) network.server.setValue(server)
# conf.supybot.nick # conf.supybot.nick
# Force the user into specifying a nick if he didn't have one already # Force the user into specifying a nick if he didn't have one already

View File

@ -51,10 +51,16 @@ _pluginsDir = os.path.join(installDir, 'plugins')
### ###
version ='0.77.2+cvs' version ='0.77.2+cvs'
###
# *** The following variables are affected by command-line options. They are
# not registry variables for a specific reason. Do *not* change these to
# registry variables without first consulting people smarter than yourself.
###
### ###
# daemonized: This determines whether or not the bot has been daemonized # daemonized: This determines whether or not the bot has been daemonized
# (i.e., set to run in the background). Obviously, this defaults # (i.e., set to run in the background). Obviously, this defaults
# to False. # to False. A command-line option for obvious reasons.
### ###
daemonized = False daemonized = False
@ -80,14 +86,14 @@ supybot = registry.Group()
supybot.setName('supybot') supybot.setName('supybot')
def registerGroup(Group, name, group=None): def registerGroup(Group, name, group=None):
Group.register(name, group) return Group.register(name, group)
def registerGlobalValue(group, name, value): def registerGlobalValue(group, name, value):
group.register(name, value) return group.register(name, value)
def registerChannelValue(group, name, value): def registerChannelValue(group, name, value):
value.supplyDefault = True value.supplyDefault = True
group.register(name, value) return group.register(name, value)
def registerPlugin(name, currentValue=None): def registerPlugin(name, currentValue=None):
registerGlobalValue(supybot.plugins, name, registerGlobalValue(supybot.plugins, name,
@ -95,7 +101,7 @@ def registerPlugin(name, currentValue=None):
default.""", showDefault=False)) default.""", showDefault=False))
if currentValue is not None: if currentValue is not None:
supybot.plugins.get(name).setValue(currentValue) supybot.plugins.get(name).setValue(currentValue)
registerGroup(users.plugins, name) return registerGroup(users.plugins, name)
### ###
# The user info registry. # The user info registry.
@ -130,8 +136,8 @@ class ValidChannel(registry.String):
else: else:
registry.String.setValue(self, v) registry.String.setValue(self, v)
supybot.register('nick', ValidNick('supybot', registerGlobalValue(supybot, 'nick',
"""Determines the bot's nick.""")) ValidNick('supybot', """Determines the bot's nick."""))
registerGlobalValue(supybot, 'ident', registerGlobalValue(supybot, 'ident',
ValidNick('supybot', """Determines the bot's ident string, if the server ValidNick('supybot', """Determines the bot's ident string, if the server
@ -142,11 +148,30 @@ registerGlobalValue(supybot, 'user',
sends to the server.""")) sends to the server."""))
# TODO: Make this check for validity. # TODO: Make this check for validity.
supybot.register('server', registry.String('irc.freenode.net', """Determines registerGroup(supybot, 'networks')
what server the bot connects to.""")) registerGlobalValue(supybot.networks, 'default', registry.String('',
"""Determines what the default network joined by the bot will be."""))
def registerNetwork(name, password='', server=''):
name = intern(name)
network = registerGroup(supybot.networks, name)
registerGlobalValue(network, 'password', registry.String(password,
"""Determines what password will be used on %s. Yes, we know that
technically passwords are server-specific and not network-specific,
but this is the best we can do right now.""" % name))
registerGlobalValue(network, 'server', registry.String(server,
"""Determines what server the bot will connect to for %s.""" % name))
return network
# Let's fill our networks.
for (name, s) in registry._cache.iteritems():
if name.startswith('supybot.networks.'):
parts = name.split('.')
print parts
name = parts[2]
if name != 'default':
registerNetwork(name)
supybot.register('password', registry.String('', """Determines the password to
be sent to the server if it requires one."""))
class SpaceSeparatedSetOfChannels(registry.SeparatedListOf): class SpaceSeparatedSetOfChannels(registry.SeparatedListOf):
List = ircutils.IrcSet List = ircutils.IrcSet

View File

@ -440,11 +440,13 @@ class Irc(IrcCommandDispatcher):
_nickSetters = sets.Set(['001', '002', '003', '004', '250', '251', '252', _nickSetters = sets.Set(['001', '002', '003', '004', '250', '251', '252',
'254', '255', '265', '266', '372', '375', '376', '254', '255', '265', '266', '372', '375', '376',
'333', '353', '332', '366', '005']) '333', '353', '332', '366', '005'])
def __init__(self, nick, user='', ident='', password='', callbacks=None): def __init__(self, nick, user='', ident='',
network='unset', password='', callbacks=None):
world.ircs.append(self) world.ircs.append(self)
self.originalNick = intern(nick) self.originalNick = intern(nick)
self.originalNetwork = intern(network)
self.nick = self.originalNick self.nick = self.originalNick
self.network = 'unset' self.network = self.originalNetwork
self.nickmods = cycle(conf.supybot.nickmods()) self.nickmods = cycle(conf.supybot.nickmods())
self.password = password self.password = password
self.user = intern(user or nick) # Default to nick self.user = intern(user or nick) # Default to nick
@ -474,6 +476,7 @@ class Irc(IrcCommandDispatcher):
def reset(self): def reset(self):
"""Resets the Irc object. Called when the driver reconnects.""" """Resets the Irc object. Called when the driver reconnects."""
self.nick = self.originalNick self.nick = self.originalNick
self.network = self.originalNetwork
self.prefix = '%s!%s@%s' % (self.nick, self.ident, 'unset.domain') self.prefix = '%s!%s@%s' % (self.nick, self.ident, 'unset.domain')
self.state.reset() self.state.reset()
self.queue.reset() self.queue.reset()
@ -570,6 +573,7 @@ class Irc(IrcCommandDispatcher):
def do001(self, msg): def do001(self, msg):
"""Logs (and stores) the name of the network.""" """Logs (and stores) the name of the network."""
welcome = msg.args[1] welcome = msg.args[1]
if self.network == 'unset':
if not welcome.startswith('Welcome to the '): if not welcome.startswith('Welcome to the '):
log.info('Unexpected 001 welcome, guessing at network name.') log.info('Unexpected 001 welcome, guessing at network name.')
self.network = msg.prefix self.network = msg.prefix