Browse Source

Ajout utilisation d'un fichier properties pour configurer le bot

Martin Passard 7 years ago
parent
commit
d4e215e997

+ 34 - 0
ressources/config/config.properties

@@ -0,0 +1,34 @@
+#### UneFede configuration
+
+### Setting the bot ###
+
+## IRC server and channel settings ##
+#Main server address
+SERVER=irc.geeknode.net
+#Main port
+PORT=6667
+#Channels with
+CHANNELS=#Marmat,#ffdn
+
+##IRC bot admins setting ##
+#Set who will be messaged if the bot needs to: exemple: if it get's kicked from somewhere
+#To set multiple admins, use the character ',' to split them.
+Admins=Marmat
+
+## Bot settings
+#Time the bot will wait until it retry to connect when disconnected
+Timeout_before_reconnecting=360
+#Time in milliseconds beween each messages the bot can say
+Time_between_messages=200
+#Time in miliseconds the bot will wait before reconnecting to a channel it just went kicked of
+Wait_before_reconnecting_when_kicked=10000
+
+##Developpment and debug settings
+#Will the bot be explicit or stay quiet (Warning: It Speaks a LOT!)
+Debug=true
+
+##DB cache settings
+#Time between each +reload allowed in seconds
+Minimum_time_beetween_+reload=360
+#Time between each autoreload in seconds
+CacheReloader_timeout=3600

+ 36 - 0
ressources/config/default.properties

@@ -0,0 +1,36 @@
+#### UneFede DEFAULT configuration
+#### DO NOT EDIT HERE !! ####
+#### OVERRIDEN BY config.properties ####
+
+### Setting the bot ###
+
+## IRC server and channel settings ##
+#Main server address
+SERVER=irc.geeknode.net
+#Main port
+PORT=6667
+#Channels with
+CHANNELS=#Marmat,#ffdn
+
+##IRC bot admins setting ##
+#Set who will be messaged if the bot needs to: exemple: if it get's kicked from somewhere
+#To set multiple admins, use the character ',' to split them.
+Admins=Marmat
+
+## Bot settings
+#Time the bot will wait until it retry to connect when disconnected
+Timeout_before_reconnecting=360
+#Time in milliseconds beween each messages the bot can say
+Time_between_messages=200
+#Time in miliseconds the bot will wait before reconnecting to a channel it just went kicked of
+Wait_before_reconnecting_when_kicked=10000
+
+##Developpment and debug settings
+#Will the bot be explicit or stay quiet (Warning: It Speaks a LOT!)
+Debug=false;
+
+##DB cache settings
+#Time between each +reload allowed in seconds
+Minimum_time_beetween_+reload=360
+#Time between each autoreload in seconds
+CacheReloader_timeout=3600

+ 2 - 2
src/actions/Reload.java

