Fri, 08 Aug 2025 09:46:55 +0800
Initial Commit
| 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/gi18n-lib.h> | |
| 20 | ||
| 21 | #include "purplesatoriprotocolconversation.h" | |
| 22 | ||
| 23 | #include "purplesatoriplugin.h" | |
| 24 | #include "purplesatoriprotocol.h" | |
| 25 | ||
| 26 | typedef struct { | |
| 27 | PurpleConversation *conversation; | |
| 28 | PurpleMessage *message; | |
| 29 | } PurpleSatoriProtocolIMInfo; | |
| 30 | ||
| 31 | /****************************************************************************** | |
| 32 | * Helpers | |
| 33 | *****************************************************************************/ | |
| 34 | static void | |
| 35 | purple_satori_protocol_im_info_free(PurpleSatoriProtocolIMInfo *info) { | |
| 36 | g_clear_object(&info->conversation); | |
| 37 | g_clear_object(&info->message); | |
| 38 | g_free(info); | |
| 39 | } | |
| 40 | ||
| 41 | static gint | |
| 42 | purple_satori_protocol_contact_sort(gconstpointer a, gconstpointer b, | |
| 43 | G_GNUC_UNUSED gpointer data) | |
| 44 | { | |
| 45 | return purple_contact_info_compare(PURPLE_CONTACT_INFO((gpointer)a), | |
| 46 | PURPLE_CONTACT_INFO((gpointer)b)); | |
| 47 | } | |
| 48 | ||
| 49 | static char * | |
| 50 | purple_satori_protocol_generate_conversation_id(PurpleAccount *account, | |
| 51 | PurpleCreateConversationDetails *details) | |
| 52 | { | |
| 53 | GChecksum *checksum = NULL; | |
| 54 | GListModel *participants = NULL; | |
| 55 | GListStore *sorted = NULL; | |
| 56 | char *ret = NULL; | |
| 57 | const char *id = NULL; | |
| 58 | ||
| 59 | /* Sort the participants. */ | |
| 60 | sorted = g_list_store_new(PURPLE_TYPE_CONTACT); | |
| 61 | participants = purple_create_conversation_details_get_participants(details); | |
| 62 | for(guint i = 0; i < g_list_model_get_n_items(participants); i++) { | |
| 63 | PurpleContactInfo *info = NULL; | |
| 64 | ||
| 65 | info = g_list_model_get_item(participants, i); | |
| 66 | g_list_store_insert_sorted(sorted, info, | |
| 67 | purple_satori_protocol_contact_sort, | |
| 68 | NULL); | |
| 69 | g_clear_object(&info); | |
| 70 | } | |
| 71 | ||
| 72 | /* Build a checksum of the account and the sorted participants. */ | |
| 73 | checksum = g_checksum_new(G_CHECKSUM_SHA256); | |
| 74 | ||
| 75 | id = purple_account_get_id(account); | |
| 76 | g_checksum_update(checksum, (guchar *)id, -1); | |
| 77 | ||
| 78 | for(guint i = 0; i < g_list_model_get_n_items(G_LIST_MODEL(sorted)); i++) { | |
| 79 | PurpleContactInfo *info = NULL; | |
| 80 | ||
| 81 | info = g_list_model_get_item(G_LIST_MODEL(sorted), i); | |
| 82 | id = purple_contact_info_get_id(info); | |
| 83 | g_checksum_update(checksum, (guchar *)id, -1); | |
| 84 | g_clear_object(&info); | |
| 85 | } | |
| 86 | ||
| 87 | ret = g_strdup(g_checksum_get_string(checksum)); | |
| 88 | ||
| 89 | g_clear_pointer(&checksum, g_checksum_free); | |
| 90 | g_clear_object(&sorted); | |
| 91 | ||
| 92 | return ret; | |
| 93 | } | |
| 94 | ||
| 95 | /****************************************************************************** | |
| 96 | * Callbacks | |
| 97 | *****************************************************************************/ | |
| 98 | static gboolean | |
| 99 | purple_satori_protocol_echo_im_cb(gpointer data) { | |
| 100 | PurpleSatoriProtocolIMInfo *info = data; | |
| 101 | ||
| 102 | purple_conversation_write_message(info->conversation, info->message); | |
| 103 | ||
| 104 | return G_SOURCE_REMOVE; | |
| 105 | } | |
| 106 | ||
| 107 | /****************************************************************************** | |
| 108 | * PurpleProtocolConversation Implementation | |
| 109 | *****************************************************************************/ | |
| 110 | static PurpleCreateConversationDetails * | |
| 111 | purple_satori_protocol_get_create_conversation_details(G_GNUC_UNUSED PurpleProtocolConversation *protocol, | |
| 112 | G_GNUC_UNUSED PurpleAccount *account) | |
| 113 | { | |
| 114 | return purple_create_conversation_details_new(9); | |
| 115 | } | |
| 116 | ||
| 117 | static void | |
| 118 | purple_satori_protocol_create_conversation_async(PurpleProtocolConversation *protocol, | |
| 119 | PurpleAccount *account, | |
| 120 | PurpleCreateConversationDetails *details, | |
| 121 | GCancellable *cancellable, | |
| 122 | GAsyncReadyCallback callback, | |
| 123 | gpointer data) | |
| 124 | { | |
| 125 | PurpleConversation *conversation = NULL; | |
| 126 | PurpleConversationManager *manager = NULL; | |
| 127 | PurpleConversationType type = PURPLE_CONVERSATION_TYPE_UNSET; | |
| 128 | GListModel *participants = NULL; | |
| 129 | GTask *task = NULL; | |
| 130 | char *id = NULL; | |
| 131 | guint n_participants = 0; | |
| 132 | ||
| 133 | task = g_task_new(protocol, cancellable, callback, data); | |
| 134 | g_task_set_source_tag(task, | |
| 135 | purple_satori_protocol_create_conversation_async); | |
| 136 | ||
| 137 | participants = purple_create_conversation_details_get_participants(details); | |
| 138 | n_participants = g_list_model_get_n_items(participants); | |
| 139 | if(n_participants == 0) { | |
| 140 | g_task_return_new_error_literal(task, PURPLE_SATORI_DOMAIN, 0, | |
| 141 | _("no participants were provided")); | |
| 142 | g_clear_object(&task); | |
| 143 | ||
| 144 | return; | |
| 145 | } | |
| 146 | ||
| 147 | if(n_participants == 1) { | |
| 148 | type = PURPLE_CONVERSATION_TYPE_DM; | |
| 149 | } else { | |
| 150 | type = PURPLE_CONVERSATION_TYPE_GROUP_DM; | |
| 151 | } | |
| 152 | id = purple_satori_protocol_generate_conversation_id(account, details); | |
| 153 | ||
| 154 | conversation = g_object_new( | |
| 155 | PURPLE_TYPE_CONVERSATION, | |
| 156 | "account", account, | |
| 157 | "id", id, | |
| 158 | "type", type, | |
| 159 | "online", TRUE, | |
| 160 | NULL); | |
| 161 | g_clear_pointer(&id, g_free); | |
| 162 | ||
| 163 | for(guint i = 0; i < g_list_model_get_n_items(participants); i++) { | |
| 164 | PurpleContactInfo *info = NULL; | |
| 165 | PurpleConversationMember *member = NULL; | |
| 166 | PurpleConversationMembers *members = NULL; | |
| 167 | PurpleTags *tags = NULL; | |
| 168 | const char *badge_id = NULL; | |
| 169 | ||
| 170 | info = g_list_model_get_item(participants, i); | |
| 171 | members = purple_conversation_get_members(conversation); | |
| 172 | member = purple_conversation_members_add_member(members, info, FALSE, | |
| 173 | NULL); | |
| 174 | ||
| 175 | tags = purple_contact_info_get_tags(info); | |
| 176 | badge_id = purple_tags_get(tags, "satori-badge"); | |
| 177 | if(!purple_strempty(badge_id)) { | |
| 178 | PurpleBadge *badge = NULL; | |
| 179 | PurpleBadgeManager *manager = NULL; | |
| 180 | PurpleBadges *badges = NULL; | |
| 181 | ||
| 182 | badges = purple_conversation_member_get_badges(member); | |
| 183 | ||
| 184 | manager = purple_badge_manager_get_default(); | |
| 185 | badge = purple_badge_manager_find(manager, badge_id); | |
| 186 | if(PURPLE_IS_BADGE(badge)) { | |
| 187 | purple_badges_add_badge(badges, badge); | |
| 188 | } else { | |
| 189 | char *icon_name = NULL; | |
| 190 | char *id = NULL; | |
| 191 | ||
| 192 | id = g_strdup_printf("satori-badge-%s", badge_id); | |
| 193 | icon_name = g_strdup_printf("satori-badge-%s", badge_id); | |
| 194 | badge = purple_badge_new(id, 0, icon_name, " "); | |
| 195 | purple_badge_set_description(badge, badge_id); | |
| 196 | g_free(id); | |
| 197 | g_free(icon_name); | |
| 198 | ||
| 199 | purple_badge_manager_add(manager, badge); | |
| 200 | purple_badges_add_badge(badges, badge); | |
| 201 | g_clear_object(&badge); | |
| 202 | } | |
| 203 | } | |
| 204 | ||
| 205 | g_clear_object(&info); | |
| 206 | } | |
| 207 | g_clear_object(&details); | |
| 208 | ||
| 209 | manager = purple_conversation_manager_get_default(); | |
| 210 | if(!purple_conversation_manager_add(manager, conversation)) { | |
| 211 | g_task_return_new_error(task, PURPLE_SATORI_DOMAIN, 0, | |
| 212 | _("This conversation already exists.")); | |
| 213 | g_clear_object(&task); | |
| 214 | ||
| 215 | return; | |
| 216 | } | |
| 217 | ||
| 218 | g_task_return_pointer(task, conversation, g_object_unref); | |
| 219 | ||
| 220 | g_clear_object(&task); | |
| 221 | } | |
| 222 | ||
| 223 | static PurpleConversation * | |
| 224 | purple_satori_protocol_create_conversation_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol, | |
| 225 | GAsyncResult *result, | |
| 226 | GError **error) | |
| 227 | { | |
| 228 | GTask *task = G_TASK(result); | |
| 229 | ||
| 230 | g_return_val_if_fail(g_task_get_source_tag(task) == | |
| 231 | purple_satori_protocol_create_conversation_async, | |
| 232 | NULL); | |
| 233 | ||
| 234 | return g_task_propagate_pointer(task, error); | |
| 235 | } | |
| 236 | ||
| 237 | static void | |
| 238 | purple_satori_protocol_conversation_leave_conversation_async(PurpleProtocolConversation *protocol, | |
| 239 | G_GNUC_UNUSED PurpleConversation *conversation, | |
| 240 | GCancellable *cancellable, | |
| 241 | GAsyncReadyCallback callback, | |
| 242 | gpointer data) | |
| 243 | { | |
| 244 | GTask *task = NULL; | |
| 245 | ||
| 246 | task = g_task_new(protocol, cancellable, callback, data); | |
| 247 | g_task_set_source_tag(task, | |
| 248 | purple_satori_protocol_conversation_leave_conversation_async); | |
| 249 | ||
| 250 | g_task_return_boolean(task, TRUE); | |
| 251 | g_clear_object(&task); | |
| 252 | } | |
| 253 | ||
| 254 | static gboolean | |
| 255 | purple_satori_protocol_conversation_leave_conversation_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol, | |
| 256 | GAsyncResult *result, | |
| 257 | GError **error) | |
| 258 | { | |
| 259 | gpointer tag = purple_satori_protocol_conversation_leave_conversation_async; | |
| 260 | ||
| 261 | g_return_val_if_fail(g_async_result_is_tagged(result, tag), FALSE); | |
| 262 | ||
| 263 | return g_task_propagate_boolean(G_TASK(result), error); | |
| 264 | } | |
| 265 | ||
| 266 | static void | |
| 267 | purple_satori_protocol_send_message_async(G_GNUC_UNUSED PurpleProtocolConversation *protocol, | |
| 268 | PurpleConversation *conversation, | |
| 269 | PurpleMessage *message, | |
| 270 | GCancellable *cancellable, | |
| 271 | GAsyncReadyCallback callback, | |
| 272 | gpointer data) | |
| 273 | { | |
| 274 | GTask *task = NULL; | |
| 275 | ||
| 276 | if(purple_conversation_is_dm(conversation)) { | |
| 277 | PurpleAccount *account = NULL; | |
| 278 | PurpleContact *contact = NULL; | |
| 279 | PurpleContactInfo *contact_info = NULL; | |
| 280 | PurpleContactManager *manager = NULL; | |
| 281 | PurpleConversationMember *member = NULL; | |
| 282 | PurpleConversationMembers *members = NULL; | |
| 283 | ||
| 284 | account = purple_conversation_get_account(conversation); | |
| 285 | members = purple_conversation_get_members(conversation); | |
| 286 | ||
| 287 | manager = purple_contact_manager_get_default(); | |
| 288 | ||
| 289 | /* Check if this dm is with echo. */ | |
| 290 | contact = purple_contact_manager_find_with_id(manager, account, | |
| 291 | "echo"); | |
| 292 | contact_info = PURPLE_CONTACT_INFO(contact); | |
| 293 | member = purple_conversation_members_find_member(members, contact_info); | |
| 294 | if(PURPLE_IS_CONVERSATION_MEMBER(member)) { | |
| 295 | PurpleSatoriProtocolIMInfo *info = NULL; | |
| 296 | const char *contents = purple_message_get_contents(message); | |
| 297 | ||
| 298 | info = g_new(PurpleSatoriProtocolIMInfo, 1); | |
| 299 | info->conversation = g_object_ref(conversation); | |
| 300 | info->message = purple_message_new(member, contents); | |
| 301 | purple_message_set_edited(info->message, | |
| 302 | purple_message_get_edited(message)); | |
| 303 | ||
| 304 | g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, | |
| 305 | purple_satori_protocol_echo_im_cb, info, | |
| 306 | (GDestroyNotify)purple_satori_protocol_im_info_free); | |
| 307 | } | |
| 308 | ||
| 309 | /* Check if this dm is with aegina. */ | |
| 310 | contact = purple_contact_manager_find_with_id(manager, account, | |
| 311 | "aegina"); | |
| 312 | contact_info = PURPLE_CONTACT_INFO(contact); | |
| 313 | member = purple_conversation_members_find_member(members, contact_info); | |
| 314 | if(PURPLE_IS_CONVERSATION_MEMBER(member)) { | |
| 315 | PurpleSatoriProtocolIMInfo *info = g_new(PurpleSatoriProtocolIMInfo, 1); | |
| 316 | PurpleConversationMember *author = purple_message_get_author(message); | |
| 317 | PurpleContactInfo *author_info = NULL; | |
| 318 | const char *contents = NULL; | |
| 319 | const char *author_id = NULL; | |
| 320 | ||
| 321 | author_info = purple_conversation_member_get_contact_info(author); | |
| 322 | author_id = purple_contact_info_get_id(author_info); | |
| 323 | if(purple_strequal(author_id, "hades")) { | |
| 324 | contents = "🫥️"; | |
| 325 | } else { | |
| 326 | /* TRANSLATORS: This is a reference to the Cap of Invisibility owned by | |
| 327 | * various Greek gods, such as Hades, as mentioned. */ | |
| 328 | contents = _("Don't tell Hades I have his Cap"); | |
| 329 | } | |
| 330 | ||
| 331 | info->conversation = g_object_ref(conversation); | |
| 332 | info->message = purple_message_new(member, contents); | |
| 333 | ||
| 334 | g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, purple_satori_protocol_echo_im_cb, | |
| 335 | info, (GDestroyNotify)purple_satori_protocol_im_info_free); | |
| 336 | } | |
| 337 | } | |
| 338 | ||
| 339 | purple_conversation_write_message(conversation, message); | |
| 340 | ||
| 341 | task = g_task_new(protocol, cancellable, callback, data); | |
| 342 | g_task_return_boolean(task, TRUE); | |
| 343 | ||
| 344 | g_clear_object(&task); | |
| 345 | } | |
| 346 | ||
| 347 | static gboolean | |
| 348 | purple_satori_protocol_send_message_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol, | |
| 349 | GAsyncResult *result, | |
| 350 | GError **error) | |
| 351 | { | |
| 352 | g_return_val_if_fail(G_IS_TASK(result), FALSE); | |
| 353 | ||
| 354 | return g_task_propagate_boolean(G_TASK(result), error); | |
| 355 | } | |
| 356 | ||
| 357 | void | |
| 358 | purple_satori_protocol_conversation_init(PurpleProtocolConversationInterface *iface) { | |
| 359 | iface->get_create_conversation_details = | |
| 360 | purple_satori_protocol_get_create_conversation_details; | |
| 361 | iface->create_conversation_async = | |
| 362 | purple_satori_protocol_create_conversation_async; | |
| 363 | iface->create_conversation_finish = | |
| 364 | purple_satori_protocol_create_conversation_finish; | |
| 365 | ||
| 366 | iface->leave_conversation_async = | |
| 367 | purple_satori_protocol_conversation_leave_conversation_async; | |
| 368 | iface->leave_conversation_finish = | |
| 369 | purple_satori_protocol_conversation_leave_conversation_finish; | |
| 370 | ||
| 371 | iface->send_message_async = purple_satori_protocol_send_message_async; | |
| 372 | iface->send_message_finish = purple_satori_protocol_send_message_finish; | |
| 373 | } |