registration.cpp

00001 /*
00002   Copyright (c) 2005-2007 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 #include "registration.h"
00015 
00016 #include "clientbase.h"
00017 #include "stanza.h"
00018 #include "prep.h"
00019 #include "oob.h"
00020 
00021 namespace gloox
00022 {
00023 
00024   Registration::Registration( ClientBase *parent, const JID& to )
00025     : m_parent( parent ), m_to( to ), m_registrationHandler( 0 )
00026   {
00027     init();
00028   }
00029 
00030   Registration::Registration( ClientBase *parent )
00031   : m_parent( parent ), m_registrationHandler( 0 )
00032   {
00033     init();
00034   }
00035 
00036   void Registration::init()
00037   {
00038     if( m_parent )
00039       m_parent->registerIqHandler( this, XMLNS_REGISTER );
00040   }
00041 
00042   Registration::~Registration()
00043   {
00044     if( m_parent )
00045       m_parent->removeIqHandler( XMLNS_REGISTER );
00046   }
00047 
00048   void Registration::fetchRegistrationFields()
00049   {
00050     if( !m_parent || m_parent->state() != StateConnected )
00051       return;
00052 
00053     const std::string& id = m_parent->getID();
00054 
00055     Tag *iq = new Tag( "iq" );
00056     if( m_to )
00057       iq->addAttribute( "to", m_to.full() );
00058     iq->addAttribute( "type", "get" );
00059     iq->addAttribute( "id", id );
00060     Tag *q = new Tag( iq, "query" );
00061     q->addAttribute( "xmlns", XMLNS_REGISTER );
00062 
00063     m_parent->trackID( this, id, FetchRegistrationFields );
00064     m_parent->send( iq );
00065   }
00066 
00067   void Registration::createAccount( int fields, const RegistrationFields& values )
00068   {
00069     if( !m_parent || m_parent->state() != StateConnected )
00070       return;
00071 
00072     const std::string& id = m_parent->getID();
00073 
00074     Tag *iq = new Tag( "iq" );
00075     if( m_to )
00076       iq->addAttribute( "to", m_to.full() );
00077     iq->addAttribute( "id", id );
00078     iq->addAttribute( "type", "set" );
00079     Tag *q = new Tag( iq, "query" );
00080     q->addAttribute( "xmlns", XMLNS_REGISTER );
00081 
00082     if( fields & FieldUsername )
00083       new Tag( q, "username", prep::nodeprep( values.username ) );
00084     if( fields & FieldNick )
00085       new Tag( q, "nick", values.nick );
00086     if( fields & FieldPassword )
00087       new Tag( q, "password", values.password );
00088     if( fields & FieldName )
00089       new Tag( q, "name", values.name );
00090     if( fields & FieldFirst )
00091       new Tag( q, "first", values.first );
00092     if( fields & FieldLast )
00093       new Tag( q, "last", values.last );
00094     if( fields & FieldEmail )
00095       new Tag( q, "email", values.email );
00096     if( fields & FieldAddress )
00097       new Tag( q, "address", values.address );
00098     if( fields & FieldCity )
00099       new Tag( q, "city", values.city );
00100     if( fields & FieldState )
00101       new Tag( q, "state", values.state );
00102     if( fields & FieldZip )
00103       new Tag( q, "zip", values.zip );
00104     if( fields & FieldPhone )
00105       new Tag( q, "phone", values.phone );
00106     if( fields & FieldUrl )
00107       new Tag( q, "url", values.url );
00108     if( fields & FieldDate )
00109       new Tag( q, "date", values.date );
00110     if( fields & FieldMisc )
00111       new Tag( q, "misc", values.misc );
00112     if( fields & FieldText )
00113       new Tag( q, "text", values.text );
00114 
00115     m_parent->trackID( this, id, CreateAccount );
00116     m_parent->send( iq );
00117   }
00118 
00119   void Registration::createAccount( const DataForm& form )
00120   {
00121     if( !m_parent || m_parent->state() != StateConnected )
00122       return;
00123 
00124     const std::string& id = m_parent->getID();
00125 
00126     Tag *iq = new Tag( "iq" );
00127     if( m_to )
00128       iq->addAttribute( "to", m_to.full() );
00129     iq->addAttribute( "id", id );
00130     iq->addAttribute( "type", "set" );
00131     Tag *q = new Tag( iq, "query" );
00132     q->addAttribute( "xmlns", XMLNS_REGISTER );
00133     q->addChild( form.tag() );
00134 
00135     m_parent->trackID( this, id, CreateAccount );
00136     m_parent->send( iq );
00137   }
00138 
00139   void Registration::removeAccount()
00140   {
00141     if( !m_parent || !m_parent->authed() )
00142       return;
00143 
00144     const std::string& id = m_parent->getID();
00145 
00146     Tag *iq = new Tag( "iq" );
00147     if( m_to )
00148       iq->addAttribute( "to", m_to.full() );
00149     iq->addAttribute( "type", "set" );
00150     iq->addAttribute( "id", id );
00151     iq->addAttribute( "from", m_parent->jid().full() );
00152     Tag *q = new Tag( iq, "query" );
00153     q->addAttribute( "xmlns", XMLNS_REGISTER );
00154     new Tag( q, "remove" );
00155 
00156     m_parent->trackID( this, id, RemoveAccount );
00157     m_parent->send( iq );
00158   }
00159 
00160   void Registration::changePassword( const std::string& username, const std::string& password )
00161   {
00162     if( !m_parent || !m_parent->authed() )
00163       return;
00164 
00165     const std::string& id = m_parent->getID();
00166 
00167     Tag *iq = new Tag( "iq" );
00168     if( m_to )
00169       iq->addAttribute( "to", m_to.full() );
00170     iq->addAttribute( "type", "set" );
00171     iq->addAttribute( "id", id );
00172     Tag *q = new Tag( iq, "query" );
00173     q->addAttribute( "xmlns", XMLNS_REGISTER );
00174     new Tag( q, "username", username );
00175     new Tag( q, "password", password );
00176 
00177     m_parent->trackID( this, id, ChangePassword );
00178     m_parent->send( iq );
00179   }
00180 
00181   void Registration::registerRegistrationHandler( RegistrationHandler *rh )
00182   {
00183     m_registrationHandler = rh;
00184   }
00185 
00186   void Registration::removeRegistrationHandler()
00187   {
00188     m_registrationHandler = 0;
00189   }
00190 
00191   bool Registration::handleIqID( Stanza* stanza, int context )
00192   {
00193     if( !m_registrationHandler )
00194       return false;
00195 
00196     if( stanza->subtype() == StanzaIqResult )
00197     {
00198       switch( context )
00199       {
00200         case FetchRegistrationFields:
00201         {
00202           Tag *q = stanza->findChild( "query" );
00203           if( !q )
00204             return false;
00205 
00206           if( q->hasChild( "registered" ) )
00207           {
00208             m_registrationHandler->handleAlreadyRegistered( stanza->from() );
00209             break;
00210           }
00211 
00212           if( q->hasChild( "x", "xmlns", XMLNS_X_DATA ) )
00213           {
00214             DataForm form( q->findChild( "x", "xmlns", XMLNS_X_DATA ) );
00215             m_registrationHandler->handleDataForm( stanza->from(), form );
00216           }
00217 
00218           if( q->hasChild( "x", "xmlns", XMLNS_X_OOB ) )
00219           {
00220             OOB oob( q->findChild( "x", "xmlns", XMLNS_X_OOB ) );
00221             m_registrationHandler->handleOOB( stanza->from(), oob );
00222           }
00223 
00224           int fields = 0;
00225           std::string instructions;
00226 
00227           if( q->hasChild( "username" ) )
00228             fields |= FieldUsername;
00229           if( q->hasChild( "nick" ) )
00230             fields |= FieldNick;
00231           if( q->hasChild( "password" ) )
00232             fields |= FieldPassword;
00233           if( q->hasChild( "name" ) )
00234             fields |= FieldName;
00235           if( q->hasChild( "first" ) )
00236             fields |= FieldFirst;
00237           if( q->hasChild( "last" ) )
00238             fields |= FieldLast;
00239           if( q->hasChild( "email" ) )
00240             fields |= FieldEmail;
00241           if( q->hasChild( "address" ) )
00242             fields |= FieldAddress;
00243           if( q->hasChild( "city" ) )
00244             fields |= FieldCity;
00245           if( q->hasChild( "state" ) )
00246             fields |= FieldState;
00247           if( q->hasChild( "zip" ) )
00248             fields |= FieldZip;
00249           if( q->hasChild( "phone" ) )
00250             fields |= FieldPhone;
00251           if( q->hasChild( "url" ) )
00252             fields |= FieldUrl;
00253           if( q->hasChild( "date" ) )
00254             fields |= FieldDate;
00255           if( q->hasChild( "misc" ) )
00256             fields |= FieldMisc;
00257           if( q->hasChild( "text" ) )
00258             fields |= FieldText;
00259           if( q->hasChild( "instructions" ) )
00260             instructions = q->findChild( "instructions" )->cdata();
00261 
00262           m_registrationHandler->handleRegistrationFields( stanza->from(), fields, instructions );
00263           break;
00264         }
00265 
00266         case CreateAccount:
00267           m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationSuccess );
00268           break;
00269 
00270         case ChangePassword:
00271           m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationSuccess );
00272           break;
00273 
00274         case RemoveAccount:
00275           m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationSuccess );
00276           break;
00277       }
00278     }
00279     else if( stanza->subtype() == StanzaIqError )
00280     {
00281       Tag *e = stanza->findChild( "error" );
00282 
00283       if( !e )
00284         return false;
00285 
00286       if( e->hasChild( "conflict" ) || e->hasAttribute( "code", "409" ) )
00287         m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationConflict );
00288       else if( e->hasChild( "not-acceptable" ) || e->hasAttribute( "code", "406" ) )
00289         m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationNotAcceptable );
00290       else if( e->hasChild( "bad-request" ) || e->hasAttribute( "code", "400" ) )
00291         m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationBadRequest );
00292       else if( e->hasChild( "forbidden" ) || e->hasAttribute( "code", "403" ) )
00293         m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationForbidden );
00294       else if( e->hasChild( "registration-required" ) || e->hasAttribute( "code", "407" ) )
00295         m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationRequired );
00296       else if( e->hasChild( "unexpected-request" ) || e->hasAttribute( "code", "400" ) )
00297         m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationUnexpectedRequest );
00298       else if( e->hasChild( "not-authorized" ) || e->hasAttribute( "code", "401" ) )
00299         m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationNotAuthorized );
00300       else if( e->hasChild( "not-allowed" ) || e->hasAttribute( "code", "405" ) )
00301         m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationNotAllowed );
00302       else
00303         m_registrationHandler->handleRegistrationResult( stanza->from(), RegistrationUnknownError );
00304     }
00305 
00306     return false;
00307   }
00308 
00309 }

Generated on Sat Nov 10 08:50:27 2007 for gloox by  doxygen 1.5.3-20071008