PEP8 clean
This commit is contained in:
parent
95deafe7af
commit
e17996d858
23 changed files with 496 additions and 331 deletions
|
|
@ -28,8 +28,11 @@ from message.printer.IRC import IRC as IRCPrinter
|
|||
from server.socket import SocketServer
|
||||
import tools
|
||||
|
||||
|
||||
class IRC(SocketServer):
|
||||
|
||||
"""Concrete implementation of a connexion to an IRC server"""
|
||||
|
||||
def __init__(self, owner, nick="nemubot", host="localhost", port=6667,
|
||||
ssl=False, username=None, password=None, realname="Nemubot",
|
||||
encoding="utf-8", caps=None, channels=list(),
|
||||
|
|
@ -155,7 +158,8 @@ class IRC(SocketServer):
|
|||
|
||||
# Respond to JOIN
|
||||
def _on_join(msg):
|
||||
if len(msg.params) == 0: return
|
||||
if len(msg.params) == 0:
|
||||
return
|
||||
|
||||
for chname in msg.decode(msg.params[0]).split(","):
|
||||
# Register the channel
|
||||
|
|
@ -164,7 +168,8 @@ class IRC(SocketServer):
|
|||
self.hookscmd["JOIN"] = _on_join
|
||||
# Respond to PART
|
||||
def _on_part(msg):
|
||||
if len(msg.params) != 1 and len(msg.params) != 2: return
|
||||
if len(msg.params) != 1 and len(msg.params) != 2:
|
||||
return
|
||||
|
||||
for chname in msg.params[0].split(b","):
|
||||
if chname in self.channels:
|
||||
|
|
@ -175,7 +180,8 @@ class IRC(SocketServer):
|
|||
self.hookscmd["PART"] = _on_part
|
||||
# Respond to 331/RPL_NOTOPIC,332/RPL_TOPIC,TOPIC
|
||||
def _on_topic(msg):
|
||||
if len(msg.params) != 1 and len(msg.params) != 2: return
|
||||
if len(msg.params) != 1 and len(msg.params) != 2:
|
||||
return
|
||||
if msg.params[0] in self.channels:
|
||||
if len(msg.params) == 1 or len(msg.params[1]) == 0:
|
||||
self.channels[msg.params[0]].topic = None
|
||||
|
|
@ -186,8 +192,10 @@ class IRC(SocketServer):
|
|||
self.hookscmd["TOPIC"] = _on_topic
|
||||
# Respond to 353/RPL_NAMREPLY
|
||||
def _on_353(msg):
|
||||
if len(msg.params) == 3: msg.params.pop(0) # 353: like RFC 1459
|
||||
if len(msg.params) != 2: return
|
||||
if len(msg.params) == 3:
|
||||
msg.params.pop(0) # 353: like RFC 1459
|
||||
if len(msg.params) != 2:
|
||||
return
|
||||
if msg.params[0] in self.channels:
|
||||
for nk in msg.decode(msg.params[1]).split(" "):
|
||||
res = re.match("^(?P<level>[^a-zA-Z[\]\\`_^{|}])(?P<nickname>[a-zA-Z[\]\\`_^{|}][a-zA-Z0-9[\]\\`_^{|}-]*)$")
|
||||
|
|
@ -196,7 +204,8 @@ class IRC(SocketServer):
|
|||
|
||||
# Respond to INVITE
|
||||
def _on_invite(msg):
|
||||
if len(msg.params) != 2: return
|
||||
if len(msg.params) != 2:
|
||||
return
|
||||
self.write("JOIN " + msg.decode(msg.params[1]))
|
||||
self.hookscmd["INVITE"] = _on_invite
|
||||
|
||||
|
|
@ -209,7 +218,8 @@ class IRC(SocketServer):
|
|||
|
||||
# Handle CTCP requests
|
||||
def _on_ctcp(msg):
|
||||
if len(msg.params) != 2 or not msg.is_ctcp: return
|
||||
if len(msg.params) != 2 or not msg.is_ctcp:
|
||||
return
|
||||
cmds = msg.decode(msg.params[1][1:len(msg.params[1])-1]).split(' ')
|
||||
if cmds[0] in self.ctcp_capabilities:
|
||||
res = self.ctcp_capabilities[cmds[0]](msg, cmds)
|
||||
|
|
@ -353,9 +363,10 @@ class IRCMessage:
|
|||
client -- export as a client-side string if true
|
||||
"""
|
||||
|
||||
res = ";".join(["@%s=%s" % (k,v if not isinstance(v, datetime) else v.strftime("%Y-%m-%dT%H:%M:%S.%fZ")) for k, v in self.tags.items()])
|
||||
res = ";".join(["@%s=%s" % (k, v if not isinstance(v, datetime) else v.strftime("%Y-%m-%dT%H:%M:%S.%fZ")) for k, v in self.tags.items()])
|
||||
|
||||
if not client: res += " :%s!%s@%s" % (self.nick, self.user, self.host)
|
||||
if not client:
|
||||
res += " :%s!%s@%s" % (self.nick, self.user, self.host)
|
||||
|
||||
res += " " + self.cmd
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ _rlist = []
|
|||
_wlist = []
|
||||
_xlist = []
|
||||
|
||||
|
||||
# Extends from IOBase in order to be compatible with select function
|
||||
class AbstractServer(io.IOBase):
|
||||
|
||||
|
|
@ -62,7 +63,8 @@ class AbstractServer(io.IOBase):
|
|||
|
||||
|
||||
def open(self):
|
||||
"""Generic open function that register the server un _rlist in case of successful _open"""
|
||||
"""Generic open function that register the server un _rlist in case
|
||||
of successful _open"""
|
||||
self.logger.info("Opening connection to %s", self.id)
|
||||
if not hasattr(self, "_open") or self._open():
|
||||
_rlist.append(self)
|
||||
|
|
@ -70,7 +72,8 @@ class AbstractServer(io.IOBase):
|
|||
|
||||
|
||||
def close(self):
|
||||
"""Generic close function that register the server un _{r,w,x}list in case of successful _close"""
|
||||
"""Generic close function that register the server un _{r,w,x}list in
|
||||
case of successful _close"""
|
||||
self.logger.info("Closing connection to %s", self.id)
|
||||
if not hasattr(self, "_close") or self._close():
|
||||
if self in _rlist:
|
||||
|
|
|
|||
|
|
@ -21,8 +21,11 @@ import socket
|
|||
|
||||
from server import AbstractServer
|
||||
|
||||
|
||||
class SocketServer(AbstractServer):
|
||||
|
||||
"""Concrete implementation of a socket connexion (can be wrapped with TLS)"""
|
||||
|
||||
def __init__(self, host, port, ssl=False):
|
||||
AbstractServer.__init__(self)
|
||||
self.host = host
|
||||
|
|
@ -55,12 +58,13 @@ class SocketServer(AbstractServer):
|
|||
self.socket = ctx.wrap_socket(self.socket)
|
||||
|
||||
try:
|
||||
self.socket.connect((self.host, self.port)) #Connect to server
|
||||
self.socket.connect((self.host, self.port)) # Connect to server
|
||||
self.logger.info("Connected to %s:%d", self.host, self.port)
|
||||
except socket.error as e:
|
||||
self.socket = None
|
||||
self.logger.critical("Unable to connect to %s:%d: %s",
|
||||
self.host, self.port, os.strerror(e.errno))
|
||||
self.host, self.port,
|
||||
os.strerror(e.errno))
|
||||
return False
|
||||
|
||||
return True
|
||||
|
|
@ -81,7 +85,8 @@ class SocketServer(AbstractServer):
|
|||
# Write
|
||||
|
||||
def _write(self, cnt):
|
||||
if not self.connected: return
|
||||
if not self.connected:
|
||||
return
|
||||
|
||||
self.socket.send(cnt)
|
||||
|
||||
|
|
@ -96,7 +101,8 @@ class SocketServer(AbstractServer):
|
|||
# Read
|
||||
|
||||
def read(self):
|
||||
if not self.connected: return
|
||||
if not self.connected:
|
||||
return
|
||||
|
||||
raw = self.socket.recv(1024)
|
||||
temp = (self.readbuffer + raw).split(b'\r\n')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue