registration.cpp

00001 /*
00002   Copyright (c) 2005-2006 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 
00020 namespace gloox
00021 {
00022 
00023   Registration::Registration( ClientBase *parent, const JID& to )
00024     : m_parent( parent ), m_to( to ), m_registrationHandler( 0 )
00025   {
00026     if( m_parent )
00027       m_parent->registerIqHandler( this, XMLNS_REGISTER );
00028   }
00029 
00030   Registration::Registration( ClientBase *parent )
00031   : m_parent( parent ), m_registrationHandler( 0 )
00032   {
00033     if( m_parent )
00034       m_parent->registerIqHandler( this, XMLNS_REGISTER );
00035   }
00036 
00037   Registration::~Registration()
00038   {
00039     if( m_parent )
00040       m_parent->removeIqHandler( XMLNS_REGISTER );
00041   }
00042 
00043   void Registration::fetchRegistrationFields()
00044   {
00045     if( !m_parent || m_parent->state() != StateConnected )
00046       return;
00047 
00048     std::string id = m_parent->getID();
00049 
00050     Tag *iq = new Tag( "iq" );
00051     if( !m_to.empty() )
00052       iq->addAttribute( "to", m_to.full() );
00053     iq->addAttribute( "type", "get" );
00054     iq->addAttribute( "id", id );
00055     Tag *q = new Tag( iq, "query" );
00056     q->addAttribute( "xmlns", XMLNS_REGISTER );
00057 
00058     m_parent->trackID( this, id, FETCH_REGISTRATION_FIELDS );
00059     m_parent->send( iq );
00060   }
00061 
00062   void Registration::createAccount( int fields, const fieldStruct& values )
00063   {
00064     if( !m_parent )
00065       return;
00066 
00067     const std::string id = m_parent->getID();
00068 
00069     Tag *iq = new Tag( "iq" );
00070     if( !m_to.empty() )
00071       iq->addAttribute( "to", m_to.full() );
00072     iq->addAttribute( "id", id );
00073     iq->addAttribute( "type", "set" );
00074     Tag *q = new Tag( iq, "query" );
00075     q->addAttribute( "xmlns", XMLNS_REGISTER );
00076 
00077     if( fields & FIELD_USERNAME )
00078       new Tag( q, "username", Prep::nodeprep( values.username ) );
00079     if( fields & FIELD_NICK )
00080       new Tag( q, "nick", values.nick );
00081     if( fields & FIELD_PASSWORD )
00082       new Tag( q, "password", values.password );
00083     if( fields & FIELD_NAME )
00084       new Tag( q, "name", values.name );
00085     if( fields & FIELD_FIRST )
00086       new Tag( q, "first", values.first );
00087     if( fields & FIELD_LAST )
00088       new Tag( q, "last", values.last );
00089     if( fields & FIELD_EMAIL )
00090       new Tag( q, "email", values.email );
00091     if( fields & FIELD_ADDRESS )
00092       new Tag( q, "address", values.address );
00093     if( fields & FIELD_CITY )
00094       new Tag( q, "city", values.city );
00095     if( fields & FIELD_STATE )
00096       new Tag( q, "state", values.state );
00097     if( fields & FIELD_ZIP )
00098       new Tag( q, "zip", values.zip );
00099     if( fields & FIELD_PHONE )
00100       new Tag( q, "phone", values.phone );
00101     if( fields & FIELD_URL )
00102       new Tag( q, "url", values.url );
00103     if( fields & FIELD_DATE )
00104       new Tag( q, "date", values.date );
00105     if( fields & FIELD_MISC )
00106       new Tag( q, "misc", values.misc );
00107     if( fields & FIELD_TEXT )
00108       new Tag( q, "text", values.text );
00109 
00110     m_parent->trackID( this, id, CREATE_ACCOUNT );
00111     m_parent->send( iq );
00112   }
00113 
00114   void Registration::createAccount( const DataForm& form )
00115   {
00116     const Tag *tmp = form.tag();
00117     if( tmp )
00118     {
00119       Tag *c = tmp->clone();
00120       m_parent->send( c );
00121     }
00122   }
00123 
00124   void Registration::removeAccount()
00125   {
00126     if( !m_parent || !m_parent->authed() )
00127       return;
00128 
00129     const std::string id = m_parent->getID();
00130 
00131     Tag *iq = new Tag( "iq" );
00132     if( !m_to.empty() )
00133       iq->addAttribute( "to", m_to.full() );
00134     iq->addAttribute( "type", "set" );
00135     iq->addAttribute( "id", id );
00136     iq->addAttribute( "from", m_parent->jid().full() );
00137     Tag *q = new Tag( iq, "query" );
00138     q->addAttribute( "xmlns", XMLNS_REGISTER );
00139     new Tag( q, "remove" );
00140 
00141     m_parent->trackID( this, id, REMOVE_ACCOUNT );
00142     m_parent->send( iq );
00143   }
00144 
00145   void Registration::changePassword( const std::string& username, const std::string& password )
00146   {
00147     if( !m_parent || !m_parent->authed() )
00148       return;
00149 
00150     const std::string id = m_parent->getID();
00151 
00152     Tag *iq = new Tag( "iq" );
00153     if( !m_to.empty() )
00154       iq->addAttribute( "to", m_to.full() );
00155     iq->addAttribute( "type", "set" );
00156     iq->addAttribute( "id", id );
00157     Tag *q = new Tag( iq, "query" );
00158     q->addAttribute( "xmlns", XMLNS_REGISTER );
00159     new Tag( q, "username", username );
00160     new Tag( q, "password", password );
00161 
00162     m_parent->trackID( this, id, CHANGE_PASSWORD );
00163     m_parent->send( iq );
00164   }
00165 
00166   void Registration::registerRegistrationHandler( RegistrationHandler *rh )
00167   {
00168     m_registrationHandler = rh;
00169   }
00170 
00171   void Registration::removeRegistrationHandler()
00172   {
00173     m_registrationHandler = 0;
00174   }
00175 
00176   bool Registration::handleIq( Stanza *stanza )
00177   {
00178     if( stanza->subtype() == StanzaIqError )
00179     {
00180       Tag *e = stanza->findChild( "error" );
00181 
00182       if( e->empty() || !m_registrationHandler )
00183         return false;
00184 
00185       if( e->hasChild( "conflict" ) || e->hasAttribute( "code", "409" ) )
00186         m_registrationHandler->handleRegistrationResult( stanza->from(),
00187             RegistrationHandler::REGISTRATION_CONFLICT );
00188       else if( e->hasChild( "not-acceptable" ) || e->hasAttribute( "code", "406" ) )
00189         m_registrationHandler->handleRegistrationResult( stanza->from(),
00190             RegistrationHandler::REGISTRATION_NOT_ACCEPTABLE );
00191       else if( e->hasChild( "bad-request" ) || e->hasAttribute( "code", "400" ) )
00192         m_registrationHandler->handleRegistrationResult( stanza->from(),
00193             RegistrationHandler::REGISTRATION_BAD_REQUEST );
00194       else if( e->hasChild( "forbidden" ) || e->hasAttribute( "code", "403" ) )
00195         m_registrationHandler->handleRegistrationResult( stanza->from(),
00196             RegistrationHandler::REGISTRATION_FORBIDDEN );
00197       else if( e->hasChild( "registration-required" ) || e->hasAttribute( "code", "407" ) )
00198         m_registrationHandler->handleRegistrationResult( stanza->from(),
00199             RegistrationHandler::REGISTRATION_REGISTRATION_REQUIRED );
00200       else if( e->hasChild( "unexpected-request" ) || e->hasAttribute( "code", "400" ) )
00201         m_registrationHandler->handleRegistrationResult( stanza->from(),
00202             RegistrationHandler::REGISTRATION_UNEXPECTED_REQUEST );
00203       else if( e->hasChild( "not-authorized" ) || e->hasAttribute( "code", "401" ) )
00204         m_registrationHandler->handleRegistrationResult( stanza->from(),
00205             RegistrationHandler::REGISTRATION_NOT_AUTHORIZED );
00206       else if( e->hasChild( "not-allowed" ) || e->hasAttribute( "code", "405" ) )
00207         m_registrationHandler->handleRegistrationResult( stanza->from(),
00208             RegistrationHandler::REGISTRATION_NOT_ALLOWED );
00209       else
00210         m_registrationHandler->handleRegistrationResult( stanza->from(),
00211           RegistrationHandler::UNKNOWN_ERROR );
00212     }
00213     return false;
00214   }
00215 
00216   bool Registration::handleIqID( Stanza *stanza, int context )
00217   {
00218     if( stanza->subtype() != StanzaIqResult )
00219       return false;
00220 
00221     if( !m_registrationHandler )
00222       return false;
00223 
00224     switch( context )
00225     {
00226       case FETCH_REGISTRATION_FIELDS:
00227       {
00228         Tag *q = stanza->findChild( "query" );
00229 
00230         if( q->hasChild( "registered" ) )
00231         {
00232           m_registrationHandler->handleAlreadyRegistered( stanza->from() );
00233           break;
00234         }
00235 
00236         if( q->hasChild( "x", "xmlns", XMLNS_X_DATA ) )
00237         {
00238           DataForm form( q->findChild( "x", "xmlns", XMLNS_X_DATA ) );
00239           m_registrationHandler->handleDataForm( stanza->from(), form );
00240         }
00241 
00242         if( q->hasChild( "x", "xmlns", XMLNS_X_OOB ) )
00243         {
00244           Tag *x = q->findChild( "x", "xmlns", XMLNS_X_OOB );
00245           std::string url;
00246           if( x->hasChild( "url" ) )
00247             url = x->findChild( "url" )->cdata();
00248 
00249           std::string desc;
00250           if( x->hasChild( "url" ) )
00251             url = x->findChild( "url" )->cdata();
00252 
00253           m_registrationHandler->handleOOB( stanza->from(), url, desc );
00254         }
00255 
00256         int fields = 0;
00257         std::string instructions;
00258 
00259         if( q->hasChild( "username" ) )
00260           fields |= FIELD_USERNAME;
00261         if( q->hasChild( "nick" ) )
00262           fields |= FIELD_NICK;
00263         if( q->hasChild( "password" ) )
00264           fields |= FIELD_PASSWORD;
00265         if( q->hasChild( "name" ) )
00266           fields |= FIELD_NAME;
00267         if( q->hasChild( "first" ) )
00268           fields |= FIELD_FIRST;
00269         if( q->hasChild( "last" ) )
00270             fields |= FIELD_LAST;
00271         if( q->hasChild( "email" ) )
00272           fields |= FIELD_EMAIL;
00273         if( q->hasChild( "address" ) )
00274           fields |= FIELD_ADDRESS;
00275         if( q->hasChild( "city" ) )
00276           fields |= FIELD_CITY;
00277         if( q->hasChild( "state" ) )
00278           fields |= FIELD_STATE;
00279         if( q->hasChild( "zip" ) )
00280           fields |= FIELD_ZIP;
00281         if( q->hasChild( "phone" ) )
00282           fields |= FIELD_PHONE;
00283         if( q->hasChild( "url" ) )
00284           fields |= FIELD_URL;
00285         if( q->hasChild( "date" ) )
00286           fields |= FIELD_DATE;
00287         if( q->hasChild( "misc" ) )
00288           fields |= FIELD_MISC;
00289         if( q->hasChild( "text" ) )
00290           fields |= FIELD_TEXT;
00291         if( q->hasChild( "instructions" ) )
00292           instructions = q->findChild( "instructions" )->cdata();
00293 
00294         m_registrationHandler->handleRegistrationFields( stanza->from(), fields, instructions );
00295         break;
00296       }
00297 
00298       case CREATE_ACCOUNT:
00299         m_registrationHandler->handleRegistrationResult( stanza->from(),
00300             RegistrationHandler::REGISTRATION_SUCCESS );
00301         break;
00302 
00303       case CHANGE_PASSWORD:
00304         m_registrationHandler->handleRegistrationResult( stanza->from(),
00305             RegistrationHandler::REGISTRATION_SUCCESS );
00306         break;
00307 
00308       case REMOVE_ACCOUNT:
00309         m_registrationHandler->handleRegistrationResult( stanza->from(),
00310             RegistrationHandler::REGISTRATION_SUCCESS );
00311         break;
00312     }
00313     return false;
00314   }
00315 
00316 }

Generated on Wed Dec 20 18:25:28 2006 for gloox by  doxygen 1.5.1