-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerConnection.py
85 lines (77 loc) · 2.73 KB
/
ServerConnection.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
from Hooks import callHook
from Utils import Struct, sendLineToSocket
from threading import Thread
from Logging import *
class ServerCon:
"""docstring for ServerCon"""
def __init__(self, con, serv, user):
self.con = con
self.file = con.makefile()
self.user = user
self.serv = serv
self._hooks = []
self._loggedin = False
Thread(target = self._listen).start()
def _listen(self):
callHook("newServer", self)
while True:
line = self.file.readline().replace("\n", "").replace("\r", "")
if not self._loggedin:
self._login()
self._loggedin = True
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.prefix = t[0]
t = line[len(data.prefix) + 1:].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[1].upper(), data)
if data.passthough:
self.user.broadcastToClients(args = data.args, data = data.data, prefix = data.prefix)
def _login(self):
log(Level.DEBUG, "Logging in...")
log("Test")
if not self.serv['pass'] is None:
log("Test")
self.send(args = ["PASS", self.serv['pass']])
self.send(args = ["NICK", self.user.nick], data = None)
self.send(args = ["USER", self.user.data['ident'], "0", self.serv['host']], data = self.user.data['realname'])
log(Level.DEBUG, "Done")
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):
log("Test")
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):
log(Level.DEBUG, "Test")
argline = " ".join(args)
if data is None:
data = ""
else:
data = " :" + data
self.sendLine(argline + data)