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
|
|
|
|
2019-09-07 21:38:21 +00:00
|
|
|
REGEX_SED = re.compile("^(?:(\\S+)[:,] )?s/")
|
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):
|
2020-02-09 16:26:08 +00:00
|
|
|
for_user = event["match"].group(1)
|
|
|
|
sed_s = event["message"]
|
|
|
|
if for_user:
|
|
|
|
sed_s = sed_s.split(" ", 1)[1]
|
|
|
|
if not self._closest_setting(event, "sed", False):
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
sed = utils.parse.sed.parse(event["message"])
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
event["stderr"].write("Invalid regex in pattern")
|
|
|
|
return
|
|
|
|
sed.replace = utils.irc.bold(sed.replace)
|
|
|
|
|
|
|
|
if self._closest_setting(event, "sed-sender-only", False):
|
|
|
|
for_user = event["user"].nickname
|
|
|
|
|
|
|
|
match_line = None
|
|
|
|
match_message = None
|
|
|
|
with utils.deadline():
|
|
|
|
for line in event["target"].buffer.get_all(for_user):
|
|
|
|
if not line.from_self:
|
|
|
|
match = sed.match(line.message)
|
|
|
|
if not match == line.message:
|
|
|
|
match_line = line
|
|
|
|
match_message = match
|
|
|
|
break
|
|
|
|
|
|
|
|
if match_line:
|
|
|
|
if match_line.action:
|
|
|
|
format = "* %s %s"
|
|
|
|
else:
|
|
|
|
format = "<%s> %s"
|
|
|
|
event["stdout"].write(format % (match_line.sender, match_message))
|