forked from AgentAntelope/PIbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpibot_classes.py
executable file
·269 lines (263 loc) · 6.96 KB
/
pibot_classes.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import sys
import os
from pibot_constants import *
sys.path.append("/home/pimaster/www/pibot/")
import u413lib
def isvowel(letter):
if letter=='a' or letter=='e' or letter=='i' or letter=='o' or letter=='u':
return True
return False
class module:
#default values
name=""
modtype=""
description=""
version=""
func=0
disabled=False
def enable(self):
if not self.disabled:
return self.name+" "+self.modtype+" is not disabled."
self.disabled=False
return self.name+" "+self.modtype+" is now enabled."
def disable(self):
if self.disabled:
return self.name+" "+self.modtype+" is already disabled."
self.disabled=True
return self.name+" "+self.modtype+" is now disabled."
class command(module):
#default values
parameters=""
level=user.basic
modtype="command"
initexist=False
def __init__(self,filename):
mod=__import__(filename)
self.name=mod.name
self.parameters=mod.parameters
self.description=mod.description
self.level=mod.level
self.version=mod.version
self.func=mod.func
try:
self.init=getattr(mod,"init")
self.initexist=True
except:
pass
def run(self,bot,text,args):
if self.disabled and not bot.userlvl(text["User"])>self.level:
return CK+self.name+' '+self.modtype+" is disabled."
return self.func(bot,text,args)
class service(module):
#default values
modtype="service"
initexist=False
def __init__(self,filename):
mod=__import__(filename)
self.name=mod.name
self.description=mod.description
self.version=mod.version
self.infunc=mod.infunc
self.outfunc=mod.outfunc
try:
self.init=getattr(mod,"init")
self.initexist=True
except:
pass
class mode(module):
#default values
level=user.basic
state=False
modtype="mode"
initexist=False
def on(self):
if self.disabled:
return self.name+" mode is disabled."
if self.state:
return self.name+" mode is already on."
self.state=True
return self.name+" mode is now on."
def off(self):
if self.disabled:
return self.name+" mode is disabled."
if not self.state:
return self.name+" mode is already off."
self.state=False
return self.name+" mode is now off."
def toggle(self):
if self.disabled:
return self.name+" mode is disabled."
if self.state:
self.state=False
return self.name+" mode is now off."
self.state=True
return self.name+" mode is now on."
def __init__(self,filename):
mod=__import__(filename)
self.name=mod.name
self.description=mod.description
self.version=mod.version
self.func=mod.func
self.level=mod.level
try:
self.init=getattr(mod,"init")
self.initexist=True
except:
pass
def run(self,bot,text):
return self.func(bot,text)
class bot:
dologout=False
username=""
password=""
version=""
description=""
channels=[]#list of (chat,chatname)
#modules
modes=[]
services=[]
commands=[]
#user data
admins=[]
banned=[]
bots=[]
mods=[]
def __init__(self,username,password,version,description,channel="general"):
self.username=username
self.password=password
self.version=version
self.description=description
self.client=u413lib.createclient()
if not self.client.login(username,password):
exit()
self.chat=self.client.joinchat(channel)
self.client.sendRawCommand("channel "+channel)
#admins
txtfile=open(www+"data/admins.txt","r")
string=txtfile.read().replace('\r','')
txtfile.close()
self.admins=string.split('\n')
while '' in self.admins:
self.admins.remove('')
#mods
txtfile=open(www+"data/mods.txt","r")
string=txtfile.read().replace('\r','')
txtfile.close()
self.mods=string.split('\n')
while '' in self.mods:
self.mods.remove('')
#bots
txtfile=open(www+"data/bots.txt","r")
string=txtfile.read().replace('\r','')
txtfile.close()
self.bots=string.split('\n')
while '' in self.bots:
self.bots.remove('')
#banned
txtfile=open(www+"data/banned.txt","r")
string=txtfile.read().replace('\r','')
txtfile.close()
self.banned=string.split('\n')
while '' in self.banned:
self.banned.remove('')
def addmode(self,botmode):
self.modes.append(botmode)
def addcmd(self,botcmd):
self.commands.append(botcmd)
def addservice(self,botservice):
self.services.append(botservice)
def initmodules(self):
for c in self.commands:
if c.initexist:
c.init(self)
for m in self.modes:
if m.initexist:
m.init(self)
for s in self.services:
if s.initexist:
s.init(self)
def loadmodules(self,cmddir,modedir,servdir):
#load commands
listing=os.listdir(cmddir)
for f in listing:
path=os.path.splitext(f)
if path[1]==".py":
self.addcmd(command(os.path.basename(path[0])))
#load modes
listing=os.listdir(modedir)
for f in listing:
path=os.path.splitext(f)
if path[1]==".py":
self.addmode(mode(os.path.basename(path[0])))
#load services
listing=os.listdir(servdir)
for f in listing:
path=os.path.splitext(f)
if path[1]==".py":
self.addservice(service(os.path.basename(path[0])))
def logout(self):
self.client.sendRawCommand("logout")
def userlvl(self,username):
username=username.lower()
if username in self.banned:
return user.ban
elif username==host.lower():
return user.host
elif username in self.admins:
return user.admin
elif username in self.mods:
return user.mod
elif username in self.bots:
return user.bot
else:
return user.basic
def run(self):
chatget=self.chat.get()
if chatget:
for text in chatget:
output=[]
#services
for s in self.services:
ss=s.infunc(self,text)
if type(ss)==type([]):
output.extend(ss)
elif type(ss)==type("") or type(s)==type(u""):
output.append(ss)
else:
raise TypeError(s.name+" returned an unsupported value type ("+str(type(ss))+')')
#commands
if text["Type"]=="Message":
args=text["Msg"].split(' ')
for c in self.commands:
if CK+c.name==args[0]:
if self.userlvl(text["User"])>=c.level:
s=c.run(self,text,args[1:])
if type(s)==type([]):
output.extend(s)
elif type(s)==type("") or type(s)==type(u""):
output.append(s)
else:
raise TypeError(c.name+" returned an unsupported value type ("+str(type(s))+')')
break
elif self.userlvl(text["User"])==0:
output.append("You are banned from PIbot use.")
else:
article=""
if isvowel(userlvlname(c.level)[0]):
article="an"
else:
article="a"
output.append("Error: "+CK+c.name+" is "+article+' '+userlvlname(c.level)+" command.")
#modes
for out in output:
if out=="":
continue
for m in self.modes:
if not m.disabled and m.state:
out=m.run(self,out)
#run output services on output
for s in self.services:
if not s.disabled:
s.outfunc(self,out)
print out
self.chat.send(out)