Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

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 "error.h"
00019 #include "prep.h"
00020 #include "oob.h"
00021 
00022 namespace gloox
00023 {
00024 
00025   // Registration::Query ----
00026   Registration::Query::Query( DataForm* form )
00027     : StanzaExtension( ExtRegistration ), m_form( form ), m_fields( 0 ), m_oob( 0 ),
00028       m_del( false ), m_reg( false )
00029   {
00030   }
00031 
00032   Registration::Query::Query( bool del )
00033     : StanzaExtension( ExtRegistration ), m_form( 0 ), m_fields( 0 ), m_oob( 0 ), m_del( del ),
00034       m_reg( false )
00035   {
00036   }
00037 
00038   Registration::Query::Query( int fields, const RegistrationFields& values )
00039     : StanzaExtension( ExtRegistration ), m_form( 0 ), m_fields( fields ), m_values( values ),
00040       m_oob( 0 ), m_del( false ), m_reg( false )
00041   {
00042   }
00043 
00044   Registration::Query::Query( const Tag* tag )
00045     : StanzaExtension( ExtRegistration ), m_form( 0 ), m_fields( 0 ), m_oob( 0 ), m_del( false ),
00046       m_reg( false )
00047   {
00048     if( !tag || tag->name() != "query" || tag->xmlns() != XMLNS_REGISTER )
00049       return;
00050 
00051     const TagList& l = tag->children();
00052     TagList::const_iterator it = l.begin();
00053     for( ; it != l.end(); ++it )
00054     {
00055       const std::string& name = (*it)->name();
00056       if( name == "instructions" )
00057         m_instructions = (*it)->cdata();
00058       else if( name == "remove" )
00059         m_del = true;
00060       else if( name == "registered" )
00061         m_reg = true;
00062       else if( name == "username" )
00063       {
00064         m_fields |= FieldUsername;
00065         m_values.username = (*it)->cdata();
00066       }
00067       else if( name == "nick" )
00068       {
00069         m_fields |= FieldNick;
00070         m_values.nick = (*it)->cdata();
00071       }
00072       else if( name == "password" )
00073       {
00074         m_fields |= FieldPassword;
00075         m_values.password = (*it)->cdata();
00076       }
00077       else if( name == "name" )
00078       {
00079         m_fields |= FieldName;
00080         m_values.name = (*it)->cdata();
00081       }
00082       else if( name == "first" )
00083       {
00084         m_fields |= FieldFirst;
00085         m_values.first = (*it)->cdata();
00086       }
00087       else if( name == "last" )
00088       {
00089         m_fields |= FieldLast;
00090         m_values.last = (*it)->cdata();
00091       }
00092       else if( name == "email" )
00093       {
00094         m_fields |= FieldEmail;
00095         m_values.email = (*it)->cdata();
00096       }
00097       else if( name == "address" )
00098       {
00099         m_fields |= FieldAddress;
00100         m_values.address = (*it)->cdata();
00101       }
00102       else if( name == "city" )
00103       {
00104         m_fields |= FieldCity;
00105         m_values.city = (*it)->cdata();
00106       }
00107       else if( name == "state" )
00108       {
00109         m_fields |= FieldState;
00110         m_values.state = (*it)->cdata();
00111       }
00112       else if( name == "zip" )
00113       {
00114         m_fields |= FieldZip;
00115         m_values.zip = (*it)->cdata();
00116       }
00117       else if( name == "phone" )
00118       {
00119         m_fields |= FieldPhone;
00120         m_values.phone = (*it)->cdata();
00121       }
00122       else if( name == "url" )
00123       {
00124         m_fields |= FieldUrl;
00125         m_values.url = (*it)->cdata();
00126       }
00127       else if( name == "date" )
00128       {
00129         m_fields |= FieldDate;
00130         m_values.date = (*it)->cdata();
00131       }
00132       else if( name == "misc" )
00133       {
00134         m_fields |= FieldMisc;
00135         m_values.misc = (*it)->cdata();
00136       }
00137       else if( name == "text" )
00138       {
00139         m_fields |= FieldText;
00140         m_values.text = (*it)->cdata();
00141       }
00142       else if( !m_form && name == "x" && (*it)->xmlns() == XMLNS_X_DATA )
00143         m_form = new DataForm( (*it) );
00144       else if( !m_oob && name == "x" && (*it)->xmlns() == XMLNS_X_OOB )
00145         m_oob = new OOB( (*it) );
00146     }
00147   }
00148 
00149   Registration::Query::~Query()
00150   {
00151     delete m_form;
00152     delete m_oob;
00153   }
00154 
00155   const std::string& Registration::Query::filterString() const
00156   {
00157     static const std::string filter = "/iq/query[@xmlns='" + XMLNS_REGISTER + "']";
00158     return filter;
00159   }
00160 
00161   Tag* Registration::Query::tag() const
00162   {
00163     Tag* t = new Tag( "query" );
00164     t->setXmlns( XMLNS_REGISTER );
00165 
00166     if( !m_instructions.empty() )
00167       new Tag( t, "instructions", m_instructions );
00168 
00169     if ( m_reg )
00170       new Tag( t, "registered" );
00171 
00172     if( m_form )
00173       t->addChild( m_form->tag() );
00174     else if( m_oob )
00175       t->addChild( m_oob->tag() );
00176     else if( m_del )
00177       new Tag( t, "remove" );
00178     else if( m_fields )
00179     {
00180       if( m_fields & FieldUsername )
00181         new Tag( t, "username", m_values.username );
00182       if( m_fields & FieldNick )
00183         new Tag( t, "nick", m_values.nick );
00184       if( m_fields & FieldPassword )
00185         new Tag( t, "password", m_values.password );
00186       if( m_fields & FieldName )
00187         new Tag( t, "name", m_values.name );
00188       if( m_fields & FieldFirst )
00189         new Tag( t, "first", m_values.first );
00190       if( m_fields & FieldLast )
00191         new Tag( t, "last", m_values.last );
00192       if( m_fields & FieldEmail )
00193         new Tag( t, "email", m_values.email );
00194       if( m_fields & FieldAddress )
00195         new Tag( t, "address", m_values.address );
00196       if( m_fields & FieldCity )
00197         new Tag( t, "city", m_values.city );
00198       if( m_fields & FieldState )
00199         new Tag( t, "state", m_values.state );
00200       if( m_fields & FieldZip )
00201         new Tag( t, "zip", m_values.zip );
00202       if( m_fields & FieldPhone )
00203         new Tag( t, "phone", m_values.phone );
00204       if( m_fields & FieldUrl )
00205         new Tag( t, "url", m_values.url );
00206       if( m_fields & FieldDate )
00207         new Tag( t, "date", m_values.date );
00208       if( m_fields & FieldMisc )
00209         new Tag( t, "misc", m_values.misc );
00210       if( m_fields & FieldText )
00211         new Tag( t, "text", m_values.text );
00212     }
00213 
00214     return t;
00215   }
00216   // ---- ~Registration::Query ----
00217 
00218   // ---- Registration ----
00219   Registration::Registration( ClientBase* parent, const JID& to )
00220     : m_parent( parent ), m_to( to ), m_registrationHandler( 0 )
00221   {
00222     init();
00223   }
00224 
00225   Registration::Registration( ClientBase* parent )
00226   : m_parent( parent ), m_registrationHandler( 0 )
00227   {
00228     init();
00229   }
00230 
00231   void Registration::init()
00232   {
00233     if( m_parent )
00234     {
00235       m_parent->registerIqHandler( this, ExtRegistration );
00236       m_parent->registerStanzaExtension( new Query() );
00237     }
00238   }
00239 
00240   Registration::~Registration()
00241   {
00242     if( m_parent )
00243     {
00244       m_parent->removeIqHandler( this, ExtRegistration );
00245       m_parent->removeIDHandler( this );
00246       m_parent->removeStanzaExtension( ExtRegistration );
00247     }
00248   }
00249 
00250   void Registration::fetchRegistrationFields()
00251   {
00252     if( !m_parent || m_parent->state() != StateConnected )
00253       return;
00254 
00255     IQ iq( IQ::Get, m_to );
00256     iq.addExtension( new Query() );
00257     m_parent->send( iq, this, FetchRegistrationFields );
00258   }
00259 
00260   bool Registration::createAccount( int fields, const RegistrationFields& values )
00261   {
00262     std::string username;
00263     if( !m_parent || m_parent->state() != StateConnected
00264          || !prep::nodeprep( values.username, username ) )
00265       return false;
00266 
00267     IQ iq( IQ::Set, m_to );
00268     iq.addExtension( new Query( fields, values ) );
00269     m_parent->send( iq, this, CreateAccount );
00270 
00271     return true;
00272   }
00273 
00274   void Registration::createAccount( DataForm* form )
00275   {
00276     if( !m_parent || m_parent->state() != StateConnected || !form )
00277       return;
00278 
00279     IQ iq( IQ::Set, m_to );
00280     iq.addExtension( new Query( form ) );
00281     m_parent->send( iq, this, CreateAccount );
00282   }
00283 
00284   void Registration::removeAccount()
00285   {
00286     if( !m_parent || !m_parent->authed() )
00287       return;
00288 
00289     IQ iq( IQ::Set, m_to );
00290     iq.addExtension( new Query( true ) );
00291     m_parent->send( iq, this, RemoveAccount );
00292   }
00293 
00294   void Registration::changePassword( const std::string& username, const std::string& password )
00295   {
00296     if( !m_parent || !m_parent->authed() || username.empty() )
00297       return;
00298 
00299     int fields = FieldUsername | FieldPassword;
00300     RegistrationFields rf;
00301     rf.username = username;
00302     rf.password = password;
00303     createAccount( fields, rf );
00304   }
00305 
00306   void Registration::registerRegistrationHandler( RegistrationHandler* rh )
00307   {
00308     m_registrationHandler = rh;
00309   }
00310 
00311   void Registration::removeRegistrationHandler()
00312   {
00313     m_registrationHandler = 0;
00314   }
00315 
00316   void Registration::handleIqID( const IQ& iq, int context )
00317   {
00318     if( !m_registrationHandler )
00319       return;
00320 
00321     if( iq.subtype() == IQ::Result )
00322     {
00323       switch( context )
00324       {
00325         case FetchRegistrationFields:
00326         {
00327           const Query* q = iq.findExtension<Query>( ExtRegistration );
00328           if( !q )
00329             return;
00330 
00331           if( q->registered() )
00332           {
00333             m_registrationHandler->handleAlreadyRegistered( iq.from() );
00334             break;
00335           }
00336 
00337           if( q->form() )
00338             m_registrationHandler->handleDataForm( iq.from(), *(q->form()) );
00339 
00340           if( q->oob() )
00341             m_registrationHandler->handleOOB( iq.from(), *(q->oob()) );
00342 
00343           m_registrationHandler->handleRegistrationFields( iq.from(), q->fields(), q->instructions() );
00344           break;
00345         }
00346 
00347         case CreateAccount:
00348         case ChangePassword:
00349         case RemoveAccount:
00350           m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationSuccess );
00351           break;
00352       }
00353     }
00354     else if( iq.subtype() == IQ::Error )
00355     {
00356       const Error* e = iq.error();
00357       if( !e )
00358         return;
00359 
00360       switch( e->error() )
00361       {
00362         case StanzaErrorConflict:
00363           m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationConflict );
00364           break;
00365         case StanzaErrorNotAcceptable:
00366           m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationNotAcceptable );
00367           break;
00368         case StanzaErrorBadRequest:
00369           m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationBadRequest );
00370           break;
00371         case StanzaErrorForbidden:
00372           m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationForbidden );
00373           break;
00374         case StanzaErrorRegistrationRequired:
00375           m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationRequired );
00376           break;
00377         case StanzaErrorUnexpectedRequest:
00378           m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationUnexpectedRequest );
00379           break;
00380         case StanzaErrorNotAuthorized:
00381           m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationNotAuthorized );
00382           break;
00383         case StanzaErrorNotAllowed:
00384           m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationNotAllowed );
00385           break;
00386         default:
00387           m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationUnknownError );
00388           break;
00389 
00390       }
00391     }
00392 
00393   }
00394 
00395 }

Generated on Sun Dec 28 22:10:17 2008 for gloox by  doxygen 1.4.1