satorimessage.h

Sun, 10 Aug 2025 23:53:22 +0800

author
Gong Zhile <gongzl@stu.hebust.edu.cn>
date
Sun, 10 Aug 2025 23:53:22 +0800
changeset 3
33a7b189a2c6
parent 1
98bcf06036b8
permissions
-rw-r--r--

Various improvement, Support configuration from UI

/*
 * 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 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 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