forked from adamcik/catsitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catsitter.py
executable file
·76 lines (54 loc) · 1.64 KB
/
catsitter.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
#! /usr/bin/python
import socket
import sys
import re
import urllib2
import catsitter
from optparse import OptionParser
socket.setdefaulttimeout(25)
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'catsitter')]
urllib2.install_opener(opener)
def optparse():
parser = OptionParser()
parser.add_option('--config', action='store_true', default=False)
parser.add_option('--settings')
return parser
def decode(string):
if isinstance(string, unicode) or string is None:
return string
try:
string = string.decode('utf-8')
except UnicodeDecodeError:
string = string.decode('iso-8859-1')
return string
def config():
results = []
for regexp, handler in catsitter.registry:
results.append(regexp)
print 'match = ^($nick: ?)?(%s)$' % '|'.join(results)
def dispatch(nick, target, source, message):
results = []
for regexp, func in catsitter.registry:
match = re.search('^($nick: ?)?(%s)$' % regexp, message, re.UNICODE)
if not match:
continue
result = func(**match.groupdict()) or []
result = filter(lambda r: r is not None, result)
results.extend(result)
for line in results:
print line.encode('utf-8')
def main():
parser = optparse()
(options, args) = parser.parse_args()
if not options.settings:
parser.error('no settings set')
__import__('catsitter.settings.%s' % options.settings)
if options.config:
config()
elif len(args) == 4:
dispatch(*map(decode, args))
else:
parser.print_usage()
if __name__ == '__main__':
sys.exit(main())