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("(?<!\\\\)/")
|
|
|
|
REGEX_SED = re.compile("^s/")
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.export("channelset", {"setting": "sed",
|
2018-09-27 11:08:07 +00:00
|
|
|
"help": "Disable/Enable sed in a channel",
|
2018-10-03 12:22:37 +00:00
|
|
|
"validate": utils.bool_or_none})
|
|
|
|
@utils.export("channelset", {"setting": "sed-sender-only",
|
2018-09-27 11:08:07 +00:00
|
|
|
"help": "Disable/Enable sed only looking at the messages sent by the user",
|
2018-10-03 12:22:37 +00:00
|
|
|
"validate": utils.bool_or_none})
|
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):
|
|
|
|
return event["channel"].get_setting(setting,
|
|
|
|
event["server"].get_setting(setting, default))
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.message.channel")
|
2016-03-29 11:56:58 +00:00
|
|
|
def channel_message(self, event):
|
|
|
|
sed_split = re.split(REGEX_SPLIT, event["message"], 3)
|
|
|
|
if event["message"].startswith("s/") and len(sed_split) > 2:
|
2018-10-30 14:58:48 +00:00
|
|
|
if event["action"] or not self._closest_setting(event, "sed",
|
|
|
|
False):
|
2018-08-05 14:35:23 +00:00
|
|
|
return
|
2019-04-27 13:49:57 +00:00
|
|
|
is_ignored_f = short_url = self.exports.get_one("is-ignored",
|
|
|
|
lambda _1, _2: False)
|
2019-05-06 16:08:56 +00:00
|
|
|
if is_ignored_f(event["server"], event["user"], "sed"):
|
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
|
|
|
|
flags = (sed_split[3:] or [""])[0]
|
|
|
|
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()
|
2018-09-19 12:25:12 +00:00
|
|
|
self.events.on("send.stderr").call(target=event["channel"],
|
|
|
|
module_name="Sed", server=event["server"],
|
2016-03-29 11:56:58 +00:00
|
|
|
message="Invalid regex in pattern")
|
|
|
|
return
|
2019-04-15 20:55:53 +00:00
|
|
|
replace = utils.irc.bold(sed_split[2].replace("\\/", "/"))
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
for_user = event["user"].nickname if self._closest_setting(event,
|
|
|
|
"sed-sender-only", False) else None
|
2018-08-28 13:55:35 +00:00
|
|
|
line = event["channel"].buffer.find(pattern, from_self=False,
|
2017-10-27 12:15:33 +00:00
|
|
|
for_user=for_user, not_pattern=REGEX_SED)
|
2016-03-29 11:56:58 +00:00
|
|
|
if line:
|
|
|
|
new_message = re.sub(pattern, replace, line.message, count)
|
|
|
|
if line.action:
|
|
|
|
prefix = "* %s" % line.sender
|
|
|
|
else:
|
|
|
|
prefix = "<%s>" % line.sender
|
2018-09-19 12:25:12 +00:00
|
|
|
self.events.on("send.stdout").call(target=event[
|
2016-03-29 11:56:58 +00:00
|
|
|
"channel"], module_name="Sed", server=event["server"],
|
|
|
|
message="%s %s" % (prefix, new_message))
|