[3102] | 1 | /* |
---|
| 2 | * Jeti, a Java Jabber client, Copyright (C) 2003 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/im me at jeti@jabber.org |
---|
| 21 | */ |
---|
| 22 | package nu.fw.jeti.jabber; |
---|
| 23 | |
---|
| 24 | import java.awt.Container; |
---|
| 25 | import java.awt.Frame; |
---|
| 26 | import java.awt.Window; |
---|
[3451] | 27 | import java.net.InetAddress; |
---|
[3102] | 28 | import java.util.*; |
---|
| 29 | |
---|
| 30 | import javax.swing.JFrame; |
---|
| 31 | |
---|
| 32 | import nu.fw.jeti.backend.*; |
---|
| 33 | import nu.fw.jeti.backend.roster.Roster; |
---|
| 34 | import nu.fw.jeti.backend.roster.Server; |
---|
| 35 | import nu.fw.jeti.events.*; |
---|
| 36 | import nu.fw.jeti.jabber.elements.IQXCaps; |
---|
| 37 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
| 38 | import nu.fw.jeti.jabber.elements.Message; |
---|
| 39 | import nu.fw.jeti.jabber.elements.Packet; |
---|
| 40 | import nu.fw.jeti.jabber.elements.StreamError; |
---|
| 41 | import nu.fw.jeti.jabber.handlers.ExtensionHandler; |
---|
| 42 | import nu.fw.jeti.ui.Jeti; |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * @author E.S. de Boer |
---|
| 46 | */ |
---|
| 47 | |
---|
| 48 | public class Backend |
---|
| 49 | { |
---|
| 50 | private Map eventListeners; |
---|
| 51 | private Map presenceListeners; |
---|
| 52 | private Map messageListeners; |
---|
| 53 | private int identifier; |
---|
| 54 | private Connect connect; |
---|
| 55 | private Roster roster; |
---|
| 56 | private Server server; |
---|
| 57 | private Jeti main; |
---|
| 58 | private JFrame mainFrame; |
---|
| 59 | private Container mainWindow; |
---|
| 60 | private Start start; |
---|
| 61 | private IQTimerQueue iqTimerQueue; |
---|
| 62 | private Handlers handlers; |
---|
| 63 | private CapabilitiesCache capabilitiesCache; |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | public Backend(Start start) |
---|
| 67 | { |
---|
| 68 | eventListeners = new HashMap(10); |
---|
| 69 | iqTimerQueue = new IQTimerQueue(); |
---|
| 70 | handlers = new Handlers(); |
---|
| 71 | connect = new Connect(this,iqTimerQueue,handlers); |
---|
| 72 | server =new Server(this); |
---|
| 73 | roster = new Roster(this,server); |
---|
| 74 | capabilitiesCache = new CapabilitiesCache(this); |
---|
| 75 | this.start = start; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | public void setMain(Jeti main,Window window, JFrame frame) |
---|
| 79 | { |
---|
| 80 | this.main = main; |
---|
| 81 | this.mainWindow = window; |
---|
| 82 | this.mainFrame = frame; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public Container getMainWindow() |
---|
| 86 | {//TODO bit of a hack, think of something nicer |
---|
| 87 | if( mainWindow == null ) |
---|
| 88 | { |
---|
| 89 | mainWindow=main.getTopLevelAncestor(); |
---|
| 90 | } |
---|
| 91 | return mainWindow; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | public JFrame getMainFrame() |
---|
| 95 | { |
---|
| 96 | return mainFrame; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | public Jeti getMain(){return main;} |
---|
| 100 | /* |
---|
| 101 | public void setBackend(JabberBackend backend) |
---|
| 102 | { |
---|
| 103 | this.backend = backend; |
---|
| 104 | } |
---|
| 105 | */ |
---|
| 106 | public void login(LoginInfo loginInfo) |
---|
| 107 | { |
---|
| 108 | connect.login(loginInfo); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | public void autoLogin(LoginInfo loginInfo,int tries) |
---|
| 112 | { |
---|
| 113 | connect.autoLogin(loginInfo,tries); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | public void abortLogin() |
---|
| 117 | { |
---|
| 118 | connect.abort(); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | public JID getMyJID() |
---|
| 122 | { |
---|
| 123 | return Connect.getMyJID(); |
---|
| 124 | } |
---|
[3451] | 125 | |
---|
| 126 | /* |
---|
| 127 | * Este método recupera a inteface de rede local, usada para transferir arquivo. |
---|
| 128 | * Previne erro no caso do cliente ter múltiplas interface de rede. |
---|
| 129 | * |
---|
| 130 | * emmanuel.ferro@serpro.gov.br em 08/10/2010 |
---|
| 131 | * |
---|
| 132 | */ |
---|
| 133 | public InetAddress getLocalIP(){ |
---|
| 134 | return connect.getSocket().getLocalAddress(); |
---|
| 135 | } |
---|
| 136 | |
---|
[3102] | 137 | /** |
---|
| 138 | * Checks if the password matches the password used to login on this account |
---|
| 139 | * @param password the password to check |
---|
| 140 | * @return true if the password matches the password of this account |
---|
| 141 | */ |
---|
| 142 | public boolean isPasswordValid(String password) |
---|
| 143 | { |
---|
| 144 | return connect.isPasswordValid(password); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | public synchronized String getIdentifier() |
---|
| 148 | { |
---|
| 149 | return "JETI_" + identifier++; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | public String createThread() |
---|
| 153 | { |
---|
| 154 | return Integer.toHexString("JETI".hashCode()) + Long.toHexString(System.currentTimeMillis()); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | public String getAccountInfo() |
---|
| 158 | { |
---|
| 159 | return connect.getAccountInfo(); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | public Map getAvailableTransports() |
---|
| 163 | { |
---|
| 164 | return server.getAvailableTransports(); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | public void changeStatus(int show,String status) |
---|
| 168 | { |
---|
| 169 | connect.changeStatus(show, status); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | public void rosterLoaded() |
---|
| 173 | { |
---|
| 174 | connect.connected(); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | public void disconnect() |
---|
| 178 | { |
---|
| 179 | connect.disconnect(); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | public boolean isLoggedIn() |
---|
| 183 | { |
---|
| 184 | return connect.isLoggedIn(); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | public void streamError(StreamError error) |
---|
| 188 | { |
---|
| 189 | connect.streamError(error); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | public void exit() |
---|
| 193 | { |
---|
| 194 | if(!Start.applet) |
---|
| 195 | { |
---|
| 196 | System.out.println("This window will close in one minute"); |
---|
| 197 | Timer t = new Timer(true); |
---|
| 198 | t.schedule(new TimerTask() |
---|
| 199 | { |
---|
| 200 | public void run() |
---|
| 201 | { |
---|
| 202 | System.exit(0); |
---|
| 203 | } |
---|
| 204 | },10000); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | //end connection |
---|
| 208 | connect.exit(); |
---|
| 209 | |
---|
| 210 | Frame[] frames = Frame.getFrames(); |
---|
| 211 | for(int i =0;i<frames.length;i++) |
---|
| 212 | {//remove all opened jeti frames |
---|
| 213 | String name = frames[i].getClass().getName(); |
---|
| 214 | System.out.println(name); |
---|
| 215 | if(name.startsWith("nu.fw.jeti"))frames[i].dispose(); |
---|
| 216 | //frames[i].dispose(); |
---|
| 217 | if(frames[i].isDisplayable())System.out.println(name); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | capabilitiesCache.save(); |
---|
| 221 | //unload plugins |
---|
| 222 | start.exit(); |
---|
| 223 | //remove references' |
---|
| 224 | // start=null; |
---|
| 225 | // connect=null; |
---|
| 226 | // roster=null; |
---|
| 227 | // server=null; |
---|
| 228 | // main=null; |
---|
| 229 | // mainFrame=null; |
---|
| 230 | // mainWindow =null; |
---|
| 231 | synchronized (this) |
---|
| 232 | { |
---|
| 233 | eventListeners.clear(); |
---|
| 234 | if(presenceListeners!=null)presenceListeners.clear(); |
---|
| 235 | if(messageListeners!=null)messageListeners.clear(); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * send packets |
---|
| 241 | * please send messages that need to be logged with sendMessage |
---|
| 242 | * @param packet |
---|
| 243 | */ |
---|
| 244 | public void send(Packet packet) |
---|
| 245 | { |
---|
| 246 | connect.send(packet); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | * Sends a infoquery and gives the result to listener |
---|
| 251 | * @param query the infoquery to send |
---|
| 252 | * @param listener the IQResultListener wich listens for the result |
---|
| 253 | * @param timeout not yet implemented |
---|
| 254 | */ |
---|
| 255 | public void send(InfoQuery query, IQResultListener listener,int timeout) |
---|
| 256 | { |
---|
| 257 | iqTimerQueue.add(query,listener,timeout); |
---|
| 258 | send(query); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /** |
---|
| 262 | * needed to log own send messages |
---|
| 263 | * @param message |
---|
| 264 | */ |
---|
| 265 | public void sendMessage(Message message) |
---|
| 266 | { |
---|
| 267 | //if(isr |
---|
| 268 | for(Iterator j = getListeners(OwnMessageListener.class);j.hasNext();) |
---|
| 269 | { |
---|
| 270 | ((OwnMessageListener)j.next()).sendMessage(message); |
---|
| 271 | } |
---|
| 272 | connect.send(message); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | // public void browse(JID jid, BrowseListener listener) |
---|
| 276 | // { |
---|
| 277 | // connect.browse(jid,listener); |
---|
| 278 | // } |
---|
| 279 | |
---|
| 280 | public void getItems(JID jid, DiscoveryListener listener, boolean useCache) |
---|
| 281 | { |
---|
| 282 | connect.getItems(jid,listener,useCache); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | public void getItems(JID jid, DiscoveryListener listener) |
---|
| 286 | { |
---|
| 287 | connect.getItems(jid,listener); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | public void getInfo(JID jid, DiscoveryListener listener) |
---|
| 291 | { |
---|
| 292 | connect.getInfo(jid,listener); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | public void getItems(JID jid,String node, DiscoveryListener listener) |
---|
| 296 | { |
---|
| 297 | connect.getItems(jid,node,listener); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | public void getInfo(JID jid,String node, DiscoveryListener listener) |
---|
| 301 | { |
---|
| 302 | connect.getInfo(jid,node,listener); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | /** |
---|
| 306 | * gets the current Jabber status of the user |
---|
| 307 | * @return the current status |
---|
| 308 | */ |
---|
| 309 | public int getStatus() |
---|
| 310 | { |
---|
| 311 | return connect.getStatus(); |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | |
---|
| 315 | // //muc hack |
---|
| 316 | // public void browseNotCached(JID jid, BrowseListener listener) |
---|
| 317 | // { |
---|
| 318 | // connect.browseNotCached(jid,listener); |
---|
| 319 | // } |
---|
| 320 | |
---|
| 321 | public static JIDStatus getJIDStatus(JID jid) |
---|
| 322 | { |
---|
| 323 | return Roster.getJIDStatus(jid); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | public String[] getAllGroups() |
---|
| 327 | { |
---|
| 328 | return roster.getAllGroups(); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /** |
---|
| 332 | * makes a new account |
---|
| 333 | * (the current connection will be logged off) |
---|
| 334 | * @param name Username (may be null) |
---|
| 335 | * @param password User password (may be null) |
---|
| 336 | */ |
---|
| 337 | public void newAccount(String server,int port,String name,String password) |
---|
| 338 | { |
---|
| 339 | disconnect(); |
---|
| 340 | new NewAccount(server,port,handlers,name,password); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | public void setCapability(JID jid, IQXCaps caps){ |
---|
| 344 | capabilitiesCache.setCapability(jid, caps); |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | /** |
---|
| 348 | * Has JID jid capability capability |
---|
| 349 | * @param jid |
---|
| 350 | * @param capability |
---|
| 351 | * @return true if the jid has the capability |
---|
| 352 | */ |
---|
| 353 | public boolean hasCapability(JID jid, String capability){ |
---|
| 354 | return capabilitiesCache.hasCapability(jid, capability); |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | public void addCapability(String capability,String feature){ |
---|
| 358 | connect.addCapability(capability, feature); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | public void removeCapability(String capability,String feature){ |
---|
| 362 | connect.removeCapability(capability, feature); |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | |
---|
| 366 | |
---|
| 367 | public void addExtensionHandler(String namespace,ExtensionHandler handler) |
---|
| 368 | { |
---|
| 369 | handlers.addExtensionHandler(namespace,handler); |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | public void removeExtensionHandler(String namespace) |
---|
| 373 | { |
---|
| 374 | handlers.removeExtensionHandler(namespace); |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | /** |
---|
| 378 | * Add a listener for a specific presence |
---|
| 379 | * @param jid The JID of which the presence is to be monitored (resource is not considered) |
---|
| 380 | * @param listener The class that is interested in presence events |
---|
| 381 | */ |
---|
| 382 | public synchronized void addPresenceListener(JID jid, PresenceListener listener) |
---|
| 383 | { |
---|
| 384 | if(presenceListeners == null)presenceListeners = new HashMap(); |
---|
| 385 | presenceListeners.put(jid,listener); |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | /** |
---|
| 389 | * removes a presencelistener |
---|
| 390 | * @param jid The JID of which presence is monitored |
---|
| 391 | */ |
---|
| 392 | public synchronized void removePresenceListener(JID jid) |
---|
| 393 | { |
---|
| 394 | if(presenceListeners==null)return; |
---|
| 395 | presenceListeners.remove(jid); |
---|
| 396 | if (presenceListeners.isEmpty()) presenceListeners = null; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | /** |
---|
| 400 | * Gets the presenceListener which is monitoring this JID |
---|
| 401 | * @param jid The JID of which the presenceListener is requested |
---|
| 402 | * @return PresenceListener |
---|
| 403 | */ |
---|
| 404 | public synchronized PresenceListener getPresenceListener(JID jid) |
---|
| 405 | { |
---|
| 406 | if(presenceListeners == null) return null; |
---|
| 407 | return (PresenceListener)presenceListeners.get(jid); |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | /** |
---|
| 411 | * Add a listener for messages to this JID |
---|
| 412 | * @param jid The JID of which the message is to be monitored (resource is not considered) |
---|
| 413 | * @param listener The class that is interested in message events |
---|
| 414 | */ |
---|
| 415 | public synchronized void addMessageListener(JID jid, MessageListener listener) |
---|
| 416 | { |
---|
| 417 | if(messageListeners == null)messageListeners = new HashMap(); |
---|
| 418 | messageListeners.put(jid,listener); |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | /** |
---|
| 422 | * removes a messagelistener |
---|
| 423 | * @param jid The JID of which messages are monitored |
---|
| 424 | */ |
---|
| 425 | public synchronized void removeMessageListener(JID jid) |
---|
| 426 | { |
---|
| 427 | if(messageListeners==null)return; |
---|
| 428 | messageListeners.remove(jid); |
---|
| 429 | if (messageListeners.isEmpty()) messageListeners = null; |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | /** |
---|
| 433 | * Gets the MessageListener which is monitoring this JID |
---|
| 434 | * @param jid The JID of which the messageListener is requested |
---|
| 435 | * @return MessageListener |
---|
| 436 | */ |
---|
| 437 | public synchronized MessageListener getMessageListener(JID jid) |
---|
| 438 | { |
---|
| 439 | if(messageListeners == null) return null; |
---|
| 440 | return (MessageListener)messageListeners.get(jid); |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | public synchronized void addListener(Class type, JETIListener listener) |
---|
| 444 | {//class meegeven? |
---|
| 445 | LinkedList list = (LinkedList) eventListeners.get(type.getName()); |
---|
| 446 | if(list == null) |
---|
| 447 | { |
---|
| 448 | list = new LinkedList(); |
---|
| 449 | eventListeners.put(type.getName(),list); |
---|
| 450 | } |
---|
| 451 | list.add(listener); |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | public synchronized void removeListener(Class type, JETIListener listener) |
---|
| 455 | { |
---|
| 456 | LinkedList list = (LinkedList) eventListeners.get(type.getName()); |
---|
| 457 | if(list == null) return; |
---|
| 458 | list.remove(listener); |
---|
| 459 | if(list.isEmpty()) eventListeners.remove(type.getName());//good idea? |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | public synchronized Iterator getListeners(final Class type) |
---|
| 463 | { |
---|
| 464 | LinkedList listeners = (LinkedList)eventListeners.get(type.getName()); |
---|
| 465 | if(listeners == null) |
---|
| 466 | {//no listeners so return a empty iterator, if happens use isRegisteredListener(class type) |
---|
| 467 | return new Iterator() |
---|
| 468 | {//empty iterator |
---|
| 469 | public Object next() |
---|
| 470 | { |
---|
| 471 | throw new NoSuchElementException(); |
---|
| 472 | } |
---|
| 473 | public boolean hasNext() |
---|
| 474 | { |
---|
| 475 | //System.out.println(type + " listener not registerd"); |
---|
| 476 | //throw new RuntimeException(); |
---|
| 477 | return false; |
---|
| 478 | } |
---|
| 479 | public void remove() |
---|
| 480 | { |
---|
| 481 | throw new UnsupportedOperationException(); |
---|
| 482 | } |
---|
| 483 | }; |
---|
| 484 | } |
---|
| 485 | return new LinkedList(listeners).iterator();//no concurrentmodexcep |
---|
| 486 | } |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | /* |
---|
| 490 | * Overrides for emacs |
---|
| 491 | * Local variables: |
---|
| 492 | * tab-width: 4 |
---|
| 493 | * End: |
---|
| 494 | */ |
---|