messageeventfilter.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 "messageeventfilter.h"
#include "messageeventhandler.h"
#include "messagesession.h"
#include "message.h"
#include "messageevent.h"
#include "error.h"
namespace gloox
{
MessageEventFilter::MessageEventFilter( MessageSession* parent )
: MessageFilter( parent ), m_messageEventHandler( 0 ), m_requestedEvents( 0 ),
m_lastSent( MessageEventCancel ), m_disable( false )
{
}
MessageEventFilter::~MessageEventFilter()
{
}
void MessageEventFilter::filter( Message& msg )
{
if( m_disable || !m_messageEventHandler )
return;
if( msg.subtype() == Message::Error )
{
if( msg.error() && msg.error()->error() == StanzaErrorFeatureNotImplemented )
m_disable = true;
return;
}
const MessageEvent* me = msg.findExtension<MessageEvent>( ExtMessageEvent );
if( !me )
{
m_requestedEvents = 0;
m_lastID = EmptyString;
return;
}
if( msg.body().empty() )
m_messageEventHandler->handleMessageEvent( msg.from(), static_cast<MessageEventType>( me->event() ) );
else
{
m_lastID = msg.id();
m_requestedEvents = 0;
m_requestedEvents = me->event();
}
}
void MessageEventFilter::raiseMessageEvent( MessageEventType event )
{
if( m_disable || ( !( m_requestedEvents & event ) && ( event != MessageEventCancel ) ) )
return;
switch( event )
{
case MessageEventOffline:
case MessageEventDelivered:
case MessageEventDisplayed:
m_requestedEvents &= ~event;
break;
case MessageEventComposing:
if( m_lastSent == MessageEventComposing )
return;
break;
case MessageEventCancel:
default:
break;
}
m_lastSent = event;
Message m( Message::Normal, m_parent->target() );
m.addExtension( new MessageEvent( event, m_lastID ) );
send( m );
}
void MessageEventFilter::decorate( Message& msg )
{
if( m_disable )
return;
msg.addExtension( new MessageEvent( MessageEventOffline | MessageEventDelivered |
MessageEventDisplayed | MessageEventComposing ) );
m_lastSent = MessageEventCancel;
}
void MessageEventFilter::registerMessageEventHandler( MessageEventHandler* meh )
{
m_messageEventHandler = meh;
}
void MessageEventFilter::removeMessageEventHandler()
{
m_messageEventHandler = 0;
}
}
gloox::Error::error
StanzaError error() const
Definition error.h:75
gloox::MessageEventFilter::MessageEventFilter
MessageEventFilter(MessageSession *parent)
Definition messageeventfilter.cpp:24
gloox::MessageEventFilter::decorate
virtual void decorate(Message &msg)
Definition messageeventfilter.cpp:92
gloox::MessageEventFilter::~MessageEventFilter
virtual ~MessageEventFilter()
Definition messageeventfilter.cpp:30
gloox::MessageEventFilter::filter
virtual void filter(Message &msg)
Definition messageeventfilter.cpp:34
gloox::MessageEventFilter::removeMessageEventHandler
void removeMessageEventHandler()
Definition messageeventfilter.cpp:107
gloox::MessageEventFilter::raiseMessageEvent
void raiseMessageEvent(MessageEventType event)
Definition messageeventfilter.cpp:65
gloox::MessageEventFilter::registerMessageEventHandler
void registerMessageEventHandler(MessageEventHandler *meh)
Definition messageeventfilter.cpp:102
gloox::MessageEventHandler
A virtual interface that enables an object to be notified about Message Events (XEP-0022).
Definition messageeventhandler.h:32
gloox::MessageEvent
An implementation of Message Events (XEP-0022) as a StanzaExtension.
Definition messageevent.h:33
gloox::MessageEvent::event
int event() const
Definition messageevent.h:60
gloox::MessageFilter::MessageFilter
MessageFilter(MessageSession *parent)
Definition messagefilter.cpp:20
gloox::MessageSession
An abstraction of a message session between any two entities.
Definition messagesession.h:146
gloox::Message
An abstraction of a message stanza.
Definition message.h:34
gloox::Message::body
const std::string body(const std::string &lang="default") const
Definition message.h:87
gloox::Message::Error
@ Error
Definition message.h:47
gloox::Message::Normal
@ Normal
Definition message.h:50
gloox::Message::subtype
MessageType subtype() const
Definition message.h:76
gloox::Stanza::addExtension
void addExtension(const StanzaExtension *se)
Definition stanza.cpp:52
gloox::Stanza::id
const std::string & id() const
Definition stanza.h:63
gloox::Stanza::error
const Error * error() const
Definition stanza.cpp:47
gloox::Stanza::from
const JID & from() const
Definition stanza.h:51
gloox::Stanza::findExtension
const StanzaExtension * findExtension(int type) const
Definition stanza.cpp:57
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::MessageEventType
MessageEventType
Definition gloox.h:1090
gloox::MessageEventComposing
@ MessageEventComposing
Definition gloox.h:1096
gloox::MessageEventCancel
@ MessageEventCancel
Definition gloox.h:1098
gloox::MessageEventDisplayed
@ MessageEventDisplayed
Definition gloox.h:1095
gloox::MessageEventOffline
@ MessageEventOffline
Definition gloox.h:1091
gloox::MessageEventDelivered
@ MessageEventDelivered
Definition gloox.h:1093
gloox::EmptyString
const std::string EmptyString
Definition gloox.cpp:124
gloox::StanzaErrorFeatureNotImplemented
@ StanzaErrorFeatureNotImplemented
Definition gloox.h:881
gloox::ExtMessageEvent
@ ExtMessageEvent
Definition stanzaextension.h:51