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 eric@jeti.tk |
---|
21 | */ |
---|
22 | |
---|
23 | // Created on 8-sept-2004 |
---|
24 | package nu.fw.jeti.plugins.filetransfer.ibb; |
---|
25 | |
---|
26 | import java.util.HashMap; |
---|
27 | import java.util.Map; |
---|
28 | |
---|
29 | import nu.fw.jeti.backend.XExecutableExtension; |
---|
30 | import nu.fw.jeti.jabber.Backend; |
---|
31 | import nu.fw.jeti.jabber.elements.Extension; |
---|
32 | import nu.fw.jeti.jabber.elements.IQExtension; |
---|
33 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
34 | import nu.fw.jeti.jabber.elements.Packet; |
---|
35 | import nu.fw.jeti.jabber.elements.XMPPError; |
---|
36 | import nu.fw.jeti.jabber.elements.XMPPErrorTag; |
---|
37 | import nu.fw.jeti.plugins.filetransfer.GetFileWindow; |
---|
38 | import nu.fw.jeti.plugins.filetransfer.Plugin; |
---|
39 | import nu.fw.jeti.util.Log; |
---|
40 | |
---|
41 | /** |
---|
42 | * @author E.S. de Boer |
---|
43 | * |
---|
44 | */ |
---|
45 | public class IBBExtension extends Extension implements IQExtension, XExecutableExtension |
---|
46 | { |
---|
47 | private boolean close; |
---|
48 | private boolean open; |
---|
49 | private String sid; |
---|
50 | private int sequence; |
---|
51 | private int blockSize=4096; |
---|
52 | //private byte[] data; |
---|
53 | private String base64data; |
---|
54 | private static Map fileWindows = new HashMap(10); |
---|
55 | |
---|
56 | public IBBExtension(String sid,int blockSize) |
---|
57 | { |
---|
58 | open=true; |
---|
59 | this.blockSize = blockSize; |
---|
60 | this.sid=sid; |
---|
61 | } |
---|
62 | |
---|
63 | public IBBExtension(String sid) |
---|
64 | { |
---|
65 | close=true; |
---|
66 | this.sid=sid; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | public IBBExtension(String sid,int sequence, String data) |
---|
71 | { |
---|
72 | this.sid = sid; |
---|
73 | this.sequence = sequence; |
---|
74 | base64data = data; |
---|
75 | } |
---|
76 | |
---|
77 | public IBBExtension(boolean open,boolean close, String sid, int blockSize,int sequence, String data) |
---|
78 | { |
---|
79 | this.open=open; |
---|
80 | this.close = close; |
---|
81 | this.sid = sid; |
---|
82 | this.blockSize=blockSize; |
---|
83 | this.sequence = sequence; |
---|
84 | base64data = data; |
---|
85 | } |
---|
86 | |
---|
87 | public String getData() |
---|
88 | { |
---|
89 | return base64data; |
---|
90 | } |
---|
91 | |
---|
92 | public String getSid() |
---|
93 | { |
---|
94 | return sid; |
---|
95 | } |
---|
96 | |
---|
97 | public boolean isOpen() |
---|
98 | { |
---|
99 | return open; |
---|
100 | } |
---|
101 | |
---|
102 | public boolean isClose() |
---|
103 | { |
---|
104 | return close; |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | public void execute(Packet iq, Backend backend) |
---|
109 | { |
---|
110 | //System.out.println(getSid()); |
---|
111 | GetFileWindow w= (GetFileWindow)fileWindows.get(sid); |
---|
112 | if(w==null) |
---|
113 | { |
---|
114 | Log.xmlReceivedError("ibb stream from unknown"); |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | IBBReceive r= (IBBReceive)w.getStreamReceive(); |
---|
119 | r.addData(getData()); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | public void execute(InfoQuery iq, Backend backend) |
---|
124 | { |
---|
125 | if(iq.getType().equals("set")) |
---|
126 | { |
---|
127 | //Plugin.ibb(iq.getFrom(),this); |
---|
128 | |
---|
129 | if(isOpen()) |
---|
130 | { |
---|
131 | GetFileWindow w= Plugin.getGetFile(iq.getFrom(),getSid()); |
---|
132 | if(w!=null) |
---|
133 | {//file accepted by filetransfer protocol; |
---|
134 | //System.out.println(getSid()); |
---|
135 | w.startDownloading(new IBBReceive(iq.getFrom(),getSid(),backend,w)); |
---|
136 | fileWindows.put(getSid(),w); |
---|
137 | backend.send(new InfoQuery(iq.getFrom(),"result",iq.getID(),null)); |
---|
138 | Log.xmlReceivedError("ibb stream init from unknown"); |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | XMPPError e = new XMPPError("cancel",501); |
---|
143 | e.addError(new XMPPErrorTag("feature-not-implemented")); |
---|
144 | backend.send(new InfoQuery(iq.getFrom(),iq.getID(),e)); |
---|
145 | } |
---|
146 | } |
---|
147 | if(isClose()) |
---|
148 | { |
---|
149 | GetFileWindow w= (GetFileWindow) fileWindows.remove(sid); |
---|
150 | if(w!=null)((IBBReceive)w.getStreamReceive()).stopDownloading(); |
---|
151 | //System.out.println(" close " + sid); |
---|
152 | backend.send(new InfoQuery(iq.getFrom(),"result",iq.getID(),null)); |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | public void appendToXML(StringBuffer xml) |
---|
158 | { |
---|
159 | if(open) |
---|
160 | { |
---|
161 | xml.append("<open xmlns= 'http://jabber.org/protocol/ibb'"); |
---|
162 | appendAttribute(xml,"sid",sid); |
---|
163 | appendAttribute(xml,"block-size",String.valueOf(blockSize)); |
---|
164 | xml.append("/>"); |
---|
165 | } |
---|
166 | else if (close) |
---|
167 | { |
---|
168 | xml.append("<close xmlns= 'http://jabber.org/protocol/ibb'"); |
---|
169 | appendAttribute(xml,"sid",sid); |
---|
170 | xml.append("/>"); |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | xml.append("<data xmlns= 'http://jabber.org/protocol/ibb'"); |
---|
175 | appendAttribute(xml,"sid",sid); |
---|
176 | appendAttribute(xml,"seq",String.valueOf(sequence)); |
---|
177 | xml.append(">"); |
---|
178 | //addData(data,xml); |
---|
179 | xml.append(base64data); |
---|
180 | xml.append("</data>"); |
---|
181 | //TODO change to AMP protocol instead of hardcoding |
---|
182 | xml.append("<amp xmlns='http://jabber.org/protocol/amp'>"); |
---|
183 | xml.append("<rule condition='deliver-at' value='stored' action='error'/>"); |
---|
184 | xml.append("<rule condition='match-resource' value='exact' action='error'/>"); |
---|
185 | xml.append("</amp>"); |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|
189 | /* |
---|
190 | * Overrides for emacs |
---|
191 | * Local variables: |
---|
192 | * tab-width: 4 |
---|
193 | * End: |
---|
194 | */ |
---|