Limnoria/src/asyncoreDrivers.py

245 lines
7.9 KiB
Python
Raw Normal View History

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.
###
from fix import *
import re
import sys
import time
import socket
import asyncore
import asynchat
import repl
import conf
import debug
import ircdb
import world
import drivers
import ircmsgs
import schedule
class AsyncoreRunnerDriver(drivers.IrcDriver):
def run(self):
#debug.printf(asyncore.socket_map)
try:
2003-04-17 12:07:43 +02:00
asyncore.poll(conf.poll)
2003-03-12 07:26:59 +01:00
except:
debug.recoverableException()
class AsyncoreDriver(asynchat.async_chat, object):
def __init__(self, (server, port), irc):
2003-03-12 07:26:59 +01:00
#debug.methodNamePrintf(self, '__init__')
asynchat.async_chat.__init__(self)
self.server = (server, port)
self.irc = irc
self.irc.driver = self
2003-03-12 07:26:59 +01:00
self.buffer = ''
self.set_terminator('\n')
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
2003-04-03 08:56:22 +02:00
try:
self.connect(self.server)
except:
debug.recoverableException('terse')
2003-04-03 08:56:22 +02:00
self.close()
def scheduleReconnect(self):
#debug.methodNamePrintf(self, 'scheduleReconnect')
when = time.time() + 60
whenS = time.strftime(conf.logTimestampFormat, time.localtime(when))
debug.msg('Scheduling reconnect to %s at %s' % (self.server, whenS),
'normal')
def makeNewDriver():
self.irc.reset()
driver = self.__class__(self.server, self.irc)
schedule.addEvent(makeNewDriver, when)
2003-03-12 07:26:59 +01:00
def writable(self):
#debug.methodNamePrintf(self, 'writable')
2003-04-09 17:13:51 +02:00
while self.connected:
2003-03-12 07:26:59 +01:00
m = self.irc.takeMsg()
if m:
self.push(str(m))
else:
break
return asynchat.async_chat.writable(self)
def handle_error(self):
#debug.recoverableException()
self.handle_close()
2003-03-12 07:26:59 +01:00
def collect_incoming_data(self, s):
#debug.methodNamePrintf(self, 'collect_incoming_data')
self.buffer += s
def found_terminator(self):
#debug.methodNamePrintf(self, 'found_terminator')
2003-04-15 16:36:58 +02:00
start = time.time()
2003-03-12 07:26:59 +01:00
msg = ircmsgs.IrcMsg(self.buffer)
2003-04-15 16:36:58 +02:00
debug.msg('Time to parse IrcMsg: %s' % (time.time()-start), 'verbose')
self.buffer = ''
2003-03-12 07:26:59 +01:00
try:
self.irc.feedMsg(msg)
2003-04-15 16:36:58 +02:00
except:
debug.msg('Exception caught outside Irc object.', 'normal')
2003-03-12 07:26:59 +01:00
debug.recoverableException()
def handle_close(self):
#debug.methodNamePrintf(self, 'handle_close')
2003-04-03 08:56:22 +02:00
self.scheduleReconnect()
2003-04-03 10:52:41 +02:00
self.die()
2003-04-21 06:32:42 +02:00
reconnect = handle_close
def handle_connect(self):
#debug.methodNamePrintf(self, 'handle_connect')
pass
2003-04-03 10:52:41 +02:00
def die(self):
#debug.methodNamePrintf(self, 'die')
2003-03-12 07:26:59 +01:00
self.close()
2003-08-20 18:26:23 +02:00
2003-03-12 07:26:59 +01:00
class ReplListener(asyncore.dispatcher, object):
def __init__(self, port=conf.telnetPort):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(('', port))
self.listen(5)
def handle_accept(self):
(sock, addr) = self.accept()
2003-04-03 12:06:11 +02:00
debug.msg('Connection made to telnet-REPL: ' + str(addr),'normal')
2003-03-12 07:26:59 +01:00
Repl((sock, addr))
class Repl(asynchat.async_chat, object):
filename = 'repl'
def __init__(self, (sock, addr)):
asynchat.async_chat.__init__(self, sock)
self.buffer = ''
self.prompt = """SupyBot version %s.
Python %s
Type disconnect() to disconnect.
Name: """ % (world.version, sys.version.translate(string.ascii, '\r\n'))
self.u = None
self.authed = False
self.set_terminator('\r\n')
self.repl = repl.Repl(addr[0])
self.repl.namespace['disconnect'] = self.close
self.push(self.prompt)
self.tries = 0
_re = re.compile(r'(?<!\r)\n')
def push(self, data):
asynchat.async_chat.push(self, self._re.sub('\r\n', data))
def collect_incoming_data(self, data):
if self.tries >= 3:
self.close()
self.buffer += data
if len(self.buffer) > 1024:
self.close()
def handle_close(self):
self.close()
def handle_error(self):
self.close()
def found_terminator(self):
if self.u is None:
try:
name = self.buffer
self.buffer = ''
id = ircdb.users.getUserId(name)
self.u = ircdb.users.getUser(id)
2003-03-12 07:26:59 +01:00
self.prompt = 'Password: '
except KeyError:
self.push('Unknown user.\n')
self.tries += 1
self.prompt = 'Name: '
msg = 'Unknown user %s on telnet REPL.' % name
2003-04-03 12:06:11 +02:00
debug.msg(msg,'high')
2003-03-12 07:26:59 +01:00
self.push(self.prompt)
elif self.u is not None and not self.authed:
password = self.buffer
self.buffer = ''
if self.u.checkPassword(password):
if self.u.checkCapability('owner'):
self.authed = True
self.prompt = '>>> '
else:
self.push('Only owners can use this feature.\n')
self.close()
msg = 'Attempted non-owner user %s on telnet REPL' % name
2003-04-03 12:06:11 +02:00
debug.msg(msg, 'high')
2003-03-12 07:26:59 +01:00
else:
self.push('Incorrect Password.\n')
self.prompt = 'Name: '
self.u = None
msg = 'Invalid password for user %s on telnet REPL.' % name
2003-04-03 12:06:11 +02:00
debug.msg(msg, 'high')
2003-03-12 07:26:59 +01:00
self.push(self.prompt)
elif self.authed:
2003-04-03 12:06:11 +02:00
debug.msg('Telnet REPL: %s' % self.buffer)
2003-03-12 07:26:59 +01:00
ret = self.repl.addLine(self.buffer+'\r\n')
self.buffer = ''
if ret is not repl.NotYet:
if ret is not None:
s = self._re.sub('\r\n', str(ret))
self.push(s)
self.push('\r\n')
self.prompt = '>>> '
else:
self.prompt = '... '
self.push(self.prompt)
try:
ignore(poller)
except NameError:
poller = AsyncoreRunnerDriver()
if conf.telnetEnable and __name__ != '__main__':
try:
ignore(_listener)
except NameError:
_listener = ReplListener()
2003-04-20 09:15:39 +02:00
Driver = AsyncoreDriver
2003-03-12 07:26:59 +01:00
if __name__ == '__main__':
ReplListener()
asyncore.loop()
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: