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/>.
24 from qweechat
import config
25 from PySide6
import QtCore
, QtNetwork
26 from PySide6
.QtCore
import Signal
29 _PROTO_INIT_CMD
= ['init password=%(password)s']
32 '(listbuffers) hdata buffer:gui_buffers(*) number,full_name,short_name,'
33 'type,nicklist,title,local_variables',
35 '(listlines) hdata buffer:gui_buffers(*)/own_lines/last_line(-%(lines)d)/'
36 'data date,displayed,prefix,message',
38 '(nicklist) nicklist',
46 class Network(QtCore
.QObject
):
47 """I/O with WeeChat/relay."""
49 statusChanged
= Signal(str, str)
50 messageFromWeechat
= Signal(QtCore
.QByteArray
)
52 def __init__(self
, *args
):
53 super().__init
__(*args
)
54 self
.status_disconnected
= 'disconnected'
55 self
.status_connecting
= 'connecting...'
56 self
.status_connected
= 'connected'
61 self
._lines
= config
.CONFIG_DEFAULT_RELAY_LINES
62 self
._buffer
= QtCore
.QByteArray()
63 self
._socket
= QtNetwork
.QSslSocket()
64 self
._socket
.connected
.connect(self
._socket
_connected
)
65 # self._socket.error.connect(self._socket_error)
66 self
._socket
.readyRead
.connect(self
._socket
_read
)
67 self
._socket
.disconnected
.connect(self
._socket
_disconnected
)
69 def _socket_connected(self
):
70 """Slot: socket connected."""
71 self
.statusChanged
.emit(self
.status_connected
, None)
72 print('Connected, now sending password.')
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].data())[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."""
112 self
.statusChanged
.emit(self
.status_disconnected
, None)
114 def is_connected(self
):
115 """Return True if the socket is connected, False otherwise."""
116 return self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
119 """Return True if SSL is used, False otherwise."""
122 def connect_weechat(self
, server
, port
, ssl
, password
, lines
):
123 """Connect to WeeChat."""
124 self
._server
= server
125 print(f
'Connecting to server {self._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 print('Got SSL connection')
146 self
.statusChanged
.emit(self
.status_connecting
, "")
148 def disconnect_weechat(self
):
149 """Disconnect from WeeChat."""
150 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.UnconnectedState
:
152 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
:
153 self
.send_to_weechat('quit\n')
154 self
._socket
.waitForBytesWritten(1000)
156 self
.statusChanged
.emit(self
.status_disconnected
, None)
159 def send_to_weechat(self
, message
):
160 """Send a message to WeeChat."""
161 self
._socket
.write(message
.encode('utf-8'))
163 def desync_weechat(self
):
164 """Desynchronize from WeeChat."""
165 self
.send_to_weechat('desync\n')
167 def sync_weechat(self
):
168 """Synchronize with WeeChat."""
169 self
.send_to_weechat('\n'.join(_PROTO_SYNC_CMDS
))
171 def status_icon(self
, status
):
172 """Return the name of icon for a given status."""
174 self
.status_disconnected
: 'dialog-close.png',
175 self
.status_connecting
: 'dialog-close.png',
176 self
.status_connected
: 'dialog-ok-apply.png',
178 return icon
.get(status
, '')
180 def get_options(self
):
181 """Get connection options."""
183 'server': self
._server
,
185 'ssl': 'on' if self
._ssl
else 'off',
186 'password': self
._password
,
187 'lines': str(self
._lines
),