gloox  1.0
uniquemucroom.cpp
1 /*
2  Copyright (c) 2007-2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 
15 #include "uniquemucroom.h"
16 #include "clientbase.h"
17 #include "jid.h"
18 #include "sha.h"
19 
20 namespace gloox
21 {
22 
23  // ---- UniqueMUCRoom::Unique ----
24  UniqueMUCRoom::Unique::Unique( const Tag* tag )
25  : StanzaExtension( ExtMUCUnique )
26  {
27  if( !tag || tag->name() != "unique" || tag->xmlns() != XMLNS_MUC_UNIQUE )
28  return;
29 
30  m_name = tag->cdata();
31  }
32 
33  const std::string& UniqueMUCRoom::Unique::filterString() const
34  {
35  static const std::string filter = "/iq/unique[@xmlns='" + XMLNS_MUC_UNIQUE + "']";
36  return filter;
37  }
38 
39  Tag* UniqueMUCRoom::Unique::tag() const
40  {
41  Tag* t = new Tag( "unique" );
42  t->setXmlns( XMLNS_MUC_UNIQUE );
43  if( !m_name.empty() )
44  t->setCData( m_name );
45  return t;
46  }
47  // ---- ~UniqueMUCRoom::Unique ----
48 
49  // ---- UniqueMUCRoom ----
51  : InstantMUCRoom( parent, nick, mrh )
52  {
53  if( m_parent )
54  {
55  m_parent->registerStanzaExtension( new Unique() );
56  }
57  }
58 
60  {
61  if( m_parent )
62  {
63  m_parent->removeIDHandler( this );
64 // m_parent->removeStanzaExtension( ExtMUCUnique ); // don't remove, other rooms might need it
65  }
66  }
67 
68  void UniqueMUCRoom::join()
69  {
70  if( !m_parent || m_joined )
71  return;
72 
73  IQ iq( IQ::Get, m_nick.server() );
74  iq.addExtension( new Unique() );
75  m_parent->send( iq, this, RequestUniqueName );
76  }
77 
78  void UniqueMUCRoom::handleIqID( const IQ& iq, int context )
79  {
80  switch( iq.subtype() )
81  {
82  case IQ::Result:
83  if( context == RequestUniqueName )
84  {
85  const Unique* u = iq.findExtension<Unique>( ExtMUCUnique );
86  if( u )
87  {
88  if( !u->name().empty() )
89  setName( u->name() );
90  }
91  }
92  break;
93  case IQ::Error:
94  if( context == RequestUniqueName )
95  {
96  SHA s;
97  s.feed( m_parent->jid().full() );
98  s.feed( m_parent->getID() );
99  setName( s.hex() );
100  }
101  break;
102  default:
103  break;
104  }
105 
106  MUCRoom::join();
107  }
108 
109 }