var slackbot = require('./lib/bot');
var http = require('http');
var querystring = require('querystring');
var request = require('request');
var config = {
    server: 'irc.freenode.com',
    nick: '_slack_bot',
    username: 'mslackbot612',
    token: process.env.TOKEN ||'', // get from https://api.slack.com/web#basics
    income_url: process.env.INCOME_URL || '',
    outcome_token: process.env.OUTCOME_TOKEN || '',
    channels: {
        '#g0v.tw': '#g0v_tw'
    },
    users: {
    },
    // optionals
    floodProtection: true,
    silent: false // keep the bot quiet
};
var slackUsers = {};
var slackChannels = {};
function updateLists () {
  request.get({
      url: 'https://slack.com/api/users.list?token=' + config.token
  }, function (error, response, body) {
    var res = JSON.parse(body);
    console.log('updated:', new Date());
    res.members.map(function (member) {
      slackUsers[member.id] = member.name;
    });
  });

  request.get({
      url: 'https://slack.com/api/channels.list?token=' + config.token
  }, function (error, response, body) {
    var res = JSON.parse(body);
    res.channels.map(function (channel) {
      slackChannels[channel.id] = channel.name;
    });
  });

  setTimeout(function () {
    updateLists()
  }, 10 * 60 * 1000);
}

updateLists();
var slackbot = new slackbot.Bot(config);
slackbot.listen();

var server = http.createServer(function (req, res) {
  if (req.method == 'POST') {
    req.on('data', function(data) {
      var payload = querystring.parse(data.toString());
      if (payload.token == config.outcome_token && payload.user_name != 'slackbot') {
        var ircMsg = "<" + payload.user_name + "> " + payload.text;
        var channel = Object.keys(config.channels)[0];
        ircMsg = ircMsg.replace(/<#C\w{8}>/g, function (matched) {
            var channel_id = matched.match(/#(C\w{8})/)[1];
            return '#' + slackChannels[channel_id];
        });
        ircMsg = ircMsg.replace(/<@U\w{8}>/g, function (matched) {
            var member_id = matched.match(/@(U\w{8})/)[1];
            return '@' + slackUsers[member_id];
        });
        ircMsg = ircMsg.replace(/<https?\:\/\/.+>/, function (matched) {
          return matched.replace('<','').replace('>','').replace(/&amp;/g, '&');
        });
        slackbot.speak(channel, ircMsg);
        res.end('done');
      }
      res.end('request should not be from slackbot or must have matched token.')
    });
  } else {
    res.end('recieved request (not post)');
  }
});

server.listen(5555);
console.log("Server running at http://localhost:5555/");

@@ -1732,16 +1732,151 @@
 ls)%5B0%5D;%0A
+        /*%0A         * Channel ID -%3E Channel Name%0A         * Member ID -%3E Member Name%0A         * decoed URL and remove %3C, %3E%0A         */%0A
         
@@ -1926,34 +1926,32 @@
 ion (matched) %7B%0A
-  
           var ch
@@ -1994,34 +1994,32 @@
 )%5B1%5D;%0A          
-  
 return '#' + sla
@@ -2056,33 +2056,8 @@
   %7D)
-;%0A        ircMsg = ircMsg
 .rep
@@ -2102,26 +2102,24 @@
 %7B%0A          
-  
 var member_i
@@ -2155,34 +2155,32 @@
 )%5B1%5D;%0A          
-  
 return '@' + sla
@@ -2213,33 +2213,8 @@
   %7D)
-;%0A        ircMsg = ircMsg
 .rep
@@ -2236,16 +2236,17 @@
 %5C/%5C/.+%3E/
+g
 , functi
@@ -2351,17 +2351,61 @@
       %7D)
-;
+.replace(/&lt;/g,'%3C').replace(/&gt;/g, '%3E');%0A
 %0A       

update ircMsg replacement