 modulename = 'fw'
 blacklistfile = modulename + ".json"
 
-try
-  fwdata = fs.readFileSync blacklistfile, 'utf-8'
-  if fwdata
-    data = JSON.parse(fwdata)
-catch error
-  console.log('Unable to read file', error) unless error.code is 'ENOENT'
-
 module.exports = (robot) ->
 
-  robot.router.get "/#{robot.name}/bl", (req, res) ->
+  data = []
+  try
+    fwdata = fs.readFileSync blacklistfile, 'utf-8'
+    if fwdata
+      data = JSON.parse(fwdata)
+      console.log "#{modulename} blacklist loaded: " + data.length + " entries"
+  catch error
+    console.log('Unable to read file', error) unless error.code is 'ENOENT'
+
+  robot.router.get "/#{robot.name}/#{modulename}/blacklist/url", (req, res) ->
+    clientip = req.connection.remoteAddress || req.socket.remoteAddress ||
+      req.connection.socket.remoteAddress
+    logmsg = "#{modulename}: web request from #{clientip}: url blacklist"
+    robot.logger.info logmsg
+
     arr = []
     for obj in data
-      arr.push obj.val # XXX: unless expired
+      if obj.type == 'url' and moment(obj.expires).isAfter()
+        arr.push obj.val
     content = '# nothing here yet! #'
     content = arr.join "\n" if arr.length
     res.setHeader 'content-type', 'text/plain'
     res.end content
 
-  robot.respond /fw(?: help)$/, (msg) ->
+    logmsg = "#{modulename}: robot responded to web request: sent url blacklist"
+    robot.logger.info logmsg
+
+  robot.router.get "/#{robot.name}/#{modulename}/blacklist/cidr", (req, res) ->
+    clientip = req.connection.remoteAddress || req.socket.remoteAddress ||
+      req.connection.socket.remoteAddress
+    logmsg = "#{modulename}: web request from #{clientip}: cidr blacklist"
+    robot.logger.info logmsg
+
+    arr = []
+    for obj in data
+      if obj.type == 'cidr' and moment(obj.expires).isAfter()
+        arr.push obj.val
+    content = '# nothing here yet! #'
+    content = arr.join "\n" if arr.length
+    res.setHeader 'content-type', 'text/plain'
+    res.end content
+
+    logmsg = "#{modulename}: robot responded to web request: " +
+      "sent cidr blacklist"
+    robot.logger.info logmsg
+
+  robot.respond /fw(?: help|)$/, (msg) ->
     cmds = []
     arr = [
       modulename + " blacklist - show blacklist"

      expires: moment().add(1, 'months').format()
      type: msg.match[1]
      val: msg.match[2]
    if msg.match.length > 2
      expires = msg.match[3]
      extra = expires.match(/+(\d)([hdwMQy])/)
      if extra?
        n = extra[1]
        unit = extra[2]
        bl.expires = moment().add(n,unit).format()
      else if moment(expires).isValid()
        bl.expires = moment(expires)
      else
        failed = true
        logmsg = "invalid expiration date: #{expires}"

      logmsg = "#{modulename}: #{msg.envelope.user.name} requested: " +
        "blacklist #{bl.type} #{bl.val} expires #{bl.expires}"
      robot.logger.info logmsg

      data.push bl
      fs.writeFileSync blacklistfile, JSON.stringify(data), 'utf-8'

      logmsg = "#{modulename}: robot responded to #{msg.envelope.user.name}: " +
        "added entry to blacklist"
      robot.logger.info logmsg
