From 4e8de6000582bbc2e45e4c96ffacfc139f0b2451 Mon Sep 17 00:00:00 2001 From: James Lu Date: Thu, 19 May 2016 22:23:34 -0700 Subject: [PATCH] core: Rewrite WHOIS handlers to simply listen to a custom hook (PYLINK_CUSTOM_WHOIS) --- coreplugin.py | 15 +++---------- docs/technical/writing-plugins.md | 15 +------------ plugins/relay.py | 36 +++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/coreplugin.py b/coreplugin.py index 1d6d5c4..65041e9 100644 --- a/coreplugin.py +++ b/coreplugin.py @@ -140,18 +140,9 @@ def handle_whois(irc, source, command, args): # <- 317 GL GL 15 1437632859 :seconds idle, signon time f(server, 317, source, "%s 0 %s :seconds idle, signon time" % (nick, user.ts)) - for func in world.whois_handlers: - # Iterate over custom plugin WHOIS handlers. They return a tuple - # or list with two arguments: the numeric, and the text to send. - try: - res = func(irc, target) - if res: - num, text = res - f(server, num, source, text) - except Exception as e: - # Again, we wouldn't want this to crash our service, in case - # something goes wrong! - log.exception('(%s) Error caught in WHOIS handler: %s', irc.name, e) + # Call custom WHOIS handlers via the PYLINK_CUSTOM_WHOIS hook. + irc.callHooks([source, 'PYLINK_CUSTOM_WHOIS', {'target': target, 'server': server}]) + # 318: End of WHOIS. f(server, 318, source, "%s :End of /WHOIS list" % nick) utils.add_hook(handle_whois, 'WHOIS') diff --git a/docs/technical/writing-plugins.md b/docs/technical/writing-plugins.md index 8714977..95a6534 100644 --- a/docs/technical/writing-plugins.md +++ b/docs/technical/writing-plugins.md @@ -46,20 +46,7 @@ The third option, `WHOIS` handlers, are a lot more limited compared to the other WHOIS replies are special in that any plugins wishing to add lines to a WHOIS reply must do so after the regular WHOIS lines (handled by the core), but before a special "End of WHOIS" line. This means that the regular hooks mechanism, which are only called after core handling, doesn't work here. -An example of a plugin WHOIS handler is in the relay plugin. WHOIS handler functions are added to the `world.whois_handlers` list using a simple `append()`. They should return either nothing or a two-length list: the first item being the WHOIS numeric, and the second the raw whois text. - -``` -def relayWhoisHandler(irc, target): - user = irc.users[target] - orig = getLocalUser(irc, target) - if orig: - network, remoteuid = orig - remotenick = world.networkobjects[network].users[remoteuid].nick - return [320, "%s :is a remote user connected via PyLink Relay. Home " - "network: %s; Home nick: %s" % (user.nick, network, - remotenick)] -world.whois_handlers.append(relayWhoisHandler) -``` +An example of a plugin WHOIS handler is in the [relay plugin](../../plugins/relay.py) (search for `relayWhoisHandler`. WHOIS handler functions are added to the `world.whois_handlers` list using a simple `append()`. They should return either nothing or a list of two-length tuples: the first item being the WHOIS numeric, and the second the raw whois text. ### Sending data to IRC diff --git a/plugins/relay.py b/plugins/relay.py index 99c90ff..6cec883 100644 --- a/plugins/relay.py +++ b/plugins/relay.py @@ -777,21 +777,35 @@ def relayModes(irc, remoteirc, sender, channel, modes=None): rsid = getRemoteSid(remoteirc, irc) remoteirc.proto.mode(rsid, remotechan, supported_modes) -def relayWhoisHandler(irc, target): +### EVENT HANDLERS + +def handle_relay_whois(irc, source, command, args): """ WHOIS handler for the relay plugin. """ - user = irc.users[target] - orig = getOrigUser(irc, target) - if orig: - network, remoteuid = orig - remotenick = world.networkobjects[network].users[remoteuid].nick - return [320, "%s :is a remote user connected via PyLink Relay. Home " - "network: %s; Home nick: %s" % (user.nick, network, - remotenick)] -world.whois_handlers.append(relayWhoisHandler) + target = args['target'] + server = args['server'] + targetuser = irc.users[target] -### GENERIC EVENT HOOK HANDLERS + def wreply(num, text): + """Convenience wrapper to return WHOIS replies.""" + # WHOIS replies are by convention prefixed with the target user's nick. + text = '%s %s' % (targetuser.nick, text) + irc.proto.numeric(server, num, source, text) + + + # Get the real user for the WHOIS target. + origuser = getOrigUser(irc, target) + if origuser: + homenet, uid = origuser + realirc = world.networkobjects[homenet] + realuser = realirc.users[uid] + netname = realirc.serverdata.get('netname', homenet) + + wreply(320, ":is a remote user connected via PyLink Relay. Home network: %s; " + "Home nick: %s" % (netname, realuser.nick)) + +utils.add_hook(handle_relay_whois, 'PYLINK_CUSTOM_WHOIS') def handle_operup(irc, numeric, command, args): newtype = args['text'] + '_(remote)'