Skip to content

Commit 91accc0

Browse files
famfoprogvaljlu5
authored
SedRegex: Implement changing of sed response per channel (#1556)
Fixes #1433 Co-authored-by: Val Lorentz <[email protected]> Co-authored-by: James Lu <[email protected]>
1 parent 5ab7c8a commit 91accc0

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

plugins/SedRegex/config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ def configure(advanced):
5757
conf.registerChannelValue(SedRegex, 'ignoreRegex',
5858
registry.Boolean(True, _("""Should Perl/sed regex replacing
5959
ignore messages which look like valid regex?""")))
60+
conf.registerChannelValue(SedRegex, 'format',
61+
registry.String(_('$nick meant to say: $replacement'), _("""Sets the format
62+
string for a message edited by the original
63+
author. Required fields: $nick (nick of the
64+
author), $replacement (edited message)""")))
65+
conf.registerChannelValue(SedRegex.format, 'other',
66+
registry.String(_('$otherNick thinks $nick meant to say: $replacement'), _("""
67+
Sets the format string for a message edited by
68+
another author. Required fields: $nick (nick
69+
of the original author), $otherNick (nick of
70+
the editor), $replacement (edited message)""")))
6071
conf.registerGlobalValue(SedRegex, 'processTimeout',
6172
registry.PositiveFloat(0.5, _("""Sets the timeout when processing a single
6273
regexp. The default should be adequate unless

plugins/SedRegex/plugin.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,6 @@ def _replacer_process(self, irc, msg, target, pattern, replacement, count, messa
222222
if self.registryValue('ignoreRegex', msg.channel, irc.network) and m.tagged(TAG_IS_REGEX):
223223
self.log.debug("Skipping message %s because it is tagged as isRegex", m.args[1])
224224
continue
225-
if m.nick == msg.nick:
226-
messageprefix = msg.nick
227-
else:
228-
messageprefix = '%s thinks %s' % (msg.nick, m.nick)
229225

230226
try:
231227
replace_result = pattern.search(text)
@@ -239,8 +235,15 @@ def _replacer_process(self, irc, msg, target, pattern, replacement, count, messa
239235

240236
subst = axe_spaces(subst)
241237

242-
return _("%s meant to say: %s") % \
243-
(messageprefix, subst)
238+
if m.nick == msg.nick:
239+
fmt = self.registryValue('format', msg.channel, irc.network)
240+
env = {'replacement': subst}
241+
else:
242+
fmt = self.registryValue('format.other', msg.channel, irc.network)
243+
env = {'otherNick': msg.nick, 'replacement': subst}
244+
245+
return ircutils.standardSubstitute(irc, m, fmt, env)
246+
244247
except Exception as e:
245248
self.log.warning(_("SedRegex error: %s"), e, exc_info=True)
246249
raise

plugins/SedRegex/test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,23 @@ def testSlashInNicks(self):
279279
with conf.supybot.protocols.irc.strictRfc.context(True):
280280
self.assertSnarfNoResponse('%s: s/123/321/' % ircutils.nickFromHostmask(frm), frm=self.__class__.other2)
281281

282+
def testFmtString(self):
283+
fmt = "<$nick>: $replacement"
284+
with conf.supybot.plugins.sedregex.format.context(fmt):
285+
self.feedMsg('frog')
286+
self.feedMsg('s/frog/frogged/')
287+
m = self.getMsg(' ')
288+
self.assertIn('<%s>: frogged' % self.nick, str(m))
289+
290+
def testFmtStringOtherPerson(self):
291+
fmt = "(edited by $otherNick) <$nick>: $replacement"
292+
with conf.supybot.plugins.sedregex.format.other.context(fmt):
293+
self.feedMsg('frog', frm=self.__class__.other)
294+
self.feedMsg('s/frog/frogged/', frm=self.__class__.other2)
295+
m = self.getMsg(' ')
296+
self.assertIn('(edited by %s) <%s>: frogged' % (ircutils.nickFromHostmask(self.__class__.other2),
297+
ircutils.nickFromHostmask(self.__class__.other)), str(m))
298+
282299
# TODO: test ignores
283300

284301
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

0 commit comments

Comments
 (0)