1 17 18 package rero.dcc; 19 20 import java.util.*; 21 22 import java.net.*; 23 import java.io.*; 24 25 import rero.ircfw.*; 26 import rero.ircfw.interfaces.*; 27 28 import rero.config.*; 29 30 public class Chat extends ProtocolDCC implements ClientStateListener 31 { 32 protected BufferedReader input; 33 protected PrintStream output; 34 35 public Chat(String _nickname) 36 { 37 nickname = _nickname; 38 ClientState.getClientState().addClientStateListener("client.encoding", this); 39 } 40 41 public void propertyChanged(String property, String value) 42 { 43 try 44 { 45 if (input != null && socket.isConnected()) 46 { 47 input = new BufferedReader( ClientState.getClientState().getProperInputStream( socket.getInputStream() ) ); 48 output = ClientState.getClientState().getProperPrintStream( socket.getOutputStream() ); 49 } 50 } 51 catch (Exception ex) 52 { 53 System.out.println("Unable to switch encodings..."); 54 ex.printStackTrace(); 55 } 56 } 57 58 59 public String getNickname() 60 { 61 return nickname; 62 } 63 64 65 public void sendln(String text) 66 { 67 output.println(text); 68 } 69 70 public int getTypeOfDCC() 71 { 72 return DCC_CHAT; 73 } 74 75 public void run() 76 { 77 if (socket == null || !socket.isConnected()) 78 { 79 return; 80 } 81 82 try 83 { 84 socket.setKeepAlive(true); 85 } 86 catch (Exception ex) 87 { 88 ex.printStackTrace(); 89 } 90 91 fireEvent("CHAT_OPEN", null); 92 93 String text; 94 95 try 96 { 97 output = ClientState.getClientState().getProperPrintStream( socket.getOutputStream() ); 98 input = new BufferedReader( ClientState.getClientState().getProperInputStream( socket.getInputStream() ) ); 99 100 while (socket.isConnected()) 101 { 102 text = input.readLine(); 103 if (text == null) 104 { 105 fireEvent("CHAT_CLOSE", "closed"); 106 return; 107 } 108 109 idleTime = System.currentTimeMillis(); 110 111 fireEvent("CHAT", text); 112 } 113 114 fireEvent("CHAT_CLOSE", "closed"); 115 } 116 catch (Exception ex) 117 { 118 ex.printStackTrace(); 119 120 fireError(ex.getMessage()); 121 } 122 } 123 124 public void fireEvent(String event, String description) 125 { 126 eventData.clear(); 127 128 eventData.put(FrameworkConstants.$NICK$, getNickname()); 129 eventData.put(FrameworkConstants.$EVENT$, event); 130 eventData.put("$this", this.toString()); 131 132 133 if (description != null) 134 { 135 eventData.put(FrameworkConstants.$DATA$, getNickname() + " " + description); 136 eventData.put(FrameworkConstants.$PARMS$, description); 137 } 138 139 dispatcher.dispatchEvent(eventData); 140 } 141 142 public void fireError(String description) 143 { 144 eventData.put(FrameworkConstants.$NICK$, getNickname()); 145 eventData.put(FrameworkConstants.$EVENT$, "CHAT_CLOSE"); 146 eventData.put(FrameworkConstants.$DATA$, getNickname() + " " + description); 147 eventData.put(FrameworkConstants.$PARMS$, description); 148 eventData.put("$this", this.toString()); 149 150 dispatcher.dispatchEvent(eventData); 151 } 152 } 153 | Popular Tags |