2017-12-30 14:03:03 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-01-26 18:22:06 +01:00
|
|
|
# Weechat Matrix Protocol Script
|
|
|
|
# Copyright © 2018 Damir Jelić <poljar@termina.org.uk>
|
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
# any purpose with or without fee is hereby granted, provided that the
|
|
|
|
# above copyright notice and this permission notice appear in all copies.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
|
|
|
|
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
|
|
|
|
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2017-12-30 14:03:03 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import ssl
|
|
|
|
import time
|
2018-01-19 12:43:12 +01:00
|
|
|
import pprint
|
2018-02-28 14:36:59 +01:00
|
|
|
import OpenSSL.crypto as crypto
|
|
|
|
import textwrap
|
|
|
|
from itertools import chain
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
# pylint: disable=redefined-builtin
|
2018-01-26 17:43:07 +01:00
|
|
|
from builtins import str
|
2018-02-28 15:53:33 +01:00
|
|
|
from future.utils import bytes_to_native_str as n
|
2018-01-06 17:12:54 +01:00
|
|
|
|
|
|
|
# pylint: disable=unused-import
|
2018-01-05 19:39:11 +01:00
|
|
|
from typing import (List, Set, Dict, Tuple, Text, Optional, AnyStr, Deque, Any)
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-02-10 15:34:40 +01:00
|
|
|
from matrix.colors import Formatted
|
2018-01-26 17:43:07 +01:00
|
|
|
from matrix.utf import utf8_decode
|
2018-01-26 14:38:46 +01:00
|
|
|
from matrix.http import HttpResponse
|
2018-02-25 17:59:08 +01:00
|
|
|
from matrix.api import MatrixSendMessage
|
2018-03-05 19:34:51 +01:00
|
|
|
from matrix.encryption import matrix_olm_command_cb
|
2018-01-26 17:43:07 +01:00
|
|
|
|
2018-01-27 14:39:20 +01:00
|
|
|
# Weechat searches for the registered callbacks in the scope of the main script
|
|
|
|
# file, import the callbacks here so weechat can find them.
|
2018-02-21 17:00:11 +01:00
|
|
|
from matrix.commands import (hook_commands, hook_page_up, matrix_command_cb,
|
|
|
|
matrix_command_join_cb, matrix_command_part_cb,
|
|
|
|
matrix_command_invite_cb, matrix_command_topic_cb,
|
|
|
|
matrix_command_pgup_cb, matrix_redact_command_cb,
|
2018-03-05 23:38:14 +01:00
|
|
|
matrix_command_buf_clear_cb, matrix_me_command_cb,
|
|
|
|
matrix_command_kick_cb)
|
2018-01-26 17:43:07 +01:00
|
|
|
|
2018-01-29 18:11:45 +01:00
|
|
|
from matrix.server import (
|
|
|
|
MatrixServer,
|
2018-01-30 11:46:29 +01:00
|
|
|
create_default_server,
|
2018-02-01 13:20:52 +01:00
|
|
|
send_cb,
|
2018-01-30 11:46:29 +01:00
|
|
|
matrix_timer_cb,
|
2018-01-29 18:11:45 +01:00
|
|
|
matrix_config_server_read_cb,
|
|
|
|
matrix_config_server_write_cb,
|
|
|
|
matrix_config_server_change_cb,
|
|
|
|
)
|
|
|
|
|
2018-02-21 17:00:11 +01:00
|
|
|
from matrix.bar_items import (init_bar_items, matrix_bar_item_name,
|
2018-02-22 15:02:42 +01:00
|
|
|
matrix_bar_item_plugin, matrix_bar_item_lag,
|
|
|
|
matrix_bar_item_buffer_modes)
|
2018-01-29 13:07:49 +01:00
|
|
|
|
2018-01-29 14:01:58 +01:00
|
|
|
from matrix.completion import (
|
2018-02-21 17:00:11 +01:00
|
|
|
init_completion, matrix_command_completion_cb,
|
|
|
|
matrix_server_command_completion_cb, matrix_debug_completion_cb,
|
2018-05-17 16:33:58 +02:00
|
|
|
matrix_message_completion_cb, matrix_server_completion_cb,
|
|
|
|
matrix_olm_user_completion_cb, matrix_olm_device_completion_cb)
|
2018-01-29 14:01:58 +01:00
|
|
|
|
2018-02-21 17:00:11 +01:00
|
|
|
from matrix.utils import (key_from_value, server_buffer_prnt, prnt_debug,
|
2018-02-25 17:59:08 +01:00
|
|
|
server_buffer_set_title)
|
2018-01-26 17:43:07 +01:00
|
|
|
|
2018-02-25 17:59:08 +01:00
|
|
|
from matrix.plugin_options import (DebugType, RedactType)
|
2018-01-24 17:34:07 +01:00
|
|
|
|
2018-02-21 17:00:11 +01:00
|
|
|
from matrix.config import (matrix_config_init, matrix_config_read,
|
|
|
|
matrix_config_free, matrix_config_change_cb,
|
|
|
|
matrix_config_reload_cb)
|
2018-01-26 17:43:07 +01:00
|
|
|
|
2018-02-05 12:38:45 +01:00
|
|
|
import matrix.globals
|
|
|
|
|
2018-02-25 17:59:08 +01:00
|
|
|
from matrix.globals import W, SERVERS
|
2018-01-26 17:43:07 +01:00
|
|
|
|
2018-02-21 17:00:11 +01:00
|
|
|
# yapf: disable
|
2018-01-27 16:21:10 +01:00
|
|
|
WEECHAT_SCRIPT_NAME = "matrix" # type: str
|
|
|
|
WEECHAT_SCRIPT_DESCRIPTION = "matrix chat plugin" # type: str
|
|
|
|
WEECHAT_SCRIPT_AUTHOR = "Damir Jelić <poljar@termina.org.uk>" # type: str
|
|
|
|
WEECHAT_SCRIPT_VERSION = "0.1" # type: str
|
|
|
|
WEECHAT_SCRIPT_LICENSE = "ISC" # type: str
|
2018-02-21 17:00:11 +01:00
|
|
|
# yapf: enable
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-01-24 12:41:01 +01:00
|
|
|
|
2018-02-28 14:36:59 +01:00
|
|
|
def print_certificate_info(buff, sock, cert):
|
|
|
|
cert_pem = ssl.DER_cert_to_PEM_cert(sock.getpeercert(True))
|
|
|
|
|
|
|
|
x509 = crypto.load_certificate(crypto.FILETYPE_PEM, cert_pem)
|
|
|
|
|
|
|
|
public_key = x509.get_pubkey()
|
|
|
|
|
|
|
|
key_type = ("RSA" if public_key.type() == crypto.TYPE_RSA else "DSA")
|
|
|
|
key_size = str(public_key.bits())
|
2018-02-28 15:53:33 +01:00
|
|
|
sha256_fingerprint = x509.digest(n(b"SHA256"))
|
|
|
|
sha1_fingerprint = x509.digest(n(b"SHA1"))
|
2018-02-28 14:36:59 +01:00
|
|
|
signature_algorithm = x509.get_signature_algorithm()
|
|
|
|
|
|
|
|
key_info = ("key info: {key_type} key {bits} bits, signed using "
|
|
|
|
"{algo}").format(
|
|
|
|
key_type=key_type, bits=key_size, algo=signature_algorithm)
|
|
|
|
|
|
|
|
validity_info = (" Begins on: {before}\n"
|
|
|
|
" Expires on: {after}").format(
|
|
|
|
before=cert["notBefore"], after=cert["notAfter"])
|
|
|
|
|
|
|
|
rdns = chain(*cert["subject"])
|
|
|
|
subject = ", ".join(["{}={}".format(name, value) for name, value in rdns])
|
|
|
|
|
|
|
|
rdns = chain(*cert["issuer"])
|
|
|
|
issuer = ", ".join(["{}={}".format(name, value) for name, value in rdns])
|
|
|
|
|
|
|
|
subject = "subject: {sub}, serial number {serial}".format(
|
|
|
|
sub=subject, serial=cert["serialNumber"])
|
|
|
|
|
|
|
|
issuer = "issuer: {issuer}".format(issuer=issuer)
|
|
|
|
|
|
|
|
fingerprints = (" SHA1: {}\n"
|
|
|
|
" SHA256: {}").format(sha1_fingerprint,
|
|
|
|
sha256_fingerprint)
|
|
|
|
|
|
|
|
wrapper = textwrap.TextWrapper(
|
|
|
|
initial_indent=" - ", subsequent_indent=" ")
|
|
|
|
|
|
|
|
message = ("{prefix}matrix: received certificate\n"
|
|
|
|
" - certificate info:\n"
|
|
|
|
"{subject}\n"
|
|
|
|
"{issuer}\n"
|
|
|
|
"{key_info}\n"
|
|
|
|
" - period of validity:\n{validity_info}\n"
|
|
|
|
" - fingerprints:\n{fingerprints}").format(
|
|
|
|
prefix=W.prefix("network"),
|
|
|
|
subject=wrapper.fill(subject),
|
|
|
|
issuer=wrapper.fill(issuer),
|
|
|
|
key_info=wrapper.fill(key_info),
|
|
|
|
validity_info=validity_info,
|
|
|
|
fingerprints=fingerprints)
|
|
|
|
|
|
|
|
W.prnt(buff, message)
|
|
|
|
|
|
|
|
|
2018-03-19 14:51:07 +01:00
|
|
|
@utf8_decode
|
|
|
|
def matrix_event_timer_cb(server_name, remaining_calls):
|
|
|
|
server = SERVERS[server_name]
|
|
|
|
server.handle_events()
|
|
|
|
return W.WEECHAT_RC_OK
|
|
|
|
|
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
def wrap_socket(server, file_descriptor):
|
2018-02-12 10:56:28 +01:00
|
|
|
# type: (MatrixServer, int) -> None
|
2018-01-06 17:12:54 +01:00
|
|
|
sock = None # type: socket.socket
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-02-21 17:00:11 +01:00
|
|
|
temp_socket = socket.fromfd(file_descriptor, socket.AF_INET,
|
|
|
|
socket.SOCK_STREAM)
|
2018-01-03 12:14:24 +01:00
|
|
|
|
2018-01-19 11:36:16 +01:00
|
|
|
# For python 2.7 wrap_socket() doesn't work with sockets created from an
|
|
|
|
# file descriptor because fromfd() doesn't return a wrapped socket, the bug
|
2018-01-24 17:34:07 +01:00
|
|
|
# was fixed for python 3, more info: https://bugs.python.org/issue13942
|
2018-01-23 13:30:42 +01:00
|
|
|
# pylint: disable=protected-access,unidiomatic-typecheck
|
|
|
|
if type(temp_socket) == socket._socket.socket:
|
2018-01-07 15:46:18 +01:00
|
|
|
# pylint: disable=no-member
|
2018-01-06 17:12:54 +01:00
|
|
|
sock = socket._socketobject(_sock=temp_socket)
|
2017-12-30 14:03:03 +01:00
|
|
|
else:
|
2018-01-06 17:12:54 +01:00
|
|
|
sock = temp_socket
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-02-02 11:10:46 +01:00
|
|
|
# fromfd() duplicates the file descriptor but doesn't retain it's blocking
|
|
|
|
# non-blocking attribute, so mark the socket as non-blocking even though
|
|
|
|
# weechat already did that for us
|
2018-01-30 19:29:03 +01:00
|
|
|
sock.setblocking(False)
|
2018-01-30 14:16:25 +01:00
|
|
|
|
2018-01-30 19:29:03 +01:00
|
|
|
message = "{prefix}matrix: Doing SSL handshake...".format(
|
|
|
|
prefix=W.prefix("network"))
|
|
|
|
W.prnt(server.server_buffer, message)
|
2018-01-30 14:16:25 +01:00
|
|
|
|
2018-01-30 19:29:03 +01:00
|
|
|
ssl_socket = server.ssl_context.wrap_socket(
|
2018-02-21 17:00:11 +01:00
|
|
|
sock, do_handshake_on_connect=False,
|
2018-01-30 19:29:03 +01:00
|
|
|
server_hostname=server.address) # type: ssl.SSLSocket
|
2018-01-06 17:12:54 +01:00
|
|
|
|
2018-01-30 19:29:03 +01:00
|
|
|
server.socket = ssl_socket
|
2018-01-30 14:16:25 +01:00
|
|
|
|
2018-01-30 19:29:03 +01:00
|
|
|
try_ssl_handshake(server)
|
2018-01-30 14:16:25 +01:00
|
|
|
|
|
|
|
|
2018-01-30 19:29:03 +01:00
|
|
|
@utf8_decode
|
|
|
|
def ssl_fd_cb(server_name, file_descriptor):
|
|
|
|
server = SERVERS[server_name]
|
|
|
|
|
|
|
|
if server.ssl_hook:
|
|
|
|
W.unhook(server.ssl_hook)
|
|
|
|
server.ssl_hook = None
|
|
|
|
|
|
|
|
try_ssl_handshake(server)
|
|
|
|
|
|
|
|
return W.WEECHAT_RC_OK
|
|
|
|
|
|
|
|
|
|
|
|
def try_ssl_handshake(server):
|
2018-02-01 18:02:58 +01:00
|
|
|
sock = server.socket
|
2018-01-30 19:29:03 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
2018-02-01 18:02:58 +01:00
|
|
|
sock.do_handshake()
|
2018-01-30 14:16:25 +01:00
|
|
|
|
2018-02-01 18:02:58 +01:00
|
|
|
cipher = sock.cipher()
|
2018-01-30 19:29:03 +01:00
|
|
|
cipher_message = ("{prefix}matrix: Connected using {tls}, and "
|
|
|
|
"{bit} bit {cipher} cipher suite.").format(
|
2018-02-01 18:02:58 +01:00
|
|
|
prefix=W.prefix("network"),
|
|
|
|
tls=cipher[1],
|
|
|
|
bit=cipher[2],
|
|
|
|
cipher=cipher[0])
|
2018-01-30 19:29:03 +01:00
|
|
|
W.prnt(server.server_buffer, cipher_message)
|
|
|
|
|
2018-02-28 14:36:59 +01:00
|
|
|
cert = sock.getpeercert()
|
|
|
|
if cert:
|
|
|
|
print_certificate_info(server.server_buffer, sock, cert)
|
2018-01-30 19:29:03 +01:00
|
|
|
|
|
|
|
finalize_connection(server)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
except ssl.SSLWantReadError:
|
2018-02-21 17:00:11 +01:00
|
|
|
hook = W.hook_fd(server.socket.fileno(), 1, 0, 0, "ssl_fd_cb",
|
|
|
|
server.name)
|
2018-01-30 19:29:03 +01:00
|
|
|
server.ssl_hook = hook
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
except ssl.SSLWantWriteError:
|
2018-02-21 17:00:11 +01:00
|
|
|
hook = W.hook_fd(server.socket.fileno(), 0, 1, 0, "ssl_fd_cb",
|
|
|
|
server.name)
|
2018-01-30 19:29:03 +01:00
|
|
|
server.ssl_hook = hook
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
except ssl.SSLError as error:
|
2018-02-02 09:50:08 +01:00
|
|
|
str_error = error.reason if error.reason else "Unknown error"
|
|
|
|
|
|
|
|
message = ("{prefix}Error while doing SSL handshake"
|
|
|
|
": {error}").format(
|
2018-02-21 17:00:11 +01:00
|
|
|
prefix=W.prefix("network"), error=str_error)
|
2018-02-02 09:50:08 +01:00
|
|
|
|
|
|
|
server_buffer_prnt(server, message)
|
|
|
|
|
|
|
|
server_buffer_prnt(
|
2018-02-21 17:00:11 +01:00
|
|
|
server, ("{prefix}matrix: disconnecting from server..."
|
|
|
|
).format(prefix=W.prefix("network")))
|
2018-02-02 09:50:08 +01:00
|
|
|
|
2018-02-02 10:29:59 +01:00
|
|
|
server.disconnect()
|
2018-01-30 19:29:03 +01:00
|
|
|
return False
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
@utf8_decode
|
2018-01-06 17:12:54 +01:00
|
|
|
def receive_cb(server_name, file_descriptor):
|
2018-01-05 19:39:11 +01:00
|
|
|
server = SERVERS[server_name]
|
|
|
|
|
2017-12-30 14:03:03 +01:00
|
|
|
while True:
|
|
|
|
try:
|
2018-01-05 19:39:11 +01:00
|
|
|
data = server.socket.recv(4096)
|
2017-12-30 14:03:03 +01:00
|
|
|
except ssl.SSLWantReadError:
|
|
|
|
break
|
2018-01-06 17:12:54 +01:00
|
|
|
except socket.error as error:
|
2018-02-02 09:50:08 +01:00
|
|
|
errno = "error" + str(error.errno) + " " if error.errno else ""
|
|
|
|
str_error = error.strerror if error.strerror else "Unknown error"
|
|
|
|
str_error = errno + str_error
|
|
|
|
|
|
|
|
message = ("{prefix}Error while reading from "
|
|
|
|
"socket: {error}").format(
|
2018-02-21 17:00:11 +01:00
|
|
|
prefix=W.prefix("network"), error=str_error)
|
2018-02-02 09:50:08 +01:00
|
|
|
|
|
|
|
server_buffer_prnt(server, message)
|
|
|
|
|
|
|
|
server_buffer_prnt(
|
2018-02-21 17:00:11 +01:00
|
|
|
server, ("{prefix}matrix: disconnecting from server..."
|
|
|
|
).format(prefix=W.prefix("network")))
|
2018-02-02 09:50:08 +01:00
|
|
|
|
2018-02-02 10:29:59 +01:00
|
|
|
server.disconnect()
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
return W.WEECHAT_RC_OK
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
if not data:
|
2018-02-02 09:50:08 +01:00
|
|
|
server_buffer_prnt(
|
|
|
|
server,
|
|
|
|
"{prefix}matrix: Error while reading from socket".format(
|
|
|
|
prefix=W.prefix("network")))
|
|
|
|
server_buffer_prnt(
|
2018-02-21 17:00:11 +01:00
|
|
|
server, ("{prefix}matrix: disconnecting from server..."
|
|
|
|
).format(prefix=W.prefix("network")))
|
2018-01-24 12:41:01 +01:00
|
|
|
|
2018-02-02 10:29:59 +01:00
|
|
|
server.disconnect()
|
2017-12-30 14:03:03 +01:00
|
|
|
break
|
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
received = len(data) # type: int
|
|
|
|
parsed_bytes = server.http_parser.execute(data, received)
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
assert parsed_bytes == received
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
if server.http_parser.is_partial_body():
|
|
|
|
server.http_buffer.append(server.http_parser.recv_body())
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
if server.http_parser.is_message_complete():
|
|
|
|
status = server.http_parser.get_status_code()
|
|
|
|
headers = server.http_parser.get_headers()
|
|
|
|
body = b"".join(server.http_buffer)
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
message = server.receive_queue.popleft()
|
2017-12-30 14:03:03 +01:00
|
|
|
message.response = HttpResponse(status, headers, body)
|
2018-01-24 12:41:01 +01:00
|
|
|
receive_time = time.time()
|
2018-01-30 12:58:16 +01:00
|
|
|
server.lag = (receive_time - message.send_time) * 1000
|
2018-02-08 10:58:33 +01:00
|
|
|
server.lag_done = True
|
2018-01-30 12:58:16 +01:00
|
|
|
W.bar_item_update("lag")
|
2018-01-24 12:41:01 +01:00
|
|
|
message.receive_time = receive_time
|
|
|
|
|
|
|
|
prnt_debug(DebugType.MESSAGING, server,
|
|
|
|
("{prefix}Received message of type {t} and "
|
|
|
|
"status {s}").format(
|
2018-02-25 18:04:49 +01:00
|
|
|
prefix=W.prefix("error"),
|
|
|
|
t=message.__class__.__name__,
|
|
|
|
s=status))
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
# Message done, reset the parser state.
|
2018-01-26 14:38:46 +01:00
|
|
|
server.reset_parser()
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-02-25 17:59:08 +01:00
|
|
|
server.handle_response(message)
|
2017-12-30 14:03:03 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
return W.WEECHAT_RC_OK
|
|
|
|
|
|
|
|
|
2018-01-30 19:29:03 +01:00
|
|
|
def finalize_connection(server):
|
2018-02-21 17:00:11 +01:00
|
|
|
hook = W.hook_fd(server.socket.fileno(), 1, 0, 0, "receive_cb", server.name)
|
2018-01-30 19:29:03 +01:00
|
|
|
|
|
|
|
server.fd_hook = hook
|
|
|
|
server.connected = True
|
|
|
|
server.connecting = False
|
|
|
|
|
2018-02-12 14:53:19 +01:00
|
|
|
server.login()
|
2018-01-30 19:29:03 +01:00
|
|
|
|
|
|
|
|
2017-12-30 14:03:03 +01:00
|
|
|
@utf8_decode
|
|
|
|
def connect_cb(data, status, gnutls_rc, sock, error, ip_address):
|
2018-01-07 16:04:17 +01:00
|
|
|
# pylint: disable=too-many-arguments,too-many-branches
|
2018-01-09 12:42:06 +01:00
|
|
|
status_value = int(status) # type: int
|
2018-01-03 12:14:24 +01:00
|
|
|
server = SERVERS[data]
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
if status_value == W.WEECHAT_HOOK_CONNECT_OK:
|
2018-01-06 17:12:54 +01:00
|
|
|
file_descriptor = int(sock) # type: int
|
2018-01-30 19:29:03 +01:00
|
|
|
server.numeric_address = ip_address
|
|
|
|
server_buffer_set_title(server)
|
2018-01-06 17:12:54 +01:00
|
|
|
|
2018-01-30 19:29:03 +01:00
|
|
|
wrap_socket(server, file_descriptor)
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-01-19 11:36:16 +01:00
|
|
|
return W.WEECHAT_RC_OK
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
elif status_value == W.WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND:
|
2018-01-19 11:36:16 +01:00
|
|
|
W.prnt(
|
|
|
|
server.server_buffer,
|
2018-02-21 17:00:11 +01:00
|
|
|
'{address} not found'.format(address=ip_address))
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
elif status_value == W.WEECHAT_HOOK_CONNECT_IP_ADDRESS_NOT_FOUND:
|
2018-01-19 11:36:16 +01:00
|
|
|
W.prnt(server.server_buffer, 'IP address not found')
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
elif status_value == W.WEECHAT_HOOK_CONNECT_CONNECTION_REFUSED:
|
2018-01-19 11:36:16 +01:00
|
|
|
W.prnt(server.server_buffer, 'Connection refused')
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
elif status_value == W.WEECHAT_HOOK_CONNECT_PROXY_ERROR:
|
2018-02-21 17:00:11 +01:00
|
|
|
W.prnt(server.server_buffer,
|
|
|
|
'Proxy fails to establish connection to server')
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
elif status_value == W.WEECHAT_HOOK_CONNECT_LOCAL_HOSTNAME_ERROR:
|
2018-01-19 11:36:16 +01:00
|
|
|
W.prnt(server.server_buffer, 'Unable to set local hostname')
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
elif status_value == W.WEECHAT_HOOK_CONNECT_GNUTLS_INIT_ERROR:
|
2018-01-19 11:36:16 +01:00
|
|
|
W.prnt(server.server_buffer, 'TLS init error')
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
elif status_value == W.WEECHAT_HOOK_CONNECT_GNUTLS_HANDSHAKE_ERROR:
|
2018-01-19 11:36:16 +01:00
|
|
|
W.prnt(server.server_buffer, 'TLS Handshake failed')
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
elif status_value == W.WEECHAT_HOOK_CONNECT_MEMORY_ERROR:
|
2018-01-19 11:36:16 +01:00
|
|
|
W.prnt(server.server_buffer, 'Not enough memory')
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
elif status_value == W.WEECHAT_HOOK_CONNECT_TIMEOUT:
|
2018-01-19 11:36:16 +01:00
|
|
|
W.prnt(server.server_buffer, 'Timeout')
|
2017-12-30 14:03:03 +01:00
|
|
|
|
|
|
|
elif status_value == W.WEECHAT_HOOK_CONNECT_SOCKET_ERROR:
|
2018-01-19 11:36:16 +01:00
|
|
|
W.prnt(server.server_buffer, 'Unable to create socket')
|
2017-12-30 14:03:03 +01:00
|
|
|
else:
|
2018-01-19 11:36:16 +01:00
|
|
|
W.prnt(
|
|
|
|
server.server_buffer,
|
2018-02-21 17:00:11 +01:00
|
|
|
'Unexpected error: {status}'.format(status=status_value))
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-02-14 11:08:43 +01:00
|
|
|
server.disconnect(reconnect=True)
|
2017-12-30 14:03:03 +01:00
|
|
|
return W.WEECHAT_RC_OK
|
|
|
|
|
|
|
|
|
|
|
|
@utf8_decode
|
2018-01-05 19:39:11 +01:00
|
|
|
def room_input_cb(server_name, buffer, input_data):
|
|
|
|
server = SERVERS[server_name]
|
|
|
|
|
2018-01-07 15:46:18 +01:00
|
|
|
if not server.connected:
|
2018-02-02 09:50:35 +01:00
|
|
|
message = "{prefix}matrix: you are not connected to the server".format(
|
2018-01-07 15:46:18 +01:00
|
|
|
prefix=W.prefix("error"))
|
|
|
|
W.prnt(buffer, message)
|
|
|
|
return W.WEECHAT_RC_ERROR
|
|
|
|
|
2018-01-10 17:09:02 +01:00
|
|
|
room_id = key_from_value(server.buffers, buffer)
|
2018-01-19 11:36:16 +01:00
|
|
|
room = server.rooms[room_id]
|
|
|
|
|
2018-05-09 14:00:32 +02:00
|
|
|
formatted_data = Formatted.from_input_line(input_data)
|
|
|
|
|
2018-01-19 11:36:16 +01:00
|
|
|
if room.encrypted:
|
2018-05-09 14:00:32 +02:00
|
|
|
server.send_room_message(room_id, formatted_data)
|
2018-01-19 11:36:16 +01:00
|
|
|
return W.WEECHAT_RC_OK
|
|
|
|
|
2018-02-12 10:55:27 +01:00
|
|
|
message = MatrixSendMessage(
|
2018-02-21 17:00:11 +01:00
|
|
|
server.client, room_id=room_id, formatted_message=formatted_data)
|
2018-01-10 17:09:02 +01:00
|
|
|
|
2018-02-02 10:13:56 +01:00
|
|
|
server.send_or_queue(message)
|
2017-12-30 14:03:03 +01:00
|
|
|
return W.WEECHAT_RC_OK
|
|
|
|
|
|
|
|
|
|
|
|
@utf8_decode
|
2018-01-03 12:14:24 +01:00
|
|
|
def room_close_cb(data, buffer):
|
2018-02-21 17:00:11 +01:00
|
|
|
W.prnt("",
|
|
|
|
"Buffer '%s' will be closed!" % W.buffer_get_string(buffer, "name"))
|
2017-12-30 14:03:03 +01:00
|
|
|
return W.WEECHAT_RC_OK
|
|
|
|
|
|
|
|
|
2018-01-03 12:14:24 +01:00
|
|
|
@utf8_decode
|
2018-01-05 19:39:11 +01:00
|
|
|
def matrix_unload_cb():
|
2018-02-05 12:38:45 +01:00
|
|
|
matrix_config_free(matrix.globals.CONFIG)
|
2018-03-25 22:01:34 +02:00
|
|
|
|
|
|
|
for server in SERVERS.values():
|
|
|
|
server.store_olm()
|
|
|
|
|
2018-02-27 19:22:38 +01:00
|
|
|
W.prnt("", "unloading")
|
2018-01-03 12:14:24 +01:00
|
|
|
return W.WEECHAT_RC_OK
|
|
|
|
|
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
def autoconnect(servers):
|
|
|
|
for server in servers.values():
|
|
|
|
if server.autoconnect:
|
2018-02-02 10:29:59 +01:00
|
|
|
server.connect()
|
2018-01-06 17:12:54 +01:00
|
|
|
|
|
|
|
|
2017-12-30 14:03:03 +01:00
|
|
|
if __name__ == "__main__":
|
2018-02-21 17:00:11 +01:00
|
|
|
if W.register(WEECHAT_SCRIPT_NAME, WEECHAT_SCRIPT_AUTHOR,
|
|
|
|
WEECHAT_SCRIPT_VERSION, WEECHAT_SCRIPT_LICENSE,
|
|
|
|
WEECHAT_SCRIPT_DESCRIPTION, 'matrix_unload_cb', ''):
|
2017-12-30 14:03:03 +01:00
|
|
|
|
2018-02-27 19:22:38 +01:00
|
|
|
if not W.mkdir_home("matrix", 0o700):
|
|
|
|
message = ("{prefix}matrix: Error creating session "
|
|
|
|
"directory").format(prefix=W.prefix("error"))
|
|
|
|
W.prnt("", message)
|
2018-02-05 12:38:45 +01:00
|
|
|
|
2018-02-27 19:22:38 +01:00
|
|
|
# TODO if this fails we should abort and unload the script.
|
2018-02-21 17:00:11 +01:00
|
|
|
matrix.globals.CONFIG = W.config_new("matrix",
|
|
|
|
"matrix_config_reload_cb", "")
|
2018-02-05 12:38:45 +01:00
|
|
|
matrix_config_init(matrix.globals.CONFIG)
|
|
|
|
matrix_config_read(matrix.globals.CONFIG)
|
2018-01-03 12:14:24 +01:00
|
|
|
|
2018-01-26 17:43:07 +01:00
|
|
|
hook_commands()
|
2018-01-29 13:07:49 +01:00
|
|
|
init_bar_items()
|
2018-01-29 14:01:58 +01:00
|
|
|
init_completion()
|
2018-01-19 10:15:03 +01:00
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
if not SERVERS:
|
2018-02-05 12:38:45 +01:00
|
|
|
create_default_server(matrix.globals.CONFIG)
|
2018-01-05 19:39:11 +01:00
|
|
|
|
2018-01-06 17:12:54 +01:00
|
|
|
autoconnect(SERVERS)
|