satorimessage.h

Fri, 08 Aug 2025 09:46:55 +0800

author
William Goodspeed <goodspeed@mailo.cat>
date
Fri, 08 Aug 2025 09:46:55 +0800
changeset 0
cc7c1f9d20f7
child 1
98bcf06036b8
permissions
-rw-r--r--

Initial Commit

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

mercurial