forked from TISUnion/ChatBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatBridge_client.py
336 lines (283 loc) · 8.76 KB
/
ChatBridge_client.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# -*- coding: UTF-8 -*-
import copy
import json
import os
import socket
import sys
import time
from threading import Lock
from typing import Optional
try:
from ChatBridgeLibrary import ChatBridge_lib as lib
from ChatBridgeLibrary import ChatBridge_utils as utils
except ImportError: # as a MCD plugin
sys.path.append("plugins/")
from ChatBridgeLibrary import ChatBridge_lib as lib
from ChatBridgeLibrary import ChatBridge_utils as utils
Prefix = '!!ChatBridge'
ConfigFile = 'ChatBridge_client.json'
LogFile = 'ChatBridge_client.log'
HelpMessage = '''------MCD ChatBridge插件 v0.1------
一个跨服聊天客户端插件
§a【格式说明】§r
§7''' + Prefix + '''§r 显示帮助信息
§7''' + Prefix + ''' status§r 显示ChatBridge客户端状态
§7''' + Prefix + ''' reload§r 重新加载ChatBridge客户端配置文件
§7''' + Prefix + ''' start§r 开启ChatBridge客户端状态
§7''' + Prefix + ''' stop§r 关闭ChatBridge客户端状态
'''
class Mode():
Client = 'Client'
MCD = 'MCD'
Discord = 'Discord'
CoolQ = 'CoolQ'
class ChatClient(lib.ChatClientBase):
def __init__(self, configFile, LogFile, mode):
js = json.load(open(configFile, 'r'))
super(ChatClient, self).__init__(lib.ChatClientInfo(js['name'], js['password']), js['aes_key'], LogFile)
self.mode = mode
self.consoleOutput = mode != Mode.MCD
self.server_addr = (js['server_hostname'], js['server_port'])
self.log('Client Info: name = ' + self.info.name + ', password = ' + self.info.password)
self.log('Mode = ' + mode)
self.log('AESKey = ' + self.AESKey)
self.log('Server address = ' + utils.addressToString(self.server_addr))
self.minecraftServer = None
self.start_lock = Lock()
def start(self, minecraftServer=None):
acq = self.start_lock.acquire(False)
if not acq:
return
try:
self.minecraftServer = minecraftServer
if not self.isOnline():
self.log('Trying to start the client, connecting to ' + utils.addressToString(self.server_addr))
self.sock = socket.socket()
# 发送客户端信息
try:
self.sock.settimeout(5)
self.sock.connect(self.server_addr)
self.send_login(self.info.name, self.info.password)
except socket.error:
self.log('Fail to connect to the server')
return
# 获取登录结果
try:
data = self.recieveData(timeout=5)
result = json.loads(data)['result']
except socket.error:
self.log('Fail to receive login result')
return
except ValueError:
self.log('Fail to read login result')
return
self.log(utils.stringAdd('Result: ', result))
if result == 'login success':
super(ChatClient, self).start()
else:
self.log('Client has already been started')
finally:
self.start_lock.release()
def on_recieve_message(self, data):
messages = utils.messageData_to_strings(data)
for msg in messages:
self.log(msg)
if self.mode == Mode.MCD:
self.minecraftServer.execute('tellraw @a {}'.format(json.dumps({
'text': msg,
'color': 'gray'
})))
def on_recieve_command(self, data):
ret = copy.deepcopy(data)
command = data['command'] # type: str
result = {'responded': True}
if command.startswith('!!stats '):
stats = None
if self.mode == Mode.MCD:
stats = self.minecraftServer.get_plugin_instance('stats_helper') # MCDR 1.0+
if stats is not None:
trimmed_command = command.replace('-bot', '').replace('-all', '')
try:
prefix, typ, cls, target = trimmed_command.split()
assert typ == 'rank' and type(target) is str
except:
res_raw = None
else:
res_raw = stats.show_rank(None, None, cls, target, '-bot' in command, False, '-all' in command, True)
if res_raw is not None:
lines = res_raw.splitlines()
stats_name = lines[0]
res = '\n'.join(lines[1:])
result['type'] = 0
result['stats_name'] = stats_name
result['result'] = res
else:
result['type'] = 1
else:
result['type'] = 2
elif command == '!!online': # MCDR -> bungeecord rcon
if self.minecraftServer is not None and hasattr(self.minecraftServer, 'MCDR') and self.minecraftServer.is_rcon_running():
res = self.minecraftServer.rcon_query('glist')
if res != None:
result['type'] = 0
result['result'] = res
else:
result['type'] = 1
else:
result['type'] = 2
ret['result'] = result
ret_str = json.dumps(ret)
self.log('Command received, responding {}'.format(ret_str))
self.sendData(ret_str)
def sendChatMessage(self, player, message):
self.log('Sending chat message "' + str((player, message)) + '" to the server')
self.send_message(self.info.name, player, message)
def sendMessage(self, message):
self.log('Sending message "' + message + '" to the server')
self.send_message(self.info.name, '', message)
# ------------------
# | MCDR Part Start |
# ------------------
PLUGIN_METADATA = {
'id': 'chatbridge_client',
'version': '1.0.0',
'name': 'ChatBridge_client',
'author': 'Fallen_Breath',
'link': 'https://github.com/TISUnion/ChatBridge',
# 'dependencies': {
# 'mcdreforged': '>=1.0.2'
# }
}
def thread_spam(func):
# import here to avoid dependency restriction outside MCDR
# only works for MCDR 1.0+
from mcdreforged.api.decorator import new_thread
return new_thread('ChatBridge')(func)
def printLines(server, info, msg, isTell = True):
for line in msg.splitlines():
if info.is_player:
if isTell:
server.tell(info.player, line)
else:
server.say(line)
else:
print(line)
def setMinecraftServerAndStart(server):
global client
if client == None:
reloadClient()
if not client.isOnline():
client.start(server)
def startClient(server, info):
printLines(server, info, '正在开启ChatBridge客户端')
setMinecraftServerAndStart(server)
time.sleep(1)
def stopClient(server, info):
printLines(server, info, '正在关闭ChatBridge客户端')
global client
if client == None:
reloadClient()
client.stop(True)
time.sleep(1)
def showClientStatus(server, info):
global client
printLines(server, info, 'ChatBridge客户端在线情况: ' + str(client.isOnline()))
def on_user_info(server, info):
global client
content = info.content
command = content.split()
if len(command) == 0 or command[0] != Prefix:
if info.is_player:
client.log('Sending message "' + str((info.player, info.content)) + '" to the server')
client.sendChatMessage(info.player, info.content)
return
del command[0]
cmdLen = len(command)
@thread_spam
def processInfo():
if cmdLen == 0:
printLines(server, info, HelpMessage)
return
if cmdLen == 1 and command[0] == 'status':
showClientStatus(server, info)
elif cmdLen == 1 and command[0] == 'reload':
stopClient(server, info)
reloadClient()
startClient(server, info)
showClientStatus(server, info)
elif cmdLen == 1 and command[0] == 'start':
startClient(server, info)
showClientStatus(server, info)
elif cmdLen == 1 and command[0] == 'stop':
stopClient(server, info)
showClientStatus(server, info)
else:
printLines(server, info, HelpMessage)
processInfo()
def threadedSendMessage(message: str):
@thread_spam
def inner():
global client
client.sendMessage(message)
inner()
def on_player_joined(server, playername, info):
threadedSendMessage(playername + ' joined ' + client.info.name)
def on_player_left(server, playername):
threadedSendMessage(playername + ' left ' + client.info.name)
def on_load(server, old):
server.register_help_message(Prefix, '跨服聊天控制')
setMinecraftServerAndStart(server)
# only in MCDR 0.x
def on_death_message(server, message):
global client
client.sendMessage(message)
def on_server_startup(server):
threadedSendMessage('Server has started up')
def on_server_stop(server, return_code):
threadedSendMessage('Server stopped')
def _stop():
@thread_spam
def inner():
global client
if client is not None:
client.stop()
inner()
def on_unload(server):
_stop()
def on_remove(server):
_stop()
def on_mcdr_stop(server):
_stop()
# -------------------------
# | MCDR Compatibility End |
# -------------------------
def reloadClient():
global ConfigFile, LogFile, client, mode
if mode is None:
if __name__ == '__main__':
mode = Mode.Client
else:
mode = Mode.MCD
ConfigFile = 'config/' + ConfigFile
LogFile = 'logs/' + LogFile
client = ChatClient(ConfigFile, LogFile, mode)
client = None # type: Optional[ChatClient]
mode = None
if __name__ == '__main__':
mode = Mode.Client
print('[ChatBridge] Config File = ' + ConfigFile)
if not os.path.isfile(ConfigFile):
print('[ChatBridge] Config File not Found, exiting')
exit(1)
if mode == Mode.Client:
reloadClient()
client.start(None)
while True:
# noinspection PyUnresolvedReferences
msg = raw_input() if sys.version_info.major == 2 else input()
if msg == 'stop':
client.stop(True)
elif msg == 'start':
client.start(None)
else:
client.sendMessage(msg)