diff --git a/matrix.py b/matrix.py index 7ac09e2..caa990c 100644 --- a/matrix.py +++ b/matrix.py @@ -76,33 +76,33 @@ CONFIG = matrix.globals.CONFIG SERVERS = matrix.globals.SERVERS -WEECHAT_SCRIPT_NAME = "matrix" # type: str -WEECHAT_SCRIPT_DESCRIPTION = "matrix chat plugin" # type: str -WEECHAT_SCRIPT_AUTHOR = "Damir Jelić " # type: str -WEECHAT_SCRIPT_VERSION = "0.1" # type: str -WEECHAT_SCRIPT_LICENSE = "ISC" # type: str +WEECHAT_SCRIPT_NAME = "matrix" # type: str +WEECHAT_SCRIPT_DESCRIPTION = "matrix chat plugin" # type: str +WEECHAT_SCRIPT_AUTHOR = "Damir Jelić " # type: str +WEECHAT_SCRIPT_VERSION = "0.1" # type: str +WEECHAT_SCRIPT_LICENSE = "ISC" # type: str class MatrixUser: def __init__(self, name, display_name): - self.name = name # type: str + self.name = name # type: str self.display_name = display_name # type: str - self.power_level = 0 # type: int - self.nick_color = "" # type: str - self.prefix = "" # type: str + self.power_level = 0 # type: int + self.nick_color = "" # type: str + self.prefix = "" # type: str class MatrixRoom: def __init__(self, room_id): # type: (str) -> None - self.room_id = room_id # type: str - self.alias = room_id # type: str - self.topic = "" # type: str - self.topic_author = "" # type: str - self.topic_date = None # type: datetime.datetime - self.prev_batch = "" # type: str - self.users = dict() # type: Dict[str, MatrixUser] - self.encrypted = False # type: bool + self.room_id = room_id # type: str + self.alias = room_id # type: str + self.topic = "" # type: str + self.topic_author = "" # type: str + self.topic_date = None # type: datetime.datetime + self.prev_batch = "" # type: str + self.users = dict() # type: Dict[str, MatrixUser] + self.encrypted = False # type: bool @utf8_decode @@ -921,10 +921,10 @@ def matrix_handle_message( matrix_sync(server) elif message_type is MessageType.SEND: - author = extra_data["author"] - message = extra_data["message"] - room_id = extra_data["room_id"] - date = int(time.time()) + author = extra_data["author"] + message = extra_data["message"] + room_id = extra_data["room_id"] + date = int(time.time()) # TODO the event_id can be missing if sending has failed for # some reason event_id = response["event_id"] @@ -1121,9 +1121,9 @@ def connect_cb(data, status, gnutls_rc, sock, error, ip_address): server.name ) - server.fd_hook = hook - server.connected = True - server.connecting = False + server.fd_hook = hook + server.connected = True + server.connecting = False server.reconnect_count = 0 server.numeric_address = ip_address diff --git a/matrix/api.py b/matrix/api.py index a47c1a2..165d8bc 100644 --- a/matrix/api.py +++ b/matrix/api.py @@ -28,15 +28,15 @@ MATRIX_API_PATH = "/_matrix/client/r0" # type: str @unique class MessageType(Enum): - LOGIN = 0 - SYNC = 1 - SEND = 2 - STATE = 3 - REDACT = 4 + LOGIN = 0 + SYNC = 1 + SEND = 2 + STATE = 3 + REDACT = 4 ROOM_MSG = 5 - JOIN = 6 - PART = 7 - INVITE = 8 + JOIN = 6 + PART = 7 + INVITE = 8 class MatrixMessage: @@ -52,14 +52,14 @@ class MatrixMessage: ): # type: (...) -> None # pylint: disable=dangerous-default-value - self.type = message_type # type: MessageType - self.request = None # type: HttpRequest - self.response = None # type: HttpResponse - self.extra_data = extra_data # type: Dict[str, Any] + self.type = message_type # type: MessageType + self.request = None # type: HttpRequest + self.response = None # type: HttpResponse + self.extra_data = extra_data # type: Dict[str, Any] self.creation_time = time.time() # type: float - self.send_time = None # type: float - self.receive_time = None # type: float + self.send_time = None # type: float + self.receive_time = None # type: float if message_type == MessageType.LOGIN: path = ("{api}/login").format(api=MATRIX_API_PATH) diff --git a/matrix/config.py b/matrix/config.py index 9d595b3..92bc205 100644 --- a/matrix/config.py +++ b/matrix/config.py @@ -23,22 +23,22 @@ from enum import Enum, unique @unique class RedactType(Enum): STRIKETHROUGH = 0 - NOTICE = 1 - DELETE = 2 + NOTICE = 1 + DELETE = 2 @unique class ServerBufferType(Enum): - MERGE_CORE = 0 - MERGE = 1 + MERGE_CORE = 0 + MERGE = 1 INDEPENDENT = 2 @unique class DebugType(Enum): MESSAGING = 0 - NETWORK = 1 - TIMING = 2 + NETWORK = 1 + TIMING = 2 Option = namedtuple( @@ -55,15 +55,15 @@ Option = namedtuple( class PluginOptions: def __init__(self): - self.redaction_type = RedactType.STRIKETHROUGH # type: RedactType + self.redaction_type = RedactType.STRIKETHROUGH # type: RedactType self.look_server_buf = ServerBufferType.MERGE_CORE # type: ServerBufferType - self.sync_limit = 30 # type: int - self.backlog_limit = 10 # type: int - self.enable_backlog = True # type: bool - self.page_up_hook = None # type: weechat.hook + self.sync_limit = 30 # type: int + self.backlog_limit = 10 # type: int + self.enable_backlog = True # type: bool + self.page_up_hook = None # type: weechat.hook - self.redaction_comp_len = 50 # type: int + self.redaction_comp_len = 50 # type: int - self.options = dict() # type: Dict[str, weechat.config_option] - self.debug = [] # type: List[DebugType] + self.options = dict() # type: Dict[str, weechat.config_option] + self.debug = [] # type: List[DebugType] diff --git a/matrix/globals.py b/matrix/globals.py index c5dd40e..11da688 100644 --- a/matrix/globals.py +++ b/matrix/globals.py @@ -108,6 +108,6 @@ def init_matrix_config(): W = weechat if sys.hexversion >= 0x3000000 else WeechatWrapper(weechat) -OPTIONS = PluginOptions() # type: PluginOptions -SERVERS = dict() # type: Dict[str, MatrixServer] -CONFIG = None # type: weechat.config +OPTIONS = PluginOptions() # type: PluginOptions +SERVERS = dict() # type: Dict[str, MatrixServer] +CONFIG = None # type: weechat.config diff --git a/matrix/http.py b/matrix/http.py index 0040cb7..6b558b8 100644 --- a/matrix/http.py +++ b/matrix/http.py @@ -22,16 +22,16 @@ from enum import Enum, unique @unique class RequestType(Enum): - GET = 0 - POST = 1 - PUT = 2 + GET = 0 + POST = 1 + PUT = 2 class HttpResponse: def __init__(self, status, headers, body): - self.status = status # type: int + self.status = status # type: int self.headers = headers # type: Dict[str, str] - self.body = body # type: bytes + self.body = body # type: bytes class HttpRequest: @@ -46,24 +46,24 @@ class HttpRequest: version="0.1") # type: str ): # type: (...) -> None - host_string = ':'.join([host, str(port)]) + host_string = ':'.join([host, str(port)]) - user_agent = 'User-Agent: {agent}'.format(agent=user_agent) - host_header = 'Host: {host}'.format(host=host_string) - request_list = [] # type: List[str] + user_agent = 'User-Agent: {agent}'.format(agent=user_agent) + host_header = 'Host: {host}'.format(host=host_string) + request_list = [] # type: List[str] accept_header = 'Accept: */*' # type: str end_separator = '\r\n' # type: str - payload = None # type: str + payload = None # type: str if request_type == RequestType.GET: get = 'GET {location} HTTP/1.1'.format(location=location) - request_list = [get, host_header, - user_agent, accept_header, end_separator] + request_list = [get, host_header, + user_agent, accept_header, end_separator] elif (request_type == RequestType.POST or request_type == RequestType.PUT): - json_data = json.dumps(data, separators=(',', ':')) + json_data = json.dumps(data, separators=(',', ':')) if request_type == RequestType.POST: method = "POST" @@ -75,15 +75,15 @@ class HttpRequest: location=location ) - type_header = 'Content-Type: application/x-www-form-urlencoded' + type_header = 'Content-Type: application/x-www-form-urlencoded' length_header = 'Content-Length: {length}'.format( length=len(json_data) ) - request_list = [request_line, host_header, - user_agent, accept_header, - length_header, type_header, end_separator] - payload = json_data + request_list = [request_line, host_header, + user_agent, accept_header, + length_header, type_header, end_separator] + payload = json_data request = '\r\n'.join(request_list) diff --git a/matrix/server.py b/matrix/server.py index a53ced7..953f245 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -28,41 +28,43 @@ class MatrixServer: # pylint: disable=too-many-instance-attributes def __init__(self, name, w, config_file): # type: (str, weechat, weechat.config) -> None - self.name = name # type: str - self.user_id = "" - self.address = "" # type: str - self.port = 8448 # type: int - self.options = dict() # type: Dict[str, weechat.config] - self.device_name = "Weechat Matrix" # type: str + self.name = name # type: str + self.user_id = "" + self.address = "" # type: str + self.port = 8448 # type: int + self.options = dict() # type: Dict[str, weechat.config] + self.device_name = "Weechat Matrix" # type: str - self.user = "" # type: str - self.password = "" # type: str + self.user = "" # type: str + self.password = "" # type: str - self.rooms = dict() # type: Dict[str, MatrixRoom] - self.buffers = dict() # type: Dict[str, weechat.buffer] - self.server_buffer = None # type: weechat.buffer - self.fd_hook = None # type: weechat.hook - self.timer_hook = None # type: weechat.hook - self.numeric_address = "" # type: str + self.rooms = dict() # type: Dict[str, MatrixRoom] + self.buffers = dict() # type: Dict[str, weechat.buffer] + self.server_buffer = None # type: weechat.buffer + self.fd_hook = None # type: weechat.hook + self.timer_hook = None # type: weechat.hook + self.numeric_address = "" # type: str - self.autoconnect = False # type: bool - self.connected = False # type: bool - self.connecting = False # type: bool - self.reconnect_count = 0 # type: int - self.socket = None # type: ssl.SSLSocket - self.ssl_context = ssl.create_default_context() # type: ssl.SSLContext + self.autoconnect = False # type: bool + self.connected = False # type: bool + self.connecting = False # type: bool + self.reconnect_count = 0 # type: int + self.socket = None # type: ssl.SSLSocket + self.ssl_context = ssl.create_default_context() # type: ssl.SSLContext - self.access_token = None # type: str - self.next_batch = None # type: str - self.transaction_id = 0 # type: int + self.access_token = None # type: str + self.next_batch = None # type: str + self.transaction_id = 0 # type: int self.http_parser = HttpParser() # type: HttpParser self.http_buffer = [] # type: List[bytes] # Queue of messages we need to send off. - self.send_queue = deque() # type: Deque[MatrixMessage] + self.send_queue = deque() # type: Deque[MatrixMessage] + # Queue of messages we send off and are waiting a response for self.receive_queue = deque() # type: Deque[MatrixMessage] + self.message_queue = deque() # type: Deque[MatrixMessage] self.ignore_event_list = [] # type: List[str] diff --git a/matrix/socket.py b/matrix/socket.py index de3f3df..14759be 100644 --- a/matrix/socket.py +++ b/matrix/socket.py @@ -33,9 +33,9 @@ def disconnect(server): if server.fd_hook: W.unhook(server.fd_hook) - server.fd_hook = None - server.socket = None - server.connected = False + server.fd_hook = None + server.socket = None + server.connected = False server_buffer_prnt(server, "Disconnected")