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
|
import discord
import asyncio
import argparse
import socket
import threading
import random
from threading import Lock
import sys
import time
################################################################################
## MAIN ########################################################################
################################################################################
parser = argparse.ArgumentParser(
description = (
"Discord Client for JabberHive"
)
)
parser.add_argument(
'-d',
'--destination',
type = str,
help = 'UNIX socket this client connects to.',
)
parser.add_argument(
'-t',
'--token',
type = str,
help = 'Discord token.',
)
parser.add_argument(
'-u',
'--username-chance',
dest='username_chance',
type = int,
help = 'Chance [0-100] to focus on username instead.',
)
parser.add_argument(
'-c',
'--print-chat',
dest='print_chat',
help = 'Prints all messages',
action="store_true",
)
args = parser.parse_args()
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client = discord.Client()
server_mutex = Lock()
already_disconnected = False
def get_jh_reply ():
global server
server_mutex
is_done = False
result = ""
jh_reply = b""
matched = 0
while not is_done:
c = b"\0"
jh_reply = b""
while (c != b"\n"):
c = server.recv(1)
jh_reply += c
if ((jh_reply == b"!P \n") or (jh_reply == b"!N \n")):
is_done = True
else:
jh_reply = jh_reply.decode("UTF-8", "ignore")
if (jh_reply.startswith("!GR ")):
result = jh_reply[4:]
result = result[:-1]
return result
@client.event
async def on_disconnect ():
global already_disconnected
if (not already_disconnected):
print('Disconnecting from JH network from on_disconnect?!')
time.sleep(10)
server.close()
sys.exit("l.94")
@client.event
async def on_connect ():
global args
global client
global server
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
try:
server.connect(args.destination)
except Exception as exception:
print('Could not connect to JH network: ' + str(exception))
time.sleep(10)
client.close()
sys.exit("l.113")
@client.event
async def on_message(message):
global server
global args
global server_mutex
if (message.author.id == client.user.id):
return
has_lock = False
try:
msg_encoded = message.clean_content.encode('utf-8')
msg = msg_encoded.replace(b'\n', b' ')
server_mutex.acquire()
has_lock = True
# if (random.randint(1, 100) <= args.username_chance):
# server.sendall(b"?RL " + msg + b"\n")
# result = get_jh_reply()
# msg = (message.author.display_name.encode('utf-8'))
# server.sendall(b"?RR " + msg + b"\n")
# else:
# server.sendall(b"?RLR " + msg + b"\n")
#
# result = get_jh_reply()
# [DIRTY HACK]
server.sendall(b"?RLR " + msg + b"\n")
result = get_jh_reply()
if (
(len(result) > 0)
and (random.randint(1, 100) <= args.username_chance)
):
msg = (message.author.display_name.encode('utf-8'))
server.sendall(b"?RR " + msg + b"\n")
new_result = get_jh_reply().strip()
if (new_result != msg.decode('UTF-8').lower()):
print(new_result)
print(msg)
result = new_result
# [/DIRTY HACK]
server_mutex.release()
has_lock = False
if (args.print_chat):
print(
str(message.guild)
+ "#"
+ str(message.channel.name)
+ " <"
+ str(message.author.name)
+ "> "
+ str(msg_encoded)
)
if (len(result) > 0):
print("#" + str(message.channel.name) + " <- " + str(result.encode('utf-8')))
if (len(result) > 0):
await message.channel.send(result)
except Exception as exception:
if (has_lock):
server_mutex.release()
print(exception)
time.sleep(10)
server.close()
client.close()
sys.exit("l.189")
client.run(args.token)
|