-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcortex.py
429 lines (346 loc) · 13.2 KB
/
cortex.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import re
import os
import shutil
import pkgutil
import string
import traceback
from datetime import date, timedelta
from time import time, mktime, localtime
from random import randint, choice
from config import load_config
from getpass import getpass
from datastore import connectdb, Defaults
from util import ratelimited, zalgo
from staff import Butler
from autonomic import serotonin, Neurons, Synapse
from cybernetics import metacortex
from id import Id
# Basically all the interesting interaction with
# irc and command / content parsing happens here.
# Also connects to mongodb.
class Cortex:
values = False
lastpublic = False
lastprivate = False
lastchat = False
lastid = False
gettingnames = True
memories = False
autobabble = False
lastcommand = False
joined = False
operator = False
bequiet = False
lastip = False
debugging = False
thalamus = False
cellphone = False
master = False
channels = []
public_commands = []
members = []
broken = []
realuserdata = []
enabled = []
flags = {}
commands = {}
live = {}
helpmenu = {}
boredom = int(mktime(localtime()))
namecheck = int(mktime(localtime()))
def __init__(self, master, electroshock=False):
print '* Initializing'
self.master = master
self.settings = master.settings
self.secrets = master.secrets
self.channels = self.secrets.channels
self.context = self.secrets.primary_channel
self.personality = self.settings.bot
self.enabled = self.settings.plugins.values().pop(0)
metacortex.botnick = self.personality.nick
print '* Exciting neurons'
Neurons.cortex = self
print '* Connecting to datastore'
connectdb()
print '* Fondly remembering daddy'
admin = Id(self.secrets.owner)
if not admin.password:
print '*' * 40
print 'Hey %s! You haven\'t set a password yet! As my daddy you really need a password.' % self.secrets.owner
tmp_pass = getpass('Before I can continue, please enter a password: ')
print 'See? Was that so hard?'
admin.setpassword(tmp_pass, True)
tmp_pass = None
print '* Loading brainmeats'
self.loadbrains(electroshock)
# print '* Waking butler'
# self.butler = Butler(self)
# Loads up all the files in brainmeats and runs them
# through the hookup process.
def loadbrains(self, electroshock=False):
self.brainmeats = {}
brainmeats = __import__('brainmeats', fromlist=[])
if electroshock:
reload(brainmeats)
areas = [name for _, name, _ in pkgutil.iter_modules(['brainmeats'])]
for area in areas:
print '{0: <25}'.format(' - %s' % area),
if area not in self.enabled:
print '[\033[93mDISABLED\033[0m]'
continue
try:
mod = __import__('brainmeats', fromlist=[area])
mod = getattr(mod, area)
if electroshock:
reload(mod)
cls = getattr(mod, area.capitalize())
self.brainmeats[area] = cls(self)
print '[\033[0;32mOK\033[0m]'
except Exception as e:
self.chat('Failed to load %s.' % area, error=str(e))
self.broken.append(area)
self.enabled.remove(area)
print '[\033[0;31mFAILED\033[0m]'
if self.settings.debug.verbose:
print e
print traceback.format_exc()
for brainmeat in self.brainmeats:
serotonin(self, brainmeat, electroshock)
# When you get amnesia, it's probably a good time to really
# think and try to remember who you are.
def amnesia(self):
# This is an easy way out for now...
return self.personality
# And this is the basic function that runs all the time.
# The razor qualia edge of consciousness, if you will
# (though you shouldn't). It susses out the important
# info, logs the chat, sends PONG, finds commands, and
# decides whether to send new information to the parser.
@Synapse('twitch')
def monitor(self):
self.thalamus.process()
self.cellphone.process()
# If it is indeed a command, the cortex stores who sent it,
# and any words after the command are split in a values array,
# accessible by the brainmeats as self.values.
multis = 0
def command(self, sender, context, cmd, piped=False, silent=False):
# Limit commands to allowed channels.
if self.context in self.channels \
and 'command' not in self.channels[self.context]['mods']:
return
# This handles piping. Piping just breaks
# off the first command, gathers its output
# and runs command again on the next part
# of the chain until it's done.
chain = cmd.split('|', 1)
pipe = False
if len(chain) is 2:
cmd = chain[0].strip()
pipe = chain[1].strip()
if piped:
cmd = '%s %s' % (cmd, piped)
components = cmd.split()
_what = components.pop(0)
what = _what[1:]
means = _what[:1]
# These are specific command malformations
# that cropped up.
is_nums = re.search("^[0-9]+", what)
is_breaky = re.search("^" + re.escape(self.personality.command_prefix) + "|[^\w]+", what)
if is_nums or is_breaky:
return
# Small convenience feature.
if not what:
if not self.lastcommand:
return
what = self.lastcommand
self.values = False
self.flags = {}
flags = []
if components:
for component in components:
if component[:1] == self.personality.flag_prefix:
flags.append(component)
self.values = components
# An awesome feature that's not used at all. Should be.
if flags:
for flag in flags:
self.values.remove(flag)
value = True
if '=' in flag:
flag, value = flag.split('=')
flag = flag[1:]
self.flags[flag] = value
self.logroom('%s sent command: %s\n' % (sender, what))
self.lastsender = sender
self.lastcommand = what
result = None
# So you'll notice that some commands return
# values that this function sorts out into chats,
# while other commands directly run the self.chat,
# ._act, and .announce functions attached by the
# Dendrite class. Well, it used to be just those
# self.chat(whatever) and return, because it's a bot,
# right? It's final output is a chat, otherwise it's
# not much of a chatbot.
#
# Then some asshole in our chatroom said something
# like "it'd be cool if we could pipe commands, like
# -tweet | babble or something."
#
# So THAT got stuck in my head even though it's
# totally ridiculous, but I won't be able to sleep
# until it's fully implemented, and the first step
# in that is the ability to do something besides
# just chat out at the end of the function. If it's
# being piped, the best way to do that is reset
# self.values to the result of the command if it's
# piped from or to a pipeable function (I know
# 'from or to' should be one or the other, but it's
# 1am and I'm drunkenly listening to the Nye vs.
# Ham debate over youtube and it's almost as
# upsetting as realizing I'm going to have to comb
# over every goddamn function in this bot to
# determine what's pipeable and change its output).
#
# Point is, you can return a list or a string at
# the end of a brainmeat command, or just use chat.
# I probably won't worry about act and announce.
if means == self.personality.multi_command_prefix:
# The multi checking had to be put in
# after Eli decided to enter this:
# .babble fork | :babble | :babble | :babble
# ... which of course spiked the redis server
# to 100% CPU and eventually flooded the chat
# room with n^4 chats until the bot had to be
# kicked. This is what happens when you try
# to give nice things to hackers.
self.multis += 1
if self.multis > 1:
self.chat('This look like fork bomb. You kick puppies too?', context)
self.multis = 0
return
result = []
if not components:
self.multis = 0
return
for item in components:
self.values = [item]
_result = self.commands.get(what, self.default)()
result.append(_result)
else:
try:
result = self.commands.get(what, self.default)()
except Exception as e:
# self.chat(str(e))
self.chat(traceback.format_exc().replace('\n', ' '))
print traceback.format_exc()
if not result:
return
if pipe:
# Piped output must be string
if type(result) is list:
result = ' '.join(result)
self.command(sender, context, pipe, result)
return
if type(result) in [str, unicode]:
if silent:
return result
self.chat(result, context)
if type(result) is list:
if len(result) > self.personality.throttle:
result = result[:self.personality.throttle]
result.append("Such result. So self throttle. Much erotic. Wow.")
for line in result:
self.chat(line, context)
self.multis = 0
# Careful with this one.
def bored(self):
if not self.members:
return
self.announce('Chirp chirp. Chirp Chirp.')
# Simple log for room activity.
def logroom(self, what):
with open(self.settings.directory.log, 'a') as f:
f.write('TS:%s;%s' % (time(), what))
now = date.today()
if now.day != 1:
return
prev = date.today() - timedelta(days=1)
backlog = '%s/%s-mongo.log' % (self.settings.directory.logdir, prev.strftime('%Y%m'))
if os.path.isfile(backlog):
return
shutil.move(self.settings.directory.log, backlog)
def debug(self, message, target=None):
if not self.debugging: return
self.chat(message, target)
# Announce means the chat is always sent to a channel,
# never back as a private response.
@ratelimited(2)
def announce(self, message, channel=None):
if not channel:
channel = self.context if self.context in self.channels else self.secrets.primary_channel
self.chat(message, target=channel)
# Since chat is mongo's only means of communicating with
# a room, the ratelimiting here should prevent any overflow
# violations.
# NOTE: 'and not target' may be a sketchy override.
@ratelimited(2)
def chat(self, message, target=None, error=False):
if self.context in self.channels \
and not target \
and 'speak' not in self.channels[self.context]['mods']:
return
if self.bequiet:
return
if not message:
return
if target:
whom = target
elif self.context in self.channels:
whom = self.context
else:
try:
user = Id(self.lastsender)
whom = user.nick
except:
whom = self.secrets.primary_channel
if randint(1, 170) == 13:
message = zalgo(message)
# test later
# message = filter(lambda x: x in string.printable, message)
try:
message = message.encode('utf-8')
self.logroom('___%s: %s\n' % (self.personality.nick, str(message)))
m = str(message)
if randint(1, 170) == 23:
i = m.split()
pos = randint(0, len(i))
i.insert(pos, 'fnord')
m = ' '.join(i)
if error:
m += ' %s' % str(error)
self.thalamus.send('PRIVMSG %s :%s' % (whom, m))
except Exception as e:
try:
self.thalamus.send('PRIVMSG %s :ERROR: ' % (whom, str(e)))
except:
pass
def act(self, message, public=False, target=False):
message = '\001ACTION %s\001' % message
if public:
self.announce(message, target)
elif target:
self.chat(message, target)
else:
self.chat(message)
# When all else fails.
def default(self):
backup = Defaults.objects(command=self.lastcommand)
if backup:
try:
return choice(backup).response % tuple(self.values)
except:
return choice(backup).response
self.act(" cannot do this thing :'(")