-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientConnection.py
73 lines (66 loc) · 2.2 KB
/
ClientConnection.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
from Hooks import callHook
from Utils import Struct, sendLineToSocket
from threading import Thread
from Logging import log
class ClientCon:
"""docstring for ClientCon"""
def __init__(self, con, addr):
log(1, "New Client: " + addr)
self.con = con
self.file = con.makefile()
self.addr = addr
self.user = None
self.sentpass = false
self._hooks = []
Thread(target = self._listen).start()
def _listen(self):
callHook("newClient", self)
while True:
line = self.file.readline()
if line == "":
self._callHook("disconnect", None)
return
data = Struct()
data.line = line
data.con = self
data.passthough = True
self._callHook("in_raw", data)
if not data.passthough: # a module requested to not pass the command
continue
data = Struct()
t = line.split(" :")
data.argline = t[0]
data.args = data.argline.split()
if len(t) > 1:
data.data = line[len(data.argline) + 2:]
else:
data.data = None
data.passthough = True
self._callHook("in_DATA_" + data.args[0].upper(), data)
if data.passthough and not self.user is None:
self.user.sendToServer(args = data.args, data = data.data)
def _callHook(self, name, data):
for hook in self._hooks:
if name in hook:
hook[name](data)
def regHook(self, handler):
self._hooks.append(handler)
def sendLine(self, line):
data = Struct()
data.con = self
data.passthough = True
data.line = line
self._callHook("out_raw", data)
if data.passthough:
sendLineToSocket(self.con, line + "\r\n")
def send(self, args = [], data = None, prefix = None):
if prefix is None:
prefix = ":"
else:
prefix += ":"
argline = " ".join(args)
if data is None:
data = ""
else:
data = " :" + data
self.sendLine(prefix + argline + data)