-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatten.py
123 lines (74 loc) · 2.48 KB
/
chatten.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
import asyncio
import send
import register
from kademlia.network import Server
def new_user():
username = input("Please type in a username ")
new_userlist = loop.run_until_complete(node.get("userlist"))
if new_userlist is None:
new_userlist = " "
else:
new_userlist = new_userlist
if username in new_userlist:
print("Your username is taken, please choose a different username")
new_user()
else:
register.user(port, username)
return username
def new_chat():
chatname = input("Enter a name for your chat ")
new_chatlist = loop.run_until_complete(node.get("chatlist"))
if new_chatlist is None:
new_chatlist = " "
else:
new_chatlist = new_chatlist
if chatname in new_chatlist:
print("The chat is already existing. Please choose a different chatname")
new_chat()
else:
print("We are creating chat " + chatname)
register.chat(port, chatname)
loop.run_until_complete(node.set(chatname, " "))
return chatname
def join_chat():
print("You can join the following chats:")
chatlist = loop.run_until_complete(node.get("chatlist"))
if chatlist is None:
print("Their are no chats available. Please create a new one")
new_chat()
else:
print(chatlist)
joinchat = input("Which chat do you want to join? ")
if joinchat in chatlist:
print("Just type a message to join the chat")
return joinchat
else:
print("The chat is not existing.")
join_chat()
f = open('chat.txt','r')
stars = f.read()
print(stars)
port = input("Please enter a port ")
join = input("To create a new chat: Type 'new'\nTo join an exisitng chat: Type 'join'\n")
if join == "new":
# create node, listen to input port
node = Server()
node.listen(port)
loop = asyncio.get_event_loop()
loop.run_until_complete(node.bootstrap([("0.0.0.0", 8468)]))
username = new_user()
chatname = new_chat()
while 1:
message = input(username + ": >>> ")
send.send(port, chatname, username, message)
else:
# create node, listen to input port
node = Server()
node.listen(port)
loop = asyncio.get_event_loop()
loop.run_until_complete(node.bootstrap([("0.0.0.0", 8468)]))
username = new_user()
chatname = join_chat()
while 1:
message = input(username + ": >>> ")
send.send(port, chatname, username, message)