2 # -*- coding: utf-8 -*-
4 # network.py - I/O with WeeChat/relay
6 # Copyright (C) 2011-2014 Sébastien Helleu <flashcode@flashtux.org>
8 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
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.
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.
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/>.
26 QtCore
= qt_compat
.import_module('QtCore')
27 QtNetwork
= qt_compat
.import_module('QtNetwork')
30 _PROTO_INIT_CMD
= ['init password=%(password)s']
33 '(listbuffers) hdata buffer:gui_buffers(*) number,full_name,short_name,'
34 'type,nicklist,title,local_variables',
36 '(listlines) hdata buffer:gui_buffers(*)/own_lines/last_line(-%(lines)d)/'
37 'data date,displayed,prefix,message',
39 '(nicklist) nicklist',
47 class Network(QtCore
.QObject
):
48 """I/O with WeeChat/relay."""
50 statusChanged
= qt_compat
.Signal(str, str)
51 messageFromWeechat
= qt_compat
.Signal(QtCore
.QByteArray
)
53 def __init__(self
, *args
):
54 QtCore
.QObject
.__init
__(*(self
,) + args
)
55 self
.status_disconnected
= 'disconnected'
56 self
.status_connecting
= 'connecting...'
57 self
.status_connected
= 'connected'
62 self
._lines
= config
.CONFIG_DEFAULT_RELAY_LINES
63 self
._buffer
= QtCore
.QByteArray()
64 self
._socket
= QtNetwork
.QSslSocket()
65 self
._socket
.connected
.connect(self
._socket
_connected
)
66 self
._socket
.error
.connect(self
._socket
_error
)
67 self
._socket
.readyRead
.connect(self
._socket
_read
)
68 self
._socket
.disconnected
.connect(self
._socket
_disconnected
)
70 def _socket_connected(self
):
71 """Slot: socket connected."""
72 self
.statusChanged
.emit(self
.status_connected
, None)
74 self
.send_to_weechat('\n'.join(_PROTO_INIT_CMD
+ _PROTO_SYNC_CMDS
)
75 % {'password': str(self
._password
),
76 'lines': self
._lines
})
78 def _socket_error(self
, error
):
79 """Slot: socket error."""
80 self
.statusChanged
.emit(
81 self
.status_disconnected
,
82 'Failed, error: %s' % self
._socket
.errorString())
84 def _socket_read(self
):
85 """Slot: data available on socket."""
86 data
= self
._socket
.readAll()
87 self
._buffer
.append(data
)
88 while len(self
._buffer
) >= 4:
90 length
= struct
.unpack('>i', self
._buffer
[0:4])[0]
91 if len(self
._buffer
) < length
:
92 # partial message, just wait for end of message
94 # more than one message?
95 if length
< len(self
._buffer
):
96 # save beginning of another message
97 remainder
= self
._buffer
[length
:]
98 self
._buffer
= self
._buffer
[0:length
]
99 self
.messageFromWeechat
.emit(self
._buffer
)
100 if not self
.is_connected():
104 self
._buffer
.append(remainder
)
106 def _socket_disconnected(self
):
107 """Slot: socket disconnected."""
111 self
._password
= None
112 self
.statusChanged
.emit(self
.status_disconnected
, None)
114 def is_connected(self
):
115 return self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
120 def connect_weechat(self
, server
, port
, ssl
, password
, lines
):
121 self
._server
= server
123 self
._port
= int(port
)
127 self
._password
= password
129 self
._lines
= int(lines
)
131 self
._lines
= config
.CONFIG_DEFAULT_RELAY_LINES
132 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
:
134 if self
._socket
.state() != QtNetwork
.QAbstractSocket
.UnconnectedState
:
136 self
._socket
.connectToHost(self
._server
, self
._port
)
138 self
._socket
.ignoreSslErrors()
139 self
._socket
.startClientEncryption()
140 self
.statusChanged
.emit(self
.status_connecting
, None)
142 def disconnect_weechat(self
):
143 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.UnconnectedState
:
145 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
:
146 self
.send_to_weechat('quit\n')
147 self
._socket
.waitForBytesWritten(1000)
149 self
.statusChanged
.emit(self
.status_disconnected
, None)
152 def send_to_weechat(self
, message
):
153 self
._socket
.write(message
.encode('utf-8'))
155 def desync_weechat(self
):
156 self
.send_to_weechat('desync\n')
158 def sync_weechat(self
):
159 self
.send_to_weechat('\n'.join(_PROTO_SYNC_CMDS
))
161 def status_icon(self
, status
):
162 icon
= {self
.status_disconnected
: 'dialog-close.png',
163 self
.status_connecting
: 'dialog-close.png',
164 self
.status_connected
: 'dialog-ok-apply.png'}
165 return icon
.get(status
, '')
167 def get_options(self
):
168 return {'server': self
._server
,
170 'ssl': 'on' if self
._ssl
else 'off',
171 'password': self
._password
,
172 'lines': str(self
._lines
)}