var irc = require('irc'),
    config = require('./lib/config'),
    strftime = require('strftime'),
    format = require('irc-colors'),
    rawjs = require('raw.js'),
    reddit = new rawjs(config.user_agent),
    before;

reddit.setupOAuth2(config.app.id, config.app.secret);

var client = new irc.Client(config.irc.host, config.irc.nick, {
  channels: [config.irc.channel],
  messageSplit: 2048
});

client.addListener('registered', function(msg) {
  client.say('NickServ', 'identify ' + config.irc.pass);
});

client.addListener('error', function(err) {
  // if (err.rawCommand != '421') console.log(err);
  log('ERROR in irc client');
  console.log(err);
});

client.addListener('nick', function(oldnick, newnick) {
  log('-- ' + oldnick + ' is now known as ' + newnick);
});

client.addListener('join', function(channel, nick) {
  log('>> ' + nick + ' joined');
});

client.addListener('part', function(channel, nick, reason) {
  log('<< ' + nick + ' left (' + reason + ')');
});

client.addListener('quit', function(nick, reason) {
  log('<< ' + nick + ' quit (' + reason + ')');
});

client.addListener('topic', function(channel, topic, nick) {
  log('!! ' + nick + ' changed the topic to "' + topic + '"');
});

client.addListener('kick', function(channel, nick, by, reason) {
  log('<< ' + nick + ' was kicked by ' + by + '(' + reason + ')');
});

client.addListener('message', function(from, to, msg) {
  log(from + ' ' + to + ' ' + msg);
});

var say = function(msg) {
  client.say(config.irc.channel, msg)
  log(config.irc.nick + ' ' + config.irc.channel + ' ' + msg);
}

var log = function(msg) {
  console.log(strftime('%F %T ') + msg)
}

// check for new r/philippines posts
//
var new_post = function() {

  var options = {
    r: 'Philippines',
    limit: 5,
    all: true
  };

  if (before) {
    options.before = before;
  }

  reddit.new(options, function(err, response) {
    if (err) {
      log('ERROR fetching new posts');
      console.log(err);
    } else {
      if (response.children.length) {
        if (before) {
          for (var i = response.children.length-1; i >= 0; i--) {
            var post = response.children[i].data,
                msg = format.green.underline('http://redd.it/' + post.id)
                    + ' ' + format.navy.bold(post.title)
                    + ' by ' + format.brown.italic(post.author)

            if (post.selftext) {
              msg += ': ' + post.selftext.replace(/\n/g, ' ');;
            }

            say(msg);
          }
        }

        before = response.children[0].data.name;
      }
    }
  });

  setTimeout(new_post, 30000);
}; 

setTimeout(new_post, 15000);

@@ -207,22 +207,22 @@
 t),%0A    
-before
+latest
 ;%0A%0Areddi
@@ -1686,16 +1686,66 @@
 osts
+ and if any%0A// are found, post them to the channel
 %0A//%0Avar 
 new_
@@ -1740,24 +1740,31 @@
 %0A//%0Avar 
+report_
 new_post
  = funct
@@ -1755,16 +1755,17 @@
 new_post
+s
  = funct
@@ -1851,58 +1851,8 @@
 %7D;%0A%0A
-  if (before) %7B%0A    options.before = before;%0A  %7D%0A%0A
   re
@@ -2036,22 +2036,22 @@
     if (
-before
+latest
 ) %7B%0A    
@@ -2160,18 +2160,22 @@
 %5Bi%5D.data
-,%0A
+;%0A%0A   
         
@@ -2171,30 +2171,117 @@
             
-      
+if (post.created_utc %3E latest) %7B%0A%0A              latest = post.created_utc;%0A%0A              var
  msg = forma
@@ -2291,18 +2291,8 @@
 reen
-.underline
 ('ht
@@ -2317,16 +2317,18 @@
 ost.id)%0A
+  
         
@@ -2392,24 +2392,26 @@
             
+  
 + ' by ' + f
@@ -2447,32 +2447,34 @@
 r)%0A%0A            
+  
 if (post.selftex
@@ -2474,24 +2474,26 @@
 selftext) %7B%0A
+  
             
@@ -2548,35 +2548,39 @@
 );;%0A            
+  
 %7D%0A%0A
+  
             say(
@@ -2584,16 +2584,31 @@
 ay(msg);
+%0A%0A            %7D
 %0A       
@@ -2635,14 +2635,14 @@
     
-before
+latest
  = r
@@ -2670,12 +2670,19 @@
 ata.
-name
+created_utc
 ;%0A  
@@ -2713,24 +2713,31 @@
 Timeout(
+report_
 new_post
 , 30000)
@@ -2728,16 +2728,17 @@
 new_post
+s
 , 30000)
@@ -2755,24 +2755,31 @@
 Timeout(
+report_
 new_post
 , 15000)
@@ -2774,11 +2774,11 @@
 post
+s
 , 
-1
 5000

Use created_utc to find newer posts. 'before' doesn't work if that submission gets deleted.