--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/satorimessage.h Fri Aug 08 09:46:55 2025 +0800 @@ -0,0 +1,113 @@ +/* + * 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; +} + +static inline void +satori_user_from_json(JsonObject *user_obj, SatoriUser *out_user) { + out_user->id = json_object_get_string_member(user_obj, "id"); + out_user->name = json_object_get_string_member_with_default( + user_obj, "name", NULL); + out_user->nick = json_object_get_string_member_with_default( + user_obj, "nick", NULL); + out_user->avatar = json_object_get_string_member_with_default( + user_obj, "avatar", NULL); + out_user->is_bot = json_object_get_boolean_member_with_default( + user_obj, "is_bot", FALSE); +} + +#endif /* SATORI_MESSAGE_H */