144 lines
3.8 KiB
Java
144 lines
3.8 KiB
Java
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.addToCurrentTopic(raw);
|
|
} else if (message.startsWith("!")) {
|
|
this.handleCommand(message.replaceFirst("!", ""));
|
|
}
|
|
}
|
|
|
|
this.log(new Message(sender, message));
|
|
} catch (final Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private void handleCommand(final String message) {
|
|
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);
|
|
}
|
|
}
|