00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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 )
00024 : m_parent( parent ), m_registrationHandler( 0 )
00025 {
00026 if( m_parent )
00027 m_parent->registerIqHandler( this, XMLNS_REGISTER );
00028 }
00029
00030 Registration::~Registration()
00031 {
00032 if( m_parent )
00033 m_parent->removeIqHandler( XMLNS_REGISTER );
00034 }
00035
00036 void Registration::fetchRegistrationFields()
00037 {
00038 if( !m_parent || m_parent->state() != STATE_CONNECTED )
00039 return;
00040
00041 std::string id = m_parent->getID();
00042
00043 Tag *iq = new Tag( "iq" );
00044 iq->addAttrib( "type", "get" );
00045 iq->addAttrib( "id", id );
00046 Tag *q = new Tag( "query" );
00047 q->addAttrib( "xmlns", XMLNS_REGISTER );
00048
00049 iq->addChild( q );
00050 m_parent->trackID( this, id, FETCH_REGISTRATION_FIELDS );
00051 m_parent->send( iq );
00052 }
00053
00054 void Registration::createAccount( int fields, fieldStruct values )
00055 {
00056 if( !m_parent )
00057 return;
00058
00059 const std::string id = m_parent->getID();
00060
00061 Tag *iq = new Tag( "iq" );
00062 iq->addAttrib( "id", id );
00063 iq->addAttrib( "type", "set" );
00064 Tag *q = new Tag( "query" );
00065 q->addAttrib( "xmlns", XMLNS_REGISTER );
00066
00067 if( fields & FIELD_USERNAME )
00068 q->addChild( new Tag( "username", Prep::nodeprep( values.username ) ) );
00069 if( fields & FIELD_NICK )
00070 q->addChild( new Tag( "nick", values.nick ) );
00071 if( fields & FIELD_PASSWORD )
00072 q->addChild( new Tag( "password", values.password ) );
00073 if( fields & FIELD_NAME )
00074 q->addChild( new Tag( "name", values.name ) );
00075 if( fields & FIELD_FIRST )
00076 q->addChild( new Tag( "first", values.first ) );
00077 if( fields & FIELD_LAST )
00078 q->addChild( new Tag( "last", values.last ) );
00079 if( fields & FIELD_EMAIL )
00080 q->addChild( new Tag( "email", values.email ) );
00081 if( fields & FIELD_ADDRESS )
00082 q->addChild( new Tag( "address", values.address ) );
00083 if( fields & FIELD_CITY )
00084 q->addChild( new Tag( "city", values.city ) );
00085 if( fields & FIELD_STATE )
00086 q->addChild( new Tag( "state", values.state ) );
00087 if( fields & FIELD_ZIP )
00088 q->addChild( new Tag( "zip", values.zip ) );
00089 if( fields & FIELD_PHONE )
00090 q->addChild( new Tag( "phone", values.phone ) );
00091 if( fields & FIELD_URL )
00092 q->addChild( new Tag( "url", values.url ) );
00093 if( fields & FIELD_DATE )
00094 q->addChild( new Tag( "date", values.date ) );
00095 if( fields & FIELD_MISC )
00096 q->addChild( new Tag( "misc", values.misc ) );
00097 if( fields & FIELD_TEXT )
00098 q->addChild( new Tag( "text", values.text ) );
00099
00100 iq->addChild( q );
00101 m_parent->trackID( this, id, CREATE_ACCOUNT );
00102 m_parent->send( iq );
00103 }
00104
00105 void Registration::removeAccount()
00106 {
00107 if( !m_parent || !m_parent->authed() )
00108 return;
00109
00110 const std::string id = m_parent->getID();
00111
00112 Tag *iq = new Tag( "iq" );
00113 iq->addAttrib( "type", "set" );
00114 iq->addAttrib( "id", id );
00115 iq->addAttrib( "from", m_parent->jid().full() );
00116 Tag *q = new Tag( "query" );
00117 q->addAttrib( "xmlns", XMLNS_REGISTER );
00118 q->addChild( new Tag( "remove" ) );
00119 iq->addChild( q );
00120
00121 m_parent->trackID( this, id, REMOVE_ACCOUNT );
00122 m_parent->send( iq );
00123 }
00124
00125 void Registration::changePassword( const std::string& password )
00126 {
00127 if( !m_parent || !m_parent->authed() )
00128 return;
00129
00130 const std::string id = m_parent->getID();
00131
00132 Tag *iq = new Tag( "iq" );
00133 iq->addAttrib( "type", "set" );
00134 iq->addAttrib( "id", id );
00135 iq->addAttrib( "to", m_parent->server() );
00136 Tag *q = new Tag( "query" );
00137 q->addAttrib( "xmlns", XMLNS_REGISTER );
00138 q->addChild( new Tag( "username", m_parent->username() ) );
00139 q->addChild( new Tag( "password", password ) );
00140 iq->addChild( q );
00141
00142 m_parent->trackID( this, id, CHANGE_PASSWORD );
00143 m_parent->send( iq );
00144 }
00145
00146 void Registration::registerRegistrationHandler( RegistrationHandler *rh )
00147 {
00148 m_registrationHandler = rh;
00149 }
00150
00151 void Registration::removeRegistrationHandler()
00152 {
00153 m_registrationHandler = 0;
00154 }
00155
00156 bool Registration::handleIq( Stanza *stanza )
00157 {
00158 if( stanza->subtype() == STANZA_IQ_ERROR )
00159 {
00160 Tag *e = stanza->findChild( "error" );
00161
00162 if( e->empty() || !m_registrationHandler )
00163 return false;
00164
00165 if( e->hasChild( "conflict" ) )
00166 m_registrationHandler->handleRegistrationResult( RegistrationHandler::REGISTRATION_CONFLICT );
00167 else if( e->hasChild( "not-acceptable" ) )
00168 m_registrationHandler->handleRegistrationResult( RegistrationHandler::REGISTRATION_NOT_ACCEPTABLE );
00169 else if( e->hasChild( "bad-request" ) )
00170 m_registrationHandler->handleRegistrationResult( RegistrationHandler::REGISTRATION_BAD_REQUEST );
00171 else if( e->hasChild( "forbidden" ) )
00172 m_registrationHandler->handleRegistrationResult( RegistrationHandler::REGISTRATION_FORBIDDEN );
00173 else if( e->hasChild( "registration-required" ) )
00174 m_registrationHandler->handleRegistrationResult(
00175 RegistrationHandler::REGISTRATION_REGISTRATION_REQUIRED );
00176 else if( e->hasChild( "unexpected-request" ) )
00177 m_registrationHandler->handleRegistrationResult(
00178 RegistrationHandler::REGISTRATION_UNEXPECTED_REQUEST );
00179 else if( e->hasChild( "not-authorized" ) )
00180 m_registrationHandler->handleRegistrationResult( RegistrationHandler::REGISTRATION_NOT_AUTHORIZED );
00181 else if( e->hasChild( "not-allowed" ) )
00182 m_registrationHandler->handleRegistrationResult( RegistrationHandler::REGISTRATION_NOT_ALLOWED );
00183 }
00184 return false;
00185 }
00186
00187 bool Registration::handleIqID( Stanza *stanza, int context )
00188 {
00189 if( stanza->subtype() != STANZA_IQ_RESULT )
00190 return false;
00191
00192 if( !m_registrationHandler )
00193 return false;
00194
00195 switch( context )
00196 {
00197 case FETCH_REGISTRATION_FIELDS:
00198 {
00199 Tag *q = stanza->findChild( "query" );
00200
00201 if( q->hasChild( "registered" ) )
00202 {
00203 m_registrationHandler->handleAlreadyRegistered();
00204 break;
00205 }
00206
00207 int fields = 0;
00208 std::string instructions;
00209
00210 if( q->hasChild( "username" ) )
00211 fields |= FIELD_USERNAME;
00212 if( q->hasChild( "nick" ) )
00213 fields |= FIELD_NICK;
00214 if( q->hasChild( "password" ) )
00215 fields |= FIELD_PASSWORD;
00216 if( q->hasChild( "name" ) )
00217 fields |= FIELD_NAME;
00218 if( q->hasChild( "first" ) )
00219 fields |= FIELD_FIRST;
00220 if( q->hasChild( "last" ) )
00221 fields |= FIELD_LAST;
00222 if( q->hasChild( "email" ) )
00223 fields |= FIELD_EMAIL;
00224 if( q->hasChild( "address" ) )
00225 fields |= FIELD_ADDRESS;
00226 if( q->hasChild( "city" ) )
00227 fields |= FIELD_CITY;
00228 if( q->hasChild( "state" ) )
00229 fields |= FIELD_STATE;
00230 if( q->hasChild( "zip" ) )
00231 fields |= FIELD_ZIP;
00232 if( q->hasChild( "phone" ) )
00233 fields |= FIELD_PHONE;
00234 if( q->hasChild( "url" ) )
00235 fields |= FIELD_URL;
00236 if( q->hasChild( "date" ) )
00237 fields |= FIELD_DATE;
00238 if( q->hasChild( "misc" ) )
00239 fields |= FIELD_MISC;
00240 if( q->hasChild( "text" ) )
00241 fields |= FIELD_TEXT;
00242 if( q->hasChild( "instructions" ) )
00243 instructions = q->findChild( "instructions" )->cdata();
00244
00245 m_registrationHandler->handleRegistrationFields( fields, instructions );
00246 break;
00247 }
00248
00249 case CREATE_ACCOUNT:
00250 m_registrationHandler->handleRegistrationResult( RegistrationHandler::REGISTRATION_SUCCESS );
00251 break;
00252
00253 case CHANGE_PASSWORD:
00254 m_registrationHandler->handleRegistrationResult( RegistrationHandler::REGISTRATION_SUCCESS );
00255 break;
00256
00257 case REMOVE_ACCOUNT:
00258 m_registrationHandler->handleRegistrationResult( RegistrationHandler::REGISTRATION_SUCCESS );
00259 break;
00260 }
00261 return false;
00262 }
00263
00264 }