Sun, 10 Aug 2025 23:03:27 +0800
satoriformat.{c,h}: Add message parsing support
| 0 | 1 | /* |
| 2 | * Purple Satori Plugin - Satori Protocol Plugin for Purple3 | |
| 3 | * Copyright (C) 2025 Gong Zhile | |
| 4 | * | |
| 5 | * This library is free software; you can redistribute it and/or | |
| 6 | * modify it under the terms of the GNU Lesser General Public | |
| 7 | * License as published by the Free Software Foundation; either | |
| 8 | * version 2 of the License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This library is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General Public | |
| 16 | * License along with this library; if not, see <https://www.gnu.org/licenses/>. | |
| 17 | */ | |
| 18 | ||
| 19 | #ifndef SATORI_MESSAGE_H | |
| 20 | #define SATORI_MESSAGE_H | |
| 21 | ||
| 22 | #include <glib.h> | |
| 23 | #include <glib-object.h> | |
| 24 | #include <libsoup/soup.h> | |
| 25 | #include <libsoup/soup-message.h> | |
| 26 | #include <json-glib/json-glib.h> | |
| 27 | ||
| 28 | #include "satoritypes.h" | |
| 29 | #include "purplesatoriconnection.h" | |
| 30 | ||
| 31 | #define SATORI_ENDPOINT(path) "http://" PURPLE_SATORI_HOST path | |
| 32 | ||
| 33 | #define JBO(b) json_builder_begin_object(b) | |
| 34 | #define JEO(b) json_builder_end_object(b) | |
| 35 | #define JBSN(b, name) json_builder_set_member_name(b, name) | |
| 36 | #define JBSI(b, val) json_builder_add_int_value(b, val) | |
| 37 | #define JBSS(b, val) json_builder_add_string_value(b, val) | |
| 38 | #define JBA(b, name, val) (JBSN(b, name), JBSS(b, val)) | |
| 39 | #define JBAI(b, name, val) (JBSN(b, name), JBSI(b, val)) | |
| 40 | ||
| 41 | static inline GBytes * | |
| 42 | JB2GBYTES(JsonBuilder *builder) | |
| 43 | { | |
| 44 | JsonNode *root = json_builder_get_root(builder); | |
| 45 | JsonGenerator *gen = json_generator_new(); | |
| 46 | json_generator_set_root(gen, root); | |
| 47 | ||
| 48 | gchar *json_str = json_generator_to_data(gen, NULL); | |
| 49 | GBytes *bytes = g_bytes_new_take(json_str, strlen(json_str)); | |
| 50 | ||
| 51 | json_node_free(root); | |
| 52 | g_object_unref(gen); | |
| 53 | ||
| 54 | return bytes; | |
| 55 | } | |
| 56 | ||
| 57 | #define JB_BEGIN_OBJ(name) \ | |
| 58 | JsonBuilder *name = json_builder_new(); \ | |
| 59 | JBO(name); | |
| 60 | ||
| 61 | #define JB_END_OBJ(lval, name) \ | |
| 62 | do { \ | |
| 63 | JEO(b); \ | |
| 64 | lval = JB2GBYTES(name); \ | |
| 65 | g_object_unref(b); \ | |
| 66 | } while (0) | |
| 67 | ||
| 68 | static inline SoupMessage * | |
| 69 | satori_message_new(const gchar *method, const gchar *url) | |
| 70 | { | |
| 71 | SoupMessage *msg = soup_message_new(method, url); | |
| 72 | SoupMessageHeaders *headers = soup_message_get_request_headers(msg); | |
| 73 | soup_message_headers_append(headers, "Satori-Platform", | |
| 74 | PURPLE_SATORI_PLATFORM); | |
| 75 | soup_message_headers_append(headers, "Satori-User-ID", | |
| 76 | PURPLE_SATORI_USER_ID); | |
| 77 | return msg; | |
| 78 | } | |
| 79 | ||
| 80 | static inline GBytes * | |
| 81 | satori_message_gen_ident(const gchar *token, gint sn) | |
| 82 | { | |
| 83 | GBytes *msg = NULL; | |
| 84 | ||
| 85 | JB_BEGIN_OBJ(b); | |
| 86 | JBAI(b, "op", SATORI_WEBSOCKET_OP_IDENTIFY); | |
| 87 | JBSN(b, "body"); | |
| 88 | ||
| 89 | JBO(b); | |
| 90 | if (token) | |
| 91 | JBA(b, "token", token); | |
| 92 | if (sn) | |
| 93 | JBAI(b, "sn", sn); | |
| 94 | JEO(b); | |
| 95 | ||
| 96 | JB_END_OBJ(msg, b); | |
| 97 | return msg; | |
| 98 | } | |
| 99 | ||
| 100 | #endif /* SATORI_MESSAGE_H */ |