message.cpp
/*
Copyright (c) 2007-2023 by Jakob Schröter <js@camaya.net>
This file is part of the gloox library. http://camaya.net/gloox
This software is distributed under a license. The full license
agreement can be found in the file LICENSE in this distribution.
This software may not be copied, modified, sold or distributed
other than expressed in the named license agreement.
This software is distributed without any warranty.
*/
#include "util.h"
#include "message.h"
namespace gloox
{
static const char* msgTypeStringValues[] =
{
"chat", "error", "groupchat", "headline", "normal"
};
static inline const std::string typeString( Message::MessageType type )
{
return util::lookup2( type, msgTypeStringValues );
}
Message::Message( Tag* tag )
: Stanza( tag ), m_subtype( Invalid ), m_bodies( 0 ), m_subjects( 0 )
{
if( !tag || tag->name() != "message" )
return;
const std::string& typestring = tag->findAttribute( TYPE );
if( typestring.empty() )
m_subtype = Normal;
else
m_subtype = static_cast<MessageType>( util::lookup2( typestring, msgTypeStringValues ) );
const TagList& c = tag->children();
TagList::const_iterator it = c.begin();
for( ; it != c.end(); ++it )
{
if( (*it)->name() == "body" )
setLang( &m_bodies, m_body, (*it) );
else if( (*it)->name() == "subject" )
setLang( &m_subjects, m_subject, (*it) );
else if( (*it)->name() == "thread" )
m_thread = (*it)->cdata();
}
}
Message::Message( MessageType type, const JID& to,
const std::string& body, const std::string& subject,
const std::string& thread, const std::string& xmllang )
: Stanza( to ), m_subtype( type ), m_bodies( 0 ), m_subjects( 0 ), m_thread( thread )
{
setLang( &m_bodies, m_body, body, xmllang );
setLang( &m_subjects, m_subject, subject, xmllang );
}
Message::~Message()
{
delete m_bodies;
delete m_subjects;
}
Tag* Message::tag() const
{
if( m_subtype == Invalid )
return 0;
Tag* t = new Tag( "message" );
if( m_to )
t->addAttribute( "to", m_to.full() );
if( m_from )
t->addAttribute( "from", m_from.full() );
if( !m_id.empty() )
t->addAttribute( "id", m_id );
t->addAttribute( TYPE, typeString( m_subtype ) );
getLangs( m_bodies, m_body, "body", t );
getLangs( m_subjects, m_subject, "subject", t );
if( !m_thread.empty() )
new Tag( t, "thread", m_thread );
StanzaExtensionList::const_iterator it = m_extensionList.begin();
for( ; it != m_extensionList.end(); ++it )
t->addChild( (*it)->tag() );
return t;
}
}
gloox::JID
An abstraction of a JID.
Definition jid.h:31
gloox::Message::subject
const std::string subject(const std::string &lang="default") const
Definition message.h:101
gloox::Message::body
const std::string body(const std::string &lang="default") const
Definition message.h:87
gloox::Message::~Message
virtual ~Message()
Definition message.cpp:63
gloox::Message::Message
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
gloox::Message::MessageType
MessageType
Definition message.h:45
gloox::Message::Invalid
@ Invalid
Definition message.h:51
gloox::Message::thread
const std::string & thread() const
Definition message.h:110
gloox::Message::tag
virtual Tag * tag() const
Definition message.cpp:69
gloox::Stanza
This is the base class for XMPP stanza abstractions.
Definition stanza.h:34
gloox::Stanza::Stanza
Stanza(Tag *tag)
Definition stanza.cpp:31
gloox::Stanza::to
const JID & to() const
Definition stanza.h:57
gloox::Tag
This is an abstraction of an XML element.
Definition tag.h:47
gloox::Tag::addAttribute
bool addAttribute(Attribute *attr)
Definition tag.cpp:354
gloox::Tag::addChild
void addChild(Tag *child)
Definition tag.cpp:424
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::TYPE
const std::string TYPE
Definition gloox.cpp:123