<commit_before>if ( true )
	return;

var http 		= require( "http" );
var xml2js 		= require( "xml2js" );
var entities 	= new ( require( "html-entities" ).AllHtmlEntities );

var lastPostTime = 0;
var readyToGo = false;
var handledPosts = {};

// Setup our threads table
db.run( "CREATE TABLE IF NOT EXISTS threads ( id INTEGER PRIMARY KEY AUTOINCREMENT, thread TEXT UNIQUE )" );

function IntToBase62( i ) {

	var digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	var buf = [];

	do {

		var m = i % 62;
		buf.unshift( digits.charAt( m ) );

		i = Math.floor( i / 62 );

	} while ( i > 0 );

	return buf.join( "" );

}

function HandlePosts( posts ) {

	// Handle newer posts first
	for ( var i = posts.length - 1; i >= 0; i-- ) {

		var post = posts[ i ][ "$" ]

		lastPostTime = Math.max( post.date, lastPostTime );

		var html = entities.decode( post.html );

		// Read out some data badly
		var title = html.match(/href='showthread.*?>(.*?)</); title = title && title[1];
		var postID = html.match(/href='showthread\.php(.*?)&p=([0-9]+)/); postID = postID && postID[2];

		if ( !title || !postID || !readyToGo )
			continue;

		if ( handledPosts[ postID ] )
			continue;

		handledPosts[ postID ] = true;

		// We need a new scope for our title and postID values
		( function( title, postID ) {

			db.all( "SELECT thread FROM threads", function( err, rows ) {

				for ( var i = 0; i < rows.length; i++ ) {

					var lowerTitle = title.toLowerCase();

					if ( lowerTitle.indexOf( rows[i].thread.toLowerCase() ) != -1 ) {

						console.log( postID, IntToBase62( postID ) );
						bot.sendMessage( rows[i].thread + " - http://fcpn.ch/" + IntToBase62( postID ) );
						break;

					}

				}

			} );

		} )( title, postID );

	}

	readyToGo = true; // We ignore the first set of posts

}

function OnTicker( res ) {

	var buf = [];

	res.on( "data", function( chunk ) {
		buf.push( chunk );
	} );

	res.on( "end", function() {

		buf = buf.join( "" );

		xml2js.parseString( buf, function( err, res ) {

			// Facepunch is dodgy
			if ( err )
				return; 

			if ( !res || !res.newposts || !res.newposts.post )
				return;

			HandlePosts( res.newposts.post );

		} );

	} );

}

function RequestTicker() {

	http.get( {
		hostname: "facepunch.com",
		port: 80,
		path: "/fp_ticker.php?aj=1&lasttime=" + lastPostTime
	}, OnTicker );

}

setInterval( RequestTicker, 2500 );

// Commands for controlling what threads we want

bot.registerCommand( "fplist", function() {
	
	db.all( "SELECT id, thread FROM threads", function( err, rows ) {

		var data = "Listening for:";

		if ( rows.length == 0 )
			data += " nothing.";<commit_msg>Bad error handling for fp.js<commit_after>var request		= require( "request" );
var xml2js 		= require( "xml2js" );
var entities 	= new ( require( "html-entities" ).AllHtmlEntities );

var lastPostTime = 0;
var readyToGo = false;
var handledPosts = {};

// Setup our threads table
db.run( "CREATE TABLE IF NOT EXISTS threads ( id INTEGER PRIMARY KEY AUTOINCREMENT, thread TEXT UNIQUE )" );

function IntToBase62( i ) {

	var digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	var buf = [];

	do {

		var m = i % 62;
		buf.unshift( digits.charAt( m ) );

		i = Math.floor( i / 62 );

	} while ( i > 0 );

	return buf.join( "" );

}

function HandlePosts( posts ) {

	// Handle newer posts first
	for ( var i = posts.length - 1; i >= 0; i-- ) {

		var post = posts[ i ][ "$" ]

		lastPostTime = Math.max( post.date, lastPostTime );

		var html = entities.decode( post.html );

		// Read out some data badly
		var title = html.match(/href='showthread.*?>(.*?)</); title = title && title[1];
		var postID = html.match(/href='showthread\.php(.*?)&p=([0-9]+)/); postID = postID && postID[2];

		if ( !title || !postID || !readyToGo )
			continue;

		if ( handledPosts[ postID ] )
			continue;

		handledPosts[ postID ] = true;

		// We need a new scope for our title and postID values
		( function( title, postID ) {

			db.all( "SELECT thread FROM threads", function( err, rows ) {

				for ( var i = 0; i < rows.length; i++ ) {

					var lowerTitle = title.toLowerCase();

					if ( lowerTitle.indexOf( rows[i].thread.toLowerCase() ) != -1 ) {

						console.log( postID, IntToBase62( postID ) );
						bot.sendMessage( rows[i].thread + " - http://fcpn.ch/" + IntToBase62( postID ) );
						break;

					}

				}

			} );

		} )( title, postID );

	}

	readyToGo = true; // We ignore the first set of posts

}

function OnTicker( error, res, body ) {

	if ( error ) {
		console.trace( error );
		return;
	}

	xml2js.parseString( body, function( err, res ) {

		// Facepunch is dodgy
		if ( err )
			return; 

		if ( !res || !res.newposts || !res.newposts.post )
			return;

		HandlePosts( res.newposts.post );

	} );

}

function RequestTicker() {

	var options = {
		qs: {
			aj:			1,
			lasttime:	lastPostTime
		}
	}

	request( "http://facepunch.com/fp_ticker.php", options, OnTicker );

}

setInterval( RequestTicker, 2500 );

// Commands for controlling what threads we want

bot.registerCommand( "fplist", function() {
	
	db.all( "SELECT id, thread FROM threads", function( err, rows ) {

		var data = "Listening for:";

		if ( rows.length == 0 )
			data += " nothing.";
{'fraction_non_alphanumeric': 0.1589958158995816, 'fraction_numerical': 0.015214910612400151, 'mean_word_length': 2.8926720947446336, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5849091', 'n_tokens_mistral': 2173, 'n_tokens_neox': 1863, 'n_words': 512}