-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreba.py
202 lines (175 loc) · 5.88 KB
/
reba.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
#! /usr/bin/env python
# Public domain; MZMcBride; 2011
import os
import time
import subprocess as sub
import codecs
import poplib
import re
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol, task
from email.header import decode_header
import mailsettings as settings
# IRC info
server = 'irc.freenode.net'
trusted = settings.trusted
primary_channel = settings.primary_channel(__file__)
# E-mail info
pop_host = 'pop.googlemail.com'
pop_port = '995'
pop_user = 'reba.the.mail.mistress'
pop_pass = settings.password
max_lines = 5
subject_line_re = re.compile('Subject:')
from_re = re.compile('From:')
to_re = re.compile('To:')
# Debug?
debug = False
class snerkBot(irc.IRCClient):
realname = 'Reba the Mail Lady'
nickname = 'reba'
altnick = 'rebah'
hostmask = 'nightshade.toolserver.org'
password = settings.password
got_Pong = True
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.lc = task.LoopingCall(self.sendServerPing)
self.lc.start(60, False)
self.lc2 = task.LoopingCall(self.retrieveMail)
if debug:
self.lc2.start(15, False)
else:
self.lc2.start(60, False)
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
def signedOn(self):
for i in settings.channels(__file__):
self.join(i)
def irc_ERR_NICKNAMEINUSE(self, prefix, params):
self.register(self.altnick)
def kill_self(self):
sub.Popen('kill %s' % os.getpid(),
stdout=sub.PIPE,
stderr=sub.STDOUT,
shell=True)
def retrieveMail(self):
try:
if debug:
print 'checking mail...'
pop = poplib.POP3_SSL(pop_host, pop_port)
pop.user(pop_user)
pop.pass_(pop_pass)
stat = pop.stat()
if stat[0] > 0:
for n in range(stat[0]):
msgnum = n+1
response, lines, bytes = pop.top(msgnum, max_lines)
clean_lines = []
count = 0
for i in range(len(lines)-1):
if lines[count+1].replace('\t', ' ').startswith(' '):
clean_lines.append(lines[count] + lines[count+1])
elif not lines[count].startswith(' '):
clean_lines.append(lines[count])
count += 1
subject_line = filter(subject_line_re.match, clean_lines)
subject_line = subject_line[0].split('Subject: ', 1)[1]
subject_line, subject_encoding = decode_header(subject_line)[0]
if subject_encoding is not None:
subject_line = subject_line.decode(subject_encoding).encode('utf-8')
message_sender = filter(from_re.match, clean_lines)
name_provided = re.search(r'From: (.+) <.+>', message_sender[0])
name_not_provided = re.search(r'From: (.+)@.+', message_sender[0])
if name_provided:
if debug:
print 'name provided'
sender_safe_name = name_provided.group(1)
elif name_not_provided:
if debug:
print 'name not provided'
sender_safe_name = name_not_provided.group(1)
sender_safe_name, sender_encoding = decode_header(sender_safe_name)[0]
if sender_encoding is not None:
sender_safe_name = sender_safe_name.decode(sender_encoding).encode('utf-8')
if re.search('To:.+toolserver-announce@', ' '.join(clean_lines)):
prefix = ''
elif re.search('To:.+toolserver-l@', ' '.join(clean_lines)):
prefix = ''
else:
if not debug:
pop.dele(msgnum)
continue
final_mail_line = '%s * %s' % (sender_safe_name.strip('"'),
re.sub(r'\t', ' ', re.sub(r'\s{2,}', ' ', subject_line)))
self.msg(primary_channel, final_mail_line)
time.sleep(0.5)
if not debug:
pop.dele(msgnum)
pop.quit()
except:
pass
return
def sendServerPing(self):
if not self.got_Pong:
self.kill_self()
self.got_Pong = False
self.sendLine('PING %s' % server)
def irc_PONG(self, prefix, params):
if params:
self.got_Pong = True
def privmsg(self, sender, channel, msg):
# IRC VARIABLES
user = sender.split('!', 1)[0]
try:
hostmask = sender.split('@', 1)[1]
except:
hostmask = ''
# FIND
if hostmask == self.hostmask:
return
elif channel == self.nickname: # PM'ing with the bot.
try:
hostmask = sender.split('@', 1)[1]
except:
hostmask = ''
if re.search(r'^!r(estart)?\b', msg, re.I|re.U):
self.kill_self()
elif msg.startswith('#') and hostmask in trusted:
target = msg.split(' ')[0]
verb = msg.split(' ')[1]
nmsg = ' '.join(msg.split(' ')[2:])
if verb in ['do', 'act']:
self.describe(target, nmsg)
elif verb in ['say', 'echo']:
self.msg(target, nmsg)
elif hostmask in trusted:
self.sendLine(msg)
return
def action(self, user, channel, msg):
hostmask = user.split('@', 1)[1]
user = user.split('!', 1)[0]
lovefind = re.search(r'(glomps|hugs|snuggles|snuggleglomps|cuddles|licks)( a)? %s' % self.nickname, msg, re.I|re.U)
if hostmask == self.hostmask:
return
elif re.search(r'pets %s' % self.nickname, msg, re.I|re.U):
self.describe(channel, 'purrs.')
return
elif lovefind:
self.describe(channel, lovefind.group(1) + ' %s.' % user)
return
elif re.search(r'tickles %s' % self.nickname, msg, re.I|re.U):
self.describe(channel, 'giggles.')
return
class snerkBotFactory(protocol.ClientFactory):
protocol = snerkBot
def __init__(self):
pass
def clientConnectionLost(self, connector, reason):
connector.connect()
def clientConnectionFailed(self, connector, reason):
reactor.stop()
if __name__ == '__main__':
f = snerkBotFactory()
reactor.connectTCP('%s' % server, 8001, f)
reactor.run()