gloox  1.0
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 }
virtual void decorate(Message &msg)=0
bool setResource(const std::string &resource)
Definition: jid.cpp:64
std::list< const StanzaExtension * > StanzaExtensionList
Definition: gloox.h:1219
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:144
void send(Tag *tag)
Definition: clientbase.cpp:771
void setID(const std::string &id)
Definition: message.h:121
void ForEach(T &t, F f)
Definition: util.h:88
An abstraction of a message stanza.
Definition: message.h:33
const std::string & bare() const
Definition: jid.h:67
virtual void send(const std::string &message, const std::string &subject=EmptyString, const StanzaExtensionList &sel=StanzaExtensionList())
void registerMessageSession(MessageSession *session)
An abstraction of a JID.
Definition: jid.h:30
const std::string & thread() const
Definition: message.h:109
void setThread(const std::string &thread)
Definition: message.h:115
const std::string & resource() const
Definition: jid.h:116
const std::string getID()
Definition: clientbase.cpp:886
const std::string & full() const
Definition: jid.h:61
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:118
const std::string body(const std::string &lang="default") const
Definition: message.h:86
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition: clientbase.h:75
const JID & from() const
Definition: stanza.h:51