1
0
mirror of https://github.com/sys-fs/seddy synced 2024-11-26 04:57:18 -05:00

Overall cleanup

This commit is contained in:
Thomas Mannay 2018-01-23 18:51:25 +00:00 committed by GitHub
parent c3283b1916
commit 6e8cc1681e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,8 +7,8 @@ server = 'chat.freenode.net'
channel = '#example' channel = '#example'
gecos = 'A text processing bot' gecos = 'A text processing bot'
receive = open('/tmp/' + server + '.in', 'r') receive = '/tmp/{0}.in'.format(server)
send = open('/tmp/' + server + '.out', 'w') send = '/tmp/{0}.out'.format(server)
parse_msg = re.compile(channel + ' :(.*)') parse_msg = re.compile(channel + ' :(.*)')
parse_sed = re.compile('(?<!\\\\)/') parse_sed = re.compile('(?<!\\\\)/')
@ -55,64 +55,72 @@ class Queue:
if i == self.tail-1 or self.data[i] is None: if i == self.tail-1 or self.data[i] is None:
return False return False
def seddy(sed, history): def msg_replace(find, replace, msg, f, n=1):
try:
if msg[:7] == '\x01ACTION':
res = '\x01ACTION {0}'.format(re.sub(find, replace, msg[8:], count=n, flags=f))
else:
res = re.sub(find, replace, msg, count=n, flags=f)
except:
return False
return res
def seddy(sed, history, parser):
f = 0 f = 0
regex = parse_sed.split(sed) regex = parser.split(sed)
if len(regex) < 4: if len(regex) < 4:
return False return choice(haiku)
if 'i' in regex[3]: if 'i' in regex[3]:
f |= re.I f |= re.I
try: try:
msg = history.find(regex[1], f) msg = history.find(regex[1], f)
except: except:
msg = False return choice(haiku)
if msg == False:
return False
if "g" in regex[3]: if "g" in regex[3]:
try: res = msg_replace(regex[1], regex[2], msg, 0, f)
res = re.sub(regex[1], regex[2], msg, flags=f)
except:
res = False
else: else:
try: res = msg_replace(regex[1], regex[2], msg, 1, f)
res = re.sub(regex[1], regex[2], msg, 1, f) if res:
except: res = res.replace('\0', re.search(regex[1], msg, f).group(0))
res = False
return res return res
def notice(msg): def notice(msg, channel):
send.write('NOTICE ' + channel + ' :' + msg + '\r\n') with open(send, 'w', encoding='utf-8') as g:
send.flush() g.write('NOTICE ' + channel + ' :' + msg + '\r\n')
def privmsg(msg): def privmsg(msg, channel):
if msg[:7] == '\x01ACTION': if msg[:7] == '\x01ACTION':
send.write('PRIVMSG ' + channel + ' :' + '\x01' + with open(send, 'w', encoding='utf-8') as g:
g.write('PRIVMSG ' + channel + ' :' + '\x01' +
''.join(c for c in msg if c.isprintable()) + '\x01\r\n') ''.join(c for c in msg if c.isprintable()) + '\x01\r\n')
else: else:
send.write('PRIVMSG ' + channel + ' :' + with open(send, 'w', encoding='utf-8') as g:
''.join(c for c in msg if c.isprintable()) + '\r\n') g.write('PRIVMSG ' + channel + ' :' +
send.flush() msg.replace('\n', ' ',).replace('\r', '') + '\r\n')
if __name__ == "__main__": if __name__ == "__main__":
history = Queue(48) history = Queue(48)
send.write('NICK ' + nick + '\r\n') with open(send, 'w', encoding='utf-8') as f:
send.flush() f.write('NICK ' + nick + '\r\n')
send.write('USER ' + nick + ' * 8 :' + gecos + '\r\n') f.write('USER ' + nick + ' * 8 :' + gecos + '\r\n')
send.flush() sleep(5)
send.write('JOIN ' + channel + '\r\n') f.write('JOIN ' + channel + '\r\n')
send.flush()
for line in receive: with open(receive, 'r', encoding='utf-8') as f:
msg = parse_msg.search(line) for line in f:
if msg is None:
if 'PING' in line: if 'PING' in line:
send.write('PONG\r\n') with open(send, 'w', encoding='utf-8') as g:
send.flush() g.write('PONG {0}\r\n'.format(ping.split(line)[1]))
continue
m = parse_msg.match(line)
try:
channel = m.group(1)
msg = m.group(2)
except:
continue continue
else:
msg = msg.group(1)
if history.full(): if history.full():
history.dequeue() history.dequeue()
@ -124,6 +132,6 @@ if __name__ == "__main__":
if '.source ' + nick in msg[:8 + len(nick)]: if '.source ' + nick in msg[:8 + len(nick)]:
notice('[Python] https://github.com/sys-fs/seddy') notice('[Python] https://github.com/sys-fs/seddy')
elif is_sed.match(msg): elif is_sed.match(msg):
foo = seddy(msg,history) res = seddy(msg, history)
if foo != False: if res:
privmsg(re.sub('\\\\/', '/', foo)) privmsg(re.sub('\\\\/', '/', foo))