2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
import re, traceback
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
REGEX_SPLIT = re.compile("(?<!\\\\)/")
|
2019-09-07 21:38:21 +00:00
|
|
|
REGEX_SED = re.compile("^(?:(\\S+)[:,] )?s/")
|
2019-09-08 10:18:08 +00:00
|
|
|
SED_AMPERSAND = re.compile(r"((?:^|[^\\])(?:\\\\)*)&")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-06-28 22:16:05 +00:00
|
|
|
@utils.export("channelset",
|
|
|
|
utils.BoolSetting("sed","Disable/Enable sed in a channel"))
|
|
|
|
@utils.export("channelset", utils.BoolSetting("sed-sender-only",
|
|
|
|
"Disable/Enable sed only looking at the messages sent by the user"))
|
2018-09-27 11:08:07 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-10-30 14:58:48 +00:00
|
|
|
def _closest_setting(self, event, setting, default):
|
2019-05-18 17:35:47 +00:00
|
|
|
return event["target"].get_setting(setting,
|
2018-10-30 14:58:48 +00:00
|
|
|
event["server"].get_setting(setting, default))
|
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
@utils.hook("command.regex")
|
2019-06-26 13:37:26 +00:00
|
|
|
@utils.kwarg("command", "sed")
|
|
|
|
@utils.kwarg("pattern", REGEX_SED)
|
2016-03-29 11:56:58 +00:00
|
|
|
def channel_message(self, event):
|
|
|
|
sed_split = re.split(REGEX_SPLIT, event["message"], 3)
|
2019-09-01 08:58:10 +00:00
|
|
|
if len(sed_split) > 2:
|
2019-05-18 17:35:47 +00:00
|
|
|
if not self._closest_setting(event, "sed", False):
|
2019-04-27 13:49:57 +00:00
|
|
|
return
|
2018-08-05 14:35:23 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
regex_flags = 0
|
2019-12-01 08:06:44 +00:00
|
|
|
flags = (sed_split[3:] or [""])[0].split(" ", 1)[0]
|
2016-03-29 11:56:58 +00:00
|
|
|
count = None
|
|
|
|
|
|
|
|
last_flag = ""
|
|
|
|
for flag in flags:
|
|
|
|
if flag.isdigit():
|
|
|
|
if last_flag.isdigit():
|
|
|
|
count = int(str(count) + flag)
|
|
|
|
elif not count:
|
|
|
|
count = int(flag)
|
|
|
|
elif flag == "i":
|
|
|
|
regex_flags |= re.I
|
|
|
|
elif flag == "g":
|
|
|
|
count = 0
|
|
|
|
last_flag = flag
|
|
|
|
if count == None:
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
try:
|
|
|
|
pattern = re.compile(sed_split[1], regex_flags)
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2019-05-18 17:35:47 +00:00
|
|
|
event["stderr"].write("Invalid regex in pattern")
|
2016-03-29 11:56:58 +00:00
|
|
|
return
|
|
|
|
|
2019-09-01 08:58:10 +00:00
|
|
|
for_user = event["match"].group(1)
|
|
|
|
if self._closest_setting(event, "sed-sender-only", False):
|
|
|
|
for_user = event["user"].nickname
|
2019-08-30 16:17:31 +00:00
|
|
|
|
2019-11-27 12:59:59 +00:00
|
|
|
match_line = None
|
|
|
|
match = None
|
|
|
|
match_message = None
|
2019-08-30 17:13:12 +00:00
|
|
|
with utils.deadline():
|
2019-11-27 12:59:59 +00:00
|
|
|
for line in event["target"].buffer.get_all(for_user):
|
|
|
|
if not line.from_self:
|
|
|
|
message = line.notes.get("sed-line", line.message)
|
|
|
|
match = pattern.search(message)
|
|
|
|
if match and not REGEX_SED.match(message):
|
|
|
|
match_line = line
|
|
|
|
match = match.group(0)
|
|
|
|
match_message = message
|
2019-12-01 08:06:56 +00:00
|
|
|
break
|
2019-08-30 17:13:12 +00:00
|
|
|
|
2019-08-13 12:48:03 +00:00
|
|
|
if match:
|
2019-09-08 10:18:08 +00:00
|
|
|
replace = sed_split[2]
|
|
|
|
replace = replace.replace("\\/", "/")
|
2020-01-11 16:57:34 +00:00
|
|
|
|
|
|
|
with utils.deadline():
|
|
|
|
for found in SED_AMPERSAND.finditer(replace):
|
|
|
|
found = found.group(1)
|
|
|
|
replace.replace(found, "%s%s" % (found, match))
|
|
|
|
|
2019-11-27 12:59:59 +00:00
|
|
|
replace_color = utils.irc.bold(replace)
|
2019-09-08 10:18:08 +00:00
|
|
|
|
2019-11-27 12:59:59 +00:00
|
|
|
new_message = re.sub(pattern, replace, message, count)
|
|
|
|
new_message_color = re.sub(pattern, utils.irc.bold(replace),
|
|
|
|
message, count)
|
|
|
|
if match_line.action:
|
|
|
|
prefix = "* %s" % match_line.sender
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2019-11-27 12:59:59 +00:00
|
|
|
prefix = "<%s>" % match_line.sender
|
|
|
|
match_line.notes["sed-line"] = new_message
|
|
|
|
event["stdout"].write("%s %s" % (prefix, new_message_color))
|