fulvia/modules/sed.py

104 lines
2.2 KiB
Python
Raw Normal View History

2018-03-16 03:13:43 -04:00
#!/usr/bin/env python3
"""
2020-01-08 07:51:00 -05:00
Correct the last thing you or someone else said. Uses standard sed
substitute notation eg. s/search/replace/flags
2018-03-16 03:13:43 -04:00
"""
import re
from module import hook
def setup(bot):
2020-01-07 18:58:19 -05:00
bot.memory['sed_lines'] = {}
2018-03-16 03:13:43 -04:00
@hook(True)
def collectlines(bot, trigger):
"""Create a temporary log of what people say"""
if trigger.is_privmsg:
return
2020-01-07 18:58:19 -05:00
if trigger.channel not in bot.memory['sed_lines']:
bot.memory['sed_lines'][trigger.channel] = {}
if trigger.nick not in bot.memory['sed_lines'][trigger.channel]:
bot.memory['sed_lines'][trigger.channel][trigger.nick] = []
2018-03-16 03:13:43 -04:00
2020-01-07 18:58:19 -05:00
templist = bot.memory['sed_lines'][trigger.channel][trigger.nick]
line = ' '.join(trigger.args)
2018-03-16 03:13:43 -04:00
if line.startswith("s/"): # Don't remember substitutions
return
2020-01-07 18:58:19 -05:00
templist.append(line)
2018-03-16 03:13:43 -04:00
del templist[:-10] # Keep the log to 10 lines per person
2020-01-07 18:58:19 -05:00
bot.memory['sed_lines'][trigger.channel][trigger.nick] = templist
2018-03-16 03:13:43 -04:00
@hook(True)
def findandreplace(bot, trigger):
if trigger.is_privmsg:
return
2020-09-09 19:33:53 -04:00
if not trigger.args[0]:
return
2020-01-07 18:58:19 -05:00
if not trigger.args[0].startswith('s') and trigger.args[0][-1] not in ':,':
return
line = ' '.join(trigger.args)
if 's/' not in line:
2018-03-16 03:13:43 -04:00
return
2020-01-07 18:58:19 -05:00
items = []
line = line[line.index('s/')+2:]
i = 0
while True:
i = line.find('/', i)
if i == -1:
break
if line[max(i-1, 0)] == '\\' and line[max(i-2, 0)] != '\\':
i += 1
pass
else:
items.append(line[:i])
line = line[i+1:]
i = 0
items.append(line)
2018-03-16 03:13:43 -04:00
2020-01-07 18:58:19 -05:00
if len(items) < 2:
2018-03-16 03:13:43 -04:00
return
2020-01-07 18:58:19 -05:00
find, replace = items[:2]
find = find.replace(r'\/', '/')
flags = items[2] if len(items) >= 3 else ''
2018-03-16 03:13:43 -04:00
if 'g' in flags:
count = 0
else:
count = 1
2020-01-07 18:58:19 -05:00
try:
if 'i' in flags:
regex = re.compile(find, re.I)
2018-03-16 03:13:43 -04:00
else:
2020-01-07 18:58:19 -05:00
regex = re.compile(find)
except re.error:
return
if not trigger.args[0].startswith('s'):
nick = trigger.args[0][:-1]
if nick not in bot.memory['sed_lines'][trigger.channel]:
return
else:
nick = trigger.nick
for line in reversed(bot.memory['sed_lines'][trigger.channel][nick]):
new_line = re.sub(regex, replace, line, count)
if new_line != line:
2018-03-16 03:13:43 -04:00
break
2020-01-07 18:58:19 -05:00
else:
return
2018-03-16 03:13:43 -04:00
2020-01-07 18:58:19 -05:00
if trigger.args[0].startswith('s'):
msg = f"{trigger.nick} \x02meant\x0f to say: {new_line}"
2018-03-16 03:13:43 -04:00
else:
2020-01-07 18:58:19 -05:00
msg = f"{trigger.nick} thinks {nick} \x02meant\x0f to say: {new_line}"
2018-03-16 03:13:43 -04:00
2020-01-07 18:58:19 -05:00
bot.msg(msg)