[TASK] Kickstart IrcProtocol, use double quotes for everything.
This commit is contained in:
parent
e31bd1700b
commit
b688092b74
59
bot.py
59
bot.py
|
@ -4,12 +4,13 @@ import asyncio
|
||||||
import inspect
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logging.basicConfig(format='[%(asctime)s] [%(levelname)s] %(message)s', level=logging.DEBUG, datefmt='%d.%m.%Y %H:%M:%S')
|
logging.basicConfig(format="[%(asctime)s] [%(levelname)s] %(message)s", level=logging.DEBUG, datefmt="%d.%m.%Y %H:%M:%S")
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ManagedProtocol(asyncio.Protocol):
|
class ManagedProtocol(asyncio.Protocol):
|
||||||
"""Basic managed protocol handler, registers itself to ConnectionManager.
|
"""Basic managed protocol handler, registers itself to ConnectionManager.
|
||||||
|
Inherit this to overlay the management with actual protocol parsing.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, loop, connection_manager, endpoint):
|
def __init__(self, loop, connection_manager, endpoint):
|
||||||
|
@ -26,29 +27,57 @@ class ManagedProtocol(asyncio.Protocol):
|
||||||
self._connection_manager.register_active_connection(self._endpoint, self)
|
self._connection_manager.register_active_connection(self._endpoint, self)
|
||||||
self._transport = transport
|
self._transport = transport
|
||||||
self._log("Connection made!")
|
self._log("Connection made!")
|
||||||
host, port = transport.get_extra_info('peername')
|
host, port = transport.get_extra_info("peername")
|
||||||
self._log('Connected to: {}:{}'.format(host, port))
|
self._log("Connected to: {}:{}".format(host, port))
|
||||||
|
|
||||||
def data_received(self, data):
|
def data_received(self, data):
|
||||||
self._log(data)
|
self._log("[R] "+str(data))
|
||||||
# TODO: parse and call more methodes / on itself for events or something
|
|
||||||
|
|
||||||
def eof_received(self):
|
def eof_received(self):
|
||||||
self._log("Eof received!")
|
self._log("Eof received!")
|
||||||
|
|
||||||
def connection_lost(self, exc):
|
def connection_lost(self, exc):
|
||||||
self._log("Connection lost!")
|
self._log("Connection lost! ("+str(exc)+")")
|
||||||
self._connection_manager.unregister_active_connection(self._endpoint)
|
self._connection_manager.unregister_active_connection(self._endpoint)
|
||||||
|
|
||||||
|
def send_data(self, data):
|
||||||
|
self._log("[W] "+str(data))
|
||||||
|
self._transport.write(data)
|
||||||
|
|
||||||
def destroy(self):
|
def destroy(self):
|
||||||
""" Triggered by ConnectionManager.remove_endpoint(). Closes transport. """
|
""" Triggered by ConnectionManager.remove_endpoint(). Closes transport. """
|
||||||
self._transport.close()
|
self._transport.close()
|
||||||
|
|
||||||
|
|
||||||
class IrcProtocol(ManagedProtocol):
|
class IrcProtocol(ManagedProtocol):
|
||||||
def __init__(self):
|
"""Implementation of the IRC protocol.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, loop, connection_manager, endpoint):
|
||||||
|
super(IrcProtocol, self).__init__(loop, connection_manager, endpoint)
|
||||||
|
self._loop = loop
|
||||||
|
self.motd = False
|
||||||
|
self.hello = False
|
||||||
|
|
||||||
|
def connection_made(self, transport):
|
||||||
|
super(IrcProtocol, self).connection_made(transport)
|
||||||
|
self.send_data(b"USER as as as :as\r\n")
|
||||||
|
self.send_data(b"NICK Pb42\r\n")
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def data_received(self, data):
|
||||||
|
super(IrcProtocol, self).data_received(data)
|
||||||
|
pass
|
||||||
|
|
||||||
|
def eof_received(self):
|
||||||
|
super(IrcProtocol, self).eof_received()
|
||||||
|
pass
|
||||||
|
|
||||||
|
def connection_lost(self, exc):
|
||||||
|
super(IrcProtocol, self).connection_lost()
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ConnectionManager(object):
|
class ConnectionManager(object):
|
||||||
"""Takes care of known endpoints that a connections shall be established to.
|
"""Takes care of known endpoints that a connections shall be established to.
|
||||||
|
@ -66,7 +95,7 @@ class ConnectionManager(object):
|
||||||
self._create_connection(endpoint)
|
self._create_connection(endpoint)
|
||||||
|
|
||||||
def _create_connection(self, endpoint):
|
def _create_connection(self, endpoint):
|
||||||
protocol = ManagedProtocol(self._loop, self, endpoint)
|
protocol = IrcProtocol(self._loop, self, endpoint)
|
||||||
coroutine = self._loop.create_connection(lambda: protocol, *endpoint)
|
coroutine = self._loop.create_connection(lambda: protocol, *endpoint)
|
||||||
asyncio.async(coroutine)
|
asyncio.async(coroutine)
|
||||||
|
|
||||||
|
@ -85,22 +114,22 @@ class ConnectionManager(object):
|
||||||
|
|
||||||
def _handle_async_exception(self, loop, context):
|
def _handle_async_exception(self, loop, context):
|
||||||
"""Trying to take care of connection related exceptions."""
|
"""Trying to take care of connection related exceptions."""
|
||||||
logger.error("An async exception has been caught: "+str(context['exception']))
|
logger.error("An async exception has been caught: "+str(context["exception"]))
|
||||||
stack = context['future'].get_stack()
|
stack = context["future"].get_stack()
|
||||||
if len(stack) > 1 and stack[1].f_code.co_name == 'create_connection':
|
if len(stack) > 1 and stack[1].f_code.co_name == "create_connection":
|
||||||
last_stackframe = stack[len(stack)-1]
|
last_stackframe = stack[len(stack)-1]
|
||||||
call_args = inspect.getargvalues(last_stackframe)
|
call_args = inspect.getargvalues(last_stackframe)
|
||||||
host = call_args.locals['host']
|
host = call_args.locals["host"]
|
||||||
port = call_args.locals['port']
|
port = call_args.locals["port"]
|
||||||
logger.error("Bad endpoint: {}:{}".format(host, port))
|
logger.error("Bad endpoint: {}:{}".format(host, port))
|
||||||
self.remove_endpoint((host, port))
|
self.remove_endpoint((host, port))
|
||||||
else:
|
else:
|
||||||
loop.call_exception_handler(context)
|
loop.call_exception_handler(context)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
freenode = ("irc.freenode.net", 6667)
|
freenode = ("irc.freenode.net", 6667)
|
||||||
euirc = ("irc.euisadasdrc.net", 6667)
|
euirc = ("irc.euirc.net", 6667)
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue