import uk.co.uwcs.choob.*;
import uk.co.uwcs.choob.modules.*;
import uk.co.uwcs.choob.support.*;
import uk.co.uwcs.choob.support.events.*;
import java.util.*;
import java.util.regex.*;

public class FactoidObject
{
	public FactoidObject()
	{
	}

	public FactoidObject( String subject, String info )
	{
		this.subject = subject;
		this.info = info;
	}

	public int id;
	public String subject;
	public String info;
}

public class Factoids
{
	public String filterFactoidsRegex = "(\\w{4,})\\s+(?:is|was)\\s+(.{4,})";

	public String[] info()
	{
		return new String[] {
			"Factoid watching/query plugin.",
			"The Choob Team",
			"choob@uwcs.co.uk",
			mods.util.getVersion()
		};
	}

	private Modules mods;
	private IRCInterface irc;
	public Factoids(Modules mods, IRCInterface irc)
	{
		this.mods = mods;
		this.irc = irc;
	}

	public void filterFactoids( Message msg ) throws ChoobException
	{
		Matcher factoidMatcher = (Pattern.compile( filterFactoidsRegex )).matcher( msg.getMessage() );

		factoidMatcher.find();

		try
		{
			FactoidObject fact = new FactoidObject( factoidMatcher.group(1).toLowerCase() , factoidMatcher.group(2) );
			mods.odb.save( fact );
		}
		catch( IllegalStateException e )
		{
			// Well shiver me timbers! What the hell causes this?
		}
	}

	public String[] helpTopics = { "Remember" };

	public String[] helpRemember = {
		  "The bot will automagically remember things which are said in the"
		+ " channels of the form '<a> is <b>' or '<a> was <b>'."
	};

	public String[] helpCommandWhatIs = {
		"Ask the bot what it thinks something is.",
		"<Name>",
		"<Name> is the name of the object to enquire about"
	};
	public void commandWhatIs( Message msg ) throws ChoobException
	{
		List<String> params = mods.util.getParams(msg, 1);

		List facts = mods.odb.retrieve( FactoidObject.class , "SORT RANDOM LIMIT (1) WHERE subject = \"" + params.get(1).toLowerCase() + "\"");

		if( facts.size() > 0 )
		{
			FactoidObject fact = (FactoidObject)facts.get(0);

			irc.sendContextReply( msg, params.get(1) + " is " + fact.info );
		}
		else
		{
			irc.sendContextReply( msg, "I don't know anything about " + params.get(1) + "! Ask your mum.");
		}
	}
}

@@ -1758,16 +1758,164 @@
 g, 1);%0A%0A
+%09%09if (params.size()%3C2)%0A%09%09%7B%0A%09%09%09irc.sendContextReply(msg, %22Whatis what?%22);%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09final String item = params.get(1).replaceAll(%22%5C%5C?%22,%22%22);%0A%0A
 %09%09List f
@@ -2007,29 +2007,20 @@
 = %5C%22%22 + 
-params.get(1)
+item
 .toLower
@@ -2118,17 +2118,16 @@
 get(0);%0A
-%0A
 %09%09%09irc.s
@@ -2148,29 +2148,20 @@
 y( msg, 
-params.get(1)
+item
  + %22 is 
@@ -2188,20 +2188,16 @@
 %0A%09%09else%0A
-%09%09%7B%0A
 %09%09%09irc.s
@@ -2255,47 +2255,20 @@
 %22 + 
-params.get(1) + %22! Ask your mum.%22);%0A%09%09%7D
+item + %22!%22);
 %0A%09%7D%0A

Fix for #146: s/\?//.