-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.py
127 lines (111 loc) · 4.7 KB
/
function.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
import socket
import sys
import threading
class Auth (threading.Thread) :
onlinelist = {}
flag_=True
user = {}
group = {}
def __init__ (self,idthread,client,lock,conn) :
threading.Thread.__init__(self)
self.idthread=idthread
self.client = client
self.conn = conn
self.lock = lock
def loadUser(self):
dict = {}
with open('userlist.txt','r') as file:
for line in file : #add each line to dictionary
username, password = line.split( )
dict[username] = password
file.close()
return dict
def loadGroup(self):
dict = {}
with open('grouplist.txt','r') as file:
for line in file : #add each line to dictionary
member = line.split(' ')
name_group = member[0]
del member[0]
dict[name_group] = member
file.close()
return dict
def run(self):
print ("Melakukan koneksi ke : " + str(self.client) + " (" + self.name + ")")
self.mainfunc()
print ("Melakukan koneksi ke : " + str(self.client) + " (" + self.name + ")")
def mainfunc(self) :
self.user = self.loadUser()
self.group= self.loadGroup()
logged = 0
while (1):
ask = self.conn.recv(1024).decode()
kata = ask.split(' ',2)
if kata[0]== 'LOGIN' and logged == 0 :
self.lock.acquire()
if kata[1] in self.user:
if kata[2] == self.user[kata[1]]:
self.onlinelist[kata[1]]=self.conn
logged = 1
self.temp=kata[1]
print("\nUser "+kata[1] " Berhasil Masuk!")
self.conn.send(b"Berhasil")
self.lock.release()
elif kata[0]== 'SIGNUP' and logged == 0 :
self.user[kata[1]] = kata[2]
self.lock.acquire()
file = open('userlist.txt','a')
toWrite = kata[1] + ' ' + kata[2] + '\n'
file.write(toWrite)
file.close()
print("\nBerhasil Mendaftar sebagai " + kata[1])
self.lock.release()
self.conn.send(b"berhasil")
elif kata[0]== 'SHOWONLINE' and logged == 1 :
nama=self.onlinelist.keys()
nama2='\n'.join(nama)
self.conn.send(nama2.encode() + b"\n------------------")
elif kata[0]== 'LOGOUT' and logged ==1 :
del self.onlinelist[self.temp]
self.conn.send(b"Terputus Koneksi")
break
elif kata[0]== 'WHISP':
if kata[1] in self.user and kata[1] != self.temp :
try :
target = self.onlinelist[kata[1]]
except KeyError :
self.conn.send(b"Tidak ditemukan user yang online")
else :
msg="Private :\nPesan dari " + self.temp + " : " + kata[2]
target.send(msg.encode())
self.conn.send(b"Berhasil dikirim")
elif kata[0]== 'BC':
for target in self.onlinelist.values() :
if target!=self.conn :
msg="Public : Pesan dari " + self.temp + " : " + kata[1]
target.send(msg.encode())
self.conn.send(b"Berhasil dikirim")
elif kata[0]== 'GROUPLIST':
if logged == 1:
self.lock.acquire()
nama = self.group.keys()
nama2 = '\n'.join(nama)
self.conn.send(nama2.encode() + b"\n------------------")
self.lock.release()
self.conn.send(b"Berhasil menampilkan")
# elif kata[0]== 'JOINGROUP'
#elif kata[0]== 'CHATGROUP'
#elif kata[0]== 'LISTGROUP'
elif kata[0]== 'MEMBERGROUP':
if logged ==1:
self.lock.acquire()
nama = self.group[kata[1]]
nama2 = '\n'.join(nama)
self.conn.send(nama2.encode() + b"\n------------------")
self.conn.send(b"\n------------------")
self.lock.release()
self.conn.send(b"Berhasil menampilkan")
#elif kata[0]== 'LEAVEGROUP':
else :
self.conn.send(b"Perintah tidak dikenali")
self.conn.close()