| 18 |
18 |
| 19 #include <glib/gi18n-lib.h> |
19 #include <glib/gi18n-lib.h> |
| 20 |
20 |
| 21 #include "purplesatoriprotocolconversation.h" |
21 #include "purplesatoriprotocolconversation.h" |
| 22 |
22 |
| |
23 #include "glib.h" |
| 23 #include "purplesatoriconnection.h" |
24 #include "purplesatoriconnection.h" |
| 24 #include "purplesatoriplugin.h" |
25 #include "purplesatoriplugin.h" |
| 25 #include "purplesatoriprotocol.h" |
26 #include "purplesatoriprotocol.h" |
| 26 #include "satoriapi.h" |
27 #include "satoriapi.h" |
| 27 #include "satoritypes.h" |
28 #include "satoritypes.h" |
| 28 |
|
| 29 typedef struct { |
|
| 30 PurpleConversation *conversation; |
|
| 31 PurpleMessage *message; |
|
| 32 } PurpleSatoriProtocolIMInfo; |
|
| 33 |
|
| 34 /****************************************************************************** |
|
| 35 * Helpers |
|
| 36 *****************************************************************************/ |
|
| 37 static void |
|
| 38 purple_satori_protocol_im_info_free(PurpleSatoriProtocolIMInfo *info) { |
|
| 39 g_clear_object(&info->conversation); |
|
| 40 g_clear_object(&info->message); |
|
| 41 g_free(info); |
|
| 42 } |
|
| 43 |
|
| 44 /****************************************************************************** |
|
| 45 * Callbacks |
|
| 46 *****************************************************************************/ |
|
| 47 static gboolean |
|
| 48 purple_satori_protocol_echo_im_cb(gpointer data) { |
|
| 49 PurpleSatoriProtocolIMInfo *info = data; |
|
| 50 |
|
| 51 purple_conversation_write_message(info->conversation, info->message); |
|
| 52 |
|
| 53 return G_SOURCE_REMOVE; |
|
| 54 } |
|
| 55 |
29 |
| 56 /****************************************************************************** |
30 /****************************************************************************** |
| 57 * PurpleProtocolConversation Implementation |
31 * PurpleProtocolConversation Implementation |
| 58 *****************************************************************************/ |
32 *****************************************************************************/ |
| 59 static PurpleCreateConversationDetails * |
33 static PurpleCreateConversationDetails * |
| 60 purple_satori_protocol_get_create_conversation_details(G_GNUC_UNUSED PurpleProtocolConversation *protocol, |
34 purple_satori_protocol_get_create_conversation_details(G_GNUC_UNUSED PurpleProtocolConversation *protocol, |
| 61 G_GNUC_UNUSED PurpleAccount *account) |
35 G_GNUC_UNUSED PurpleAccount *account) |
| 62 { |
36 { |
| 63 return purple_create_conversation_details_new(0); |
37 return purple_create_conversation_details_new(2); |
| 64 } |
38 } |
| 65 |
39 |
| 66 static void |
40 static void |
| 67 purple_satori_protocol_create_conversation_async(PurpleProtocolConversation *protocol, |
41 purple_satori_protocol_create_conversation_async(PurpleProtocolConversation *protocol, |
| 68 PurpleAccount *account, |
42 PurpleAccount *account, |
| 126 |
100 |
| 127 return g_task_propagate_pointer(task, error); |
101 return g_task_propagate_pointer(task, error); |
| 128 } |
102 } |
| 129 |
103 |
| 130 static void |
104 static void |
| 131 purple_satori_protocol_conversation_leave_conversation_async(PurpleProtocolConversation *protocol, |
105 purple_satori_protocol_send_message_async(PurpleProtocolConversation *protocol, |
| 132 G_GNUC_UNUSED PurpleConversation *conversation, |
106 PurpleConversation *conversation, |
| 133 GCancellable *cancellable, |
107 PurpleMessage *message, |
| 134 GAsyncReadyCallback callback, |
108 GCancellable *cancellable, |
| 135 gpointer data) |
109 GAsyncReadyCallback callback, |
| |
110 gpointer data) |
| 136 { |
111 { |
| 137 GTask *task = NULL; |
112 PurpleSatoriConnection *con = \ |
| 138 |
113 PURPLE_SATORI_CONNECTION(purple_conversation_get_connection(conversation)); |
| 139 task = g_task_new(protocol, cancellable, callback, data); |
|
| 140 g_task_set_source_tag(task, |
|
| 141 purple_satori_protocol_conversation_leave_conversation_async); |
|
| 142 |
|
| 143 g_task_return_boolean(task, TRUE); |
|
| 144 g_clear_object(&task); |
|
| 145 } |
|
| 146 |
|
| 147 static gboolean |
|
| 148 purple_satori_protocol_conversation_leave_conversation_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol, |
|
| 149 GAsyncResult *result, |
|
| 150 GError **error) |
|
| 151 { |
|
| 152 gpointer tag = purple_satori_protocol_conversation_leave_conversation_async; |
|
| 153 |
|
| 154 g_return_val_if_fail(g_async_result_is_tagged(result, tag), FALSE); |
|
| 155 |
|
| 156 return g_task_propagate_boolean(G_TASK(result), error); |
|
| 157 } |
|
| 158 |
|
| 159 static void |
|
| 160 purple_satori_protocol_send_message_async(G_GNUC_UNUSED PurpleProtocolConversation *protocol, |
|
| 161 PurpleConversation *conversation, |
|
| 162 PurpleMessage *message, |
|
| 163 GCancellable *cancellable, |
|
| 164 GAsyncReadyCallback callback, |
|
| 165 gpointer data) |
|
| 166 { |
|
| 167 GTask *task = NULL; |
|
| 168 |
|
| 169 if(purple_conversation_is_dm(conversation)) { |
|
| 170 PurpleAccount *account = NULL; |
|
| 171 PurpleContact *contact = NULL; |
|
| 172 PurpleContactInfo *contact_info = NULL; |
|
| 173 PurpleContactManager *manager = NULL; |
|
| 174 PurpleConversationMember *member = NULL; |
|
| 175 PurpleConversationMembers *members = NULL; |
|
| 176 |
|
| 177 account = purple_conversation_get_account(conversation); |
|
| 178 members = purple_conversation_get_members(conversation); |
|
| 179 |
|
| 180 manager = purple_contact_manager_get_default(); |
|
| 181 |
|
| 182 /* Check if this dm is with echo. */ |
|
| 183 contact = purple_contact_manager_find_with_id(manager, account, |
|
| 184 "echo"); |
|
| 185 contact_info = PURPLE_CONTACT_INFO(contact); |
|
| 186 member = purple_conversation_members_find_member(members, contact_info); |
|
| 187 if(PURPLE_IS_CONVERSATION_MEMBER(member)) { |
|
| 188 PurpleSatoriProtocolIMInfo *info = NULL; |
|
| 189 const char *contents = purple_message_get_contents(message); |
|
| 190 |
|
| 191 info = g_new(PurpleSatoriProtocolIMInfo, 1); |
|
| 192 info->conversation = g_object_ref(conversation); |
|
| 193 info->message = purple_message_new(member, contents); |
|
| 194 purple_message_set_edited(info->message, |
|
| 195 purple_message_get_edited(message)); |
|
| 196 |
|
| 197 g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, |
|
| 198 purple_satori_protocol_echo_im_cb, info, |
|
| 199 (GDestroyNotify)purple_satori_protocol_im_info_free); |
|
| 200 } |
|
| 201 |
|
| 202 /* Check if this dm is with aegina. */ |
|
| 203 contact = purple_contact_manager_find_with_id(manager, account, |
|
| 204 "aegina"); |
|
| 205 contact_info = PURPLE_CONTACT_INFO(contact); |
|
| 206 member = purple_conversation_members_find_member(members, contact_info); |
|
| 207 if(PURPLE_IS_CONVERSATION_MEMBER(member)) { |
|
| 208 PurpleSatoriProtocolIMInfo *info = g_new(PurpleSatoriProtocolIMInfo, 1); |
|
| 209 PurpleConversationMember *author = purple_message_get_author(message); |
|
| 210 PurpleContactInfo *author_info = NULL; |
|
| 211 const char *contents = NULL; |
|
| 212 const char *author_id = NULL; |
|
| 213 |
|
| 214 author_info = purple_conversation_member_get_contact_info(author); |
|
| 215 author_id = purple_contact_info_get_id(author_info); |
|
| 216 if(purple_strequal(author_id, "hades")) { |
|
| 217 contents = "🫥️"; |
|
| 218 } else { |
|
| 219 /* TRANSLATORS: This is a reference to the Cap of Invisibility owned by |
|
| 220 * various Greek gods, such as Hades, as mentioned. */ |
|
| 221 contents = _("Don't tell Hades I have his Cap"); |
|
| 222 } |
|
| 223 |
|
| 224 info->conversation = g_object_ref(conversation); |
|
| 225 info->message = purple_message_new(member, contents); |
|
| 226 |
|
| 227 g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, purple_satori_protocol_echo_im_cb, |
|
| 228 info, (GDestroyNotify)purple_satori_protocol_im_info_free); |
|
| 229 } |
|
| 230 } |
|
| 231 |
114 |
| 232 purple_conversation_write_message(conversation, message); |
115 purple_conversation_write_message(conversation, message); |
| 233 |
116 |
| 234 task = g_task_new(protocol, cancellable, callback, data); |
117 GTask *task = g_task_new(protocol, cancellable, callback, data); |
| 235 g_task_return_boolean(task, TRUE); |
118 g_task_set_source_tag(task, |
| |
119 purple_satori_protocol_send_message_async); |
| 236 |
120 |
| 237 g_clear_object(&task); |
121 gchar *content = \ |
| |
122 g_markup_escape_text(purple_message_get_contents(message), -1); |
| |
123 satori_send_message(con, conversation, message, content, task); |
| |
124 g_free(content); |
| |
125 |
| |
126 return; |
| 238 } |
127 } |
| 239 |
128 |
| 240 static gboolean |
129 static gboolean |
| 241 purple_satori_protocol_send_message_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol, |
130 purple_satori_protocol_send_message_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol, |
| 242 GAsyncResult *result, |
131 GAsyncResult *result, |