purple_compat.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Discord plugin for libpurple
  3. * Copyright (C) 2016-2017 Eion Robb
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include <purple.h>
  19. #if PURPLE_VERSION_CHECK(3, 0, 0)
  20. #include <http.h>
  21. #endif
  22. #ifndef PURPLE_PLUGINS
  23. #define PURPLE_PLUGINS
  24. #endif
  25. #ifdef _WIN32
  26. #include <win32/win32dep.h>
  27. #endif
  28. // Purple2 compat functions
  29. #if !PURPLE_VERSION_CHECK(3, 0, 0)
  30. #define purple_connection_error purple_connection_error_reason
  31. #define purple_connection_get_protocol purple_connection_get_prpl
  32. #define PURPLE_CONNECTION_CONNECTING PURPLE_CONNECTING
  33. #define PURPLE_CONNECTION_CONNECTED PURPLE_CONNECTED
  34. #define PURPLE_CONNECTION_FLAG_HTML PURPLE_CONNECTION_HTML
  35. #define PURPLE_CONNECTION_FLAG_NO_BGCOLOR PURPLE_CONNECTION_NO_BGCOLOR
  36. #define PURPLE_CONNECTION_FLAG_NO_FONTSIZE PURPLE_CONNECTION_NO_FONTSIZE
  37. #define PURPLE_CONNECTION_FLAG_NO_IMAGES PURPLE_CONNECTION_NO_IMAGES
  38. #define purple_connection_set_flags(pc, f) ((pc)->flags = (f))
  39. #define purple_connection_get_flags(pc) ((pc)->flags)
  40. #define purple_blist_find_group purple_find_group
  41. #define purple_protocol_action_get_connection(action) ((PurpleConnection *) (action)->context)
  42. #define purple_protocol_action_new purple_plugin_action_new
  43. #define purple_protocol_get_id purple_plugin_get_id
  44. #define PurpleProtocolAction PurplePluginAction
  45. #define PurpleProtocolChatEntry struct proto_chat_entry
  46. #define PurpleChatConversation PurpleConvChat
  47. #define PurpleIMConversation PurpleConvIm
  48. #define purple_conversations_find_chat_with_account(id, account) \
  49. PURPLE_CONV_CHAT(purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, id, account))
  50. #define purple_chat_conversation_has_left purple_conv_chat_has_left
  51. #define PurpleConversationUpdateType PurpleConvUpdateType
  52. #define PURPLE_CONVERSATION_UPDATE_UNSEEN PURPLE_CONV_UPDATE_UNSEEN
  53. #define PURPLE_IS_IM_CONVERSATION(conv) (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
  54. #define PURPLE_IS_CHAT_CONVERSATION(conv) (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
  55. #define PURPLE_CONVERSATION(chatorim) (chatorim == NULL ? NULL : chatorim->conv)
  56. #define PURPLE_IM_CONVERSATION(conv) PURPLE_CONV_IM(conv)
  57. #define PURPLE_CHAT_CONVERSATION(conv) PURPLE_CONV_CHAT(conv)
  58. #define purple_conversation_present_error purple_conv_present_error
  59. #define purple_serv_got_joined_chat(pc, id, name) PURPLE_CONV_CHAT(serv_got_joined_chat(pc, id, name))
  60. static inline PurpleConvChat *
  61. purple_conversations_find_chat(PurpleConnection *pc, int id)
  62. {
  63. PurpleConversation *conv = purple_find_chat(pc, id);
  64. if (conv != NULL) {
  65. return PURPLE_CONV_CHAT(conv);
  66. }
  67. return NULL;
  68. }
  69. #define purple_serv_got_chat_in serv_got_chat_in
  70. #define purple_chat_conversation_add_user purple_conv_chat_add_user
  71. #define purple_chat_conversation_add_users purple_conv_chat_add_users
  72. #define purple_chat_conversation_remove_user purple_conv_chat_remove_user
  73. #define purple_chat_conversation_clear_users purple_conv_chat_clear_users
  74. #define purple_chat_conversation_has_user purple_conv_chat_find_user
  75. #define purple_chat_conversation_rename_user purple_conv_chat_rename_user
  76. #define purple_chat_conversation_get_topic purple_conv_chat_get_topic
  77. #define purple_chat_conversation_set_topic purple_conv_chat_set_topic
  78. #define PurpleChatUserFlags PurpleConvChatBuddyFlags
  79. #define PURPLE_CHAT_USER_NONE PURPLE_CBFLAGS_NONE
  80. #define PURPLE_CHAT_USER_OP PURPLE_CBFLAGS_OP
  81. #define PURPLE_CHAT_USER_FOUNDER PURPLE_CBFLAGS_FOUNDER
  82. #define PURPLE_CHAT_USER_TYPING PURPLE_CBFLAGS_TYPING
  83. #define PURPLE_CHAT_USER_AWAY PURPLE_CBFLAGS_AWAY
  84. #define PURPLE_CHAT_USER_HALFOP PURPLE_CBFLAGS_HALFOP
  85. #define PURPLE_CHAT_USER_VOICE PURPLE_CBFLAGS_VOICE
  86. #define PURPLE_CHAT_USER_TYPING PURPLE_CBFLAGS_TYPING
  87. #define PurpleChatUser PurpleConvChatBuddy
  88. static inline PurpleChatUser *
  89. purple_chat_conversation_find_user(PurpleChatConversation *chat, const char *name)
  90. {
  91. PurpleChatUser *cb = purple_conv_chat_cb_find(chat, name);
  92. if (cb != NULL) {
  93. g_dataset_set_data(cb, "chat", chat);
  94. }
  95. return cb;
  96. }
  97. #define purple_chat_user_get_flags(cb) purple_conv_chat_user_get_flags(g_dataset_get_data((cb), "chat"), (cb)->name)
  98. #define purple_chat_user_set_flags(cb, f) purple_conv_chat_user_set_flags(g_dataset_get_data((cb), "chat"), (cb)->name, (f))
  99. #define purple_chat_user_set_alias(cb, a) (g_free((cb)->alias), (cb)->alias = g_strdup(a))
  100. #define PurpleIMTypingState PurpleTypingState
  101. #define PURPLE_IM_NOT_TYPING PURPLE_NOT_TYPING
  102. #define PURPLE_IM_TYPING PURPLE_TYPING
  103. #define PURPLE_IM_TYPED PURPLE_TYPED
  104. #define purple_conversation_get_connection purple_conversation_get_gc
  105. #define purple_conversation_write_system_message(conv, message, flags) purple_conversation_write((conv), NULL, (message), ((flags) | PURPLE_MESSAGE_SYSTEM), time(NULL))
  106. #define purple_chat_conversation_get_id purple_conv_chat_get_id
  107. #define PURPLE_CMD_FLAG_PROTOCOL_ONLY PURPLE_CMD_FLAG_PRPL_ONLY
  108. #define PURPLE_IS_BUDDY PURPLE_BLIST_NODE_IS_BUDDY
  109. #define PURPLE_IS_CHAT PURPLE_BLIST_NODE_IS_CHAT
  110. #define purple_chat_get_name_only purple_chat_get_name
  111. #define purple_blist_find_buddy purple_find_buddy
  112. #define purple_serv_got_alias serv_got_alias
  113. #define purple_account_set_private_alias purple_account_set_alias
  114. #define purple_account_get_private_alias purple_account_get_alias
  115. #define purple_protocol_got_user_status purple_prpl_got_user_status
  116. #define purple_protocol_got_user_idle purple_prpl_got_user_idle
  117. #define purple_serv_got_im serv_got_im
  118. #define purple_serv_got_typing serv_got_typing
  119. #define purple_conversations_find_im_with_account(name, account) \
  120. PURPLE_CONV_IM(purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, name, account))
  121. #define purple_im_conversation_new(account, from) PURPLE_CONV_IM(purple_conversation_new(PURPLE_CONV_TYPE_IM, account, from))
  122. #define PurpleMessage PurpleConvMessage
  123. #define purple_message_set_time(msg, time) ((msg)->when = (time))
  124. #define purple_conversation_write_message(conv, msg) purple_conversation_write(conv, msg->who, msg->what, msg->flags, msg->when)
  125. static inline PurpleMessage *
  126. purple_message_new_outgoing(const gchar *who, const gchar *contents, PurpleMessageFlags flags)
  127. {
  128. PurpleMessage *message = g_new0(PurpleMessage, 1);
  129. message->who = g_strdup(who);
  130. message->what = g_strdup(contents);
  131. message->flags = flags;
  132. message->when = time(NULL);
  133. return message;
  134. }
  135. static inline void
  136. purple_message_destroy(PurpleMessage *message)
  137. {
  138. g_free(message->who);
  139. g_free(message->what);
  140. g_free(message);
  141. }
  142. #define purple_message_get_recipient(message) (message->who)
  143. #define purple_message_get_contents(message) (message->what)
  144. #if !PURPLE_VERSION_CHECK(2, 12, 0)
  145. #define PURPLE_MESSAGE_REMOTE_SEND 0x10000
  146. #endif
  147. #define purple_account_privacy_deny_add purple_privacy_deny_add
  148. #define purple_account_privacy_deny_remove purple_privacy_deny_remove
  149. #define PurpleHttpConnection PurpleUtilFetchUrlData
  150. #define purple_buddy_set_name purple_blist_rename_buddy
  151. #define purple_request_cpar_from_connection(a) purple_connection_get_account(a), NULL, NULL
  152. #define purple_notify_user_info_add_pair_html purple_notify_user_info_add_pair
  153. #ifdef purple_notify_error
  154. #undef purple_notify_error
  155. #endif
  156. #define purple_notify_error(handle, title, primary, secondary, cpar) \
  157. purple_notify_message((handle), PURPLE_NOTIFY_MSG_ERROR, (title), \
  158. (primary), (secondary), NULL, NULL)
  159. // Kinda gross, since we can technically use the glib mainloop from purple2
  160. #define g_timeout_add_seconds purple_timeout_add_seconds
  161. #define g_timeout_add purple_timeout_add
  162. #define g_source_remove purple_timeout_remove
  163. #else
  164. // Purple3 helper functions
  165. #define purple_conversation_set_data(conv, key, value) g_object_set_data(G_OBJECT(conv), key, value)
  166. #define purple_conversation_get_data(conv, key) g_object_get_data(G_OBJECT(conv), key)
  167. #define purple_message_destroy g_object_unref
  168. #define purple_chat_user_set_alias(cb, alias) g_object_set((cb), "alias", (alias), NULL)
  169. #define purple_chat_get_alias(chat) g_object_get_data(G_OBJECT(chat), "alias")
  170. #define purple_protocol_action_get_connection(action) ((action)->connection)
  171. #define PURPLE_TYPE_STRING G_TYPE_STRING
  172. //TODO remove this when dx adds this to the PurpleMessageFlags enum
  173. #define PURPLE_MESSAGE_REMOTE_SEND 0x10000
  174. #endif