Manage aliases

This commit is contained in:
Nicolas VINOT 2011-09-03 16:29:09 +02:00
parent 3116d8c96b
commit 44fa9b9457
9 changed files with 262 additions and 91 deletions

View File

@ -6,5 +6,10 @@
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="src" output="target/classes" path="target/generated-sources/jaxb">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1 +1,7 @@
syntax:glob
target
*.log
syntax: regexp
^users\.xml$

View File

@ -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

99
pom.xml
View File

@ -1,28 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.imirhil.april</groupId>
<artifactId>ircbot</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>pircbot</groupId>
<artifactId>pircbot</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.imirhil.april</groupId>
<artifactId>ircbot</artifactId>
<version>1.0.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/fr/imirhil/april/hebdobot</schemaDirectory>
<bindingDirectory>${basedir}/src/main/resources/fr/imirhil/april/hebdobot</bindingDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/jaxb</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>pircbot</groupId>
<artifactId>pircbot</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</project>

View File

@ -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");

View File

@ -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<String> participants = new HashSet<String>();
private final List<IndividualTopic> 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));
}
}

View File

@ -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<String, String> aliases = new HashMap<String, String>();
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<String, String> entry : this.aliases.entrySet()) {
if (nick.toLowerCase().contains(entry.getKey().toLowerCase())) {
return entry.getValue() + " ( " + nick + " )";
}
}
return nick;
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" version="2.1">
<bindings schemaLocation="users.xsd">
<schemaBindings>
<package name="fr.imirhil.april.hebdobot.xml" />
</schemaBindings>
</bindings>
</bindings>

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="User">
<xsd:sequence>
<xsd:element name="realName">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"></xsd:minLength>
<xsd:whiteSpace value="collapse"></xsd:whiteSpace>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="nick" maxOccurs="unbounded"
minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"></xsd:minLength>
<xsd:whiteSpace value="collapse"></xsd:whiteSpace>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="users">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="user" type="User" maxOccurs="unbounded" minOccurs="0"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>