From a937308d3db3cad746f155f7d58d73cbb6803b67 Mon Sep 17 00:00:00 2001 From: Nicolas VINOT Date: Fri, 30 Sep 2011 00:35:49 +0200 Subject: [PATCH] Send review to pastebin --- .classpath | 2 +- .hgignore | 6 +- .settings/org.eclipse.jdt.core.prefs | 8 +- conf.properties | 6 - exe/hebdobot.sh | 2 +- pom.xml | 32 ++++ .../imirhil/april/hebdobot/Application.java | 88 +++++++++ .../java/fr/imirhil/april/hebdobot/Bot.java | 144 -------------- .../fr/imirhil/april/hebdobot/irc/Bot.java | 180 ++++++++++++++++++ .../april/hebdobot/irc/ReviewListener.java | 7 + .../april/hebdobot/pastebin/Expiration.java | 15 ++ .../april/hebdobot/pastebin/Format.java | 15 ++ .../april/hebdobot/pastebin/Option.java | 15 ++ .../hebdobot/pastebin/PastebinClient.java | 116 +++++++++++ .../{ => review}/CollectiveTopic.java | 2 +- .../{ => review}/IndividualTopic.java | 2 +- .../april/hebdobot/{ => review}/Message.java | 2 +- .../{Meeting.java => review/Review.java} | 58 ++++-- .../april/hebdobot/{ => review}/Topic.java | 2 +- .../april/hebdobot/statusnet/Client.java | 5 + .../xml/{UserService.java => UserAlias.java} | 12 +- src/main/resources/log4j.properties | 7 + 22 files changed, 538 insertions(+), 188 deletions(-) delete mode 100644 conf.properties create mode 100644 src/main/java/fr/imirhil/april/hebdobot/Application.java delete mode 100644 src/main/java/fr/imirhil/april/hebdobot/Bot.java create mode 100644 src/main/java/fr/imirhil/april/hebdobot/irc/Bot.java create mode 100644 src/main/java/fr/imirhil/april/hebdobot/irc/ReviewListener.java create mode 100644 src/main/java/fr/imirhil/april/hebdobot/pastebin/Expiration.java create mode 100644 src/main/java/fr/imirhil/april/hebdobot/pastebin/Format.java create mode 100644 src/main/java/fr/imirhil/april/hebdobot/pastebin/Option.java create mode 100644 src/main/java/fr/imirhil/april/hebdobot/pastebin/PastebinClient.java rename src/main/java/fr/imirhil/april/hebdobot/{ => review}/CollectiveTopic.java (94%) rename src/main/java/fr/imirhil/april/hebdobot/{ => review}/IndividualTopic.java (95%) rename src/main/java/fr/imirhil/april/hebdobot/{ => review}/Message.java (91%) rename src/main/java/fr/imirhil/april/hebdobot/{Meeting.java => review/Review.java} (72%) rename src/main/java/fr/imirhil/april/hebdobot/{ => review}/Topic.java (87%) create mode 100644 src/main/java/fr/imirhil/april/hebdobot/statusnet/Client.java rename src/main/java/fr/imirhil/april/hebdobot/xml/{UserService.java => UserAlias.java} (86%) create mode 100644 src/main/resources/log4j.properties diff --git a/.classpath b/.classpath index 37b6b02..f5a2fae 100644 --- a/.classpath +++ b/.classpath @@ -4,7 +4,7 @@ - + diff --git a/.hgignore b/.hgignore index 5772d5b..28191b1 100644 --- a/.hgignore +++ b/.hgignore @@ -1,7 +1,5 @@ syntax:glob - +conf.properties +users.xml 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 8543e9b..3a63c4e 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,6 +1,6 @@ -#Fri Sep 02 19:35:13 CEST 2011 +#Wed Sep 28 23:04:47 CEST 2011 eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/conf.properties b/conf.properties deleted file mode 100644 index b47b9c9..0000000 --- a/conf.properties +++ /dev/null @@ -1,6 +0,0 @@ -irc.host = irc.freenode.org -irc.port = 6667 -irc.chan = #april -irc.nick = Hebdobot - -log.output = reunion.log \ No newline at end of file diff --git a/exe/hebdobot.sh b/exe/hebdobot.sh index bdbb693..45ca0d0 100755 --- a/exe/hebdobot.sh +++ b/exe/hebdobot.sh @@ -1,2 +1,2 @@ #!/bin/sh -java -cp '*:lib/*' fr.imirhil.april.hebdobot.Bot +java -cp '*:lib/*' fr.imirhil.april.hebdobot.Application diff --git a/pom.xml b/pom.xml index 65224a0..92f7ce8 100644 --- a/pom.xml +++ b/pom.xml @@ -6,6 +6,11 @@ scm:hg:file://${basedir} + + 1.6 + ${maven.compiler.source} + UTF8 + @@ -71,5 +76,32 @@ jaxb-api 2.1 + + org.quartz-scheduler + quartz + 2.0.2 + + + org.apache.httpcomponents + httpclient + 4.1.2 + + + junit + junit + 4.8.2 + test + + + org.slf4j + slf4j-api + 1.6.1 + + + org.slf4j + slf4j-log4j12 + 1.6.1 + runtime + \ No newline at end of file diff --git a/src/main/java/fr/imirhil/april/hebdobot/Application.java b/src/main/java/fr/imirhil/april/hebdobot/Application.java new file mode 100644 index 0000000..66f8e49 --- /dev/null +++ b/src/main/java/fr/imirhil/april/hebdobot/Application.java @@ -0,0 +1,88 @@ +package fr.imirhil.april.hebdobot; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.util.Properties; + +import org.apache.commons.io.FileUtils; +import org.joda.time.DateTime; +import org.joda.time.format.ISODateTimeFormat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.imirhil.april.hebdobot.irc.Bot; +import fr.imirhil.april.hebdobot.irc.ReviewListener; +import fr.imirhil.april.hebdobot.pastebin.PastebinClient; +import fr.imirhil.april.hebdobot.review.Review; +import fr.imirhil.april.hebdobot.xml.UserAlias; + +public class Application implements ReviewListener { + private static final String USERS_FILE = "users.xml"; + + private static final Logger LOGGER = LoggerFactory + .getLogger(Application.class); + + private static final String CONF_PROPERTIES = "conf.properties"; + private static final String IRC_HOST = "irc.host"; + private static final String IRC_PORT = "irc.port"; + private static final String IRC_NICK = "irc.nick"; + private static final String IRC_CHAN = "irc.chan"; + private static final String PASTEBIN_KEY = "pastebin.key"; + private static final String FILE_SUFFIX = "file.suffix"; + + private final Properties properties = new Properties(); + private final Bot bot; + private final UserAlias userAlias; + private final String channel; + + private Application() throws Exception { + this.userAlias = new UserAlias(new FileInputStream(USERS_FILE)); + + this.properties.load(new FileReader(CONF_PROPERTIES)); + final String name = this.properties.getProperty(IRC_NICK); + this.channel = this.properties.getProperty(IRC_CHAN); + + this.bot = new Bot(name, this.channel); + this.bot.add(this); + } + + public void start() throws Exception { + this.bot.connect(this.properties.getProperty(IRC_HOST), + Integer.valueOf(this.properties.getProperty(IRC_PORT))); + this.bot.joinChannel(this.channel); + } + + @Override + public void onEnd(final Review review) { + final String date = ISODateTimeFormat.basicDate().print(new DateTime()); + final String text = review.toString(this.userAlias); + if (this.properties.containsKey(PASTEBIN_KEY)) { + try { + this.bot.sendMessage("Compte-rendu de la revue : " + + new PastebinClient(this.properties + .getProperty(PASTEBIN_KEY)).paste(text, + "Revue APRIL " + date)); + } catch (final Exception e) { + LOGGER.error("Error during Pastebin submit", e); + } + } + + if (this.properties.containsKey(FILE_SUFFIX)) { + try { + final File file = + new File(date + "_" + + this.properties.getProperty(FILE_SUFFIX)); + FileUtils.writeStringToFile(file, text); + this.bot.sendMessage("Compte-rendu de la revue : " + + file.getName()); + } catch (final Exception e) { + LOGGER.error("Error during file generation", e); + } + } + } + + public static void main(final String[] args) throws Exception { + new Application().start(); + } +} diff --git a/src/main/java/fr/imirhil/april/hebdobot/Bot.java b/src/main/java/fr/imirhil/april/hebdobot/Bot.java deleted file mode 100644 index a43ec1f..0000000 --- a/src/main/java/fr/imirhil/april/hebdobot/Bot.java +++ /dev/null @@ -1,144 +0,0 @@ -package fr.imirhil.april.hebdobot; - -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -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; - private final String channel; - private final File output; - private String owner; - - public Bot(final String channel, final File output) { - this.channel = channel; - this.output = output; - } - - @Override - protected void onMessage(final String channel, final String sender, - final String login, final String hostname, String message) { - try { - if (!channel.equalsIgnoreCase(this.channel)) { - return; - } - - 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.handleCommand(message); - } else if (!message.startsWith("%")) { - this.addToCurrentTopic(raw); - } - } - - this.log(new Message(sender, message)); - } catch (final Exception e) { - e.printStackTrace(); - } - } - - private void handleCommand(String message) { - message = message.replaceFirst("!", ""); - if (message.startsWith("courant")) { - if (this.currentTopic != null) { - this.sendMessage(this.channel, "Topic courant : " - + this.currentTopic.getTitle()); - } else { - this.sendMessage(this.channel, "Pas de topic en cours"); - } - } - } - - private void log(final Message message) { - if (this.meeting != null) { - this.meeting.add(message); - } - } - - private void start(final String sender) { - this.owner = sender; - this.meeting = new Meeting(); - this.sendMessage(this.channel, "Début de la réunion hebdo"); - } - - private void end(final String sender) { - if (!this.owner.equalsIgnoreCase(sender)) { - return; - } - - this.sendMessage(this.channel, "Fin de la réunion hebdo"); - try { - FileUtils.writeStringToFile(this.output, this.meeting.toString()); - } catch (final IOException e) { - } - this.meeting = null; - } - - private void - startIndividualTopic(final String sender, final String message) { - if (!this.owner.equalsIgnoreCase(sender)) { - return; - } - - this.currentTopic = - new IndividualTopic(message.replaceFirst("^\\s*#\\s*", "")); - this.meeting.add(this.currentTopic); - this.sendMessage(this.channel, "Début topic individuel : " - + this.currentTopic.getTitle()); - } - - private void - startCollectiveTopic(final String sender, final String message) { - if (!this.owner.equalsIgnoreCase(sender)) { - return; - } - - this.currentTopic = - new CollectiveTopic(message.replaceFirst("^\\s*##\\s*", "")); - this.meeting.add(this.currentTopic); - this.sendMessage(this.channel, "Début topic collectif : " - + this.currentTopic.getTitle()); - } - - private void addToCurrentTopic(final Message message) { - if (this.currentTopic != null) { - this.currentTopic.add(message); - } - } - - 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"); - - final Bot bot = - new Bot(channel, new File(properties.getProperty("log.output"))); - bot.setName(properties.getProperty("irc.nick")); - bot.connect(properties.getProperty("irc.host"), - Integer.valueOf(properties.getProperty("irc.port"))); - bot.joinChannel(channel); - } -} diff --git a/src/main/java/fr/imirhil/april/hebdobot/irc/Bot.java b/src/main/java/fr/imirhil/april/hebdobot/irc/Bot.java new file mode 100644 index 0000000..b92a687 --- /dev/null +++ b/src/main/java/fr/imirhil/april/hebdobot/irc/Bot.java @@ -0,0 +1,180 @@ +package fr.imirhil.april.hebdobot.irc; + +import java.util.Collection; +import java.util.LinkedList; + +import org.jibble.pircbot.PircBot; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.imirhil.april.hebdobot.review.CollectiveTopic; +import fr.imirhil.april.hebdobot.review.IndividualTopic; +import fr.imirhil.april.hebdobot.review.Message; +import fr.imirhil.april.hebdobot.review.Review; +import fr.imirhil.april.hebdobot.review.Topic; + +public class Bot extends PircBot { + private static final Logger LOGGER = LoggerFactory.getLogger(Bot.class); + + private Review review = null; + private final String channel; + private final Collection listeners = + new LinkedList(); + + public Bot(final String name, final String channel) { + this.setName(name); + this.channel = channel; + } + + public void add(final ReviewListener listener) { + this.listeners.add(listener); + } + + @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(); + + this.handleDie(sender, message); + this.handleStartReview(sender, message); + this.handleStopReview(sender, message); + this.handleStartIndividualTopic(sender, message); + this.handleStartCollectiveTopic(sender, message); + this.handleComment(sender, message); + this.handleMessage(sender, message); + this.handleDisplayCurrent(sender, message); + this.handleHelp(sender, message); + } + + private void handleHelp(final String sender, final String message) { + if (!"!help".equalsIgnoreCase(message)) { + return; + } + + this.sendMessage(sender, "Bienvenue " + sender); + this.sendMessage(sender, "Je suis " + this.getName() + + ", le robot de gestion des revues hebdomadaires de l'APRIL"); + this.sendMessage(sender, "Voici les commandes que je comprend :"); + this.sendMessage(sender, " "); + this.sendMessage(sender, "— !debut : commencer une nouvelle revue"); + this.sendMessage(sender, "— !fin : terminer la revue en cours"); + this.sendMessage(sender, "— # titre : démarrer un sujet individuel"); + this.sendMessage(sender, "— ## titre : démarrer un sujet collectif"); + this.sendMessage(sender, "— !courant : affiche le sujet en cours"); + this.sendMessage(sender, "— % message : un commentaire"); + } + + private void handleMessage(final String sender, final String message) { + if (this.review == null || message.startsWith("%")) { + return; + } + this.review.add(new Message(sender, message)); + } + + private void handleComment(final String sender, final String message) { + if (this.review == null) { + return; + } + this.review.addRaw(new Message(sender, message)); + } + + private void handleDie(final String sender, final String message) { + if (!"!stop".equalsIgnoreCase(message)) { + return; + } + + this.disconnect(); + this.dispose(); + } + + private void handleStartReview(final String sender, final String message) { + if (!"!debut".equalsIgnoreCase(message)) { + return; + } + + this.review = new Review(sender); + this.sendMessage(sender, "Vous êtes le conducteur de réunion"); + this.sendMessage(sender, "Pour terminer la réunion, tapez \"!fin\""); + this.sendMessage("Début de la réunion hebdomadaire"); + } + + private void handleStopReview(final String sender, final String message) { + if (this.review == null || !"!fin".equalsIgnoreCase(message)) { + return; + } + + if (!this.review.isOwner(sender)) { + this.sendMessage(sender + + ", vous n'êtes pas le conducteur de la réunion"); + return; + } + + for (final ReviewListener listener : this.listeners) { + listener.onEnd(this.review); + } + + this.review = null; + this.sendMessage("Fin de la revue hebdomadaire"); + } + + private void handleStartIndividualTopic(final String sender, + final String message) { + if (this.review == null || !message.matches("\\s*#[^#].*")) { + return; + } + + if (!this.review.isOwner(sender)) { + this.sendMessage(sender + + ", vous n'êtes pas le conducteur de la réunion"); + return; + } + + final IndividualTopic topic = + new IndividualTopic(message.replaceFirst("#", "").trim()); + this.review.begin(topic); + this.sendMessage("Sujet individuel : " + topic.getTitle()); + } + + private void handleStartCollectiveTopic(final String sender, + final String message) { + if (this.review == null || !message.matches("\\s*##.*")) { + return; + } + + if (!this.review.isOwner(sender)) { + this.sendMessage(sender + + ", vous n'êtes pas le conducteur de la réunion"); + return; + } + + final CollectiveTopic topic = + new CollectiveTopic(message.replaceFirst("##", "").trim()); + this.review.begin(topic); + this.sendMessage("Sujet collectif : " + topic.getTitle()); + } + + private void + handleDisplayCurrent(final String sender, final String message) { + if (this.review == null || !"!courant".equalsIgnoreCase(message)) { + return; + } + + final Topic current = this.review.getCurrentTopic(); + if (current == null) { + this.sendMessage("Pas de sujet en cours"); + } else if (current instanceof IndividualTopic) { + this.sendMessage("Sujet individuel en cours : " + + current.getTitle()); + } else if (current instanceof CollectiveTopic) { + this.sendMessage("Sujet collectif en cours : " + current.getTitle()); + } + } + + public void sendMessage(final String message) { + this.sendMessage(this.channel, message); + } +} diff --git a/src/main/java/fr/imirhil/april/hebdobot/irc/ReviewListener.java b/src/main/java/fr/imirhil/april/hebdobot/irc/ReviewListener.java new file mode 100644 index 0000000..38dd232 --- /dev/null +++ b/src/main/java/fr/imirhil/april/hebdobot/irc/ReviewListener.java @@ -0,0 +1,7 @@ +package fr.imirhil.april.hebdobot.irc; + +import fr.imirhil.april.hebdobot.review.Review; + +public interface ReviewListener { + void onEnd(Review review); +} diff --git a/src/main/java/fr/imirhil/april/hebdobot/pastebin/Expiration.java b/src/main/java/fr/imirhil/april/hebdobot/pastebin/Expiration.java new file mode 100644 index 0000000..332707e --- /dev/null +++ b/src/main/java/fr/imirhil/april/hebdobot/pastebin/Expiration.java @@ -0,0 +1,15 @@ +package fr.imirhil.april.hebdobot.pastebin; + +public enum Expiration { + NEVER(null), MINUTE_10("10M"), HOUR_1("1H"), DAY_1("1D"), MONTH_1("1M"); + + private final String value; + + private Expiration(final String value) { + this.value = value; + } + + public String getValue() { + return this.value; + } +} diff --git a/src/main/java/fr/imirhil/april/hebdobot/pastebin/Format.java b/src/main/java/fr/imirhil/april/hebdobot/pastebin/Format.java new file mode 100644 index 0000000..6f091fd --- /dev/null +++ b/src/main/java/fr/imirhil/april/hebdobot/pastebin/Format.java @@ -0,0 +1,15 @@ +package fr.imirhil.april.hebdobot.pastebin; + +public enum Format { + NONE(null), C("c"), CPP("cpp"), JAVA("java"), JAVA_5("java5"); + + private final String value; + + private Format(final String value) { + this.value = value; + } + + public String getValue() { + return this.value; + } +} diff --git a/src/main/java/fr/imirhil/april/hebdobot/pastebin/Option.java b/src/main/java/fr/imirhil/april/hebdobot/pastebin/Option.java new file mode 100644 index 0000000..31d56c7 --- /dev/null +++ b/src/main/java/fr/imirhil/april/hebdobot/pastebin/Option.java @@ -0,0 +1,15 @@ +package fr.imirhil.april.hebdobot.pastebin; + +public enum Option { + PASTE("paste"); + + private final String value; + + private Option(final String value) { + this.value = value; + } + + public String getValue() { + return this.value; + } +} diff --git a/src/main/java/fr/imirhil/april/hebdobot/pastebin/PastebinClient.java b/src/main/java/fr/imirhil/april/hebdobot/pastebin/PastebinClient.java new file mode 100644 index 0000000..ee6351b --- /dev/null +++ b/src/main/java/fr/imirhil/april/hebdobot/pastebin/PastebinClient.java @@ -0,0 +1,116 @@ +package fr.imirhil.april.hebdobot.pastebin; + +import java.util.LinkedList; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.params.HttpParams; + +public class PastebinClient { + private static final String API_DEV_KEY = "api_dev_key"; + private static final String API_USER_KEY = "api_user_key"; + private static final String API_USER_NAME = "api_user_name"; + private static final String API_USER_PASSWORD = "api_user_password"; + private static final String API_OPTION = "api_option"; + private static final String API_PASTE_PRIVATE = "api_paste_private"; + private static final String API_PASTE_NAME = "api_paste_name"; + private static final String API_PASTE_EXPIRATION = "api_paste_expire_date"; + private static final String API_PASTE_FORMAT = "api_paste_format"; + private static final String API_PASTE_CODE = "api_paste_code"; + + private static final String API_ERROR = "Bad API request,"; + + public static class APIException extends Exception { + private APIException(final String message) { + super(message); + } + + private static void throwIfError(final String content) + throws APIException { + if (content.contains(API_ERROR)) { + throw new APIException(content.replaceFirst(API_ERROR, "")); + } + } + } + + private final String apiDevKey; + private String apiUserKey = null; + HttpClient httpClient = new DefaultHttpClient(); + + public PastebinClient(final String apiDevKey) { + this.apiDevKey = apiDevKey; + } + + public String paste(final String code) throws Exception { + return this.paste(code, null); + } + + public String paste(final String code, final String name) throws Exception { + return this.paste(code, name, Format.NONE, false, Expiration.DAY_1); + } + + public void login(final String name, final String password) + throws Exception { + final List params = new LinkedList(); + setParameter(params, API_DEV_KEY, this.apiDevKey); + setParameter(params, API_USER_NAME, name); + setParameter(params, API_USER_PASSWORD, password); + + final HttpPost request = + new HttpPost("http://pastebin.com/api/api_login.php"); + request.setEntity(new UrlEncodedFormEntity(params)); + + final HttpResponse response = this.httpClient.execute(request); + final String content = + IOUtils.toString(response.getEntity().getContent()); + APIException.throwIfError(content); + this.apiUserKey = content; + } + + public String paste(final String code, final String name, + final Format format, final boolean privat, + final Expiration expiration) throws Exception { + final List params = new LinkedList(); + setParameter(params, API_DEV_KEY, this.apiDevKey); + setParameter(params, API_USER_KEY, this.apiUserKey); + setParameter(params, API_OPTION, Option.PASTE.getValue()); + setParameter(params, API_PASTE_PRIVATE, privat ? "1" : "0"); + setParameter(params, API_PASTE_NAME, name); + setParameter(params, API_PASTE_EXPIRATION, expiration.getValue()); + setParameter(params, API_PASTE_FORMAT, format.getValue()); + setParameter(params, API_PASTE_CODE, code); + + final HttpPost request = + new HttpPost("http://pastebin.com/api/api_post.php"); + request.setEntity(new UrlEncodedFormEntity(params)); + + final HttpResponse response = this.httpClient.execute(request); + final String content = + IOUtils.toString(response.getEntity().getContent()); + APIException.throwIfError(content); + return content; + } + + private static void setParameter(final HttpParams params, + final String name, final Object value) { + if (value == null) { + return; + } + params.setParameter(name, value); + } + + private static void setParameter(final List params, + final String name, final String value) { + if (value == null) { + return; + } + params.add(new BasicNameValuePair(name, value)); + } +} diff --git a/src/main/java/fr/imirhil/april/hebdobot/CollectiveTopic.java b/src/main/java/fr/imirhil/april/hebdobot/review/CollectiveTopic.java similarity index 94% rename from src/main/java/fr/imirhil/april/hebdobot/CollectiveTopic.java rename to src/main/java/fr/imirhil/april/hebdobot/review/CollectiveTopic.java index 8a363e2..079c530 100644 --- a/src/main/java/fr/imirhil/april/hebdobot/CollectiveTopic.java +++ b/src/main/java/fr/imirhil/april/hebdobot/review/CollectiveTopic.java @@ -1,4 +1,4 @@ -package fr.imirhil.april.hebdobot; +package fr.imirhil.april.hebdobot.review; import java.util.HashSet; import java.util.LinkedList; diff --git a/src/main/java/fr/imirhil/april/hebdobot/IndividualTopic.java b/src/main/java/fr/imirhil/april/hebdobot/review/IndividualTopic.java similarity index 95% rename from src/main/java/fr/imirhil/april/hebdobot/IndividualTopic.java rename to src/main/java/fr/imirhil/april/hebdobot/review/IndividualTopic.java index 1cfe701..3ffea8e 100644 --- a/src/main/java/fr/imirhil/april/hebdobot/IndividualTopic.java +++ b/src/main/java/fr/imirhil/april/hebdobot/review/IndividualTopic.java @@ -1,4 +1,4 @@ -package fr.imirhil.april.hebdobot; +package fr.imirhil.april.hebdobot.review; import java.util.HashMap; import java.util.LinkedList; diff --git a/src/main/java/fr/imirhil/april/hebdobot/Message.java b/src/main/java/fr/imirhil/april/hebdobot/review/Message.java similarity index 91% rename from src/main/java/fr/imirhil/april/hebdobot/Message.java rename to src/main/java/fr/imirhil/april/hebdobot/review/Message.java index 5b94ee6..16f4f9b 100644 --- a/src/main/java/fr/imirhil/april/hebdobot/Message.java +++ b/src/main/java/fr/imirhil/april/hebdobot/review/Message.java @@ -1,4 +1,4 @@ -package fr.imirhil.april.hebdobot; +package fr.imirhil.april.hebdobot.review; import org.joda.time.DateTime; diff --git a/src/main/java/fr/imirhil/april/hebdobot/Meeting.java b/src/main/java/fr/imirhil/april/hebdobot/review/Review.java similarity index 72% rename from src/main/java/fr/imirhil/april/hebdobot/Meeting.java rename to src/main/java/fr/imirhil/april/hebdobot/review/Review.java index 8779641..08446e0 100644 --- a/src/main/java/fr/imirhil/april/hebdobot/Meeting.java +++ b/src/main/java/fr/imirhil/april/hebdobot/review/Review.java @@ -1,4 +1,4 @@ -package fr.imirhil.april.hebdobot; +package fr.imirhil.april.hebdobot.review; import java.util.HashSet; import java.util.LinkedList; @@ -7,10 +7,9 @@ import java.util.Set; import org.apache.commons.lang.StringUtils; -import fr.imirhil.april.hebdobot.xml.UserService; +import fr.imirhil.april.hebdobot.xml.UserAlias; -public class Meeting { - private static final UserService USER_SERVICE = new UserService(); +public class Review { private static final int LENGTH = 74; private final Set participants = new HashSet(); @@ -18,23 +17,48 @@ public class Meeting { new LinkedList(); private final List collectiveTopics = new LinkedList(); + private Topic currentTopic; private final List messages = new LinkedList(); + private final String owner; - public void add(final Topic topic) { - if (topic instanceof IndividualTopic) { - this.individualTopics.add((IndividualTopic) topic); - } else if (topic instanceof CollectiveTopic) { - this.collectiveTopics.add((CollectiveTopic) topic); - } + public String getOwner() { + return this.owner; + } + + public boolean isOwner(final String name) { + return this.owner.equalsIgnoreCase(name); + } + + public Review(final String owner) { + this.owner = owner; + } + + public Topic getCurrentTopic() { + return this.currentTopic; + } + + public void begin(final IndividualTopic topic) { + this.individualTopics.add(topic); + this.currentTopic = topic; + } + + public void begin(final CollectiveTopic topic) { + this.collectiveTopics.add(topic); + this.currentTopic = topic; } public void add(final Message message) { - this.messages.add(message); - this.participants.add(message.getAuthor()); + if (this.currentTopic != null) { + this.participants.add(message.getAuthor()); + this.currentTopic.add(message); + } } - @Override - public String toString() { + public void addRaw(final Message message) { + this.messages.add(message); + } + + public String toString(final UserAlias userService) { final StringBuffer buffer = new StringBuffer(); addBox(buffer, "Revue de la semaine en cours", '#'); buffer.append("\n"); @@ -43,8 +67,7 @@ public class Meeting { addBox(buffer, "Participants", '='); buffer.append("\n"); for (final String participant : this.participants) { - buffer.append("* " + Meeting.USER_SERVICE.getRealName(participant) - + "\n"); + buffer.append("* " + userService.getRealName(participant) + "\n"); } buffer.append("\n"); @@ -53,8 +76,7 @@ public class Meeting { addBox(buffer, "Sujets individuels", '='); for (final String participant : this.participants) { buffer.append("\n"); - buffer.append(getLine( - Meeting.USER_SERVICE.getRealName(participant), '-')); + buffer.append(getLine(userService.getRealName(participant), '-')); for (final IndividualTopic topic : this.individualTopics) { if (topic.hasParticipant(participant)) { buffer.append("\n"); diff --git a/src/main/java/fr/imirhil/april/hebdobot/Topic.java b/src/main/java/fr/imirhil/april/hebdobot/review/Topic.java similarity index 87% rename from src/main/java/fr/imirhil/april/hebdobot/Topic.java rename to src/main/java/fr/imirhil/april/hebdobot/review/Topic.java index f17cf47..0d97b34 100644 --- a/src/main/java/fr/imirhil/april/hebdobot/Topic.java +++ b/src/main/java/fr/imirhil/april/hebdobot/review/Topic.java @@ -1,4 +1,4 @@ -package fr.imirhil.april.hebdobot; +package fr.imirhil.april.hebdobot.review; import java.util.Set; diff --git a/src/main/java/fr/imirhil/april/hebdobot/statusnet/Client.java b/src/main/java/fr/imirhil/april/hebdobot/statusnet/Client.java new file mode 100644 index 0000000..1b07ba8 --- /dev/null +++ b/src/main/java/fr/imirhil/april/hebdobot/statusnet/Client.java @@ -0,0 +1,5 @@ +package fr.imirhil.april.hebdobot.statusnet; + +public class Client { + +} diff --git a/src/main/java/fr/imirhil/april/hebdobot/xml/UserService.java b/src/main/java/fr/imirhil/april/hebdobot/xml/UserAlias.java similarity index 86% rename from src/main/java/fr/imirhil/april/hebdobot/xml/UserService.java rename to src/main/java/fr/imirhil/april/hebdobot/xml/UserAlias.java index 3036d11..c65df5d 100644 --- a/src/main/java/fr/imirhil/april/hebdobot/xml/UserService.java +++ b/src/main/java/fr/imirhil/april/hebdobot/xml/UserAlias.java @@ -1,6 +1,6 @@ package fr.imirhil.april.hebdobot.xml; -import java.io.File; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -14,22 +14,22 @@ import javax.xml.validation.SchemaFactory; import org.xml.sax.SAXException; -public class UserService { +public class UserAlias { private final Map aliases = new HashMap(); - public UserService() { + public UserAlias(final InputStream source) { try { final Unmarshaller u = JAXBContext.newInstance(Users.class).createUnmarshaller(); u.setSchema(SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema( - UserService.class + UserAlias.class .getResource("/fr/imirhil/april/hebdobot/users.xsd"))); for (final User user : u - .unmarshal(new StreamSource(new File("users.xml")), - Users.class).getValue().getUser()) { + .unmarshal(new StreamSource(source), Users.class) + .getValue().getUser()) { final String realName = user.getRealName(); for (final String nick : user.getNick()) { this.aliases.put(nick, realName); diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties new file mode 100644 index 0000000..2f5926c --- /dev/null +++ b/src/main/resources/log4j.properties @@ -0,0 +1,7 @@ +log4j.rootLogger = warn, stdout + +log4j.logger.fr.imirhil.april.hebdobot = info + +log4j.appender.stdout = org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout = org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern = %d %-5p %c - %m%n