Fediverse: Add usernameSnarfer.

This commit is contained in:
Valentin Lorentz 2020-05-09 20:47:11 +02:00
parent 76950126b5
commit e9ff57a1c9
2 changed files with 24 additions and 6 deletions

View File

@ -51,9 +51,9 @@ def configure(advanced):
Fediverse = conf.registerPlugin("Fediverse") Fediverse = conf.registerPlugin("Fediverse")
# This is where your configuration variables (if any) should go. For example: conf.registerChannelValue(Fediverse, 'usernameSnarfer',
# conf.registerGlobalValue(Fediverse, 'someConfigVariableName', registry.Boolean(False, _("""Determines whether the bot will output the
# registry.Boolean(False, _("""Help for someConfigVariableName."""))) profile of @username@hostname accounts it sees in channel messages.""")))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -46,7 +46,7 @@ importlib.reload(ap)
_ = PluginInternationalization("Fediverse") _ = PluginInternationalization("Fediverse")
_username_re = re.compile("@(?P<localuser>[^@]+)@(?P<hostname>[^@]+)") _username_regexp = re.compile("@(?P<localuser>[^@ ]+)@(?P<hostname>[^@ ]+)")
class FediverseHttp(httpserver.SupyHTTPServerCallback): class FediverseHttp(httpserver.SupyHTTPServerCallback):
@ -113,10 +113,11 @@ class FediverseHttp(httpserver.SupyHTTPServerCallback):
self.wfile.write(json.dumps(actor).encode()) self.wfile.write(json.dumps(actor).encode())
class Fediverse(callbacks.Plugin): class Fediverse(callbacks.PluginRegexp):
"""Fetches information from ActivityPub servers.""" """Fetches information from ActivityPub servers."""
threaded = True threaded = True
regexps = ['usernameSnarfer']
def __init__(self, irc): def __init__(self, irc):
super().__init__(irc) super().__init__(irc)
@ -138,7 +139,7 @@ class Fediverse(callbacks.Plugin):
def _get_actor(self, irc, username): def _get_actor(self, irc, username):
if username in self._actor_cache: if username in self._actor_cache:
return self._actor_cache[username] return self._actor_cache[username]
match = _username_re.match(username) match = _username_regexp.match(username)
if not match: if not match:
irc.errorInvalid("fediverse username", username) irc.errorInvalid("fediverse username", username)
localuser = match.group("localuser") localuser = match.group("localuser")
@ -185,6 +186,23 @@ class Fediverse(callbacks.Plugin):
) )
) )
def usernameSnarfer(self, irc, msg, match):
try:
actor = self._get_actor(irc, match.group(0))
except ap.ActivityPubError:
# Be silent on errors
return
irc.reply(
_("\x02%s\x02 (%s): %s")
% (
actor["name"],
self._format_actor_username(actor),
utils.web.htmlToText(actor["summary"]),
)
)
usernameSnarfer.__doc__ = _username_regexp.pattern
@wrap(["somethingWithoutSpaces"]) @wrap(["somethingWithoutSpaces"])
def featured(self, irc, msg, args, username): def featured(self, irc, msg, args, username):
"""<@user@instance> """<@user@instance>