satorimessage.h

Sat, 09 Aug 2025 00:19:03 +0800

author
William Goodspeed <goodspeed@mailo.cat>
date
Sat, 09 Aug 2025 00:19:03 +0800
changeset 1
98bcf06036b8
parent 0
cc7c1f9d20f7
child 3
33a7b189a2c6
permissions
-rw-r--r--

Another Minor Milestone Reached, Conversation Creation & Recv works now

/*
 * Purple Satori Plugin - Satori Protocol Plugin for Purple3
 * Copyright (C) 2025 Gong Zhile
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#ifndef SATORI_MESSAGE_H
#define SATORI_MESSAGE_H

#include <glib.h>
#include <glib-object.h>
#include <libsoup/soup.h>
#include <libsoup/soup-message.h>
#include <json-glib/json-glib.h>

#include "satoritypes.h"
#include "purplesatoriconnection.h"

#define SATORI_ENDPOINT(path) "http://" PURPLE_SATORI_HOST path

#define JBO(b) json_builder_begin_object(b)
#define JEO(b) json_builder_end_object(b)
#define JBSN(b, name) json_builder_set_member_name(b, name)
#define JBSI(b, val) json_builder_add_int_value(b, val)
#define JBSS(b, val) json_builder_add_string_value(b, val)
#define JBA(b, name, val) (JBSN(b, name), JBSS(b, val))
#define JBAI(b, name, val) (JBSN(b, name), JBSI(b, val))

static inline GBytes *
JB2GBYTES(JsonBuilder *builder)
{
	JsonNode *root = json_builder_get_root(builder);
	JsonGenerator *gen = json_generator_new();
	json_generator_set_root(gen, root);

	gchar *json_str = json_generator_to_data(gen, NULL);
	GBytes *bytes = g_bytes_new_take(json_str, strlen(json_str));

	json_node_free(root);
	g_object_unref(gen);

	return bytes;
}

#define JB_BEGIN_OBJ(name) \
	JsonBuilder *name = json_builder_new(); \
	JBO(name);

#define JB_END_OBJ(lval, name) \
	do { \
		JEO(b); \
		lval = JB2GBYTES(name); \
		g_object_unref(b); \
	} while (0)

static inline SoupMessage *
satori_message_new(const gchar *method, const gchar *url)
{
	SoupMessage *msg = soup_message_new(method, url);
	SoupMessageHeaders *headers = soup_message_get_request_headers(msg);
	soup_message_headers_append(headers, "Satori-Platform",
				    PURPLE_SATORI_PLATFORM);
	soup_message_headers_append(headers, "Satori-User-ID",
				    PURPLE_SATORI_USER_ID);
	return msg;
}

static inline GBytes *
satori_message_gen_ident(const gchar *token, gint sn)
{
	GBytes *msg = NULL;

	JB_BEGIN_OBJ(b);
	JBAI(b, "op", SATORI_WEBSOCKET_OP_IDENTIFY);
	JBSN(b, "body");

	JBO(b);
	if (token)
		JBA(b, "token", token);
	if (sn)
		JBAI(b, "sn", sn);
	JEO(b);

	JB_END_OBJ(msg, b);
	return msg;
}

#endif	/* SATORI_MESSAGE_H */

mercurial