1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
###
# Copyright (c) 2020-2021, 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, ircutils, ircmsgs, callbacks
from supybot.i18n import PluginInternationalization
_ = PluginInternationalization("Autocomplete")
REQUEST_TAG = "+draft/autocomplete-request"
RESPONSE_TAG = "+draft/autocomplete-response"
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
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)
candidate_commands = _getCandidates(irc, normalized_payload)
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
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
}
commands = [
" ".join(common_prefix + [word]) for word in words_after_prefix
]
# strip what the user already typed
assert all(command.startswith(normalized_payload) for command in commands)
normalized_payload_length = len(normalized_payload)
response_items = [
command[normalized_payload_length:] for command in commands
]
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 "msgid" not 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:
|