gloox  1.0.28
messageeventfilter.cpp
1 /*
2  Copyright (c) 2005-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 #include "messageeventfilter.h"
15 #include "messageeventhandler.h"
16 #include "messagesession.h"
17 #include "message.h"
18 #include "messageevent.h"
19 #include "error.h"
20 
21 namespace gloox
22 {
23 
25  : MessageFilter( parent ), m_messageEventHandler( 0 ), m_requestedEvents( 0 ),
26  m_lastSent( MessageEventCancel ), m_disable( false )
27  {
28  }
29 
31  {
32  }
33 
35  {
36  if( m_disable || !m_messageEventHandler )
37  return;
38 
39  if( msg.subtype() == Message::Error )
40  {
41  if( msg.error() && msg.error()->error() == StanzaErrorFeatureNotImplemented )
42  m_disable = true;
43 
44  return;
45  }
46 
48  if( !me )
49  {
50  m_requestedEvents = 0;
51  m_lastID = EmptyString;
52  return;
53  }
54 
55  if( msg.body().empty() )
56  m_messageEventHandler->handleMessageEvent( msg.from(), static_cast<MessageEventType>( me->event() ) );
57  else
58  {
59  m_lastID = msg.id();
60  m_requestedEvents = 0;
61  m_requestedEvents = me->event();
62  }
63  }
64 
66  {
67  if( m_disable || ( !( m_requestedEvents & event ) && ( event != MessageEventCancel ) ) )
68  return;
69 
70  switch( event )
71  {
75  m_requestedEvents &= ~event;
76  break;
78  if( m_lastSent == MessageEventComposing )
79  return;
80  break;
81  case MessageEventCancel:
82  default:
83  break;
84  }
85 
86  m_lastSent = event;
87  Message m( Message::Normal, m_parent->target() );
88  m.addExtension( new MessageEvent( event, m_lastID ) );
89  send( m );
90  }
91 
93  {
94  if( m_disable )
95  return;
96 
99  m_lastSent = MessageEventCancel;
100  }
101 
103  {
104  m_messageEventHandler = meh;
105  }
106 
108  {
109  m_messageEventHandler = 0;
110  }
111 
112 }
StanzaError error() const
Definition: error.h:75
MessageEventFilter(MessageSession *parent)
virtual void decorate(Message &msg)
virtual void filter(Message &msg)
void raiseMessageEvent(MessageEventType event)
void registerMessageEventHandler(MessageEventHandler *meh)
A virtual interface that enables an object to be notified about Message Events (XEP-0022).
virtual void handleMessageEvent(const JID &from, MessageEventType event)=0
An implementation of Message Events (XEP-0022) as a StanzaExtension.
Definition: messageevent.h:33
int event() const
Definition: messageevent.h:60
Virtual base class for message filters.
Definition: messagefilter.h:38
An abstraction of a message session between any two entities.
const JID & target() const
An abstraction of a message stanza.
Definition: message.h:34
const std::string body(const std::string &lang="default") const
Definition: message.h:87
MessageType subtype() const
Definition: message.h:76
void addExtension(const StanzaExtension *se)
Definition: stanza.cpp:52
const Error * error() const
Definition: stanza.cpp:47
const std::string & id() const
Definition: stanza.h:63
const JID & from() const
Definition: stanza.h:51
const StanzaExtension * findExtension(int type) const
Definition: stanza.cpp:57
The namespace for the gloox library.
Definition: adhoc.cpp:28
MessageEventType
Definition: gloox.h:1090
@ MessageEventComposing
Definition: gloox.h:1096
@ MessageEventCancel
Definition: gloox.h:1098
@ MessageEventDisplayed
Definition: gloox.h:1095
@ MessageEventOffline
Definition: gloox.h:1091
@ MessageEventDelivered
Definition: gloox.h:1093
const std::string EmptyString
Definition: gloox.cpp:124
@ StanzaErrorFeatureNotImplemented
Definition: gloox.h:881
@ ExtMessageEvent