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
|
/**
* 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)://[^\\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 xmlOut;
private final Object outSync = new Object();
private PrintStream out;
public LogBot(boolean showHostname, String joinMessage, boolean xmlOut) {
this(showHostname, joinMessage, TimeTool.getNearZuluTimeZone(), Locale.getDefault(), System.out, xmlOut);
}
public LogBot(boolean showHostname, String joinMessage, TimeZone timeZone, Locale locale, PrintStream out, boolean xmlOut) {
this.showHostname = showHostname;
this.calendar = new GregorianCalendar(timeZone, locale);
this.xmlOut = xmlOut;
this.out = out;
this.joinMessage = joinMessage;
}
public final void setLoginAndName(String login, String nick) {
super.setLogin(login);
super.setName(nick);
}
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) {
synchronized(outSync) {
final PrintStream old = out;
out = newOut;
return old;
}
}
public void append(String color, String line) {
tick();
if(xmlOut) {
line = Colors.removeFormattingAndColors(line);
line = line.replaceAll("&", "&");
line = line.replaceAll("<", "<");
line = line.replaceAll(">", ">");
Matcher matcher = urlPattern.matcher(line);
line = matcher.replaceAll("<a href=\"$1\">$1</a>");
}
synchronized(outSync) {
if(xmlOut) {
out.println("<span class=\"irc-date\">[" + getTimeStamp() + "]</span> <span class=\"" + color + "\">" + line + "</span><br />");
} else {
out.println(getTimeStamp()+" "+line);
}
out.flush();
}
}
public void onAction(String sender, String login, String hostname, String target, String action) {
append(BRICK, "* " + sender + " " + action);
}
public void onJoin(String channel, String sender, String login, String hostname) {
append(GREEN, "* " + sender + " (" + login + "@" + ( showHostname ? hostname : ANONYMOUS ) + ") has joined " + channel);
if (sender.equals(getNick())) {
sendNotice(channel, joinMessage);
}
else {
sendNotice(sender, joinMessage);
}
}
public void onMessage(String channel, String sender, String login, String hostname, String message) {
append(BLACK, "<" + sender + "> " + message);
message = message.toLowerCase();
if (message.startsWith(getNick().toLowerCase()) && message.indexOf("help") > 0) {
sendMessage(channel, joinMessage);
}
}
public void onMode(String channel, String sourceNick, String sourceLogin, String sourceHostname, String mode) {
append(GREEN, "* " + sourceNick + " sets mode " + mode);
}
public void onNickChange(String oldNick, String login, String hostname, String newNick) {
append(GREEN, "* " + oldNick + " is now known as " + newNick);
}
public void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice) {
append(BROWN, "-" + sourceNick + "- " + notice);
}
public void onPart(String channel, String sender, String login, String hostname) {
append(GREEN, "* " + sender + " (" + login + "@" + ( showHostname ? hostname : ANONYMOUS ) + ") has left " + channel);
}
public void onPing(String sourceNick, String sourceLogin, String sourceHostname, String target, String pingValue) {
append(RED, "[" + sourceNick + " PING]");
}
public void onPrivateMessage(String sender, String login, String hostname, String message) {
append(BLACK, "<- *" + sender + "* " + message);
}
public void onQuit(String sourceNick, String sourceLogin, String sourceHostname, String reason) {
append(NAVY, "* " + sourceNick + " (" + sourceLogin + "@" + ( showHostname ? sourceHostname : ANONYMOUS ) + ") Quit (" + reason + ")");
}
public void onTime(String sourceNick, String sourceLogin, String sourceHostname, String target) {
append(RED, "[" + sourceNick + " TIME]");
}
public void onTopic(String channel, String topic, String setBy, long date, boolean changed) {
if (changed) {
append(GREEN, "* " + setBy + " changes topic to '" + topic + "'");
}
else {
append(GREEN, "* Topic is '" + topic + "'");
append(GREEN, "* Set by " + setBy + " on " + getTimeStamp(date));
}
}
public void onVersion(String sourceNick, String sourceLogin, String sourceHostname, String target) {
append(RED, "[" + sourceNick + " VERSION]");
}
public void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason) {
append(GREEN, "* " + recipientNick + " was kicked from " + channel + " by " + kickerNick);
if (recipientNick.equalsIgnoreCase(getNick())) {
joinChannel(channel);
}
}
public void onDisconnect() {
append(NAVY, "* Disconnected.");
while (!isConnected()) {
try {
reconnect();
}
catch (Exception e) {
try {
Thread.sleep(10000);
}
catch (Exception anye) {
// Do nothing.
}
}
}
}
}
|