mucroom.cpp

来自「Jabber code library, developed with c」· C++ 代码 · 共 1,119 行 · 第 1/3 页

CPP
1,119
字号
/*  Copyright (c) 2006-2008 by Jakob Schroeter <js@camaya.net>  This file is part of the gloox library. http://camaya.net/gloox  This software is distributed under a license. The full license  agreement can be found in the file LICENSE in this distribution.  This software may not be copied, modified, sold or distributed  other than expressed in the named license agreement.  This software is distributed without any warranty.*/#include "mucroom.h"#include "clientbase.h"#include "dataform.h"#include "stanza.h"#include "disco.h"#include "mucmessagesession.h"namespace gloox{  MUCRoom::MUCRoom( ClientBase *parent, const JID& nick, MUCRoomHandler *mrh,                    MUCRoomConfigHandler *mrch )    : m_parent( parent ), m_nick( nick ), m_joined( false ), m_roomHandler( mrh ),      m_roomConfigHandler( mrch ), m_affiliation( AffiliationNone ), m_role( RoleNone ),      m_historyType( HistoryUnknown ), m_historyValue( 0 ), m_flags( 0 ),      m_creationInProgress( false ), m_configChanged( false ),      m_publishNick( false ), m_publish( false ), m_unique( false )  {  }  MUCRoom::~MUCRoom()  {    if( m_joined )      leave();    if( m_parent )    {      if(m_publish )        m_parent->disco()->removeNodeHandler( this, XMLNS_MUC_ROOMS );      m_parent->removeIDHandler( this );    }  }  void MUCRoom::join()  {    if( m_joined || !m_parent )      return;    m_parent->registerPresenceHandler( m_nick.bareJID(), this );    m_session = new MUCMessageSession( m_parent, m_nick.bareJID() );    m_session->registerMessageHandler( this );    Stanza *s = Stanza::createPresenceStanza( m_nick );    Tag *x = new Tag( s, "x" );    x->addAttribute( "xmlns", XMLNS_MUC );    if( !m_password.empty() )      new Tag( x, "password",  m_password );    if( m_historyType != HistoryUnknown )    {      switch( m_historyType )      {        case HistoryMaxChars:        {          Tag *h = new Tag( x, "history" );          h->addAttribute( "maxchars", m_historyValue );          break;        }        case HistoryMaxStanzas:        {          Tag *h = new Tag( x, "history" );          h->addAttribute( "maxstanzas", m_historyValue );          break;        }        case HistorySeconds:        {          Tag *h = new Tag( x, "history" );          h->addAttribute( "seconds", m_historyValue );          break;        }        case HistorySince:        {          Tag *h = new Tag( x, "history" );          h->addAttribute( "since", m_historySince );          break;        }        default:          break;      }    }    if( m_parent )      m_parent->send( s );    m_joined = true;  }  void MUCRoom::leave( const std::string& msg )  {    if( !m_joined )      return;    Stanza *s = Stanza::createPresenceStanza( m_nick, msg, PresenceUnavailable );    Tag *x = new Tag( s, "x" );    x->addAttribute( "xmlns", XMLNS_MUC );    if( m_parent )    {      m_parent->send( s );      m_parent->removePresenceHandler( m_nick.bareJID(), this );      m_parent->disposeMessageSession( m_session );      m_session = 0;    }    m_joined = false;  }  void MUCRoom::destroy( const std::string& reason, const JID* alternate, const std::string& password )  {    if( !m_parent || !m_joined )      return;    Tag *d = new Tag( "destroy" );    if( alternate )      d->addAttribute( "jid", alternate->bare() );    if( !reason.empty() )      new Tag( d, "reason", reason );    if( !password.empty() )      new Tag( d, "password", password );    const std::string& id = m_parent->getID();    JID j( m_nick.bare() );    Stanza *iq = Stanza::createIqStanza( j, id, StanzaIqSet, XMLNS_MUC_OWNER, d );    m_parent->trackID( this, id, DestroyRoom );    m_parent->send( iq );  }  void MUCRoom::send( const std::string& message )  {    if( m_session && m_joined )      m_session->send( message );  }  void MUCRoom::setSubject( const std::string& subject )  {    if( m_session && m_joined )      m_session->setSubject( subject );  }  void MUCRoom::setNick( const std::string& nick )  {    if( m_parent && m_joined )    {      m_newNick = nick;      Tag *p = new Tag( "presence" );      p->addAttribute( "to", m_nick.bare() + "/" + m_newNick );      m_parent->send( p );    }    else      m_nick.setResource( nick );  }  void MUCRoom::getRoomInfo()  {    if( m_parent )    {      JID j( m_nick.bare() );      m_parent->disco()->getDiscoInfo( j, "", this, GetRoomInfo );    }  }  void MUCRoom::getRoomItems()  {    if( m_parent )    {      JID j( m_nick.bare() );      m_parent->disco()->getDiscoItems( j, "", this, GetRoomItems );    }  }  void MUCRoom::setPresence( Presence presence, const std::string& msg )  {    if( m_parent && presence != PresenceUnavailable && m_joined )    {      Stanza *p = Stanza::createPresenceStanza( m_nick, msg, presence );      m_parent->send( p );    }  }  void MUCRoom::invite( const JID& invitee, const std::string& reason, bool cont )  {    if( !m_parent || !m_joined )      return;    Tag *m = new Tag( "message" );    m->addAttribute( "to", m_nick.bare() );    Tag *x = new Tag( m, "x" );    x->addAttribute( "xmlns", XMLNS_MUC_USER );    Tag *i = new Tag( x, "invite" );    i->addAttribute( "to", invitee.bare() );    if( !reason.empty() )      new Tag( i, "reason", reason );    if( cont )      new Tag( i, "continue" );    m_parent->send( m );  }  Stanza* MUCRoom::declineInvitation( const JID& room, const JID& invitor, const std::string& reason )  {    Stanza *m = new Stanza( "message" );    m->addAttribute( "to", room.bare() );    Tag *x = new Tag( m, "x" );    x->addAttribute( "xmlns", XMLNS_MUC_USER );    Tag *d = new Tag( x, "decline" );    d->addAttribute( "to", invitor.bare() );    if( !reason.empty() )      new Tag( d, "reason", reason );    return m;  }  void MUCRoom::setPublish( bool publish, bool publishNick )  {    m_publish = publish;    m_publishNick = publishNick;    if( !m_parent )      return;    if( m_publish )      m_parent->disco()->registerNodeHandler( this, XMLNS_MUC_ROOMS );    else      m_parent->disco()->removeNodeHandler( this, XMLNS_MUC_ROOMS );  }  void MUCRoom::addHistory( const std::string& message, const JID& from, const std::string& stamp )  {    if( !m_joined || !m_parent )      return;    Tag *m = new Tag( "message" );    m->addAttribute( "to", m_nick.bare() );    m->addAttribute( "type", "groupchat" );    new Tag( m, "body", message );    Tag *x = new Tag( m, "x" );    x->addAttribute( "xmlns", XMLNS_X_DELAY );    x->addAttribute( "from", from.full() );    x->addAttribute( "stamp", stamp );    m_parent->send( m );  }  void MUCRoom::setRequestHistory( int value, MUCRoom::HistoryRequestType type )  {    m_historyType = type;    m_historySince = "";    m_historyValue = value;  }  void MUCRoom::setRequestHistory( const std::string& since )  {    m_historyType = HistorySince;    m_historySince = since;    m_historyValue = 0;  }  Stanza* MUCRoom::createDataForm( const JID& room, const DataForm& df )  {    Stanza *m = new Stanza( "message" );    m->addAttribute( "to", room.bare() );    m->addChild( df.tag() );    return m;  }  void MUCRoom::requestVoice()  {    if( !m_parent || !m_joined )      return;    DataForm df( DataForm::FormTypeSubmit );    DataFormField *field = new DataFormField( DataFormField::FieldTypeNone );    field->setName( "FORM_TYPE" );    field->setValue( XMLNS_MUC_REQUEST );    df.addField( field );    field = new DataFormField( DataFormField::FieldTypeTextSingle );    field->setName( "muc#role" );    field->setLabel( "Requested role" );    field->setValue( "participant" );    df.addField( field );    Tag *m = new Tag( "messsage" );    m->addAttribute( "to", m_nick.bare() );    m->addChild( df.tag() );    m_parent->send( m );  }  void MUCRoom::kick( const std::string& nick, const std::string& reason )  {    setRole( nick, RoleNone, reason );  }  void MUCRoom::grantVoice( const std::string& nick, const std::string& reason )  {    setRole( nick, RoleParticipant, reason );  }  void MUCRoom::revokeVoice( const std::string& nick, const std::string& reason )  {    setRole( nick, RoleVisitor, reason );  }  void MUCRoom::ban( const std::string& nick, const std::string& reason )  {    setAffiliation( nick, AffiliationOutcast, reason );  }  void MUCRoom::setRole( const std::string& nick, MUCRoomRole role, const std::string& reason )  {    modifyOccupant( nick, role, "role", reason );  }  void MUCRoom::setAffiliation( const std::string& nick, MUCRoomAffiliation affiliation,                                const std::string& reason )  {    modifyOccupant( nick, affiliation, "affiliation", reason );  }  void MUCRoom::modifyOccupant( const std::string& nick, int state, const std::string roa,                                const std::string& reason )  {    if( !m_parent || !m_joined || nick.empty() || roa.empty() )      return;    std::string newRoA;    MUCOperation action = SetRNone;    if( roa == "role" )    {      switch( state )      {        case RoleNone:          newRoA = "none";          action = SetRNone;          break;        case RoleVisitor:          newRoA = "visitor";          action = SetVisitor;          break;        case RoleParticipant:          newRoA = "participant";          action = SetParticipant;          break;        case RoleModerator:          newRoA = "moderator";          action = SetModerator;          break;      }    }    else    {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?