📄 chipchatapplet.java
字号:
/*
* Created on 2003. 2. 20.
*/
package chipchatapplet;
import java.applet.Applet;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import netscape.javascript.JSException;
import netscape.javascript.JSObject;
/**
* Chipchat Applet.
* @author Mr. Lee
*/
public final class ChipChatApplet extends Applet {
/** Is debug mode? */
private final boolean debug = true;
/** Socket */
private Socket sock = null;
/** Input Stream */
private BufferedReader inStream = null;
/** Output Stream */
private DataOutputStream outStream = null;
/** win JSObject */
private JSObject win = null;
/** doc JSObject */
private JSObject doc = null;
/** Is connected? */
private boolean connected = false;
/** Id number of user */
private int userid = -1;
/** Id number of master */
private int hostid = -2;
/** Remained time of keeping quiet */
private long keepQuietTime = 0;
/**
* Get Applet information
* @return Information
*/
public String getAppletInfo() {
return "ChipChat Applet v1.0\r\nCopyright by Mr.Lee";
}
/**
* Init.
*/
public void init() {
logMsg("Init..");
this.setBackground(Color.white);
String keepSessionMinute = getParameter("keepsession");
if (keepSessionMinute != null) {
int m;
try {
m = Integer.parseInt(keepSessionMinute);
} catch (NumberFormatException e) {
logMsg("Error : 'keepsession' parameter is not number", e);
m = 9;
}
SessionKeeper keeper = new SessionKeeper(this, m);
keeper.setDaemon(true);
keeper.start();
}
}
/**
* Destroy.
*/
public void destroy() {
closeConnect();
}
private JSObject getWin() {
if (win == null)
win = JSObject.getWindow(this);
return win;
}
private JSObject getDoc() {
if (doc == null)
doc = (JSObject) getWin().getMember("document");
return doc;
}
/**
* Process messages which is received from server.
* @param msg received message.
*/
void processMessage(final String msg) {
logMsg("PMSG:" + msg);
try {
// Parsing msg....
int index = msg.indexOf(':');
String cmd;
if (index < 0) {
// ignore.
logMsg("NOT_CMD:" + msg);
return;
} else {
cmd = msg.substring(0, index);
}
if (cmd.equalsIgnoreCase("MSG")) {
String[] v = spliteString(msg, '>', index + 1);
wincall("chipchat_printMsg", new Object[] { v[0], v[1] });
} else if (cmd.equalsIgnoreCase("WSPSND")) {
String[] v = spliteString(msg, '>', index + 1);
wincall("chipchat_output_wspsnd", new Object[] { v[0], v[1] });
} else if (cmd.equalsIgnoreCase("WSPRCV")) {
String[] v = spliteString(msg, '>', index + 1);
wincall("chipchat_output_wsprcv", new Object[] { v[0], v[1] });
} else if (cmd.equalsIgnoreCase("ACK")) {
return;
} else if (cmd.equalsIgnoreCase("ERROR")) {
String error = msg.substring(index + 1);
wincall("chipchat_error", new Object[] { error });
} else if (cmd.equalsIgnoreCase("INFO")) {
String[] v = spliteString(msg, '>', index + 1);
if (v[0].equalsIgnoreCase("GetIn")) {
String[] v2 = spliteString(v[1], '>', 0);
wincall("chipchat_getin", new Object[] { v2[0], v2[1] });
} else if (v[0].equalsIgnoreCase("GetOut")) {
wincall("chipchat_getout", new Object[] { v[1] });
} else if (v[0].equalsIgnoreCase("ChangePasswd")) {
wincall("chipchat_passwdChanged", new Object[] { v[1] });
} else if (v[0].equalsIgnoreCase("ChangeRoomName")) {
wincall("chipchat_RoomnameChanged", new Object[] { v[1] });
} else if (v[0].equalsIgnoreCase("ChangeMaxMan")) {
wincall("chipchat_MaxmanChanged", new Object[] { v[1] });
} else if (v[0].equalsIgnoreCase("RoomInfo")) {
String[] p1 = spliteString(v[1], '>', 0);
String[] p2 = spliteString(p1[1], '>', 0);
wincall(
"chipchat_RoomInfo",
new Object[] { p2[1], p1[0], p2[0] });
} else {
logMsg("Need to Process.. : " + v[0]);
}
} else if (cmd.equalsIgnoreCase("USERS")) {
wincall("chipchat_userlistStart", null);
int i = index + 1;
while (true) {
int idx1 = msg.indexOf('<', i);
int idx2 = msg.indexOf('>', i);
if (idx1 == -1 || idx2 == -1) {
break;
}
String id = msg.substring(i, idx1);
String name = msg.substring(idx1 + 1, idx2);
i = idx2 + 1;
wincall("chipchat_userlistAdd", new Object[] { id, name });
}
wincall("chipchat_userlistEnd", null);
} else if (cmd.equalsIgnoreCase("ADMIN")) {
String msg2 = msg.substring(index + 1);
try {
hostid = Integer.parseInt(msg2);
} catch (NumberFormatException e) {
logMsg("Error.", e);
}
wincall("chipchat_setAdmin", new Object[] { msg2 });
} else if (cmd.equalsIgnoreCase("ADMCG")) {
String msg2 = msg.substring(index + 1);
wincall("chipchat_adminChange", new Object[] { msg2 });
} else if (cmd.equalsIgnoreCase("KEEPQUIET")) {
String[] v = spliteString(msg, '>', index + 1);
int who = Integer.parseInt(v[1]);
if (who == userid) {
keepQuietTime = System.currentTimeMillis();
}
wincall("chipchat_keepQuit", new Object[] { v[0], v[1] });
} else if (cmd.equalsIgnoreCase("KICKOUT")) {
String[] v = spliteString(msg, '>', index + 1);
int who = Integer.parseInt(v[1]);
if (who == userid) {
closeConnect();
}
wincall("chipchat_kickOut", new Object[] { v[0], v[1] });
} else if (cmd.equalsIgnoreCase("CUSTOM")) {
String[] p1 = spliteString(msg, '>', index + 1);
String[] p2 = spliteString(p1[1], '>', 0);
wincall(
"chipchat_custom",
new Object[] { p1[0], p2[0], p2[1] });
} else if (cmd.equalsIgnoreCase("Connected")) {
connected = true;
String msg2 = msg.substring(index + 1);
wincall("chipchat_connected", new Object[] { msg2 });
} else if (cmd.equalsIgnoreCase("ConnectID")) {
String submsg = msg.substring(index + 1);
userid = Integer.parseInt(submsg);
wincall("chipchat_setConnectID", new Object[] { submsg });
} else if (cmd.equalsIgnoreCase("ConnectName")) {
String error = msg.substring(index + 1);
wincall("chipchat_setConnectName", new Object[] { error });
} else if (
cmd.equalsIgnoreCase("Content-Type")
|| cmd.equalsIgnoreCase("Transfer-Encoding")
|| cmd.equalsIgnoreCase("Date")
|| cmd.equalsIgnoreCase("Server")) { // Ignores Comands....
return;
} else if (cmd.equals("CLOSED")) {
closeConnect();
} else {
logMsg("Need to Process.. : " + cmd);
}
} catch (Exception e) {
System.out.println("## Parsing Error... of [" + msg + "]");
e.printStackTrace();
}
}
/**
* Send message to server.
* @param msg message that will be sent.
*/
void sendToServer(final String msg) {
if (connected && outStream != null) {
try {
outStream.write((msg).getBytes());
} catch (IOException e) {
logMsg("Stream write error.", e);
}
} else {
wincall("chipchat_notconnected", null);
}
}
/**
* Close connection.
*/
void closeConnect() {
connected = false;
if (sock != null) {
try {
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
sock = null;
}
}
/**
* Split message to two part.
* @param src Source String.
* @param ch Splite token.
* @param from Starting position.
* @return Splited string array(size 2).
*/
String[] spliteString(final String src, final char ch, final int from) {
int index2 = src.indexOf(ch, from);
String writer;
if (index2 < 0) {
throw new IllegalArgumentException(
"Input source can not split by '"
+ ch
+ "'. ["
+ src
+ ":"
+ from
+ "]");
} else {
return new String[] {
src.substring(from, index2),
src.substring(index2 + 1)};
}
}
/**
* Call function of outside explorer's script.
* @param function function name.
* @param param Parameter values.
*/
void wincall(final String function, final Object[] param) {
Object[] realParam;
if (param == null) {
realParam = new Object[0];
} else {
realParam = param;
}
try {
getWin().call(function, realParam);
} catch (JSException e) {
callAlert(
"function : "
+ function
+ "(arg*"
+ realParam.length
+ ") is exist?");
}
}
/**
* Call alert function of outside explorer's script.
* @param msg Message.
*/
void callAlert(final String msg) {
try {
getWin().call("alert", new String[] { msg });
} catch (JSException e1) {
e1.printStackTrace();
}
}
/**
* Log message when debug is true.
* @param msg Message.
*/
void logMsg(final String msg) {
if (debug) {
System.out.println(msg);
}
}
/**
* Log message when debug is true.
* @param msg Message.
* @param e Exception.
*/
void logMsg(final String msg, final Throwable e) {
if (debug) {
System.out.println(msg + "\r\nCause : " + e.getMessage());
e.printStackTrace();
}
}
/**
* Convert html special charectors.
* @param src Source String.
* @return Conveted string.
*/
public static String htmlSpecialChars(final String src) {
return htmlSpecialChars(new StringBuffer(src)).toString();
}
/**
* Convert html special charectors.
* @param src Source String.
* @return Conveted string.
*/
static StringBuffer htmlSpecialChars(final StringBuffer src) {
if (src == null) {
return null;
}
int length = src.length();
StringBuffer result = new StringBuffer();
char c2 = 'x';
for (int i = 0; i < length; i++) {
char c1 = src.charAt(i);
if (c1 == '<') {
result.append("<");
} else if (c1 == '>') {
result.append(">");
} else if (c1 == '&') {
result.append("&");
} else if (c1 == '"') {
result.append(""");
} else if (c1 == '\'') {
result.append("'");
} else if (c1 == ' ' && c2 == ' ') {
result.append(" ");
} else {
result.append(c1);
}
c2 = c1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -