KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > client > dcc > FeatureDCC


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 rero.dialogs.*;
22
23 public class FeatureDCC extends Feature implements ClientCommand, ChatListener, FrameworkConstants
24 {
25    protected ChatFramework ircfw;
26    protected DataDCC dccData;
27    protected LocalInfo localInfo;
28
29    public FeatureDCC()
30    {
31       dccData = new DataDCC();
32    }
33
34    public void storeDataStructures(WeakHashMap data)
35    {
36       data.put("dcc", dccData);
37    }
38
39    public void init()
40    {
41       ircfw = getCapabilities().getChatFramework();
42       localInfo = (LocalInfo)getCapabilities().getDataStructure(DataStructures.LocalInfo);
43    
44       getCapabilities().addChatListener(this);
45       getCapabilities().registerCommand("DCC", this); // register the dcc alias to this class
46
getCapabilities().registerCommand("SEND", this); // register the dcc alias to this class
47
getCapabilities().registerCommand("CHAT", this); // register the dcc alias to this class
48
}
49  
50    public void requestChat(String JavaDoc nickname)
51    {
52       Chat protocol = new Chat(nickname);
53
54       ListenDCC connect = new ListenDCC();
55       connect.announceFramework(ircfw);
56       connect.setImplementation(protocol);
57
58       int port = connect.getListenerPort();
59               
60       if (port > -1)
61       {
62          String JavaDoc out = "PRIVMSG " + nickname + " :" + (char)1 + "DCC CHAT chat " + ClientUtils.longip(localInfo.localip()) + " " + port + "" + (char)1 + "";
63
64          getCapabilities().getOutputCapabilities().fireSetConfused(ClientUtils.getEventHashMap(nickname, "CHAT " + port), nickname, "reply", "SEND_DCC");
65
66          getCapabilities().sendln(out);
67
68          dccData.addConnection(port+"", connect);
69          connect.connect();
70       }
71       else
72       {
73          connect.getImplementation().fireError("unable to establish a port");
74          // else we had trouble listening for a dcc, fire an error set here?
75
}
76    }
77
78    public void sendFile(String JavaDoc nickname, File file)
79    {
80       Send protocol = new Send(nickname, file);
81
82       ListenDCC connect = new ListenDCC();
83       connect.announceFramework(ircfw);
84       connect.setImplementation(protocol);
85
86       int port = connect.getListenerPort();
87               
88       if (port > -1)
89       {
90          String JavaDoc fname = file.getName();
91          if (ClientState.getClientState().isOption("dcc.fillspaces", ClientDefaults.dcc_fillspaces))
92          {
93             fname = fname.replace(' ', '_');
94          }
95
96          String JavaDoc out = "PRIVMSG " + nickname + " :" + (char)1 + "DCC SEND " + fname + " " + ClientUtils.longip(localInfo.localip()) + " " + port + " " + file.length() + (char)1 + "";
97
98          getCapabilities().getOutputCapabilities().fireSetConfused(ClientUtils.getEventHashMap(nickname, "SEND " + port + " " + file.length() + " " + file.getAbsolutePath()), nickname, "reply", "SEND_DCC");
99
100          getCapabilities().sendln(out);
101
102          dccData.addConnection(port+"", connect);
103          connect.connect();
104       }
105       else
106       {
107          connect.getImplementation().fireError("unable to establish a port");
108          // else we had trouble listening for a dcc
109
}
110    }
111
112    public void runAlias(String JavaDoc command, String JavaDoc parameters)
113    {
114       command = command.toLowerCase();
115
116       if (command.equals("send") || command.equals("chat"))
117       {
118          runAlias("dcc", command + " " + parameters);
119          return;
120       }
121
122       if (!command.equals("dcc")) // eh? wtf?!? I only registered as the /dcc command. bastards
123
{
124          return;
125       }
126
127       TokenizedString temps = new TokenizedString(parameters);
128       temps.tokenize(" ");
129      
130       if (temps.getToken(0).equals("stats"))
131       {
132          getCapabilities().getUserInterface().openDCCWindow();
133       }
134       else if (temps.getToken(0).equals("chat"))
135       {
136          requestChat(temps.getToken(1));
137       }
138       else if (temps.getToken(0).equals("close"))
139       {
140           if (temps.getToken(1).charAt(0) == '=')
141           {
142              dccData.closeChat(temps.getToken(1).substring(1, temps.getToken(1).length()));
143           }
144           else if (temps.getTotalTokens() == 3)
145           {
146              int type = rero.client.functions.DCCOperators.getType(temps.getToken(1));
147              dccData.closeConnection(temps.getToken(2), type);
148           }
149           else
150           {
151              dccData.closeConnection(temps.getToken(1));
152           }
153       }
154       else if (temps.getToken(0).equals("accept"))
155       {
156          ConnectDCC temp = (ConnectDCC)dccData.getConnectionToAccept(temps.getToken(1));
157          
158          if (temps.getTotalTokens() >= 3 && temp.getImplementation() instanceof Receive)
159          {
160             ((Receive)(temp.getImplementation())).setFile(new File(temps.getToken(2)));
161          }
162
163          if (temp != null)
164             temp.connect();
165       }
166       else if (temps.getToken(0).equals("send"))
167       {
168          File sendme;
169
170          if (temps.getTotalTokens() == 2 || !(new File(temps.getToken(2))).exists())
171          {
172             sendme = DialogUtilities.showFileDialog("Send File", "Accept", null);
173          }
174          else
175          {
176             sendme = ClientUtils.getFile(temps.getToken(2));
177          }
178
179          if (sendme != null && sendme.exists())
180          {
181             sendFile(temps.getToken(1), sendme);
182          }
183       }
184    }
185     
186    public boolean isChatEvent(String JavaDoc event, HashMap eventDescription)
187    {
188       String JavaDoc parms = (String JavaDoc)eventDescription.get($PARMS$);
189
190       return event.equals("REQUEST") && parms.substring(0, 3).equals("DCC");
191    }
192
193    public int fireChatEvent(HashMap description)
194    {
195       TokenizedString temp = new TokenizedString(description.get("$parms").toString());
196       temp.tokenize(" ");
197
198       if (temp.getToken(1).equals("CHAT"))
199       {
200           int port = Integer.parseInt(temp.getToken(4));
201           String JavaDoc server = ClientUtils.longip(temp.getToken(3));
202
203           if (port <= 0 || port == 19)
204           {
205              return EVENT_DONE;
206           }
207
208           Chat protocol = new Chat( description.get($NICK$).toString() );
209
210           ConnectDCC connect = new ConnectDCC(server, port);
211           connect.announceFramework(ircfw);
212           connect.setImplementation(protocol);
213
214           dccData.addConnection(port+"", connect);
215
216           boolean checkDialog = ClientState.getClientState().getInteger("dcc.onchat", ClientDefaults.dcc_accept) == 0 && ChatRequest.showDialog(getCapabilities().getGlobalCapabilities().getFrame(), connect);
217           boolean checkAutoAccept = ClientState.getClientState().getInteger("dcc.onchat", ClientDefaults.dcc_accept) == 1;
218
219           if (checkDialog || checkAutoAccept)
220              connect.connect();
221        }
222        else if (temp.getToken(1).equals("CLOSE"))
223        {
224           dccData.closeChat(temp.getToken(2).substring(1, temp.getToken(2).length()));
225        }
226        else if (temp.getToken(1).equals("ACCEPT"))
227        {
228           ConnectDCC tempc = (ConnectDCC)dccData.getConnection(temp.getToken(3));
229
230           if (tempc != null)
231           {
232              getCapabilities().getOutputCapabilities().fireSetStatus(description, "RESUME_SUCCEEDED");
233              tempc.connect();
234           }
235           else
236           {
237              getCapabilities().getOutputCapabilities().fireSetStatus(description, "RESUME_FAILED");
238           }
239        }
240        else if (temp.getToken(1).equals("RESUME"))
241        {
242           // DCC RESUME "" 4099 595468
243
GenericDCC tempc = dccData.getConnection(temp.getToken(3));
244         
245           if (tempc != null && tempc.getImplementation().getTypeOfDCC() == ProtocolDCC.DCC_SEND)
246           {
247              Send send = (Send)tempc.getImplementation();
248
249              if (send.resume(Long.parseLong(temp.getToken(4))))
250              {
251                  getCapabilities().getOutputCapabilities().fireSetStatus(description, "RESUME_REQUEST");
252
253                  String JavaDoc output = "PRIVMSG " + description.get($NICK$) + " :" + (char)1 + "DCC ACCEPT " + temp.getTokenFrom(2) + (char)1;
254
255                  getCapabilities().sendln(output);
256
257                  tempc.connect();
258              }
259              else
260              {
261                  getCapabilities().getOutputCapabilities().fireSetStatus(description, "RESUME_REQUEST_ERROR");
262              }
263           }
264           else
265           {
266              // requested resume failed... eh
267
}
268        }
269        else if (temp.getToken(1).equals("SEND"))
270        {
271           // 0 1 2 3 4 5
272
// DCC SEND ror.r02 3232235801 41005 2862333
273

274           int offset = temp.getTotalTokens() - 6;
275
276           int port = Integer.parseInt(temp.getToken(4 + offset));
277           String JavaDoc server = ClientUtils.longip(temp.getToken(3 + offset));
278
279           if (port < 1024)
280           {
281              return EVENT_DONE;
282           }
283
284           String JavaDoc fstring = temp.getToken(2);
285      
286           if (offset > 0)
287             fstring = temp.getTokenRange(2, 2 + offset);
288
289           fstring = (new File(fstring)).getName(); // strip off any path information
290

291           File output = new File(ClientState.getClientState().getString("dcc.saveto", ClientDefaults.dcc_saveto), fstring);
292
293           Receive protocol = new Receive( description.get($NICK$).toString(), output, Long.parseLong(temp.getToken(5 + offset)));
294
295           ConnectDCC connect = new ConnectDCC(server, port);
296           connect.announceFramework(ircfw);
297           connect.setImplementation(protocol);
298
299           dccData.addConnection(port+"", connect);
300
301           boolean checkDialog = ClientState.getClientState().getInteger("dcc.onsend", ClientDefaults.dcc_accept) == 0 && SendRequest.showDialog(getCapabilities().getGlobalCapabilities().getFrame(), connect);
302           boolean checkAutoAccept = ClientState.getClientState().getInteger("dcc.onsend", ClientDefaults.dcc_accept) == 1;
303
304           if (checkDialog || checkAutoAccept)
305           {
306               handleReceive(protocol, connect, description, temp);
307           }
308        }
309
310        return EVENT_DONE;
311    }
312
313    private void handleReceive(Receive protocol, ConnectDCC connect, HashMap description, TokenizedString temp)
314    {
315        File output = protocol.getFile();
316
317        // check if a resume is necessary //
318
if (output.exists())
319        {
320           int resumeOption = DCCUtilities.DetermineResumeOption(getCapabilities(), connect);
321
322           switch (resumeOption)
323           {
324              case DCCUtilities.RESUME_OPTION_SELECTED:
325                if (output.length() < protocol.getExpectedSize())
326                {
327                   protocol.pleaseResume(); // tells the dcc object that we want a resume so it sets up all the offsets and such.
328

329                   String JavaDoc outputz = "PRIVMSG " + description.get($NICK$) + " :" + ((char)1) + "DCC RESUME " + temp.getToken(2) + " " + temp.getToken(4) + " " + output.length() + ((char)1);
330
331                   getCapabilities().sendln(outputz);
332                   getCapabilities().getOutputCapabilities().fireSetStatus(description, "SEND_RESUME_REQUEST");
333                }
334                else
335                {
336                   protocol.fireError("file already completed, no need to resume");
337                }
338                break;
339              case DCCUtilities.RENAME_OPTION_SELECTED:
340                boolean option = SendRequest.showDialog(getCapabilities().getGlobalCapabilities().getFrame(), connect);
341
342                if (option)
343                {
344                   handleReceive(protocol, connect, description, temp);
345                }
346                break;
347
348              case DCCUtilities.OVERWRITE_OPTION_SELECTED:
349                connect.connect();
350                break;
351           }
352        }
353        else
354        {
355           connect.connect();
356        }
357    }
358 }
359
Popular Tags