From 6fcf0e0a4f8152b103efac6c594fc31bab8e01f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucio=20Andr=C3=A9s=20Illanes=20Albornoz?= Date: Wed, 3 Jan 2018 04:15:43 +0100 Subject: [PATCH] IrcClient.py:IrcClient.{queue,unqueue}(): split. IrcMiRCARTBot.py:IrcMiRCARTBot._dispatch{001,Ping,Privmsg,Timer}(): replace readline() calls w/ queue() calls. IrcMiRCARTBot.py:IrcMiRCARTBot.dispatch(): call unqueue() after processing timers and prior to calling readline(). --- IrcClient.py | 34 ++++++++++++++++++++++++---------- IrcMiRCARTBot.py | 11 ++++++----- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/IrcClient.py b/IrcClient.py index 423cb55..e475ba2 100644 --- a/IrcClient.py +++ b/IrcClient.py @@ -30,7 +30,7 @@ class IrcClient: serverHname = serverPort = None; clientNick = clientIdent = clientGecos = None; clientSocket = clientSocketFile = None; - clientNextTimeout = None + clientNextTimeout = None; clientQueue = None; # {{{ close(): Close connection to server def close(self): @@ -53,8 +53,9 @@ class IrcClient: else: select.select([], [self.clientSocket.fileno()], []) self.clientSocketFile = self.clientSocket.makefile() - self.sendline("NICK", self.clientNick) - self.sendline("USER", self.clientIdent, "0", "0", self.clientGecos) + self.clientQueue = [] + self.queue("NICK", self.clientNick) + self.queue("USER", self.clientIdent, "0", "0", self.clientGecos) return True # }}} # {{{ readline(): Read and parse single line from server into canonicalised list, honouring timers @@ -86,20 +87,33 @@ class IrcClient: msg = [""] + msg[0:] return msg # }}} - # {{{ sendline(): Parse and send single line to server from list, ignoring timers - def sendline(self, *args): + # {{{ queue(): Parse and queue single line to server from list + def queue(self, *args): msg = ""; argNumMax = len(args); for argNum in range(0, argNumMax): if argNum == (argNumMax - 1): msg += ":" + args[argNum] else: msg += args[argNum] + " " - msg = (msg + "\r\n").encode(); msgLen = len(msg); msgBytesSent = 0; - while msgBytesSent < msgLen: - readySet = select.select([], [self.clientSocket.fileno()], []) - msgBytesSent = self.clientSocket.send(msg) - msg = msg[msgBytesSent:]; msgLen -= msgBytesSent; + self.clientQueue.append(msg) # }}} + # {{{ unqueue(): Send all queued lines to server, honouring timers + def unqueue(self): + while self.clientQueue: + msg = self.clientQueue[0] + msg = (msg + "\r\n").encode(); msgLen = len(msg); msgBytesSent = 0; + while msgBytesSent < msgLen: + if self.clientNextTimeout: + timeNow = time.time() + if self.clientNextTimeout <= timeNow: + self.clientQueue[0] = msg; return; + else: + readySet = select.select([], [self.clientSocket.fileno()], [], self.clientNextTimeout - timeNow) + else: + readySet = select.select([], [self.clientSocket.fileno()], []) + msgBytesSent = self.clientSocket.send(msg) + msg = msg[msgBytesSent:]; msgLen -= msgBytesSent; + del self.clientQueue[0] # # Initialisation method diff --git a/IrcMiRCARTBot.py b/IrcMiRCARTBot.py index bcb049f..8c32884 100755 --- a/IrcMiRCARTBot.py +++ b/IrcMiRCARTBot.py @@ -37,7 +37,7 @@ class IrcMiRCARTBot(IrcClient.IrcClient): def _dispatch001(self, message): self._log("Registered on {}:{} as {}, {}, {}.".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos)) self._log("Attempting to join {} on {}:{}...".format(self.clientChannel, self.serverHname, self.serverPort)) - self.sendline("JOIN", self.clientChannel) + self.queue("JOIN", self.clientChannel) # }}} # {{{ _dispatch353(): Dispatch single 353 (RPL_NAMREPLY) def _dispatch353(self, message): @@ -96,7 +96,7 @@ class IrcMiRCARTBot(IrcClient.IrcClient): # }}} # {{{ _dispatchPing(): Dispatch single PING message from server def _dispatchPing(self, message): - self.sendline("PONG", message[2]) + self.queue("PONG", message[2]) # }}} # {{{ _dispatchPrivmsg(): Dispatch single PRIVMSG message from server def _dispatchPrivmsg(self, message): @@ -122,10 +122,10 @@ class IrcMiRCARTBot(IrcClient.IrcClient): imgurResponse = self._uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", "c9a6efb3d7932fd") if imgurResponse[0] == 200: self._log("Uploaded as: {}".format(imgurResponse[1])) - self.sendline("PRIVMSG", message[2], "8/!\\ Uploaded as: {}".format(imgurResponse[1])) + self.queue("PRIVMSG", message[2], "8/!\\ Uploaded as: {}".format(imgurResponse[1])) else: self._log("Upload failed with HTTP status code {}".format(imgurResponse[0])) - self.sendline("PRIVMSG", message[2], "4/!\\ Uploaded failed with HTTP status code {}!".format(imgurResponse[0])) + self.queue("PRIVMSG", message[2], "4/!\\ Uploaded failed with HTTP status code {}!".format(imgurResponse[0])) if os.path.isfile(asciiTmpFilePath): os.remove(asciiTmpFilePath) if os.path.isfile(imgTmpFilePath): @@ -135,7 +135,7 @@ class IrcMiRCARTBot(IrcClient.IrcClient): def _dispatchTimer(self): if self.clientChannelRejoin: self._log("Attempting to join {} on {}:{}...".format(self.clientChannel, self.serverHname, self.serverPort)) - self.sendline("JOIN", self.clientChannel) + self.queue("JOIN", self.clientChannel) self.clientNextTimeout = time.time() + 15; self.clientChannelRejoin = True; # }}} # {{{ _log(): Log single message to stdout w/ timestamp @@ -179,6 +179,7 @@ class IrcMiRCARTBot(IrcClient.IrcClient): timeNow = time.time() if self.clientNextTimeout <= timeNow: self._dispatchTimer() + self.unqueue() serverMessage = self.readline() if serverMessage == None: self._dispatchNone(); break;