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