2011-12-06 20:27:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2013-02-04 09:29:51 +00:00
|
|
|
# network.py - I/O with WeeChat/relay
|
|
|
|
#
|
2021-01-15 19:42:06 +00:00
|
|
|
# Copyright (C) 2011-2021 Sébastien Helleu <flashcode@flashtux.org>
|
2011-12-06 20:27:09 +00:00
|
|
|
#
|
|
|
|
# This file is part of QWeeChat, a Qt remote GUI for WeeChat.
|
|
|
|
#
|
|
|
|
# QWeeChat is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# QWeeChat is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2021-11-13 18:50:33 +00:00
|
|
|
"""I/O with WeeChat/relay."""
|
|
|
|
|
2021-11-14 17:40:26 +00:00
|
|
|
import hashlib
|
|
|
|
import secrets
|
2011-12-06 20:27:09 +00:00
|
|
|
import struct
|
2021-06-05 17:09:48 +00:00
|
|
|
|
2021-06-01 04:02:25 +00:00
|
|
|
from PySide6 import QtCore, QtNetwork
|
2021-06-05 17:09:48 +00:00
|
|
|
|
|
|
|
from qweechat import config
|
2021-11-14 17:40:26 +00:00
|
|
|
from qweechat.debug import DebugDialog
|
2021-06-05 17:09:48 +00:00
|
|
|
|
2016-01-16 07:10:28 +00:00
|
|
|
|
2021-11-14 17:40:26 +00:00
|
|
|
# list of supported hash algorithms on our side
|
|
|
|
# (the hash algorithm will be negotiated with the remote WeeChat)
|
|
|
|
_HASH_ALGOS_LIST = [
|
|
|
|
'plain',
|
|
|
|
'sha256',
|
|
|
|
'sha512',
|
|
|
|
'pbkdf2+sha256',
|
|
|
|
'pbkdf2+sha512',
|
2021-11-14 09:04:24 +00:00
|
|
|
]
|
2021-11-14 17:40:26 +00:00
|
|
|
_HASH_ALGOS = ':'.join(_HASH_ALGOS_LIST)
|
2014-05-08 15:40:31 +00:00
|
|
|
|
2021-11-14 17:40:26 +00:00
|
|
|
# handshake with remote WeeChat (before init)
|
|
|
|
_PROTO_HANDSHAKE = f'(handshake) handshake password_hash_algo={_HASH_ALGOS}\n'
|
|
|
|
|
|
|
|
# initialize with the password (plain text)
|
2021-11-14 18:00:55 +00:00
|
|
|
_PROTO_INIT_PWD = 'init password=%(password)s%(totp)s\n' # nosec
|
2021-11-14 17:40:26 +00:00
|
|
|
|
|
|
|
# initialize with the hashed password
|
|
|
|
_PROTO_INIT_HASH = ('init password_hash='
|
|
|
|
'%(algo)s:%(salt)s%(iter)s:%(hash)s%(totp)s\n')
|
|
|
|
|
2021-11-14 17:56:48 +00:00
|
|
|
_PROTO_SYNC_CMDS = [
|
2021-11-14 09:04:24 +00:00
|
|
|
# get buffers
|
2014-05-08 15:40:31 +00:00
|
|
|
'(listbuffers) hdata buffer:gui_buffers(*) number,full_name,short_name,'
|
|
|
|
'type,nicklist,title,local_variables',
|
2021-11-14 09:04:24 +00:00
|
|
|
# get lines
|
2014-05-08 15:40:31 +00:00
|
|
|
'(listlines) hdata buffer:gui_buffers(*)/own_lines/last_line(-%(lines)d)/'
|
|
|
|
'data date,displayed,prefix,message',
|
2021-11-14 09:04:24 +00:00
|
|
|
# get nicklist for all buffers
|
2014-05-08 15:40:31 +00:00
|
|
|
'(nicklist) nicklist',
|
2021-11-14 09:04:24 +00:00
|
|
|
# enable synchronization
|
2014-05-08 15:40:31 +00:00
|
|
|
'sync',
|
|
|
|
]
|
2011-12-06 20:27:09 +00:00
|
|
|
|
2021-11-14 08:13:35 +00:00
|
|
|
STATUS_DISCONNECTED = 'disconnected'
|
|
|
|
STATUS_CONNECTING = 'connecting'
|
2021-11-14 17:40:26 +00:00
|
|
|
STATUS_AUTHENTICATING = 'authenticating'
|
2021-11-14 08:13:35 +00:00
|
|
|
STATUS_CONNECTED = 'connected'
|
|
|
|
|
|
|
|
NETWORK_STATUS = {
|
2021-11-14 17:40:26 +00:00
|
|
|
STATUS_DISCONNECTED: {
|
2021-11-14 08:13:35 +00:00
|
|
|
'label': 'Disconnected',
|
|
|
|
'color': '#aa0000',
|
|
|
|
'icon': 'dialog-close.png',
|
|
|
|
},
|
2021-11-14 17:40:26 +00:00
|
|
|
STATUS_CONNECTING: {
|
2021-11-14 08:13:35 +00:00
|
|
|
'label': 'Connecting…',
|
2021-11-14 17:40:26 +00:00
|
|
|
'color': '#dd5f00',
|
2021-11-14 08:13:35 +00:00
|
|
|
'icon': 'dialog-warning.png',
|
|
|
|
},
|
2021-11-14 17:40:26 +00:00
|
|
|
STATUS_AUTHENTICATING: {
|
|
|
|
'label': 'Authenticating…',
|
|
|
|
'color': '#007fff',
|
|
|
|
'icon': 'dialog-password.png',
|
|
|
|
},
|
|
|
|
STATUS_CONNECTED: {
|
2021-11-14 08:13:35 +00:00
|
|
|
'label': 'Connected',
|
|
|
|
'color': 'green',
|
|
|
|
'icon': 'dialog-ok-apply.png',
|
|
|
|
},
|
|
|
|
}
|
2011-12-06 20:27:09 +00:00
|
|
|
|
2021-11-14 17:40:26 +00:00
|
|
|
|
2011-12-06 20:27:09 +00:00
|
|
|
class Network(QtCore.QObject):
|
|
|
|
"""I/O with WeeChat/relay."""
|
|
|
|
|
2021-06-05 17:09:48 +00:00
|
|
|
statusChanged = QtCore.Signal(str, str)
|
|
|
|
messageFromWeechat = QtCore.Signal(QtCore.QByteArray)
|
2011-12-06 20:27:09 +00:00
|
|
|
|
|
|
|
def __init__(self, *args):
|
2021-06-01 04:02:25 +00:00
|
|
|
super().__init__(*args)
|
2021-11-14 17:40:26 +00:00
|
|
|
self._init_connection()
|
|
|
|
self.debug_lines = []
|
|
|
|
self.debug_dialog = None
|
2014-02-07 12:03:15 +00:00
|
|
|
self._lines = config.CONFIG_DEFAULT_RELAY_LINES
|
2011-12-06 20:27:09 +00:00
|
|
|
self._buffer = QtCore.QByteArray()
|
2012-07-27 15:56:55 +00:00
|
|
|
self._socket = QtNetwork.QSslSocket()
|
2011-12-06 20:27:09 +00:00
|
|
|
self._socket.connected.connect(self._socket_connected)
|
|
|
|
self._socket.readyRead.connect(self._socket_read)
|
|
|
|
self._socket.disconnected.connect(self._socket_disconnected)
|
|
|
|
|
2021-11-14 17:40:26 +00:00
|
|
|
def _init_connection(self):
|
|
|
|
self.status = STATUS_DISCONNECTED
|
2021-11-14 17:54:48 +00:00
|
|
|
self._hostname = None
|
2021-11-14 17:40:26 +00:00
|
|
|
self._port = None
|
|
|
|
self._ssl = None
|
|
|
|
self._password = None
|
|
|
|
self._totp = None
|
|
|
|
self._handshake_received = False
|
|
|
|
self._handshake_timer = None
|
|
|
|
self._handshake_timer = False
|
|
|
|
self._pwd_hash_algo = None
|
|
|
|
self._pwd_hash_iter = 0
|
|
|
|
self._server_nonce = None
|
|
|
|
|
|
|
|
def set_status(self, status):
|
|
|
|
"""Set current status."""
|
|
|
|
self.status = status
|
|
|
|
self.statusChanged.emit(status, None)
|
|
|
|
|
|
|
|
def pbkdf2(self, hash_name, salt):
|
|
|
|
"""Return hashed password with PBKDF2-HMAC."""
|
|
|
|
return hashlib.pbkdf2_hmac(
|
|
|
|
hash_name,
|
|
|
|
password=self._password.encode('utf-8'),
|
|
|
|
salt=salt,
|
|
|
|
iterations=self._pwd_hash_iter,
|
|
|
|
).hex()
|
|
|
|
|
2021-11-14 09:04:24 +00:00
|
|
|
def _build_init_command(self):
|
|
|
|
"""Build the init command to send to WeeChat."""
|
2021-11-14 17:40:26 +00:00
|
|
|
totp = f',totp={self._totp}' if self._totp else ''
|
|
|
|
if self._pwd_hash_algo == 'plain':
|
|
|
|
cmd = _PROTO_INIT_PWD % {
|
|
|
|
'password': self._password,
|
|
|
|
'totp': totp,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
client_nonce = secrets.token_bytes(16)
|
|
|
|
salt = self._server_nonce + client_nonce
|
|
|
|
pwd_hash = None
|
|
|
|
iterations = ''
|
|
|
|
if self._pwd_hash_algo == 'pbkdf2+sha512':
|
|
|
|
pwd_hash = self.pbkdf2('sha512', salt)
|
|
|
|
iterations = f':{self._pwd_hash_iter}'
|
|
|
|
elif self._pwd_hash_algo == 'pbkdf2+sha256':
|
|
|
|
pwd_hash = self.pbkdf2('sha256', salt)
|
|
|
|
iterations = f':{self._pwd_hash_iter}'
|
|
|
|
elif self._pwd_hash_algo == 'sha512':
|
|
|
|
pwd = salt + self._password.encode('utf-8')
|
|
|
|
pwd_hash = hashlib.sha512(pwd).hexdigest()
|
|
|
|
elif self._pwd_hash_algo == 'sha256':
|
|
|
|
pwd = salt + self._password.encode('utf-8')
|
|
|
|
pwd_hash = hashlib.sha256(pwd).hexdigest()
|
|
|
|
if not pwd_hash:
|
|
|
|
return None
|
|
|
|
cmd = _PROTO_INIT_HASH % {
|
|
|
|
'algo': self._pwd_hash_algo,
|
|
|
|
'salt': bytearray(salt).hex(),
|
|
|
|
'iter': iterations,
|
|
|
|
'hash': pwd_hash,
|
|
|
|
'totp': totp,
|
|
|
|
}
|
|
|
|
return cmd
|
2021-11-14 09:04:24 +00:00
|
|
|
|
|
|
|
def _build_sync_command(self):
|
|
|
|
"""Build the sync commands to send to WeeChat."""
|
2021-11-14 17:56:48 +00:00
|
|
|
cmd = '\n'.join(_PROTO_SYNC_CMDS) + '\n'
|
2021-11-14 09:04:24 +00:00
|
|
|
return cmd % {'lines': self._lines}
|
|
|
|
|
2021-11-14 17:40:26 +00:00
|
|
|
def handshake_timer_expired(self):
|
|
|
|
if self.status == STATUS_AUTHENTICATING:
|
|
|
|
self._pwd_hash_algo = 'plain'
|
|
|
|
self.send_to_weechat(self._build_init_command())
|
|
|
|
self.sync_weechat()
|
|
|
|
self.set_status(STATUS_CONNECTED)
|
|
|
|
|
2011-12-06 20:27:09 +00:00
|
|
|
def _socket_connected(self):
|
|
|
|
"""Slot: socket connected."""
|
2021-11-14 17:40:26 +00:00
|
|
|
self.set_status(STATUS_AUTHENTICATING)
|
|
|
|
self.send_to_weechat(_PROTO_HANDSHAKE)
|
|
|
|
self._handshake_timer = QtCore.QTimer()
|
|
|
|
self._handshake_timer.setSingleShot(True)
|
|
|
|
self._handshake_timer.setInterval(2000)
|
|
|
|
self._handshake_timer.timeout.connect(self.handshake_timer_expired)
|
|
|
|
self._handshake_timer.start()
|
2011-12-06 20:27:09 +00:00
|
|
|
|
|
|
|
def _socket_read(self):
|
|
|
|
"""Slot: data available on socket."""
|
2014-07-07 18:53:18 +00:00
|
|
|
data = self._socket.readAll()
|
|
|
|
self._buffer.append(data)
|
2011-12-06 20:27:09 +00:00
|
|
|
while len(self._buffer) >= 4:
|
|
|
|
remainder = None
|
2021-06-01 04:02:25 +00:00
|
|
|
length = struct.unpack('>i', self._buffer[0:4].data())[0]
|
2011-12-06 20:27:09 +00:00
|
|
|
if len(self._buffer) < length:
|
|
|
|
# partial message, just wait for end of message
|
|
|
|
break
|
|
|
|
# more than one message?
|
|
|
|
if length < len(self._buffer):
|
|
|
|
# save beginning of another message
|
|
|
|
remainder = self._buffer[length:]
|
|
|
|
self._buffer = self._buffer[0:length]
|
|
|
|
self.messageFromWeechat.emit(self._buffer)
|
2012-07-27 15:56:55 +00:00
|
|
|
if not self.is_connected():
|
|
|
|
return
|
2011-12-06 20:27:09 +00:00
|
|
|
self._buffer.clear()
|
|
|
|
if remainder:
|
|
|
|
self._buffer.append(remainder)
|
|
|
|
|
|
|
|
def _socket_disconnected(self):
|
|
|
|
"""Slot: socket disconnected."""
|
2021-11-14 17:40:26 +00:00
|
|
|
if self._handshake_timer:
|
|
|
|
self._handshake_timer.stop()
|
|
|
|
self._init_connection()
|
|
|
|
self.set_status(STATUS_DISCONNECTED)
|
2011-12-06 20:27:09 +00:00
|
|
|
|
|
|
|
def is_connected(self):
|
2014-07-11 05:58:55 +00:00
|
|
|
"""Return True if the socket is connected, False otherwise."""
|
2011-12-06 20:27:09 +00:00
|
|
|
return self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState
|
|
|
|
|
2012-07-27 15:56:55 +00:00
|
|
|
def is_ssl(self):
|
2014-07-11 05:58:55 +00:00
|
|
|
"""Return True if SSL is used, False otherwise."""
|
2012-07-27 15:56:55 +00:00
|
|
|
return self._ssl
|
|
|
|
|
2021-11-14 17:54:48 +00:00
|
|
|
def connect_weechat(self, hostname, port, ssl, password, totp, lines):
|
2014-07-11 05:58:55 +00:00
|
|
|
"""Connect to WeeChat."""
|
2021-11-14 17:54:48 +00:00
|
|
|
self._hostname = hostname
|
2011-12-06 20:27:09 +00:00
|
|
|
try:
|
|
|
|
self._port = int(port)
|
2014-07-15 05:54:41 +00:00
|
|
|
except ValueError:
|
2011-12-06 20:27:09 +00:00
|
|
|
self._port = 0
|
2012-07-27 15:56:55 +00:00
|
|
|
self._ssl = ssl
|
2011-12-06 20:27:09 +00:00
|
|
|
self._password = password
|
2021-11-14 17:40:26 +00:00
|
|
|
self._totp = totp
|
2014-02-07 12:03:15 +00:00
|
|
|
try:
|
|
|
|
self._lines = int(lines)
|
2014-07-15 05:54:41 +00:00
|
|
|
except ValueError:
|
2014-02-07 12:03:15 +00:00
|
|
|
self._lines = config.CONFIG_DEFAULT_RELAY_LINES
|
2011-12-06 20:27:09 +00:00
|
|
|
if self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState:
|
|
|
|
return
|
|
|
|
if self._socket.state() != QtNetwork.QAbstractSocket.UnconnectedState:
|
|
|
|
self._socket.abort()
|
2012-07-27 15:56:55 +00:00
|
|
|
if self._ssl:
|
|
|
|
self._socket.ignoreSslErrors()
|
2021-11-14 17:54:48 +00:00
|
|
|
self._socket.connectToHostEncrypted(self._hostname, self._port)
|
2021-06-01 04:02:25 +00:00
|
|
|
else:
|
2021-11-14 17:54:48 +00:00
|
|
|
self._socket.connectToHost(self._hostname, self._port)
|
2021-11-14 17:40:26 +00:00
|
|
|
self.set_status(STATUS_CONNECTING)
|
2011-12-06 20:27:09 +00:00
|
|
|
|
|
|
|
def disconnect_weechat(self):
|
2014-07-11 05:58:55 +00:00
|
|
|
"""Disconnect from WeeChat."""
|
2014-05-08 15:40:31 +00:00
|
|
|
if self._socket.state() == QtNetwork.QAbstractSocket.UnconnectedState:
|
2021-11-14 17:40:26 +00:00
|
|
|
self.set_status(STATUS_DISCONNECTED)
|
2014-05-08 15:40:31 +00:00
|
|
|
return
|
|
|
|
if self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState:
|
|
|
|
self.send_to_weechat('quit\n')
|
|
|
|
self._socket.waitForBytesWritten(1000)
|
|
|
|
else:
|
2021-11-14 17:40:26 +00:00
|
|
|
self.set_status(STATUS_DISCONNECTED)
|
2014-05-08 15:40:31 +00:00
|
|
|
self._socket.abort()
|
2011-12-06 20:27:09 +00:00
|
|
|
|
|
|
|
def send_to_weechat(self, message):
|
2014-07-11 05:58:55 +00:00
|
|
|
"""Send a message to WeeChat."""
|
2021-11-14 17:40:26 +00:00
|
|
|
self.debug_print(0, '<==', message, forcecolor='#AA0000')
|
2013-03-04 12:31:24 +00:00
|
|
|
self._socket.write(message.encode('utf-8'))
|
2011-12-06 20:27:09 +00:00
|
|
|
|
2021-11-14 17:40:26 +00:00
|
|
|
def init_with_handshake(self, response):
|
|
|
|
"""Initialize with WeeChat using the handshake response."""
|
|
|
|
self._pwd_hash_algo = response['password_hash_algo']
|
|
|
|
self._pwd_hash_iter = int(response['password_hash_iterations'])
|
|
|
|
self._server_nonce = bytearray.fromhex(response['nonce'])
|
|
|
|
if self._pwd_hash_algo:
|
|
|
|
cmd = self._build_init_command()
|
|
|
|
if cmd:
|
|
|
|
self.send_to_weechat(cmd)
|
|
|
|
self.sync_weechat()
|
|
|
|
self.set_status(STATUS_CONNECTED)
|
|
|
|
return
|
|
|
|
# failed to initialize: disconnect
|
|
|
|
self.disconnect_weechat()
|
|
|
|
|
2012-05-17 09:28:15 +00:00
|
|
|
def desync_weechat(self):
|
2014-07-11 05:58:55 +00:00
|
|
|
"""Desynchronize from WeeChat."""
|
2012-07-27 15:56:55 +00:00
|
|
|
self.send_to_weechat('desync\n')
|
2012-05-17 09:28:15 +00:00
|
|
|
|
|
|
|
def sync_weechat(self):
|
2014-07-11 05:58:55 +00:00
|
|
|
"""Synchronize with WeeChat."""
|
2021-11-14 09:04:24 +00:00
|
|
|
self.send_to_weechat(self._build_sync_command())
|
2012-05-17 09:28:15 +00:00
|
|
|
|
2021-11-14 08:13:35 +00:00
|
|
|
def status_label(self, status):
|
|
|
|
"""Return the label for a given status."""
|
|
|
|
return NETWORK_STATUS.get(status, {}).get('label', '')
|
|
|
|
|
|
|
|
def status_color(self, status):
|
|
|
|
"""Return the color for a given status."""
|
|
|
|
return NETWORK_STATUS.get(status, {}).get('color', 'black')
|
|
|
|
|
2011-12-06 20:27:09 +00:00
|
|
|
def status_icon(self, status):
|
2014-07-13 08:12:59 +00:00
|
|
|
"""Return the name of icon for a given status."""
|
2021-11-14 08:13:35 +00:00
|
|
|
return NETWORK_STATUS.get(status, {}).get('icon', '')
|
2013-07-27 15:36:47 +00:00
|
|
|
|
|
|
|
def get_options(self):
|
2014-07-13 08:12:59 +00:00
|
|
|
"""Get connection options."""
|
|
|
|
return {
|
2021-11-14 17:54:48 +00:00
|
|
|
'hostname': self._hostname,
|
2014-07-13 08:12:59 +00:00
|
|
|
'port': self._port,
|
|
|
|
'ssl': 'on' if self._ssl else 'off',
|
|
|
|
'password': self._password,
|
|
|
|
'lines': str(self._lines),
|
|
|
|
}
|
2021-11-14 17:40:26 +00:00
|
|
|
|
|
|
|
def debug_print(self, *args, **kwargs):
|
|
|
|
"""Display a debug message."""
|
|
|
|
self.debug_lines.append((args, kwargs))
|
|
|
|
if self.debug_dialog:
|
|
|
|
self.debug_dialog.chat.display(*args, **kwargs)
|
|
|
|
|
|
|
|
def _debug_dialog_closed(self, result):
|
|
|
|
"""Called when debug dialog is closed."""
|
|
|
|
self.debug_dialog = None
|
|
|
|
|
|
|
|
def debug_input_text_sent(self, text):
|
|
|
|
"""Send debug buffer input to WeeChat."""
|
|
|
|
if self.network.is_connected():
|
|
|
|
text = str(text)
|
|
|
|
pos = text.find(')')
|
|
|
|
if text.startswith('(') and pos >= 0:
|
|
|
|
text = '(debug_%s)%s' % (text[1:pos], text[pos+1:])
|
|
|
|
else:
|
|
|
|
text = '(debug) %s' % text
|
|
|
|
self.network.debug_print(0, '<==', text, forcecolor='#AA0000')
|
|
|
|
self.network.send_to_weechat(text + '\n')
|
|
|
|
|
|
|
|
def open_debug_dialog(self):
|
|
|
|
"""Open a dialog with debug messages."""
|
|
|
|
if not self.debug_dialog:
|
|
|
|
self.debug_dialog = DebugDialog()
|
|
|
|
self.debug_dialog.input.textSent.connect(
|
|
|
|
self.debug_input_text_sent)
|
|
|
|
self.debug_dialog.finished.connect(self._debug_dialog_closed)
|
|
|
|
self.debug_dialog.display_lines(self.debug_lines)
|
|
|
|
self.debug_dialog.chat.scroll_bottom()
|