3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-24 19:52:53 +01:00

core: Rewrite WHOIS handlers to simply listen to a custom hook (PYLINK_CUSTOM_WHOIS)

This commit is contained in:
James Lu 2016-05-19 22:23:34 -07:00
parent a2a009cac4
commit 4e8de60005
3 changed files with 29 additions and 37 deletions

View File

@ -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')

View File

@ -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

View File

@ -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)'