KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > client > server > ServerHandler


1 package rero.client.server;
2
3 import rero.ircfw.*;
4 import rero.ircfw.interfaces.*;
5
6 import rero.net.*;
7 import rero.net.interfaces.*;
8
9 import rero.client.*;
10 import rero.client.notify.*;
11 import rero.bridges.set.*;
12
13 import rero.dialogs.server.*;
14 import rero.dck.items.NetworkSelect;
15
16 import rero.ident.*;
17
18 import java.util.*;
19 import rero.util.*;
20 import rero.config.*;
21
22 /**
23  * Responsible for the following:
24     - miscellaneous features i.e. responding to server PING's etc
25     - send altnick IF we're not connected yet and get a reply of nick in use.
26  */

27 public class ServerHandler extends Feature implements FrameworkConstants, SocketStatusListener, ChatListener, IdentListener
28 {
29    protected NotifyData notify;
30    protected InternalDataList data;
31    protected SocketConnection socket;
32
33    protected User restoreInformation = null;
34    protected String JavaDoc restoreServer;
35
36    protected IgnoreHandler ignoreHandler;
37    protected NickInUseListener nickListener = null;
38
39    public void init()
40    {
41       getCapabilities().getChatFramework().getProtocolDispatcher().setInternalListener(this); // make sure we are the first
42
// listener to handle this stuff...
43
getCapabilities().getSocketConnection().addSocketStatusListener(this);
44       socket = getCapabilities().getSocketConnection();
45
46       data = (InternalDataList)getCapabilities().getDataStructure("clientInformation");
47
48       notify = (NotifyData)getCapabilities().getDataStructure("notify");
49
50       ignoreHandler = new IgnoreHandler(); // handles processing for the ignore list
51

52       IdentDaemon.getIdentDaemon().addIdentListener(this);
53    }
54
55    public void identRequest(String JavaDoc host, String JavaDoc text)
56    {
57        getCapabilities().getOutputCapabilities().fireSetStatus(ClientUtils.getEventHashMap(host, text), "IDENT_REQUEST");
58    }
59    
60    public void cleanup()
61    {
62       data.reset();
63       notify.reset();
64
65       restoreInformation = null;
66    }
67
68    public int fireChatEvent (HashMap eventDescription)
69    {
70       String JavaDoc event = (String JavaDoc)eventDescription.get($EVENT$);
71
72       if (event.equals("376")) // end of MOTD command.
73
{
74          if (restoreInformation != null)
75          {
76             Iterator i = restoreInformation.getChannels().iterator();
77             while (i.hasNext())
78             {
79                Channel temp = (Channel)i.next();
80                getCapabilities().sendln("JOIN " + temp.getName() + " :" + temp.getKey());
81             }
82
83             restoreInformation = null;
84          }
85  
86          return EVENT_DONE;
87       }
88
89       if (event.equals("PING"))
90       {
91          getCapabilities().sendln("PONG :" + eventDescription.get($PARMS$));
92          return EVENT_HALT;
93       }
94
95       if (ignoreHandler.isIgnore((String JavaDoc)eventDescription.get($NICK$), (String JavaDoc)eventDescription.get($ADDRESS$)))
96       {
97          return EVENT_HALT;
98       }
99       else
100       {
101          return EVENT_DONE;
102       }
103    }
104
105    public boolean isChatEvent(String JavaDoc eventId, HashMap eventDescription)
106    {
107       if (ignoreHandler.isCheckingIgnore() && (eventId.equals("NOTICE") || eventId.equals("PRIVMSG") || eventId.equals("REPLY") || eventId.equals("REQUEST") || eventId.equals("ACTION"))) { return true; }
108       if (eventId.equals("376")) { return true; }
109       if (eventId.equals("PING")) { return true; } // yeaperz, we handle PING events.
110

111       return false;
112    }
113
114    public void socketStatusChanged(SocketEvent ev)
115    {
116        if (ev.data.isConnected)
117        {
118            getCapabilities().getOutputCapabilities().fireSetStatus(ClientUtils.getEventHashMap(ev.data.hostname, "connected"), "IRC_CONNECT");
119
120            String JavaDoc[] parms = ClientState.getClientState().getString("user.email", "jircii@127.0.0.1").split("@");
121
122            if (parms.length == 1)
123            {
124                parms = new String JavaDoc[] { parms[0], "127.0.0.1" };
125
126                if (parms[0].length() == 0) { parms[0] = "jircii"; }
127            }
128
129            if (ev.data.password != null)
130            {
131               getCapabilities().sendln("PASS " + ev.data.password);
132            }
133
134            String JavaDoc user, nick;
135
136            if (rero.test.QuickConnect.IsQuickConnect())
137            {
138               user = ClientState.getClientState().getString("user.rname", "jIRCii Web User: http://jirc.hick.org/");
139               nick = ClientState.getClientState().getString("user.nick", rero.test.QuickConnect.GetInformation().getNickname());
140            }
141            else if ((System.currentTimeMillis() % 5) == 0) // haveing some more fun...
142
{
143               user = ClientState.getClientState().getString("user.rname", "I'm to lame to read mIRC.hlp");
144               nick = ClientState.getClientState().getString("user.nick", "madgoat");
145            }
146            else
147            {
148               user = ClientState.getClientState().getString("user.rname", ClientUtils.tagline());
149               nick = ClientState.getClientState().getString("user.nick", "IRCFrEAK");
150            }
151
152            getCapabilities().sendln("USER " + parms[0] + " " + parms[1] + " " + parms[1] + " :" + user);
153            getCapabilities().sendln("NICK " + nick);
154
155            if (nickListener == null || ! nickListener.isArmed())
156            {
157                nickListener = new NickInUseListener();
158                getCapabilities().addTemporaryListener(nickListener);
159            }
160        }
161        else
162        {
163            boolean isDone = false;
164
165            if (data.getMyUser().getChannels().size() > 0)
166            {
167                getCapabilities().getOutputCapabilities().fireSetAll(ClientUtils.getEventHashMap(ev.data.hostname, ev.message), "IRC_DISCONNECT");
168
169                if (ClientState.getClientState().isOption("option.reconnect", ClientDefaults.option_reconnect))
170                {
171                   restoreInformation = data.getMyUser();
172                   restoreServer = ev.data.hostname;
173  
174                   getCapabilities().getOutputCapabilities().fireSetAll(ClientUtils.getEventHashMap(ev.data.hostname, ev.message), "IRC_RECONNECT");
175                   getCapabilities().getGlobalCapabilities().setTabTitle(getCapabilities(), "reconnecting");
176                   socket.connect(ev.data.hostname, ev.data.port, ClientState.getClientState().getInteger("reconnect.time", ClientDefaults.reconnect_time) * 1000, ev.data.password, ev.data.isSecure);
177
178                   isDone = true;
179                }
180            }
181            else
182            {
183                getCapabilities().getOutputCapabilities().fireSetStatus(ClientUtils.getEventHashMap(ev.data.hostname, ev.message), "IRC_DISCONNECT");
184            }
185
186            data.reset();
187            notify.reset();
188
189            if (restoreInformation != null && !isDone)
190            {
191                getCapabilities().getOutputCapabilities().fireSetStatus(ClientUtils.getEventHashMap(ev.data.hostname, ev.message), "IRC_RECONNECT");
192                getCapabilities().getGlobalCapabilities().setTabTitle(getCapabilities(), "reconnecting");
193                socket.connect(ev.data.hostname, ev.data.port, ClientState.getClientState().getInteger("reconnect.time", ClientDefaults.reconnect_time) * 1000, ev.data.password, ev.data.isSecure);
194            }
195        }
196    }
197
198    protected class NickInUseListener implements ChatListener
199    {
200        protected String JavaDoc altNick = ClientState.getClientState().getString("user.altnick", "lamer" + System.currentTimeMillis());
201        protected boolean armed = true;
202
203        public boolean isChatEvent(String JavaDoc event, HashMap data)
204        {
205           return event.equals("001") || event.equals("433");
206        }
207  
208        public boolean isArmed()
209        {
210           return armed;
211        }
212
213        public int fireChatEvent(HashMap desc)
214        {
215           String JavaDoc event = (String JavaDoc)desc.get($EVENT$);
216
217           if (event.equals("433"))
218           {
219               if (rero.test.QuickConnect.IsQuickConnect() && rero.test.QuickConnect.GetInformation().getURL().getUserInfo() != null)
220               {
221                   // set a new alternate alt nick iff a user is quick connecting and a default nickname has been specified...
222
// otherwise lamer will do just fine...
223

224                   altNick = ClientState.getClientState().getString("user.nick", rero.test.QuickConnect.GetInformation().getNickname()) + System.currentTimeMillis();
225               }
226               getCapabilities().sendln("NICK " + altNick);
227           }
228
229           armed = false;
230           return EVENT_DONE | REMOVE_LISTENER;
231        }
232   }
233 }
234
Popular Tags