Updated devinsy-strings library. Improved code.

This commit is contained in:
Christian P. MOMON 2017-12-26 00:39:52 +01:00
parent 7025e74490
commit 7e0c4ab4ed
7 changed files with 67 additions and 46 deletions

View file

@ -37,10 +37,9 @@
<classpathentry kind="lib" path="lib/commons-cli-1.4.jar"/> <classpathentry kind="lib" path="lib/commons-cli-1.4.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.4.jar"/> <classpathentry kind="lib" path="lib/commons-codec-1.4.jar"/>
<classpathentry kind="lib" path="lib/commons-collections-3.2.1.jar"/> <classpathentry kind="lib" path="lib/commons-collections-3.2.1.jar"/>
<classpathentry kind="lib" path="lib/commons-io-2.5.jar"/> <classpathentry kind="lib" path="lib/commons-io-2.5.jar" sourcepath="lib/commons-io-2.5-src.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.4.jar"/> <classpathentry kind="lib" path="lib/commons-lang3-3.4.jar" sourcepath="lib/commons-lang3-3.4-src.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/> <classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
<classpathentry kind="lib" path="lib/devinsy-strings-0.4.1.jar"/>
<classpathentry kind="lib" path="lib/httpclient-4.5.jar"/> <classpathentry kind="lib" path="lib/httpclient-4.5.jar"/>
<classpathentry kind="lib" path="lib/httpcore-4.4.1.jar"/> <classpathentry kind="lib" path="lib/httpcore-4.4.1.jar"/>
<classpathentry kind="lib" path="lib/jaxb-api-2.2.12.jar"/> <classpathentry kind="lib" path="lib/jaxb-api-2.2.12.jar"/>
@ -48,5 +47,6 @@
<classpathentry kind="lib" path="lib/pircbot-1.5.0.jar"/> <classpathentry kind="lib" path="lib/pircbot-1.5.0.jar"/>
<classpathentry kind="lib" path="lib/quartz-2.2.1.jar"/> <classpathentry kind="lib" path="lib/quartz-2.2.1.jar"/>
<classpathentry kind="lib" path="lib/scribe-1.3.7.jar"/> <classpathentry kind="lib" path="lib/scribe-1.3.7.jar"/>
<classpathentry kind="lib" path="lib/devinsy-strings-0.4.2.jar" sourcepath="lib/devinsy-strings-0.4.2-sources.zip"/>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

Binary file not shown.

Binary file not shown.

View file

