From 03790b5939ac414422dfbc6bba139bf72ab5a3bf Mon Sep 17 00:00:00 2001
From: James Lu <GLolol@overdrivenetworks.com>
Date: Sat, 21 May 2016 22:55:06 -0700
Subject: [PATCH] relay: implement LINKED <netname> to filter by network

Closes #227.
---
 plugins/relay.py | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/plugins/relay.py b/plugins/relay.py
index 0ea480e..fcf6dcb 100644
--- a/plugins/relay.py
+++ b/plugins/relay.py
@@ -1487,9 +1487,9 @@ def delink(irc, source, args):
 
 @utils.add_cmd
 def linked(irc, source, args):
-    """takes no arguments.
+    """[<network>]
 
-    Returns a list of channels shared across the relay."""
+    Returns a list of channels shared across the relay. If <network> is given, filters output to channels linked to the given network."""
 
     # Only show remote networks that are marked as connected.
     remote_networks = [netname for netname, ircobj in world.networkobjects.copy().items()
@@ -1503,9 +1503,22 @@ def linked(irc, source, args):
     s = 'Connected networks: \x02%s\x02 %s' % (irc.name, ' '.join(remote_networks))
     irc.msg(source, s)
 
+    net = ''
+    try:
+        net = args[0]
+    except:
+        pass
+    else:
+        irc.msg(source, "Showing channels linked to %s:" % net)
+
     # Sort the list of shared channels when displaying
     for k, v in sorted(db.items()):
 
+        # Skip if we're filtering by network and the network given isn't relayed
+        # to the channel.
+        if net and not (net == k[0] or net in [link[0] for link in v['links']]):
+            continue
+
         # Bold each network/channel name pair
         s = '\x02%s%s\x02 ' % k
         remoteirc = world.networkobjects.get(k[0])