-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathripley.py
120 lines (89 loc) · 3.54 KB
/
ripley.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
import re
import random
from twisted.words.protocols import irc
from twisted.internet import protocol, reactor
from datetime import datetime
from collections import defaultdict
last_seen = {}
tell_messages = defaultdict(list)
bttf = filter(lambda x: len(x.strip()), open('backtothefuture_transcript.txt').read().split('\n\n'))
def ping():
num_lines = random.randint(1,8)
while True:
start_line = random.randint(1,len(bttf))
if re.match('^[A-Za-z]+: ',bttf[start_line]):
break
return '\n'.join(bttf[start_line:start_line+num_lines])
def say_hi_back(helloword, msg, username):
return helloword + ' ' + username + '!'
def get_last_seen(seen, msg, username):
search_name = msg.split(seen)[1].strip()
if search_name not in last_seen:
return "I haven't seen {0} before".format(search_name)
return "I last saw {0} at {1}".format(search_name, last_seen[search_name])
def tell_user(tell, msg, username):
tell_name = msg.split(tell,1)[1].split()[0]
message = msg.split(tell_name,1)[1]
tell_messages[tell_name].append("{0} @ {1} : {2}".format(username, datetime.now(), message))
return "I will tell {0} that when they come back".format(tell_name)
def print_help(*args):
return """
<< (hi|hello|hey) ripley >>
ripley will say hi back
<< ripley: tell username message message message >>
you can queue up multiple messages to [username] that are delivered when [username] next appears
<< ripley: last seen username >>
<< ripley: seen username >>
will print out in local time when [username] was last seen
"""
responses = {}
responses['(hi|hello|hey)'] = say_hi_back
responses['(last )?seen'] = get_last_seen
responses['tell'] = tell_user
responses['help'] = print_help
def find_match(text):
for resp in responses:
match = re.compile(resp, re.I).match(text)
if match:
return responses[resp], match.group()
return None, None
class RipleyBot(irc.IRCClient):
def _get_nickname(self):
return self.factory.nickname
nickname = property(_get_nickname)
def signedOn(self):
self.join(self.factory.channel)
print "Signed on as {0}".format(self.nickname)
def joined(self, channel):
print "Joined {0}".format(channel)
def privmsg(self, user, channel, msg):
if not user:
return
user = user.split('!',1)[0]
last_seen[user] = datetime.now()
if user in tell_messages:
self.msg(self.factory.channel, "OI {0}, you have messages!".format(user.upper()))
for message in tell_messages[user]:
self.msg(self.factory.channel, message)
del tell_messages[user]
if 'ping' == msg:
self.msg(self.factory.channel, ping())
if self.nickname in msg:
msg = re.compile(self.nickname + '[:,]* ?', re.I).sub('',msg)
f, group = find_match(msg)
if f:
response = f(group, msg, user)
self.msg(self.factory.channel, response)
class RipleyFactory(protocol.ClientFactory):
protocol = RipleyBot
def __init__(self, channel, nickname="ripley"):
self.channel = channel
self.nickname = nickname
def clientConnectionLost(self, connector, reason):
print "Lost connection {0}, reconnecting...".format(reason)
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "Could not connect: {0}".format(reason)
if __name__=='__main__':
reactor.connectTCP('server',6667,RipleyFactory('#chatroom'))
reactor.run()