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