registration.cpp

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

Generated on Fri Oct 10 15:26:11 2008 for gloox by  doxygen 1.5.6