Document IRC code

This commit is contained in:
Mark Veidemanis 2021-12-27 21:19:58 +00:00
parent 6339e88385
commit 5350a2e44c
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 19 additions and 2 deletions

View File

@ -37,6 +37,10 @@ class IRCBot(irc.IRCClient):
def parse(self, user, host, channel, msg): def parse(self, user, host, channel, msg):
""" """
Simple handler for IRC commands. Simple handler for IRC commands.
:param user: full user string with host
:param host: user's hostname
:param channel: channel the message was received on
:param msg: the message
""" """
spl = msg.split() spl = msg.split()
# nick = user.split("!")[0] # nick = user.split("!")[0]
@ -101,6 +105,7 @@ class IRCBot(irc.IRCClient):
Called when we have joined a channel. Called when we have joined a channel.
Setup the Agora LoopingCall to get trades. Setup the Agora LoopingCall to get trades.
This is here to ensure the IRC client is initialised enough to send the trades. This is here to ensure the IRC client is initialised enough to send the trades.
:param channel: channel we joined
""" """
self.agora.setup_loop() self.agora.setup_loop()
self.log.info("Joined channel %s" % (channel)) self.log.info("Joined channel %s" % (channel))
@ -109,6 +114,9 @@ class IRCBot(irc.IRCClient):
""" """
Called on received PRIVMSGs. Called on received PRIVMSGs.
Pass through identified commands to the parse function. Pass through identified commands to the parse function.
:param user: full user string with host
:param channel: channel the message was received on
:param msg: the message
""" """
nick = user.split("!")[0] nick = user.split("!")[0]
@ -126,14 +134,17 @@ class IRCBot(irc.IRCClient):
if msg.split()[0] != "!": if msg.split()[0] != "!":
self.parse(user, host, channel, msg[1:]) self.parse(user, host, channel, msg[1:])
def noticed(self, user, channel, message): def noticed(self, user, channel, msg):
""" """
Called on received NOTICEs. Called on received NOTICEs.
:param user: full user string with host
:param channel: channel the notice was received on
:param msg: the message
""" """
nick = user.split("!")[0] nick = user.split("!")[0]
if channel == self.nickname: if channel == self.nickname:
channel = nick channel = nick
# self.log.info("[%s] %s: %s" % (channel, user, message)) # self.log.info("[%s] %s: %s" % (channel, user, msg))
class IRCBotFactory(protocol.ClientFactory): class IRCBotFactory(protocol.ClientFactory):
@ -157,6 +168,8 @@ class IRCBotFactory(protocol.ClientFactory):
def clientConnectionLost(self, connector, reason): def clientConnectionLost(self, connector, reason):
""" """
Called when connection to IRC server lost. Reconnect. Called when connection to IRC server lost. Reconnect.
:param connector: connector object
:param reason: reason connection lost
""" """
self.log.error("Lost connection (%s), reconnecting" % (reason)) self.log.error("Lost connection (%s), reconnecting" % (reason))
connector.connect() connector.connect()
@ -164,13 +177,17 @@ class IRCBotFactory(protocol.ClientFactory):
def clientConnectionFailed(self, connector, reason): def clientConnectionFailed(self, connector, reason):
""" """
Called when connection to IRC server failed. Reconnect. Called when connection to IRC server failed. Reconnect.
:param connector: connector object
:param reason: reason connection failed
""" """
self.log.error("Could not connect: %s" % (reason)) self.log.error("Could not connect: %s" % (reason))
connector.connect() connector.connect()
def bot(): def bot():
# Load the certificates
context = ssl.DefaultOpenSSLContextFactory(settings.IRC.Cert, settings.IRC.Cert) context = ssl.DefaultOpenSSLContextFactory(settings.IRC.Cert, settings.IRC.Cert)
# Define the factory instance
factory = IRCBotFactory() factory = IRCBotFactory()
reactor.connectSSL(settings.IRC.Host, int(settings.IRC.Port), factory, context) reactor.connectSSL(settings.IRC.Host, int(settings.IRC.Port), factory, context)
return factory return factory