changeset 8:8391a973c9dc

Possibilite d'ajouter des callback. Concretement, il est possible d'ajouter dans les codes des objets implementant la classe Callback. Quand un message correspondant sera recu, on executera l'action associee.
author Franck Deroche <defr@defr.net>
date Sat, 19 May 2007 22:24:31 +0200
parents c7972ff66484
children 888f56363d6d
files org/defr/bots/Bot.java org/defr/bots/Callback.java
diffstat 2 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/org/defr/bots/Bot.java	Sat May 19 17:47:17 2007 +0200
+++ b/org/defr/bots/Bot.java	Sat May 19 22:24:31 2007 +0200
@@ -2,6 +2,8 @@
 
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.jibble.pircbot.IrcException;
 import org.jibble.pircbot.NickAlreadyInUseException;
@@ -14,9 +16,11 @@
  */
 public class Bot extends PircBot {
 	protected Configuration conf;
+	protected Map<String, Callback> messageListeners;
 	
 	public Bot() {
 		super();
+		messageListeners = new HashMap<String, Callback>();
 	}
 	
 	/**
@@ -24,6 +28,7 @@
 	 * @param botName
 	 */
 	public Bot(String botName) {
+		this();
 		conf = new Configuration("conf/" + botName + ".xml");
 		setName(conf.getBotName());
 		setLogin(conf.getBotName());
@@ -35,6 +40,17 @@
 		}
 	}
 	
+	public void onMessage(String channel, String sender, String login, String host, String message) {
+		if(messageListeners.containsKey(message.toLowerCase())) {
+			messageListeners.get(message.toLowerCase()).go(sender, channel, message);
+			messageListeners.remove(message.toLowerCase());
+		}
+	}
+	
+	public void addMessageListener(String message, Callback callback) {
+		messageListeners.put(message.toLowerCase(), callback);
+	}
+	
 	/**
 	 * Connexion au serveur IRC défini dans le fichier de configuration
 	 * @throws NickAlreadyInUseException
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org/defr/bots/Callback.java	Sat May 19 22:24:31 2007 +0200
@@ -0,0 +1,5 @@
+package org.defr.bots;
+
+public interface Callback {
+	public void go(String sender, String replyTo, String message);
+}