2020-08-29 16:33:29 +02:00
|
|
|
###
|
2021-10-17 09:54:06 +02:00
|
|
|
# Copyright (c) 2020-2021, Valentin Lorentz
|
2020-08-29 16:33:29 +02:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
###
|
|
|
|
|
2020-08-29 18:39:03 +02:00
|
|
|
from supybot import conf, ircutils, ircmsgs, callbacks
|
|
|
|
from supybot.i18n import PluginInternationalization
|
2020-08-29 16:33:29 +02:00
|
|
|
|
2020-08-29 18:39:03 +02:00
|
|
|
_ = PluginInternationalization("Autocomplete")
|
2020-08-29 16:33:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
REQUEST_TAG = "+draft/autocomplete-request"
|
2020-08-29 16:42:19 +02:00
|
|
|
RESPONSE_TAG = "+draft/autocomplete-response"
|
2020-08-29 16:33:29 +02:00
|
|
|
|
|
|
|
|
2020-08-29 18:38:39 +02:00
|
|
|
def _commonPrefix(L):
|
|
|
|
"""Takes a list of lists, and returns their longest common prefix."""
|
|
|
|
assert L
|
|
|
|
if len(L) == 1:
|
|
|
|
return L[0]
|
|
|
|
|
|
|
|
for n in range(1, max(map(len, L)) + 1):
|
|
|
|
prefix = L[0][:n]
|
|
|
|
for item in L[1:]:
|
|
|
|
if prefix != item[:n]:
|
|
|
|
return prefix[0:-1]
|
|
|
|
|
|
|
|
assert False
|
|
|
|
|
|
|
|
|
2020-08-29 16:33:29 +02:00
|
|
|
def _getAutocompleteResponse(irc, msg, payload):
|
2020-08-29 16:42:19 +02:00
|
|
|
"""Returns the value of the +draft/autocomplete-response tag for the given
|
2020-08-29 16:33:29 +02:00
|
|
|
+draft/autocomplete-request payload."""
|
2020-08-29 18:44:32 +02:00
|
|
|
tokens = callbacks.tokenize(
|
|
|
|
payload, channel=msg.channel, network=irc.network
|
|
|
|
)
|
2020-08-29 16:33:29 +02:00
|
|
|
normalized_payload = " ".join(tokens)
|
|
|
|
|
2020-08-29 18:38:39 +02:00
|
|
|
candidate_commands = _getCandidates(irc, normalized_payload)
|
2020-08-29 16:33:29 +02:00
|
|
|
|
2020-08-29 18:38:39 +02:00
|
|
|
if len(candidate_commands) == 0:
|
|
|
|
# No result
|
|
|
|
return None
|
|
|
|
elif len(candidate_commands) == 1:
|
|
|
|
# One result, return it directly
|
|
|
|
commands = candidate_commands
|
|
|
|
else:
|
|
|
|
# Multiple results, return only the longest common prefix + one word
|
2020-08-29 16:33:29 +02:00
|
|
|
|
2020-08-29 18:38:39 +02:00
|
|
|
tokenized_candidates = [
|
|
|
|
callbacks.tokenize(c, channel=msg.channel, network=irc.network)
|
|
|
|
for c in candidate_commands
|
|
|
|
]
|
|
|
|
|
|
|
|
common_prefix = _commonPrefix(tokenized_candidates)
|
|
|
|
|
|
|
|
words_after_prefix = {
|
|
|
|
candidate[len(common_prefix)] for candidate in tokenized_candidates
|
|
|
|
}
|
2020-08-29 18:19:00 +02:00
|
|
|
|
2020-08-29 18:44:32 +02:00
|
|
|
commands = [
|
|
|
|
" ".join(common_prefix + [word]) for word in words_after_prefix
|
|
|
|
]
|
2020-08-29 18:19:00 +02:00
|
|
|
|
2020-08-29 18:38:39 +02:00
|
|
|
# strip what the user already typed
|
|
|
|
assert all(command.startswith(normalized_payload) for command in commands)
|
|
|
|
normalized_payload_length = len(normalized_payload)
|
2020-08-29 18:44:32 +02:00
|
|
|
response_items = [
|
|
|
|
command[normalized_payload_length:] for command in commands
|
|
|
|
]
|
2020-08-29 18:19:00 +02:00
|
|
|
|
|
|
|
return "\t".join(sorted(response_items))
|
2020-08-29 16:33:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
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 |= {
|
2020-08-29 18:44:32 +02:00
|
|
|
command
|
|
|
|
for command in cb_commands
|
|
|
|
if command.startswith(normalized_payload)
|
2020-08-29 16:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return candidates
|
|
|
|
|
|
|
|
|
|
|
|
class Autocomplete(callbacks.Plugin):
|
|
|
|
"""Provides command completion for IRC clients that support it."""
|
|
|
|
|
|
|
|
def _enabled(self, irc, msg):
|
2021-06-19 16:39:23 +02:00
|
|
|
return (
|
|
|
|
conf.supybot.protocols.irc.experimentalExtensions()
|
|
|
|
and self.registryValue("enabled", msg.channel, irc.network)
|
2020-08-29 16:33:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def doTagmsg(self, irc, msg):
|
|
|
|
if REQUEST_TAG not in msg.server_tags:
|
|
|
|
return
|
2020-08-29 18:39:03 +02:00
|
|
|
if "msgid" not in msg.server_tags:
|
2020-08-29 16:33:29 +02:00
|
|
|
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,
|
2020-08-29 16:42:19 +02:00
|
|
|
RESPONSE_TAG: autocomplete_response,
|
2020-08-29 16:33:29 +02:00
|
|
|
},
|
|
|
|
command="TAGMSG",
|
|
|
|
args=[target],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
Class = Autocomplete
|
|
|
|
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|