registration.cpp
/*
Copyright (c) 2005-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 "registration.h"
#include "clientbase.h"
#include "stanza.h"
#include "error.h"
#include "prep.h"
#include "oob.h"
namespace gloox
{
// Registration::Query ----
Registration::Query::Query( DataForm* form )
: StanzaExtension( ExtRegistration ), m_form( form ), m_fields( 0 ), m_oob( 0 ),
m_del( false ), m_reg( false )
{
}
Registration::Query::Query( bool del )
: StanzaExtension( ExtRegistration ), m_form( 0 ), m_fields( 0 ), m_oob( 0 ), m_del( del ),
m_reg( false )
{
}
Registration::Query::Query( int fields, const RegistrationFields& values )
: StanzaExtension( ExtRegistration ), m_form( 0 ), m_fields( fields ), m_values( values ),
m_oob( 0 ), m_del( false ), m_reg( false )
{
}
Registration::Query::Query( const Tag* tag )
: StanzaExtension( ExtRegistration ), m_form( 0 ), m_fields( 0 ), m_oob( 0 ), m_del( false ),
m_reg( false )
{
if( !tag || tag->name() != "query" || tag->xmlns() != XMLNS_REGISTER )
return;
const TagList& l = tag->children();
TagList::const_iterator it = l.begin();
for( ; it != l.end(); ++it )
{
const std::string& name = (*it)->name();
if( name == "instructions" )
m_instructions = (*it)->cdata();
else if( name == "remove" )
m_del = true;
else if( name == "registered" )
m_reg = true;
else if( name == "username" )
{
m_fields |= FieldUsername;
m_values.username = (*it)->cdata();
}
else if( name == "nick" )
{
m_fields |= FieldNick;
m_values.nick = (*it)->cdata();
}
else if( name == "password" )
{
m_fields |= FieldPassword;
m_values.password = (*it)->cdata();
}
else if( name == "name" )
{
m_fields |= FieldName;
m_values.name = (*it)->cdata();
}
else if( name == "first" )
{
m_fields |= FieldFirst;
m_values.first = (*it)->cdata();
}
else if( name == "last" )
{
m_fields |= FieldLast;
m_values.last = (*it)->cdata();
}
else if( name == "email" )
{
m_fields |= FieldEmail;
m_values.email = (*it)->cdata();
}
else if( name == "address" )
{
m_fields |= FieldAddress;
m_values.address = (*it)->cdata();
}
else if( name == "city" )
{
m_fields |= FieldCity;
m_values.city = (*it)->cdata();
}
else if( name == "state" )
{
m_fields |= FieldState;
m_values.state = (*it)->cdata();
}
else if( name == "zip" )
{
m_fields |= FieldZip;
m_values.zip = (*it)->cdata();
}
else if( name == "phone" )
{
m_fields |= FieldPhone;
m_values.phone = (*it)->cdata();
}
else if( name == "url" )
{
m_fields |= FieldUrl;
m_values.url = (*it)->cdata();
}
else if( name == "date" )
{
m_fields |= FieldDate;
m_values.date = (*it)->cdata();
}
else if( name == "misc" )
{
m_fields |= FieldMisc;
m_values.misc = (*it)->cdata();
}
else if( name == "text" )
{
m_fields |= FieldText;
m_values.text = (*it)->cdata();
}
else if( !m_form && name == "x" && (*it)->xmlns() == XMLNS_X_DATA )
m_form = new DataForm( (*it) );
else if( !m_oob && name == "x" && (*it)->xmlns() == XMLNS_X_OOB )
m_oob = new OOB( (*it) );
}
}
Registration::Query::~Query()
{
delete m_form;
delete m_oob;
}
const std::string& Registration::Query::filterString() const
{
static const std::string filter = "/iq/query[@xmlns='" + XMLNS_REGISTER + "']";
return filter;
}
Tag* Registration::Query::tag() const
{
Tag* t = new Tag( "query" );
t->setXmlns( XMLNS_REGISTER );
if( !m_instructions.empty() )
new Tag( t, "instructions", m_instructions );
if ( m_reg )
new Tag( t, "registered" );
if( m_form )
t->addChild( m_form->tag() );
else if( m_oob )
t->addChild( m_oob->tag() );
else if( m_del )
new Tag( t, "remove" );
else if( m_fields )
{
if( m_fields & FieldUsername )
new Tag( t, "username", m_values.username );
if( m_fields & FieldNick )
new Tag( t, "nick", m_values.nick );
if( m_fields & FieldPassword )
new Tag( t, "password", m_values.password );
if( m_fields & FieldName )
new Tag( t, "name", m_values.name );
if( m_fields & FieldFirst )
new Tag( t, "first", m_values.first );
if( m_fields & FieldLast )
new Tag( t, "last", m_values.last );
if( m_fields & FieldEmail )
new Tag( t, "email", m_values.email );
if( m_fields & FieldAddress )
new Tag( t, "address", m_values.address );
if( m_fields & FieldCity )
new Tag( t, "city", m_values.city );
if( m_fields & FieldState )
new Tag( t, "state", m_values.state );
if( m_fields & FieldZip )
new Tag( t, "zip", m_values.zip );
if( m_fields & FieldPhone )
new Tag( t, "phone", m_values.phone );
if( m_fields & FieldUrl )
new Tag( t, "url", m_values.url );
if( m_fields & FieldDate )
new Tag( t, "date", m_values.date );
if( m_fields & FieldMisc )
new Tag( t, "misc", m_values.misc );
if( m_fields & FieldText )
new Tag( t, "text", m_values.text );
}
return t;
}
// ---- ~Registration::Query ----
// ---- Registration ----
Registration::Registration( ClientBase* parent, const JID& to )
: m_parent( parent ), m_to( to ), m_registrationHandler( 0 )
{
init();
}
Registration::Registration( ClientBase* parent )
: m_parent( parent ), m_registrationHandler( 0 )
{
init();
}
void Registration::init()
{
if( m_parent )
{
m_parent->registerIqHandler( this, ExtRegistration );
m_parent->registerStanzaExtension( new Query() );
}
}
Registration::~Registration()
{
if( m_parent )
{
m_parent->removeIqHandler( this, ExtRegistration );
m_parent->removeIDHandler( this );
m_parent->removeStanzaExtension( ExtRegistration );
}
}
void Registration::fetchRegistrationFields()
{
if( !m_parent || m_parent->state() != StateConnected )
return;
IQ iq( IQ::Get, m_to );
iq.addExtension( new Query() );
m_parent->send( iq, this, FetchRegistrationFields );
}
bool Registration::createAccount( int fields, const RegistrationFields& values )
{
std::string username;
if( !m_parent || !prep::nodeprep( values.username, username ) )
return false;
IQ iq( IQ::Set, m_to );
iq.addExtension( new Query( fields, values ) );
m_parent->send( iq, this, CreateAccount );
return true;
}
void Registration::createAccount( DataForm* form )
{
if( !m_parent || !form )
return;
IQ iq( IQ::Set, m_to );
iq.addExtension( new Query( form ) );
m_parent->send( iq, this, CreateAccount );
}
void Registration::removeAccount()
{
if( !m_parent || !m_parent->authed() )
return;
IQ iq( IQ::Set, m_to );
iq.addExtension( new Query( true ) );
m_parent->send( iq, this, RemoveAccount );
}
void Registration::changePassword( const std::string& username, const std::string& password )
{
if( !m_parent || !m_parent->authed() || username.empty() )
return;
int fields = FieldUsername | FieldPassword;
RegistrationFields rf;
rf.username = username;
rf.password = password;
createAccount( fields, rf );
}
void Registration::registerRegistrationHandler( RegistrationHandler* rh )
{
m_registrationHandler = rh;
}
void Registration::removeRegistrationHandler()
{
m_registrationHandler = 0;
}
void Registration::handleIqID( const IQ& iq, int context )
{
if( !m_registrationHandler )
return;
if( iq.subtype() == IQ::Result )
{
switch( context )
{
case FetchRegistrationFields:
{
const Query* q = iq.findExtension<Query>( ExtRegistration );
if( !q )
return;
if( q->registered() )
m_registrationHandler->handleAlreadyRegistered( iq.from() );
if( q->form() )
m_registrationHandler->handleDataForm( iq.from(), *(q->form()) );
if( q->oob() )
m_registrationHandler->handleOOB( iq.from(), *(q->oob()) );
m_registrationHandler->handleRegistrationFields( iq.from(), q->fields(), q->instructions() );
break;
}
case CreateAccount:
case ChangePassword:
case RemoveAccount:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationSuccess );
break;
}
}
else if( iq.subtype() == IQ::Error )
{
const Error* e = iq.error();
if( !e )
return;
switch( e->error() )
{
case StanzaErrorConflict:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationConflict );
break;
case StanzaErrorNotAcceptable:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationNotAcceptable );
break;
case StanzaErrorBadRequest:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationBadRequest );
break;
case StanzaErrorForbidden:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationForbidden );
break;
case StanzaErrorRegistrationRequired:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationRequired );
break;
case StanzaErrorUnexpectedRequest:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationUnexpectedRequest );
break;
case StanzaErrorNotAuthorized:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationNotAuthorized );
break;
case StanzaErrorNotAllowed:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationNotAllowed );
break;
case StanzaErrorResourceConstraint:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationConstraint );
break;
default:
m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationUnknownError );
break;
}
}
}
}
gloox::ClientBase
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition clientbase.h:79
gloox::ClientBase::registerIqHandler
void registerIqHandler(IqHandler *ih, int exttype)
Definition clientbase.cpp:1374
gloox::ClientBase::registerStanzaExtension
void registerStanzaExtension(StanzaExtension *ext)
Definition clientbase.cpp:1102
gloox::DataForm
An abstraction of a XEP-0004 Data Form.
Definition dataform.h:57
gloox::Error
A stanza error abstraction implemented as a StanzaExtension.
Definition error.h:35
gloox::Error::error
StanzaError error() const
Definition error.h:75
gloox::IQ
An abstraction of an IQ stanza.
Definition iq.h:34
gloox::IQ::subtype
IqType subtype() const
Definition iq.h:74
gloox::IQ::Set
@ Set
Definition iq.h:46
gloox::IQ::Error
@ Error
Definition iq.h:49
gloox::IQ::Result
@ Result
Definition iq.h:48
gloox::IQ::Get
@ Get
Definition iq.h:45
gloox::JID
An abstraction of a JID.
Definition jid.h:31
gloox::OOB
This is an abstraction of a jabber:x:oob namespace element or a jabber:iq:oob namespace element as sp...
Definition oob.h:36
gloox::RegistrationHandler
A virtual interface that receives events from an Registration object.
Definition registrationhandler.h:81
gloox::Registration::Query
A wrapping class for the XEP-0077 <query> element.
Definition registration.h:126
gloox::Registration::Query::registered
bool registered() const
Definition registration.h:186
gloox::Registration::Query::~Query
virtual ~Query()
Definition registration.cpp:149
gloox::Registration::Query::form
const DataForm * form() const
Definition registration.h:163
gloox::Registration::Query::Query
Query(DataForm *form)
Definition registration.cpp:26
gloox::Registration::Query::oob
const OOB * oob() const
Definition registration.h:198
gloox::Registration::Query::filterString
virtual const std::string & filterString() const
Definition registration.cpp:155
gloox::Registration::Query::instructions
const std::string & instructions() const
Definition registration.h:169
gloox::Registration::Query::fields
int fields() const
Definition registration.h:175
gloox::Registration::Query::tag
virtual Tag * tag() const
Definition registration.cpp:161
gloox::Registration::~Registration
virtual ~Registration()
Definition registration.cpp:240
gloox::Registration::FieldNick
@ FieldNick
Definition registration.h:102
gloox::Registration::FieldLast
@ FieldLast
Definition registration.h:106
gloox::Registration::FieldAddress
@ FieldAddress
Definition registration.h:108
gloox::Registration::FieldCity
@ FieldCity
Definition registration.h:109
gloox::Registration::FieldState
@ FieldState
Definition registration.h:110
gloox::Registration::FieldZip
@ FieldZip
Definition registration.h:111
gloox::Registration::FieldName
@ FieldName
Definition registration.h:104
gloox::Registration::FieldUrl
@ FieldUrl
Definition registration.h:113
gloox::Registration::FieldFirst
@ FieldFirst
Definition registration.h:105
gloox::Registration::FieldEmail
@ FieldEmail
Definition registration.h:107
gloox::Registration::FieldMisc
@ FieldMisc
Definition registration.h:115
gloox::Registration::FieldPassword
@ FieldPassword
Definition registration.h:103
gloox::Registration::FieldText
@ FieldText
Definition registration.h:116
gloox::Registration::FieldPhone
@ FieldPhone
Definition registration.h:112
gloox::Registration::FieldUsername
@ FieldUsername
Definition registration.h:101
gloox::Registration::FieldDate
@ FieldDate
Definition registration.h:114
gloox::Registration::changePassword
void changePassword(const std::string &username, const std::string &password)
Definition registration.cpp:293
gloox::Registration::handleIqID
virtual void handleIqID(const IQ &iq, int context)
Definition registration.cpp:315
gloox::Registration::removeRegistrationHandler
void removeRegistrationHandler()
Definition registration.cpp:310
gloox::Registration::registerRegistrationHandler
void registerRegistrationHandler(RegistrationHandler *rh)
Definition registration.cpp:305
gloox::Registration::fetchRegistrationFields
void fetchRegistrationFields()
Definition registration.cpp:250
gloox::Registration::createAccount
bool createAccount(int fields, const RegistrationFields &values)
Definition registration.cpp:260
gloox::Registration::removeAccount
void removeAccount()
Definition registration.cpp:283
gloox::Registration::Registration
Registration(ClientBase *parent, const JID &to)
Definition registration.cpp:219
gloox::StanzaExtension::StanzaExtension
StanzaExtension(int type)
Definition stanzaextension.h:211
gloox::Stanza::addExtension
void addExtension(const StanzaExtension *se)
Definition stanza.cpp:52
gloox::Stanza::error
const Error * error() const
Definition stanza.cpp:47
gloox::Stanza::from
const JID & from() const
Definition stanza.h:51
gloox::Stanza::findExtension
const StanzaExtension * findExtension(int type) const
Definition stanza.cpp:57
gloox::Tag
This is an abstraction of an XML element.
Definition tag.h:47
gloox::Tag::name
const std::string & name() const
Definition tag.h:394
gloox::Tag::addChild
void addChild(Tag *child)
Definition tag.cpp:424
gloox::Tag::setXmlns
bool setXmlns(const std::string &xmlns, const std::string &prefix=EmptyString)
Definition tag.cpp:522
gloox::prep::nodeprep
bool nodeprep(const std::string &node, std::string &out)
Definition prep.cpp:59
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::TagList
std::list< Tag * > TagList
Definition tag.h:31
gloox::RegistrationUnknownError
@ RegistrationUnknownError
Definition registrationhandler.h:67
gloox::RegistrationForbidden
@ RegistrationForbidden
Definition registrationhandler.h:50
gloox::RegistrationConflict
@ RegistrationConflict
Definition registrationhandler.h:37
gloox::RegistrationNotAllowed
@ RegistrationNotAllowed
Definition registrationhandler.h:62
gloox::RegistrationSuccess
@ RegistrationSuccess
Definition registrationhandler.h:34
gloox::RegistrationUnexpectedRequest
@ RegistrationUnexpectedRequest
Definition registrationhandler.h:54
gloox::RegistrationNotAuthorized
@ RegistrationNotAuthorized
Definition registrationhandler.h:38
gloox::RegistrationNotAcceptable
@ RegistrationNotAcceptable
Definition registrationhandler.h:36
gloox::RegistrationRequired
@ RegistrationRequired
Definition registrationhandler.h:52
gloox::RegistrationConstraint
@ RegistrationConstraint
Definition registrationhandler.h:64
gloox::RegistrationBadRequest
@ RegistrationBadRequest
Definition registrationhandler.h:43
gloox::XMLNS_X_OOB
const std::string XMLNS_X_OOB
Definition gloox.cpp:51
gloox::XMLNS_REGISTER
const std::string XMLNS_REGISTER
Definition gloox.cpp:41
gloox::XMLNS_X_DATA
const std::string XMLNS_X_DATA
Definition gloox.cpp:49
gloox::StanzaErrorResourceConstraint
@ StanzaErrorResourceConstraint
Definition gloox.h:934
gloox::StanzaErrorNotAllowed
@ StanzaErrorNotAllowed
Definition gloox.h:904
gloox::StanzaErrorForbidden
@ StanzaErrorForbidden
Definition gloox.h:884
gloox::StanzaErrorConflict
@ StanzaErrorConflict
Definition gloox.h:878
gloox::StanzaErrorRegistrationRequired
@ StanzaErrorRegistrationRequired
Definition gloox.h:924
gloox::StanzaErrorNotAuthorized
@ StanzaErrorNotAuthorized
Definition gloox.h:906
gloox::StanzaErrorUnexpectedRequest
@ StanzaErrorUnexpectedRequest
Definition gloox.h:945
gloox::StanzaErrorBadRequest
@ StanzaErrorBadRequest
Definition gloox.h:874
gloox::StanzaErrorNotAcceptable
@ StanzaErrorNotAcceptable
Definition gloox.h:900
gloox::ExtRegistration
@ ExtRegistration
Definition stanzaextension.h:76
gloox::StateConnected
@ StateConnected
Definition gloox.h:644
gloox::RegistrationFields
Definition registration.h:37
gloox::RegistrationFields::password
std::string password
Definition registration.h:40
gloox::RegistrationFields::username
std::string username
Definition registration.h:38