-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
107 lines (104 loc) · 2.84 KB
/
manager.py
File metadata and controls
107 lines (104 loc) · 2.84 KB
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
from hashlib import md5
class Topic:
def __init__(self,name):
self.uname = name
self.users = []
def send(self,msg):
for x in self.users:
try:
x.sock.send(msg)
except OSError:
self.users.remove(x)
def add(self,client):
if not client in self.users:
self.users.append(client)
return True
else:return False
def remove(self,client):
if client in self.users:
self.users.remove(client)
return True
return False
#the Manager and the chat never interact with the database directly
class Manager:
def __init__(self,t,u):
self.threads = {'base':Topic('base'),'base1':Topic('base1')}
self.users = {}
for x in t:
self.threads[x.name] = Topic(x.name)
for x in u:
self.users[x.name] = md5(x.password.encode()).hexdigest()
print(str(self.users))
def add_user(self,t):
#to be done by the outer server
if t[0] not in self.users:
self.users[t[0]] = t[1]
return True
return False
def add_thread(self,t):
#to be done by the outer server
if t not in self.threads:
self.threads[t[0]] = Topic(t[1])
return True
return True
def drop_user(self,uname):
#the user is his password initially, so if there's an object , it should be unsubscribed and
#removed from the line of active users
if not uname in self.users:return False
if not isinstance(self.users.get(uname),str):
usr = self.users.get(uname)
self.remove(usr)
usr.close()
del self.users[uname]
return True
def drop_thread(self,tname):
if self.threads.get(tname):
thread = self.threads.get(tname)
for x in thread.users:
self.unsubscribe(x)
else:return False
del self.threads[tname]
return True
def send(self,client,msg):
#send messages to all the users in the user thread , if there's not any , subscribe
#user to the base thread
thread = client.thread
if not thread:
self.threads['base'].add(client)
client.thread = 'base'
self.threads['base'].send(msg)
else:
self.threads[thread].send(msg)
def add(self,client):
#this is auth
if client.uname in self.users:
if isinstance(self.users[client.uname],str):
print("adding client...")
if self.users[client.uname] == client.password:
self.users[client.uname] = client
return True
return False
def remove(self,client):
#logout
if client.uname in self.users:
if client.thread:
self.threads[client.thread].remove(client)
self.users[client.uname] = client.password
def subscribe(self,client,t):
thread = self.threads.get(t)
if thread:
if client.thread:
try:
self.threads[client.thread].remove(client)
except KeyError:
pass
client.thread = thread.uname
return thread.add(client)
return False
def unsubscribe(self,client):
return self.subscribe(client,'base')
if client.thread:
client.thread = 'base'
self.thread['base'].add(client)
return self.threads[client.thread].remove(client)
return False