1 | /* |
---|
2 | * Jeti, a Java Jabber client, Copyright (C) 2004 E.S. de Boer |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | * |
---|
18 | * For questions, comments etc, |
---|
19 | * use the website at http://jeti.jabberstudio.org |
---|
20 | * or mail me at jeti@jabber.org |
---|
21 | */ |
---|
22 | |
---|
23 | package nu.fw.jeti.plugins.filetransfer; |
---|
24 | |
---|
25 | import java.awt.datatransfer.DataFlavor; |
---|
26 | import java.awt.datatransfer.Transferable; |
---|
27 | import java.awt.datatransfer.UnsupportedFlavorException; |
---|
28 | import java.io.File; |
---|
29 | import java.io.IOException; |
---|
30 | import java.net.Inet4Address; |
---|
31 | import java.net.InetAddress; |
---|
32 | import java.net.NetworkInterface; |
---|
33 | import java.net.SocketException; |
---|
34 | import java.security.MessageDigest; |
---|
35 | import java.security.NoSuchAlgorithmException; |
---|
36 | import java.text.NumberFormat; |
---|
37 | import java.util.*; |
---|
38 | |
---|
39 | import javax.swing.JComponent; |
---|
40 | import javax.swing.JFileChooser; |
---|
41 | import javax.swing.JTree; |
---|
42 | import javax.swing.TransferHandler; |
---|
43 | |
---|
44 | import nu.fw.jeti.backend.roster.PrimaryJIDStatus; |
---|
45 | import nu.fw.jeti.jabber.Backend; |
---|
46 | import nu.fw.jeti.jabber.JID; |
---|
47 | import nu.fw.jeti.jabber.JIDStatus; |
---|
48 | import nu.fw.jeti.jabber.elements.Extension; |
---|
49 | import nu.fw.jeti.jabber.elements.XData; |
---|
50 | import nu.fw.jeti.jabber.handlers.ExtensionHandler; |
---|
51 | import nu.fw.jeti.plugins.Plugins; |
---|
52 | import nu.fw.jeti.plugins.RosterMenuListener; |
---|
53 | import nu.fw.jeti.plugins.filetransfer.ibb.IBBHandler; |
---|
54 | import nu.fw.jeti.plugins.filetransfer.socks5.Socks5Handler; |
---|
55 | import nu.fw.jeti.plugins.filetransfer.socks5.Socks5Send; |
---|
56 | import nu.fw.jeti.util.I18N; |
---|
57 | import nu.fw.jeti.util.Popups; |
---|
58 | import nu.fw.jeti.util.Preferences; |
---|
59 | import nu.fw.jeti.util.Utils; |
---|
60 | |
---|
61 | import org.xml.sax.Attributes; |
---|
62 | |
---|
63 | /** |
---|
64 | * @author E.S. de Boer |
---|
65 | |
---|
66 | */ |
---|
67 | //Created on 20-okt-2004 |
---|
68 | public class Plugin implements Plugins |
---|
69 | { |
---|
70 | private static Backend backend; |
---|
71 | private static Plugin plugin; |
---|
72 | private static JFileChooser fileChooser = new JFileChooser(); |
---|
73 | private Map fileWindows = new HashMap(10); |
---|
74 | public final static String VERSION = "0.1"; |
---|
75 | public final static String DESCRIPTION = "filetransfer.File_Transfer"; |
---|
76 | public final static String MIN_JETI_VERSION = "0.6.1"; |
---|
77 | public final static String NAME = "filetransfer"; |
---|
78 | public final static String ABOUT = "by E.S. de Boer"; |
---|
79 | |
---|
80 | public static void init(final Backend backend) |
---|
81 | { |
---|
82 | backend.getMain().addToOnlineRosterMenu(I18N.gettext("filetransfer.Transfer_File")+ "...",new RosterMenuListener() |
---|
83 | { |
---|
84 | public void actionPerformed(JIDStatus jidStatus,nu.fw.jeti.backend.roster.JIDStatusGroup group) |
---|
85 | { |
---|
86 | if(Socks5Send.isProxyAvailable() || |
---|
87 | Preferences.getBoolean("filetransfer", "useLocalIP", true)) |
---|
88 | { |
---|
89 | new SendFileWindow(backend,jidStatus.getCompleteJID()).setVisible(true); |
---|
90 | } |
---|
91 | else |
---|
92 | { |
---|
93 | Popups.errorPopup(I18N.gettext("filetransfer.File_Transfer_not_available,_because_no_filetransfer_proxy_is_available"),I18N.gettext("filetransfer.File_Transfer")); |
---|
94 | } |
---|
95 | } |
---|
96 | }); |
---|
97 | |
---|
98 | plugin = new Plugin(); |
---|
99 | Plugin.backend = backend; |
---|
100 | backend.addExtensionHandler("http://jabber.org/protocol/si",new IQSiHandler()); |
---|
101 | backend.addExtensionHandler("http://jabber.org/protocol/si/profile/file-transfer",new XSiFileTransferHandler()); |
---|
102 | backend.addCapability("si", "http://jabber.org/protocol/bytestreams"); |
---|
103 | backend.addCapability("si", "http://jabber.org/protocol/ibb"); |
---|
104 | backend.addCapability("si", "http://jabber.org/protocol/si"); |
---|
105 | backend.addCapability("si", "http://jabber.org/protocol/si/profile/file-transfer"); |
---|
106 | backend.addExtensionHandler("http://jabber.org/protocol/feature-neg",new ExtensionHandler() |
---|
107 | { |
---|
108 | private XData data; |
---|
109 | |
---|
110 | public void startHandling(Attributes attr) |
---|
111 | { |
---|
112 | data=null; |
---|
113 | } |
---|
114 | |
---|
115 | public void addExtension(Extension extension) |
---|
116 | { |
---|
117 | if(extension instanceof XData) data = (XData)extension; |
---|
118 | } |
---|
119 | |
---|
120 | public Extension build() throws InstantiationException |
---|
121 | { |
---|
122 | return data; |
---|
123 | } |
---|
124 | }); |
---|
125 | backend.addExtensionHandler("http://jabber.org/protocol/ibb",new IBBHandler()); |
---|
126 | backend.addExtensionHandler("http://jabber.org/protocol/bytestreams",new Socks5Handler()); |
---|
127 | Socks5Send.initProxies(backend); |
---|
128 | backend.getMain().getOnlinePanel().setTransferHandler(new FileTransferhandler()); |
---|
129 | } |
---|
130 | |
---|
131 | protected static Backend getBackend() |
---|
132 | { |
---|
133 | return backend; |
---|
134 | } |
---|
135 | |
---|
136 | public static void addGetFile(GetFileWindow w, JID jid,String id) |
---|
137 | { |
---|
138 | //FIXME add sid to getfile to know the precise file |
---|
139 | plugin.fileWindows.put(jid,w); |
---|
140 | //plugin.fileWindows.put(new JIDSID(jid,id),w); |
---|
141 | } |
---|
142 | |
---|
143 | public static GetFileWindow getGetFile(JID jid,String sid) |
---|
144 | { |
---|
145 | //FIXME add sid to getfile to know the precise file |
---|
146 | return (GetFileWindow)plugin.fileWindows.remove(jid); |
---|
147 | //return (GetFileWindow)plugin.fileWindows.remove(new JIDSID(jid,sid)); |
---|
148 | } |
---|
149 | |
---|
150 | public static void unload(Backend backend) |
---|
151 | { |
---|
152 | backend.getMain().removeFromRosterMenu(I18N.gettext("filetransfer.Transfer_File")+ "..."); |
---|
153 | //backend.getMain().removeFromMenu(plugin.menuItem); |
---|
154 | backend.removeExtensionHandler("http://jabber.org/protocol/ibb"); |
---|
155 | backend.removeExtensionHandler("http://jabber.org/protocol/si"); |
---|
156 | backend.removeExtensionHandler("http://jabber.org/protocol/si/profile/file-transfer"); |
---|
157 | backend.removeExtensionHandler("http://jabber.org/protocol/feature-neg"); |
---|
158 | backend.removeCapability("si", "http://jabber.org/protocol/bytestreams"); |
---|
159 | backend.removeCapability("si", "http://jabber.org/protocol/ibb"); |
---|
160 | backend.removeCapability("si", "http://jabber.org/protocol/si"); |
---|
161 | backend.removeCapability("si", "http://jabber.org/protocol/si/profile/file-transfer"); |
---|
162 | |
---|
163 | plugin = null; |
---|
164 | } |
---|
165 | |
---|
166 | public void unload() {} |
---|
167 | |
---|
168 | public static String sha(String sid,JID initiator,JID target) |
---|
169 | { |
---|
170 | //System.out.println(sid.toString()); |
---|
171 | //System.out.println(initiator.toString()); |
---|
172 | //System.out.println(target.toString()); |
---|
173 | |
---|
174 | MessageDigest sha=null; |
---|
175 | try |
---|
176 | { |
---|
177 | sha = MessageDigest.getInstance("SHA"); |
---|
178 | } catch (NoSuchAlgorithmException e) |
---|
179 | { |
---|
180 | // TODO Auto-generated catch block |
---|
181 | e.printStackTrace(); |
---|
182 | } |
---|
183 | sha.update(sid.getBytes()); |
---|
184 | sha.update(stringPrep(initiator).getBytes()); |
---|
185 | return Utils.toString(sha.digest(stringPrep(target).getBytes())); |
---|
186 | } |
---|
187 | |
---|
188 | private static String stringPrep(JID jid) |
---|
189 | {//TODO make real stringprep |
---|
190 | StringBuffer j = new StringBuffer(); |
---|
191 | if (jid.getUser() != null){ |
---|
192 | j.append(jid.getUser().toLowerCase()); |
---|
193 | j.append("@"); |
---|
194 | } |
---|
195 | j.append(jid.getDomain().toLowerCase()); |
---|
196 | if (jid.getResource() != null){ |
---|
197 | j.append("/"); |
---|
198 | j.append(jid.getResource()); |
---|
199 | } |
---|
200 | return j.toString(); |
---|
201 | } |
---|
202 | |
---|
203 | public static String getIP() |
---|
204 | { |
---|
205 | Enumeration interfaceEnum; |
---|
206 | try |
---|
207 | { |
---|
208 | interfaceEnum = NetworkInterface.getNetworkInterfaces(); |
---|
209 | while (interfaceEnum.hasMoreElements()) |
---|
210 | { |
---|
211 | NetworkInterface n = (NetworkInterface) |
---|
212 | interfaceEnum.nextElement(); |
---|
213 | //System.out.println(n.getDisplayName() + ":"); |
---|
214 | Enumeration ipEnum = n.getInetAddresses(); |
---|
215 | while (ipEnum.hasMoreElements()) |
---|
216 | { |
---|
217 | InetAddress ip = (InetAddress) ipEnum.nextElement(); |
---|
218 | if(ip instanceof Inet4Address) |
---|
219 | { |
---|
220 | if (!ip.isLoopbackAddress()) |
---|
221 | { |
---|
222 | return ip.getHostAddress(); |
---|
223 | } |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | } catch (SocketException e) |
---|
228 | { |
---|
229 | e.printStackTrace(); |
---|
230 | } |
---|
231 | |
---|
232 | return null; |
---|
233 | } |
---|
234 | |
---|
235 | protected static JFileChooser getFileChooser() { |
---|
236 | return fileChooser; |
---|
237 | } |
---|
238 | |
---|
239 | public static String getSizeText(long size) |
---|
240 | { |
---|
241 | if(size<1024) return size + " bytes"; |
---|
242 | double s; |
---|
243 | NumberFormat n = NumberFormat.getNumberInstance(); |
---|
244 | n.setMaximumFractionDigits(2); |
---|
245 | s = size/1024d; |
---|
246 | if(s<1024) return n.format(s) + " kB"; |
---|
247 | s = s/1024d; |
---|
248 | if(s<1024) return n.format(s) + " MB"; |
---|
249 | s = s/1024d; |
---|
250 | return n.format(s) + " GB"; |
---|
251 | } |
---|
252 | |
---|
253 | static private class FileTransferhandler extends TransferHandler { |
---|
254 | |
---|
255 | public boolean canImport(JComponent comp, DataFlavor[] flavors) { |
---|
256 | for (int i = 0; i < flavors.length; i++) { |
---|
257 | if (flavors[i].equals(DataFlavor.javaFileListFlavor)) { |
---|
258 | return true; |
---|
259 | } |
---|
260 | } |
---|
261 | return false; |
---|
262 | } |
---|
263 | |
---|
264 | public boolean importData(JComponent comp, Transferable t) { |
---|
265 | Object o = ((JTree)comp).getLastSelectedPathComponent(); |
---|
266 | JID to; |
---|
267 | |
---|
268 | if(!Socks5Send.isProxyAvailable() && |
---|
269 | !Preferences.getBoolean("filetransfer", "useLocalIP", true)) |
---|
270 | { |
---|
271 | Popups.errorPopup(I18N.gettext("filetransfer.File_Transfer_not_available,_because_no_filetransfer_proxy_is_available"),I18N.gettext("filetransfer.File_Transfer")); |
---|
272 | return false; |
---|
273 | } |
---|
274 | |
---|
275 | if (o instanceof JIDStatus) { |
---|
276 | to = ((JIDStatus)o).getCompleteJID(); |
---|
277 | } else if (o instanceof PrimaryJIDStatus) { |
---|
278 | PrimaryJIDStatus p = (PrimaryJIDStatus)o; |
---|
279 | to = p.getJIDPrimaryStatus().getCompleteJID(); |
---|
280 | } else { |
---|
281 | return false; |
---|
282 | } |
---|
283 | |
---|
284 | if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { |
---|
285 | return false; |
---|
286 | } |
---|
287 | try { |
---|
288 | Collection files = (Collection) t.getTransferData( |
---|
289 | DataFlavor.javaFileListFlavor); |
---|
290 | |
---|
291 | if (files == null || files.size() == 0) { |
---|
292 | return false; |
---|
293 | } |
---|
294 | |
---|
295 | // Check that everything is regular files |
---|
296 | for (Iterator i = files.iterator(); i.hasNext(); ) { |
---|
297 | File file = (File) i.next(); |
---|
298 | if (!file.isFile()) { |
---|
299 | Popups.errorPopup( |
---|
300 | I18N.gettext("filetransfer.Can_not_transfer_directories"), |
---|
301 | I18N.gettext("main.error.Error")); |
---|
302 | return false; |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | for (Iterator i = files.iterator(); i.hasNext(); ) { |
---|
307 | File file = (File) i.next(); |
---|
308 | new SendFileWindow(backend, to, file).setVisible(true); |
---|
309 | } |
---|
310 | |
---|
311 | return true; |
---|
312 | } catch (UnsupportedFlavorException e) { |
---|
313 | System.out.println("unsupported data"); |
---|
314 | } catch (IOException e) { |
---|
315 | System.out.println("IOException"); |
---|
316 | } |
---|
317 | return false; |
---|
318 | } |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | /* |
---|
323 | * Overrides for emacs |
---|
324 | * Local variables: |
---|
325 | * tab-width: 4 |
---|
326 | * End: |
---|
327 | */ |
---|