-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
289 lines (263 loc) · 9.82 KB
/
bot.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python
import functions,re, responses, irc,sys,time,random,imp
from threading import Thread
from threading import Timer
from time import strftime
def decode(bytes):
try: text = bytes.decode('utf-8')
except UnicodeDecodeError:
try: text = bytes.decode('iso-8859-1')
except UnicodeDecodeError:
text = bytes.decode('cp1252')
return text
class Redditron(irc.Bot):
def __init__(self, config):
if not hasattr(config,'cli'):
args = (config.nick, config.ident, config.realname, config.chanlist, config.nspassword)
irc.Bot.__init__(self, *args)
self.uptime = 0
self.stack = [0]
self.config = config
self.nick = self.config.nick
self.waitfactor=self.config.waitfactor
self.cooldowntime=self.config.cooldowntime
self.freespeech=self.config.freespeech
self.starttime=time.time()
self.admins=self.config.admins
self.connected= False
self.cooldown = False
self.kicked = False
self.responses = responses.Responses()
self.setup()
def startup(self):
self.msg('NickServ', 'IDENTIFY %s' % self.config.nspassword)
print >> sys.stderr, 'joining channels...',
time.sleep(5)
for ch in self.config.chanlist:
self.joinch(ch)
self.connected=True
def setup(self):
self.commands={}
self.admincommands={}
funcs=imp.load_source('functions',sys.path[0]+'/functions.py')
for name, obj in vars(funcs).iteritems():
if hasattr(obj,'commands'):
if hasattr(obj,'admin'):
for c in obj.commands:
self.admincommands[c]=obj
else:
for c in obj.commands:
self.commands[c]=obj
def dispatch(self, origin, args):
# origin.sender = nick when PM, else chan
bytes, event, args = args[0], args[1], args[2:]
msg = decode(bytes)
if event =='KICK' and args[1] == self.nick:
self.logger('KICKED from '+args[0])
self.kicked = True
self.joinch(args[0])
elif event=='251':
self.startup()
elif event=='366':
self.logger('joined '+args[1])
input = self.input(origin, msg, args)
input.source=args[1]
if self.kicked:
functions.stfu(self, input)
self.kicked = False
else:
functions._greet(self, input)
elif event=='PRIVMSG':
if self.freespeech:
input = self.input(origin, msg, args)
if input.priv:
input.text=self.nick+' '+msg
functions._freespeech(self,input)
else:
input = self.input(origin, msg, args)
self.checkforvarioustriggers(input, origin, msg, args)
def checkforspam(self, input):
if input.priv or input.source =="#reddit": return True
else:
if (time.time() - self.stack[0]) < 200:
functions.leave(self, input)
return False
else:
self.stack.append(time.time())
self.stack = self.stack[-8:]
return True
def checkforvarioustriggers(self, input, origin, msg, args):
nickmatch = re.match(self.nick.lower()+'(:|,|\s)', msg, re.I)
if re.match('(redditron(.*)a bot)|(a bot(.*)redditron)', msg.lower()):
if self.checkforspam(input):
functions.detected(self,input)
elif self.nick.lower() in msg.lower() and 'a bot' in msg.lower():
if self.checkforspam(input):
functions.detected(self,input)
elif 'shut up' in msg or 'stfu' in msg:
if self.nick in msg:
if self.checkforspam(input):
functions.stfu(self,input)
elif re.match('(wb|yo|dear|h(ai(l)?|ello|ey(a)?|i))\s'+ self.nick, msg, re.I):
if self.checkforspam(input):
functions._greet(self,input)
elif nickmatch or input.priv:
if self.checkforspam(input):
if not input.priv:
msg=msg[len(self.nick)+1:].lstrip()
input =self.input(origin,msg,args)
self.checkforcommands(input)
else:
return False
return True
def checkforcommands(self, input):
responded = False
msg = input.text
if msg:
cmd=msg.split()[0]
for c in self.commands:
if c == cmd:
try: responded = self.commands[c](self,input)
except Exception, e:
self.error(input.source)
break
for c in self.admincommands:
if c == cmd:
if input.admin:
try: responded=self.admincommands[c](self,input)
except Exception, e:
self.error(input.source)
#self.say(input.source,'error.')
else:
self.say(input.source,'Only botadmins can do that.')
break
if responded == False:
responded = functions.detecttrigger(self,input)
if responded == False:
functions.fallback(self,input)
responded = True
def joinch(self,ch):
self.write(('JOIN',ch))
def partch(self,ch):
self.write(('PART',ch))
def nickch(self,ch):
self.write(('NICK',ch))
def input(self, origin, text, args):
class CommandInput(unicode):
def __new__(cls, text, origin, args):
s = unicode.__new__(cls, text)
s.text=text
s.source = origin.sender
s.nick = origin.nick
s.args = args
s.admin = origin.nick in self.config.admins
s.priv = not origin.sender.startswith('#')
return s
return CommandInput(text, origin, args)
def startcooldown(self):
sleepfor = random.choice((self.cooldowntime/2, self.cooldowntime/4,
self.cooldowntime*3, self.cooldowntime*2))
self.logger('sleeping for '+str(sleepfor)+ ' seconds.')
t = Thread(target=self.sleeper, args=(sleepfor,))
t.start()
def postresponse(self,source,response):
'''
splits the string into several lines and then posts it
'''
if '\n' in response:
for x in response.splitlines():
self.postresponse(source,x)
time.sleep(3)
return
def splitintwo(response,lit):
r=response.split(lit)
self.postresponse(source, lit.join(r[:len(r)/2])+lit.strip())
self.postresponse(source, lit.join(r[len(r)/2:]))
if len(response) > 70:
if '? ' in response:
splitintwo(response, '? ')
elif '; ' in response:
splitintwo(response, '; ')
elif '.. ' in response:
splitintwo(response, '.. ')
elif '. ' in response:
splitintwo(response, '. ')
else: self.say(source,response,cooldown=True)
else: self.say(source,response,cooldown=True)
def say(self, ch, msg, cooldown=False):
if self.waitfactor == 0:
waitfor = 0
else:
if self.freespeech: waitfor = 2+len(msg)/(self.waitfactor)
else: waitfor = 1+len(msg)/(4*self.waitfactor)
self.logger('waiting for '+str(waitfor)+' second(s) before responding')
time.sleep(waitfor)
if self.connected:
self.msg(ch,msg)
else: print msg
if cooldown and self.freespeech:
self.startcooldown()
def sleeper(self, i):
if self.cooldown: return
self.cooldown = True
time.sleep(i)
self.cooldown = False
self.logger("woke up")
def logger(self, msg):
if self.connected:
print msg
def incrementtime(self):
redditron.time+=1
t = Timer(1.0, incrementtime)
t.start()
def testallresponses(redditron):
storewf, redditron.waitfactor = redditron.waitfactor, 0
l = redditron.responses.getresponseslist()
errors,errcounter='',0
for r in l:
try: redditron.postresponse("",r)
except:
errors+= r+'\n'+str(sys.exc_info())+'\n\n'
errcounter+=1
redditron.waitfactor = storewf
print errors
print 'errors: '+str(errcounter)
def main():
'''Starts a Redditron Command Line Session
'''
print 'Welcome to Redditron'
class BotConfig(object):
def __init__(self):
self.nick='redditron'
self.waitfactor=0
self.cooldowntime=20
self.admins=['a']
self.freespeech=False
self.exitfreespeechphrase = "You're a cracker"
self.fallbackr = ["i don't understand you"]
self.qfallbackr = ["i don't understand your question"]
self.botresponses = ["not a bot."]
self.stfuresponses = ["no."]
self.greetings = ["hi."]
self.cli=True
class Origin(object):
def __init__(self):
self.sender=('a')
self.nick=('a')
config = BotConfig()
bot=Redditron(config)
origin=Origin()
while True:
msg= raw_input('>>')
if msg == 'q':
sys.exit("Bye")
elif msg == 'r':
input=bot.input(origin,msg,[''])
input.priv=True
functions.randomquote(bot,input)
elif msg == 'test':
testallresponses(bot)
else:
bot.dispatch(origin,[msg.encode(),'PRIVMSG'])
if __name__ == '__main__':
main()