/
__init__.py
857 lines (726 loc) · 29.6 KB
/
__init__.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# Copyright (C) 2007-2011 Christopher Jones
#
# This file is part of Madcow.
#
# Madcow is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Madcow is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Madcow. If not, see <http://www.gnu.org/licenses/>.
"""Core Madcow infobot"""
import threading
import warnings
import shutil
import codecs
import Queue as queue
import time
import sys
import os
import re
# be mindful of win32
try:
import signal
except ImportError:
signal = None
# add our include path for third party libs
PREFIX = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(PREFIX, 'include'))
from madcow.util import gateway, get_logger, http, Request
from madcow.conf import settings
from madcow.util.color import ColorLib
from madcow.util.auth import AuthLib
from madcow.util.text import encode, decode, set_encoding, get_encoding
VERSION = 2, 3, 0
__version__ = '.'.join(str(_) for _ in VERSION)
__author__ = 'Chris Jones <cjones@gmail.com>'
__url__ = 'https://github.com/cjones/madcow'
delim_re = re.compile(r'\s*[,;]\s*')
help_re = re.compile(r'^help(?:\s+(\w+))?$')
class MadcowError(Exception):
"""Base madcow error"""
class Madcow(object):
"""Core bot handler, subclassed by protocols"""
_botname = u'madcow'
_cor1_re = None
_cor2_re = None
_addrend_re = None
_feedback_re = None
_addrpre_re = None
_punc = '!"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~'
_proto = None
### INITIALIZATION FUNCTIONS ###
def __init__(self, base, scheme=None):
"""Initialize bot"""
self.base = base
self.log = get_logger('madcow', unique=False, stream=sys.stdout)
self.colorlib = ColorLib(scheme)
self.cached_nick = None
self.running = False
self.ignore_list = [nick.lower() for nick in settings.IGNORE_NICKS]
# set encoding
set_encoding(settings.ENCODING)
self.log.info('Preferred encoding set to %s', get_encoding())
# create admin instance
self.admin = Admin(self)
# load modules
self.modules = Modules(self, 'modules', settings.MODULES, supercedes=settings.MODULE_SUPERCEDES)
self.tasks = Modules(self, 'tasks', settings.TASKS, supercedes=settings.TASK_SUPERCEDES)
self.usage_lines = self.modules.help + self.tasks.help
self.usage_lines.append(u'help - this screen')
self.usage_lines.append(u'version - get bot version')
# signal handlers
if signal:
signal.signal(signal.SIGHUP, self.signal_handler)
signal.signal(signal.SIGTERM, self.signal_handler)
# initialize threads
self.request_queue = queue.Queue()
self.response_queue = queue.Queue()
self.lock = threading.RLock()
self.ignore_res = []
if settings.IGNORE_REGEX:
for pattern in settings.IGNORE_REGEX:
try:
regex = re.compile(pattern, re.I)
except Exception, error:
self.log.warn('%r pattern did not compile', pattern)
continue
self.ignore_res.append(regex)
@property
def prefix(self):
return PREFIX
@classmethod
def _get_proto(cls):
return cls.__module__.split('.')[-1]
@property
def proto(self):
if self._proto is None:
return self._get_proto()
return self._proto
def start(self):
"""Start the bot"""
self.running = True
# start services
for service in Service.__subclasses__():
self.log.info('starting service: %s', service.__name__)
thread = service(self)
thread.setDaemon(True)
thread.start()
# start worker threads
for i in range(settings.WORKERS):
name = u'ModuleWorker%d' % (i + 1)
self.log.debug('Starting Thread: %s', name)
thread = threading.Thread(target=self.request_handler, name=name)
thread.setDaemon(True)
thread.start()
self.run()
def run(self):
"""Runs madcow loop"""
while self.running:
self.check_response_queue()
line = decode(raw_input('>>> '), sys.stdin.encoding).rstrip()
req = Request(message=line)
req.nick = os.environ['USER']
req.channel = u'none'
req.addressed = True
req.private = True
self.check_addressing(req)
self.process_message(req)
def signal_handler(self, sig, *args):
"""Handles signals"""
if sig == signal.SIGTERM:
self.log.warn(u'got SIGTERM, signaling shutting down')
self.running = False
elif sig == signal.SIGHUP:
self.reload_modules()
def reload_modules(self):
"""Reload all modules"""
self.log.info(u'reloading modules')
self.modules.load_modules()
self.tasks.load_modules()
### OUTPUT FUNCTIONS
def output(self, message, req=None):
"""Add response to output queue"""
self.response_queue.put((message, req))
def check_response_queue(self):
"""Check if there's any message in response queue and process"""
try:
self.handle_response(*self.response_queue.get_nowait())
except queue.Empty:
pass
except:
self.log.exception('error reading response queue')
def handle_response(self, response, req=None):
"""encode output, lock threads, and call protocol_output"""
with self.lock:
try:
self.protocol_output(response, req)
except:
self.log.exception('error in output: %r', response)
def protocol_output(self, message, req=None):
"""Override with protocol-specific output method"""
print encode(message, sys.stdout.encoding)
### MODULE PROCESSING ###
def request_handler(self):
"""Dispatcher for workers"""
while self.running:
request = self.request_queue.get()
try:
self.process_module_item(request)
except:
self.log.exception('error processing request')
def process_module_item(self, request):
"""Run module response method and output any response"""
obj, nick, args, kwargs = request
try:
response = obj.get_response(nick, args, kwargs)
except:
self.log.exception(u'Uncaught module exception')
return
if response is not None and len(response) > 0:
self.output(response, kwargs[u'req'])
### INPUT FROM USER ###
def check_addressing(self, req):
"""Is bot being addressed?"""
# this is.. torturous. it works, but it really really
# needs to be un-perlified at some point.
botname = self.botname()
if botname != self.cached_nick:
self.cached_nick = botname
nicks = []
pre_nicks = []
for nick in [botname] + settings.ALIASES:
nick_e = re.escape(nick)
nicks.append(nick_e)
if nick_e[-1] not in self._punc:
nick_e += '[-,: ]+'
pre_nicks.append(nick_e)
nicks = '(?:%s)' % '|'.join(nicks)
pre_nicks = '(?:%s)' % '|'.join(pre_nicks)
pre_pat = r'^\s*%s\s*(.+)$' % pre_nicks
self._addrpre_re = re.compile(pre_pat, re.I)
self._cor1_re = re.compile(r'^\s*no[ ,]+%s[ ,:-]+\s*(.+)$' % nicks, re.I)
self._cor2_re = re.compile(r'^\s*no[ ,]+(.+)$', re.I)
self._feedback_re = re.compile(r'^\s*%s[ !]*\?[ !]*$' % nicks, re.I)
self._addrend_re = re.compile(r'^(.+),\s+%s\W*$' % nicks, re.I)
if self._feedback_re.search(req.message):
req.feedback = req.addressed = True
try:
req.message = self._addrend_re.search(req.message).group(1)
req.addressed = True
except AttributeError:
pass
try:
req.message = self._addrpre_re.search(req.message).group(1) # XXX
req.addressed = True
except AttributeError:
pass
try:
req.message = self._cor1_re.search(req.message).group(1)
req.correction = req.addressed = True
except AttributeError:
pass
if req.addressed:
try:
req.message = self._cor2_re.search(req.message).group(1)
req.correction = True
except AttributeError:
pass
def process_message(self, req):
"""Process requests"""
for ignore_re in self.ignore_res:
if ignore_re.search(req.message):
return
if settings.LOG_PUBLIC and not req.private:
self.logmessage(req)
if req.nick.lower() in self.ignore_list:
self.log.info(u'Ignored %r from %s', req.message, req.nick)
return
# builtins
if req.feedback:
self.output(u'yes?', req)
return
if req.addressed:
lmessage = req.message.lower().strip()
help_result = help_re.search(lmessage)
if help_result is not None:
if settings.PRIVATE_HELP:
req.make_private()
module = help_result.group(1)
if module:
if module in self.modules.modules:
self.output(self.modules.modules[module]['obj'].help, req)
elif module == u'all':
self.output(self.usage(), req)
else:
self.output(u'Unknown module %s' % module, req)
else:
self.output(u'usage: help [<module>|all]', req)
self.output(u'modules: %s' % u' '.join(sorted(self.modules.modules)), req)
return
if lmessage == u'version':
self.output(u'madcow %s by %s: %s' % (__version__, __author__, __url__), req)
return
if req.private:
response = self.admin.parse(req)
if response is not None and len(response):
self.output(response, req)
return
if settings.PROTOCOL == u'cli' and req.message == u'reload':
self.reload_modules()
for mod_name, mod in self.modules.by_priority():
obj = mod['obj']
self.log.debug('trying: %s', mod_name)
if obj.require_addressing and not req.addressed:
continue
try:
args = obj.pattern.search(req.message).groups()
except AttributeError:
continue
req.matched = True # module can set this to false to avoid term
# config forces this to be private
if mod[u'private']:
req.private = True
req.sendto = req.nick
# see if we can filter some of this information..
kwargs = {u'req': req}
kwargs.update(req.__dict__)
request = (obj, req.nick, args, kwargs,)
if (settings.PROTOCOL in (u'cli', u'ipython') or not obj.allow_threading):
self.log.debug('running non-threaded code for module %s', mod_name)
self.process_module_item(request)
else:
self.log.debug('launching thread for module: %s', mod_name)
self.request_queue.put(request)
if obj.terminate and req.matched:
self.log.debug('terminating because %s matched', mod_name)
break
def logmessage(self, req):
"""Logs public chatter"""
self.logpublic(req.channel, (u' * %s %s' if req.action else u'<%s> %s') % (req.nick, req.message))
def logpublic(self, channel, line):
logdir = os.path.join(self.base, 'log', 'public')
if not os.path.exists(logdir):
os.makedirs(logdir)
filename = ['public', channel.replace('#', '')]
if settings.LOG_BY_DATE:
filename.append(time.strftime('%F'))
logfile = os.path.join(logdir, '-'.join(filename) + '.log')
with open(logfile, 'a') as fp:
print >> fp, '%s %s' % (time.strftime('%T'), encode(line))
### MISC FUNCTIONS ###
def usage(self):
"""Returns help data as a string"""
return u'\n'.join(sorted(self.usage_lines))
def stop(self):
"""Stop the bot"""
self.running = False
def botname(self):
"""Should return the real name of the bot"""
return self._botname
class Service(threading.Thread):
"""Service object"""
def __init__(self, bot):
self.bot = bot
super(Service, self).__init__(name=type(self).__name__)
class GatewayService(gateway.GatewayService, Service):
"""Gateway service spawns TCP socket and listens for requests"""
class PeriodicEvents(Service):
"""Class to manage modules which are periodically executed"""
_ignore_modules = [u'__init__', u'template']
_process_frequency = 1
def __init__(self, *args, **kwargs):
self.last_run = {}
super(PeriodicEvents, self).__init__(*args, **kwargs)
def run(self):
"""While bot is alive, process periodic event queue"""
delay = 5
now = time.time()
for mod_name, mod in self.bot.tasks.modules.iteritems():
self.last_run[mod_name] = now - mod[u'obj'].frequency + delay
while self.bot.running:
self.process_queue()
time.sleep(self._process_frequency)
def process_queue(self):
"""Process queue"""
now = time.time()
for mod_name, mod in self.bot.tasks.modules.iteritems():
obj = mod[u'obj']
if (now - self.last_run[mod_name]) < obj.frequency:
continue
self.last_run[mod_name] = now
req = Request()
req.sendto = obj.output
request = (obj, None, None, {u'req': req})
self.bot.request_queue.put(request)
class User(object):
"""This class represents a logged in user"""
def __init__(self, user, flags):
self.user = user
self.flags = flags
@property
def is_admin(self):
"""Boolean: user is an admin"""
return u'a' in self.flags
@property
def is_registered(self):
"""Boolean: user is registerd"""
return self.is_admin or 'r' in self.flags
class Admin(object):
"""Class to handle admin interface"""
_admin_cmd_re = re.compile(r'^\s*admin\s+(.+?)\s*$', re.I)
_register_re = re.compile(u'^\s*register\s+(\S+)\s*$', re.I)
_auth_re = re.compile(u'^\s*(?:log[io]n|auth)\s+(\S+)\s*$', re.I)
_fist_re = re.compile(u'^\s*fist\s+(\S+)\s+(.+)$', re.I)
_help_re = re.compile(u'^\s*help\s*$', re.I)
_logout_re = re.compile(u'^\s*log(?:out|off)\s*$', re.I)
_deluser_re = re.compile(r'\s*del(?:ete)?\s+(\S+)\s*$', re.I)
_list_users_re = re.compile(r'\s*list\s+users\s*$', re.I)
_chflag_re = re.compile(r'\s*chflag\s+(\S+)\s+(\S+)\s*$', re.I)
_adduser_re = re.compile(r'^\s*add\s+(\S+)\s+(\S+)(?:\s+(\S+))?\s*$', re.I)
_basic_usage = [u'help - this screen',
u'register <pass> - register with bot',
u'login <pass> - login to bot']
_loggedin_usage = [u'logout - log out of bot']
_admin_usage = [u'fist <chan> <msg> - make bot say something in channel',
u'add <user> <flags> [pass] - add a user',
u'del <user> - delete a user',
u'list users - list users :P',
u'chflag <user> <[+-][aor]> - update user flags']
def __init__(self, bot):
self.bot = bot
self.users = {}
self.authlib = AuthLib(os.path.join(bot.base, 'db', 'passwd'))
def parse(self, req):
"""Parse request for admin commands and execute, returns output"""
try:
command = self._admin_cmd_re.search(req.message).group(1)
except AttributeError:
return
nick = req.nick.lower()
# register
try:
passwd = self._register_re.search(command).group(1)
return self.register_user(nick, passwd)
except AttributeError:
pass
# log in
try:
passwd = self._auth_re.search(command).group(1)
return self.authuser(nick, passwd)
except AttributeError:
pass
# help
usage = []
usage += self._basic_usage
if nick in self.users:
usage += self._loggedin_usage
if self.users[nick].is_admin:
usage += self._admin_usage
if self._help_re.search(command):
return u'\n'.join(usage)
# don't pass this point unless we are logged in
if nick not in self.users:
return
user = self.users[nick]
# logout
if Admin._logout_re.search(command):
del self.users[nick]
return u'You are now logged out.'
# functions past here require admin
if not user.is_admin:
return
try:
adduser, flags, password = self._adduser_re.search(command).groups()
return self.adduser(adduser, flags, password)
except AttributeError:
pass
# be the puppetmaster
try:
channel, message = Admin._fist_re.search(command).groups()
req.sendto = channel
return message
except AttributeError:
pass
# delete a user
try:
deluser = self._deluser_re.search(command).group(1)
self.authlib.delete_user(deluser)
if deluser in self.users:
del self.users[deluser]
return u'User deleted: %s' % deluser
except AttributeError:
pass
# list users
if self._list_users_re.search(command):
output = []
passwd = self.authlib.get_passwd()
for luser, data in passwd.items():
flags = []
if u'a' in data[u'flags']:
flags.append(u'admin')
if u'r' in data[u'flags']:
flags.append(u'registered')
if u'o' in data[u'flags']:
flags.append(u'autoop')
if luser in self.users:
flags.append(u'loggedin')
flags = u' '.join(flags)
output.append(u'%s: %s' % (luser, flags))
return u'\n'.join(output)
# update user flags
try:
chuser, newflags = self._chflag_re.search(command).groups()
return self.change_flags(chuser, newflags)
except AttributeError:
pass
def change_flags(self, user, chflags):
"""Change flags for a user"""
curflags = self.authlib.get_flags(user)
curflags = set(curflags)
args = re.split(r'([+-])', chflags)[1:]
for i in range(0, len(args), 2):
action, flags = args[i], args[i+1]
flags = set(flags)
if action == u'-':
for flag in flags:
curflags.discard(flag)
elif action == u'+':
for flag in flags:
curflags.add(flag)
curflags = u''.join(curflags)
self.authlib.change_flags(user, curflags)
if user in self.users:
self.users[user].flags = curflags
return u'flags for %s changed to %s' % (user, curflags)
def adduser(self, user, flags, password):
"""Add a new user"""
if self.authlib.user_exists(user):
return u"User already registered."
flags = u''.join(set(flags))
self.authlib.add_user(user, password, flags)
return u'user added: %s' % user
def register_user(self, user, passwd):
"""Register with the bot"""
if not settings.ALLOW_REGISTRATION:
return u"Registration is disabled."
if self.authlib.user_exists(user):
return u"User already registered."
flags = settings.DEFAULT_FLAGS
if not flags:
flags = u'r'
flags = set(flags)
if user.lower() == settings.OWNER_NICK.lower():
flags.add(u'a')
flags = u''.join(flags)
self.authlib.add_user(user, passwd, flags)
return u"You are now registered, try logging in: login <pass>"
def authuser(self, user, passwd):
"""Attempt to log in"""
if not self.authlib.user_exists(user):
return u"You are not registered: try register <password>."
if not self.authlib.check_user(user, passwd):
return u'Nice try.. notifying FBI'
self.users[user] = User(user, self.authlib.get_flags(user))
return u'You are now logged in. Message me "admin help" for help'
def getuser(self, nick):
if nick in self.users:
return self.users[nick]
class Modules(object):
"""This class dynamically loads plugins and instantiates them"""
_pyext = re.compile(r'\.py$')
def __init__(self, madcow, subdir, names, supercedes=None):
self.madcow = madcow
self.subdir = subdir
self.names = names
self.modules = {}
self.help = []
self.supercedes = supercedes
self.load_modules()
def load_modules(self):
"""Load/reload modules"""
self.madcow.log.info('loading modules')
for name in self.names:
try:
mod = __import__('madcow.' + self.subdir, globals(), locals(), [name])
mod = getattr(mod, name)
if not mod.Main.enabled:
raise MadcowError('this module is marked as disabled')
if name in self.modules:
reload(mod)
self.modules[name] = {
'mod': mod,
'obj': mod.Main(self.madcow),
'private': name in settings.PRIVATE_MODULES,
}
help = getattr(self.modules[name]['obj'], 'help', None)
if help:
self.help.append(help)
self.madcow.log.info('loaded module: %s', name)
except Exception, error:
self.madcow.log.warn('failed to load %s: %s', name, error)
if name in self.modules:
del self.modules[name]
# check if we enabled anything that conflicts
for master, slaves in self.supercedes.iteritems():
if master in self.modules:
module = self.modules[master]
if module['mod'].Main.enabled:
if isinstance(slaves, basestring):
slaves = [slaves]
for slave in slaves:
if slave in self.modules:
slave_module = self.modules[slave]
if slave_module['mod'].Main.enabled:
slave_module['mod'].Main.enabled = False
self.modules.pop(slave, None)
self.madcow.log.warn('unloading module %s: superceded by %s', slave, master)
def by_priority(self):
"""Return list of tuples for modules, sorted by priority"""
return sorted(self.modules.iteritems(),
key=lambda item: item[1][u'obj'].priority)
def daemonize():
"""POSIX only: run as a daemon"""
import resource
if os.fork():
os._exit(0)
os.setsid()
if os.fork():
os._exit(0)
for fd in xrange(resource.getrlimit(resource.RLIMIT_NOFILE)[0]):
try:
os.close(fd)
except OSError:
pass
fd = os.open(os.devnull, os.O_RDWR)
os.dup(fd)
os.dup(fd)
os.umask(027)
os.chdir('/')
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
def run(base, err=None, noeditor=False):
"""Execute the main bot"""
if err is None:
err = sys.stderr
# if this is a new bot, create base and stop
base = os.path.abspath(base)
for subdir in 'db', 'log':
dir = os.path.join(base, subdir)
if not os.path.exists(dir):
os.makedirs(dir)
settings_file = os.path.join(base, 'settings.py')
default_settings_file = os.path.join(PREFIX, 'conf', 'defaults.py')
if not os.path.exists(settings_file):
shutil.copy(default_settings_file, settings_file)
os.chmod(settings_file, 0640)
# TODO this code grew like a cancer. move it elsewhere, it's longer than the main loop..
if not noeditor:
# try to launch the editor configured in the user's environment instead
# of erroring out on first run, which is sort of rude.
try:
for env in 'EDITOR', 'VISUAL':
ed = os.environ.get(env)
if ed is not None:
# handle both path lookup and explicit path to program
if os.path.sep in ed:
if not os.access(ed, os.F_OK | os.X_OK):
ed = None
else:
path = os.environ.get('PATH', os.path.defpath)
for dir in path.split(os.path.pathsep):
bin = os.path.join(dir, ed)
if os.access(bin, os.F_OK | os.X_OK):
ed = bin
break
else:
ed = None
if ed is not None:
ed = os.path.abspath(ed)
break
else:
ed = None
if ed is not None:
# make sure the user is actually at a terminal before launching $EDITOR
# there might be a better way to do this..? but it seems to do the trick.
for fd in xrange(3):
if not os.isatty(fd):
break
else:
# NOTE: i am not sure this is technically correct, as it
# would fail when the user specifies any arguments in the
# EDTIOR env... i know PAGER actually evals() shell code in
# some cases, allowing complex expressions but i am not even
# gonna go there. if it becomes a problem, shlex.split may help.
pid = os.fork()
if pid == 0:
os.execl(ed, ed, settings_file)
os._exit(1)
ret = 255
while True:
wpid, wstatus = os.waitpid(pid, 0)
if pid == wpid:
if os.WIFSIGNALED(wstatus):
ret = -os.WTERMSIG(wstatus)
elif os.WIFEXITED(wstatus):
ret = os.WEXITSTATUS(wstatus)
break
if ret != os.EX_OK:
print >> err
print >> err, '{} exited with status: {:d}'.format(ed, ret)
print >> err
print >> err, 'if you are done configuring it, you may now rerun the bot to launch it'
print >> err
return ret
# the number of ways the above can go wrong are too numerous to test for, don't bother user with it
except (SystemExit, KeyboardInterrupt):
raise
except:
import traceback
traceback.print_exc()
# if they made it this far, invoking an editor was not in the cards. do things old-fashioned.
print >> err
print >> err, 'A new bot has been created, please configure it by editing this file, then rerun:'
print >> err
print >> err, settings_file
print >> err
return 0
os.environ['MADCOW_BASE'] = base
log = get_logger('madcow', stream=sys.stdout, unique=False)
try:
log.info('setting up http')
http.setup(cookies=settings.HTTP_COOKIES, agent=settings.HTTP_AGENT, timeout=settings.HTTP_TIMEOUT)
log.info('loading protocol handler')
protocol = __import__('madcow.protocol', globals(), locals(), [settings.PROTOCOL])
protocol = getattr(protocol, settings.PROTOCOL).ProtocolHandler
if settings.DETACH and protocol.allow_detach:
log.info('turning into a daemon')
try:
daemonize()
except (SystemExit, KeyboardInterrupt):
raise
except:
pass
if os.path.exists(settings.PIDFILE):
log.warn('removing stale pidfile')
os.remove(settings.PIDFILE)
with open(settings.PIDFILE, 'wb') as fp:
fp.write(str(os.getpid()))
protocol(base).start()
except SystemExit:
raise
except:
log.exception('A fatal error ocurred')
raise
finally:
try:
if os.path.exists(settings.PIDFILE):
log.info('removing pidfile')
os.remove(settings.PIDFILE)
except:
pass