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/gi18n-lib.h> | |
| 20 | ||
| 21 | #include "purplesatoriprotocolconversation.h" | |
| 22 | ||
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
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 "purplesatoriconnection.h" |
| 0 | 25 | #include "purplesatoriplugin.h" |
| 26 | #include "purplesatoriprotocol.h" | |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
27 | #include "satoriapi.h" |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
28 | #include "satoritypes.h" |
| 0 | 29 | |
| 30 | /****************************************************************************** | |
| 31 | * PurpleProtocolConversation Implementation | |
| 32 | *****************************************************************************/ | |
| 33 | static PurpleCreateConversationDetails * | |
| 34 | purple_satori_protocol_get_create_conversation_details(G_GNUC_UNUSED PurpleProtocolConversation *protocol, | |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
35 | G_GNUC_UNUSED PurpleAccount *account) |
| 0 | 36 | { |
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
37 | return purple_create_conversation_details_new(2); |
| 0 | 38 | } |
| 39 | ||
| 40 | static void | |
| 41 | purple_satori_protocol_create_conversation_async(PurpleProtocolConversation *protocol, | |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
42 | PurpleAccount *account, |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
43 | PurpleCreateConversationDetails *details, |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
44 | GCancellable *cancellable, |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
45 | GAsyncReadyCallback callback, |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
46 | gpointer data) |
| 0 | 47 | { |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
48 | GListModel *participants = NULL; |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
49 | GTask *task = NULL; |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
50 | guint n_participants = 0; |
| 0 | 51 | |
| 52 | task = g_task_new(protocol, cancellable, callback, data); | |
| 53 | g_task_set_source_tag(task, | |
| 54 | purple_satori_protocol_create_conversation_async); | |
| 55 | ||
| 56 | participants = purple_create_conversation_details_get_participants(details); | |
| 57 | n_participants = g_list_model_get_n_items(participants); | |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
58 | if (n_participants == 0) { |
| 0 | 59 | g_task_return_new_error_literal(task, PURPLE_SATORI_DOMAIN, 0, |
| 60 | _("no participants were provided")); | |
| 61 | g_clear_object(&task); | |
| 62 | return; | |
| 63 | } | |
| 64 | ||
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
65 | if (n_participants > 1) { |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
66 | g_task_return_new_error_literal(task, PURPLE_SATORI_DOMAIN, 0, |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
67 | _("only dm conversation supported")); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
68 | g_clear_object(&task); |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
69 | return; /* not implimented */ |
| 0 | 70 | } |
| 71 | ||
| 72 | for(guint i = 0; i < g_list_model_get_n_items(participants); i++) { | |
| 73 | PurpleContactInfo *info = NULL; | |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
74 | SatoriUser user = { 0 }; |
| 0 | 75 | |
| 76 | info = g_list_model_get_item(participants, i); | |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
77 | satori_user_from_contactinfo(info, &user); |
| 0 | 78 | |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
79 | satori_create_dm_channel( |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
80 | PURPLE_SATORI_CONNECTION( |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
81 | purple_account_get_connection(account)), |
|
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
82 | &user, task); |
| 0 | 83 | |
| 84 | g_clear_object(&info); | |
|
1
98bcf06036b8
Another Minor Milestone Reached, Conversation Creation & Recv works now
William Goodspeed <goodspeed@mailo.cat>
parents:
0
diff
changeset
|
85 | g_clear_object(&details); |
| 0 | 86 | return; |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | static PurpleConversation * | |
| 91 | purple_satori_protocol_create_conversation_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol, | |
| 92 | GAsyncResult *result, | |
| 93 | GError **error) | |
| 94 | { | |
| 95 | GTask *task = G_TASK(result); | |
| 96 | ||
| 97 | g_return_val_if_fail(g_task_get_source_tag(task) == | |
| 98 | purple_satori_protocol_create_conversation_async, | |
| 99 | NULL); | |
| 100 | ||
| 101 | return g_task_propagate_pointer(task, error); | |
| 102 | } | |
| 103 | ||
| 104 | static void | |
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
105 | purple_satori_protocol_send_message_async(PurpleProtocolConversation *protocol, |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
106 | PurpleConversation *conversation, |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
107 | PurpleMessage *message, |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
108 | GCancellable *cancellable, |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
109 | GAsyncReadyCallback callback, |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
110 | gpointer data) |
| 0 | 111 | { |
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
112 | PurpleSatoriConnection *con = \ |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
113 | PURPLE_SATORI_CONNECTION(purple_conversation_get_connection(conversation)); |
| 0 | 114 | |
| 115 | purple_conversation_write_message(conversation, message); | |
| 116 | ||
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
117 | GTask *task = g_task_new(protocol, cancellable, callback, data); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
118 | g_task_set_source_tag(task, |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
119 | purple_satori_protocol_send_message_async); |
| 0 | 120 | |
|
3
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
121 | gchar *content = \ |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
122 | g_markup_escape_text(purple_message_get_contents(message), -1); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
123 | satori_send_message(con, conversation, message, content, task); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
124 | g_free(content); |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
125 | |
|
33a7b189a2c6
Various improvement, Support configuration from UI
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
1
diff
changeset
|
126 | return; |
| 0 | 127 | } |
| 128 | ||
| 129 | static gboolean | |
| 130 | purple_satori_protocol_send_message_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol, | |
| 131 | GAsyncResult *result, | |
| 132 | GError **error) | |
| 133 | { | |
| 134 | g_return_val_if_fail(G_IS_TASK(result), FALSE); | |
| 135 | ||
| 136 | return g_task_propagate_boolean(G_TASK(result), error); | |
| 137 | } | |
| 138 | ||
| 139 | void | |
| 140 | purple_satori_protocol_conversation_init(PurpleProtocolConversationInterface *iface) { | |
| 141 | iface->get_create_conversation_details = | |
| 142 | purple_satori_protocol_get_create_conversation_details; | |
| 143 | iface->create_conversation_async = | |
| 144 | purple_satori_protocol_create_conversation_async; | |
| 145 | iface->create_conversation_finish = | |
| 146 | purple_satori_protocol_create_conversation_finish; | |
| 147 | ||
| 148 | iface->send_message_async = purple_satori_protocol_send_message_async; | |
| 149 | iface->send_message_finish = purple_satori_protocol_send_message_finish; | |
| 150 | } |