Browse Source

feat: RP fonctionnels

Martin Passard 7 years ago
parent
commit
a9d6f3c376
2 changed files with 86 additions and 1 deletions
  1. 1 1
      src/actions/Action.java
  2. 85 0
      src/actions/RP.java

+ 1 - 1
src/actions/Action.java

@@ -4,7 +4,6 @@ import java.util.ArrayList;
 import java.util.List;
 
 import main.Bot;
-import rss.RssDataRemainder;
 
 public abstract class Action {
 
@@ -86,6 +85,7 @@ public abstract class Action {
 		ar.add(new ID(b));
 		ar.add(new Distance(b));
 		ar.add(new RSS(b));
+		ar.add(new RP(b));
 		return ar;
 	}
 	

+ 85 - 0
src/actions/RP.java

@@ -0,0 +1,85 @@
+package actions;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import main.Bot;
+import main.Main;
+import main.PropertiesSetter;
+
+public class RP extends Action {
+	
+	public File rpFile;
+
+	protected RP(Bot b) {
+		super(b);
+		List<String> ar = new ArrayList<>();
+		ar.add("rp");
+		ar.add("citation");
+		this.keyWords = ar;
+		rpFile = new File(PropertiesSetter.here+File.separator+"ressources"+File.separator+"rp"+File.separator+"index.txt");
+		boolean fileExists = rpFile.exists();
+		if(!fileExists) {
+			if(Main.isDebug()) {
+				System.out.println("Le fichier de RP "+rpFile.getAbsolutePath()+" n'existe pas, je le crée");
+			}
+			try {
+				if(!rpFile.createNewFile()) {
+					throw new Exception("impossible de creer le fichier");
+				}
+			} catch (Exception e) {
+				System.err.println("Erreur a la création du fichier de RP :");
+				e.printStackTrace();
+			}
+			
+		}else {
+			if(Main.isDebug()) {
+				System.out.println("Le fichier de RP "+rpFile.getAbsolutePath()+"existe");
+			}
+		}
+		
+		PrintWriter writer = null;
+		try {
+			writer = new PrintWriter(rpFile, "UTF-8");
+		if(!fileExists) {
+				writer.println("\n");
+		}
+		}catch (IOException e) {
+			System.err.println("Erreur lors de la création du fileWriter du ficher RP:");
+			e.printStackTrace();
+		}finally {
+			writer.close();
+		}
+	}
+
+	@Override
+	public void react(String channel, String sender, String login, String hostname, String message) {
+		Date now = new Date();
+		PrintWriter writer = null;
+		try {
+			writer = new PrintWriter(rpFile, "UTF-8");
+		} catch (FileNotFoundException e) {
+			bot.sendMessageToAdmins("Erreur: Le fichier rp n'existe pas, impossible d'écrire");
+			bot.sendMessageToAdmins(e.getLocalizedMessage());
+			e.printStackTrace();
+		} catch (UnsupportedEncodingException e) {
+			e.printStackTrace();
+		}
+		writer.println(Main.DATE_FORMAT_OUT.format(now)+" "+message.substring(message.indexOf(" "))+"\n");
+		bot.sendMessage(sender, channel, "Ajout a la RP réussi!");
+		writer.close();
+	}
+	
+
+	@Override
+	public String help() {
+		return "Concatène l'URL indiquée dans un fichier pour les revue de presse";
+	}
+
+}