gloox  1.0.10
messagesession.cpp
1 /*
2  Copyright (c) 2005-2013 by Jakob Schroeter <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 }