mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-23 11:12:47 +01:00
prefixChars == reply.whenAddressedBy.chars.
This commit is contained in:
parent
b6b22f7c23
commit
6b1f742e71
@ -491,12 +491,12 @@ def main():
|
||||
try:
|
||||
c = anything('What would you like your bot\'s prefix '
|
||||
'character(s) to be?')
|
||||
conf.supybot.prefixChars.set(c)
|
||||
conf.supybot.reply.whenAddressedBy.chars.set(c)
|
||||
except registry.InvalidRegistryValue, e:
|
||||
output(str(e))
|
||||
c = ''
|
||||
else:
|
||||
conf.supybot.prefixChars.set('')
|
||||
conf.supybot.reply.whenAddressedBy.chars.set('')
|
||||
|
||||
###
|
||||
# logging variables.
|
||||
|
@ -180,7 +180,8 @@ class Misc(callbacks.Privmsg):
|
||||
cb = irc.getCallback(args[0]) # No pop, we'll use this later.
|
||||
if cb is not None:
|
||||
command = callbacks.canonicalName(privmsgs.getArgs(args[1:]))
|
||||
command = command.lstrip(conf.supybot.prefixChars())
|
||||
prefixChars = conf.supybot.reply.whenAddressedBy.chars()
|
||||
command = command.lstrip(prefixChars)
|
||||
name = ' '.join(args)
|
||||
if hasattr(cb, 'isCommand') and cb.isCommand(command):
|
||||
method = getattr(cb, command)
|
||||
@ -197,7 +198,7 @@ class Misc(callbacks.Privmsg):
|
||||
return
|
||||
command = callbacks.canonicalName(name)
|
||||
# Users might expect "@help @list" to work.
|
||||
# command = command.lstrip(conf.supybot.prefixChars())
|
||||
# command = command.lstrip(conf.supybot.reply.whenAddressedBy.chars())
|
||||
cbs = callbacks.findCallbackForCommand(irc, command)
|
||||
if len(cbs) > 1:
|
||||
tokens = [command]
|
||||
|
@ -65,27 +65,36 @@ import supybot.ircmsgs as ircmsgs
|
||||
import supybot.ircutils as ircutils
|
||||
import supybot.registry as registry
|
||||
|
||||
def addressed(nick, msg, prefixChars=None, whenAddressedByNick=None):
|
||||
def addressed(nick, msg, prefixChars=None,
|
||||
prefixStrings=None, whenAddressedByNick=None):
|
||||
"""If msg is addressed to 'name', returns the portion after the address.
|
||||
Otherwise returns the empty string.
|
||||
"""
|
||||
assert msg.command == 'PRIVMSG'
|
||||
(target, payload) = msg.args
|
||||
def get(group):
|
||||
if ircutils.isChannel(target):
|
||||
group = group.get(target)
|
||||
return group()
|
||||
def stripPrefixStrings(payload):
|
||||
for prefixString in prefixStrings:
|
||||
if payload.startswith(prefixString):
|
||||
payload = payload[len(prefixString):].lstrip()
|
||||
return payload
|
||||
|
||||
assert msg.command == 'PRIVMSG'
|
||||
(target, payload) = msg.args
|
||||
if prefixChars is None:
|
||||
prefixChars = get(conf.supybot.prefixChars)
|
||||
prefixChars = get(conf.supybot.reply.whenAddressedBy.chars)
|
||||
if whenAddressedByNick is None:
|
||||
whenAddressedByNick = get(conf.supybot.reply.whenAddressedByNick)
|
||||
whenAddressedByNick = get(conf.supybot.reply.whenAddressedBy.nick)
|
||||
if prefixStrings is None:
|
||||
prefixStrings = get(conf.supybot.reply.whenAddressedBy.strings)
|
||||
nick = ircutils.toLower(nick)
|
||||
# Ok, let's see if it's a private message.
|
||||
if ircutils.nickEqual(target, nick):
|
||||
if payload[0] in prefixChars:
|
||||
return payload[1:].strip()
|
||||
else:
|
||||
return payload.strip()
|
||||
payload = stripPrefixStrings(payload)
|
||||
while payload and payload[0] in prefixChars:
|
||||
payload = payload[1:].lstrip()
|
||||
return payload
|
||||
# Ok, not private. Does it start with our nick?
|
||||
elif whenAddressedByNick and \
|
||||
ircutils.toLower(payload).startswith(nick):
|
||||
@ -101,6 +110,8 @@ def addressed(nick, msg, prefixChars=None, whenAddressedByNick=None):
|
||||
return ''
|
||||
except ValueError: # split didn't work.
|
||||
return ''
|
||||
elif payload and any(payload.startswith, prefixStrings):
|
||||
return stripPrefixStrings(payload)
|
||||
elif payload and payload[0] in prefixChars:
|
||||
return payload[1:].strip()
|
||||
elif conf.supybot.reply.whenNotAddressed():
|
||||
|
49
src/conf.py
49
src/conf.py
@ -252,22 +252,6 @@ registerGlobalValue(supybot, 'channels',
|
||||
SpaceSeparatedSetOfChannels([], """Determines what channels the bot will
|
||||
join when it connects to the server."""))
|
||||
|
||||
class ValidPrefixChars(registry.String):
|
||||
"""Value must contain only ~!@#$%^&*()_-+=[{}]\\|'\";:,<.>/?"""
|
||||
def setValue(self, v):
|
||||
if v.translate(string.ascii, '`~!@#$%^&*()_-+=[{}]\\|\'";:,<.>/?'):
|
||||
self.error()
|
||||
registry.String.setValue(self, v)
|
||||
|
||||
registerChannelValue(supybot, 'prefixChars',
|
||||
ValidPrefixChars('', """Determines what prefix characters the bot will
|
||||
reply to. A prefix character is a single character that the bot will use
|
||||
to determine what messages are addressed to it; when there are no prefix
|
||||
characters set, it just uses its nick. Each character in this string is
|
||||
interpreted individually; you can have multiple prefixChars simultaneously,
|
||||
and if any one of them is used as a prefix the bot will assume it is being
|
||||
addressed."""))
|
||||
|
||||
class DefaultCapabilities(registry.SpaceSeparatedListOfStrings):
|
||||
List = ircutils.IrcSet
|
||||
# We use a keyword argument trick here to prevent eval'ing of code that
|
||||
@ -392,10 +376,6 @@ registerChannelValue(supybot.reply, 'withNickPrefix',
|
||||
registry.Boolean(True, """Determines whether the bot will always prefix the
|
||||
user's nick to its reply to that user's command."""))
|
||||
|
||||
registerChannelValue(supybot.reply, 'whenAddressedByNick',
|
||||
registry.Boolean(True, """Determines whether the bot will reply when people
|
||||
address it by its nick, rather than with a prefix character."""))
|
||||
|
||||
registerChannelValue(supybot.reply, 'whenNotAddressed',
|
||||
registry.Boolean(False, """Determines whether the bot should attempt to
|
||||
reply to all messages even if they don't address it (either via its nick
|
||||
@ -428,6 +408,35 @@ registerChannelValue(supybot.reply, 'showSimpleSyntax',
|
||||
will only reply with the syntax of the command (the first line of the
|
||||
help) rather than the full help."""))
|
||||
|
||||
class ValidPrefixChars(registry.String):
|
||||
"""Value must contain only ~!@#$%^&*()_-+=[{}]\\|'\";:,<.>/?"""
|
||||
def setValue(self, v):
|
||||
if v.translate(string.ascii, '`~!@#$%^&*()_-+=[{}]\\|\'";:,<.>/?'):
|
||||
self.error()
|
||||
registry.String.setValue(self, v)
|
||||
|
||||
registerGroup(supybot.reply, 'whenAddressedBy')
|
||||
registerChannelValue(supybot.reply.whenAddressedBy, 'chars',
|
||||
ValidPrefixChars('', """Determines what prefix characters the bot will
|
||||
reply to. A prefix character is a single character that the bot will use
|
||||
to determine what messages are addressed to it; when there are no prefix
|
||||
characters set, it just uses its nick. Each character in this string is
|
||||
interpreted individually; you can have multiple prefixChars simultaneously,
|
||||
and if any one of them is used as a prefix the bot will assume it is being
|
||||
addressed."""))
|
||||
|
||||
registerChannelValue(supybot.reply.whenAddressedBy, 'strings',
|
||||
registry.SpaceSeparatedSetOfStrings([], """Determines what strings the bot
|
||||
will reply to when they are at the beginning of the message. Whereas
|
||||
prefix.chars can only be one character (although there can be many of
|
||||
them), this variable is a space-separated list of strings, so you can
|
||||
set something like '@@ ??' and the bot will reply when a message is
|
||||
prefixed by either @@ or ??."""))
|
||||
|
||||
registerChannelValue(supybot.reply.whenAddressedBy, 'nick',
|
||||
registry.Boolean(True, """Determines whether the bot will reply when people
|
||||
address it by its nick, rather than with a prefix character."""))
|
||||
|
||||
###
|
||||
# Replies
|
||||
###
|
||||
|
@ -50,7 +50,7 @@ supybot.log.stdout: False
|
||||
supybot.log.level: DEBUG
|
||||
supybot.log.detailedTracebacks: False
|
||||
supybot.throttleTime: 0
|
||||
supybot.prefixChars: @
|
||||
supybot.reply.whenAddressedBy.chars: @
|
||||
supybot.protocols.irc.throttleTime: -1
|
||||
supybot.networks.test.server: should.not.need.this
|
||||
supybot.nick: test
|
||||
|
@ -61,9 +61,9 @@ class ConfigTestCase(ChannelPluginTestCase):
|
||||
'AssertionError')
|
||||
|
||||
def testSearch(self):
|
||||
self.assertNotError('config search prefixChars')
|
||||
self.assertNotError('config channel prefixChars @')
|
||||
self.assertNotRegexp('config search prefixChars', self.channel)
|
||||
self.assertNotError('config search chars')
|
||||
self.assertNotError('config channel reply.whenAddressedBy.chars @')
|
||||
self.assertNotRegexp('config search chars', self.channel)
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||
|
@ -145,9 +145,9 @@ class FunctionsTestCase(SupyTestCase):
|
||||
self.assertEqual('foobar--', callbacks.canonicalName('foobar--'))
|
||||
|
||||
def testAddressed(self):
|
||||
oldprefixchars = str(conf.supybot.prefixChars)
|
||||
oldprefixchars = str(conf.supybot.reply.whenAddressedBy.chars)
|
||||
nick = 'supybot'
|
||||
conf.supybot.prefixChars.set('~!@')
|
||||
conf.supybot.reply.whenAddressedBy.chars.set('~!@')
|
||||
inChannel = ['~foo', '@foo', '!foo',
|
||||
'%s: foo' % nick, '%s foo' % nick,
|
||||
'%s: foo' % nick.capitalize(), '%s: foo' % nick.upper()]
|
||||
@ -160,7 +160,7 @@ class FunctionsTestCase(SupyTestCase):
|
||||
self.assertEqual('foo', callbacks.addressed(nick, msg), msg)
|
||||
msg = ircmsgs.privmsg(nick, 'foo')
|
||||
self.assertEqual('foo', callbacks.addressed(nick, msg))
|
||||
conf.supybot.prefixChars.set(oldprefixchars)
|
||||
conf.supybot.reply.whenAddressedBy.chars.set(oldprefixchars)
|
||||
msg = ircmsgs.privmsg('#foo', '%s::::: bar' % nick)
|
||||
self.assertEqual('bar', callbacks.addressed(nick, msg))
|
||||
msg = ircmsgs.privmsg('#foo', '%s: foo' % nick.upper())
|
||||
|
@ -132,7 +132,7 @@ class PluginTestCase(SupyTestCase):
|
||||
for irc in world.ircs[:]:
|
||||
irc._reallyDie()
|
||||
# Set conf variables appropriately.
|
||||
conf.supybot.prefixChars.setValue('@')
|
||||
conf.supybot.reply.whenAddressedBy.chars.setValue('@')
|
||||
conf.supybot.reply.detailedErrors.setValue(True)
|
||||
conf.supybot.reply.whenNotCommand.setValue(True)
|
||||
self.myVerbose = world.myVerbose
|
||||
@ -194,7 +194,8 @@ class PluginTestCase(SupyTestCase):
|
||||
timeout = self.timeout
|
||||
if self.myVerbose:
|
||||
print # Extra newline, so it's pretty.
|
||||
if not usePrefixChar and query[0] in conf.supybot.prefixChars():
|
||||
prefixChars = conf.supybot.reply.whenAddressedBy.chars()
|
||||
if not usePrefixChar and query[0] in prefixChars:
|
||||
query = query[1:]
|
||||
msg = ircmsgs.privmsg(to, query, prefix=frm)
|
||||
if self.myVerbose:
|
||||
@ -376,8 +377,9 @@ class ChannelPluginTestCase(PluginTestCase):
|
||||
timeout = self.timeout
|
||||
if self.myVerbose:
|
||||
print # Newline, just like PluginTestCase.
|
||||
if query[0] not in conf.supybot.prefixChars() and usePrefixChar:
|
||||
query = conf.supybot.prefixChars()[0] + query
|
||||
prefixChars = conf.supybot.reply.whenAddressedBy.chars()
|
||||
if query[0] not in prefixChars and usePrefixChar:
|
||||
query = prefixChars[0] + query
|
||||
msg = ircmsgs.privmsg(to, query, prefix=frm)
|
||||
if self.myVerbose:
|
||||
print 'Feeding: %r' % msg
|
||||
|
Loading…
Reference in New Issue
Block a user