gloox  1.0.28
message.cpp
1 /*
2  Copyright (c) 2007-2023 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 "util.h"
14 #include "message.h"
15 
16 namespace gloox
17 {
18 
19  static const char* msgTypeStringValues[] =
20  {
21  "chat", "error", "groupchat", "headline", "normal"
22  };
23 
24  static inline const std::string typeString( Message::MessageType type )
25  {
26  return util::lookup2( type, msgTypeStringValues );
27  }
28 
29  Message::Message( Tag* tag )
30  : Stanza( tag ), m_subtype( Invalid ), m_bodies( 0 ), m_subjects( 0 )
31  {
32  if( !tag || tag->name() != "message" )
33  return;
34 
35  const std::string& typestring = tag->findAttribute( TYPE );
36  if( typestring.empty() )
37  m_subtype = Normal;
38  else
39  m_subtype = static_cast<MessageType>( util::lookup2( typestring, msgTypeStringValues ) );
40 
41  const TagList& c = tag->children();
42  TagList::const_iterator it = c.begin();
43  for( ; it != c.end(); ++it )
44  {
45  if( (*it)->name() == "body" )
46  setLang( &m_bodies, m_body, (*it) );
47  else if( (*it)->name() == "subject" )
48  setLang( &m_subjects, m_subject, (*it) );
49  else if( (*it)->name() == "thread" )
50  m_thread = (*it)->cdata();
51  }
52  }
53 
54  Message::Message( MessageType type, const JID& to,
55  const std::string& body, const std::string& subject,
56  const std::string& thread, const std::string& xmllang )
57  : Stanza( to ), m_subtype( type ), m_bodies( 0 ), m_subjects( 0 ), m_thread( thread )
58  {
59  setLang( &m_bodies, m_body, body, xmllang );
60  setLang( &m_subjects, m_subject, subject, xmllang );
61  }
62 
64  {
65  delete m_bodies;
66  delete m_subjects;
67  }
68 
69  Tag* Message::tag() const
70  {
71  if( m_subtype == Invalid )
72  return 0;
73 
74  Tag* t = new Tag( "message" );
75  if( m_to )
76  t->addAttribute( "to", m_to.full() );
77  if( m_from )
78  t->addAttribute( "from", m_from.full() );
79  if( !m_id.empty() )
80  t->addAttribute( "id", m_id );
81  t->addAttribute( TYPE, typeString( m_subtype ) );
82 
83  getLangs( m_bodies, m_body, "body", t );
84  getLangs( m_subjects, m_subject, "subject", t );
85 
86  if( !m_thread.empty() )
87  new Tag( t, "thread", m_thread );
88 
89  StanzaExtensionList::const_iterator it = m_extensionList.begin();
90  for( ; it != m_extensionList.end(); ++it )
91  t->addChild( (*it)->tag() );
92 
93  return t;
94  }
95 
96 }
An abstraction of a JID.
Definition: jid.h:31
const std::string & full() const
Definition: jid.h:61
const std::string subject(const std::string &lang="default") const
Definition: message.h:101
const std::string body(const std::string &lang="default") const
Definition: message.h:87
virtual ~Message()
Definition: message.cpp:63
Message(MessageType type, const JID &to, const std::string &body=EmptyString, const std::string &subject=EmptyString, const std::string &thread=EmptyString, const std::string &xmllang=EmptyString)
Definition: message.cpp:54
virtual Tag * tag() const
Definition: message.cpp:69
This is the base class for XMPP stanza abstractions.
Definition: stanza.h:34
This is an abstraction of an XML element.
Definition: tag.h:47
bool addAttribute(Attribute *attr)
Definition: tag.cpp:354
void addChild(Tag *child)
Definition: tag.cpp:424
The namespace for the gloox library.
Definition: adhoc.cpp:28
std::list< Tag * > TagList
Definition: tag.h:26
const std::string TYPE
Definition: gloox.cpp:123