-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSuperbot.py
executable file
·211 lines (168 loc) · 5.48 KB
/
Superbot.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/python
from twisted.words.protocols import irc
from twisted.internet import protocol,reactor
import sys,traceback,os
from time import time
from Colors import *
#
# Consume the error and display an entry in the log
#
def onError():
et, ev, tb = sys.exc_info()
t = time()
red = "\x1b\x5b1;31;40m"
regular= "\x1b\x5b0;37;40m"
print "========="+red+"ERROR"+regular+"========"
print "Time :",t
last = []
skipped= 0
while tb :
co = tb.tb_frame.f_code
filename = str(co.co_filename)
line_no = str(traceback.tb_lineno(tb))
tb = tb.tb_next
if last!=[filename,line_no]:
if skipped!=0:
print "... Skipped",skipped,"repeat(s)."
print "File :",filename
print "Line :",line_no
print "------"
last=[filename,line_no]
skipped = 0
else:
skipped += 1
if skipped!=0:
print "... Skipped",skipped,"repeats(s)."
print "Error:", ev
print "======================="
pass
class SuperBot(irc.IRCClient, object):
def __init__(self):
global CONFIG
self.config = CONFIG
self.plugins=[]
self.nickname=CONFIG["nick"]
super(SuperBot,self).__init__()
global ircColor
self.ircColor=ircColor
for i in CONFIG["plugins"]:
print "Loading",i,
for n in range(len(i),20):print "",
if self.loadPlugin(i):
print "[ OK ]"
else:
print "[FAILED]"
def signedOn(self):
print "Signed on"
print "Joining default channels:"
for i in CONFIG["channels"]:
print " <%s>"%(i,)
self.join(i)
def handleCommand(self,cmd,prefix,params):
super(SuperBot, self).handleCommand(cmd,prefix,params)
print "---NORM---"
print "CMD =",cmd
print "PRE =",prefix
print "PAR =",params
print "----------"*2
for i in self.plugins:
try:
if hasattr(sys.modules[i],"on_"+cmd):
getattr(sys.modules[i],"on_"+cmd)(self,prefix,params)
except Exception as E:
if hasattr(sys.modules[i],"on_error"):
getattr(sys.modules[i],"on_error")(E)
else:
onError()
def ctcpQuery(self,user,chan,mess):
super(SuperBot,self).ctcpQuery(user,chan,mess)
cmd = mess[0][0]
params=mess[0][1]
print "---CTCP---"
print cmd
print user
print [chan,params]
print "----------"*2
for i in self.plugins:
try:
if hasattr(sys.modules[i],"on_"+cmd):
getattr(sys.modules[i],"on_"+cmd)(self,user,[chan,params])
except:
onError()
def loadPlugin(self,name):
print "Loading",name
if self.plugins.count(name)!=0:
print "Plugin Found!, not loading."
return False
try:
print "Plugin not loaded, attempting to load"
mod = __import__(name)
print "mod =",mod
try:
if hasattr(mod,"on_load"):
mod.on_load(self);
except:
onError()
self.plugins.append(name)
print self.plugins
return True
except:
onError()
return False
def hasPlugin(self,name):
return self.plugins.count(name)!=0
def getPlugins(self):
return self.plugins
def unloadPlugin(self,name):
print "unloadPlugin",name
if self.plugins.count(name)!=0:
mod = sys.modules[name]
print "mod =",mod
try:
if hasattr(mod,"on_unload"):
mod.on_unload(self);
except:
onError()
print sys.modules[name]
del sys.modules[name]
self.plugins.remove(name)
print mod.__file__
try:
os.remove(mod.__file__+"c")
except:
onError()
return True
return False
def loadSettings(config):
attrs = ["server","port","nick","plugins","channels","pluginDir"]
listAttrs=["plugins","channels","pluginDir"]
try:
settings = dict(l.split(None,1) for l in file(config,"r").readlines())
for i in attrs:
if not settings.has_key(i):
raise Exception("Configure error: '%s' not specified." % (i) )
settings[i] = settings[i].strip()
if i in listAttrs:
settings[i]=settings[i].split()
except:
onError()
return
try:
port = int(settings["port"])
settings["port"]=port
except:
raise Exception("Invalid port number")
if port < 1 or port > 65535:
raise Exception("Port value out of range")
print "Configuration:"
for i in attrs:
print i,"=>",settings[i]
return settings
class SuperBotFactory(protocol.ReconnectingClientFactory):
protocol=SuperBot
CONFIG = loadSettings("Config")
if __name__ == "__main__" and CONFIG:
for i in CONFIG["pluginDir"]:
sys.path.append(i)
reactor.connectTCP(CONFIG["server"],CONFIG["port"],SuperBotFactory())
reactor.run()