From 44fa9b9457b4406b06f3f3a3b32977f57987c4a4 Mon Sep 17 00:00:00 2001 From: Nicolas VINOT Date: Sat, 3 Sep 2011 16:29:09 +0200 Subject: [PATCH] Manage aliases --- .classpath | 5 + .hgignore | 6 ++ .settings/org.eclipse.jdt.core.prefs | 2 +- pom.xml | 99 ++++++++++++++----- .../java/fr/imirhil/april/hebdobot/Bot.java | 65 ++++++------ .../fr/imirhil/april/hebdobot/Meeting.java | 79 +++++++++------ .../april/hebdobot/xml/UserService.java | 55 +++++++++++ .../fr/imirhil/april/hebdobot/users.xjb | 9 ++ .../fr/imirhil/april/hebdobot/users.xsd | 33 +++++++ 9 files changed, 262 insertions(+), 91 deletions(-) create mode 100644 src/main/java/fr/imirhil/april/hebdobot/xml/UserService.java create mode 100644 src/main/resources/fr/imirhil/april/hebdobot/users.xjb create mode 100644 src/main/resources/fr/imirhil/april/hebdobot/users.xsd diff --git a/.classpath b/.classpath index 3c96d64..37b6b02 100644 --- a/.classpath +++ b/.classpath @@ -6,5 +6,10 @@ + + + + + diff --git a/.hgignore b/.hgignore index eb5a316..5772d5b 100644 --- a/.hgignore +++ b/.hgignore @@ -1 +1,7 @@ +syntax:glob + target +*.log + +syntax: regexp +^users\.xml$ \ No newline at end of file diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 0f018c2..8543e9b 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,4 +1,4 @@ -#Fri Sep 02 15:54:38 CEST 2011 +#Fri Sep 02 19:35:13 CEST 2011 eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 org.eclipse.jdt.core.compiler.compliance=1.5 diff --git a/pom.xml b/pom.xml index 717a9a4..cc607b0 100644 --- a/pom.xml +++ b/pom.xml @@ -1,28 +1,73 @@ - - 4.0.0 - fr.imirhil.april - ircbot - 1.0.0-SNAPSHOT - - - pircbot - pircbot - 1.5.0 - - - joda-time - joda-time - 2.0 - - - commons-io - commons-io - 2.0.1 - - - commons-lang - commons-lang - 2.6 - - + + 4.0.0 + fr.imirhil.april + ircbot + 1.0.0-SNAPSHOT + + + + org.codehaus.mojo + jaxb2-maven-plugin + 1.3 + + + + xjc + + + + + ${basedir}/src/main/resources/fr/imirhil/april/hebdobot + ${basedir}/src/main/resources/fr/imirhil/april/hebdobot + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.7 + + + add-source + generate-sources + + add-source + + + + ${project.build.directory}/generated-sources/jaxb + + + + + + + + + + pircbot + pircbot + 1.5.0 + + + joda-time + joda-time + 2.0 + + + commons-io + commons-io + 2.0.1 + + + commons-lang + commons-lang + 2.6 + + + javax.xml + jaxb-api + 2.1 + + \ No newline at end of file diff --git a/src/main/java/fr/imirhil/april/hebdobot/Bot.java b/src/main/java/fr/imirhil/april/hebdobot/Bot.java index 4580544..91324ee 100644 --- a/src/main/java/fr/imirhil/april/hebdobot/Bot.java +++ b/src/main/java/fr/imirhil/april/hebdobot/Bot.java @@ -8,6 +8,8 @@ import java.util.Properties; import org.apache.commons.io.FileUtils; import org.jibble.pircbot.PircBot; +import fr.imirhil.april.hebdobot.xml.UserService; + public class Bot extends PircBot { private Meeting meeting = null; private Topic currentTopic = null; @@ -23,31 +25,36 @@ public class Bot extends PircBot { @Override protected void onMessage(final String channel, final String sender, final String login, final String hostname, String message) { - if (!channel.equalsIgnoreCase(this.channel)) { - return; - } - - message = message.trim(); - - final Message raw = - new Message(sender, message.replaceFirst("\\s*[#%]*\\s*", "")); - this.log(new Message(sender, message)); - if (this.meeting == null) { - if (message.startsWith("!debut")) { - this.start(sender); + try { + if (!channel.equalsIgnoreCase(this.channel)) { + return; } - } else { - if (message.startsWith("!fin")) { - this.end(sender); - } else if (message.startsWith("##")) { - this.startCollectiveTopic(sender, message); - } else if (message.startsWith("#")) { - this.startIndividualTopic(sender, message); - } else if (message.startsWith("%")) { - this.addToCurrentTopic(raw); - } else if (message.startsWith("!")) { - this.handleCommand(message.replaceFirst("!", "")); + + message = message.trim(); + final Message raw = + new Message(sender, message.replaceFirst("\\s*[#%]*\\s*", + "")); + if (this.meeting == null) { + if (message.startsWith("!debut")) { + this.start(sender); + } + } else { + if (message.startsWith("!fin")) { + this.end(sender); + } else if (message.startsWith("##")) { + this.startCollectiveTopic(sender, message); + } else if (message.startsWith("#")) { + this.startIndividualTopic(sender, message); + } else if (message.startsWith("%")) { + this.addToCurrentTopic(raw); + } else if (message.startsWith("!")) { + this.handleCommand(message.replaceFirst("!", "")); + } } + + this.log(new Message(sender, message)); + } catch (final Exception e) { + e.printStackTrace(); } } @@ -87,21 +94,15 @@ public class Bot extends PircBot { this.meeting = null; } - private void archiveCurrentTopic() { - if (this.currentTopic != null) { - this.meeting.add(this.currentTopic); - } - } - private void startIndividualTopic(final String sender, final String message) { if (!this.owner.equalsIgnoreCase(sender)) { return; } - this.archiveCurrentTopic(); this.currentTopic = new IndividualTopic(message.replaceFirst("^\\s*#\\s*", "")); + this.meeting.add(this.currentTopic); this.sendMessage(this.channel, "Début topic individuel : " + this.currentTopic.getTitle()); } @@ -112,9 +113,9 @@ public class Bot extends PircBot { return; } - this.archiveCurrentTopic(); this.currentTopic = new CollectiveTopic(message.replaceFirst("^\\s*##\\s*", "")); + this.meeting.add(this.currentTopic); this.sendMessage(this.channel, "Début topic collectif : " + this.currentTopic.getTitle()); } @@ -126,6 +127,8 @@ public class Bot extends PircBot { } public static void main(final String[] args) throws Exception { + new UserService(); + final Properties properties = new Properties(); properties.load(new FileReader("conf.properties")); final String channel = properties.getProperty("irc.chan"); diff --git a/src/main/java/fr/imirhil/april/hebdobot/Meeting.java b/src/main/java/fr/imirhil/april/hebdobot/Meeting.java index 4e857da..75f01d4 100644 --- a/src/main/java/fr/imirhil/april/hebdobot/Meeting.java +++ b/src/main/java/fr/imirhil/april/hebdobot/Meeting.java @@ -7,9 +7,11 @@ import java.util.Set; import org.apache.commons.lang.StringUtils; +import fr.imirhil.april.hebdobot.xml.UserService; + public class Meeting { - private static final String SEPARATOR = - "========================================================================\n"; + private static final UserService USER_SERVICE = new UserService(); + private static final int LENGTH = 74; private final Set participants = new HashSet(); private final List individualTopics = @@ -34,51 +36,54 @@ public class Meeting { @Override public String toString() { final StringBuffer buffer = new StringBuffer(); - buffer.append(SEPARATOR); - buffer.append(getLine("Revue de la semaine en cours", ' ')); - buffer.append(SEPARATOR); - buffer.append("\n\n"); - buffer.append(SEPARATOR); + addBox(buffer, "Revue de la semaine en cours", '#'); buffer.append("\n"); - buffer.append(getLine("Participants", '-')); + buffer.append("\n"); + addBox(buffer, "Participants", '='); + buffer.append("\n"); for (final String participant : this.participants) { - buffer.append("* " + participant + "\n"); + buffer.append("* " + Meeting.USER_SERVICE.getRealName(participant) + + "\n"); } buffer.append("\n"); - for (final String participant : this.participants) { - buffer.append(SEPARATOR + "\n"); - buffer.append(getLine(participant, '-')); + if (!this.individualTopics.isEmpty()) { buffer.append("\n"); - - for (final IndividualTopic topic : this.individualTopics) { - buffer.append(getTopic(topic)); + addBox(buffer, "Sujets individuels", '='); + for (final String participant : this.participants) { buffer.append("\n"); - for (final Message message : topic.getMessages(participant)) { - buffer.append("* " + message.getContent() + "\n"); + buffer.append(getLine( + Meeting.USER_SERVICE.getRealName(participant), '-')); + for (final IndividualTopic topic : this.individualTopics) { + buffer.append("\n"); + buffer.append(getTopic(topic)); + buffer.append("\n"); + for (final Message message : topic.getMessages(participant)) { + buffer.append("* " + message.getContent() + "\n"); + } } } - buffer.append("\n"); } - for (final CollectiveTopic topic : this.collectiveTopics) { + if (!this.collectiveTopics.isEmpty()) { buffer.append("\n"); - buffer.append(SEPARATOR); - buffer.append(getLine(topic.getTitle(), ' ')); - buffer.append(SEPARATOR + "\n"); - for (final Message message : topic.getMessages()) { - buffer.append("* " + message.getAuthor() + " : " - + message.getContent() + "\n"); + addBox(buffer, "Sujets collectifs", '='); + for (final CollectiveTopic topic : this.collectiveTopics) { + buffer.append("\n"); + addBox(buffer, topic.getTitle(), '-'); + for (final Message message : topic.getMessages()) { + buffer.append("* " + message.getAuthor() + " : " + + message.getContent() + "\n"); + } } + buffer.append("\n"); } buffer.append("\n"); - buffer.append(SEPARATOR); - buffer.append(getLine("Log complet", ' ')); - buffer.append(SEPARATOR + "\n"); - + addBox(buffer, "Log complet", '='); + buffer.append("\n"); for (final Message message : this.messages) { buffer.append("* " + message.getAuthor() + " : " + message.getContent() + "\n"); @@ -87,12 +92,22 @@ public class Meeting { return buffer.toString(); } + private static String getLine(final Character c) { + return StringUtils.repeat(c.toString(), LENGTH) + "\n"; + } + private static String getLine(final String content, final Character c) { - return StringUtils.center(" " + content + " ", SEPARATOR.length(), c) - + "\n"; + return StringUtils.center(" " + content + " ", LENGTH, c) + "\n"; } private static String getTopic(final Topic topic) { - return "=== " + topic.getTitle() + " ==="; + return "=== " + topic.getTitle() + " ==="; + } + + private static void addBox(final StringBuffer buffer, final String title, + final Character c) { + buffer.append(getLine(c)); + buffer.append(getLine(title, ' ')); + buffer.append(getLine(c)); } } diff --git a/src/main/java/fr/imirhil/april/hebdobot/xml/UserService.java b/src/main/java/fr/imirhil/april/hebdobot/xml/UserService.java new file mode 100644 index 0000000..3036d11 --- /dev/null +++ b/src/main/java/fr/imirhil/april/hebdobot/xml/UserService.java @@ -0,0 +1,55 @@ +package fr.imirhil.april.hebdobot.xml; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import javax.xml.XMLConstants; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.SchemaFactory; + +import org.xml.sax.SAXException; + +public class UserService { + private final Map aliases = new HashMap(); + + public UserService() { + try { + final Unmarshaller u = + JAXBContext.newInstance(Users.class).createUnmarshaller(); + u.setSchema(SchemaFactory + .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) + .newSchema( + UserService.class + .getResource("/fr/imirhil/april/hebdobot/users.xsd"))); + + for (final User user : u + .unmarshal(new StreamSource(new File("users.xml")), + Users.class).getValue().getUser()) { + final String realName = user.getRealName(); + for (final String nick : user.getNick()) { + this.aliases.put(nick, realName); + } + } + } catch (final SAXException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } catch (final JAXBException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + public String getRealName(final String nick) { + for (final Entry entry : this.aliases.entrySet()) { + if (nick.toLowerCase().contains(entry.getKey().toLowerCase())) { + return entry.getValue() + " ( " + nick + " )"; + } + } + return nick; + } +} diff --git a/src/main/resources/fr/imirhil/april/hebdobot/users.xjb b/src/main/resources/fr/imirhil/april/hebdobot/users.xjb new file mode 100644 index 0000000..690db61 --- /dev/null +++ b/src/main/resources/fr/imirhil/april/hebdobot/users.xjb @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/fr/imirhil/april/hebdobot/users.xsd b/src/main/resources/fr/imirhil/april/hebdobot/users.xsd new file mode 100644 index 0000000..bd526f9 --- /dev/null +++ b/src/main/resources/fr/imirhil/april/hebdobot/users.xsd @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file