### # Copyright (c) 2020, Valentin Lorentz # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions, and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions, and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the author of this software nor the name of # contributors to this software may be used to endorse or promote products # derived from this software without specific prior written consent. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. ### from supybot import conf, utils, plugins, ircutils, ircmsgs, callbacks from supybot.commands import * try: from supybot.i18n import PluginInternationalization _ = PluginInternationalization("Autocomplete") except ImportError: # Placeholder that allows to run the plugin on a bot # without the i18n module _ = lambda x: x REQUEST_TAG = "+draft/autocomplete-request" RESPONSE_TAG = "+draft/autocomplete-response" def _getAutocompleteResponse(irc, msg, payload): """Returns the value of the +draft/autocomplete-response tag for the given +draft/autocomplete-request payload.""" tokens = callbacks.tokenize(payload, channel=msg.channel, network=irc.network) normalized_payload = " ".join(tokens) candidates = _getCandidates(irc, normalized_payload) if not candidates: return "" # strip what the user already typed assert all(candidate.startswith(normalized_payload) for candidate in candidates) normalized_payload_length = len(normalized_payload) candidate_commands = [ candidate[normalized_payload_length:] for candidate in candidates ] tokenized_candidates = [ callbacks.tokenize(c, channel=msg.channel, network=irc.network) for c in candidate_commands ] response_items = {candidate[0] for candidate in tokenized_candidates} return "\t".join(sorted(response_items)) def _getCandidates(irc, normalized_payload): """Returns a list of commands starting with the normalized_payload.""" candidates = set() for cb in irc.callbacks: cb_commands = cb.listCommands() # copy them with the plugin name (optional when calling a command) # at the beginning plugin_name = cb.canonicalName() cb_commands += [plugin_name + " " + command for command in cb_commands] candidates |= { command for command in cb_commands if command.startswith(normalized_payload) } return candidates class Autocomplete(callbacks.Plugin): """Provides command completion for IRC clients that support it.""" def _enabled(self, irc, msg): return conf.supybot.protocols.irc.experimentalExtensions() and self.registryValue( "enabled", msg.channel, irc.network ) def doTagmsg(self, irc, msg): if REQUEST_TAG not in msg.server_tags: return if not "msgid" in msg.server_tags: return if not self._enabled(irc, msg): return msgid = msg.server_tags["msgid"] text = msg.server_tags[REQUEST_TAG] # using callbacks._addressed instead of callbacks.addressed, as # callbacks.addressed would tag the m payload = callbacks._addressed(irc, msg, payload=text) if not payload: # not addressed return # marks used by '_addressed' are usually prefixes (char, string, # nick), but may also be suffixes (with # supybot.reply.whenAddressedBy.nick.atEnd); but there is no way to # have it in the middle of the message AFAIK. assert payload in text if not text.endswith(payload): # If there is a suffix, it means the end of the text is used to # address the bot, so it can't be a method to be completed. return autocomplete_response = _getAutocompleteResponse(irc, msg, payload) if not autocomplete_response: return target = msg.channel or ircutils.nickFromHostmask(msg.prefix) irc.queueMsg( ircmsgs.IrcMsg( server_tags={ "+draft/reply": msgid, RESPONSE_TAG: autocomplete_response, }, command="TAGMSG", args=[target], ) ) Class = Autocomplete # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: