1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/**
* 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 org.jibble.pircbot.PircBot;
import org.jogamp.jabot.util.TimeTool;
public class CatOut extends PircBot {
private static final String HASH = "#";
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 joinMessage;
final boolean showHostname = false;
final String login, nick, nickPwd, server, channelNoHash;
final boolean verbose;
final long logrotate, logrotateStart;
final String logprefix, urlprefix;
final boolean htmlOut;
final File htmlHeader, htmlFooter;
{
String _joinMessage = "This channel is logged";
String _login=null, _nick=null, _nickPwd=null, _server=null, _channelNoHash=null;
boolean _verbose=false;
long _logrotate = 0, _logrotateStart=System.currentTimeMillis();
String _logprefix="", _urlprefix="";
boolean _htmlOut = false;
String _htmlHeader = null, _htmlFooter=null;
for(int i=0; i<args.length; i++) {
if(args[i].equals("-joinMessage")) {
i++;
_joinMessage = args[i];
} else if(args[i].equals("-login")) {
i++;
_login = args[i];
} else if( args[i].equals("-name") || args[i].equals("-nick") ) {
i++;
_nick = args[i];
} else if( args[i].equals("-nickpwd") ) {
i++;
_nickPwd = args[i];
} else if(args[i].equals("-server")) {
i++;
_server = args[i];
} else if(args[i].equals("-channel")) {
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];
} else if(args[i].equals("-urlprefix")) {
i++;
_urlprefix = args[i];
} else if(args[i].equals("-logrotateStart")) {
i++;
final String hhmmS = args[i];
if( 4 == hhmmS.length() ) {
final String hhS = hhmmS.substring(0, 2);
final String mmS = hhmmS.substring(2, 4);
final int hh = (int)atol(hhS, 0);
final int mm = (int)atol(mmS, 0);
final Calendar cal = new GregorianCalendar(TimeTool.getNearZuluTimeZone(), Locale.getDefault());
cal.set(Calendar.HOUR_OF_DAY, hh);
cal.set(Calendar.MINUTE, mm);
_logrotateStart = cal.getTimeInMillis();
}
}
}
if( null == _login ||
null == _nick ||
null == _server ||
null == _channelNoHash ||
( _htmlOut && null == _htmlHeader || null == _htmlFooter ) ) {
System.err.println("Incomplete commandline, use "+CatOut.class.getName()+" -login VAL -nick VAL -server VAL -channel VAL [-verbose] [-joinMessage VAL] [-htmlHeader VAL -htmlFooter VAL] [-logrotate millis [-logrotateStart hhmm] [-logprefix VAL] [-urlprefix VAL]]");
return;
}
joinMessage = _joinMessage;
login=_login; nick=_nick; nickPwd=_nickPwd;
server=_server; channelNoHash=_channelNoHash;
verbose=_verbose;
logrotate = _logrotate;
logrotateStart = _logrotateStart;
logprefix = _logprefix;
urlprefix = _urlprefix;
htmlOut = _htmlOut;
htmlHeader = new File(_htmlHeader);
htmlFooter= new File(_htmlFooter);
}
long nextLogrotate = logrotateStart + logrotate;
final Calendar logCal = new GregorianCalendar(TimeTool.getNearZuluTimeZone(), Locale.getDefault());
if(0 < logrotate) {
if( System.currentTimeMillis() > nextLogrotate ) {
nextLogrotate += logrotate;
}
System.err.println("Now : "+logCal.getTime());
logCal.setTimeInMillis(logrotateStart);
System.err.println("Logrotate Start : "+logCal.getTime());
logCal.setTimeInMillis(logrotate);
System.err.println("Logrotate Period: "+logCal.getTime()+", "+logrotate+" ms");
logCal.setTimeInMillis(nextLogrotate);
System.err.println("Next Logrotate-1: "+logCal.getTime());
logCal.setTimeInMillis(nextLogrotate+logrotate);
System.err.println("Next Logrotate-2: "+logCal.getTime());
}
final LogBot bot = new LogBot(showHostname, joinMessage, htmlOut);
bot.setVerbose(verbose);
bot.setLoginAndNick(login, nick);
LogStream logOut;
if ( 0 < logrotate ) {
logOut = new LogStream(bot, logprefix, urlprefix, server, channelNoHash, htmlHeader);
bot.setOut(logOut.out, true);
bot.setJoinMessage(joinMessage+" @ "+logOut.urlString);
bot.logNotice(bot.getJoinMessage());
} else {
logOut = null;
}
if( null != nickPwd && nickPwd.length() > 0 ) {
bot.setPostConnectAction(new Runnable() {
public void run() {
bot.identify(nickPwd);
}
});
}
bot.connect(server);
bot.joinChannel(HASH+channelNoHash);
if( 0 < logrotate ) {
while(true) { // forever !
final long now = System.currentTimeMillis();
if( now >= nextLogrotate ) {
// Swap Logfiles: Open and set new logfile, then close old one.
final LogStream _logOut = new LogStream(bot, logprefix, urlprefix, server, channelNoHash, htmlHeader);
bot.logNotice("Continue @ "+_logOut.urlString);
bot.setOut(_logOut.out, true);
bot.logNotice("Previous @ "+ logOut.urlString);
bot.setJoinMessage(joinMessage+" @ "+_logOut.urlString);
bot.sendLoggedNotice(HASH+channelNoHash, bot.getJoinMessage());
if(htmlOut) {
logOut.cat(htmlFooter);
}
logOut.close(); // implies flush
logOut = _logOut;
nextLogrotate += logrotate;
logCal.setTimeInMillis(now);
System.err.println("Now : "+logCal.getTime());
logCal.setTimeInMillis(nextLogrotate);
System.err.println("Next Logrotate: "+logCal.getTime());
}
try {
Thread.sleep(FLUSH_INTERVAL);
} catch (Exception e) { }
if(null != logOut) {
logOut.flush();
}
}
}
}
public static final long FLUSH_INTERVAL = 60*1000; // flush every minute
static class LogStream {
public final PrintStream out;
public final String fileName;
public final String urlString;
public LogStream(final LogBot bot, String logprefix, String urlprefix, String serverName, String channelName, File htmlHeader) throws Exception {
final Calendar calendar = bot.tick();
final String suffix = null != htmlHeader ? ".html" : ".txt" ;
final String baseName = channelName+"_"+TimeTool.getTimeStamp(calendar, true, false)+suffix;
fileName = logprefix+baseName;
urlString = urlprefix+baseName;
final File _file = new File(fileName);
out = new PrintStream(new FileOutputStream(_file));
if(null != htmlHeader) {
cat(htmlHeader);
htmlHead(HASH+channelName+" @ "+serverName+" - "+bot.getTimeStamp()+" ("+calendar.getTimeZone().getID()+")");
}
}
public void flush() {
out.flush();
}
public void close() {
out.close();
}
public void cat(File source) 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();
}
private void htmlHead(String head) {
out.println("<h2>"+head+"</h2>");
out.println("<br/>");
out.flush();
}
}
}
|