3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 13:09:23 +01:00

Services API rework

- Move nick/ident/host/gecos fetching from services_support into functions
- Remove the unused 'ident' argument from ServiceBot
- Rename the 'nick' argument in ServiceBot to 'default_nick'
- Define default nicks for the PyLink, Automode, and Games services
This commit is contained in:
James Lu 2017-08-21 21:19:38 -07:00
parent 3e150d8514
commit 89699051d5
5 changed files with 54 additions and 26 deletions

View File

@ -22,21 +22,10 @@ def spawn_service(irc, source, command, args):
# Get the ServiceBot object.
sbot = world.services[name]
# Look up the nick or ident in the following order:
# 1) Network specific nick/ident settings for this service (servers::irc.name::servicename_nick)
# 2) Global settings for this service (servicename::nick)
# 3) The preferred nick/ident combination defined by the plugin (sbot.nick / sbot.ident)
# 4) The literal service name.
# settings, and then falling back to the literal service name.
sbconf = conf.conf.get(name, {})
nick = irc.serverdata.get("%s_nick" % name) or sbconf.get('nick') or sbot.nick or name
ident = irc.serverdata.get("%s_ident" % name) or sbconf.get('ident') or sbot.ident or name
# Determine host the same way as above, except fall back to server hostname.
host = irc.serverdata.get("%s_host" % name) or sbconf.get('host') or irc.hostname()
# Determine realname the same way as above, except fall back to pylink:realname (and if that fails, the service name).
realname = irc.serverdata.get("%s_realname" % name) or sbconf.get('realname') or conf.conf['pylink'].get('realname') or name
nick = sbot.get_nick(irc)
ident = sbot.get_ident(irc)
host = sbot.get_host(irc)
realname = sbot.get_realname(irc)
# Spawning service clients with these umodes where supported. servprotect usage is a
# configuration option.
@ -143,4 +132,4 @@ utils.add_hook(handle_commands, 'PRIVMSG')
# TODO: be more specific, and possibly allow plugins to modify this to mention
# their features?
mydesc = "\x02PyLink\x02 provides extended network services for IRC."
utils.registerService('pylink', desc=mydesc, manipulatable=True)
utils.registerService('pylink', default_nick="PyLink", desc=mydesc, manipulatable=True)

View File

@ -24,7 +24,7 @@ myservice = utils.registerService("myservice", desc=desc)
- **`name`** - defines the service name (mandatory)
- `default_help` - Determines whether the default HELP command should be used for the service. Defaults to True.
- `default_list` - Determines whether the default LIST command should be used for the service. Defaults to True.
- `nick`, `ident` - Sets the default nick and ident for the service bot. If not given, these simply default to the service name.
- `default_nick` - Sets the default nick this service should use if the user doesn't provide it. Defaults to the same as the service name.
- `manipulatable` - Determines whether the bot is marked manipulatable. Only manipulatable clients can be force joined, etc. using PyLink commands. Defaults to False.
- `desc` - Sets the command description of the service. This is shown in the default HELP command if enabled.

View File

@ -12,7 +12,7 @@ mydesc = ("The \x02Automode\x02 plugin provides simple channel ACL management by
"to users matching hostmasks or exttargets.")
# Register ourselves as a service.
modebot = utils.registerService("automode", desc=mydesc)
modebot = utils.registerService("automode", default_nick="Automode", desc=mydesc)
reply = modebot.reply
error = modebot.error

View File

@ -8,7 +8,7 @@ from pylinkirc.log import log
mydesc = "The \x02Games\x02 plugin provides simple games for IRC."
gameclient = utils.registerService("Games", manipulatable=True, desc=mydesc)
gameclient = utils.registerService("Games", default_nick="Games", manipulatable=True, desc=mydesc)
reply = gameclient.reply # TODO find a better syntax for ServiceBot.reply()
error = gameclient.error # TODO find a better syntax for ServiceBot.error()
# commands

View File

@ -183,18 +183,14 @@ class ServiceBot():
PyLink IRC Service class.
"""
def __init__(self, name, default_help=True, default_list=True,
nick=None, ident=None, manipulatable=False, desc=None):
# Service name
def __init__(self, name, default_help=True, default_list=True, manipulatable=False, default_nick=None, desc=None):
# Service name and default nick
self.name = name
self.default_nick = default_nick
# TODO: validate nick, ident, etc. on runtime as well
assert isNick(name), "Invalid service name %r" % name
# Nick/ident to take. Defaults to the same as the service name if not given.
self.nick = nick
self.ident = ident
# Tracks whether the bot should be manipulatable by the 'bots' plugin and other commands.
self.manipulatable = manipulatable
@ -363,6 +359,49 @@ class ServiceBot():
self.commands[name].append(func)
return func
def get_nick(self, irc):
"""
Returns the preferred nick for this service bot on the given network. The following fields are checked in the given order:
# 1) Network specific nick settings for this service (servers:<netname>:servicename_nick)
# 2) Global settings for this service (servicename:nick)
# 3) The service's hardcoded default nick.
# 4) The literal service name.
"""
sbconf = conf.conf.get(self.name, {})
return irc.serverdata.get("%s_nick" % self.name) or sbconf.get('nick') or self.default_nick or self.name
def get_ident(self, irc):
"""
Returns the preferred ident for this service bot on the given network. The following fields are checked in the given order:
# 1) Network specific ident settings for this service (servers:<netname>:servicename_ident)
# 2) Global settings for this service (servicename:ident)
# 3) The service's hardcoded default nick.
# 4) The literal service name.
"""
sbconf = conf.conf.get(self.name, {})
return irc.serverdata.get("%s_ident" % self.name) or sbconf.get('ident') or self.default_nick or self.name
def get_host(self, irc):
"""
Returns the preferred hostname for this service bot on the given network. The following fields are checked in the given order:
# 1) Network specific hostname settings for this service (servers:<netname>:servicename_host)
# 2) Global settings for this service (servicename:host)
# 3) The PyLink server hostname.
"""
sbconf = conf.conf.get(self.name, {})
return irc.serverdata.get("%s_host" % self.name) or sbconf.get('host') or irc.hostname()
def get_realname(self, irc):
"""
Returns the preferred real name for this service bot on the given network. The following fields are checked in the given order:
# 1) Network specific realname settings for this service (servers:<netname>:servicename_realname)
# 2) Global settings for this service (servicename:realname)
# 3) The globally configured real name (pylink:realname).
# 4) The literal service name.
"""
sbconf = conf.conf.get(self.name, {})
return irc.serverdata.get("%s_realname" % self.name) or sbconf.get('realname') or conf.conf['pylink'].get('realname') or self.name
def _show_command_help(self, irc, command, private=False, shortform=False):
"""
Shows help for the given command.