2018-01-03 01:33:12 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
2019-09-09 10:46:52 +00:00
|
|
|
# IrcClient.py
|
2019-09-04 14:23:59 +00:00
|
|
|
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
2018-11-26 09:35:48 +00:00
|
|
|
# This project is licensed under the terms of the MIT licence.
|
2018-01-03 01:33:12 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
from itertools import chain
|
|
|
|
import select, socket, time
|
|
|
|
|
|
|
|
class IrcClient:
|
|
|
|
"""Non-blocking abstraction over the IRC protocol"""
|
|
|
|
|
2018-01-03 02:33:57 +00:00
|
|
|
def close(self):
|
|
|
|
if self.clientSocket != None:
|
|
|
|
self.clientSocket.close()
|
|
|
|
self.clientSocket = self.clientSocketFile = None;
|
Various bugfixes & usability improvements.
1) Add background colour toolbar beneath (foreground) colour toolbar.
2) Add colour flipping command w/ {accelerator,{menu,toolbar} item}.
3) Add {de,in}crease {brush,canvas} size accelerator.
4) Add {hide,show} assets window toolbar item.
5) Circle tool: draw outline with foreground colour.
6) Circle tool: honour transparency.
7) Fill tool: change comprehensive fill modifier key from <Shift> to <Ctrl>.
8) Fill tool: fill with {back,fore}ground colour given <[RL]MB>
9) Fix arrow keys cursor motion when scrolled down.
10 Instantly reflect {brush size,colour,tool} changes in canvas.
11) Object tool: honour transparency w/ non-external objects.
12) Object tool: update selection rectangle during <LMB> whilst dragging, set w/ release of <LMB>.
13) Rectangle tool: draw outline with foreground colour.
14) Rectangle tool: honour transparency.
15) Replace wx.ToolBar() w/ wx.lib.agw.aui.AuiToolBar() & custom wx.lib.agw.aui.AuiDefaultToolBarArt().
16) Restore scrolling position after resizing canvas.
.TODO: deleted.
assets/audio/roar{arab8,spoke11}.wav: added.
assets/text/hotkeys.txt: added to document hotkeys.
assets/text/requirements.txt, requirements.txt: moved.
assets/text/TODO: updated.
{assets/tools,lib{canvas,gui,roar,rtl,tools}}/*.py: remove Vim fold markers.
libroar/RoarCanvasCommandsFile.py:_importFile(): update wx.FileDialog() message.
libroar/RoarCanvasCommandsOperators.py:canvasOperator(): update invert colours {caption,label}.
2019-09-23 16:49:33 +00:00
|
|
|
|
2018-10-25 01:06:28 +00:00
|
|
|
def connect(self, localAddr=None, preferFamily=socket.AF_INET, timeout=None):
|
2018-06-28 16:18:48 +00:00
|
|
|
gaiInfo = socket.getaddrinfo(self.serverHname, self.serverPort,
|
|
|
|
preferFamily, socket.SOCK_STREAM, socket.IPPROTO_TCP)
|
|
|
|
self.clientSocket = socket.socket(*gaiInfo[0][:3])
|
2018-01-03 01:33:12 +00:00
|
|
|
self.clientSocket.setblocking(0)
|
2018-10-25 01:06:28 +00:00
|
|
|
if localAddr != None:
|
|
|
|
gaiInfo_ = socket.getaddrinfo(localAddr, None, preferFamily, socket.SOCK_STREAM, socket.IPPROTO_TCP)
|
|
|
|
self.clientSocket.bind(gaiInfo_[0][4])
|
2018-01-03 01:33:12 +00:00
|
|
|
try:
|
2018-06-28 16:18:48 +00:00
|
|
|
self.clientSocket.connect(gaiInfo[0][4])
|
2018-01-03 01:33:12 +00:00
|
|
|
except BlockingIOError:
|
|
|
|
pass
|
|
|
|
if timeout:
|
|
|
|
readySet = select.select([], [self.clientSocket.fileno()], [], timeout)
|
|
|
|
if len(readySet[1]) == 0:
|
|
|
|
self.close(); return False;
|
|
|
|
else:
|
|
|
|
select.select([], [self.clientSocket.fileno()], [])
|
2018-01-22 20:06:46 +00:00
|
|
|
self.clientSocketFile = self.clientSocket.makefile(encoding="utf-8", errors="replace")
|
2018-01-03 03:15:43 +00:00
|
|
|
self.clientQueue = []
|
|
|
|
self.queue("NICK", self.clientNick)
|
|
|
|
self.queue("USER", self.clientIdent, "0", "0", self.clientGecos)
|
2018-01-03 01:33:12 +00:00
|
|
|
return True
|
Various bugfixes & usability improvements.
1) Add background colour toolbar beneath (foreground) colour toolbar.
2) Add colour flipping command w/ {accelerator,{menu,toolbar} item}.
3) Add {de,in}crease {brush,canvas} size accelerator.
4) Add {hide,show} assets window toolbar item.
5) Circle tool: draw outline with foreground colour.
6) Circle tool: honour transparency.
7) Fill tool: change comprehensive fill modifier key from <Shift> to <Ctrl>.
8) Fill tool: fill with {back,fore}ground colour given <[RL]MB>
9) Fix arrow keys cursor motion when scrolled down.
10 Instantly reflect {brush size,colour,tool} changes in canvas.
11) Object tool: honour transparency w/ non-external objects.
12) Object tool: update selection rectangle during <LMB> whilst dragging, set w/ release of <LMB>.
13) Rectangle tool: draw outline with foreground colour.
14) Rectangle tool: honour transparency.
15) Replace wx.ToolBar() w/ wx.lib.agw.aui.AuiToolBar() & custom wx.lib.agw.aui.AuiDefaultToolBarArt().
16) Restore scrolling position after resizing canvas.
.TODO: deleted.
assets/audio/roar{arab8,spoke11}.wav: added.
assets/text/hotkeys.txt: added to document hotkeys.
assets/text/requirements.txt, requirements.txt: moved.
assets/text/TODO: updated.
{assets/tools,lib{canvas,gui,roar,rtl,tools}}/*.py: remove Vim fold markers.
libroar/RoarCanvasCommandsFile.py:_importFile(): update wx.FileDialog() message.
libroar/RoarCanvasCommandsOperators.py:canvasOperator(): update invert colours {caption,label}.
2019-09-23 16:49:33 +00:00
|
|
|
|
2019-09-04 14:23:59 +00:00
|
|
|
def queue(self, *args):
|
|
|
|
msg = ""; argNumMax = len(args);
|
|
|
|
for argNum in range(argNumMax):
|
|
|
|
if argNum == (argNumMax - 1):
|
|
|
|
msg += ":" + args[argNum]
|
|
|
|
else:
|
|
|
|
msg += args[argNum] + " "
|
|
|
|
self.clientQueue.append((msg + "\r\n").encode())
|
Various bugfixes & usability improvements.
1) Add background colour toolbar beneath (foreground) colour toolbar.
2) Add colour flipping command w/ {accelerator,{menu,toolbar} item}.
3) Add {de,in}crease {brush,canvas} size accelerator.
4) Add {hide,show} assets window toolbar item.
5) Circle tool: draw outline with foreground colour.
6) Circle tool: honour transparency.
7) Fill tool: change comprehensive fill modifier key from <Shift> to <Ctrl>.
8) Fill tool: fill with {back,fore}ground colour given <[RL]MB>
9) Fix arrow keys cursor motion when scrolled down.
10 Instantly reflect {brush size,colour,tool} changes in canvas.
11) Object tool: honour transparency w/ non-external objects.
12) Object tool: update selection rectangle during <LMB> whilst dragging, set w/ release of <LMB>.
13) Rectangle tool: draw outline with foreground colour.
14) Rectangle tool: honour transparency.
15) Replace wx.ToolBar() w/ wx.lib.agw.aui.AuiToolBar() & custom wx.lib.agw.aui.AuiDefaultToolBarArt().
16) Restore scrolling position after resizing canvas.
.TODO: deleted.
assets/audio/roar{arab8,spoke11}.wav: added.
assets/text/hotkeys.txt: added to document hotkeys.
assets/text/requirements.txt, requirements.txt: moved.
assets/text/TODO: updated.
{assets/tools,lib{canvas,gui,roar,rtl,tools}}/*.py: remove Vim fold markers.
libroar/RoarCanvasCommandsFile.py:_importFile(): update wx.FileDialog() message.
libroar/RoarCanvasCommandsOperators.py:canvasOperator(): update invert colours {caption,label}.
2019-09-23 16:49:33 +00:00
|
|
|
|
2018-10-25 01:06:28 +00:00
|
|
|
def readline(self, timeout=30):
|
2018-01-03 01:33:12 +00:00
|
|
|
if self.clientNextTimeout:
|
|
|
|
timeNow = time.time()
|
|
|
|
if self.clientNextTimeout <= timeNow:
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
readySet = select.select([self.clientSocket.fileno()], [], [], self.clientNextTimeout - timeNow)
|
2018-10-25 01:06:28 +00:00
|
|
|
if len(readySet[0]) == 0 \
|
|
|
|
and (time.time() - timeNow) >= timeout:
|
|
|
|
return ""
|
2018-01-03 01:33:12 +00:00
|
|
|
else:
|
2018-10-25 01:06:28 +00:00
|
|
|
readySet = select.select([self.clientSocket.fileno()], [], [], timeout)
|
|
|
|
if len(readySet[0]) == 0:
|
|
|
|
return ""
|
2018-01-03 01:33:12 +00:00
|
|
|
msg = self.clientSocketFile.readline()
|
|
|
|
if len(msg):
|
|
|
|
msg = msg.rstrip("\r\n")
|
|
|
|
else:
|
|
|
|
if len(readySet[0]) == 0:
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
msg = msg.split(" :", 1)
|
|
|
|
if len(msg) == 1:
|
|
|
|
msg = list(chain.from_iterable(m.split(" ") for m in msg))
|
|
|
|
elif len(msg) == 2:
|
|
|
|
msg = msg[0].split(" ") + [msg[1]]
|
|
|
|
if msg[0][0] == ':':
|
|
|
|
msg = [msg[0][1:]] + msg[1:]
|
|
|
|
else:
|
|
|
|
msg = [""] + msg[0:]
|
|
|
|
return msg
|
Various bugfixes & usability improvements.
1) Add background colour toolbar beneath (foreground) colour toolbar.
2) Add colour flipping command w/ {accelerator,{menu,toolbar} item}.
3) Add {de,in}crease {brush,canvas} size accelerator.
4) Add {hide,show} assets window toolbar item.
5) Circle tool: draw outline with foreground colour.
6) Circle tool: honour transparency.
7) Fill tool: change comprehensive fill modifier key from <Shift> to <Ctrl>.
8) Fill tool: fill with {back,fore}ground colour given <[RL]MB>
9) Fix arrow keys cursor motion when scrolled down.
10 Instantly reflect {brush size,colour,tool} changes in canvas.
11) Object tool: honour transparency w/ non-external objects.
12) Object tool: update selection rectangle during <LMB> whilst dragging, set w/ release of <LMB>.
13) Rectangle tool: draw outline with foreground colour.
14) Rectangle tool: honour transparency.
15) Replace wx.ToolBar() w/ wx.lib.agw.aui.AuiToolBar() & custom wx.lib.agw.aui.AuiDefaultToolBarArt().
16) Restore scrolling position after resizing canvas.
.TODO: deleted.
assets/audio/roar{arab8,spoke11}.wav: added.
assets/text/hotkeys.txt: added to document hotkeys.
assets/text/requirements.txt, requirements.txt: moved.
assets/text/TODO: updated.
{assets/tools,lib{canvas,gui,roar,rtl,tools}}/*.py: remove Vim fold markers.
libroar/RoarCanvasCommandsFile.py:_importFile(): update wx.FileDialog() message.
libroar/RoarCanvasCommandsOperators.py:canvasOperator(): update invert colours {caption,label}.
2019-09-23 16:49:33 +00:00
|
|
|
|
2018-10-25 01:06:28 +00:00
|
|
|
def unqueue(self, timeout=15):
|
2018-01-03 03:15:43 +00:00
|
|
|
while self.clientQueue:
|
2018-01-03 14:18:28 +00:00
|
|
|
msg = self.clientQueue[0]; msgLen = len(msg); msgBytesSent = 0;
|
2018-01-03 03:15:43 +00:00
|
|
|
while msgBytesSent < msgLen:
|
|
|
|
if self.clientNextTimeout:
|
|
|
|
timeNow = time.time()
|
|
|
|
if self.clientNextTimeout <= timeNow:
|
2018-10-25 01:06:28 +00:00
|
|
|
self.clientQueue[0] = msg; return True;
|
2018-01-03 03:15:43 +00:00
|
|
|
else:
|
2018-10-25 01:06:28 +00:00
|
|
|
readySet = select.select([], [self.clientSocket.fileno()], [], min(self.clientNextTimeout - timeNow, timeout))
|
2018-01-03 14:13:52 +00:00
|
|
|
if len(readySet[1]) == 0:
|
2018-10-25 01:06:28 +00:00
|
|
|
timeNow_ = time.time()
|
|
|
|
if (timeNow_ - timeNow) >= timeout:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
self.clientQueue[0] = msg; return True;
|
2018-01-03 03:15:43 +00:00
|
|
|
else:
|
2018-10-25 01:06:28 +00:00
|
|
|
readySet = select.select([], [self.clientSocket.fileno()], [], timeout)
|
|
|
|
if len(readySet[1]) == 0:
|
|
|
|
return False
|
2018-01-03 03:15:43 +00:00
|
|
|
msgBytesSent = self.clientSocket.send(msg)
|
|
|
|
msg = msg[msgBytesSent:]; msgLen -= msgBytesSent;
|
|
|
|
del self.clientQueue[0]
|
2018-10-25 01:06:28 +00:00
|
|
|
return True
|
Various bugfixes & usability improvements.
1) Add background colour toolbar beneath (foreground) colour toolbar.
2) Add colour flipping command w/ {accelerator,{menu,toolbar} item}.
3) Add {de,in}crease {brush,canvas} size accelerator.
4) Add {hide,show} assets window toolbar item.
5) Circle tool: draw outline with foreground colour.
6) Circle tool: honour transparency.
7) Fill tool: change comprehensive fill modifier key from <Shift> to <Ctrl>.
8) Fill tool: fill with {back,fore}ground colour given <[RL]MB>
9) Fix arrow keys cursor motion when scrolled down.
10 Instantly reflect {brush size,colour,tool} changes in canvas.
11) Object tool: honour transparency w/ non-external objects.
12) Object tool: update selection rectangle during <LMB> whilst dragging, set w/ release of <LMB>.
13) Rectangle tool: draw outline with foreground colour.
14) Rectangle tool: honour transparency.
15) Replace wx.ToolBar() w/ wx.lib.agw.aui.AuiToolBar() & custom wx.lib.agw.aui.AuiDefaultToolBarArt().
16) Restore scrolling position after resizing canvas.
.TODO: deleted.
assets/audio/roar{arab8,spoke11}.wav: added.
assets/text/hotkeys.txt: added to document hotkeys.
assets/text/requirements.txt, requirements.txt: moved.
assets/text/TODO: updated.
{assets/tools,lib{canvas,gui,roar,rtl,tools}}/*.py: remove Vim fold markers.
libroar/RoarCanvasCommandsFile.py:_importFile(): update wx.FileDialog() message.
libroar/RoarCanvasCommandsOperators.py:canvasOperator(): update invert colours {caption,label}.
2019-09-23 16:49:33 +00:00
|
|
|
|
2018-01-03 01:33:12 +00:00
|
|
|
#
|
2018-01-06 00:44:45 +00:00
|
|
|
# __init__(self, serverHname, serverPort, clientNick, clientIdent, clientGecos): initialisation method
|
2018-01-03 01:33:12 +00:00
|
|
|
def __init__(self, serverHname, serverPort, clientNick, clientIdent, clientGecos):
|
2019-09-04 14:23:59 +00:00
|
|
|
self.clientGecos, self.clientIdent, self.clientNick = clientGecos, clientIdent, clientNick
|
|
|
|
self.clientNextTimeout, self.clientQueue, self.clientSocket, self.clientSocketFile = None, None, None, None
|
|
|
|
self.serverHname, self.serverPort = serverHname, serverPort
|
2018-01-03 01:33:12 +00:00
|
|
|
|
2018-01-04 15:24:06 +00:00
|
|
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|