gloox  1.1-svn
messagesession.cpp
1 /*
2  Copyright (c) 2005-2009 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 wantUpgrade, int types, bool honorTID )
25  : m_parent( parent ), m_target( jid ), m_messageHandler( 0 ),
26  m_types( types ), m_wantUpgrade( wantUpgrade ), 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_wantUpgrade && msg.from().bare() == m_target.full() )
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 && !msg.body().empty() )
59  m_messageHandler->handleMessage( msg, this );
60  }
61 
62  void MessageSession::send( const std::string& message, const std::string& subject, const StanzaExtensionList& sel )
63  {
64  if( !m_hadMessages )
65  {
66  m_thread = "gloox" + m_parent->getID();
67  m_hadMessages = true;
68  }
69 
70  Message m( Message::Chat, m_target.full(), message, subject, m_thread );
71  m.setID( m_parent->getID() );
72  decorate( m );
73 
74  if( sel.size() )
75  {
76  StanzaExtensionList::const_iterator it = sel.begin();
77  for( ; it != sel.end(); ++it )
78  m.addExtension( (*it));
79  }
80 
81  m_parent->send( m );
82  }
83 
84  void MessageSession::send( const Message& msg )
85  {
86  m_parent->send( msg );
87  }
88 
89  void MessageSession::decorate( Message& msg )
90  {
91  util::ForEach( m_messageFilterList, &MessageFilter::decorate, msg );
92  }
93 
95  {
96  m_wantUpgrade = true;
97  m_target.setResource( EmptyString );
98  }
99 
100  void MessageSession::setResource( const std::string& resource )
101  {
102  m_target.setResource( resource );
103  }
104 
106  {
107  removeMessageFilter( mf );
108  delete mf;
109  }
110 
111 }