2016-10-30 11:53:05 -04:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# See the LICENSE file for licensing information
|
|
|
|
import re
|
2017-03-11 14:39:19 -05:00
|
|
|
from string import printable
|
2016-10-30 11:53:05 -04:00
|
|
|
|
|
|
|
nick = 'seddy'
|
|
|
|
server = 'chat.freenode.net'
|
|
|
|
channel = '#example'
|
|
|
|
gecos = 'A text processing bot'
|
|
|
|
|
|
|
|
receive = open('/tmp/' + server + '.in', 'r')
|
|
|
|
send = open('/tmp/' + server + '.out', 'w')
|
|
|
|
|
2016-11-05 15:17:10 -04:00
|
|
|
parse_msg = re.compile(channel + ' :(.*)')
|
2016-10-30 11:53:05 -04:00
|
|
|
parse_sed = re.compile('(?<!\\\\)/')
|
2016-10-31 08:31:04 -04:00
|
|
|
is_sed = re.compile('^s/.*/.*')
|
2016-10-30 11:53:05 -04:00
|
|
|
|
|
|
|
class Queue:
|
|
|
|
size = 0
|
|
|
|
data = []
|
|
|
|
head = 0
|
|
|
|
tail = 0
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
def __init__(self, size):
|
|
|
|
self.size = size
|
|
|
|
self.data = [None]*size
|
|
|
|
|
|
|
|
def enqueue(self, s):
|
|
|
|
if not self.full():
|
|
|
|
self.count += 1
|
|
|
|
self.data[self.tail] = s
|
|
|
|
self.tail = (self.tail + 1) % self.size
|
|
|
|
|
|
|
|
def dequeue(self):
|
|
|
|
if not self.empty():
|
|
|
|
self.count -= 1
|
|
|
|
s = self.data[self.head]
|
|
|
|
self.head = (self.head + 1) % self.size
|
|
|
|
return s
|
|
|
|
|
|
|
|
def full(self):
|
|
|
|
return self.size == self.count
|
|
|
|
|
|
|
|
def empty(self):
|
|
|
|
return self.head == self.count
|
|
|
|
|
2016-11-05 16:16:32 -04:00
|
|
|
def find(self, s, flags=0):
|
2016-10-30 11:53:05 -04:00
|
|
|
i = self.tail-1
|
|
|
|
while True:
|
|
|
|
if i == -1:
|
|
|
|
i = self.size-1
|
2016-11-05 16:16:32 -04:00
|
|
|
if re.search(s, self.data[i], flags):
|
2016-10-30 11:53:05 -04:00
|
|
|
return self.data[i]
|
|
|
|
i -= 1
|
2016-11-05 15:40:38 -04:00
|
|
|
if i == self.tail-1 or self.data[i] is None:
|
2016-10-30 11:53:05 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
def seddy(sed, history):
|
2016-11-05 16:16:32 -04:00
|
|
|
flag = 0
|
2016-10-30 11:53:05 -04:00
|
|
|
regex = parse_sed.split(sed)
|
2016-11-05 16:16:32 -04:00
|
|
|
|
2016-11-05 13:19:27 -04:00
|
|
|
if len(regex) < 4:
|
|
|
|
return False
|
2016-11-05 16:16:32 -04:00
|
|
|
if 'i' in regex[3]:
|
|
|
|
flag |= re.I
|
2016-12-15 18:32:12 -05:00
|
|
|
try:
|
|
|
|
msg = history.find(regex[1], f)
|
|
|
|
except:
|
|
|
|
msg = False
|
2016-10-30 11:53:05 -04:00
|
|
|
if msg == False:
|
|
|
|
return False
|
|
|
|
if "g" in regex[3]:
|
2016-12-15 18:06:57 -05:00
|
|
|
try:
|
|
|
|
res = re.sub(regex[1], regex[2], msg, flags=f)
|
|
|
|
except:
|
|
|
|
res = False
|
2016-10-30 11:53:05 -04:00
|
|
|
else:
|
2016-12-15 18:06:57 -05:00
|
|
|
try:
|
|
|
|
res = re.sub(regex[1], regex[2], msg, 1, f)
|
|
|
|
except:
|
|
|
|
res = False
|
|
|
|
return res
|
2016-10-30 11:53:05 -04:00
|
|
|
|
|
|
|
def notice(msg):
|
|
|
|
send.write('NOTICE ' + channel + ' :' + msg + '\r\n')
|
|
|
|
send.flush()
|
|
|
|
|
|
|
|
def privmsg(msg):
|
2017-03-11 14:39:19 -05:00
|
|
|
send.write('PRIVMSG ' + channel + ' :' +
|
|
|
|
''.join(filter(printable.__contains__, msg)) + '\r\n')
|
2016-10-30 11:53:05 -04:00
|
|
|
send.flush()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
history = Queue(48)
|
|
|
|
|
|
|
|
send.write('NICK ' + nick + '\r\n')
|
|
|
|
send.flush()
|
|
|
|
send.write('USER ' + nick + ' * 8 :' + gecos + '\r\n')
|
|
|
|
send.flush()
|
|
|
|
send.write('JOIN ' + channel + '\r\n')
|
|
|
|
send.flush()
|
|
|
|
|
|
|
|
for line in receive:
|
|
|
|
msg = parse_msg.search(line)
|
|
|
|
if msg is None:
|
2016-11-05 16:27:12 -04:00
|
|
|
if 'PING' in line:
|
|
|
|
send.write('PONG\r\n')
|
|
|
|
send.flush()
|
2016-10-30 11:53:05 -04:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
msg = msg.group(1)
|
|
|
|
|
|
|
|
if history.full():
|
|
|
|
history.dequeue()
|
|
|
|
if 'PRIVMSG' in line and not is_sed.match(msg):
|
|
|
|
history.enqueue(msg)
|
|
|
|
|
2016-10-30 14:12:27 -04:00
|
|
|
if '.bots' in msg[:5] or '.bot ' + nick in msg[:5 + len(nick)]:
|
2016-10-30 11:53:05 -04:00
|
|
|
notice("I was written to correct your mistakes.")
|
2016-10-30 14:12:27 -04:00
|
|
|
if '.source ' + nick in msg[:8 + len(nick)]:
|
2016-10-30 11:53:05 -04:00
|
|
|
notice('[Python] https://github.com/sys-fs/seddy')
|
|
|
|
elif is_sed.match(msg):
|
2017-03-11 14:39:19 -05:00
|
|
|
foo = ''.join(c for c in seddy(msg, history) if c not in '\r\n')
|
2016-10-30 11:53:05 -04:00
|
|
|
if foo != False:
|
|
|
|
privmsg(re.sub('\\\\/', '/', foo))
|