diff options
Diffstat (limited to 'src/org/jogamp')
-rw-r--r-- | src/org/jogamp/jabot/irc/CatOut.java | 92 | ||||
-rw-r--r-- | src/org/jogamp/jabot/util/TimeTool.java | 55 |
2 files changed, 147 insertions, 0 deletions
diff --git a/src/org/jogamp/jabot/irc/CatOut.java b/src/org/jogamp/jabot/irc/CatOut.java new file mode 100644 index 0000000..af2f04d --- /dev/null +++ b/src/org/jogamp/jabot/irc/CatOut.java @@ -0,0 +1,92 @@ +package org.jogamp.jabot.irc; + +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; + + 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); + } + + public static void main(String[] args) throws Exception { + final String login, name, server, channel; + final boolean verbose; + { + String _login=null, _name=null, _server=null, _channel=null; + boolean _verbose=false; + for(int i=0; i<args.length; i++) { + if(args[i].equals("-login")) { + i++; + _login = args[i]; + } else if(args[i].equals("-name")) { + i++; + _name = args[i]; + } else if(args[i].equals("-server")) { + i++; + _server = args[i]; + } else if(args[i].equals("-channel")) { + i++; + _channel= args[i]; + } else if(args[i].equals("-verbose")) { + _verbose=true; + } + } + 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]"); + return; + } + login=_login; name=_name; + server=_server; channel=_channel; + verbose=_verbose; + } + + final CatOut bot = new CatOut(); + bot.setVerbose(verbose); + bot.setLoginAndName(login, name); + bot.connect(server); + bot.joinChannel(channel); + } +} diff --git a/src/org/jogamp/jabot/util/TimeTool.java b/src/org/jogamp/jabot/util/TimeTool.java new file mode 100644 index 0000000..de90361 --- /dev/null +++ b/src/org/jogamp/jabot/util/TimeTool.java @@ -0,0 +1,55 @@ +package org.jogamp.jabot.util; + +import java.util.Calendar; +import java.util.TimeZone; + +public class TimeTool { + public static String UTC_TZ = "UTC"; + public static String[] ZERO_TZs = { UTC_TZ, "GMT", "GMT0", "Zulu" }; + + /** Returns closest to Zulu/UTC TimeZone w/ shortest ID */ + public static TimeZone getNearZuluTimeZone() { + TimeZone tz = null; + // Get preferred .. + for(int i=0; null == tz && i<ZERO_TZs.length; i++) { + tz = TimeZone.getTimeZone(ZERO_TZs[i]); + } + if(null == tz) { + // Get shortest .. + final String[] ids = TimeZone.getAvailableIDs(0); + if( null != ids && ids.length > 0 ) { + int min_len = Integer.MAX_VALUE; + int min_idx = -1; + for(int i=0; i<ids.length; i++) { + final int len = ids[i].length(); + if( len < min_len ) { + min_len = len; + min_idx = i; + } + } + tz = TimeZone.getTimeZone(ids[min_idx]); + } + } + if(null == tz) { + // last resort - default + tz = TimeZone.getDefault(); + } + return tz; + } + + /** Returns timestamp of internal Calendar: YYYYMMDD HH:MM:SS (TMZ) */ + public static String getTimeStamp(final Calendar calendar) { + final int year, month, day, hour, minute, seconds; + { + year = calendar.get(Calendar.YEAR); + month = calendar.get(Calendar.MONTH) + 1; // Jan - 1 + day = calendar.get(Calendar.DAY_OF_MONTH); + hour = calendar.get(Calendar.HOUR_OF_DAY); + minute = calendar.get(Calendar.MINUTE); + seconds = calendar.get(Calendar.SECOND); + } + return String.format("%04d%02d%02d %02d:%02d:%02d (%s)", + year, month, day, hour, minute, seconds, calendar.getTimeZone().getID()); + } + +} |