gloox  1.0.22
messagesession.cpp
1 /*
2  Copyright (c) 2005-2017 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 #include "messagesession.h"
14 #include "messagefilter.h"
15 #include "messagehandler.h"
16 #include "clientbase.h"
17 #include "disco.h"
18 #include "message.h"
19 #include "util.h"
20 
21 namespace gloox
22 {
23 
24  MessageSession::MessageSession( ClientBase* parent, const JID& jid, bool wantResourceTracking, int types, bool honorTID )
25  : m_parent( parent ), m_target( jid ), m_messageHandler( 0 ),
26  m_types( types ), m_wantResourceTracking( wantResourceTracking ), m_hadMessages( false ), m_honorThreadID( honorTID )
27  {
28  if( m_parent )
29  m_parent->registerMessageSession( this );
30  }
31 
33  {
34  util::clearList( m_messageFilterList );
35  }
36 
38  {
39  if( m_wantResourceTracking && msg.from().resource() != m_target.resource() )
40  setResource( msg.from().resource() );
41 
42  if( !m_hadMessages )
43  {
44  m_hadMessages = true;
45  if( msg.thread().empty() )
46  {
47  m_thread = "gloox" + m_parent->getID();
48  msg.setThread( m_thread );
49  }
50  else
51  m_thread = msg.thread();
52  }
53 
54  MessageFilterList::const_iterator it = m_messageFilterList.begin();
55  for( ; it != m_messageFilterList.end(); ++it )
56  (*it)->filter( msg );
57 
58  if( m_messageHandler )
59  m_messageHandler->handleMessage( msg, this );
60  }
61 
62  void MessageSession::send( const std::string& message )
63  {
64  send( message, EmptyString );
65  }
66 
67  void MessageSession::send( const std::string& message, const std::string& subject, const StanzaExtensionList& sel )
68  {
69  if( !m_hadMessages )
70  {
71  m_thread = "gloox" + m_parent->getID();
72  m_hadMessages = true;
73  }
74 
75  Message m( Message::Chat, m_target.full(), message, subject, m_thread );
76  m.setID( m_parent->getID() );
77  decorate( m );
78 
79  if( sel.size() )
80  {
81  StanzaExtensionList::const_iterator it = sel.begin();
82  for( ; it != sel.end(); ++it )
83  m.addExtension( (*it));
84  }
85 
86  m_parent->send( m );
87  }
88 
89  void MessageSession::send( const Message& msg )
90  {
91  m_parent->send( msg );
92  }
93 
94  void MessageSession::decorate( Message& msg )
95  {
96  util::ForEach( m_messageFilterList, &MessageFilter::decorate, msg );
97  }
98 
100  {
101  m_target.setResource( EmptyString );
102  }
103 
104  void MessageSession::setResource( const std::string& resource )
105  {
106  m_target.setResource( resource );
107  }
108 
110  {
111  removeMessageFilter( mf );
112  delete mf;
113  }
114 
115 }
gloox::MessageSession::removeMessageFilter
void removeMessageFilter(MessageFilter *mf)
Definition: messagesession.h:255
gloox::Message::setThread
void setThread(const std::string &thread)
Definition: message.h:116
gloox::ClientBase::getID
const std::string getID()
Definition: clientbase.cpp:1168
gloox::JID::resource
const std::string & resource() const
Definition: jid.h:116
gloox::util::clearList
void clearList(std::list< T * > &L)
Definition: util.h:152
gloox::MessageSession::~MessageSession
virtual ~MessageSession()
Definition: messagesession.cpp:32
gloox::Message
An abstraction of a message stanza.
Definition: message.h:33
gloox::Stanza::addExtension
void addExtension(const StanzaExtension *se)
Definition: stanza.cpp:52
gloox::JID::full
const std::string & full() const
Definition: jid.h:61
gloox::MessageSession::resetResource
void resetResource()
Definition: messagesession.cpp:99
gloox::util::ForEach
void ForEach(T &t, F f)
Definition: util.h:96
gloox::ClientBase::registerMessageSession
void registerMessageSession(MessageSession *session)
Definition: clientbase.cpp:1409
gloox::ClientBase::send
void send(Tag *tag)
Definition: clientbase.cpp:1004
gloox::MessageFilter
Virtual base class for message filters.
Definition: messagefilter.h:37
gloox::Stanza::from
const JID & from() const
Definition: stanza.h:51
gloox::StanzaExtensionList
std::list< const StanzaExtension * > StanzaExtensionList
Definition: gloox.h:1268
gloox::Message::Chat
@ Chat
Definition: message.h:46
gloox::EmptyString
const std::string EmptyString
Definition: gloox.cpp:124
gloox::MessageFilter::decorate
virtual void decorate(Message &msg)=0
gloox::JID
An abstraction of a JID.
Definition: jid.h:30
gloox
The namespace for the gloox library.
Definition: adhoc.cpp:27
gloox::ClientBase
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition: clientbase.h:76
gloox::MessageSession::send
virtual void send(const std::string &message)
Definition: messagesession.cpp:62
gloox::Message::setID
void setID(const std::string &id)
Definition: message.h:122
gloox::MessageSession::MessageSession
MessageSession(ClientBase *parent, const JID &jid, bool wantUpgrade=true, int types=0, bool honorTID=true)
Definition: messagesession.cpp:24
gloox::MessageSession::disposeMessageFilter
void disposeMessageFilter(MessageFilter *mf)
Definition: messagesession.cpp:109
gloox::JID::setResource
bool setResource(const std::string &resource)
Definition: jid.cpp:64
gloox::Message::thread
const std::string & thread() const
Definition: message.h:110
gloox::MessageHandler::handleMessage
virtual void handleMessage(const Message &msg, MessageSession *session=0)=0
gloox::MessageSession::handleMessage
virtual void handleMessage(Message &msg)
Definition: messagesession.cpp:37