mirror of
https://github.com/sys-fs/seddy
synced 2024-11-22 11:54:16 -05:00
Overall cleanup
This commit is contained in:
parent
c3283b1916
commit
6e8cc1681e
112
seddy.py
112
seddy.py
@ -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('(?<!\\\\)/')
|
||||||
@ -54,76 +54,84 @@ class Queue:
|
|||||||
i -= 1
|
i -= 1
|
||||||
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 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):
|
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:
|
||||||
''.join(c for c in msg if c.isprintable()) + '\x01\r\n')
|
g.write('PRIVMSG ' + channel + ' :' + '\x01' +
|
||||||
|
''.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:
|
with open(send, 'w', encoding='utf-8') as g:
|
||||||
send.write('PONG\r\n')
|
g.write('PONG {0}\r\n'.format(ping.split(line)[1]))
|
||||||
send.flush()
|
continue
|
||||||
continue
|
|
||||||
else:
|
|
||||||
msg = msg.group(1)
|
|
||||||
|
|
||||||
if history.full():
|
m = parse_msg.match(line)
|
||||||
history.dequeue()
|
try:
|
||||||
if 'PRIVMSG' in line and not is_sed.match(msg):
|
channel = m.group(1)
|
||||||
history.enqueue(msg)
|
msg = m.group(2)
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
if '.bots' in msg[:5] or '.bot ' + nick in msg[:5 + len(nick)]:
|
if history.full():
|
||||||
notice("I was written to correct your mistakes.")
|
history.dequeue()
|
||||||
if '.source ' + nick in msg[:8 + len(nick)]:
|
if 'PRIVMSG' in line and not is_sed.match(msg):
|
||||||
notice('[Python] https://github.com/sys-fs/seddy')
|
history.enqueue(msg)
|
||||||
elif is_sed.match(msg):
|
|
||||||
foo = seddy(msg,history)
|
if '.bots' in msg[:5] or '.bot ' + nick in msg[:5 + len(nick)]:
|
||||||
if foo != False:
|
notice("I was written to correct your mistakes.")
|
||||||
privmsg(re.sub('\\\\/', '/', foo))
|
if '.source ' + nick in msg[:8 + len(nick)]:
|
||||||
|
notice('[Python] https://github.com/sys-fs/seddy')
|
||||||
|
elif is_sed.match(msg):
|
||||||
|
res = seddy(msg, history)
|
||||||
|
if res:
|
||||||
|
privmsg(re.sub('\\\\/', '/', foo))
|
||||||
|
Loading…
Reference in New Issue
Block a user