@@ -20,7 +20,7 @@ public class Reload extends Action {
 	public void react(String channel, String sender, String login, String hostname, String message) {
 	public void react(String channel, String sender, String login, String hostname, String message) {
 		Date now = new Date();
 		Date now = new Date();
 		Date lastCU = Cache.getInstance().getLastCacheUpdate();
 		Date lastCU = Cache.getInstance().getLastCacheUpdate();
-		if(lastCU.getTime() < now.getTime()-Cache.TIME_BETWEEN_RELOADS ) {		// Si la dernière MAJ date de + de 5 minutes
+		if(lastCU.getTime() < now.getTime()-Cache.getTIME_BETWEEN_RELOADS() ) {		// Si la dernière MAJ date de + de 5 minutes
 			bot.sendMessage(channel, "Je lance le reload!");
 			bot.sendMessage(channel, "Je lance le reload!");
 			if(reload()) {
 			if(reload()) {
 				bot.sendMessage(channel, sender+": Le reload s'est bien passé.");
 				bot.sendMessage(channel, sender+": Le reload s'est bien passé.");
@@ -28,7 +28,7 @@ public class Reload extends Action {
 				bot.sendMessage(channel, sender+": Erreur au moment du reload.");
 				bot.sendMessage(channel, sender+": Erreur au moment du reload.");
 			}
 			}
 		}else {
 		}else {
-			Date nextAllowed = new Date(lastCU.getTime()+Cache.TIME_BETWEEN_RELOADS);
+			Date nextAllowed = new Date(lastCU.getTime()+Cache.getTIME_BETWEEN_RELOADS());
 			bot.sendMessage(channel, "Trop de reload, attendez un peu. Le dernier à eu lieu le "+lastCU.toString()+" Prochain autorisé le "+nextAllowed);
 			bot.sendMessage(channel, "Trop de reload, attendez un peu. Le dernier à eu lieu le "+lastCU.toString()+" Prochain autorisé le "+nextAllowed);
 		}
 		}
 		
 		

+ 49 - 1
src/main/Bot.java

@@ -8,8 +8,9 @@ import actions.Action;
 
 
 public class Bot extends PircBot {
 public class Bot extends PircBot {
 
 
-	public static final long TIME_BETWEEN_MESSAGES = 200;
+	private static long TIME_BETWEEN_MESSAGES = 200;
 	private List<Action> actions = Action.getAllActions(this);
 	private List<Action> actions = Action.getAllActions(this);
+	private String[] admins;
 
 
 	public Bot() {
 	public Bot() {
 		this.setName("UneFede2");
 		this.setName("UneFede2");
@@ -54,6 +55,10 @@ public class Bot extends PircBot {
 		}
 		}
 	}
 	}
 	
 	
+	/**
+	 * 
+	 */
+	@Override
 	public void onKick (String channel, String kickerNick, String login, String hostname, String recipientNick, String reason) {
 	public void onKick (String channel, String kickerNick, String login, String hostname, String recipientNick, String reason) {
 		if(recipientNick.equalsIgnoreCase(this.getNick())){
 		if(recipientNick.equalsIgnoreCase(this.getNick())){
 			RejoinThread rj = new RejoinThread(this,channel);
 			RejoinThread rj = new RejoinThread(this,channel);
@@ -61,5 +66,48 @@ public class Bot extends PircBot {
 		}
 		}
 		
 		
 	}
 	}
+	
+	public void sendMessageToAdmins(String message) {
+		for(int i=0;i<admins.length;i++) {
+			sendMessage(admins[i], message);
+		}
+	}
+	
+	public void sendMessageToAdmins(List<String> messages) {
+		for(int i=0;i<admins.length;i++) {
+			sendMessage(admins[i], messages);
+		}
+	}
+	
+	
+	/**
+	 * @return the tIME_BETWEEN_MESSAGES
+	 */
+	public static long getTIME_BETWEEN_MESSAGES() {
+		return TIME_BETWEEN_MESSAGES;
+	}
+
+	/**
+	 * @param tIME_BETWEEN_MESSAGES the tIME_BETWEEN_MESSAGES to set
+	 */
+	public static void setTIME_BETWEEN_MESSAGES(long tIME_BETWEEN_MESSAGES) {
+		TIME_BETWEEN_MESSAGES = tIME_BETWEEN_MESSAGES;
+	}
+
+	/**
+	 * @return the admins
+	 */
+	public String[] getAdmins() {
+		return admins;
+	}
+
+	/**
+	 * @param admins the admins to set
+	 */
+	public void setAdmins(String[] admins) {
+		this.admins = admins;
+	}
+	
+	
 
 
 }
 }

+ 19 - 1
src/main/Cache.java

@@ -19,7 +19,7 @@ public class Cache implements AffichableSurIRC {
 	public static volatile Cache instance = null;
 	public static volatile Cache instance = null;
 	private Date lastCacheUpdate;
 	private Date lastCacheUpdate;
 	private List<ISP> cache;
 	private List<ISP> cache;
-	public static final long TIME_BETWEEN_RELOADS = 360000;
+	private static long TIME_BETWEEN_RELOADS = 360000;
 	
 	
 	/**
 	/**
 	 * Constructeur de cache. Celui ci est privé car la classe utilise le Design Patern singleton.
 	 * Constructeur de cache. Celui ci est privé car la classe utilise le Design Patern singleton.
@@ -36,6 +36,24 @@ public class Cache implements AffichableSurIRC {
 	}
 	}
 	
 	
 	/**
 	/**
+	 * @return the tIME_BETWEEN_RELOADS
+	 */
+	public static long getTIME_BETWEEN_RELOADS() {
+		return TIME_BETWEEN_RELOADS;
+	}
+
+
+
+	/**
+	 * @param tIME_BETWEEN_RELOADS the tIME_BETWEEN_RELOADS to set
+	 */
+	public static void setTIME_BETWEEN_RELOADS(long tIME_BETWEEN_RELOADS) {
+		TIME_BETWEEN_RELOADS = tIME_BETWEEN_RELOADS;
+	}
+
+
+
+	/**
 	 * Dernière fois que le cache à été mis a jour.
 	 * Dernière fois que le cache à été mis a jour.
 	 * @return Date correspondante à la dernière fois que le cache à été mis a jour
 	 * @return Date correspondante à la dernière fois que le cache à été mis a jour
 	 */
 	 */

+ 16 - 0
src/main/CacheReloader.java

@@ -64,4 +64,20 @@ public class CacheReloader implements Runnable {
 		
 		
 	}
 	}
 
 
+	/**
+	 * @return the timeout
+	 */
+	public final long getTimeout() {
+		return timeout;
+	}
+
+	/**
+	 * @param timeout the timeout to set
+	 */
+	public final void setTimeout(long timeout) {
+		this.timeout = timeout;
+	}
+	
+	
+
 }
 }

+ 60 - 14
src/main/Main.java

@@ -4,24 +4,24 @@ import java.net.ConnectException;
 
 
 public class Main {
 public class Main {
 
 
-	public static final String SERVER = "irc.geeknode.net";
-	public static final int PORT = 6667;
-	public static final String[] CHANNELS = { "#marmat" };
-	public static final long TIMEOUT_BEFORE_RECONNECTING = 360;
-	public static final Cache cache = Cache.getInstance();
-	public static int failures = 0;
+	private static String SERVER = "irc.geeknode.net";
+	private static int PORT = 6667;
+	private static String[] CHANNELS = { "#marmat" };
+	private static long TIMEOUT_BEFORE_RECONNECTING = 360;
+	private static int failures = 0;
 	private static boolean DEBUG=true;
 	private static boolean DEBUG=true;
 
 
 	public static void main(String[] args) throws Exception {
 	public static void main(String[] args) throws Exception {
 
 
 		try {
 		try {
-			if(args.length>0) {
-			setDebug(args[0].equals("-debug"));
-			}else {
-				setDebug(false);
-			}
+			
+			CacheReloader cr = new CacheReloader(3600); // Met à jour la base toute les heures.
 			// Now start our bot up.
 			// Now start our bot up.
 			Bot bot = new Bot();
 			Bot bot = new Bot();
+			
+			//Properties Setter
+			PropertiesSetter ps = new PropertiesSetter("../../ressources/config/config.properties");
+			ps.setPropertiesOn(cr, bot);
 
 
 			// Connect to the IRC server.
 			// Connect to the IRC server.
 			bot.connect(SERVER,PORT);
 			bot.connect(SERVER,PORT);
@@ -33,9 +33,13 @@ public class Main {
 			for(int i = 0; i< CHANNELS.length; i++) {
 			for(int i = 0; i< CHANNELS.length; i++) {
 				bot.joinChannel(CHANNELS[i]);
 				bot.joinChannel(CHANNELS[i]);
 			}
 			}
-			CacheReloader cacheReloader = new CacheReloader(3600); // Met à jour la base toute les heures.
-			cacheReloader.start();
 			
 			
+			cr.start();
+			if(args.length>0) {
+				setDebug(args[0].equals("-debug"));
+				}else {
+					setDebug(false);
+			}
 			System.out.println("Debug? "+DEBUG);
 			System.out.println("Debug? "+DEBUG);
 			
 			
 
 
@@ -53,8 +57,50 @@ public class Main {
 		return Main.DEBUG;
 		return Main.DEBUG;
 	}
 	}
 
 
-	private static void setDebug(boolean b) {
+	public static void setDebug(boolean b) {
 		System.out.println("Mise de debug à "+b);
 		System.out.println("Mise de debug à "+b);
 		DEBUG=b;
 		DEBUG=b;
 	}
 	}
+
+
+	public static final String getSERVER() {
+		return SERVER;
+	}
+
+
+	public static final void setSERVER(String sERVER) {
+		SERVER = sERVER;
+	}
+
+
+	public static final int getPORT() {
+		return PORT;
+	}
+
+
+	public static final void setPORT(int pORT) {
+		PORT = pORT;
+	}
+
+
+	public static final String[] getCHANNELS() {
+		return CHANNELS;
+	}
+
+
+	public static final void setCHANNELS(String[] cHANNELS) {
+		CHANNELS = cHANNELS;
+	}
+
+
+	public static final long getTIMEOUT_BEFORE_RECONNECTING() {
+		return TIMEOUT_BEFORE_RECONNECTING;
+	}
+
+
+	public static final void setTIMEOUT_BEFORE_RECONNECTING(long tIMEOUT_BEFORE_RECONNECTING) {
+		TIMEOUT_BEFORE_RECONNECTING = tIMEOUT_BEFORE_RECONNECTING;
+	}
+	
+	
 }
 }

+ 79 - 0
src/main/PropertiesSetter.java

@@ -0,0 +1,79 @@
+package main;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Properties;
+
+import verif_saisie.EntierPositifNonVide;
+
+
+public class PropertiesSetter {
+
+	private String f;
+	public final static String DEFAULT_PROPERTIES_FILE = "default.properties";
+	public final static String here = (new File(".")).getAbsolutePath();
+	
+	public PropertiesSetter(String configFile) {
+		f = configFile;
+	}
+	
+	public void setFile(String configFile) {
+		f=configFile;
+	}
+
+	public boolean setPropertiesOn(CacheReloader cr, Bot b) throws IOException,NumberFormatException {
+		System.out.println(here);
+		System.out.println(here+File.separator+"ressources"+File.separator+"config"+File.separator+DEFAULT_PROPERTIES_FILE);
+		FileReader defaultProperties = new FileReader(new File(here+File.separator+"ressources"+File.separator+"config"+File.separator+DEFAULT_PROPERTIES_FILE));
+		Properties defaultProp = new Properties();
+		Properties prop;
+		defaultProp.load(defaultProperties);
+		if(defaultProp !=null) {
+			prop = new Properties(defaultProp);
+			for(Object o: prop.keySet()) {
+				String s = (String) o;
+				System.out.println(s+":"+prop.getProperty(s));
+			}
+		}else {
+			System.err.println("Pas de default properties : fichier default.properties manquant?");
+			prop = new Properties();
+		}
+		
+		FileReader is = new FileReader(new File(here+File.separator+"ressources"+File.separator+"config"+File.separator+f));
+		if(is == null) {
+			throw new FileNotFoundException("property file: "+f);
+		}
+		prop.load(is);
+		Main.setSERVER(prop.getProperty("SERVER"));
+		String port =prop.getProperty("PORT", "6667");
+		if(EntierPositifNonVide.entre(port, 0, 65536)) {
+			Main.setPORT(Integer.parseInt(port));
+		}
+		Main.setCHANNELS(getMultipleValues(prop, "CHANNELS"));
+		Main.setTIMEOUT_BEFORE_RECONNECTING(Long.parseLong(prop.getProperty("Timeout_before_reconnecting")));
+		Main.setDebug(Boolean.parseBoolean(prop.getProperty("Debug")));
+		
+		Bot.setTIME_BETWEEN_MESSAGES(Long.parseLong(prop.getProperty("Time_between_messages")));
+		b.setAdmins(getMultipleValues(prop, "Admins"));
+		
+		RejoinThread.setDEFAULT_WAIT_BEFORE_RECONNECT(Long.parseLong(prop.getProperty("Wait_before_reconnecting_when_kicked")));
+		
+		Cache.setTIME_BETWEEN_RELOADS(1000*Long.parseLong(prop.getProperty("Minimum_time_beetween_+reload")));
+		
+		cr.setTimeout(Long.parseLong(prop.getProperty("CacheReloader_timeout")));
+
+		return true;
+	}
+	
+	
+	
+	
+	
+	private String[] getMultipleValues(Properties prop, String key) {
+		return prop.getProperty(key).split(",");
+	}
+	
+	
+}

+ 17 - 1
src/main/RejoinThread.java

@@ -6,7 +6,7 @@ public class RejoinThread implements Runnable{
 
 
 	private volatile PircBot pb;
 	private volatile PircBot pb;
 	private String chan;
 	private String chan;
-	public static final long DEFAULT_WAIT_BEFORE_RECONNECT = 10000;
+	private static long DEFAULT_WAIT_BEFORE_RECONNECT = 10000;
 	private Thread thread;
 	private Thread thread;
 	private String threadName;
 	private String threadName;
 	private int failures;
 	private int failures;
@@ -68,5 +68,21 @@ public class RejoinThread implements Runnable{
 		return failures;
 		return failures;
 	}
 	}
 
 
+
+	/**
+	 * @return the dEFAULT_WAIT_BEFORE_RECONNECT
+	 */
+	public static long getDEFAULT_WAIT_BEFORE_RECONNECT() {
+		return DEFAULT_WAIT_BEFORE_RECONNECT;
+	}
+
+
+	/**
+	 * @param dEFAULT_WAIT_BEFORE_RECONNECT the dEFAULT_WAIT_BEFORE_RECONNECT to set
+	 */
+	public static void setDEFAULT_WAIT_BEFORE_RECONNECT(long dEFAULT_WAIT_BEFORE_RECONNECT) {
+		DEFAULT_WAIT_BEFORE_RECONNECT = dEFAULT_WAIT_BEFORE_RECONNECT;
+	}
+
 	
 	
 }
 }