#!/usr/bin/python
# -*- coding: utf-8 -*-
#
-# Copyright (C) 2011-2012 Sebastien Helleu <flashcode@flashtux.org>
+# network.py - I/O with WeeChat/relay
+#
+# Copyright (C) 2011-2013 Sebastien Helleu <flashcode@flashtux.org>
#
# This file is part of QWeeChat, a Qt remote GUI for WeeChat.
#
# along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
#
-#
-# I/O with WeeChat/relay.
-#
-
import struct
import qt_compat
QtCore = qt_compat.import_module('QtCore')
self.status_connected = 'connected'
self._server = None
self._port = None
+ self._ssl = None
self._password = None
self._buffer = QtCore.QByteArray()
- self._socket = QtNetwork.QTcpSocket()
+ self._socket = QtNetwork.QSslSocket()
self._socket.connected.connect(self._socket_connected)
self._socket.error.connect(self._socket_error)
self._socket.readyRead.connect(self._socket_read)
"""Slot: socket connected."""
self.statusChanged.emit(self.status_connected, None)
if self._password:
- self._socket.write('\n'.join(_PROTO_INIT_CMD + _PROTO_SYNC_CMDS) % {'password': str(self._password)})
+ self.send_to_weechat('\n'.join(_PROTO_INIT_CMD + _PROTO_SYNC_CMDS) % {'password': str(self._password)})
def _socket_error(self, error):
"""Slot: socket error."""
def _socket_read(self):
"""Slot: data available on socket."""
- avail = self._socket.bytesAvailable()
- bytes = self._socket.read(avail)
+ bytes = self._socket.readAll()
self._buffer.append(bytes)
while len(self._buffer) >= 4:
remainder = None
remainder = self._buffer[length:]
self._buffer = self._buffer[0:length]
self.messageFromWeechat.emit(self._buffer)
+ if not self.is_connected():
+ return
self._buffer.clear()
if remainder:
self._buffer.append(remainder)
"""Slot: socket disconnected."""
self._server = None
self._port = None
+ self._ssl = None
self._password = None
self.statusChanged.emit(self.status_disconnected, None)
def is_connected(self):
return self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState
- def connect_weechat(self, server, port, password):
+ def is_ssl(self):
+ return self._ssl
+
+ def connect_weechat(self, server, port, ssl, password):
self._server = server
try:
self._port = int(port)
except:
self._port = 0
+ self._ssl = ssl
self._password = password
if self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState:
return
if self._socket.state() != QtNetwork.QAbstractSocket.UnconnectedState:
self._socket.abort()
self._socket.connectToHost(self._server, self._port)
+ if self._ssl:
+ self._socket.ignoreSslErrors()
+ self._socket.startClientEncryption()
self.statusChanged.emit(self.status_connecting, None)
def disconnect_weechat(self):
if self._socket.state() != QtNetwork.QAbstractSocket.UnconnectedState:
- if self._socket.state() != QtNetwork.QAbstractSocket.ConnectedState:
+ if self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState:
+ self.send_to_weechat('quit\n')
+ self._socket.waitForBytesWritten(1000)
+ else:
self.statusChanged.emit(self.status_disconnected, None)
self._socket.abort()
self._socket.write(str(message))
def desync_weechat(self):
- self._socket.write('desync\n')
+ self.send_to_weechat('desync\n')
def sync_weechat(self):
- self._socket.write('\n'.join(_PROTO_SYNC_CMDS))
+ self.send_to_weechat('\n'.join(_PROTO_SYNC_CMDS))
def status_icon(self, status):
icon = {self.status_disconnected: 'dialog-close.png',