Sun, 10 Aug 2025 23:53:22 +0800
Various improvement, Support configuration from UI
| 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 | #include <glib.h> | |
| 20 | #include <glib-object.h> | |
| 21 | #include <glib/gi18n-lib.h> | |
| 22 | #include <json-glib/json-glib.h> | |
| 23 | ||
| 24 | #include <gio/gio.h> | |
| 25 | #include <libsoup/soup.h> | |
| 26 | #include <libsoup/soup-session.h> | |
| 27 | #include <libsoup/soup-message.h> | |
| 28 | #include <libsoup/soup-types.h> | |
| 29 | #include <libsoup/soup-websocket-connection.h> | |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
30 | #include <time.h> |
| 0 | 31 | |
| 32 | #include "purplesatoriconnection.h" | |
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
33 | #include "pango/pango-attributes.h" |
| 0 | 34 | #include "purplesatoriprotocolcontacts.h" |
| 35 | #include "satorimessage.h" | |
| 36 | #include "satoritypes.h" | |
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
37 | #include "satoriformat.h" |
| 0 | 38 | #include "satoriapi.h" |
| 39 | ||
| 40 | struct _PurpleSatoriConnection { | |
| 41 | PurpleConnection parent; | |
| 42 | SoupSession *session; | |
| 43 | ||
| 44 | SoupWebsocketConnection *wscon; | |
| 45 | gboolean wsidented; | |
| 46 | }; | |
| 47 | ||
| 48 | G_DEFINE_DYNAMIC_TYPE_EXTENDED(PurpleSatoriConnection, purple_satori_connection, | |
| 49 | PURPLE_TYPE_CONNECTION, G_TYPE_FLAG_FINAL, {}); | |
| 50 | ||
| 51 | /****************************************************************************** | |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
52 | * PurpleConversation Helpers |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
53 | *****************************************************************************/ |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
54 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
55 | static void |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
56 | purple_satori_handle_pending_message(PurpleSatoriConnection *con, |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
57 | SatoriUser *user, |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
58 | SatoriChannel *chan, |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
59 | JsonObject *msg_obj) |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
60 | { |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
61 | PurpleConversation *conversation = \ |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
62 | purple_satori_add_conversation_from_chan(con, chan); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
63 | PurpleConversationMember *mbr = \ |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
64 | purple_satori_add_conversation_member_from_user( |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
65 | con, conversation, user); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
66 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
67 | const gchar *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
|
68 | msg_obj, "id", NULL); |
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
69 | const gchar *html = json_object_get_string_member_with_default( |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
70 | msg_obj, "content", "Invalid Message"); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
71 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
72 | time_t created_at = json_object_get_int_member_with_default( |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
73 | msg_obj, "created_at", 0) / 1000; /* timestamp in mS */ |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
74 | |
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
75 | PangoAttrList *attrs = pango_attr_list_new(); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
76 | GString *text = NULL; |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
77 | satori_format_html_to_purple(conversation, html, &text, attrs); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
78 | |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
79 | PurpleMessage *message = purple_message_new(mbr, text->str); |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
80 | if (id) purple_message_set_id(message, id); |
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
81 | purple_message_set_attributes(message, attrs); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
82 | |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
83 | pango_attr_list_unref(attrs); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
84 | g_free(text); |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
85 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
86 | if (created_at) { |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
87 | GDateTime *ts = g_date_time_new_from_unix_local(created_at); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
88 | purple_message_set_timestamp(message, ts); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
89 | g_date_time_unref(ts); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
90 | } else purple_message_set_timestamp_now(message); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
91 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
92 | purple_conversation_write_message(conversation, message); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
93 | g_clear_object(&message); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
94 | } |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
95 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
96 | /****************************************************************************** |
| 0 | 97 | * PurpleConnection WS Callbacks |
| 98 | *****************************************************************************/ | |
| 99 | ||
| 100 | static void | |
| 101 | satori_ws_on_closed(SoupWebsocketConnection *wscon, PurpleSatoriConnection *data) | |
| 102 | { | |
| 103 | PurpleSatoriConnection *con = PURPLE_SATORI_CONNECTION(data); | |
| 104 | PurpleAccount *acc = purple_connection_get_account(PURPLE_CONNECTION(con)); | |
| 105 | purple_account_disconnect(acc); | |
| 106 | ||
| 107 | con->wscon = NULL; | |
| 108 | } | |
| 109 | ||
| 110 | static void | |
| 111 | satori_ws_on_message(SoupWebsocketConnection *wscon, gint type, | |
| 112 | GBytes *message, PurpleSatoriConnection *con) | |
| 113 | { | |
| 114 | PurpleAccount *acc = purple_connection_get_account( | |
| 115 | PURPLE_CONNECTION(con)); | |
| 116 | ||
| 117 | if (type != SOUP_WEBSOCKET_DATA_TEXT) { | |
| 118 | purple_debug_warning("satori", "unexpected data recv from ws"); | |
| 119 | return; | |
| 120 | } | |
| 121 | ||
| 122 | gsize sz; | |
| 123 | const gchar *ptr = g_bytes_get_data(message, &sz); | |
| 124 | ||
| 125 | JsonParser *parser = json_parser_new(); | |
| 126 | if (!json_parser_load_from_data(parser, ptr, sz, NULL)) { | |
| 127 | purple_debug_warning("satori", "bad json received from ws"); | |
| 128 | g_object_unref(parser); | |
| 129 | return; | |
| 130 | } | |
| 131 | ||
| 132 | JsonObject *root = json_node_get_object(json_parser_get_root(parser)); | |
| 133 | JsonObject *body = json_object_get_object_member(root, "body"); | |
| 134 | SatoriWebsocketOpcode op = json_object_get_int_member(root, "op"); | |
| 135 | ||
| 136 | switch (op) { | |
| 137 | ||
| 138 | case SATORI_WEBSOCKET_OP_READY: | |
| 139 | { | |
| 140 | purple_account_connected(acc); | |
| 141 | ||
| 142 | JsonArray *logins = json_object_get_array_member(body, "logins"); | |
| 143 | JsonObject *user_obj = json_object_get_object_member( | |
| 144 | json_array_get_object_element(logins, 0), "user"); | |
| 145 | SatoriUser user = { 0 }; | |
| 146 | ||
| 147 | if (!user_obj) break; | |
| 148 | satori_user_from_json(user_obj, &user); | |
| 149 | ||
| 150 | PurpleContactInfo *ci = purple_account_get_contact_info(acc); | |
| 151 | purple_contact_info_set_id(ci, user.id); | |
| 152 | purple_contact_info_set_display_name(ci, user.nick | |
| 153 | ? user.nick : user.name); | |
| 154 | ||
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
155 | PurplePresence *presence = purple_contact_info_get_presence(ci); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
156 | purple_presence_set_primitive(presence, |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
157 | PURPLE_PRESENCE_PRIMITIVE_AVAILABLE); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
158 | |
| 0 | 159 | satori_refresh_buddy_contacts(con, NULL); |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
160 | satori_refresh_conversations(con, NULL); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
161 | break; |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
162 | } |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
163 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
164 | case SATORI_WEBSOCKET_OP_EVENT: |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
165 | { |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
166 | const gchar *type = json_object_get_string_member(body, "type"); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
167 | if (purple_strequal(type, "message-created")) { |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
168 | JsonObject *obj = json_object_get_object_member( |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
169 | body, "message"); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
170 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
171 | JsonObject *usr_obj = json_object_get_object_member( |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
172 | json_object_get_object_member(body, "member"), |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
173 | "user"); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
174 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
175 | if (!usr_obj) |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
176 | usr_obj = \ |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
177 | json_object_get_object_member(body, "user"); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
178 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
179 | if (!usr_obj) break; |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
180 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
181 | JsonObject *chan_obj = \ |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
182 | json_object_get_object_member(body, "channel"); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
183 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
184 | if (!chan_obj) break; |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
185 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
186 | SatoriChannel chan; |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
187 | SatoriUser usr; |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
188 | satori_channel_from_json(chan_obj, &chan); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
189 | satori_user_from_json(usr_obj, &usr); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
190 | |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
191 | purple_satori_handle_pending_message(con, &usr, &chan, obj); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
192 | } |
| 0 | 193 | break; |
| 194 | } | |
| 195 | ||
| 196 | default: /* ignored */ | |
| 197 | break; | |
| 198 | ||
| 199 | } | |
| 200 | ||
| 201 | g_object_unref(parser); | |
| 202 | } | |
| 203 | ||
| 204 | static void | |
| 205 | satori_ws_on_connection(SoupSession *session, GAsyncResult *res, gpointer data) | |
| 206 | { | |
| 207 | PurpleSatoriConnection *con = PURPLE_SATORI_CONNECTION(data); | |
| 208 | PurpleAccount *acc = purple_connection_get_account(PURPLE_CONNECTION(con)); | |
| 209 | GError *err = NULL; | |
| 210 | ||
| 211 | con->wscon = soup_session_websocket_connect_finish(session, res, &err); | |
| 212 | if (err) { | |
| 213 | purple_account_disconnect_with_error(acc, err); | |
| 214 | return; | |
| 215 | } | |
| 216 | ||
| 217 | g_signal_connect(con->wscon, "message", | |
| 218 | G_CALLBACK(satori_ws_on_message), con); | |
| 219 | g_signal_connect(con->wscon, "closed", | |
| 220 | G_CALLBACK(satori_ws_on_closed), con); | |
| 221 | ||
| 222 | GBytes *frame = satori_message_gen_ident(NULL, 0); | |
| 223 | soup_websocket_connection_send_text(con->wscon, | |
| 224 | g_bytes_get_data(frame, NULL)); | |
| 225 | g_bytes_unref(frame); | |
| 226 | } | |
| 227 | ||
| 228 | /****************************************************************************** | |
| 229 | * PurpleConnection Implementation | |
| 230 | *****************************************************************************/ | |
| 231 | static gboolean | |
| 232 | purple_satori_connection_connect(PurpleConnection *connection, | |
| 233 | G_GNUC_UNUSED GError **error) | |
| 234 | { | |
| 235 | PurpleSatoriConnection *con = PURPLE_SATORI_CONNECTION(connection); | |
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
236 | PurpleAccount *acc = purple_connection_get_account(connection); |
| 0 | 237 | |
| 238 | if (con->wscon) | |
| 239 | g_object_unref(con->wscon); | |
| 240 | ||
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
241 | gchar *wsurl = \ |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
242 | g_strdup_printf("%s://%s%s/events", |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
243 | purple_account_get_bool(acc, "https", FALSE) ? "wss" : "ws", |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
244 | purple_account_get_string(acc, "host", "127.0.0.1:5600"), |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
245 | purple_account_get_string(acc, "path", "/v1")); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
246 | |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
247 | SoupMessage *svmsg = soup_message_new("GET", wsurl); |
| 0 | 248 | soup_session_websocket_connect_async( |
| 249 | con->session, svmsg, | |
| 250 | NULL, NULL, 0, NULL, | |
| 251 | (GAsyncReadyCallback) satori_ws_on_connection, | |
| 252 | con); | |
| 253 | ||
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
254 | g_free(wsurl); |
| 0 | 255 | return TRUE; |
| 256 | } | |
| 257 | ||
| 258 | static gboolean | |
| 259 | purple_satori_connection_disconnect(PurpleConnection *connection, | |
| 260 | G_GNUC_UNUSED GError **error) | |
| 261 | { | |
| 262 | PurpleSatoriConnection *con = PURPLE_SATORI_CONNECTION(connection); | |
| 263 | if (!con->wscon) return TRUE; | |
| 264 | ||
| 265 | g_signal_handlers_disconnect_by_data(con->wscon, con); | |
| 266 | soup_websocket_connection_close(con->wscon, | |
| 267 | SOUP_WEBSOCKET_CLOSE_NO_STATUS, NULL); | |
| 268 | con->wscon = NULL; | |
| 269 | return TRUE; | |
| 270 | } | |
| 271 | ||
| 272 | /****************************************************************************** | |
| 273 | * GObject Implementation | |
| 274 | *****************************************************************************/ | |
| 275 | static void | |
| 276 | purple_satori_connection_init(PurpleSatoriConnection *connection) { | |
| 277 | connection->session = soup_session_new(); | |
| 278 | connection->wscon = NULL; | |
| 279 | } | |
| 280 | ||
| 281 | static void | |
| 282 | purple_satori_connection_class_finalize(G_GNUC_UNUSED PurpleSatoriConnectionClass *klass) { | |
| 283 | } | |
| 284 | ||
| 285 | static void | |
| 286 | purple_satori_connection_class_init(PurpleSatoriConnectionClass *klass) { | |
| 287 | PurpleConnectionClass *connection_class = PURPLE_CONNECTION_CLASS(klass); | |
| 288 | ||
| 289 | connection_class->connect = purple_satori_connection_connect; | |
| 290 | connection_class->disconnect = purple_satori_connection_disconnect; | |
| 291 | } | |
| 292 | ||
| 293 | /****************************************************************************** | |
| 294 | * Internal API | |
| 295 | *****************************************************************************/ | |
| 296 | void | |
| 297 | purple_satori_connection_register(GPluginNativePlugin *plugin) { | |
| 298 | purple_satori_connection_register_type(G_TYPE_MODULE(plugin)); | |
| 299 | } | |
| 300 | ||
| 301 | /****************************************************************************** | |
| 302 | * Public API Implementation | |
| 303 | *****************************************************************************/ | |
| 304 | ||
| 305 | void | |
| 306 | purple_satori_connection_send_and_read_async(PurpleSatoriConnection *con, | |
| 307 | SoupMessage *msg, | |
| 308 | int io_priority, | |
| 309 | GCancellable *cancellable, | |
| 310 | GAsyncReadyCallback callback, | |
| 311 | gpointer user_data) | |
| 312 | { | |
| 313 | soup_session_send_and_read_async(con->session, msg, io_priority, | |
| 314 | cancellable, callback, user_data); | |
| 315 | } |