-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
99 lines (85 loc) · 3.14 KB
/
bot.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
import discord
from discord.ext import commands
from diffie_hellman import DiffieHellman
from aes import AESCipher
database = {
"token": None,
"users": {},
"chats": {},
}
bot = commands.Bot(command_prefix="")
def start_bot():
global bot
with open("dcToken.txt", "r") as f:
token = ''.join(f.readlines())
database['token'] = token
bot.run(token, bot=False)
async def reply(userID, message):
u = await bot.fetch_user(userID)
try:
await u.send(message)
except:
print("Erorr sending message:", user)
@bot.event
async def on_ready():
print("-" * 40)
print('Started the bot as:', bot.user)
print("BOT:", database)
users = bot.users
for user in users:
print(user)
df = DiffieHellman()
k1 = df.generate_k()
database['chats'][str(user.id)] = []
database['users'][str(user.id)] = {
'name': user.name,
'key': df
}
print("Sending to:", user)
u = await bot.fetch_user(user.id)
try:
await u.send(f'===KEYEXCH===\nvalue:{k1}')
except:
print("Erorr sending key-exchange to:", user)
print("-" * 40)
@bot.event
async def on_message(message):
#Ignore MSGs if they're not DMs
#TODO implement end-to-end group chatting
if message.author == bot.user or not isinstance(message.channel, discord.DMChannel):
return
#database['chats'][str(message.author.id)].append(message.content)
if message.content.startswith("===KEYEXCH==="):
# Perform a Diffie-Hellman key exchange
print(f"Key exchange started with {message.author.name}")
k2 = message.content.find("value:")
df = DiffieHellman()
k1 = df.generate_k()
key = df.generate_full_key(k2)
database['users'][str(message.author.id)]['key'] = df
print("Key with user: ", message.author.name, " is ", database['users'][str(message.author.id)]['key'].key)
await message.author.send(f"===KEYACK===\nvalue:{k1}")
elif message.content.startswith("===KEYACK==="):
value = message.content.find("value:")
k2 = message.content[value+6:]
df = database['users'][str(message.author.id)]['key']
df.generate_full_key(k2)
database['users'][str(message.author.id)]['key'] = df
print("Key with user: ", message.author.name, " is ", database['users'][str(message.author.id)]['key'].key)
elif message.content.startswith("===MSG==="):
# Recieve a message:
content = message.content[9:]
try:
key = database['users'][str(message.author.id)]['key'].key
aes = AESCipher( key )
decrypted = aes.decrypt(content)
except:
print("Error while decrypting message")
return
print(content)
print(decrypted)
# 0 - Sent by someone
# 1 - Sent by you
database['chats'][str(message.author.id)].append( [0, decrypted] )
if __name__ == "__main__":
start_bot()