-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMain.py
126 lines (117 loc) · 3.69 KB
/
Main.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
# -*- coding: utf-8 -*-
import os
import sys
import string
import base64
import md5
import time
import ParseConfig
import thread
import Plugin
import traceback
from colors import *
import Client
import binascii
class MainApp:
firstconnect = 1
er = 0
configfile = ""
reg = False
connected = False
def PingLoop(self):
while self.er == 0:
self.tasclient.ping()
time.sleep(10)
raise SystemExit(0)
def onlogin(self,socket):
if self.firstconnect == 1:
thread.start_new_thread(self.tasclient.mainloop,())
thread.start_new_thread(self.PingLoop,())
self.firstconnect = 0
#self.tasclient.events.ondisconnected = self.ph.ondisconnected
self.tasclient.events.onmotd = self.ph.onmotd
self.tasclient.events.onsaid = self.ph.onsaid
self.tasclient.events.onsaidex = self.ph.onsaidex
self.tasclient.events.onsaidprivate = self.ph.onsaidprivate
self.tasclient.events.onpong = self.ph.onpong
self.tasclient.events.oncommandfromserver = self.ph.oncommandfromserver
self.tasclient.events.ondisconnected = self.ph.ondisconnected
self.ph.onloggedin(socket)
self.ph.oncommandfromserver("ACCEPTED",[],self.tasclient.sock)
self.connected = True
good("Logged in")
def SaveConfig(self):
ParseConfig.writeconfigfile(self.configfile,self.config)
def isAdmin(self,username):
if username in self.admins:
return True
elif username in self.tasclient.users:
if "#"+str(self.tasclient.users[username].id) in self.admins:
return True
else:
return False
else:
return False
def Dologin(self):
if self.tasclient.fl.register:
notice("Not logging in because a registration is in progress")
return
if self.verbose:
notice("Logging in...")
m = md5.new()
m.update(self.config["password"])
phash = base64.b64encode(binascii.a2b_hex(m.hexdigest()))
self.tasclient.login(self.config["nick"],phash,"Newbot",2400,self.config["lanip"] if "lanip" in self.config else "*")
def Register(self,username,password):
m = md5.new()
m.update(self.config["password"])
self.tasclient.register(self.config["nick"],base64.b64encode(binascii.a2b_hex(m.hexdigest())))
def destroy(self):
self.tasclient.er = 1
self.er = 1
raise SystemExit(0)
def ReloadConfig(self):
self.config = ParseConfig.readconfigfile(self.configfile)
self.admins = ParseConfig.parselist(self.config["admins"],",")
def run(self,configfile,register=False,verbose=False):
self.cwd = os.getcwd()
self.ph = Plugin.plghandler(self)
self.configfile = configfile
self.config = ParseConfig.readconfigfile(configfile)
self.admins = ParseConfig.parselist(self.config["admins"],",")
self.verbose = verbose
self.tasclient = Client.tasclient(self)
for p in ParseConfig.parselist(self.config["plugins"],","):
self.ph.addplugin(p,self.tasclient)
self.tasclient.events.onconnectedplugin = self.ph.onconnected
self.tasclient.events.onconnected = self.Dologin
self.tasclient.events.onloggedin = self.onlogin
self.reg = register
notice("Connecting to %s:%i" % (self.config["serveraddr"],int(self.config["serverport"])))
self.tasclient.connect(self.config["serveraddr"],int(self.config["serverport"]))
inst = MainApp()
cf = "Main.conf"
i = 0
r = False
try:
for arg in sys.argv:
if arg.strip() == "-c":
cf = sys.argv[i+1]
if arg.strip() == "-r":
r = True
notice("Registering account")
i += 1
inst.run(cf,r,True)
while 1:
time.sleep(10)
except SystemExit:
raise SystemExit(0)
except KeyboardInterrupt:
error("SIGINT, Exiting")
inst.ph.onexit()
exit(0)
except:
error("parsing command line")
print '-'*60
traceback.print_exc(file=sys.stdout)
print '-'*60