satoritypes.h

Sun, 10 Aug 2025 23:03:27 +0800

author
Gong Zhile <gongzl@stu.hebust.edu.cn>
date
Sun, 10 Aug 2025 23:03:27 +0800
changeset 2
efafd19ab2fe
parent 1
98bcf06036b8
permissions
-rw-r--r--

satoriformat.{c,h}: Add message parsing support

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_TYPES_H
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
20 #define SATORI_TYPES_H
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
21
1
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
22 #include "purplesatoriprotocolconversation.h"
0
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
23 #include <glib.h>
1
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
24 #include <json-glib/json-glib.h>
0
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
25
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
26 typedef enum {
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
27 SATORI_WEBSOCKET_OP_EVENT = 0,
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
28 SATORI_WEBSOCKET_OP_PING,
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
29 SATORI_WEBSOCKET_OP_PONG,
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
30 SATORI_WEBSOCKET_OP_IDENTIFY,
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
31 SATORI_WEBSOCKET_OP_READY,
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
32 SATORI_WEBSOCKET_OP_META,
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
33 } SatoriWebsocketOpcode;
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
34
1
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
35 typedef struct satori_user {
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
36 const gchar *id;
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
37 const gchar *name;
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
38 const gchar *nick;
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
39 const gchar *avatar;
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
40 gboolean is_bot;
0
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
41 } SatoriUser;
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
42
1
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
43 typedef enum {
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
44 SATORI_CHANNEL_TEXT = 0,
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
45 SATORI_CHANNEL_DIRECT,
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
46 SATORI_CHANNEL_CATEGORY,
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
47 SATORI_CHANNEL_VOICE,
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
48 } SatoriChannelType;
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
49
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
50 static inline void
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
51 satori_user_from_json(JsonObject *user_obj, SatoriUser *out_user) {
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
52 out_user->id = json_object_get_string_member(user_obj, "id");
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
53 out_user->name = json_object_get_string_member_with_default(
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
54 user_obj, "name", NULL);
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
55 out_user->nick = json_object_get_string_member_with_default(
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
56 user_obj, "nick", NULL);
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
57 out_user->avatar = json_object_get_string_member_with_default(
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
58 user_obj, "avatar", NULL);
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
59 out_user->is_bot = json_object_get_boolean_member_with_default(
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
60 user_obj, "is_bot", FALSE);
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
61 }
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
62
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
63 static inline void
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
64 satori_user_from_contactinfo(PurpleContactInfo *info, SatoriUser *out_user) {
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
65 out_user->nick = out_user->name = \
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
66 purple_contact_info_get_display_name(info);
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
67 out_user->id = \
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
68 purple_contact_info_get_id(info);
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
69 }
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
70
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
71 typedef struct satori_channel {
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
72 const gchar *id;
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
73 SatoriChannelType type;
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
74 const gchar *name;
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
75 const gchar *parent_id;
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
76 } SatoriChannel;
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
77
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
78 static inline void
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
79 satori_channel_from_json(JsonObject *obj, SatoriChannel *out_chan) {
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
80 out_chan->id = json_object_get_string_member(obj, "id");
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
81 out_chan->type = (SatoriChannelType) json_object_get_int_member(
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
82 obj, "type");
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
83 out_chan->name = json_object_get_string_member_with_default(
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
84 obj, "name", NULL);
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
85 out_chan->parent_id = json_object_get_string_member_with_default(
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
86 obj, "parent_id", NULL);
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
87 }
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
88
98bcf06036b8 Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents: 0
diff changeset
89
0
cc7c1f9d20f7 Initial Commit
William Goodspeed <goodspeed@mailo.cat>
parents:
diff changeset
90 #endif /* SATORI_TYPES_H */

mercurial