refactor bot replies.

This commit is contained in:
Elián Hanisch 2012-07-29 23:36:18 -03:00
parent d241665208
commit 8205738099
2 changed files with 45 additions and 33 deletions

View File

@ -1533,8 +1533,8 @@ class Bantracker(callbacks.Plugin):
def comment(self, irc, msg, args, ids, kickmsg): def comment(self, irc, msg, args, ids, kickmsg):
"""<id>[,<id> ...] [<comment>][, <duration>] """<id>[,<id> ...] [<comment>][, <duration>]
Reads or adds the <comment> for the ban with <id>, Reads or adds the <comment> for the ban with <id>, use @bansearch to
use @bansearch to find the id of a ban find the id of a ban. Using <duration> will set the duration of the ban.
""" """
def addComment(id, nick, msg): def addComment(id, nick, msg):
@ -1555,7 +1555,7 @@ class Bantracker(callbacks.Plugin):
for id in splitID(ids): for id in splitID(ids):
try: try:
mask, channel, removal = self._getBan(id) self._getBan(id)
except ValueError: except ValueError:
irc.reply("I don't know any ban with id %s." % id) irc.reply("I don't know any ban with id %s." % id)
continue continue
@ -1564,15 +1564,11 @@ class Bantracker(callbacks.Plugin):
addComment(id, nick, kickmsg) addComment(id, nick, kickmsg)
if duration is not None: if duration is not None:
# set duration time # set duration time
type = guessBanType(mask)
if type not in ('ban', 'quiet'):
continue
try: try:
self._setBanDuration(id, duration) self._setBanDuration(id, duration)
banset.append(str(id)) banset.append(str(id))
except Exception as exc: except Exception as exc:
irc.reply("Failed to set duration time on ban %s (%s)" % (id, exc)) irc.reply("Failed to set duration time on %s (%s)" % (id, exc))
else: else:
data = readComment(id) data = readComment(id)
if data: if data:
@ -1582,25 +1578,30 @@ class Bantracker(callbacks.Plugin):
else: else:
irc.reply("No comments recorded for ban %s" % id) irc.reply("No comments recorded for ban %s" % id)
# success reply. If duration time used, say which ones. # success reply. If duration time used, say which ones were set.
if kickmsg: if kickmsg:
if banset: if banset:
irc.replySuccess("%s set to expire." % ', '.join(banset)) try:
time = 'after ' + timeElapsed(duration)
except ValueError:
time = 'soon'
irc.reply("Comment added. %s will be removed %s." \
% (utils.str.format('%L', banset), time))
else: else:
irc.replySuccess() # only a comment
irc.reply("Comment added.")
comment = wrap(comment, ['something', optional('text')]) comment = wrap(comment, ['something', optional('text')])
def duration(self, irc, msg, args, ids, duration): def duration(self, irc, msg, args, ids, duration):
"""[<id>[,<id> ...]] [<duration>] """[<id>[,<id> ...]] [<duration>]
Sets the duration of a ban. If <duration> isn't given show when a ban expires. Sets the duration of a ban. If <duration> isn't given show when a ban expires. f no <id> is given shows the ids of bans set to expire.
If no <id> is given shows the ids of bans set to expire.
""" """
if ids is None: if ids is None:
count = len(self.managedBans) count = len(self.managedBans)
L = [ str(item.ban.id) for item in self.managedBans ] L = [ str(item.ban.id) for item in self.managedBans ]
irc.reply("%s bans set to expire: %s" % (count, ', '.join(L))) irc.reply("%s bans set to expire: %s" % (count, utils.str.format('%L', L)))
return return
if duration is not None: if duration is not None:
@ -1640,10 +1641,13 @@ class Bantracker(callbacks.Plugin):
expires = None expires = None
if br: if br:
expires = (br.ban.when + br.expires) - nowSeconds() expires = (br.ban.when + br.expires) - nowSeconds()
try: if expires > 0:
expires = "expires in %s" % timeElapsed(expires) try:
except ValueError: expires = "expires in %s" % timeElapsed(expires)
expires = "expires soon" except ValueError:
expires = "expires soon"
else:
expires = "expired and will be removed soon"
else: else:
if type in ('quiet', 'ban'): if type in ('quiet', 'ban'):
if not removal: if not removal:
@ -1658,7 +1662,11 @@ class Bantracker(callbacks.Plugin):
# reply with the bans ids that were correctly set. # reply with the bans ids that were correctly set.
if banset: if banset:
irc.reply("%s set to expire." % ', '.join(banset)) try:
time = 'after ' + timeElapsed(duration)
except ValueError:
time = 'soon'
irc.reply("%s will be removed %s." % (utils.str.format('%L', banset), time))
duration = wrap(duration, [optional('something'), optional('text')]) duration = wrap(duration, [optional('something'), optional('text')])

View File

