comparison libpurple/protocols/mxit/actions.c @ 31957:67c2d695e8d5

Move the PIN-change option into a separate PluginAction.
author andrew.victor@mxit.com
date Mon, 11 Apr 2011 22:16:34 +0000
parents cce18a0ff43a
children efed7d3c5574
comparison
equal deleted inserted replaced
31956:cce18a0ff43a 31957:67c2d695e8d5
39 * The user has selected to change their profile. 39 * The user has selected to change their profile.
40 * 40 *
41 * @param gc The connection object 41 * @param gc The connection object
42 * @param fields The fields from the request pop-up 42 * @param fields The fields from the request pop-up
43 */ 43 */
44 static void mxit_cb_set_profile( PurpleConnection* gc, PurpleRequestFields* fields ) 44 static void mxit_profile_cb( PurpleConnection* gc, PurpleRequestFields* fields )
45 { 45 {
46 struct MXitSession* session = (struct MXitSession*) gc->proto_data; 46 struct MXitSession* session = (struct MXitSession*) gc->proto_data;
47 PurpleRequestField* field = NULL; 47 PurpleRequestField* field = NULL;
48 const char* pin = NULL;
49 const char* pin2 = NULL;
50 const char* name = NULL; 48 const char* name = NULL;
51 const char* bday = NULL; 49 const char* bday = NULL;
52 const char* err = NULL; 50 const char* err = NULL;
53 int len; 51
54 int i; 52 purple_debug_info( MXIT_PLUGIN_ID, "mxit_profile_cb\n" );
55
56 purple_debug_info( MXIT_PLUGIN_ID, "mxit_cb_set_profile\n" );
57 53
58 if ( !PURPLE_CONNECTION_IS_VALID( gc ) ) { 54 if ( !PURPLE_CONNECTION_IS_VALID( gc ) ) {
59 purple_debug_error( MXIT_PLUGIN_ID, "Unable to update profile; account offline.\n" ); 55 purple_debug_error( MXIT_PLUGIN_ID, "Unable to update profile; account offline.\n" );
60 return; 56 return;
61 }
62
63 /* validate pin */
64 pin = purple_request_fields_get_string( fields, "pin" );
65 if ( !pin ) {
66 err = _( "The PIN you entered is invalid." );
67 goto out;
68 }
69 len = strlen( pin );
70 if ( ( len < 4 ) || ( len > 10 ) ) {
71 err = _( "The PIN you entered has an invalid length [4-10]." );
72 goto out;
73 }
74 for ( i = 0; i < len; i++ ) {
75 if ( !g_ascii_isdigit( pin[i] ) ) {
76 err = _( "The PIN is invalid. It should only consist of digits [0-9]." );
77 goto out;
78 }
79 }
80 pin2 = purple_request_fields_get_string( fields, "pin2" );
81 if ( ( !pin2 ) || ( strcmp( pin, pin2 ) != 0 ) ) {
82 err = _( "The two PINs you entered do not match." );
83 goto out;
84 } 57 }
85 58
86 /* validate name */ 59 /* validate name */
87 name = purple_request_fields_get_string( fields, "name" ); 60 name = purple_request_fields_get_string( fields, "name" );
88 if ( ( !name ) || ( strlen( name ) < 3 ) ) { 61 if ( ( !name ) || ( strlen( name ) < 3 ) ) {
102 struct MXitProfile* profile = session->profile; 75 struct MXitProfile* profile = session->profile;
103 GString* attributes = g_string_sized_new( 128 ); 76 GString* attributes = g_string_sized_new( 128 );
104 char attrib[512]; 77 char attrib[512];
105 unsigned int acount = 0; 78 unsigned int acount = 0;
106 79
107 /* all good, so we can now update the profile */
108
109 /* update pin */
110 purple_account_set_password( session->acc, pin );
111 g_free( session->encpwd );
112 session->encpwd = mxit_encrypt_password( session );
113 80
114 /* update name */ 81 /* update name */
115 g_strlcpy( profile->nickname, name, sizeof( profile->nickname ) ); 82 g_strlcpy( profile->nickname, name, sizeof( profile->nickname ) );
116 g_snprintf( attrib, sizeof( attrib ), "\01%s\01%i\01%s", CP_PROFILE_FULLNAME, CP_PROFILE_TYPE_UTF8, profile->nickname ); 83 g_snprintf( attrib, sizeof( attrib ), "\01%s\01%i\01%s", CP_PROFILE_FULLNAME, CP_PROFILE_TYPE_UTF8, profile->nickname );
117 g_string_append( attributes, attrib ); 84 g_string_append( attributes, attrib );
220 g_snprintf( attrib, sizeof( attrib ), "\01%s\01%i\01%i", CP_PROFILE_FLAGS, CP_PROFILE_TYPE_LONG, profile->flags); 187 g_snprintf( attrib, sizeof( attrib ), "\01%s\01%i\01%i", CP_PROFILE_FLAGS, CP_PROFILE_TYPE_LONG, profile->flags);
221 g_string_append( attributes, attrib ); 188 g_string_append( attributes, attrib );
222 acount++; 189 acount++;
223 190
224 /* send the profile update to MXit */ 191 /* send the profile update to MXit */
225 mxit_send_extprofile_update( session, session->encpwd, acount, attributes->str ); 192 mxit_send_extprofile_update( session, NULL, acount, attributes->str );
226 g_string_free( attributes, TRUE ); 193 g_string_free( attributes, TRUE );
227 } 194 }
228 else { 195 else {
229 /* show error to user */ 196 /* show error to user */
230 mxit_popup( PURPLE_NOTIFY_MSG_ERROR, _( "Profile Update Error" ), err ); 197 mxit_popup( PURPLE_NOTIFY_MSG_ERROR, _( "Profile Update Error" ), err );
235 /*------------------------------------------------------------------------ 202 /*------------------------------------------------------------------------
236 * Display and update the user's profile. 203 * Display and update the user's profile.
237 * 204 *
238 * @param action The action object 205 * @param action The action object
239 */ 206 */
240 static void mxit_cb_action_profile( PurplePluginAction* action ) 207 static void mxit_profile_action( PurplePluginAction* action )
241 { 208 {
242 PurpleConnection* gc = (PurpleConnection*) action->context; 209 PurpleConnection* gc = (PurpleConnection*) action->context;
243 struct MXitSession* session = (struct MXitSession*) gc->proto_data; 210 struct MXitSession* session = (struct MXitSession*) gc->proto_data;
244 struct MXitProfile* profile = session->profile; 211 struct MXitProfile* profile = session->profile;
245 212
246 PurpleRequestFields* fields = NULL; 213 PurpleRequestFields* fields = NULL;
247 PurpleRequestField* field = NULL; 214 PurpleRequestField* field = NULL;
248 215
249 purple_debug_info( MXIT_PLUGIN_ID, "mxit_cb_action_profile\n" ); 216 purple_debug_info( MXIT_PLUGIN_ID, "mxit_profile_action\n" );
250 217
251 /* ensure that we actually have the user's profile information */ 218 /* ensure that we actually have the user's profile information */
252 if ( !profile ) { 219 if ( !profile ) {
253 /* no profile information yet, so we cannot update */ 220 /* no profile information yet, so we cannot update */
254 mxit_popup( PURPLE_NOTIFY_MSG_WARNING, _( "Profile" ), _( "Your profile information is not yet retrieved. Please try again later." ) ); 221 mxit_popup( PURPLE_NOTIFY_MSG_WARNING, _( "Profile" ), _( "Your profile information is not yet retrieved. Please try again later." ) );
255 return; 222 return;
256 } 223 }
257 224
258 fields = purple_request_fields_new(); 225 fields = purple_request_fields_new();
259 226
260 /* Security information - PIN, etc */
261 {
262 PurpleRequestFieldGroup* security_group = purple_request_field_group_new( "PIN" );
263
264 /* pin */
265 field = purple_request_field_string_new( "pin", _( "PIN" ), session->acc->password, FALSE );
266 purple_request_field_string_set_masked( field, TRUE );
267 purple_request_field_group_add_field( security_group, field );
268
269 field = purple_request_field_string_new( "pin2", _( "Verify PIN" ), session->acc->password, FALSE );
270 purple_request_field_string_set_masked( field, TRUE );
271 purple_request_field_group_add_field( security_group, field );
272
273 purple_request_fields_add_group( fields, security_group );
274 }
275
276 /* Public information - what other users can see */ 227 /* Public information - what other users can see */
277 { 228 {
278 PurpleRequestFieldGroup* public_group = purple_request_field_group_new( "Public information" ); 229 PurpleRequestFieldGroup* public_group = purple_request_field_group_new( "Public information" );
279 230
280 /* display name */ 231 /* display name */
339 purple_request_fields_add_group( fields, private_group ); 290 purple_request_fields_add_group( fields, private_group );
340 } 291 }
341 292
342 /* (reference: "libpurple/request.h") */ 293 /* (reference: "libpurple/request.h") */
343 purple_request_fields( gc, _( "Profile" ), _( "Update your MXit Profile" ), NULL, fields, _( "Set" ), 294 purple_request_fields( gc, _( "Profile" ), _( "Update your MXit Profile" ), NULL, fields, _( "Set" ),
344 G_CALLBACK( mxit_cb_set_profile ), _( "Cancel" ), NULL, purple_connection_get_account( gc ), NULL, NULL, gc ); 295 G_CALLBACK( mxit_profile_cb ), _( "Cancel" ), NULL, purple_connection_get_account( gc ), NULL, NULL, gc );
296 }
297
298
299 /*------------------------------------------------------------------------
300 * The user has selected to change their PIN.
301 *
302 * @param gc The connection object
303 * @param fields The fields from the request pop-up
304 */
305 static void mxit_change_pin_cb( PurpleConnection* gc, PurpleRequestFields* fields )
306 {
307 struct MXitSession* session = (struct MXitSession*) gc->proto_data;
308 const char* pin = NULL;
309 const char* pin2 = NULL;
310 const char* err = NULL;
311 int len;
312 int i;
313
314 if ( !PURPLE_CONNECTION_IS_VALID( gc ) ) {
315 purple_debug_error( MXIT_PLUGIN_ID, "Unable to update PIN; account offline.\n" );
316 return;
317 }
318
319 /* validate pin */
320 pin = purple_request_fields_get_string( fields, "pin" );
321 if ( !pin ) {
322 err = _( "The PIN you entered is invalid." );
323 goto out;
324 }
325 len = strlen( pin );
326 if ( ( len < 4 ) || ( len > 10 ) ) {
327 err = _( "The PIN you entered has an invalid length [4-10]." );
328 goto out;
329 }
330 for ( i = 0; i < len; i++ ) {
331 if ( !g_ascii_isdigit( pin[i] ) ) {
332 err = _( "The PIN is invalid. It should only consist of digits [0-9]." );
333 goto out;
334 }
335 }
336 pin2 = purple_request_fields_get_string( fields, "pin2" );
337 if ( ( !pin2 ) || ( strcmp( pin, pin2 ) != 0 ) ) {
338 err = _( "The two PINs you entered do not match." );
339 goto out;
340 }
341
342 out:
343 if ( !err ) {
344 /* update PIN in account */
345 purple_account_set_password( session->acc, pin );
346
347 /* update session object */
348 g_free( session->encpwd );
349 session->encpwd = mxit_encrypt_password( session );
350
351 /* send the update request to MXit */
352 mxit_send_extprofile_update( session, session->encpwd, 0, NULL );
353 }
354 else {
355 /* show error to user */
356 mxit_popup( PURPLE_NOTIFY_MSG_ERROR, _( "PIN Update Error" ), err );
357 }
358 }
359
360
361 /*------------------------------------------------------------------------
362 * Enable the user to change their PIN.
363 *
364 * @param action The action object
365 */
366 static void mxit_change_pin_action( PurplePluginAction* action )
367 {
368 PurpleConnection* gc = (PurpleConnection*) action->context;
369 struct MXitSession* session = (struct MXitSession*) gc->proto_data;
370
371 PurpleRequestFields* fields = NULL;
372 PurpleRequestFieldGroup* group = NULL;
373 PurpleRequestField* field = NULL;
374
375 purple_debug_info( MXIT_PLUGIN_ID, "mxit_change_pin_action\n" );
376
377 fields = purple_request_fields_new();
378 group = purple_request_field_group_new(NULL);
379 purple_request_fields_add_group(fields, group);
380
381 /* pin */
382 field = purple_request_field_string_new( "pin", _( "PIN" ), session->acc->password, FALSE );
383 purple_request_field_string_set_masked( field, TRUE );
384 purple_request_field_group_add_field( group, field );
385
386 /* verify pin */
387 field = purple_request_field_string_new( "pin2", _( "Verify PIN" ), session->acc->password, FALSE );
388 purple_request_field_string_set_masked( field, TRUE );
389 purple_request_field_group_add_field( group, field );
390
391 /* (reference: "libpurple/request.h") */
392 purple_request_fields( gc, _( "Change PIN" ), _( "Change MXit PIN" ), NULL, fields, _( "Set" ),
393 G_CALLBACK( mxit_change_pin_cb ), _( "Cancel" ), NULL, purple_connection_get_account( gc ), NULL, NULL, gc );
345 } 394 }
346 395
347 396
348 /*------------------------------------------------------------------------ 397 /*------------------------------------------------------------------------
349 * Display the current splash-screen, or a notification pop-up if one is not available. 398 * Display the current splash-screen, or a notification pop-up if one is not available.
350 * 399 *
351 * @param action The action object 400 * @param action The action object
352 */ 401 */
353 static void mxit_cb_action_splash( PurplePluginAction* action ) 402 static void mxit_splash_action( PurplePluginAction* action )
354 { 403 {
355 PurpleConnection* gc = (PurpleConnection*) action->context; 404 PurpleConnection* gc = (PurpleConnection*) action->context;
356 struct MXitSession* session = (struct MXitSession*) gc->proto_data; 405 struct MXitSession* session = (struct MXitSession*) gc->proto_data;
357 406
358 if ( splash_current( session ) != NULL ) 407 if ( splash_current( session ) != NULL )
365 /*------------------------------------------------------------------------ 414 /*------------------------------------------------------------------------
366 * Display info about the plugin. 415 * Display info about the plugin.
367 * 416 *
368 * @param action The action object 417 * @param action The action object
369 */ 418 */
370 static void mxit_cb_action_about( PurplePluginAction* action ) 419 static void mxit_about_action( PurplePluginAction* action )
371 { 420 {
372 char version[256]; 421 char version[256];
373 422
374 g_snprintf( version, sizeof( version ), 423 g_snprintf( version, sizeof( version ),
375 "MXit Client Protocol v%i.%i\n\n" 424 "MXit Client Protocol v%i.%i\n\n"
385 /*------------------------------------------------------------------------ 434 /*------------------------------------------------------------------------
386 * Request list of suggested friends. 435 * Request list of suggested friends.
387 * 436 *
388 * @param action The action object 437 * @param action The action object
389 */ 438 */
390 static void mxit_cb_suggested_friends( PurplePluginAction* action ) 439 static void mxit_suggested_friends_action( PurplePluginAction* action )
391 { 440 {
392 PurpleConnection* gc = (PurpleConnection*) action->context; 441 PurpleConnection* gc = (PurpleConnection*) action->context;
393 struct MXitSession* session = (struct MXitSession*) gc->proto_data; 442 struct MXitSession* session = (struct MXitSession*) gc->proto_data;
394 const char* profilelist[] = { 443 const char* profilelist[] = {
395 CP_PROFILE_BIRTHDATE, CP_PROFILE_GENDER, CP_PROFILE_FULLNAME, CP_PROFILE_FIRSTNAME, 444 CP_PROFILE_BIRTHDATE, CP_PROFILE_GENDER, CP_PROFILE_FULLNAME, CP_PROFILE_FIRSTNAME,
418 /*------------------------------------------------------------------------ 467 /*------------------------------------------------------------------------
419 * Display the search input form. 468 * Display the search input form.
420 * 469 *
421 * @param action The action object 470 * @param action The action object
422 */ 471 */
423 static void mxit_cb_search_begin( PurplePluginAction* action ) 472 static void mxit_user_search_action( PurplePluginAction* action )
424 { 473 {
425 PurpleConnection* gc = (PurpleConnection*) action->context; 474 PurpleConnection* gc = (PurpleConnection*) action->context;
426 475
427 purple_request_input( gc, _( "Search for user" ), 476 purple_request_input( gc, _( "Search for user" ),
428 _( "Search for a MXit contact" ), 477 _( "Search for a MXit contact" ),
446 { 495 {
447 PurplePluginAction* action = NULL; 496 PurplePluginAction* action = NULL;
448 GList* m = NULL; 497 GList* m = NULL;
449 498
450 /* display / change profile */ 499 /* display / change profile */
451 action = purple_plugin_action_new( _( "Change Profile..." ), mxit_cb_action_profile ); 500 action = purple_plugin_action_new( _( "Change Profile..." ), mxit_profile_action );
452 m = g_list_append( m, action ); 501 m = g_list_append( m, action );
453 502
503 /* change PIN */
504 action = purple_plugin_action_new( _( "Change PIN..." ), mxit_change_pin_action );
505 m = g_list_append( m, action );
506
507 /* suggested friends */
508 action = purple_plugin_action_new( _( "Suggested friends..." ), mxit_suggested_friends_action );
509 m = g_list_append( m, action );
510
511 /* search for users */
512 action = purple_plugin_action_new( _( "Search for Users..." ), mxit_user_search_action );
513 m = g_list_append( m, action );
514
454 /* display splash-screen */ 515 /* display splash-screen */
455 action = purple_plugin_action_new( _( "View Splash..." ), mxit_cb_action_splash ); 516 action = purple_plugin_action_new( _( "View Splash..." ), mxit_splash_action );
456 m = g_list_append( m, action ); 517 m = g_list_append( m, action );
457 518
458 /* display plugin version */ 519 /* display plugin version */
459 action = purple_plugin_action_new( _( "About..." ), mxit_cb_action_about ); 520 action = purple_plugin_action_new( _( "About..." ), mxit_about_action );
460 m = g_list_append( m, action ); 521 m = g_list_append( m, action );
461 522
462 /* suggested friends */
463 action = purple_plugin_action_new( _( "Suggested friends..." ), mxit_cb_suggested_friends );
464 m = g_list_append( m, action );
465
466 /* search for users */
467 action = purple_plugin_action_new( _( "Search for Users..." ), mxit_cb_search_begin );
468 m = g_list_append( m, action );
469
470 return m; 523 return m;
471 } 524 }