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 || m_parent->state() != StateConnected )
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     if( !m_parent || m_parent->state() != StateConnected )
00117       return;
00118 
00119     const std::string& id = m_parent->getID();
00120 
00121     Tag *iq = new Tag( "iq" );
00122     if( !m_to.empty() )
00123       iq->addAttribute( "to", m_to.full() );
00124     iq->addAttribute( "id", id );
00125     iq->addAttribute( "type", "set" );
00126     Tag *q = new Tag( iq, "query" );
00127     q->addAttribute( "xmlns", XMLNS_REGISTER );
00128     q->addChild( form.tag() );
00129 
00130     m_parent->trackID( this, id, CREATE_ACCOUNT );
00131     m_parent->send( iq );
00132   }
00133 
00134   void Registration::removeAccount()
00135   {
00136     if( !m_parent || !m_parent->authed() )
00137       return;
00138 
00139     const std::string id = m_parent->getID();
00140 
00141     Tag *iq = new Tag( "iq" );
00142     if( !m_to.empty() )
00143       iq->addAttribute( "to", m_to.full() );
00144     iq->addAttribute( "type", "set" );
00145     iq->addAttribute( "id", id );
00146     iq->addAttribute( "from", m_parent->jid().full() );
00147     Tag *q = new Tag( iq, "query" );
00148     q->addAttribute( "xmlns", XMLNS_REGISTER );
00149     new Tag( q, "remove" );
00150 
00151     m_parent->trackID( this, id, REMOVE_ACCOUNT );
00152     m_parent->send( iq );
00153   }
00154 
00155   void Registration::changePassword( const std::string& username, const std::string& password )
00156   {
00157     if( !m_parent || !m_parent->authed() )
00158       return;
00159 
00160     const std::string id = m_parent->getID();
00161 
00162     Tag *iq = new Tag( "iq" );
00163     if( !m_to.empty() )
00164       iq->addAttribute( "to", m_to.full() );
00165     iq->addAttribute( "type", "set" );
00166     iq->addAttribute( "id", id );
00167     Tag *q = new Tag( iq, "query" );
00168     q->addAttribute( "xmlns", XMLNS_REGISTER );
00169     new Tag( q, "username", username );
00170     new Tag( q, "password", password );
00171 
00172     m_parent->trackID( this, id, CHANGE_PASSWORD );
00173     m_parent->send( iq );
00174   }
00175 
00176   void Registration::registerRegistrationHandler( RegistrationHandler *rh )
00177   {
00178     m_registrationHandler = rh;
00179   }
00180 
00181   void Registration::removeRegistrationHandler()
00182   {
00183     m_registrationHandler = 0;
00184   }
00185 
00186   bool Registration::handleIq( Stanza *stanza )
00187   {
00188     if( stanza->subtype() == StanzaIqError )
00189     {
00190       Tag *e = stanza->findChild( "error" );
00191 
00192       if( e->empty() || !m_registrationHandler )
00193         return false;
00194 
00195       if( e->hasChild( "conflict" ) || e->hasAttribute( "code", "409" ) )
00196         m_registrationHandler->handleRegistrationResult( stanza->from(),
00197             RegistrationHandler::REGISTRATION_CONFLICT );
00198       else if( e->hasChild( "not-acceptable" ) || e->hasAttribute( "code", "406" ) )
00199         m_registrationHandler->handleRegistrationResult( stanza->from(),
00200             RegistrationHandler::REGISTRATION_NOT_ACCEPTABLE );
00201       else if( e->hasChild( "bad-request" ) || e->hasAttribute( "code", "400" ) )
00202         m_registrationHandler->handleRegistrationResult( stanza->from(),
00203             RegistrationHandler::REGISTRATION_BAD_REQUEST );
00204       else if( e->hasChild( "forbidden" ) || e->hasAttribute( "code", "403" ) )
00205         m_registrationHandler->handleRegistrationResult( stanza->from(),
00206             RegistrationHandler::REGISTRATION_FORBIDDEN );
00207       else if( e->hasChild( "registration-required" ) || e->hasAttribute( "code", "407" ) )
00208         m_registrationHandler->handleRegistrationResult( stanza->from(),
00209             RegistrationHandler::REGISTRATION_REGISTRATION_REQUIRED );
00210       else if( e->hasChild( "unexpected-request" ) || e->hasAttribute( "code", "400" ) )
00211         m_registrationHandler->handleRegistrationResult( stanza->from(),
00212             RegistrationHandler::REGISTRATION_UNEXPECTED_REQUEST );
00213       else if( e->hasChild( "not-authorized" ) || e->hasAttribute( "code", "401" ) )
00214         m_registrationHandler->handleRegistrationResult( stanza->from(),
00215             RegistrationHandler::REGISTRATION_NOT_AUTHORIZED );
00216       else if( e->hasChild( "not-allowed" ) || e->hasAttribute( "code", "405" ) )
00217         m_registrationHandler->handleRegistrationResult( stanza->from(),
00218             RegistrationHandler::REGISTRATION_NOT_ALLOWED );
00219       else
00220         m_registrationHandler->handleRegistrationResult( stanza->from(),
00221           RegistrationHandler::UNKNOWN_ERROR );
00222     }
00223     return false;
00224   }
00225 
00226   bool Registration::handleIqID( Stanza *stanza, int context )
00227   {
00228     if( stanza->subtype() != StanzaIqResult )
00229       return false;
00230 
00231     if( !m_registrationHandler )
00232       return false;
00233 
00234     switch( context )
00235     {
00236       case FETCH_REGISTRATION_FIELDS:
00237       {
00238         Tag *q = stanza->findChild( "query" );
00239 
00240         if( q->hasChild( "registered" ) )
00241         {
00242           m_registrationHandler->handleAlreadyRegistered( stanza->from() );
00243           break;
00244         }
00245 
00246         if( q->hasChild( "x", "xmlns", XMLNS_X_DATA ) )
00247         {
00248           DataForm form( q->findChild( "x", "xmlns", XMLNS_X_DATA ) );
00249           m_registrationHandler->handleDataForm( stanza->from(), form );
00250         }
00251 
00252         if( q->hasChild( "x", "xmlns", XMLNS_X_OOB ) )
00253         {
00254           Tag *x = q->findChild( "x", "xmlns", XMLNS_X_OOB );
00255           std::string url;
00256           if( x->hasChild( "url" ) )
00257             url = x->findChild( "url" )->cdata();
00258 
00259           std::string desc;
00260           if( x->hasChild( "url" ) )
00261             url = x->findChild( "url" )->cdata();
00262 
00263           m_registrationHandler->handleOOB( stanza->from(), url, desc );
00264         }
00265 
00266         int fields = 0;
00267         std::string instructions;
00268 
00269         if( q->hasChild( "username" ) )
00270           fields |= FIELD_USERNAME;
00271         if( q->hasChild( "nick" ) )
00272           fields |= FIELD_NICK;
00273         if( q->hasChild( "password" ) )
00274           fields |= FIELD_PASSWORD;
00275         if( q->hasChild( "name" ) )
00276           fields |= FIELD_NAME;
00277         if( q->hasChild( "first" ) )
00278           fields |= FIELD_FIRST;
00279         if( q->hasChild( "last" ) )
00280             fields |= FIELD_LAST;
00281         if( q->hasChild( "email" ) )
00282           fields |= FIELD_EMAIL;
00283         if( q->hasChild( "address" ) )
00284           fields |= FIELD_ADDRESS;
00285         if( q->hasChild( "city" ) )
00286           fields |= FIELD_CITY;
00287         if( q->hasChild( "state" ) )
00288           fields |= FIELD_STATE;
00289         if( q->hasChild( "zip" ) )
00290           fields |= FIELD_ZIP;
00291         if( q->hasChild( "phone" ) )
00292           fields |= FIELD_PHONE;
00293         if( q->hasChild( "url" ) )
00294           fields |= FIELD_URL;
00295         if( q->hasChild( "date" ) )
00296           fields |= FIELD_DATE;
00297         if( q->hasChild( "misc" ) )
00298           fields |= FIELD_MISC;
00299         if( q->hasChild( "text" ) )
00300           fields |= FIELD_TEXT;
00301         if( q->hasChild( "instructions" ) )
00302           instructions = q->findChild( "instructions" )->cdata();
00303 
00304         m_registrationHandler->handleRegistrationFields( stanza->from(), fields, instructions );
00305         break;
00306       }
00307 
00308       case CREATE_ACCOUNT:
00309         m_registrationHandler->handleRegistrationResult( stanza->from(),
00310             RegistrationHandler::REGISTRATION_SUCCESS );
00311         break;
00312 
00313       case CHANGE_PASSWORD:
00314         m_registrationHandler->handleRegistrationResult( stanza->from(),
00315             RegistrationHandler::REGISTRATION_SUCCESS );
00316         break;
00317 
00318       case REMOVE_ACCOUNT:
00319         m_registrationHandler->handleRegistrationResult( stanza->from(),
00320             RegistrationHandler::REGISTRATION_SUCCESS );
00321         break;
00322     }
00323     return false;
00324   }
00325 
00326 }

Generated on Tue May 1 14:20:20 2007 for gloox by  doxygen 1.5.1