gloox  1.0.28
uniquemucroom.cpp
1 /*
2  Copyright (c) 2007-2023 by Jakob Schröter <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 ----
50  UniqueMUCRoom::UniqueMUCRoom( ClientBase* parent, const JID& nick, MUCRoomHandler* mrh )
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 }
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition: clientbase.h:79
const std::string getID()
const JID & jid()
Definition: clientbase.h:147
void removeIDHandler(IqHandler *ih)
void send(Tag *tag)
void registerStanzaExtension(StanzaExtension *ext)
An abstraction of an IQ stanza.
Definition: iq.h:34
@ Error
Definition: iq.h:49
@ Result
Definition: iq.h:48
@ Get
Definition: iq.h:45
This class implements an instant MUC room.
An abstraction of a JID.
Definition: jid.h:31
const std::string & server() const
Definition: jid.h:104
const std::string & full() const
Definition: jid.h:61
This interface enables inheriting classes to be notified about certain events in a MUC room.
virtual void join(Presence::PresenceType type=Presence::Available, const std::string &status=EmptyString, int priority=0)
Definition: mucroom.cpp:655
void setName(const std::string &name)
Definition: mucroom.h:780
The namespace for the gloox library.
Definition: adhoc.cpp:28
const std::string XMLNS_MUC_UNIQUE
Definition: gloox.cpp:71
@ ExtMUCUnique