@ -136,8 +136,9 @@ public class HebdobotCLI
File aliasFile = new File(configFile.getParentFile(), DEFAULT_ALIAS_FILE); File aliasFile = new File(configFile.getParentFile(), DEFAULT_ALIAS_FILE);
logger.info("Aliases file loading… ({}).", aliasFile.getAbsolutePath()); logger.info("Aliases file loading… ({}).", aliasFile.getAbsolutePath());
UserAliases aliases = new UserAliases(aliasFile); UserAliases aliases = new UserAliases(aliasFile);
logger.info("Aliases file loaded."); logger.info("Aliases file loaded (" + aliases.size() + " aliases).");
System.exit(0);
if (config.isValid()) if (config.isValid())
{ {
logger.info("Bot configuring…"); logger.info("Bot configuring…");

View file

@ -46,6 +46,8 @@ import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.util.strings.StringsUtils;
/** /**
* The Class Hebdobot. * The Class Hebdobot.
*/ */
@ -153,24 +155,25 @@ public class Hebdobot extends PircBot
{ {
String text = message.trim(); String text = message.trim();
if (StringUtils.equalsIgnoreCase(text, "!help")) if ((StringUtils.equalsIgnoreCase(text, "!aide")) || (StringUtils.equalsIgnoreCase(text, "!help")))
{ {
logger.info("!help caught."); logger.info("!help caught.");
// Help. // Help.
sendMessage(sender, "Bienvenue " + sender); sendMessage(sender, "Bienvenue " + sender);
sendMessage(sender, "Je suis " + getName() + ", le robot de gestion des revues hebdomadaires de l'APRIL"); sendMessage(sender, "Je suis " + getName() + ", le robot de gestion des revues hebdomadaires de l'APRIL.");
sendMessage(sender, "Voici les commandes que je comprends :"); sendMessage(sender, "Voici les commandes que je comprends :");
sendMessage(sender, " "); sendMessage(sender, " ");
sendMessage(sender, "— !debut : commencer une nouvelle revue"); sendMessage(sender, " !debut : commencer une nouvelle revue");
sendMessage(sender, "— !fin : terminer la revue en cours"); sendMessage(sender, " !fin : terminer la revue en cours");
sendMessage(sender, "— # titre : démarrer un sujet individuel"); sendMessage(sender, " # titre : démarrer un sujet individuel");
sendMessage(sender, "— ## titre : démarrer un sujet collectif"); sendMessage(sender, " ## titre : démarrer un sujet collectif");
sendMessage(sender, "— !courant : affiche le sujet en cours"); sendMessage(sender, " !courant : affiche le sujet en cours");
sendMessage(sender, "— !manquants : affiche les participants qui n'ont pas répondu sur le dernier sujet"); sendMessage(sender, " !manquants : affiche les participants qui n'ont pas répondu sur le dernier sujet");
sendMessage(sender, "— % message : un commentaire"); sendMessage(sender, " % message : un commentaire");
sendMessage(sender, "— !stop : abandonner la revue en cours"); sendMessage(sender, " !stop : abandonner la revue en cours");
sendMessage(sender, "— !vaten : faire partir le bot"); sendMessage(sender, " !vaten : faire partir le bot");
sendMessage(sender, " !aide : afficher cette aide");
} }
else if (StringUtils.equalsIgnoreCase(text, "!vaten")) else if (StringUtils.equalsIgnoreCase(text, "!vaten"))
{ {
@ -192,24 +195,25 @@ public class Hebdobot extends PircBot
} }
else else
{ {
sendMessage("% Une revue est en cours, arrêt impossible"); sendMessage("% Une revue est en cours, arrêt impossible.");
} }
} }
else if (StringUtils.equalsIgnoreCase(text, "!stop")) else if (StringUtils.equalsIgnoreCase(text, "!stop"))
{ {
logger.info("!stop caught."); logger.info("!stop caught.");
// Die. // Stop.
if (this.review == null) if (this.review == null)
{ {
sendMessage("Aucune revue en cours, arrêt impossible"); sendMessage("Aucune revue en cours, arrêt impossible.");
} }
else else
{ {
sendMessage("Arrêt de la revue en cours.");
this.review = null; this.review = null;
} }
} }
else if ((StringUtils.equalsIgnoreCase(text, "!debut")) || (StringUtils.equalsIgnoreCase(text, "!début"))) else if (StringsUtils.equalsAnyIgnoreCase(text, "!debut", "!début"))
{ {
logger.info("!debut caught."); logger.info("!debut caught.");

View file

@ -22,31 +22,32 @@ package org.april.hebdobot.model;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.april.hebdobot.HebdobotException; import org.april.hebdobot.HebdobotException;
import org.april.hebdobot.util.HebdobotUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.util.strings.StringList;
/** /**
* The Class UserAliases. * The Class UserAliases.
*/ */
public class UserAliases public class UserAliases extends HashMap<String, String>
{ {
private static final long serialVersionUID = -7563433397065777556L;
private static final Logger logger = LoggerFactory.getLogger(UserAliases.class); private static final Logger logger = LoggerFactory.getLogger(UserAliases.class);
private final Map<String, String> aliases;
/** /**
* Instantiates a new user aliases. * Instantiates a new user aliases.
*/ */
public UserAliases() public UserAliases()
{ {
this.aliases = new HashMap<String, String>(); super();
} }
/** /**
@ -59,20 +60,24 @@ public class UserAliases
*/ */
public UserAliases(final File source) throws HebdobotException public UserAliases(final File source) throws HebdobotException
{ {
this.aliases = new HashMap<String, String>(); super();
try try
{ {
StringList lines = new StringList(FileUtils.readLines(source, StandardCharsets.UTF_8));
Properties users = HebdobotUtils.loadProperties(source); for (String line : lines)
for (String realName : users.stringPropertyNames())
{ {
String[] nicks = users.getProperty(realName).split(","); if ((StringUtils.isNotBlank(line)) && (!line.startsWith("#")))
for (String nick : nicks)
{ {
this.aliases.put(nick, realName); String[] tokens = line.split("=");
String realName = tokens[0];
String[] nicks = tokens[1].split(",");
for (String nick : nicks)
{
put(nick, realName);
}
} }
} }
} }
@ -97,12 +102,12 @@ public class UserAliases
{ {
String result; String result;
result = this.aliases.get(nick); String realName = get(nick);
if (result == null) if (realName == null)
{ {
boolean ended = false; boolean ended = false;
Iterator<String> iterator = this.aliases.keySet().iterator(); Iterator<String> iterator = keySet().iterator();
while (!ended) while (!ended)
{ {
if (iterator.hasNext()) if (iterator.hasNext())
@ -112,29 +117,40 @@ public class UserAliases
if (nick.toLowerCase().contains(currentNick.toLowerCase())) if (nick.toLowerCase().contains(currentNick.toLowerCase()))
{ {
ended = true; ended = true;
result = this.aliases.get(currentNick) + " ( " + nick + " )"; realName = get(currentNick);
} }
} }
else else
{ {
result = nick; realName = nick;
ended = true; ended = true;
} }
} }
} }
result = realName + " (" + nick + ")";
// //
return result; return result;
} }
/** /* (non-Javadoc)
* Put all. * @see java.util.AbstractMap#toString()
*
* @param source
* the source
*/ */
public void putAll(final UserAliases source) @Override
public String toString()
{ {
this.aliases.putAll(source.aliases); String result;
StringList buffer = new StringList();
for (String nick : this.keySet())
{
buffer.appendln(getRealName(nick));
}
result = buffer.toString();
//
return result;
} }
} }