-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
248 lines (202 loc) · 7.86 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""
slixmppd backend server
"""
import html
import re
import logging
import stat
import os
from typing import TYPE_CHECKING, Dict, Optional, Tuple
# nuqql-based
from nuqql_based.based import Based
from nuqql_based.callback import Callback
from nuqql_based.message import Message
# slixmppd
from nuqql_slixmppd.client import BackendClient
if TYPE_CHECKING: # imports for typing
# pylint: disable=ungrouped-imports
from nuqql_based.account import Account # noqa
from nuqql_based.based import CallbackList
from nuqql_based.config import Config
# slixmppd version
VERSION = "0.8.3"
class BackendServer:
"""
Backend server class, manages the BackendClients for connections to
IM networks
"""
def __init__(self) -> None:
self.connections: Dict[int, BackendClient] = {}
self.based = Based("slixmppd", VERSION)
async def start(self) -> None:
"""
Start server
"""
# register callbacks
callbacks: "CallbackList" = [
# based events
(Callback.BASED_CONFIG, self._based_config),
(Callback.BASED_QUIT, self._based_quit),
# nuqql messages
(Callback.HELP_WELCOME, self._help_welcome),
(Callback.HELP_ACCOUNT_ADD, self._help_account_add),
(Callback.ADD_ACCOUNT, self.add_account),
(Callback.DEL_ACCOUNT, self.del_account),
(Callback.GET_BUDDIES, self.handle_command),
(Callback.SEND_MESSAGE, self.send_message),
(Callback.SET_STATUS, self.handle_command),
(Callback.GET_STATUS, self.handle_command),
(Callback.CHAT_LIST, self.handle_command),
(Callback.CHAT_JOIN, self.handle_command),
(Callback.CHAT_PART, self.handle_command),
(Callback.CHAT_SEND, self.chat_send),
(Callback.CHAT_USERS, self.handle_command),
(Callback.CHAT_INVITE, self.handle_command),
]
self.based.set_callbacks(callbacks)
# start based
await self.based.start()
async def handle_command(self, account: Optional["Account"], cmd: Callback,
params: Tuple) -> str:
"""
Helper for adding commands to the command queue of the account/client
"""
assert account
try:
xmpp = self.connections[account.aid]
except KeyError:
# no active connection
return ""
await xmpp.handle_command(cmd, params)
return ""
async def send_message(self, account: Optional["Account"], cmd: Callback,
params: Tuple) -> str:
"""
send a message to a jabber id on an account
"""
# parse parameters
if len(params) > 2:
dest, msg, msg_type = params
else:
dest, msg = params
msg_type = "chat"
# nuqql sends a html-escaped message; construct "plain-text" version
# and xhtml version using nuqql's message and use them as message body
# later
html_msg = f'<body xmlns="http://www.w3.org/1999/xhtml">{msg}</body>'
msg = html.unescape(msg)
msg = "\n".join(re.split("<br/>", msg, flags=re.IGNORECASE))
# send message
await self.handle_command(account, cmd, (dest, msg, html_msg,
msg_type))
return ""
async def chat_send(self, account: Optional["Account"], _cmd: Callback,
params: Tuple) -> str:
"""
Send message to chat on account
"""
chat, msg = params
return await self.send_message(account, Callback.SEND_MESSAGE,
(chat, msg, "groupchat"))
async def run_client(self, account: Optional["Account"]) -> None:
"""
Run client connection
"""
# start client connection
assert account
xmpp = BackendClient(account)
xmpp.register_plugin('xep_0071') # XHTML-IM
xmpp.register_plugin('xep_0082') # XMPP Date and Time Profiles
xmpp.register_plugin('xep_0203') # Delayed Delivery, time stamps
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0045') # Multi-User Chat
xmpp.register_plugin('xep_0199') # XMPP Ping
xmpp.connect()
# save client connection in active connections dictionary
self.connections[account.aid] = xmpp
async def add_account(self, account: Optional["Account"], _cmd: Callback,
_params: Tuple) -> str:
"""
Add a new account (from based) and run a new slixmpp client thread for
it
"""
# only handle xmpp accounts
assert account
if account.type != "xmpp":
return ""
# create and start thread
await self.run_client(account)
return ""
async def del_account(self, account: Optional["Account"], _cmd: Callback,
_params: Tuple) -> str:
"""
Delete an existing account (in based) and
stop slixmpp client thread for it
"""
# stop client
assert account
xmpp = self.connections[account.aid]
xmpp.shutdown = True
xmpp.disconnect()
await xmpp.disconnected
# cleanup
del self.connections[account.aid]
return ""
@staticmethod
def init_logging(config: "Config") -> None:
"""
Configure logging module, so slixmpp logs are written to a file
"""
# determine logging path from command line parameters and
# make sure it exists
logs_dir = config.get_dir() / "logs"
logs_dir.mkdir(parents=True, exist_ok=True)
log_file = logs_dir / "slixmpp.log"
# configure logging module to write to file
log_format = "%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s"
loglevel = config.get_loglevel()
logging.basicConfig(filename=log_file, level=loglevel,
format=log_format, datefmt="%s")
os.chmod(log_file, stat.S_IRWXU)
@staticmethod
async def _help_account_add(_account: Optional["Account"],
_cmd: Callback, _params: Tuple) -> str:
"""
Handle account add help event
"""
add_help = Message.info("You do not have any accounts configured.")
add_help += Message.info("You can add a new xmpp account with the "
"following command: "
"account add xmpp <jabber ID> <password>")
add_help += Message.info("Example: account add xmpp "
"dummy@jabber.org MyPassword")
return add_help
async def _help_welcome(self, _account: Optional["Account"],
_cmd: Callback, _params: Tuple) -> str:
"""
Handle welcome help message event
"""
welcome = Message.info(f"Welcome to nuqql-slixmppd v{VERSION}!")
welcome += Message.info("Enter \"help\" for a list of available "
"commands and their help texts")
if self.based.config.get_push_accounts():
welcome += Message.info("Listing your accounts:")
return welcome
async def _based_config(self, _account: Optional["Account"],
_cmd: Callback, params: Tuple) -> str:
"""
Config event in based
"""
config = params[0]
self.init_logging(config)
return ""
async def _based_quit(self, _account: Optional["Account"], _cmd: Callback,
_params: Tuple) -> str:
"""
Based shut down event
"""
for xmpp in self.connections.values():
xmpp.shutdown = True
xmpp.disconnect()
await xmpp.disconnected
return ""