/** * Copyright Paul James Mutton, 2001-2009, http://www.jibble.org/ * * Copyright 2013 JogAmp Community. All rights reserved. * * This software is licensed under the GNU General Public License (GPL) Version 3. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ package org.jogamp.jabot.irc; import java.io.PrintStream; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Locale; import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jibble.pircbot.Colors; import org.jibble.pircbot.PircBot; import org.jogamp.jabot.util.TimeTool; public class LogBot extends PircBot { public static final String GREEN = "irc-green"; public static final String BLACK = "irc-black"; public static final String BROWN = "irc-brown"; public static final String NAVY = "irc-navy"; public static final String BRICK = "irc-brick"; public static final String RED = "irc-red"; private static final Pattern urlPattern = Pattern.compile("(?i:\\b((http|https|ftp|irc|file)://[^\\s]+))"); private static String ANONYMOUS = "anon"; private final boolean showHostname; private String joinMessage; private final Calendar calendar; private final Object timeSync = new Object(); private final boolean reconnectOnDisconnect; private final boolean xmlOut; private final Object outSync = new Object(); private PrintStream out; private int lineCount; private long lastTS; private Runnable postConnectAction; public LogBot(boolean showHostname, String joinMessage, boolean xmlOut, boolean reconnectOnDisconnect) { this(showHostname, joinMessage, TimeTool.getNearZuluTimeZone(), Locale.getDefault(), System.out, xmlOut, reconnectOnDisconnect); } public LogBot(boolean showHostname, String joinMessage, TimeZone timeZone, Locale locale, PrintStream out, boolean xmlOut, boolean reconnectOnDisconnect) { this.showHostname = showHostname; this.calendar = new GregorianCalendar(timeZone, locale); this.xmlOut = xmlOut; this.out = out; this.joinMessage = joinMessage; this.lineCount = 0; this.postConnectAction = null; this.lastTS = System.currentTimeMillis(); this.reconnectOnDisconnect = reconnectOnDisconnect; } @Override protected void handleLine(String line) { lastTS = System.currentTimeMillis(); super.handleLine(line); } public void sendPing() { this.sendRawLine("PING :" + getNick()); } /** Returns the last timestamp as set by ctor or receiving a line from server. */ public final long getLastTS() { return lastTS; } public final void setJoinMessage(String joinMessage) { this.joinMessage = joinMessage; } public final String getJoinMessage() { return joinMessage; } /** *
* Sets the internal login of the Bot. This should be set before joining * any servers. *
** Sets the internal nick of the bot. This is only to be called by the * PircBot class in response to notification of nick changes that apply * to us. *
*/ public final void setLoginAndNick(String login, String nick) { super.setLogin(login); super.setName(nick); } /** Sets the action to be performed after each connect, incl. reconnect. */ public final void setPostConnectAction(Runnable action) { this.postConnectAction = action; } public final Calendar getCalendar() { return calendar; } /** Returns timestamp of internal Calendar: YYYYMMDD HH:MM:SS (TMZ) */ public final String getTimeStamp() { return TimeTool.getTimeStamp(calendar, true, true); } public final String getTimeStamp(long millis) { final String ts; synchronized(timeSync) { calendar.setTimeInMillis(millis); ts = TimeTool.getTimeStamp(calendar, true, true); calendar.setTimeInMillis(System.currentTimeMillis()); } return ts; } /** Updates internal Calendar w/ current time and returns it.*/ public final Calendar tick() { synchronized(timeSync) { calendar.setTimeInMillis(System.currentTimeMillis()); return calendar; } } public PrintStream getOut() { synchronized(outSync) { return out; } } public PrintStream setOut(PrintStream newOut, boolean resetLineCount) { synchronized(outSync) { final PrintStream old = out; out = newOut; if( resetLineCount ) { lineCount = 0; } return old; } } public int getLineCount() { return lineCount; } public void resetLineCount() { synchronized(outSync) { lineCount = 0; } } public void append(String color, String line) { tick(); lineCount++; if(xmlOut) { line = Colors.removeFormattingAndColors(line); line = line.replaceAll("&", "&"); line = line.replaceAll("<", "<"); line = line.replaceAll(">", ">"); Matcher matcher = urlPattern.matcher(line); line = matcher.replaceAll("$1"); } synchronized(outSync) { if(xmlOut) { // 20130117 21:06:04 out.println("" + getTimeStamp() + " " + line + "