Mercurial > pidgin
comparison src/protocols/jabber/jabber.c @ 7072:1350352f5818
[gaim-migrate @ 7637]
Jabber Registration Support
Also, what I think is a bugfix for the request API. If it's not, then
I'm very confused.
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Tue, 30 Sep 2003 14:37:05 +0000 |
parents | b40f12355812 |
children | c8bf2da398e3 |
comparison
equal
deleted
inserted
replaced
7071:470b14b1c70d | 7072:1350352f5818 |
---|---|
35 #include "accountopt.h" | 35 #include "accountopt.h" |
36 #include "debug.h" | 36 #include "debug.h" |
37 #include "html.h" | 37 #include "html.h" |
38 #include "message.h" | 38 #include "message.h" |
39 #include "multi.h" | 39 #include "multi.h" |
40 #include "notify.h" | |
40 #include "prpl.h" | 41 #include "prpl.h" |
42 #include "request.h" | |
41 #include "server.h" | 43 #include "server.h" |
42 | 44 |
43 #include "auth.h" | 45 #include "auth.h" |
44 #include "buddy.h" | 46 #include "buddy.h" |
45 #include "chat.h" | 47 #include "chat.h" |
374 if (rc != 0) | 376 if (rc != 0) |
375 gaim_connection_error(gc, _("Unable to create socket")); | 377 gaim_connection_error(gc, _("Unable to create socket")); |
376 } | 378 } |
377 } | 379 } |
378 | 380 |
381 static gboolean | |
382 conn_close_cb(gpointer data) | |
383 { | |
384 JabberStream *js = data; | |
385 gaim_connection_destroy(js->gc); | |
386 return FALSE; | |
387 } | |
388 | |
389 static void | |
390 jabber_connection_schedule_close(JabberStream *js) | |
391 { | |
392 g_timeout_add(0, conn_close_cb, js); | |
393 } | |
394 | |
395 static void | |
396 jabber_registration_result_cb(JabberStream *js, xmlnode *packet) | |
397 { | |
398 const char *type = xmlnode_get_attrib(packet, "type"); | |
399 char *buf; | |
400 | |
401 if(!strcmp(type, "result")) { | |
402 buf = g_strdup_printf(_("Registration of %s@%s successful"), | |
403 js->user->node, js->user->domain); | |
404 gaim_notify_info(NULL, _("Registration Successful"), | |
405 _("Registration Successful"), buf); | |
406 g_free(buf); | |
407 } else { | |
408 char *error; | |
409 xmlnode *y; | |
410 | |
411 if((y = xmlnode_get_child(packet, "error"))) { | |
412 error = xmlnode_get_data(y); | |
413 } else { | |
414 error = g_strdup(_("Unknown Error")); | |
415 } | |
416 | |
417 buf = g_strdup_printf(_("Registration of %s@%s failed: %s"), | |
418 js->user->node, js->user->domain, error); | |
419 gaim_notify_error(NULL, _("Registration Failed"), | |
420 _("Registration Failed"), buf); | |
421 g_free(buf); | |
422 g_free(error); | |
423 } | |
424 jabber_connection_schedule_close(js); | |
425 } | |
426 | |
427 static void | |
428 jabber_register_cb(JabberStream *js, GaimRequestFields *fields) | |
429 { | |
430 GList *groups, *flds; | |
431 xmlnode *query, *y; | |
432 JabberIq *iq; | |
433 | |
434 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:register"); | |
435 query = xmlnode_get_child(iq->node, "query"); | |
436 | |
437 for(groups = gaim_request_fields_get_groups(fields); groups; | |
438 groups = groups->next) { | |
439 for(flds = gaim_request_field_group_get_fields(groups->data); | |
440 flds; flds = flds->next) { | |
441 GaimRequestField *field = flds->data; | |
442 const char *id = gaim_request_field_get_id(field); | |
443 const char *value = gaim_request_field_string_get_value(field); | |
444 | |
445 if(!strcmp(id, "username")) { | |
446 y = xmlnode_new_child(query, "username"); | |
447 } else if(!strcmp(id, "password")) { | |
448 y = xmlnode_new_child(query, "password"); | |
449 } else if(!strcmp(id, "name")) { | |
450 y = xmlnode_new_child(query, "name"); | |
451 } else if(!strcmp(id, "email")) { | |
452 y = xmlnode_new_child(query, "email"); | |
453 } else if(!strcmp(id, "nick")) { | |
454 y = xmlnode_new_child(query, "nick"); | |
455 } else if(!strcmp(id, "first")) { | |
456 y = xmlnode_new_child(query, "first"); | |
457 } else if(!strcmp(id, "last")) { | |
458 y = xmlnode_new_child(query, "last"); | |
459 } else if(!strcmp(id, "address")) { | |
460 y = xmlnode_new_child(query, "address"); | |
461 } else if(!strcmp(id, "city")) { | |
462 y = xmlnode_new_child(query, "city"); | |
463 } else if(!strcmp(id, "state")) { | |
464 y = xmlnode_new_child(query, "state"); | |
465 } else if(!strcmp(id, "zip")) { | |
466 y = xmlnode_new_child(query, "zip"); | |
467 } else if(!strcmp(id, "phone")) { | |
468 y = xmlnode_new_child(query, "phone"); | |
469 } else if(!strcmp(id, "url")) { | |
470 y = xmlnode_new_child(query, "url"); | |
471 } else if(!strcmp(id, "date")) { | |
472 y = xmlnode_new_child(query, "date"); | |
473 } else { | |
474 continue; | |
475 } | |
476 xmlnode_insert_data(y, value, -1); | |
477 } | |
478 } | |
479 | |
480 jabber_iq_set_callback(iq, jabber_registration_result_cb); | |
481 | |
482 jabber_iq_send(iq); | |
483 | |
484 } | |
485 | |
486 static void | |
487 jabber_register_cancel_cb(JabberStream *js, GaimRequestFields *fields) | |
488 { | |
489 jabber_connection_schedule_close(js); | |
490 } | |
491 | |
492 void jabber_register_parse(JabberStream *js, xmlnode *packet) | |
493 { | |
494 if(js->registration) { | |
495 GaimRequestFields *fields; | |
496 GaimRequestFieldGroup *group; | |
497 GaimRequestField *field; | |
498 xmlnode *query, *y; | |
499 char *instructions; | |
500 | |
501 /* get rid of the login thingy */ | |
502 gaim_connection_set_state(js->gc, GAIM_CONNECTED); | |
503 | |
504 query = xmlnode_get_child(packet, "query"); | |
505 | |
506 if(xmlnode_get_child(query, "registered")) { | |
507 gaim_notify_error(NULL, _("Already Registered"), | |
508 _("Already Registered"), NULL); | |
509 jabber_connection_schedule_close(js); | |
510 return; | |
511 } | |
512 | |
513 fields = gaim_request_fields_new(); | |
514 group = gaim_request_field_group_new(NULL); | |
515 gaim_request_fields_add_group(fields, group); | |
516 | |
517 field = gaim_request_field_string_new("username", _("Username"), | |
518 js->user->node, FALSE); | |
519 gaim_request_field_group_add_field(group, field); | |
520 | |
521 field = gaim_request_field_string_new("password", _("Password"), | |
522 gaim_account_get_password(js->gc->account), FALSE); | |
523 gaim_request_field_string_set_masked(field, TRUE); | |
524 gaim_request_field_group_add_field(group, field); | |
525 | |
526 if(xmlnode_get_child(query, "name")) { | |
527 field = gaim_request_field_string_new("name", _("Name"), | |
528 gaim_account_get_alias(js->gc->account), FALSE); | |
529 gaim_request_field_group_add_field(group, field); | |
530 } | |
531 if(xmlnode_get_child(query, "email")) { | |
532 field = gaim_request_field_string_new("email", _("E-Mail"), | |
533 NULL, FALSE); | |
534 gaim_request_field_group_add_field(group, field); | |
535 } | |
536 if(xmlnode_get_child(query, "nick")) { | |
537 field = gaim_request_field_string_new("nick", _("Nickname"), | |
538 NULL, FALSE); | |
539 gaim_request_field_group_add_field(group, field); | |
540 } | |
541 if(xmlnode_get_child(query, "first")) { | |
542 field = gaim_request_field_string_new("first", _("First Name"), | |
543 NULL, FALSE); | |
544 gaim_request_field_group_add_field(group, field); | |
545 } | |
546 if(xmlnode_get_child(query, "last")) { | |
547 field = gaim_request_field_string_new("last", _("Last Name"), | |
548 NULL, FALSE); | |
549 gaim_request_field_group_add_field(group, field); | |
550 } | |
551 if(xmlnode_get_child(query, "address")) { | |
552 field = gaim_request_field_string_new("address", _("Address"), | |
553 NULL, FALSE); | |
554 gaim_request_field_group_add_field(group, field); | |
555 } | |
556 if(xmlnode_get_child(query, "city")) { | |
557 field = gaim_request_field_string_new("city", _("City"), | |
558 NULL, FALSE); | |
559 gaim_request_field_group_add_field(group, field); | |
560 } | |
561 if(xmlnode_get_child(query, "state")) { | |
562 field = gaim_request_field_string_new("state", _("State"), | |
563 NULL, FALSE); | |
564 gaim_request_field_group_add_field(group, field); | |
565 } | |
566 if(xmlnode_get_child(query, "zip")) { | |
567 field = gaim_request_field_string_new("zip", _("Postal Code"), | |
568 NULL, FALSE); | |
569 gaim_request_field_group_add_field(group, field); | |
570 } | |
571 if(xmlnode_get_child(query, "phone")) { | |
572 field = gaim_request_field_string_new("phone", _("Phone"), | |
573 NULL, FALSE); | |
574 gaim_request_field_group_add_field(group, field); | |
575 } | |
576 if(xmlnode_get_child(query, "url")) { | |
577 field = gaim_request_field_string_new("url", _("URL"), | |
578 NULL, FALSE); | |
579 gaim_request_field_group_add_field(group, field); | |
580 } | |
581 if(xmlnode_get_child(query, "date")) { | |
582 field = gaim_request_field_string_new("date", _("Date"), | |
583 NULL, FALSE); | |
584 gaim_request_field_group_add_field(group, field); | |
585 } | |
586 | |
587 if((y = xmlnode_get_child(query, "instructions"))) | |
588 instructions = xmlnode_get_data(y); | |
589 else | |
590 instructions = g_strdup(_("Please fill out the information below " | |
591 "to register your new account.")); | |
592 | |
593 gaim_request_fields(js->gc, _("Register New Jabber Account"), | |
594 _("Register New Jabber Account"), instructions, fields, | |
595 _("Register"), G_CALLBACK(jabber_register_cb), | |
596 _("Cancel"), G_CALLBACK(jabber_register_cancel_cb), js); | |
597 } | |
598 } | |
599 | |
600 static void jabber_register_start(JabberStream *js) | |
601 { | |
602 JabberIq *iq; | |
603 | |
604 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:register"); | |
605 jabber_iq_send(iq); | |
606 } | |
607 | |
608 static void jabber_register_account(GaimAccount *account) | |
609 { | |
610 GaimConnection *gc = gaim_account_get_connection(account); | |
611 JabberStream *js; | |
612 const char *connect_server = gaim_account_get_string(account, | |
613 "connect_server", ""); | |
614 const char *server; | |
615 int rc; | |
616 | |
617 js = gc->proto_data = g_new0(JabberStream, 1); | |
618 js->gc = gc; | |
619 js->registration = TRUE; | |
620 js->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, | |
621 g_free, NULL); | |
622 js->user = jabber_id_new(gaim_account_get_username(account)); | |
623 | |
624 server = connect_server[0] ? connect_server : js->user->domain; | |
625 | |
626 jabber_stream_set_state(js, JABBER_STREAM_CONNECTING); | |
627 | |
628 if(gaim_account_get_bool(account, "old_ssl", FALSE) | |
629 && gaim_ssl_is_supported()) { | |
630 js->gsc = gaim_ssl_connect(account, server, | |
631 gaim_account_get_int(account, "port", 5222), | |
632 jabber_login_callback_ssl, gc); | |
633 } | |
634 | |
635 if(!js->gsc) { | |
636 rc = gaim_proxy_connect(account, server, | |
637 gaim_account_get_int(account, "port", 5222), | |
638 jabber_login_callback, gc); | |
639 | |
640 if (rc != 0) | |
641 gaim_connection_error(gc, _("Unable to create socket")); | |
642 } | |
643 } | |
644 | |
379 static void jabber_close(GaimConnection *gc) | 645 static void jabber_close(GaimConnection *gc) |
380 { | 646 { |
381 JabberStream *js = gc->proto_data; | 647 JabberStream *js = gc->proto_data; |
382 | 648 |
383 jabber_send_raw(js, "</stream:stream>"); | 649 jabber_send_raw(js, "</stream:stream>"); |
384 | 650 |
385 if(js->gsc) { | 651 if(js->gsc) { |
386 gaim_ssl_close(js->gsc); | 652 gaim_ssl_close(js->gsc); |
387 } else { | 653 } else { |
654 if(js->gc->inpa) | |
655 gaim_input_remove(js->gc->inpa); | |
388 close(js->fd); | 656 close(js->fd); |
389 } | 657 } |
390 | 658 |
391 g_markup_parse_context_free(js->context); | 659 g_markup_parse_context_free(js->context); |
392 | 660 |
393 g_hash_table_destroy(js->callbacks); | 661 if(js->callbacks) |
394 g_hash_table_destroy(js->buddies); | 662 g_hash_table_destroy(js->callbacks); |
663 if(js->buddies) | |
664 g_hash_table_destroy(js->buddies); | |
665 if(js->chats) | |
666 g_hash_table_destroy(js->chats); | |
395 if(js->stream_id) | 667 if(js->stream_id) |
396 g_free(js->stream_id); | 668 g_free(js->stream_id); |
397 jabber_id_free(js->user); | 669 jabber_id_free(js->user); |
398 g_free(js); | 670 g_free(js); |
399 } | 671 } |
415 jabber_parser_setup(js); | 687 jabber_parser_setup(js); |
416 break; | 688 break; |
417 case JABBER_STREAM_AUTHENTICATING: | 689 case JABBER_STREAM_AUTHENTICATING: |
418 gaim_connection_update_progress(js->gc, _("Authenticating"), | 690 gaim_connection_update_progress(js->gc, _("Authenticating"), |
419 js->gsc ? 6 : 3, JABBER_CONNECT_STEPS); | 691 js->gsc ? 6 : 3, JABBER_CONNECT_STEPS); |
420 if(js->protocol_version == JABBER_PROTO_0_9) | 692 if(js->registration) |
693 jabber_register_start(js); | |
694 else if(js->protocol_version == JABBER_PROTO_0_9) | |
421 jabber_auth_start_old(js); | 695 jabber_auth_start_old(js); |
422 break; | 696 break; |
423 case JABBER_STREAM_REINITIALIZING: | 697 case JABBER_STREAM_REINITIALIZING: |
424 gaim_connection_update_progress(js->gc, _("Re-initializing Stream"), | 698 gaim_connection_update_progress(js->gc, _("Re-initializing Stream"), |
425 6, JABBER_CONNECT_STEPS); | 699 6, JABBER_CONNECT_STEPS); |
625 jabber_chat_invite, | 899 jabber_chat_invite, |
626 jabber_chat_leave, | 900 jabber_chat_leave, |
627 jabber_chat_whisper, | 901 jabber_chat_whisper, |
628 jabber_message_send_chat, | 902 jabber_message_send_chat, |
629 jabber_keepalive, | 903 jabber_keepalive, |
630 NULL, /* register_user */ /* XXX tell the user success/failure */ | 904 jabber_register_account, |
631 jabber_buddy_get_info_chat, | 905 jabber_buddy_get_info_chat, |
632 NULL, | 906 NULL, |
633 jabber_roster_alias_change, | 907 jabber_roster_alias_change, |
634 jabber_roster_group_change, | 908 jabber_roster_group_change, |
635 jabber_roster_group_rename, | 909 jabber_roster_group_rename, |
669 init_plugin(GaimPlugin *plugin) | 943 init_plugin(GaimPlugin *plugin) |
670 { | 944 { |
671 GaimAccountUserSplit *split; | 945 GaimAccountUserSplit *split; |
672 GaimAccountOption *option; | 946 GaimAccountOption *option; |
673 | 947 |
948 /* Ugly Hack for SSL */ | |
949 GaimPlugin *ssl_plugin; | |
950 ssl_plugin = gaim_plugins_find_with_id("core-ssl"); | |
951 if (ssl_plugin != NULL && !gaim_plugin_is_loaded(ssl_plugin)) | |
952 gaim_plugin_load(ssl_plugin); | |
953 | |
674 split = gaim_account_user_split_new(_("Server"), "jabber.org", '@'); | 954 split = gaim_account_user_split_new(_("Server"), "jabber.org", '@'); |
675 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split); | 955 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split); |
676 | 956 |
677 split = gaim_account_user_split_new(_("Resource"), "Gaim", '/'); | 957 split = gaim_account_user_split_new(_("Resource"), "Gaim", '/'); |
678 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split); | 958 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split); |