jabber.c

来自「Linux下的多协议即时通讯程序源代码」· C语言 代码 · 共 855 行 · 第 1/2 页

C
855
字号
		return;	else if (ret <= 0) {		const char *err = strerror(errno);		PurpleConversation *conv;		purple_debug_error("bonjour", "Error starting stream with buddy %s at %s:%d error: %s\n",				   purple_buddy_get_name(pb), bb->ip ? bb->ip : "(null)", bb->port_p2pj, err ? err : "(null)");		conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, bb->name, pb->account);		if (conv != NULL)			purple_conversation_write(conv, NULL,				  _("Unable to send the message, the conversation couldn't be started."),				  PURPLE_MESSAGE_SYSTEM, time(NULL));		bonjour_jabber_close_conversation(bb->conversation);		bb->conversation = NULL;		return;	}	/* This is EXTREMELY unlikely to happen */	if (ret < len) {		char *tmp = g_strdup(ss->msg + ret);		g_free(ss->msg);		ss->msg = tmp;		return;	}	/* Stream started; process the send buffer if there is one*/	purple_input_remove(bb->conversation->tx_handler);	bb->conversation->tx_handler= -1;	bb->conversation->stream_started = TRUE;	g_free(ss->msg);	g_free(ss);	bb->conversation->stream_data = NULL;	if (ss->tx_handler_cb) {		bb->conversation->tx_handler = purple_input_add(source, PURPLE_INPUT_WRITE,			ss->tx_handler_cb, pb);		/* We can probably write the data now. */		(ss->tx_handler_cb)(pb, source, PURPLE_INPUT_WRITE);	}}static void_server_socket_handler(gpointer data, int server_socket, PurpleInputCondition condition){	PurpleBuddy *pb = NULL;	struct sockaddr_in their_addr; /* connector's address information */	socklen_t sin_size = sizeof(struct sockaddr);	int client_socket;	BonjourBuddy *bb;	char *address_text = NULL;	PurpleBuddyList *bl = purple_get_blist();	struct _check_buddy_by_address_t *cbba;	/* Check that it is a read condition */	if (condition != PURPLE_INPUT_READ)		return;	if ((client_socket = accept(server_socket, (struct sockaddr *)&their_addr, &sin_size)) == -1)		return;	fcntl(client_socket, F_SETFL, O_NONBLOCK);	/* Look for the buddy that has opened the conversation and fill information */	address_text = inet_ntoa(their_addr.sin_addr);	cbba = g_new0(struct _check_buddy_by_address_t, 1);	cbba->address = address_text;	cbba->pb = &pb;	cbba->bj = data;	g_hash_table_foreach(bl->buddies, _check_buddy_by_address, cbba);	g_free(cbba);	if (pb == NULL)	{		purple_debug_info("bonjour", "We don't like invisible buddies, this is not a superheros comic\n");		close(client_socket);		return;	}	bb = pb->proto_data;	/* Check if the conversation has been previously started */	if (bb->conversation == NULL)	{		int ret, len;		char *stream_start = g_strdup_printf(DOCTYPE, purple_account_get_username(pb->account),			purple_buddy_get_name(pb));		len = strlen(stream_start);		/* Start the stream */		ret = send(client_socket, stream_start, len, 0);		if (ret == -1 && errno == EAGAIN)			ret = 0;		else if (ret <= 0) {			const char *err = strerror(errno);			purple_debug_error("bonjour", "Error starting stream with buddy %s at %s:%d error: %s\n",					   purple_buddy_get_name(pb), bb->ip ? bb->ip : "(null)", bb->port_p2pj, err ? err : "(null)");			close(client_socket);			g_free(stream_start);			return;		}		bb->conversation = bonjour_jabber_conv_new();		bb->conversation->socket = client_socket;		bb->conversation->rx_handler = purple_input_add(client_socket,			PURPLE_INPUT_READ, _client_socket_handler, pb);		/* This is unlikely to happen */		if (ret < len) {			struct _stream_start_data *ss = g_new(struct _stream_start_data, 1);			ss->msg = g_strdup(stream_start + ret);			ss->tx_handler_cb = NULL; /* We have nothing to write yet */			bb->conversation->stream_data = ss;			/* Finish sending the stream start */			bb->conversation->tx_handler = purple_input_add(client_socket,				PURPLE_INPUT_WRITE, _start_stream, pb);		} else {			bb->conversation->stream_started = TRUE;		}		g_free(stream_start);	} else {		close(client_socket);	}}gintbonjour_jabber_start(BonjourJabber *data){	struct sockaddr_in my_addr;	int yes = 1;	int i;	gboolean bind_successful;	/* Open a listening socket for incoming conversations */	if ((data->socket = socket(PF_INET, SOCK_STREAM, 0)) < 0)	{		purple_debug_error("bonjour", "Cannot open socket: %s\n", strerror(errno));		purple_connection_error(data->account->gc, _("Cannot open socket"));		return -1;	}	/* Make the socket reusable */	if (setsockopt(data->socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0)	{		purple_debug_error("bonjour", "Error setting socket options: %s\n", strerror(errno));		purple_connection_error(data->account->gc, _("Error setting socket options"));		return -1;	}	memset(&my_addr, 0, sizeof(struct sockaddr_in));	my_addr.sin_family = PF_INET;	/* Attempt to find a free port */	bind_successful = FALSE;	for (i = 0; i < 10; i++)	{		my_addr.sin_port = htons(data->port);		if (bind(data->socket, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) == 0)		{			bind_successful = TRUE;			break;		}		data->port++;	}	/* On no!  We tried 10 ports and could not bind to ANY of them */	if (!bind_successful)	{		purple_debug_error("bonjour", "Cannot bind socket: %s\n", strerror(errno));		purple_connection_error(data->account->gc, _("Could not bind socket to port"));		return -1;	}	/* Attempt to listen on the bound socket */	if (listen(data->socket, 10) != 0)	{		purple_debug_error("bonjour", "Cannot listen on socket: %s\n", strerror(errno));		purple_connection_error(data->account->gc, _("Could not listen on socket"));		return -1;	}#if 0	/* TODO: Why isn't this being used? */	data->socket = purple_network_listen(data->port, SOCK_STREAM);	if (data->socket == -1)	{		purple_debug_error("bonjour", "No se ha podido crear el socket\n");	}#endif	/* Open a watcher in the socket we have just opened */	data->watcher_id = purple_input_add(data->socket, PURPLE_INPUT_READ, _server_socket_handler, data);	return data->port;}static void_connected_to_buddy(gpointer data, gint source, const gchar *error){	PurpleBuddy *pb = data;	BonjourBuddy *bb = pb->proto_data;	int len, ret;	char *stream_start = g_strdup_printf(DOCTYPE, purple_account_get_username(pb->account), purple_buddy_get_name(pb));	bb->conversation->connect_data = NULL;	if (source < 0) {		PurpleConversation *conv;		purple_debug_error("bonjour", "Error connecting to buddy %s at %s:%d error: %s\n",				   purple_buddy_get_name(pb), bb->ip ? bb->ip : "(null)", bb->port_p2pj, error ? error : "(null)");		conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, bb->name, pb->account);		if (conv != NULL)			purple_conversation_write(conv, NULL,				  _("Unable to send the message, the conversation couldn't be started."),				  PURPLE_MESSAGE_SYSTEM, time(NULL));		bonjour_jabber_close_conversation(bb->conversation);		bb->conversation = NULL;		return;	}	len = strlen(stream_start);	/* Start the stream and send queued messages */	ret = send(source, stream_start, len, 0);	if (ret == -1 && errno == EAGAIN)		ret = 0;	else if (ret <= 0) {		const char *err = strerror(errno);		PurpleConversation *conv;		purple_debug_error("bonjour", "Error starting stream with buddy %s at %s:%d error: %s\n",				   purple_buddy_get_name(pb), bb->ip ? bb->ip : "(null)", bb->port_p2pj, err ? err : "(null)");		conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, bb->name, pb->account);		if (conv != NULL)			purple_conversation_write(conv, NULL,				  _("Unable to send the message, the conversation couldn't be started."),				  PURPLE_MESSAGE_SYSTEM, time(NULL));		close(source);		bonjour_jabber_close_conversation(bb->conversation);		bb->conversation = NULL;		g_free(stream_start);		return;	}	bb->conversation->socket = source;	bb->conversation->rx_handler = purple_input_add(source,		PURPLE_INPUT_READ, _client_socket_handler, pb);	/* This is unlikely to happen */	if (ret < len) {		struct _stream_start_data *ss = g_new(struct _stream_start_data, 1);		ss->msg = g_strdup(stream_start + ret);		ss->tx_handler_cb = _send_data_write_cb;		bb->conversation->stream_data = ss;		/* Finish sending the stream start */		bb->conversation->tx_handler = purple_input_add(source,			PURPLE_INPUT_WRITE, _start_stream, pb);	}	/* Process the send buffer */	else {		bb->conversation->stream_started = TRUE;		/* Watch for when we can write the buffered messages */		bb->conversation->tx_handler = purple_input_add(source, PURPLE_INPUT_WRITE,			_send_data_write_cb, pb);		/* We can probably write the data now. */		_send_data_write_cb(pb, source, PURPLE_INPUT_WRITE);	}	g_free(stream_start);}intbonjour_jabber_send_message(BonjourJabber *data, const gchar *to, const gchar *body){	xmlnode *message_node, *node, *node2;	gchar *message;	PurpleBuddy *pb;	BonjourBuddy *bb;	int ret;	pb = purple_find_buddy(data->account, to);	if (pb == NULL) {		purple_debug_info("bonjour", "Can't send a message to an offline buddy (%s).\n", to);		/* You can not send a message to an offline buddy */		return -10000;	}	bb = pb->proto_data;	/* Check if there is a previously open conversation */	if (bb->conversation == NULL)	{		PurpleProxyConnectData *connect_data;		PurpleProxyInfo *proxy_info;		/* Make sure that the account always has a proxy of "none".		 * This is kind of dirty, but proxy_connect_none() isn't exposed. */		proxy_info = purple_account_get_proxy_info(data->account);		if (proxy_info == NULL) {			proxy_info = purple_proxy_info_new();			purple_account_set_proxy_info(data->account, proxy_info);		}		purple_proxy_info_set_type(proxy_info, PURPLE_PROXY_NONE);		connect_data =			purple_proxy_connect(data->account->gc, data->account, bb->ip,					     bb->port_p2pj, _connected_to_buddy, pb);		if (connect_data == NULL) {			purple_debug_error("bonjour", "Unable to connect to buddy (%s).\n", to);			return -10001;		}		bb->conversation = bonjour_jabber_conv_new();		bb->conversation->connect_data = connect_data;		/* We don't want _send_data() to register the tx_handler;		 * that neeeds to wait until we're actually connected. */		bb->conversation->tx_handler = 0;	}	message_node = xmlnode_new("message");	xmlnode_set_attrib(message_node, "to", bb->name);	xmlnode_set_attrib(message_node, "from", purple_account_get_username(data->account));	xmlnode_set_attrib(message_node, "type", "chat");	/* Enclose the message from the UI within a "font" node */	node = xmlnode_new_child(message_node, "body");	message = purple_markup_strip_html(body);	xmlnode_insert_data(node, message, strlen(message));	g_free(message);	node = xmlnode_new_child(message_node, "html");	xmlnode_set_namespace(node, "http://www.w3.org/1999/xhtml");	node = xmlnode_new_child(node, "body");	message = g_strdup_printf("<font>%s</font>", body);	node2 = xmlnode_from_str(message, strlen(message));	g_free(message);	xmlnode_insert_child(node, node2);	node = xmlnode_new_child(message_node, "x");	xmlnode_set_namespace(node, "jabber:x:event");	xmlnode_insert_child(node, xmlnode_new("composing"));	message = xmlnode_to_str(message_node, NULL);	xmlnode_free(message_node);	ret = _send_data(pb, message) >= 0;	g_free(message);	return ret;}voidbonjour_jabber_close_conversation(BonjourJabberConversation *bconv){	if (bconv != NULL)	{		/* Close the socket and remove the watcher */		if (bconv->socket >= 0) {			/* Send the end of the stream to the other end of the conversation */			if (bconv->stream_started)				send(bconv->socket, STREAM_END, strlen(STREAM_END), 0);			/* TODO: We're really supposed to wait for "</stream:stream>" before closing the socket */			close(bconv->socket);		}		if (bconv->rx_handler != -1)			purple_input_remove(bconv->rx_handler);		if (bconv->tx_handler > 0)			purple_input_remove(bconv->tx_handler);		/* Free all the data related to the conversation */		purple_circ_buffer_destroy(bconv->tx_buf);		if (bconv->connect_data != NULL)			purple_proxy_connect_cancel(bconv->connect_data);		if (bconv->stream_data != NULL) {			struct _stream_start_data *ss = bconv->stream_data;			g_free(ss->msg);			g_free(ss);		}		g_free(bconv);	}}voidbonjour_jabber_stop(BonjourJabber *data){	/* Close the server socket and remove the watcher */	if (data->socket >= 0)		close(data->socket);	if (data->watcher_id > 0)		purple_input_remove(data->watcher_id);	/* Close all the conversation sockets and remove all the watchers after sending end streams */	if (data->account->gc != NULL)	{		GSList *buddies, *l;		buddies = purple_find_buddies(data->account, purple_account_get_username(data->account));		for (l = buddies; l; l = l->next) {			BonjourBuddy *bb = ((PurpleBuddy*) l->data)->proto_data;			bonjour_jabber_close_conversation(bb->conversation);			bb->conversation = NULL;		}		g_slist_free(buddies);	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?