@ -129,31 +129,34 @@ class BantrackerTestCase(ChannelPluginTestCase):
self.feedBan('asd!*@*') self.feedBan('asd!*@*')
self.assertResponse('comment 1', 'No comments recorded for ban 1') self.assertResponse('comment 1', 'No comments recorded for ban 1')
self.assertResponse('comment 1 this is a test', self.assertResponse('comment 1 this is a test',
'The operation succeeded.') 'Comment added.')
self.assertRegexp('comment 1', 'test: this is a test$') self.assertRegexp('comment 1', 'test: this is a test$')
self.assertResponse('comment 1 this is a test, another test', self.assertResponse('comment 1 this is a test, another test',
'The operation succeeded.') 'Comment added.')
self.feedBan('nick', mode='k') self.feedBan('nick', mode='k')
self.assertResponse('comment 2 this is a kick, 2week', self.assertResponse('comment 2 this is a kick, 2week',
'The operation succeeded.') "Failed to set duration time on 2 (not a ban or quiet)")
msg = self.irc.takeMsg()
self.assertEqual(msg.args[1], 'test: Comment added.')
self.assertResponse('comment 1 not a valid, duration 2', self.assertResponse('comment 1 not a valid, duration 2',
'The operation succeeded.') 'Comment added.')
def testMultiComment(self): def testMultiComment(self):
self.feedBan('asd!*@*') self.feedBan('asd!*@*')
self.feedBan('qwe!*@*') self.feedBan('qwe!*@*')
self.assertResponse('comment 1,2,3 this is a test', self.assertResponse('comment 1,2,3 this is a test, 2 days',
"I don't know any ban with id 3.") "I don't know any ban with id 3.")
msg = self.irc.takeMsg() msg = self.irc.takeMsg()
self.assertEqual(msg.args[1], "test: The operation succeeded.") self.assertEqual(msg.args[1],
self.assertRegexp('comment 1,2', 'test: this is a test$') "test: Comment added. 1 and 2 will be removed after 2 days.")
self.assertRegexp('comment 1,2', 'test: this is a test, 2 days$')
msg = self.irc.takeMsg() msg = self.irc.takeMsg()
self.assertTrue(msg.args[1].endswith("test: this is a test")) self.assertTrue(msg.args[1].endswith("test: this is a test, 2 days"))
def testCommentDuration(self): def testCommentDuration(self):
self.feedBan('asd!*@*') self.feedBan('asd!*@*')
self.assertResponse('comment 1 this is a test, 1 week 10', self.assertResponse('comment 1 this is a test, 1 week 10',
'The operation succeeded. 1 set to expire.') 'Comment added. 1 will be removed after 1 week.')
self.assertRegexp('comment 1', 'test: this is a test, 1 week 10$') self.assertRegexp('comment 1', 'test: this is a test, 1 week 10$')
self.assertRegexp('duration 1', 'expires in 1 week$') self.assertRegexp('duration 1', 'expires in 1 week$')
@ -385,7 +388,7 @@ class BantrackerTestCase(ChannelPluginTestCase):
self.feedBan('asd!*@*') self.feedBan('asd!*@*')
cb.autoRemoveBans(self.irc) cb.autoRemoveBans(self.irc)
self.assertFalse(cb.managedBans) self.assertFalse(cb.managedBans)
self.assertNotError('duration 1 1') self.assertResponse('duration 1 1', "1 will be removed soon.")
self.assertTrue(cb.managedBans) # ban in list self.assertTrue(cb.managedBans) # ban in list
print 'waiting 2 secs ...' print 'waiting 2 secs ...'
time.sleep(2) time.sleep(2)
@ -414,10 +417,11 @@ class BantrackerTestCase(ChannelPluginTestCase):
def testDurationMultiSet(self): def testDurationMultiSet(self):
self.feedBan('asd!*@*') self.feedBan('asd!*@*')
self.assertResponse('duration 1,2 10', self.assertResponse('duration 1,2 10d',
"Failed to set duration time on 2 (unknow id)") "Failed to set duration time on 2 (unknow id)")
msg = self.irc.takeMsg() msg = self.irc.takeMsg()
self.assertEqual(msg.args[1], "test: 1 set to expire.") self.assertEqual(msg.args[1],
"test: 1 will be removed after 1 week and 3 days.")
def testDurationQuiet(self): def testDurationQuiet(self):
@ -435,7 +439,7 @@ class BantrackerTestCase(ChannelPluginTestCase):
self.assertResponse('duration 1 0', self.assertResponse('duration 1 0',
"Failed to set duration time on 1 (not a ban or quiet)") "Failed to set duration time on 1 (not a ban or quiet)")
self.feedBan('$a:nick') self.feedBan('$a:nick')
self.assertResponse('duration 2 0', '2 set to expire.') self.assertResponse('duration 2 0', '2 will be removed soon.')
def testDurationBadId(self): def testDurationBadId(self):
self.assertResponse('duration 1 0', self.assertResponse('duration 1 0',
@ -530,6 +534,6 @@ class BantrackerTestCase(ChannelPluginTestCase):
self.assertNotError('duration 1 1d') self.assertNotError('duration 1 1d')
self.assertResponse('duration', "1 bans set to expire: 1") self.assertResponse('duration', "1 bans set to expire: 1")
self.assertNotError('duration 2 1d') self.assertNotError('duration 2 1d')
self.assertResponse('duration', "2 bans set to expire: 1, 2") self.assertResponse('duration', "2 bans set to expire: 1 and 2")