summaryrefslogtreecommitdiff
path: root/NET/worlds/console/VoiceChat.java
blob: bf52f71715920bb1a5b169ca751bcba736abe1ee (plain) (blame)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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);
      }
   }
}