comparison libpurple/protocols/jabber/jabber.c @ 17599:8c3fbc353a9c

Added the ability to register gateways, re-using most of the account registration code for this.
author Andreas Monitzer <pidgin@monitzer.com>
date Tue, 19 Jun 2007 14:50:22 +0000
parents 6842cc73b1b7
children 7a3a66c6530a
comparison
equal deleted inserted replaced
17598:43df07968000 17599:8c3fbc353a9c
637 static void 637 static void
638 jabber_registration_result_cb(JabberStream *js, xmlnode *packet, gpointer data) 638 jabber_registration_result_cb(JabberStream *js, xmlnode *packet, gpointer data)
639 { 639 {
640 const char *type = xmlnode_get_attrib(packet, "type"); 640 const char *type = xmlnode_get_attrib(packet, "type");
641 char *buf; 641 char *buf;
642 char *to = data;
642 643
643 if(!strcmp(type, "result")) { 644 if(!strcmp(type, "result")) {
644 buf = g_strdup_printf(_("Registration of %s@%s successful"), 645 if(js->registration)
646 buf = g_strdup_printf(_("Registration of %s@%s successful"),
645 js->user->node, js->user->domain); 647 js->user->node, js->user->domain);
648 else
649 buf = g_strdup_printf(_("Registration to %s successful"),
650 to);
646 purple_notify_info(NULL, _("Registration Successful"), 651 purple_notify_info(NULL, _("Registration Successful"),
647 _("Registration Successful"), buf); 652 _("Registration Successful"), buf);
648 g_free(buf); 653 g_free(buf);
649 } else { 654 } else {
650 char *msg = jabber_parse_error(js, packet); 655 char *msg = jabber_parse_error(js, packet);
654 659
655 purple_notify_error(NULL, _("Registration Failed"), 660 purple_notify_error(NULL, _("Registration Failed"),
656 _("Registration Failed"), msg); 661 _("Registration Failed"), msg);
657 g_free(msg); 662 g_free(msg);
658 } 663 }
659 jabber_connection_schedule_close(js); 664 g_free(to);
660 } 665 if(js->registration)
666 jabber_connection_schedule_close(js);
667 }
668
669 typedef struct _JabberRegisterCBData {
670 JabberStream *js;
671 char *who;
672 } JabberRegisterCBData;
661 673
662 static void 674 static void
663 jabber_register_cb(JabberStream *js, PurpleRequestFields *fields) 675 jabber_register_cb(JabberRegisterCBData *cbdata, PurpleRequestFields *fields)
664 { 676 {
665 GList *groups, *flds; 677 GList *groups, *flds;
666 xmlnode *query, *y; 678 xmlnode *query, *y;
667 JabberIq *iq; 679 JabberIq *iq;
668 char *username; 680 char *username;
669 681
670 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:register"); 682 iq = jabber_iq_new_query(cbdata->js, JABBER_IQ_SET, "jabber:iq:register");
671 query = xmlnode_get_child(iq->node, "query"); 683 query = xmlnode_get_child(iq->node, "query");
684 xmlnode_set_attrib(iq->node,"to",cbdata->who);
672 685
673 for(groups = purple_request_fields_get_groups(fields); groups; 686 for(groups = purple_request_fields_get_groups(fields); groups;
674 groups = groups->next) { 687 groups = groups->next) {
675 for(flds = purple_request_field_group_get_fields(groups->data); 688 for(flds = purple_request_field_group_get_fields(groups->data);
676 flds; flds = flds->next) { 689 flds; flds = flds->next) {
708 y = xmlnode_new_child(query, "date"); 721 y = xmlnode_new_child(query, "date");
709 } else { 722 } else {
710 continue; 723 continue;
711 } 724 }
712 xmlnode_insert_data(y, value, -1); 725 xmlnode_insert_data(y, value, -1);
713 if(!strcmp(id, "username")) { 726 if(cbdata->js->registration && !strcmp(id, "username")) {
714 if(js->user->node) 727 if(cbdata->js->user->node)
715 g_free(js->user->node); 728 g_free(cbdata->js->user->node);
716 js->user->node = g_strdup(value); 729 cbdata->js->user->node = g_strdup(value);
717 } 730 }
718 } 731 }
719 } 732 }
720 733
721 username = g_strdup_printf("%s@%s/%s", js->user->node, js->user->domain, 734 if(cbdata->js->registration) {
722 js->user->resource); 735 username = g_strdup_printf("%s@%s/%s", cbdata->js->user->node, cbdata->js->user->domain,
723 purple_account_set_username(js->gc->account, username); 736 cbdata->js->user->resource);
724 g_free(username); 737 purple_account_set_username(cbdata->js->gc->account, username);
725 738 g_free(username);
726 jabber_iq_set_callback(iq, jabber_registration_result_cb, NULL); 739 }
740
741 jabber_iq_set_callback(iq, jabber_registration_result_cb, cbdata->who);
727 742
728 jabber_iq_send(iq); 743 jabber_iq_send(iq);
729 744 g_free(cbdata);
730 } 745 }
731 746
732 static void 747 static void
733 jabber_register_cancel_cb(JabberStream *js, PurpleRequestFields *fields) 748 jabber_register_cancel_cb(JabberRegisterCBData *cbdata, PurpleRequestFields *fields)
734 { 749 {
735 jabber_connection_schedule_close(js); 750 if(cbdata->js->registration)
751 jabber_connection_schedule_close(cbdata->js);
752 g_free(cbdata->who);
753 g_free(cbdata);
736 } 754 }
737 755
738 static void jabber_register_x_data_cb(JabberStream *js, xmlnode *result, gpointer data) 756 static void jabber_register_x_data_cb(JabberStream *js, xmlnode *result, gpointer data)
739 { 757 {
740 xmlnode *query; 758 xmlnode *query;
741 JabberIq *iq; 759 JabberIq *iq;
760 char *to = data;
742 761
743 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:register"); 762 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:register");
744 query = xmlnode_get_child(iq->node, "query"); 763 query = xmlnode_get_child(iq->node, "query");
764 xmlnode_set_attrib(iq->node,"to",to);
745 765
746 xmlnode_insert_child(query, result); 766 xmlnode_insert_child(query, result);
747 767
748 jabber_iq_set_callback(iq, jabber_registration_result_cb, NULL); 768 jabber_iq_set_callback(iq, jabber_registration_result_cb, to);
749 jabber_iq_send(iq); 769 jabber_iq_send(iq);
750 } 770 }
751 771
752 void jabber_register_parse(JabberStream *js, xmlnode *packet) 772 void jabber_register_parse(JabberStream *js, xmlnode *packet)
753 { 773 {
754 const char *type; 774 const char *type;
775 const char *from = xmlnode_get_attrib(packet, "from");
776 PurpleRequestFields *fields;
777 PurpleRequestFieldGroup *group;
778 PurpleRequestField *field;
779 xmlnode *query, *x, *y;
780 char *instructions;
781 JabberRegisterCBData *cbdata;
782
755 if(!(type = xmlnode_get_attrib(packet, "type")) || strcmp(type, "result")) 783 if(!(type = xmlnode_get_attrib(packet, "type")) || strcmp(type, "result"))
756 return; 784 return;
757 785
758 if(js->registration) { 786 if(js->registration)
759 PurpleRequestFields *fields;
760 PurpleRequestFieldGroup *group;
761 PurpleRequestField *field;
762 xmlnode *query, *x, *y;
763 char *instructions;
764
765 /* get rid of the login thingy */ 787 /* get rid of the login thingy */
766 purple_connection_set_state(js->gc, PURPLE_CONNECTED); 788 purple_connection_set_state(js->gc, PURPLE_CONNECTED);
767 789
768 query = xmlnode_get_child(packet, "query"); 790 query = xmlnode_get_child(packet, "query");
769 791
770 if(xmlnode_get_child(query, "registered")) { 792 if(js->registration && xmlnode_get_child(query, "registered")) {
771 purple_notify_error(NULL, _("Already Registered"), 793 purple_notify_error(NULL, _("Already Registered"),
772 _("Already Registered"), NULL); 794 _("Already Registered"), NULL);
795 if(js->registration)
773 jabber_connection_schedule_close(js); 796 jabber_connection_schedule_close(js);
774 return; 797 return;
775 } 798 }
776 799
777 if((x = xmlnode_get_child_with_namespace(packet, "x", 800 if((x = xmlnode_get_child_with_namespace(packet, "x",
778 "jabber:x:data"))) { 801 "jabber:x:data"))) {
779 jabber_x_data_request(js, x, jabber_register_x_data_cb, NULL); 802 jabber_x_data_request(js, x, jabber_register_x_data_cb, g_strdup(from));
780 return; 803 return;
781 } else if((x = xmlnode_get_child_with_namespace(packet, "x", 804 } else if((x = xmlnode_get_child_with_namespace(packet, "x",
782 "jabber:x:oob"))) { 805 "jabber:x:oob"))) {
783 xmlnode *url; 806 xmlnode *url;
784 807
785 if((url = xmlnode_get_child(x, "url"))) { 808 if((url = xmlnode_get_child(x, "url"))) {
786 char *href; 809 char *href;
787 if((href = xmlnode_get_data(url))) { 810 if((href = xmlnode_get_data(url))) {
788 purple_notify_uri(NULL, href); 811 purple_notify_uri(NULL, href);
789 g_free(href); 812 g_free(href);
813 if(js->registration) {
790 js->gc->wants_to_die = TRUE; 814 js->gc->wants_to_die = TRUE;
791 jabber_connection_schedule_close(js); 815 jabber_connection_schedule_close(js);
792 return;
793 } 816 }
817 return;
794 } 818 }
795 } 819 }
796 820 }
797 /* as a last resort, use the old jabber:iq:register syntax */ 821
798 822 /* as a last resort, use the old jabber:iq:register syntax */
799 fields = purple_request_fields_new(); 823
800 group = purple_request_field_group_new(NULL); 824 fields = purple_request_fields_new();
801 purple_request_fields_add_group(fields, group); 825 group = purple_request_field_group_new(NULL);
802 826 purple_request_fields_add_group(fields, group);
827
828 if(js->registration)
803 field = purple_request_field_string_new("username", _("Username"), 829 field = purple_request_field_string_new("username", _("Username"),
804 js->user->node, FALSE); 830 js->user->node, FALSE);
805 purple_request_field_group_add_field(group, field); 831 else
806 832 field = purple_request_field_string_new("username", _("Username"),
833 NULL, FALSE);
834
835 purple_request_field_group_add_field(group, field);
836
837 if(js->registration)
807 field = purple_request_field_string_new("password", _("Password"), 838 field = purple_request_field_string_new("password", _("Password"),
808 purple_connection_get_password(js->gc), FALSE); 839 purple_connection_get_password(js->gc), FALSE);
809 purple_request_field_string_set_masked(field, TRUE); 840 else
810 purple_request_field_group_add_field(group, field); 841 field = purple_request_field_string_new("password", _("Password"),
811 842 NULL, FALSE);
812 if(xmlnode_get_child(query, "name")) { 843
844 purple_request_field_string_set_masked(field, TRUE);
845 purple_request_field_group_add_field(group, field);
846
847 if(xmlnode_get_child(query, "name")) {
848 if(js->registration)
813 field = purple_request_field_string_new("name", _("Name"), 849 field = purple_request_field_string_new("name", _("Name"),
814 purple_account_get_alias(js->gc->account), FALSE); 850 purple_account_get_alias(js->gc->account), FALSE);
815 purple_request_field_group_add_field(group, field); 851 else
816 } 852 field = purple_request_field_string_new("name", _("Name"),
817 if(xmlnode_get_child(query, "email")) {
818 field = purple_request_field_string_new("email", _("E-mail"),
819 NULL, FALSE); 853 NULL, FALSE);
820 purple_request_field_group_add_field(group, field); 854 purple_request_field_group_add_field(group, field);
821 } 855 }
822 if(xmlnode_get_child(query, "nick")) { 856 if(xmlnode_get_child(query, "email")) {
823 field = purple_request_field_string_new("nick", _("Nickname"), 857 field = purple_request_field_string_new("email", _("E-mail"),
824 NULL, FALSE); 858 NULL, FALSE);
825 purple_request_field_group_add_field(group, field); 859 purple_request_field_group_add_field(group, field);
826 } 860 }
827 if(xmlnode_get_child(query, "first")) { 861 if(xmlnode_get_child(query, "nick")) {
828 field = purple_request_field_string_new("first", _("First name"), 862 field = purple_request_field_string_new("nick", _("Nickname"),
829 NULL, FALSE); 863 NULL, FALSE);
830 purple_request_field_group_add_field(group, field); 864 purple_request_field_group_add_field(group, field);
831 } 865 }
832 if(xmlnode_get_child(query, "last")) { 866 if(xmlnode_get_child(query, "first")) {
833 field = purple_request_field_string_new("last", _("Last name"), 867 field = purple_request_field_string_new("first", _("First name"),
834 NULL, FALSE); 868 NULL, FALSE);
835 purple_request_field_group_add_field(group, field); 869 purple_request_field_group_add_field(group, field);
836 } 870 }
837 if(xmlnode_get_child(query, "address")) { 871 if(xmlnode_get_child(query, "last")) {
838 field = purple_request_field_string_new("address", _("Address"), 872 field = purple_request_field_string_new("last", _("Last name"),
839 NULL, FALSE); 873 NULL, FALSE);
840 purple_request_field_group_add_field(group, field); 874 purple_request_field_group_add_field(group, field);
841 } 875 }
842 if(xmlnode_get_child(query, "city")) { 876 if(xmlnode_get_child(query, "address")) {
843 field = purple_request_field_string_new("city", _("City"), 877 field = purple_request_field_string_new("address", _("Address"),
844 NULL, FALSE); 878 NULL, FALSE);
845 purple_request_field_group_add_field(group, field); 879 purple_request_field_group_add_field(group, field);
846 } 880 }
847 if(xmlnode_get_child(query, "state")) { 881 if(xmlnode_get_child(query, "city")) {
848 field = purple_request_field_string_new("state", _("State"), 882 field = purple_request_field_string_new("city", _("City"),
849 NULL, FALSE); 883 NULL, FALSE);
850 purple_request_field_group_add_field(group, field); 884 purple_request_field_group_add_field(group, field);
851 } 885 }
852 if(xmlnode_get_child(query, "zip")) { 886 if(xmlnode_get_child(query, "state")) {
853 field = purple_request_field_string_new("zip", _("Postal code"), 887 field = purple_request_field_string_new("state", _("State"),
854 NULL, FALSE); 888 NULL, FALSE);
855 purple_request_field_group_add_field(group, field); 889 purple_request_field_group_add_field(group, field);
856 } 890 }
857 if(xmlnode_get_child(query, "phone")) { 891 if(xmlnode_get_child(query, "zip")) {
858 field = purple_request_field_string_new("phone", _("Phone"), 892 field = purple_request_field_string_new("zip", _("Postal code"),
859 NULL, FALSE); 893 NULL, FALSE);
860 purple_request_field_group_add_field(group, field); 894 purple_request_field_group_add_field(group, field);
861 } 895 }
862 if(xmlnode_get_child(query, "url")) { 896 if(xmlnode_get_child(query, "phone")) {
863 field = purple_request_field_string_new("url", _("URL"), 897 field = purple_request_field_string_new("phone", _("Phone"),
864 NULL, FALSE); 898 NULL, FALSE);
865 purple_request_field_group_add_field(group, field); 899 purple_request_field_group_add_field(group, field);
866 } 900 }
867 if(xmlnode_get_child(query, "date")) { 901 if(xmlnode_get_child(query, "url")) {
868 field = purple_request_field_string_new("date", _("Date"), 902 field = purple_request_field_string_new("url", _("URL"),
869 NULL, FALSE); 903 NULL, FALSE);
870 purple_request_field_group_add_field(group, field); 904 purple_request_field_group_add_field(group, field);
871 } 905 }
872 906 if(xmlnode_get_child(query, "date")) {
873 if((y = xmlnode_get_child(query, "instructions"))) 907 field = purple_request_field_string_new("date", _("Date"),
874 instructions = xmlnode_get_data(y); 908 NULL, FALSE);
875 else 909 purple_request_field_group_add_field(group, field);
876 instructions = g_strdup(_("Please fill out the information below " 910 }
877 "to register your new account.")); 911
878 912 if((y = xmlnode_get_child(query, "instructions")))
913 instructions = xmlnode_get_data(y);
914 else
915 instructions = g_strdup(_("Please fill out the information below "
916 "to register your new account."));
917
918 cbdata = g_new0(JabberRegisterCBData, 1);
919 cbdata->js = js;
920 cbdata->who = g_strdup(from);
921
922 if(js->registration)
879 purple_request_fields(js->gc, _("Register New XMPP Account"), 923 purple_request_fields(js->gc, _("Register New XMPP Account"),
880 _("Register New XMPP Account"), instructions, fields, 924 _("Register New XMPP Account"), instructions, fields,
881 _("Register"), G_CALLBACK(jabber_register_cb), 925 _("Register"), G_CALLBACK(jabber_register_cb),
882 _("Cancel"), G_CALLBACK(jabber_register_cancel_cb), 926 _("Cancel"), G_CALLBACK(jabber_register_cancel_cb),
883 purple_connection_get_account(js->gc), NULL, NULL, 927 purple_connection_get_account(js->gc), NULL, NULL,
884 js); 928 cbdata);
885 929 else {
886 g_free(instructions); 930 char *title = g_strdup_printf(_("Register New Account at %s"), from);
887 } 931 purple_request_fields(js->gc, title,
932 title, instructions, fields,
933 _("Register"), G_CALLBACK(jabber_register_cb),
934 _("Cancel"), G_CALLBACK(jabber_register_cancel_cb),
935 purple_connection_get_account(js->gc), NULL, NULL,
936 cbdata);
937 g_free(title);
938 }
939
940 g_free(instructions);
888 } 941 }
889 942
890 void jabber_register_start(JabberStream *js) 943 void jabber_register_start(JabberStream *js)
891 { 944 {
892 JabberIq *iq; 945 JabberIq *iq;
893 946
894 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:register"); 947 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:register");
948 jabber_iq_send(iq);
949 }
950
951 void jabber_register_gateway(JabberStream *js, const char *gateway) {
952 JabberIq *iq;
953
954 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:register");
955 xmlnode_set_attrib(iq->node, "to", gateway);
895 jabber_iq_send(iq); 956 jabber_iq_send(iq);
896 } 957 }
897 958
898 void jabber_register_account(PurpleAccount *account) 959 void jabber_register_account(PurpleAccount *account)
899 { 960 {