gloox  1.0.20
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 }
virtual void decorate(Message &msg)=0
bool setResource(const std::string &resource)
Definition: jid.cpp:64
std::list< const StanzaExtension * > StanzaExtensionList
Definition: gloox.h:1268
MessageSession(ClientBase *parent, const JID &jid, bool wantUpgrade=true, int types=0, bool honorTID=true)
virtual void handleMessage(Message &msg)
void clearList(std::list< T * > &L)
Definition: util.h:152
void send(Tag *tag)
void setID(const std::string &id)
Definition: message.h:122
void ForEach(T &t, F f)
Definition: util.h:96
const std::string & thread() const
Definition: message.h:110
An abstraction of a message stanza.
Definition: message.h:33
The namespace for the gloox library.
Definition: adhoc.cpp:27
const std::string & full() const
Definition: jid.h:61
virtual void send(const std::string &message)
void registerMessageSession(MessageSession *session)
An abstraction of a JID.
Definition: jid.h:30
const std::string & resource() const
Definition: jid.h:116
void setThread(const std::string &thread)
Definition: message.h:116
const JID & from() const
Definition: stanza.h:51
const std::string getID()
Virtual base class for message filters.
Definition: messagefilter.h:37
void disposeMessageFilter(MessageFilter *mf)
void removeMessageFilter(MessageFilter *mf)
virtual void handleMessage(const Message &msg, MessageSession *session=0)=0
const std::string EmptyString
Definition: gloox.cpp:124
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition: clientbase.h:76