Skip to content
This repository has been archived by the owner on Jul 27, 2019. It is now read-only.

Commit

Permalink
Log tailer enhancements.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnaude committed Nov 29, 2015
1 parent d020a67 commit 68fa5de
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 20 deletions.
6 changes: 6 additions & 0 deletions dependency-reduced-pom.xml
Expand Up @@ -144,6 +144,12 @@
<version>18.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.cnaude.nplugins</groupId>
<artifactId>ntheendagain</artifactId>
<version>0.6.10-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.cnaude.essentials</groupId>
<artifactId>essentials</artifactId>
Expand Down
34 changes: 26 additions & 8 deletions src/main/java/com/cnaude/purpleirc/LogTailer.java
Expand Up @@ -20,6 +20,7 @@
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener;
import org.apache.commons.io.input.TailerListenerAdapter;
import org.pircbotx.Channel;

/**
*
Expand Down Expand Up @@ -76,10 +77,31 @@ public class MyTailerListener extends TailerListenerAdapter {

@Override
public void handle(String line) {
if (ctcp) {
blockingCTCPMessage(target, line);
boolean okayToSend = false;
if (target.startsWith("#")) {
try {
for (Channel channel : ircBot.bot.getUserBot().getChannels()) {
if (channel.getName().equalsIgnoreCase(target)) {
okayToSend = true;
break;
}
}
} catch (Exception ex) {
plugin.logDebug(ex.getMessage());
}
} else {
blockingIRCMessage(target, line);
okayToSend = true;
}
if (okayToSend) {
String template = plugin.getMsgTemplate(ircBot.botNick, target, TemplateName.LOG_TAILER);
String message = plugin.tokenizer.logTailerTokenizer(file.getName(), line, template);
if (ctcp) {
blockingCTCPMessage(target, message);
} else {
blockingIRCMessage(target, message);
}
} else {
plugin.logDebug("[MyTailerListener] Can't send to " + target + " yet.");
}
}

Expand All @@ -89,23 +111,19 @@ private void blockingIRCMessage(final String target, final String message) {
if (!ircBot.isConnected()) {
return;
}
plugin.logDebug("[blockingIRCMessage] About to send IRC message to " + target + ": " + message);
ircBot.bot.sendIRC().message(target, message);
plugin.logDebug("[blockingIRCMessage] Message sent to " + target + ": " + message);
}

private void blockingCTCPMessage(final String target, final String message) {
if (!ircBot.isConnected()) {
return;
}
plugin.logDebug("[blockingCTCPMessage] About to send IRC message to " + target + ": " + message);
ircBot.bot.sendIRC().ctcpResponse(target, message);
plugin.logDebug("[blockingCTCPMessage] Message sent to " + target + ": " + message);
}

protected void stopTailer() {
if (tailer != null) {
plugin.logInfo("Stoping tailer.");
plugin.logInfo("Stopping tailer.");
tailer.stop();
}
}
Expand Down
47 changes: 36 additions & 11 deletions src/main/java/com/cnaude/purpleirc/PurpleBot.java
Expand Up @@ -59,7 +59,6 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;
import me.botsko.prism.actionlibs.QueryParameters;
import me.botsko.prism.events.BlockStateChange;
import org.apache.commons.io.input.Tailer;
import org.bukkit.Achievement;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
Expand Down Expand Up @@ -189,9 +188,9 @@ public final class PurpleBot {
public CaseInsensitiveMap<String> linkRequests;
public CaseInsensitiveMap<Collection<String>> remotePlayers;
public CaseInsensitiveMap<CaseInsensitiveMap<String>> remoteServerInfo;
private LogTailer tailer;
private final List<LogTailer> tailers;
private boolean tailerEnabled;
private String tailerFile;
private final List<String> tailerFiles;
private String tailerRecipient;
private boolean tailerCtcp;
/**
Expand Down Expand Up @@ -262,6 +261,8 @@ public PurpleBot(File file, PurpleIRC plugin) {
this.remotePlayers = new CaseInsensitiveMap<>();
this.remoteServerInfo = new CaseInsensitiveMap<>();
this.ircPrivateMsgMap = new CaseInsensitiveMap<>();
this.tailers = new ArrayList<>();
this.tailerFiles = new ArrayList<>();
config = new YamlConfiguration();
goodBot = loadConfig();
if (goodBot) {
Expand All @@ -286,8 +287,10 @@ public void run() {
}

public void buildBot(boolean reload) {
if (tailer != null) {
tailer.stopTailer();
if (!tailers.isEmpty()) {
for (LogTailer tailer : tailers) {
tailer.stopTailer();
}
}
Configuration.Builder configBuilder = new Configuration.Builder()
.setName(botNick)
Expand Down Expand Up @@ -368,14 +371,22 @@ public void buildBot(boolean reload) {
plugin.logInfo("Auto-connect is disabled. To connect: /irc connect " + bot.getNick());
}
plugin.logDebug("Max line length: " + configBuilder.getMaxLineLength());
if (tailerEnabled && !tailerFile.isEmpty() && !tailerRecipient.isEmpty()) {
tailer = new LogTailer(this, plugin, tailerRecipient, tailerCtcp, tailerFile);
if (tailerEnabled && !tailerFiles.isEmpty() && !tailerRecipient.isEmpty()) {
for (String tailerFile : tailerFiles) {
LogTailer tailer = new LogTailer(this, plugin, tailerRecipient, tailerCtcp, tailerFile);
tailers.add(tailer);
}
}
}

protected void stopTailer() {
if (tailer != null) {
tailer.stopTailer();
protected void stopTailers() {
if (!tailers.isEmpty()) {
for (LogTailer tailer : tailers) {
if (tailer != null) {
tailer.stopTailer();
}
}
tailers.clear();
}
}

Expand Down Expand Up @@ -802,7 +813,21 @@ private boolean loadConfig() {

// load tailer settings
tailerEnabled = config.getBoolean("file-tailer.enabled", false);
tailerFile = config.getString("file-tailer.file", "server.log");

String tailerFile = config.getString("file-tailer.file", "server.log");
if (!tailerFiles.contains(tailerFile)) {
tailerFiles.add(tailerFile);
plugin.logDebug(" Tailer File => " + tailerFile);
}
for (String f : config.getStringList("file-tailer.extra_files")) {
if (!tailerFiles.contains(f)) {
tailerFiles.add(f);
}
plugin.logDebug(" Tailer File => " + f);
}
if (tailerFiles.isEmpty()) {
plugin.logInfo(" No command recipients defined.");
}
tailerRecipient = config.getString("file-tailer.recipient", "");
tailerCtcp = config.getBoolean("file-tailer.ctcp", false);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cnaude/purpleirc/PurpleIRC.java
Expand Up @@ -348,7 +348,7 @@ public void onDisable() {
logInfo("Disconnecting IRC bots.");
for (PurpleBot ircBot : ircBots.values()) {
commandQueue.cancel();
ircBot.stopTailer();
ircBot.stopTailers();
ircBot.saveConfig(getServer().getConsoleSender());
ircBot.quit();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/cnaude/purpleirc/TemplateName.java
Expand Up @@ -147,4 +147,6 @@ public class TemplateName {
public final static String NTHE_END_AGAIN_SOFT = "ntheendagain-soft";
public final static String NTHE_END_AGAIN_CRYSTAL = "ntheendagain-crystal";

public final static String LOG_TAILER = "log-tailer";

}
Expand Up @@ -884,4 +884,10 @@ public String msgChatResponseTokenizer(CommandSender sender, Player targetPlayer
.replace("%TARGET%", targetPlayer.getName())
.replace("%MESSAGE%", message);
}

public String logTailerTokenizer(String file, String line, String template) {
return plugin.colorConverter.ircColorsToGame(template
.replace("%FILE%", file)
.replace("%LINE%", line));
}
}
1 change: 1 addition & 0 deletions src/main/resources/SampleBot.yml
Expand Up @@ -70,6 +70,7 @@ command-notify:
file-tailer:
enabled: false
file: 'server.log'
extra_files: []
recipient: '#minecraft-test'
ctcp: false
# Messaging flood control (game and IRC)
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Expand Up @@ -233,6 +233,8 @@ message-format:
# Flood control
game-flood-warning: '&3Message not sent to IRC due to spamming. &rCooldown: %COOLDOWN%s'
irc-flood-warning: '&3Message not sent to game due to spamming. &rCooldown: %COOLDOWN%s'
# Log tailer format
log-tailer: '[LOG: %FILE%] %LINE%'
# Format for the @list command in IRC
list-format: '[&9Minecraft&r] &2Online &r(%COUNT%/%MAX%): %PLAYERS%'
list-separator: ', '
Expand Down

0 comments on commit 68fa5de

Please sign in to comment.