/* direct_tls_xmpp_stream.c generated by valac 0.56.19, the Vala compiler
 * generated from direct_tls_xmpp_stream.vala, do not modify */

#include "xmpp-vala.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	XMPP_DIRECT_TLS_XMPP_STREAM_0_PROPERTY,
	XMPP_DIRECT_TLS_XMPP_STREAM_NUM_PROPERTIES
};
static GParamSpec* xmpp_direct_tls_xmpp_stream_properties[XMPP_DIRECT_TLS_XMPP_STREAM_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _xmpp_tls_xmpp_stream_on_invalid_cert_wrapper_unref0(var) ((var == NULL) ? NULL : (var = (xmpp_tls_xmpp_stream_on_invalid_cert_wrapper_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _XmppDirectTlsXmppStreamConnectData XmppDirectTlsXmppStreamConnectData;

struct _XmppDirectTlsXmppStreamPrivate {
	gchar* host;
	guint16 port;
	XmppTlsXmppStreamOnInvalidCertWrapper* on_invalid_cert;
};

struct _XmppDirectTlsXmppStreamConnectData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	XmppDirectTlsXmppStream* self;
	GSocketClient* client;
	GSocketClient* _tmp0_;
	const gchar* _tmp1_;
	GIOStream* io_stream;
	GSocketClient* _tmp2_;
	const gchar* _tmp3_;
	GCancellable* _tmp4_;
	GSocketConnection* _tmp5_;
	GTlsConnection* tls_connection;
	GIOStream* _tmp6_;
	XmppJid* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	GNetworkAddress* _tmp10_;
	GNetworkAddress* _tmp11_;
	GTlsClientConnection* _tmp12_;
	GTlsConnection* _tmp13_;
	GTlsConnection* _tmp14_;
	GTlsConnection* _tmp15_;
	GTlsConnection* _tmp16_;
	GError* e;
	GError* _tmp17_;
	GError* _tmp18_;
	GError* _vala1_e;
	const gchar* _tmp19_;
	GError* _tmp20_;
	const gchar* _tmp21_;
	GError* _tmp22_;
	GError* _inner_error0_;
};

static gint XmppDirectTlsXmppStream_private_offset;
static gpointer xmpp_direct_tls_xmpp_stream_parent_class = NULL;

static void xmpp_direct_tls_xmpp_stream_real_connect_data_free (gpointer _data);
static void xmpp_direct_tls_xmpp_stream_real_connect (XmppXmppStream* base,
                                               GAsyncReadyCallback _callback_,
                                               gpointer _user_data_);
static gboolean xmpp_direct_tls_xmpp_stream_real_connect_co (XmppDirectTlsXmppStreamConnectData* _data_);
static void xmpp_direct_tls_xmpp_stream_connect_ready (GObject* source_object,
                                                GAsyncResult* _res_,
                                                gpointer _user_data_);
static gboolean _xmpp_tls_xmpp_stream_on_invalid_certificate_g_tls_connection_accept_certificate (GTlsConnection* _sender,
                                                                                           GTlsCertificate* peer_cert,
                                                                                           GTlsCertificateFlags errors,
                                                                                           gpointer self);
static gboolean ___lambda5_ (XmppDirectTlsXmppStream* self,
                      GTlsCertificate* cert,
                      GTlsCertificateFlags flags);
static gboolean ____lambda5__g_tls_connection_accept_certificate (GTlsConnection* _sender,
                                                           GTlsCertificate* peer_cert,
                                                           GTlsCertificateFlags errors,
                                                           gpointer self);
static void xmpp_direct_tls_xmpp_stream_finalize (GObject * obj);
static GType xmpp_direct_tls_xmpp_stream_get_type_once (void);

static const gchar* XMPP_DIRECT_TLS_XMPP_STREAM_ADVERTISED_PROTOCOLS[2] = {"xmpp-client", NULL};

static inline gpointer
xmpp_direct_tls_xmpp_stream_get_instance_private (XmppDirectTlsXmppStream* self)
{
	return G_STRUCT_MEMBER_P (self, XmppDirectTlsXmppStream_private_offset);
}

static gpointer
_xmpp_tls_xmpp_stream_on_invalid_cert_wrapper_ref0 (gpointer self)
{
	return self ? xmpp_tls_xmpp_stream_on_invalid_cert_wrapper_ref (self) : NULL;
}

XmppDirectTlsXmppStream*
xmpp_direct_tls_xmpp_stream_construct (GType object_type,
                                       XmppJid* remote_name,
                                       const gchar* host,
                                       guint16 port,
                                       XmppTlsXmppStreamOnInvalidCertWrapper* on_invalid_cert)
{
	XmppDirectTlsXmppStream * self = NULL;
	gchar* _tmp0_;
	XmppTlsXmppStreamOnInvalidCertWrapper* _tmp1_;
	g_return_val_if_fail (remote_name != NULL, NULL);
	g_return_val_if_fail (host != NULL, NULL);
	g_return_val_if_fail (on_invalid_cert != NULL, NULL);
	self = (XmppDirectTlsXmppStream*) xmpp_tls_xmpp_stream_construct (object_type, remote_name);
	_tmp0_ = g_strdup (host);
	_g_free0 (self->priv->host);
	self->priv->host = _tmp0_;
	self->priv->port = port;
	_tmp1_ = _xmpp_tls_xmpp_stream_on_invalid_cert_wrapper_ref0 (on_invalid_cert);
	_xmpp_tls_xmpp_stream_on_invalid_cert_wrapper_unref0 (self->priv->on_invalid_cert);
	self->priv->on_invalid_cert = _tmp1_;
	return self;
}

XmppDirectTlsXmppStream*
xmpp_direct_tls_xmpp_stream_new (XmppJid* remote_name,
                                 const gchar* host,
                                 guint16 port,
                                 XmppTlsXmppStreamOnInvalidCertWrapper* on_invalid_cert)
{
	return xmpp_direct_tls_xmpp_stream_construct (XMPP_TYPE_DIRECT_TLS_XMPP_STREAM, remote_name, host, port, on_invalid_cert);
}

static void
xmpp_direct_tls_xmpp_stream_real_connect_data_free (gpointer _data)
{
	XmppDirectTlsXmppStreamConnectData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (XmppDirectTlsXmppStreamConnectData, _data_);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static void
xmpp_direct_tls_xmpp_stream_real_connect (XmppXmppStream* base,
                                          GAsyncReadyCallback _callback_,
                                          gpointer _user_data_)
{
	XmppDirectTlsXmppStream * self;
	XmppDirectTlsXmppStreamConnectData* _data_;
	XmppDirectTlsXmppStream* _tmp0_;
	self = (XmppDirectTlsXmppStream*) base;
	_data_ = g_slice_new0 (XmppDirectTlsXmppStreamConnectData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, xmpp_direct_tls_xmpp_stream_real_connect_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	xmpp_direct_tls_xmpp_stream_real_connect_co (_data_);
}

static void
xmpp_direct_tls_xmpp_stream_connect_finish (XmppXmppStream* base,
                                            GAsyncResult* _res_,
                                            GError** error)
{
	XmppDirectTlsXmppStreamConnectData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}

static void
xmpp_direct_tls_xmpp_stream_connect_ready (GObject* source_object,
                                           GAsyncResult* _res_,
                                           gpointer _user_data_)
{
	XmppDirectTlsXmppStreamConnectData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	xmpp_direct_tls_xmpp_stream_real_connect_co (_data_);
}

static gboolean
_xmpp_tls_xmpp_stream_on_invalid_certificate_g_tls_connection_accept_certificate (GTlsConnection* _sender,
                                                                                  GTlsCertificate* peer_cert,
                                                                                  GTlsCertificateFlags errors,
                                                                                  gpointer self)
{
	gboolean result;
	result = xmpp_tls_xmpp_stream_on_invalid_certificate ((XmppTlsXmppStream*) self, peer_cert, errors);
	return result;
}

static gboolean
___lambda5_ (XmppDirectTlsXmppStream* self,
             GTlsCertificate* cert,
             GTlsCertificateFlags flags)
{
	XmppTlsXmppStreamOnInvalidCertWrapper* _tmp0_;
	XmppTlsXmppStreamOnInvalidCert _tmp1_;
	gpointer _tmp1__target;
	gboolean result;
	g_return_val_if_fail (cert != NULL, FALSE);
	_tmp0_ = self->priv->on_invalid_cert;
	_tmp1_ = _tmp0_->func;
	_tmp1__target = _tmp0_->func_target;
	result = _tmp1_ (cert, flags, _tmp1__target);
	return result;
}

static gboolean
____lambda5__g_tls_connection_accept_certificate (GTlsConnection* _sender,
                                                  GTlsCertificate* peer_cert,
                                                  GTlsCertificateFlags errors,
                                                  gpointer self)
{
	gboolean result;
	result = ___lambda5_ ((XmppDirectTlsXmppStream*) self, peer_cert, errors);
	return result;
}

static gpointer
_g_error_copy0 (gpointer self)
{
	return self ? g_error_copy (self) : NULL;
}

static gboolean
xmpp_direct_tls_xmpp_stream_real_connect_co (XmppDirectTlsXmppStreamConnectData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		case 2:
		goto _state_2;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = g_socket_client_new ();
	_data_->client = _data_->_tmp0_;
	{
		_data_->_tmp1_ = _data_->self->priv->host;
		g_debug ("direct_tls_xmpp_stream.vala:19: Connecting to %s:%i (tls)", _data_->_tmp1_, (gint) _data_->self->priv->port);
		_data_->_tmp2_ = _data_->client;
		_data_->_tmp3_ = _data_->self->priv->host;
		_data_->_tmp4_ = ((XmppIoXmppStream*) _data_->self)->cancellable;
		_data_->_state_ = 1;
		g_socket_client_connect_to_host_async (_data_->_tmp2_, _data_->_tmp3_, _data_->self->priv->port, _data_->_tmp4_, xmpp_direct_tls_xmpp_stream_connect_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp5_ = g_socket_client_connect_to_host_finish (_data_->_tmp2_, _data_->_res_, &_data_->_inner_error0_);
		_data_->io_stream = (GIOStream*) _data_->_tmp5_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			if (_data_->_inner_error0_->domain == G_IO_ERROR) {
				goto __catch0_g_io_error;
			}
			goto __catch0_g_error;
		}
		_data_->_tmp6_ = _data_->io_stream;
		_data_->_tmp7_ = ((XmppXmppStream*) _data_->self)->remote_name;
		_data_->_tmp8_ = xmpp_jid_to_string (_data_->_tmp7_);
		_data_->_tmp9_ = _data_->_tmp8_;
		_data_->_tmp10_ = (GNetworkAddress*) g_network_address_new (_data_->_tmp9_, _data_->self->priv->port);
		_data_->_tmp11_ = _data_->_tmp10_;
		_data_->_tmp12_ = g_tls_client_connection_new (_data_->_tmp6_, (GSocketConnectable*) _data_->_tmp11_, &_data_->_inner_error0_);
		_data_->_tmp13_ = (GTlsConnection*) _data_->_tmp12_;
		_g_object_unref0 (_data_->_tmp11_);
		_g_free0 (_data_->_tmp9_);
		_data_->tls_connection = _data_->_tmp13_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			_g_object_unref0 (_data_->io_stream);
			if (_data_->_inner_error0_->domain == G_IO_ERROR) {
				goto __catch0_g_io_error;
			}
			goto __catch0_g_error;
		}
		_data_->_tmp14_ = _data_->tls_connection;
		g_signal_connect_object (_data_->_tmp14_, "accept-certificate", (GCallback) _xmpp_tls_xmpp_stream_on_invalid_certificate_g_tls_connection_accept_certificate, (XmppTlsXmppStream*) _data_->self, 0);
		_data_->_tmp15_ = _data_->tls_connection;
		g_signal_connect_object (_data_->_tmp15_, "accept-certificate", (GCallback) ____lambda5__g_tls_connection_accept_certificate, _data_->self, 0);
		_data_->_tmp16_ = _data_->tls_connection;
		xmpp_io_xmpp_stream_reset_stream ((XmppIoXmppStream*) _data_->self, (GIOStream*) _data_->_tmp16_);
		_data_->_state_ = 2;
		xmpp_xmpp_stream_setup ((XmppXmppStream*) _data_->self, xmpp_direct_tls_xmpp_stream_connect_ready, _data_);
		return FALSE;
		_state_2:
		xmpp_xmpp_stream_setup_finish ((XmppXmppStream*) _data_->self, _data_->_res_, &_data_->_inner_error0_);
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			_g_object_unref0 (_data_->tls_connection);
			_g_object_unref0 (_data_->io_stream);
			if (_data_->_inner_error0_->domain == G_IO_ERROR) {
				goto __catch0_g_io_error;
			}
			goto __catch0_g_error;
		}
		xmpp_xmpp_stream_attach_negotation_modules ((XmppXmppStream*) _data_->self);
		_g_object_unref0 (_data_->tls_connection);
		_g_object_unref0 (_data_->io_stream);
	}
	goto __finally0;
	__catch0_g_io_error:
	{
		_data_->e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp17_ = _data_->e;
		_data_->_tmp18_ = _g_error_copy0 (_data_->_tmp17_);
		_data_->_inner_error0_ = _data_->_tmp18_;
		_g_error_free0 (_data_->e);
		goto __finally0;
	}
	goto __finally0;
	__catch0_g_error:
	{
		_data_->_vala1_e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp19_ = _data_->self->priv->host;
		_data_->_tmp20_ = _data_->_vala1_e;
		_data_->_tmp21_ = _data_->_tmp20_->message;
		_data_->_tmp22_ = g_error_new (G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED, "Failed connecting to %s:%i (tls): %s", _data_->_tmp19_, (gint) _data_->self->priv->port, _data_->_tmp21_);
		_data_->_inner_error0_ = _data_->_tmp22_;
		_g_error_free0 (_data_->_vala1_e);
		goto __finally0;
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		if (_data_->_inner_error0_->domain == G_IO_ERROR) {
			g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
			_g_object_unref0 (_data_->client);
			g_object_unref (_data_->_async_result);
			return FALSE;
		} else {
			_g_object_unref0 (_data_->client);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
			g_clear_error (&_data_->_inner_error0_);
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
	}
	_g_object_unref0 (_data_->client);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
xmpp_direct_tls_xmpp_stream_class_init (XmppDirectTlsXmppStreamClass * klass,
                                        gpointer klass_data)
{
	xmpp_direct_tls_xmpp_stream_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &XmppDirectTlsXmppStream_private_offset);
	((XmppXmppStreamClass *) klass)->connect = (void (*) (XmppXmppStream*, GAsyncReadyCallback, gpointer)) xmpp_direct_tls_xmpp_stream_real_connect;
	((XmppXmppStreamClass *) klass)->connect_finish = (void (*) (XmppXmppStream*, GAsyncResult*, GError**)) xmpp_direct_tls_xmpp_stream_connect_finish;
	G_OBJECT_CLASS (klass)->finalize = xmpp_direct_tls_xmpp_stream_finalize;
}

static void
xmpp_direct_tls_xmpp_stream_instance_init (XmppDirectTlsXmppStream * self,
                                           gpointer klass)
{
	self->priv = xmpp_direct_tls_xmpp_stream_get_instance_private (self);
}

static void
xmpp_direct_tls_xmpp_stream_finalize (GObject * obj)
{
	XmppDirectTlsXmppStream * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, XMPP_TYPE_DIRECT_TLS_XMPP_STREAM, XmppDirectTlsXmppStream);
	_g_free0 (self->priv->host);
	_xmpp_tls_xmpp_stream_on_invalid_cert_wrapper_unref0 (self->priv->on_invalid_cert);
	G_OBJECT_CLASS (xmpp_direct_tls_xmpp_stream_parent_class)->finalize (obj);
}

static GType
xmpp_direct_tls_xmpp_stream_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (XmppDirectTlsXmppStreamClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) xmpp_direct_tls_xmpp_stream_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (XmppDirectTlsXmppStream), 0, (GInstanceInitFunc) xmpp_direct_tls_xmpp_stream_instance_init, NULL };
	GType xmpp_direct_tls_xmpp_stream_type_id;
	xmpp_direct_tls_xmpp_stream_type_id = g_type_register_static (XMPP_TYPE_TLS_XMPP_STREAM, "XmppDirectTlsXmppStream", &g_define_type_info, 0);
	XmppDirectTlsXmppStream_private_offset = g_type_add_instance_private (xmpp_direct_tls_xmpp_stream_type_id, sizeof (XmppDirectTlsXmppStreamPrivate));
	return xmpp_direct_tls_xmpp_stream_type_id;
}

GType
xmpp_direct_tls_xmpp_stream_get_type (void)
{
	static volatile gsize xmpp_direct_tls_xmpp_stream_type_id__once = 0;
	if (g_once_init_enter (&xmpp_direct_tls_xmpp_stream_type_id__once)) {
		GType xmpp_direct_tls_xmpp_stream_type_id;
		xmpp_direct_tls_xmpp_stream_type_id = xmpp_direct_tls_xmpp_stream_get_type_once ();
		g_once_init_leave (&xmpp_direct_tls_xmpp_stream_type_id__once, xmpp_direct_tls_xmpp_stream_type_id);
	}
	return xmpp_direct_tls_xmpp_stream_type_id__once;
}

