1 package rero.client.dcc; 2 3 import rero.dcc.*; 4 5 import rero.client.*; 6 import rero.client.user.*; 7 8 import rero.ircfw.*; 9 import rero.ircfw.interfaces.*; 10 11 import rero.util.*; 12 13 import java.io.*; 14 import java.util.*; 15 16 import rero.gui.*; 17 import rero.dialogs.dcc.*; 18 19 import rero.config.*; 20 21 import java.net.*; 22 23 public class LocalInfo extends Feature implements FrameworkConstants, ClientStateListener 24 { 25 public static final String RESOLVE_FROM_SERVER = "Server Method"; 26 public static final String RESOLVE_AUTOMATIC = "Normal Method"; 27 28 protected static String localHost = null; 29 30 public void setLocalHost(String host) 31 { 32 try 33 { 34 if (localHost == null) 35 { 36 localHost = InetAddress.getByName(host).getHostAddress(); 37 resolveHost = localHost; 38 39 HashMap eventDescription = new HashMap(); 40 eventDescription.put("$data", localHost + " " + localHost); 41 eventDescription.put("$parms", localHost); 42 getCapabilities().getOutputCapabilities().fireSetStatus(eventDescription, "RESOLVED_LOCALINFO"); 43 } 44 } 45 catch (Exception ex) 46 { 47 localHost = null; 48 resolveProcessed = false; 49 resolveHost = null; 50 ex.printStackTrace(); 51 } 52 } 53 54 public String localip() 55 { 56 if (localHost != null) 57 { 58 return localHost; 59 } 60 61 try 62 { 63 localHost = InetAddress.getLocalHost().getHostAddress(); 64 return localHost; 65 } 66 catch (Exception ex) 67 { 68 ex.printStackTrace(); 69 } 70 71 return "127.0.0.1"; 72 } 73 74 75 public void storeDataStructures(WeakHashMap data) 76 { 77 data.put("localInfo", this); 78 } 79 80 public void init() 81 { 82 ClientState.getClientState().addClientStateListener("dcc.localinfo", this); 83 processLocalInfoSettings(); 84 } 85 86 protected static boolean resolveProcessed = false; protected static String resolveHost = null; 89 public void processLocalInfoSettings() 90 { 91 if (localHost != null) return; 93 94 if (ClientState.getClientState().getString("dcc.localinfo", ClientDefaults.dcc_localinfo).equals(RESOLVE_AUTOMATIC)) 95 { 96 (new Thread (new Runnable () { public void run() { localip(); } })).start(); 97 } 98 else if (ClientState.getClientState().getString("dcc.localinfo", ClientDefaults.dcc_localinfo).equals(RESOLVE_FROM_SERVER)) 99 { 100 if (! resolveProcessed) 101 { 102 getCapabilities().addTemporaryListener(new ResolveLocalInfo()); 103 } 104 else 105 { 106 localHost = resolveHost; 107 } 108 } 109 else 110 { 111 localHost = ClientState.getClientState().getString("dcc.localinfo", "127.0.0.1"); 112 } 113 } 114 115 public void propertyChanged(String property, String parameter) 116 { 117 localHost = null; 118 processLocalInfoSettings(); 119 } 120 121 protected class ResolveLocalInfo implements ChatListener, FrameworkConstants 122 { 123 protected String myhost; 124 125 public ResolveLocalInfo() 126 { 127 if (getCapabilities().isConnected()) 128 { 129 getCapabilities().sendln("USERHOST " + ((InternalDataList)getCapabilities().getDataStructure(DataStructures.InternalDataList)).getMyNick()); 130 } 131 } 132 133 public int fireChatEvent(HashMap eventDescription) 134 { 135 if (eventDescription.get($EVENT$).equals("302")) 136 { 137 myhost = eventDescription.get("$address").toString(); 138 myhost = myhost.substring(myhost.indexOf('@') + 1, myhost.length()); 139 140 new Thread (new Runnable () 141 { 142 public void run() 143 { 144 setLocalHost(myhost); 145 } 146 }).start(); 147 148 resolveProcessed = true; 151 return REMOVE_LISTENER | EVENT_HALT; } 153 else if (eventDescription.get($EVENT$).equals("376")) 154 { 155 getCapabilities().sendln("USERHOST " + ((InternalDataList)getCapabilities().getDataStructure(DataStructures.InternalDataList)).getMyNick()); 156 } 157 158 return EVENT_DONE; 159 } 160 161 public boolean isChatEvent(String event, HashMap eventDescription) 162 { 163 return event.equals("302") || event.equals("376"); } 165 } 166 } 167 | Popular Tags |