diff options
Diffstat (limited to 'src/org/jogamp/jabot/irc/CatOut.java')
-rw-r--r-- | src/org/jogamp/jabot/irc/CatOut.java | 173 |
1 files changed, 126 insertions, 47 deletions
diff --git a/src/org/jogamp/jabot/irc/CatOut.java b/src/org/jogamp/jabot/irc/CatOut.java index af2f04d..c7888b3 100644 --- a/src/org/jogamp/jabot/irc/CatOut.java +++ b/src/org/jogamp/jabot/irc/CatOut.java @@ -1,59 +1,53 @@ +/** + * 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.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; import java.util.Calendar; -import java.util.GregorianCalendar; -import java.util.Locale; -import java.util.TimeZone; import org.jibble.pircbot.PircBot; import org.jogamp.jabot.util.TimeTool; public class CatOut extends PircBot { - private final TimeZone timeZone; - private final Locale locale; - private final Calendar calendar; + private static final String HASH = "#"; - public CatOut() { - this(TimeTool.getNearZuluTimeZone(), Locale.getDefault()); - } - - public CatOut(TimeZone timeZone, Locale locale) { - this.timeZone = timeZone; - this.locale = locale; - calendar = new GregorianCalendar(timeZone, locale); - } - - public final TimeZone getTimeZone() { return timeZone; } - public final Locale getLocale() { return locale; } - - /** Returns timestamp of internal Calendar: YYYYMMDD HH:MM:SS (TMZ) */ - public final String getTimeStamp() { - return TimeTool.getTimeStamp(calendar); - } - - public final void setLoginAndName(String login, String nick) { - super.setLogin(login); - super.setName(nick); - } - - /** Updates internal Calendar w/ current time */ - public final void tick() { - calendar.setTimeInMillis(System.currentTimeMillis()); - } - - public void onMessage(String channel, String sender, - String login, String hostname, String message) { - tick(); - System.out.println(getTimeStamp()+" <"+sender+"/"+login+">: "+message); + private static long atol(String str, long def) { + try { + return Long.parseLong(str); + } catch (Exception ex) { + ex.printStackTrace(); + } + return def; } public static void main(String[] args) throws Exception { - final String login, name, server, channel; + final String joinMessage = "This channel is logged"; + final String login, name, server, channelNoHash; final boolean verbose; + final long logrotate; + final String logprefix; + final boolean htmlOut; + final File htmlHeader, htmlFooter; { - String _login=null, _name=null, _server=null, _channel=null; + String _login=null, _name=null, _server=null, _channelNoHash=null; boolean _verbose=false; + long _logrotate = 0; + String _logprefix=""; + boolean _htmlOut = false; + String _htmlHeader = null, _htmlFooter=null; for(int i=0; i<args.length; i++) { if(args[i].equals("-login")) { i++; @@ -66,27 +60,112 @@ public class CatOut extends PircBot { _server = args[i]; } else if(args[i].equals("-channel")) { i++; - _channel= args[i]; + _channelNoHash= args[i]; + if( _channelNoHash.startsWith(HASH) ) { + _channelNoHash = _channelNoHash.substring(1); + } } else if(args[i].equals("-verbose")) { _verbose=true; + } else if(args[i].equals("-htmlHeader")) { + i++; + _htmlOut=true; + _htmlHeader=args[i]; + } else if(args[i].equals("-htmlFooter")) { + i++; + _htmlOut=true; + _htmlFooter=args[i]; + } else if(args[i].equals("-logrotate")) { + i++; + _logrotate= atol(args[i], _logrotate); + } else if(args[i].equals("-logprefix")) { + i++; + _logprefix = args[i]; } } if( null == _login || null == _name || null == _server || - null == _channel ) { - System.err.println("Incomplete commandline, use "+CatOut.class.getName()+" -login VAL -name VAL -server VAL -channel VAL [-verbose]"); + null == _channelNoHash || + ( _htmlOut && null == _htmlHeader || null == _htmlFooter ) ) { + System.err.println("Incomplete commandline, use "+CatOut.class.getName()+" -login VAL -name VAL -server VAL -channel VAL [-verbose] [-htmlHeader VAL -htmlFooter VAL] [-logrotate millis [-logprefix VAL]]"); return; } login=_login; name=_name; - server=_server; channel=_channel; + server=_server; channelNoHash=_channelNoHash; verbose=_verbose; + logrotate = _logrotate; + logprefix = _logprefix; + htmlOut = _htmlOut; + htmlHeader = new File(_htmlHeader); + htmlFooter= new File(_htmlFooter); } - final CatOut bot = new CatOut(); + final LogBot bot = new LogBot(joinMessage, htmlOut); bot.setVerbose(verbose); - bot.setLoginAndName(login, name); + bot.setLoginAndName(login, name); bot.connect(server); - bot.joinChannel(channel); + bot.joinChannel(HASH+channelNoHash); + if( 0 >= logrotate ) { + // bot.sendRawLine("NAMES "+HASH+channelNoHash); // FIXME ? + } else { + PrintStream fout = null; + long t0 = System.currentTimeMillis(); + int sendNamesCount = 0; + while(true) { // forever ! + // Swap Logfiles: Open and set new logfile, then close old one. + { + final Calendar calendar = bot.tick(); + final String suffix = htmlOut ? ".html" : ".txt" ; + final String logfilename = logprefix+channelNoHash+"_"+name+"_"+TimeTool.getTimeStamp(calendar, true, false)+suffix; + final File _file = new File(logfilename); + final PrintStream _fout = new PrintStream(new FileOutputStream(_file)); + if(htmlOut) { + cat(htmlHeader, _fout); + htmlHead(HASH+channelNoHash+" @ "+server+" - "+bot.getTimeStamp()+" ("+calendar.getTimeZone().getID()+")", + _fout); + } + bot.setOut(_fout); + // bot.sendRawLine("NAMES "+HASH+channelNoHash); // FIXME ? + if(null != fout) { + if(htmlOut) { + cat(htmlFooter, fout); + } + fout.close(); // implies flush + } + fout = _fout; + } + final long t1 = System.currentTimeMillis(); + long td = 0; + while ( td < logrotate ) { + try { + Thread.sleep(FLUSH_INTERVAL); + } catch (Exception e) { } + fout.flush(); + final long t2 = System.currentTimeMillis(); + td = t2 - t1; + if ( sendNamesCount < ( t2 - t0 ) / SEND_NAMES ) { + // bot.sendRawLine("NAMES "+HASH+channelNoHash); // FIXME ? + sendNamesCount++; + } + } + } + } } + public static final long FLUSH_INTERVAL = 60*1000; // every minute + public static final long SEND_NAMES = 10*60*1000; // every 10 minutes + + public static void cat(File source, PrintStream out) throws IOException { + final BufferedInputStream input = new BufferedInputStream(new FileInputStream(source)); + int bytesRead = 0; + byte[] buffer = new byte[1024]; + while ((bytesRead = input.read(buffer, 0, buffer.length)) != -1) { + out.write(buffer, 0, bytesRead); + } + out.flush(); + input.close(); + } + public static void htmlHead(String head, PrintStream out) { + out.println("<h2>"+head+"</h2>"); + out.println("<br/>"); + } } |