package NET.worlds.console; import NET.worlds.core.IniFile; import NET.worlds.network.URL; import NET.worlds.scape.CDAudio; import NET.worlds.scape.Pilot; import NET.worlds.scape.WavSoundPlayer; import java.io.File; import java.io.IOException; import java.net.InetAddress; import java.text.MessageFormat; public class VoiceChat implements DialogReceiver { private static final String SPEAKFREELY = "sfmain.exe"; private static final int INITIAL_STATE = 0; private static final int BEGINNING_CONNECTION = 1; private static final int REQUESTING_CONNECTION = 2; private static final int ATTEMPTING_CONNECTION = 3; private static final int SUCCESSFULLY_CONNECTED = 4; private static final int CONNECTION_FAILED_RESETTING = 5; private static final int CLOSING_CONNECTION = 6; private static final int CONNECTION_CLOSED_RESETTING = 7; private static final String[] _stateStrs = new String[]{ "Initial State", "Beginning Connection", "Attempting Connection", "Successfully Connected", "Resetting (Connection Failed)", "Closing Connection", "Resetting (Connection Closed)" }; private int _state = 0; private String _error = ""; private static boolean _selfWhisper = false; public static String VCdebugCommand = "&|+debug>VCcommand="; public static String VCdebugCommandReset = "&|+debug>VCcommandReset="; private static String _extraCommand = ""; private static GammaPhoneMonitor chatMonitor = null; private String _who = null; public static UpdateableDialog voiceChatDialog; private static UpdateableDialog voiceChatQueryDialog; private static String voiceChatQueryIP; private static String voiceChatQueryWho; public static boolean voiceChatEnabled = IniFile.gamma().getIniInt("VoiceChat", voiceChatAvailable() ? 1 : 0) != 0; private static boolean voiceChatExeChecked = false; private static boolean voiceChatExists = false; private int _gammaphoneWindow = 0; DefaultConsole _console = null; VCTimerThread _vtimer = null; private static final String voiceChatQuery = "&|+voicechat?"; private static final String voiceChatAccept = "&|+voicechat>accept"; private static final String voiceChatReject = "&|+voicechat>reject"; private static final String voiceChatBusy = "&|+voicechat>busy"; private static final String voiceChatNotVIP = "&|+voicechat>notvip"; private static final String voiceChatDisabled = "&|+voicechat>disabled"; public int getState() { return this._state; } public String getStateString() { return _stateStrs[this._state]; } public String getLastError() { return this._error; } public static void activateSelfWhisper() { _selfWhisper = true; Console.println(Console.message("Selfwhisper-on")); } public static void deactivateSelfWhisper() { _selfWhisper = false; Console.println(Console.message("Selfwhisper-off")); } public static void setExtra(String text) { int start = text.indexOf(34) + 1; int end = text.lastIndexOf(34); _extraCommand = text.substring(start, end); Object[] arguments = new Object[]{new String(_extraCommand)}; Console.println(MessageFormat.format(Console.message("Add-VoiceChat"), arguments)); } public static void resetExtra() { _extraCommand = ""; Console.println(Console.message("VoiceChat-reset")); } public String who() { return this._who; } public static void setVoiceChatEnabled(boolean f) { if (!voiceChatAvailable()) { f = false; } voiceChatEnabled = f; IniFile.gamma().setIniInt("VoiceChat", f ? 1 : 0); } public static boolean voiceChatAvailable() { if (voiceChatExeChecked) { return voiceChatExists; } else { URL vcexe = URL.make("home:sfmain.exe"); String fname = vcexe.unalias(); File f = new File(fname); voiceChatExists = f.exists(); voiceChatExeChecked = true; return voiceChatExists; } } public void registerProcess(int id) { this._gammaphoneWindow = id; } public void beginChat(String friendName, DefaultConsole console) { this._state = 1; this._console = console; if (!console.getVIP()) { this.resetConnection(Console.message("must-be-VIP")); } else { String ip = null; try { InetAddress localAddr = console.getServerNew().getLocalAddress(); ip = localAddr != null ? localAddr.toString() : null; ip = ip.substring(ip.indexOf(47) + 1); } catch (SecurityException var7) { this.resetConnection(Console.message("sec-violation")); } if (ip == null) { this.resetConnection(Console.message("cant-determine-IP")); } else { if (voiceChatDialog != null) { if (friendName.equals(this._who)) { Object[] arguments = new Object[]{new String(this._who)}; Console.println(MessageFormat.format(Console.message("already-VoiceChat"), arguments)); return; } this.resetConnection(Console.message("chat-ended")); } if (voiceChatQueryDialog != null && voiceChatQueryWho.equals(friendName)) { Object[] arguments = new Object[]{new String(friendName)}; Console.println(MessageFormat.format(Console.message("already-calling"), arguments)); return; } while (ConnectionRecord.checkList(this._who)) { Object[] arguments = new Object[]{new String(this._who)}; Console.println(MessageFormat.format(Console.message("wait-a-few"), arguments)); try { Thread.sleep(2000L); } catch (InterruptedException var6) { } } if (this.initiateVoiceChat(friendName, null)) { Object[] arguments = new Object[]{new String(friendName), new String(ip)}; Console.println(MessageFormat.format(Console.message("Asking-to-call"), arguments)); this._state = 2; this.sendWhisper(friendName, "&|+voicechat?" + ip); this._vtimer = new VCTimerThread(this, 30000L); this._vtimer.start(); } } } } private static native void terminateVC(int var0); private boolean initiateVoiceChat(String friendName, String ip) { this._who = friendName; int hWnd = Window.getHWnd(); String startString = URL.make( "home:sfmain.exe" + (ip == null ? " -a " : " -c " + ip) + " -g " + hWnd + " -p " + Window.getGammaProcessID() + " " + _extraCommand + " " ) .unalias(); WavSoundPlayer.pauseSystem(); CDAudio.get().setEnabled(false); if (tryToRun(startString)) { Object[] arguments = new Object[]{new String(this._who)}; voiceChatDialog = new UpdateableDialog( Console.getFrame(), this, Console.message("Voice-connect"), null, Console.message("Terminate"), MessageFormat.format(Console.message("end-VoiceChat"), arguments), false ); if (chatMonitor == null) { chatMonitor = new GammaPhoneMonitor(this); chatMonitor.start(); } return true; } else { WavSoundPlayer.resumeSystem(); CDAudio.get().setEnabled(true); Object[] arguments = new Object[]{new String(this._who), new String(startString)}; voiceChatDialog = new UpdateableDialog( Console.getFrame(), this, Console.message("Voice-Chat-199"), null, Console.message("Continue"), MessageFormat.format(Console.message("Unable-to-chat"), arguments), false ); return false; } } public void inform(int msgid, String msg) { if (msgid >= 900) { if (voiceChatDialog == null) { return; } voiceChatDialog.setTitle(Console.message("VoiceChat-Connected")); this._state = 4; } else { this.resetConnection(msg); } } public boolean handleChatRequest(String user, String text, DefaultConsole console) { Object[] arguments = new Object[]{new String(user)}; if (!console.getVIP()) { Console.println(MessageFormat.format(Console.message("reject-chat"), arguments)); this.sendWhisper(user, "&|+voicechat>notvip"); } else if (!voiceChatEnabled) { Console.println(MessageFormat.format(Console.message("chat-disabled"), arguments)); this.sendWhisper(user, "&|+voicechat>disabled"); } else if (voiceChatQueryDialog != null) { this.sendWhisper(user, "&|+voicechat>busy"); } else { voiceChatQueryIP = text.substring("&|+voicechat?".length()); voiceChatQueryWho = user; voiceChatQueryDialog = new UpdateableDialog( Console.getFrame(), this, Console.message("Accept-voice"), Console.message("Reject"), Console.message("Accept"), MessageFormat.format(Console.message("do-you-voice"), arguments), false ); } return true; } public boolean handleChatWhisper(String user, String whisper, DefaultConsole console) { Object[] arguments = new Object[]{new String(user)}; if (whisper.startsWith("&|+voicechat?")) { this.handleChatRequest(user, whisper, console); return true; } else { if (whisper.startsWith("&|+voicechat>accept")) { if (this._state != 2) { this.sendWhisper(user, "&|+voicechat>reject"); return false; } Console.println(MessageFormat.format(Console.message("chat-accepted"), arguments)); synchronized (this._vtimer) { this._vtimer._vc = null; } this._vtimer = null; this._state = 3; this._vtimer = new VCTimerThread(this, 15000L); this._vtimer.start(); } else if (whisper.startsWith("&|+voicechat>notvip")) { if (voiceChatDialog != null && this._who.equals(user)) { this.resetConnection(MessageFormat.format(Console.message("cannot-voice"), arguments)); } } else if (whisper.startsWith("&|+voicechat>disabled")) { if (voiceChatDialog != null && this._who.equals(user)) { this.resetConnection(MessageFormat.format(Console.message("user-voice-dis"), arguments)); } } else if (whisper.startsWith("&|+voicechat>busy")) { if (voiceChatDialog != null && this._who.equals(user)) { this.resetConnection(MessageFormat.format(Console.message("receiving-Voice"), arguments)); } } else if (whisper.startsWith("&|+voicechat>reject")) { if (voiceChatDialog != null && this._who.equals(user)) { this.resetConnection(MessageFormat.format(Console.message("Voice-terminated"), arguments)); } if (voiceChatQueryDialog != null && voiceChatQueryWho != null && voiceChatQueryWho.equals(user)) { voiceChatQueryDialog.closeIt(false); voiceChatQueryWho = null; this.resetConnection(MessageFormat.format(Console.message("Voice-terminated"), arguments)); } } return false; } } public static boolean tryToRun(String s) { try { Runtime.getRuntime().exec(s); return true; } catch (IOException var2) { return false; } } public void checkConnected() { if (this.getState() == 2) { this.resetConnection(Console.message("never-answered")); this.sendWhisper(this.who(), "&|+voicechat>reject"); } else { if (this.getState() != 4) { this.resetConnection(Console.message("VoiceChat-failed")); } } } public void resetConnection(String err) { this._error = err; if (err != null) { this._state = 5; Console.println(this._error); } if (voiceChatDialog != null) { voiceChatDialog.closeIt(true); } } public void terminate() { if (this._vtimer != null) { synchronized (this._vtimer) { this._vtimer._vc = null; } this._vtimer = null; } CDAudio.get().setEnabled(true); ConnectionRecord.getList().addElement(new ConnectionRecord(this.who())); this._state = 0; } public void release() { if (chatMonitor != null) { chatMonitor.stop(); chatMonitor = null; } } @Override public synchronized void dialogDone(Object who, boolean confirmed) { if (who == voiceChatQueryDialog) { voiceChatQueryDialog = null; if (confirmed) { this.sendWhisper(voiceChatQueryWho, "&|+voicechat>accept"); this.initiateVoiceChat(voiceChatQueryWho, voiceChatQueryIP); } else if (voiceChatQueryWho != null) { this.sendWhisper(voiceChatQueryWho, "&|+voicechat>reject"); } } else if (who == voiceChatDialog) { this._state = 6; String stopString = URL.make("home:sfmain.exe -q").unalias(); tryToRun(stopString); voiceChatDialog = null; this.sendWhisper(this._who, "&|+voicechat>reject"); this.terminate(); } else { String stopString = URL.make("home:sfmain.exe -q").unalias(); tryToRun(stopString); } } private void sendWhisper(String destination, String text) { Pilot.sendText(destination, text); if (_selfWhisper) { this.handleChatWhisper(destination, text, this._console); } } }