1 # -*- coding: utf-8 -*-
3 # network.py - I/O with WeeChat/relay
5 # Copyright (C) 2011-2021 Sébastien Helleu <flashcode@flashtux.org>
7 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
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.
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.
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/>.
25 from PySide6
import QtCore
, QtNetwork
27 from qweechat
import config
31 _PROTO_INIT_CMD
= ['init password=%(password)s']
34 '(listbuffers) hdata buffer:gui_buffers(*) number,full_name,short_name,'
35 'type,nicklist,title,local_variables',
37 '(listlines) hdata buffer:gui_buffers(*)/own_lines/last_line(-%(lines)d)/'
38 'data date,displayed,prefix,message',
40 '(nicklist) nicklist',
48 class Network(QtCore
.QObject
):
49 """I/O with WeeChat/relay."""
51 statusChanged
= QtCore
.Signal(str, str)
52 messageFromWeechat
= QtCore
.Signal(QtCore
.QByteArray
)
54 def __init__(self
, *args
):
55 super().__init
__(*args
)
56 self
.status_disconnected
= 'disconnected'
57 self
.status_connecting
= 'connecting...'
58 self
.status_connected
= 'connected'
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
)
71 def _socket_connected(self
):
72 """Slot: socket connected."""
73 self
.statusChanged
.emit(self
.status_connected
, None)
75 self
.send_to_weechat('\n'.join(_PROTO_INIT_CMD
+ _PROTO_SYNC_CMDS
)
76 % {'password': str(self
._password
),
77 'lines': self
._lines
})
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())
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:
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
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():
105 self
._buffer
.append(remainder
)
107 def _socket_disconnected(self
):
108 """Slot: socket disconnected."""
113 self
.statusChanged
.emit(self
.status_disconnected
, None)
115 def is_connected(self
):
116 """Return True if the socket is connected, False otherwise."""
117 return self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
120 """Return True if SSL is used, False otherwise."""
123 def connect_weechat(self
, server
, port
, ssl
, password
, lines
):
124 """Connect to WeeChat."""
125 self
._server
= server
127 self
._port
= int(port
)
131 self
._password
= password
133 self
._lines
= int(lines
)
135 self
._lines
= config
.CONFIG_DEFAULT_RELAY_LINES
136 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
:
138 if self
._socket
.state() != QtNetwork
.QAbstractSocket
.UnconnectedState
:
141 self
._socket
.ignoreSslErrors()
142 self
._socket
.connectToHostEncrypted(self
._server
, self
._port
)
144 self
._socket
.connectToHost(self
._server
, self
._port
)
145 self
.statusChanged
.emit(self
.status_connecting
, "")
147 def disconnect_weechat(self
):
148 """Disconnect from WeeChat."""
149 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.UnconnectedState
:
151 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
:
152 self
.send_to_weechat('quit\n')
153 self
._socket
.waitForBytesWritten(1000)
155 self
.statusChanged
.emit(self
.status_disconnected
, None)
158 def send_to_weechat(self
, message
):
159 """Send a message to WeeChat."""
160 self
._socket
.write(message
.encode('utf-8'))
162 def desync_weechat(self
):
163 """Desynchronize from WeeChat."""
164 self
.send_to_weechat('desync\n')
166 def sync_weechat(self
):
167 """Synchronize with WeeChat."""
168 self
.send_to_weechat('\n'.join(_PROTO_SYNC_CMDS
))
170 def status_icon(self
, status
):
171 """Return the name of icon for a given status."""
173 self
.status_disconnected
: 'dialog-close.png',
174 self
.status_connecting
: 'dialog-close.png',
175 self
.status_connected
: 'dialog-ok-apply.png',
177 return icon
.get(status
, '')
179 def get_options(self
):
180 """Get connection options."""
182 'server': self
._server
,
184 'ssl': 'on' if self
._ssl
else 'off',
185 'password': self
._password
,
186 'lines': str(self
._lines
),