gloox  0.9.9.12
messagesession.cpp
1 /*
2  Copyright (c) 2005-2008 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 
14 #include "messagesession.h"
15 
16 #include "messagefilter.h"
17 #include "messagehandler.h"
18 #include "clientbase.h"
19 #include "disco.h"
20 #include "stanza.h"
21 #include "tag.h"
22 
23 namespace gloox
24 {
25 
26  MessageSession::MessageSession( ClientBase *parent, const JID& jid, bool wantUpgrade, int types )
27  : m_parent( parent ), m_target( jid ), m_messageHandler( 0 ),
28  m_types( types ), m_wantUpgrade( wantUpgrade ), m_hadMessages( false )
29  {
30  if( m_parent )
31  m_parent->registerMessageSession( this );
32  }
33 
35  {
36  MessageFilterList::const_iterator it = m_messageFilterList.begin();
37  for( ; it != m_messageFilterList.end(); ++it )
38  delete (*it);
39  }
40 
41  void MessageSession::handleMessage( Stanza *stanza )
42  {
43  if( m_wantUpgrade && stanza->from().bare() == m_target.full() )
44  setResource( stanza->from().resource() );
45 
46  if( !m_hadMessages )
47  {
48  m_hadMessages = true;
49  if( stanza->thread().empty() )
50  {
51  m_thread = "gloox" + m_parent->getID();
52  stanza->setThread( m_thread );
53  }
54  else
55  m_thread = stanza->thread();
56  }
57 
58  MessageFilterList::const_iterator it = m_messageFilterList.begin();
59  for( ; it != m_messageFilterList.end(); ++it )
60  {
61  (*it)->filter( stanza );
62  }
63 
64  if( m_messageHandler && !stanza->body().empty() )
65  m_messageHandler->handleMessage( stanza, this );
66  }
67 
68  void MessageSession::send( const std::string& message, const std::string& subject )
69  {
70  if( !m_hadMessages )
71  {
72  m_thread = "gloox" + m_parent->getID();
73  m_hadMessages = true;
74  }
75 
76  Tag *m = new Tag( "message" );
77  m->addAttribute( "type", "chat" );
78  new Tag( m, "body", message );
79  if( !subject.empty() )
80  new Tag( m, "subject", subject );
81 
82  m->addAttribute( "from", m_parent->jid().full() );
83  m->addAttribute( "to", m_target.full() );
84  m->addAttribute( "id", m_parent->getID() );
85  new Tag( m, "thread", m_thread );
86 
87  decorate( m );
88 
89  m_parent->send( m );
90  }
91 
93  {
94  m_parent->send( tag );
95  }
96 
97  void MessageSession::decorate( Tag *tag )
98  {
99  MessageFilterList::const_iterator it = m_messageFilterList.begin();
100  for( ; it != m_messageFilterList.end(); ++it )
101  {
102  (*it)->decorate( tag );
103  }
104  }
105 
107  {
108  m_wantUpgrade = true;
109  m_target.setResource( "" );
110  }
111 
112  void MessageSession::setResource( const std::string& resource )
113  {
114  m_target.setResource( resource );
115  }
116 
118  {
119  m_messageHandler = mh;
120  }
121 
123  {
124  m_messageHandler = 0;
125  }
126 
128  {
129  m_messageFilterList.push_back( mf );
130  }
131 
133  {
134  m_messageFilterList.remove( mf );
135  }
136 
138  {
139  removeMessageFilter( mf );
140  delete mf;
141  }
142 
143 }