messagesession.cpp
/*
Copyright (c) 2005-2023 by Jakob Schröter <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 "messagesession.h"
#include "messagefilter.h"
#include "messagehandler.h"
#include "clientbase.h"
#include "disco.h"
#include "message.h"
#include "util.h"
namespace gloox
{
MessageSession::MessageSession( ClientBase* parent, const JID& jid, bool wantResourceTracking, int types, bool honorTID )
: m_parent( parent ), m_target( jid ), m_messageHandler( 0 ),
m_types( types ), m_wantResourceTracking( wantResourceTracking ), m_hadMessages( false ), m_honorThreadID( honorTID )
{
if( m_parent )
m_parent->registerMessageSession( this );
}
MessageSession::~MessageSession()
{
util::clearList( m_messageFilterList );
}
void MessageSession::handleMessage( Message& msg )
{
if( m_wantResourceTracking && msg.from().resource() != m_target.resource() )
setResource( msg.from().resource() );
if( !m_hadMessages )
{
m_hadMessages = true;
if( msg.thread().empty() )
{
m_thread = "gloox" + m_parent->getID();
msg.setThread( m_thread );
}
else
m_thread = msg.thread();
}
MessageFilterList::const_iterator it = m_messageFilterList.begin();
for( ; it != m_messageFilterList.end(); ++it )
(*it)->filter( msg );
if( m_messageHandler )
m_messageHandler->handleMessage( msg, this );
}
void MessageSession::send( const std::string& message )
{
send( message, EmptyString );
}
void MessageSession::send( const std::string& message, const std::string& subject, const StanzaExtensionList& sel )
{
if( !m_hadMessages )
{
m_thread = "gloox" + m_parent->getID();
m_hadMessages = true;
}
Message m( Message::Chat, m_target.full(), message, subject, m_thread );
m.setID( m_parent->getID() );
decorate( m );
if( sel.size() )
{
StanzaExtensionList::const_iterator it = sel.begin();
for( ; it != sel.end(); ++it )
m.addExtension( (*it));
}
m_parent->send( m );
}
void MessageSession::send( const Message& msg )
{
m_parent->send( msg );
}
void MessageSession::decorate( Message& msg )
{
util::ForEach( m_messageFilterList, &MessageFilter::decorate, msg );
}
void MessageSession::resetResource()
{
m_target.setResource( EmptyString );
}
void MessageSession::setResource( const std::string& resource )
{
m_target.setResource( resource );
}
void MessageSession::disposeMessageFilter( MessageFilter* mf )
{
removeMessageFilter( mf );
delete mf;
}
}
gloox::ClientBase
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition clientbase.h:79
gloox::JID
An abstraction of a JID.
Definition jid.h:31
gloox::JID::resource
const std::string & resource() const
Definition jid.h:116
gloox::JID::setResource
bool setResource(const std::string &resource)
Definition jid.cpp:64
gloox::MessageFilter::decorate
virtual void decorate(Message &msg)=0
gloox::MessageSession::disposeMessageFilter
void disposeMessageFilter(MessageFilter *mf)
Definition messagesession.cpp:109
gloox::MessageSession::send
virtual void send(const std::string &message)
Definition messagesession.cpp:62
gloox::MessageSession::MessageSession
MessageSession(ClientBase *parent, const JID &jid, bool wantUpgrade=true, int types=0, bool honorTID=true)
Definition messagesession.cpp:24
gloox::MessageSession::removeMessageFilter
void removeMessageFilter(MessageFilter *mf)
Definition messagesession.h:255
gloox::MessageSession::resetResource
void resetResource()
Definition messagesession.cpp:99
gloox::MessageSession::~MessageSession
virtual ~MessageSession()
Definition messagesession.cpp:32
gloox::MessageSession::types
int types() const
Definition messagesession.h:269
gloox::MessageSession::handleMessage
virtual void handleMessage(Message &msg)
Definition messagesession.cpp:37
gloox::Message
An abstraction of a message stanza.
Definition message.h:34
gloox::Message::setID
void setID(const std::string &id)
Definition message.h:122
gloox::Message::Chat
@ Chat
Definition message.h:46
gloox::Message::setThread
void setThread(const std::string &thread)
Definition message.h:116
gloox::Message::thread
const std::string & thread() const
Definition message.h:110
gloox::Stanza::addExtension
void addExtension(const StanzaExtension *se)
Definition stanza.cpp:52
gloox::Stanza::from
const JID & from() const
Definition stanza.h:51
gloox::util::clearList
void clearList(std::list< T * > &L)
Definition util.h:152
gloox::util::ForEach
void ForEach(T &t, F f)
Definition util.h:96
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::StanzaExtensionList
std::list< const StanzaExtension * > StanzaExtensionList
Definition gloox.h:1272
gloox::EmptyString
const std::string EmptyString
Definition gloox.cpp:124