gloox  1.0
uniquemucroom.cpp
00001 /*
00002   Copyright (c) 2007-2009 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 
00015 #include "uniquemucroom.h"
00016 #include "clientbase.h"
00017 #include "jid.h"
00018 #include "sha.h"
00019 
00020 namespace gloox
00021 {
00022 
00023   // ---- UniqueMUCRoom::Unique ----
00024   UniqueMUCRoom::Unique::Unique( const Tag* tag )
00025     : StanzaExtension( ExtMUCUnique )
00026   {
00027     if( !tag || tag->name() != "unique" || tag->xmlns() != XMLNS_MUC_UNIQUE )
00028       return;
00029 
00030     m_name = tag->cdata();
00031   }
00032 
00033   const std::string& UniqueMUCRoom::Unique::filterString() const
00034   {
00035     static const std::string filter = "/iq/unique[@xmlns='" + XMLNS_MUC_UNIQUE + "']";
00036     return filter;
00037   }
00038 
00039   Tag* UniqueMUCRoom::Unique::tag() const
00040   {
00041     Tag* t = new Tag( "unique" );
00042     t->setXmlns( XMLNS_MUC_UNIQUE );
00043     if( !m_name.empty() )
00044       t->setCData( m_name );
00045     return t;
00046   }
00047   // ---- ~UniqueMUCRoom::Unique ----
00048 
00049   // ---- UniqueMUCRoom ----
00050   UniqueMUCRoom::UniqueMUCRoom( ClientBase* parent, const JID& nick, MUCRoomHandler* mrh )
00051     : InstantMUCRoom( parent, nick, mrh )
00052   {
00053     if( m_parent )
00054     {
00055       m_parent->registerStanzaExtension( new Unique() );
00056     }
00057   }
00058 
00059   UniqueMUCRoom::~UniqueMUCRoom()
00060   {
00061     if( m_parent )
00062     {
00063       m_parent->removeIDHandler( this );
00064 //       m_parent->removeStanzaExtension( ExtMUCUnique ); // don't remove, other rooms might need it
00065     }
00066   }
00067 
00068   void UniqueMUCRoom::join()
00069   {
00070     if( !m_parent || m_joined )
00071       return;
00072 
00073     IQ iq( IQ::Get, m_nick.server() );
00074     iq.addExtension( new Unique() );
00075     m_parent->send( iq, this, RequestUniqueName );
00076   }
00077 
00078   void UniqueMUCRoom::handleIqID( const IQ& iq, int context )
00079   {
00080     switch( iq.subtype() )
00081     {
00082       case IQ::Result:
00083         if( context == RequestUniqueName )
00084         {
00085           const Unique* u = iq.findExtension<Unique>( ExtMUCUnique );
00086           if( u )
00087           {
00088             if( !u->name().empty() )
00089               setName( u->name() );
00090           }
00091         }
00092         break;
00093       case IQ::Error:
00094         if( context == RequestUniqueName )
00095         {
00096           SHA s;
00097           s.feed( m_parent->jid().full() );
00098           s.feed( m_parent->getID() );
00099           setName( s.hex() );
00100         }
00101         break;
00102       default:
00103         break;
00104     }
00105 
00106     MUCRoom::join();
00107   }
00108 
00109 }