]>
Commit | Line | Data |
---|---|---|
1 | # -*- coding: utf-8 -*- | |
2 | # | |
3 | # network.py - I/O with WeeChat/relay | |
4 | # | |
5 | # Copyright (C) 2011-2021 Sébastien Helleu <flashcode@flashtux.org> | |
6 | # | |
7 | # This file is part of QWeeChat, a Qt remote GUI for WeeChat. | |
8 | # | |
9 | # QWeeChat is free software; you can redistribute it and/or modify | |
10 | # it under the terms of the GNU General Public License as published by | |
11 | # the Free Software Foundation; either version 3 of the License, or | |
12 | # (at your option) any later version. | |
13 | # | |
14 | # QWeeChat is distributed in the hope that it will be useful, | |
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | # GNU General Public License for more details. | |
18 | # | |
19 | # You should have received a copy of the GNU General Public License | |
20 | # along with QWeeChat. If not, see <http://www.gnu.org/licenses/>. | |
21 | # | |
22 | ||
23 | import struct | |
24 | ||
25 | from PySide6 import QtCore, QtNetwork | |
26 | ||
27 | from qweechat import config | |
28 | ||
29 | ||
30 | ||
31 | _PROTO_INIT_CMD = ['init password=%(password)s'] | |
32 | ||
33 | _PROTO_SYNC_CMDS = [ | |
34 | '(listbuffers) hdata buffer:gui_buffers(*) number,full_name,short_name,' | |
35 | 'type,nicklist,title,local_variables', | |
36 | ||
37 | '(listlines) hdata buffer:gui_buffers(*)/own_lines/last_line(-%(lines)d)/' | |
38 | 'data date,displayed,prefix,message', | |
39 | ||
40 | '(nicklist) nicklist', | |
41 | ||
42 | 'sync', | |
43 | ||
44 | '' | |
45 | ] | |
46 | ||
47 | ||
48 | class Network(QtCore.QObject): | |
49 | """I/O with WeeChat/relay.""" | |
50 | ||
51 | statusChanged = QtCore.Signal(str, str) | |
52 | messageFromWeechat = QtCore.Signal(QtCore.QByteArray) | |
53 | ||
54 | def __init__(self, *args): | |
55 | super().__init__(*args) | |
56 | self.status_disconnected = 'disconnected' | |
57 | self.status_connecting = 'connecting...' | |
58 | self.status_connected = 'connected' | |
59 | self._server = None | |
60 | self._port = None | |
61 | self._ssl = None | |
62 | self._password = None | |
63 | self._lines = config.CONFIG_DEFAULT_RELAY_LINES | |
64 | self._buffer = QtCore.QByteArray() | |
65 | self._socket = QtNetwork.QSslSocket() | |
66 | self._socket.connected.connect(self._socket_connected) | |
67 | # self._socket.error.connect(self._socket_error) | |
68 | self._socket.readyRead.connect(self._socket_read) | |
69 | self._socket.disconnected.connect(self._socket_disconnected) | |
70 | ||
71 | def _socket_connected(self): | |
72 | """Slot: socket connected.""" | |
73 | self.statusChanged.emit(self.status_connected, None) | |
74 | if self._password: | |
75 | self.send_to_weechat('\n'.join(_PROTO_INIT_CMD + _PROTO_SYNC_CMDS) | |
76 | % {'password': str(self._password), | |
77 | 'lines': self._lines}) | |
78 | ||
79 | def _socket_error(self, error): | |
80 | """Slot: socket error.""" | |
81 | self.statusChanged.emit( | |
82 | self.status_disconnected, | |
83 | 'Failed, error: %s' % self._socket.errorString()) | |
84 | ||
85 | def _socket_read(self): | |
86 | """Slot: data available on socket.""" | |
87 | data = self._socket.readAll() | |
88 | self._buffer.append(data) | |
89 | while len(self._buffer) >= 4: | |
90 | remainder = None | |
91 | length = struct.unpack('>i', self._buffer[0:4].data())[0] | |
92 | if len(self._buffer) < length: | |
93 | # partial message, just wait for end of message | |
94 | break | |
95 | # more than one message? | |
96 | if length < len(self._buffer): | |
97 | # save beginning of another message | |
98 | remainder = self._buffer[length:] | |
99 | self._buffer = self._buffer[0:length] | |
100 | self.messageFromWeechat.emit(self._buffer) | |
101 | if not self.is_connected(): | |
102 | return | |
103 | self._buffer.clear() | |
104 | if remainder: | |
105 | self._buffer.append(remainder) | |
106 | ||
107 | def _socket_disconnected(self): | |
108 | """Slot: socket disconnected.""" | |
109 | self._server = None | |
110 | self._port = None | |
111 | self._ssl = None | |
112 | self._password = "" | |
113 | self.statusChanged.emit(self.status_disconnected, None) | |
114 | ||
115 | def is_connected(self): | |
116 | """Return True if the socket is connected, False otherwise.""" | |
117 | return self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState | |
118 | ||
119 | def is_ssl(self): | |
120 | """Return True if SSL is used, False otherwise.""" | |
121 | return self._ssl | |
122 | ||
123 | def connect_weechat(self, server, port, ssl, password, lines): | |
124 | """Connect to WeeChat.""" | |
125 | self._server = server | |
126 | try: | |
127 | self._port = int(port) | |
128 | except ValueError: | |
129 | self._port = 0 | |
130 | self._ssl = ssl | |
131 | self._password = password | |
132 | try: | |
133 | self._lines = int(lines) | |
134 | except ValueError: | |
135 | self._lines = config.CONFIG_DEFAULT_RELAY_LINES | |
136 | if self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState: | |
137 | return | |
138 | if self._socket.state() != QtNetwork.QAbstractSocket.UnconnectedState: | |
139 | self._socket.abort() | |
140 | if self._ssl: | |
141 | self._socket.ignoreSslErrors() | |
142 | self._socket.connectToHostEncrypted(self._server, self._port) | |
143 | else: | |
144 | self._socket.connectToHost(self._server, self._port) | |
145 | self.statusChanged.emit(self.status_connecting, "") | |
146 | ||
147 | def disconnect_weechat(self): | |
148 | """Disconnect from WeeChat.""" | |
149 | if self._socket.state() == QtNetwork.QAbstractSocket.UnconnectedState: | |
150 | return | |
151 | if self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState: | |
152 | self.send_to_weechat('quit\n') | |
153 | self._socket.waitForBytesWritten(1000) | |
154 | else: | |
155 | self.statusChanged.emit(self.status_disconnected, None) | |
156 | self._socket.abort() | |
157 | ||
158 | def send_to_weechat(self, message): | |
159 | """Send a message to WeeChat.""" | |
160 | self._socket.write(message.encode('utf-8')) | |
161 | ||
162 | def desync_weechat(self): | |
163 | """Desynchronize from WeeChat.""" | |
164 | self.send_to_weechat('desync\n') | |
165 | ||
166 | def sync_weechat(self): | |
167 | """Synchronize with WeeChat.""" | |
168 | self.send_to_weechat('\n'.join(_PROTO_SYNC_CMDS)) | |
169 | ||
170 | def status_icon(self, status): | |
171 | """Return the name of icon for a given status.""" | |
172 | icon = { | |
173 | self.status_disconnected: 'dialog-close.png', | |
174 | self.status_connecting: 'dialog-close.png', | |
175 | self.status_connected: 'dialog-ok-apply.png', | |
176 | } | |
177 | return icon.get(status, '') | |
178 | ||
179 | def get_options(self): | |
180 | """Get connection options.""" | |
181 | return { | |
182 | 'server': self._server, | |
183 | 'port': self._port, | |
184 | 'ssl': 'on' if self._ssl else 'off', | |
185 | 'password': self._password, | |
186 | 'lines': str(self._lines), | |
187 | } |