Mercurial > pidgin
comparison libpurple/protocols/jabber/jabber.c @ 27599:409ef6d76bf6
Refactor jabber_login and jabber_register_account
Now they both call jabber_stream_new and jabber_stream_connect
and do the separate stuff between those two calls.
author | Paul Aurich <paul@darkrain42.org> |
---|---|
date | Fri, 17 Jul 2009 23:49:51 +0000 |
parents | a12574d982a1 |
children | f95bcb5eb047 |
comparison
equal
deleted
inserted
replaced
27598:e5fdfff98aa9 | 27599:409ef6d76bf6 |
---|---|
757 jabber_login_connect(js, js->user->domain, js->user->domain, | 757 jabber_login_connect(js, js->user->domain, js->user->domain, |
758 purple_account_get_int(js->gc->account, "port", 5222), TRUE); | 758 purple_account_get_int(js->gc->account, "port", 5222), TRUE); |
759 } | 759 } |
760 } | 760 } |
761 | 761 |
762 void | 762 static JabberStream * |
763 jabber_login(PurpleAccount *account) | 763 jabber_stream_new(PurpleAccount *account) |
764 { | 764 { |
765 PurpleConnection *gc = purple_account_get_connection(account); | 765 PurpleConnection *gc = purple_account_get_connection(account); |
766 const char *connect_server = purple_account_get_string(account, | |
767 "connect_server", ""); | |
768 const char *bosh_url = purple_account_get_string(account, | |
769 "bosh_url", ""); | |
770 JabberStream *js; | 766 JabberStream *js; |
767 JabberBuddy *my_jb; | |
771 PurplePresence *presence; | 768 PurplePresence *presence; |
772 PurpleStoredImage *image; | 769 |
773 JabberBuddy *my_jb = NULL; | |
774 | |
775 gc->flags |= PURPLE_CONNECTION_HTML | | |
776 PURPLE_CONNECTION_ALLOW_CUSTOM_SMILEY; | |
777 js = gc->proto_data = g_new0(JabberStream, 1); | 770 js = gc->proto_data = g_new0(JabberStream, 1); |
778 js->gc = gc; | 771 js->gc = gc; |
779 js->fd = -1; | 772 js->fd = -1; |
773 js->user = jabber_id_new(purple_account_get_username(account)); | |
774 | |
775 if (!js->user) { | |
776 purple_connection_error_reason(gc, | |
777 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS, | |
778 _("Invalid XMPP ID")); | |
779 /* Destroying the connection will free the JabberStream */ | |
780 return NULL; | |
781 } | |
782 | |
783 if (!js->user->domain || *(js->user->domain) == '\0') { | |
784 purple_connection_error_reason(gc, | |
785 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS, | |
786 _("Invalid XMPP ID. Domain must be set.")); | |
787 /* Destroying the connection will free the JabberStream */ | |
788 return NULL; | |
789 } | |
790 | |
791 js->buddies = g_hash_table_new_full(g_str_hash, g_str_equal, | |
792 g_free, (GDestroyNotify)jabber_buddy_free); | |
793 | |
794 my_jb = jabber_buddy_find(js, purple_account_get_username(account), TRUE); | |
795 if (!my_jb) { | |
796 /* This basically *can't* fail, but for good measure... */ | |
797 purple_connection_error_reason(gc, | |
798 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS, | |
799 _("Invalid XMPP ID")); | |
800 /* Destroying the connection will free the JabberStream */ | |
801 g_return_val_if_reached(NULL); | |
802 } | |
803 | |
804 my_jb->subscription |= JABBER_SUB_BOTH; | |
805 | |
780 js->iq_callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, | 806 js->iq_callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, |
781 g_free, g_free); | 807 g_free, g_free); |
782 js->buddies = g_hash_table_new_full(g_str_hash, g_str_equal, | |
783 g_free, (GDestroyNotify)jabber_buddy_free); | |
784 js->chats = g_hash_table_new_full(g_str_hash, g_str_equal, | 808 js->chats = g_hash_table_new_full(g_str_hash, g_str_equal, |
785 g_free, (GDestroyNotify)jabber_chat_free); | 809 g_free, (GDestroyNotify)jabber_chat_free); |
786 js->user = jabber_id_new(purple_account_get_username(account)); | |
787 js->next_id = g_random_int(); | 810 js->next_id = g_random_int(); |
788 js->write_buffer = purple_circ_buffer_new(512); | 811 js->write_buffer = purple_circ_buffer_new(512); |
789 js->old_length = 0; | 812 js->old_length = 0; |
790 js->keepalive_timeout = 0; | 813 js->keepalive_timeout = 0; |
791 /* Set the default protocol version to 1.0. Overridden in parser.c. */ | 814 /* Set the default protocol version to 1.0. Overridden in parser.c. */ |
800 sense to do this when registering a new account... */ | 823 sense to do this when registering a new account... */ |
801 presence = purple_account_get_presence(account); | 824 presence = purple_account_get_presence(account); |
802 if (purple_presence_is_idle(presence)) | 825 if (purple_presence_is_idle(presence)) |
803 js->idle = purple_presence_get_idle_time(presence); | 826 js->idle = purple_presence_get_idle_time(presence); |
804 | 827 |
805 if(!js->user) { | 828 return js; |
806 purple_connection_error_reason(gc, | 829 } |
807 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS, | 830 |
808 _("Invalid XMPP ID")); | 831 static void |
809 return; | 832 jabber_stream_connect(JabberStream *js) |
810 } | 833 { |
811 | 834 PurpleConnection *gc = js->gc; |
812 if (!js->user->domain || *(js->user->domain) == '\0') { | 835 PurpleAccount *account = purple_connection_get_account(gc); |
813 purple_connection_error_reason(gc, | 836 const char *connect_server = purple_account_get_string(account, |
814 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS, | 837 "connect_server", ""); |
815 _("Invalid XMPP ID. Domain must be set.")); | 838 const char *bosh_url = purple_account_get_string(account, |
816 return; | 839 "bosh_url", ""); |
817 } | |
818 | |
819 /* | |
820 * Calculate the avatar hash for our current image so we know (when we | |
821 * fetch our vCard and PEP avatar) if we should send our avatar to the | |
822 * server. | |
823 */ | |
824 if ((image = purple_buddy_icons_find_account_icon(account))) { | |
825 js->initial_avatar_hash = jabber_calculate_data_sha1sum(purple_imgstore_get_data(image), | |
826 purple_imgstore_get_size(image)); | |
827 purple_imgstore_unref(image); | |
828 } | |
829 | |
830 if((my_jb = jabber_buddy_find(js, purple_account_get_username(account), TRUE))) | |
831 my_jb->subscription |= JABBER_SUB_BOTH; | |
832 | 840 |
833 jabber_stream_set_state(js, JABBER_STREAM_CONNECTING); | 841 jabber_stream_set_state(js, JABBER_STREAM_CONNECTING); |
834 | 842 |
835 /* TODO: Just use purple_url_parse? */ | |
836 /* If both BOSH and a Connect Server are specified, we prefer BOSH. I'm not | 843 /* If both BOSH and a Connect Server are specified, we prefer BOSH. I'm not |
837 * attached to that choice, though. | 844 * attached to that choice, though. |
838 */ | 845 */ |
839 if (*bosh_url) { | 846 if (*bosh_url) { |
840 js->use_bosh = TRUE; | 847 js->use_bosh = TRUE; |
841 js->bosh = jabber_bosh_connection_init(js, bosh_url); | 848 js->bosh = jabber_bosh_connection_init(js, bosh_url); |
842 if (js->bosh) | 849 if (js->bosh) |
843 jabber_bosh_connection_connect(js->bosh); | 850 jabber_bosh_connection_connect(js->bosh); |
844 else { | 851 else { |
845 purple_connection_error_reason(js->gc, | 852 purple_connection_error_reason(gc, |
846 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS, | 853 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS, |
847 _("Malformed BOSH URL")); | 854 _("Malformed BOSH URL")); |
848 } | 855 } |
849 | 856 |
850 return; | 857 return; |
851 } | 858 } |
852 | 859 |
853 js->certificate_CN = g_strdup(connect_server[0] ? connect_server : js->user->domain); | 860 js->certificate_CN = g_strdup(connect_server[0] ? connect_server : js->user->domain); |
854 | 861 |
855 /* if they've got old-ssl mode going, we probably want to ignore SRV lookups */ | 862 /* if they've got old-ssl mode going, we probably want to ignore SRV lookups */ |
856 if(purple_account_get_bool(js->gc->account, "old_ssl", FALSE)) { | 863 if(purple_account_get_bool(account, "old_ssl", FALSE)) { |
857 if(purple_ssl_is_supported()) { | 864 if(purple_ssl_is_supported()) { |
858 js->gsc = purple_ssl_connect(js->gc->account, | 865 js->gsc = purple_ssl_connect(account, js->certificate_CN, |
859 js->certificate_CN, | 866 purple_account_get_int(account, "port", 5223), |
860 purple_account_get_int(account, "port", 5223), jabber_login_callback_ssl, | 867 jabber_login_callback_ssl, jabber_ssl_connect_failure, gc); |
861 jabber_ssl_connect_failure, js->gc); | |
862 if (!js->gsc) { | 868 if (!js->gsc) { |
863 purple_connection_error_reason(js->gc, | 869 purple_connection_error_reason(gc, |
864 PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT, | 870 PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT, |
865 _("Unable to establish SSL connection")); | 871 _("Unable to establish SSL connection")); |
866 } | 872 } |
867 } else { | 873 } else { |
868 purple_connection_error_reason(js->gc, | 874 purple_connection_error_reason(gc, |
869 PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT, | 875 PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT, |
870 _("SSL support unavailable")); | 876 _("SSL support unavailable")); |
871 } | 877 } |
872 | 878 |
873 return; | 879 return; |
879 jabber_login_connect(js, js->user->domain, connect_server, purple_account_get_int(account, "port", 5222), TRUE); | 885 jabber_login_connect(js, js->user->domain, connect_server, purple_account_get_int(account, "port", 5222), TRUE); |
880 } else { | 886 } else { |
881 js->srv_query_data = purple_srv_resolve("xmpp-client", | 887 js->srv_query_data = purple_srv_resolve("xmpp-client", |
882 "tcp", js->user->domain, srv_resolved_cb, js); | 888 "tcp", js->user->domain, srv_resolved_cb, js); |
883 } | 889 } |
890 } | |
891 | |
892 void | |
893 jabber_login(PurpleAccount *account) | |
894 { | |
895 PurpleConnection *gc = purple_account_get_connection(account); | |
896 JabberStream *js; | |
897 PurpleStoredImage *image; | |
898 | |
899 gc->flags |= PURPLE_CONNECTION_HTML | | |
900 PURPLE_CONNECTION_ALLOW_CUSTOM_SMILEY; | |
901 js = jabber_stream_new(account); | |
902 if (js == NULL) | |
903 return; | |
904 | |
905 /* | |
906 * Calculate the avatar hash for our current image so we know (when we | |
907 * fetch our vCard and PEP avatar) if we should send our avatar to the | |
908 * server. | |
909 */ | |
910 image = purple_buddy_icons_find_account_icon(account); | |
911 if (image != NULL) { | |
912 js->initial_avatar_hash = | |
913 jabber_calculate_data_sha1sum(purple_imgstore_get_data(image), | |
914 purple_imgstore_get_size(image)); | |
915 purple_imgstore_unref(image); | |
916 } | |
917 | |
918 jabber_stream_connect(js); | |
884 } | 919 } |
885 | 920 |
886 | 921 |
887 static gboolean | 922 static gboolean |
888 conn_close_cb(gpointer data) | 923 conn_close_cb(gpointer data) |
1291 jabber_iq_send(iq); | 1326 jabber_iq_send(iq); |
1292 } | 1327 } |
1293 | 1328 |
1294 void jabber_register_account(PurpleAccount *account) | 1329 void jabber_register_account(PurpleAccount *account) |
1295 { | 1330 { |
1296 PurpleConnection *gc = purple_account_get_connection(account); | |
1297 JabberStream *js; | 1331 JabberStream *js; |
1298 JabberBuddy *my_jb = NULL; | 1332 |
1299 const char *connect_server = purple_account_get_string(account, | 1333 js = jabber_stream_new(account); |
1300 "connect_server", ""); | 1334 if (js == NULL) |
1301 const char *server; | 1335 return; |
1302 | 1336 |
1303 js = gc->proto_data = g_new0(JabberStream, 1); | |
1304 js->gc = gc; | |
1305 js->registration = TRUE; | 1337 js->registration = TRUE; |
1306 js->iq_callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, | 1338 jabber_stream_connect(js); |
1307 g_free, g_free); | |
1308 js->user = jabber_id_new(purple_account_get_username(account)); | |
1309 js->next_id = g_random_int(); | |
1310 js->old_length = 0; | |
1311 js->keepalive_timeout = 0; | |
1312 | |
1313 if(!js->user) { | |
1314 purple_connection_error_reason(gc, | |
1315 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS, | |
1316 _("Invalid XMPP ID")); | |
1317 return; | |
1318 } | |
1319 | |
1320 js->write_buffer = purple_circ_buffer_new(512); | |
1321 | |
1322 if((my_jb = jabber_buddy_find(js, purple_account_get_username(account), TRUE))) | |
1323 my_jb->subscription |= JABBER_SUB_BOTH; | |
1324 | |
1325 server = connect_server[0] ? connect_server : js->user->domain; | |
1326 js->certificate_CN = g_strdup(server); | |
1327 | |
1328 js->stun_ip = NULL; | |
1329 js->stun_port = 0; | |
1330 js->stun_query = NULL; | |
1331 | |
1332 jabber_stream_set_state(js, JABBER_STREAM_CONNECTING); | |
1333 | |
1334 /* TODO: Just use purple_url_parse? */ | |
1335 if (!g_ascii_strncasecmp(connect_server, "http://", 7) || !g_ascii_strncasecmp(connect_server, "https://", 8)) { | |
1336 js->use_bosh = TRUE; | |
1337 js->bosh = jabber_bosh_connection_init(js, connect_server); | |
1338 if (!js->bosh) { | |
1339 purple_connection_error_reason(js->gc, | |
1340 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS, | |
1341 _("Malformed BOSH Connect Server")); | |
1342 return; | |
1343 } | |
1344 jabber_bosh_connection_connect(js->bosh); | |
1345 return; | |
1346 } else { | |
1347 js->certificate_CN = g_strdup(connect_server[0] ? connect_server : js->user->domain); | |
1348 } | |
1349 | |
1350 if(purple_account_get_bool(account, "old_ssl", FALSE)) { | |
1351 if(purple_ssl_is_supported()) { | |
1352 js->gsc = purple_ssl_connect(account, server, | |
1353 purple_account_get_int(account, "port", 5222), | |
1354 jabber_login_callback_ssl, jabber_ssl_connect_failure, gc); | |
1355 if (!js->gsc) { | |
1356 purple_connection_error_reason(js->gc, | |
1357 PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT, | |
1358 _("Unable to establish SSL connection")); | |
1359 } | |
1360 } else { | |
1361 purple_connection_error_reason(gc, | |
1362 PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT, | |
1363 _("SSL support unavailable")); | |
1364 } | |
1365 | |
1366 return; | |
1367 } | |
1368 | |
1369 if (connect_server[0]) { | |
1370 jabber_login_connect(js, js->user->domain, server, | |
1371 purple_account_get_int(account, | |
1372 "port", 5222), TRUE); | |
1373 } else { | |
1374 js->srv_query_data = purple_srv_resolve("xmpp-client", | |
1375 "tcp", | |
1376 js->user->domain, | |
1377 srv_resolved_cb, | |
1378 js); | |
1379 } | |
1380 } | 1339 } |
1381 | 1340 |
1382 static void | 1341 static void |
1383 jabber_unregister_account_iq_cb(JabberStream *js, const char *from, | 1342 jabber_unregister_account_iq_cb(JabberStream *js, const char *from, |
1384 JabberIqType type, const char *id, | 1343 JabberIqType type, const char *id, |