comparison libpurple/protocols/bonjour/jabber.c @ 21441:da75dd6c41fb

Leak fix, cleanup and code reuse.
author Daniel Atallah <daniel.atallah@gmail.com>
date Mon, 12 Nov 2007 05:09:17 +0000
parents d8106b63b0a4
children 2b6553584385
comparison
equal deleted inserted replaced
21440:d8106b63b0a4 21441:da75dd6c41fb
709 bb->conversation = NULL; 709 bb->conversation = NULL;
710 return; 710 return;
711 } 711 }
712 } 712 }
713 713
714 int 714 static PurpleBuddy *
715 bonjour_jabber_send_message(BonjourJabber *data, const gchar *to, const gchar *body) 715 _find_or_start_conversation(BonjourJabber *data, const gchar *to)
716 { 716 {
717 xmlnode *message_node, *node, *node2; 717 PurpleBuddy *pb = NULL;
718 gchar *message; 718 BonjourBuddy *bb = NULL;
719 PurpleBuddy *pb; 719
720 BonjourBuddy *bb; 720 g_return_val_if_fail(data != NULL, NULL);
721 int ret; 721 g_return_val_if_fail(to != NULL, NULL);
722 722
723 pb = purple_find_buddy(data->account, to); 723 pb = purple_find_buddy(data->account, to);
724 if (pb == NULL) { 724 if (pb == NULL || pb->proto_data == NULL)
725 purple_debug_info("bonjour", "Can't send a message to an offline buddy (%s).\n", to);
726 /* You can not send a message to an offline buddy */ 725 /* You can not send a message to an offline buddy */
727 return -10000; 726 return NULL;
728 } 727
729 728 bb = (BonjourBuddy *) pb->proto_data;
730 bb = pb->proto_data;
731 729
732 /* Check if there is a previously open conversation */ 730 /* Check if there is a previously open conversation */
733 if (bb->conversation == NULL) 731 if (bb->conversation == NULL)
734 { 732 {
735 PurpleProxyConnectData *connect_data; 733 PurpleProxyConnectData *connect_data;
736 PurpleProxyInfo *proxy_info; 734 PurpleProxyInfo *proxy_info;
735
736 purple_debug_info("Bonjour", "Starting conversation with %s\n", to);
737 737
738 /* Make sure that the account always has a proxy of "none". 738 /* Make sure that the account always has a proxy of "none".
739 * This is kind of dirty, but proxy_connect_none() isn't exposed. */ 739 * This is kind of dirty, but proxy_connect_none() isn't exposed. */
740 proxy_info = purple_account_get_proxy_info(data->account); 740 proxy_info = purple_account_get_proxy_info(data->account);
741 if (proxy_info == NULL) { 741 if (proxy_info == NULL) {
742 proxy_info = purple_proxy_info_new(); 742 proxy_info = purple_proxy_info_new();
743 purple_account_set_proxy_info(data->account, proxy_info); 743 purple_account_set_proxy_info(data->account, proxy_info);
744 } 744 }
745 purple_proxy_info_set_type(proxy_info, PURPLE_PROXY_NONE); 745 purple_proxy_info_set_type(proxy_info, PURPLE_PROXY_NONE);
746 746
747 connect_data = 747 connect_data = purple_proxy_connect(data->account->gc, data->account,
748 purple_proxy_connect(data->account->gc, data->account, bb->ip, 748 bb->ip, bb->port_p2pj, _connected_to_buddy, pb);
749 bb->port_p2pj, _connected_to_buddy, pb);
750 749
751 if (connect_data == NULL) { 750 if (connect_data == NULL) {
752 purple_debug_error("bonjour", "Unable to connect to buddy (%s).\n", to); 751 purple_debug_error("bonjour", "Unable to connect to buddy (%s).\n", to);
753 return -10001; 752 return NULL;
754 } 753 }
755 754
756 bb->conversation = bonjour_jabber_conv_new(); 755 bb->conversation = bonjour_jabber_conv_new();
757 bb->conversation->connect_data = connect_data; 756 bb->conversation->connect_data = connect_data;
758 /* We don't want _send_data() to register the tx_handler; 757 /* We don't want _send_data() to register the tx_handler;
759 * that neeeds to wait until we're actually connected. */ 758 * that neeeds to wait until we're actually connected. */
760 bb->conversation->tx_handler = 0; 759 bb->conversation->tx_handler = 0;
761 } 760 }
761 return pb;
762 }
763
764 int
765 bonjour_jabber_send_message(BonjourJabber *data, const gchar *to, const gchar *body)
766 {
767 xmlnode *message_node, *node, *node2;
768 gchar *message;
769 PurpleBuddy *pb;
770 BonjourBuddy *bb;
771 int ret;
772
773 pb = _find_or_start_conversation(data, to);
774 if (pb == NULL) {
775 purple_debug_info("bonjour", "Can't send a message to an offline buddy (%s).\n", to);
776 /* You can not send a message to an offline buddy */
777 return -10000;
778 }
779
780 bb = pb->proto_data;
762 781
763 message_node = xmlnode_new("message"); 782 message_node = xmlnode_new("message");
764 xmlnode_set_attrib(message_node, "to", bb->name); 783 xmlnode_set_attrib(message_node, "to", bb->name);
765 xmlnode_set_attrib(message_node, "from", purple_account_get_username(data->account)); 784 xmlnode_set_attrib(message_node, "from", purple_account_get_username(data->account));
766 xmlnode_set_attrib(message_node, "type", "chat"); 785 xmlnode_set_attrib(message_node, "type", "chat");
850 bb->conversation = NULL; 869 bb->conversation = NULL;
851 } 870 }
852 871
853 g_slist_free(buddies); 872 g_slist_free(buddies);
854 } 873 }
855 }
856 static PurpleBuddy *
857 _start_conversation(BonjourJabber *data, const gchar *to)
858 {
859 PurpleBuddy *pb = NULL;
860 BonjourBuddy *bb = NULL;
861
862 if(data == NULL || to == NULL)
863 return NULL;
864
865 purple_debug_info("Bonjour", "start-conversation with %s - \n", to);
866
867 pb = purple_find_buddy(data->account, to);
868 if (pb == NULL)
869 /* You can not send a message to an offline buddy */
870 return NULL;
871
872 bb = (BonjourBuddy *) pb->proto_data;
873 if(bb == NULL)
874 return NULL;
875
876 /* Check if there is a previously open conversation */
877 if (bb->conversation == NULL)
878 {
879 PurpleProxyConnectData *connect_data;
880 PurpleProxyInfo *proxy_info;
881
882 /* Make sure that the account always has a proxy of "none".
883 * This is kind of dirty, but proxy_connect_none() isn't exposed. */
884 proxy_info = purple_account_get_proxy_info(data->account);
885 if (proxy_info == NULL) {
886 proxy_info = purple_proxy_info_new();
887 purple_account_set_proxy_info(data->account, proxy_info);
888 }
889 purple_proxy_info_set_type(proxy_info, PURPLE_PROXY_NONE);
890
891 connect_data = purple_proxy_connect(data->account->gc, data->account,
892 bb->ip, bb->port_p2pj, _connected_to_buddy, pb);
893
894 if (connect_data == NULL) {
895 purple_debug_error("bonjour", "Unable to connect to buddy (%s).\n", to);
896 return NULL;
897 }
898
899 bb->conversation = bonjour_jabber_conv_new();
900 bb->conversation->connect_data = connect_data;
901 /* We don't want _send_data() to register the tx_handler;
902 * that neeeds to wait until we're actually connected. */
903 bb->conversation->tx_handler = 0;
904 }
905 return pb;
906 } 874 }
907 875
908 XepIq * 876 XepIq *
909 xep_iq_new(void *data, XepIqType type, const gchar *to, const gchar *id) 877 xep_iq_new(void *data, XepIqType type, const gchar *to, const gchar *id)
910 { 878 {
996 } 964 }
997 965
998 int 966 int
999 xep_iq_send(XepIq *iq) 967 xep_iq_send(XepIq *iq)
1000 { 968 {
1001 char *msg = NULL;
1002 gint msg_len = 0;
1003 int ret = -1; 969 int ret = -1;
1004 PurpleBuddy *pb = NULL; 970 PurpleBuddy *pb = NULL;
1005 /* Convert xml node into stream */ 971
1006 msg = xmlnode_to_str(iq->node, &msg_len); 972 /* start the talk, reuse the message socket */
973 pb = _find_or_start_conversation ((BonjourJabber*)iq->data, iq->to);
974 /* Send the message */
975 if (pb != NULL) {
976 /* Convert xml node into stream */
977 gchar *msg = xmlnode_to_str(iq->node, NULL);
978 ret = _send_data(pb, msg);
979 g_free(msg);
980 }
1007 xmlnode_free(iq->node); 981 xmlnode_free(iq->node);
1008 /* start the talk, reuse the message socket */ 982
1009 pb = _start_conversation ((BonjourJabber*)iq->data, iq->to); 983 return (ret >= 0) ? 0 : -1;
1010 /* Send the message */
1011 if (pb != NULL)
1012 ret = _send_data(pb, msg);
1013 g_free(msg);
1014 if (ret == -1)
1015 return -1;
1016 return 0;
1017 } 984 }
1018 985
1019 /* This returns a ';' delimited string containing all non-localhost IPs */ 986 /* This returns a ';' delimited string containing all non-localhost IPs */
1020 char * 987 char *
1021 purple_network_get_my_ip_ext2(int fd) 988 purple_network_get_my_ip_ext2(int fd)