PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/Filtering.lua

https://bitbucket.org/sylvanaar2/prat-3.0
Lua | 295 lines | 157 code | 46 blank | 92 comment | 26 complexity | 1d665ad6603e4ef19cd4d63eb0245490 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. ---------------------------------------------------------------------------------
  2. --
  3. -- Prat - A framework for World of Warcraft chat mods
  4. --
  5. -- Copyright (C) 2006-2011 Prat Development Team
  6. --
  7. -- This program is free software; you can redistribute it and/or
  8. -- modify it under the terms of the GNU General Public License
  9. -- as published by the Free Software Foundation; either version 2
  10. -- of the License, or (at your option) any later version.
  11. --
  12. -- This program is distributed in the hope that it will be useful,
  13. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. -- GNU General Public License for more details.
  16. --
  17. -- You should have received a copy of the GNU General Public License
  18. -- along with this program; if not, write to:
  19. --
  20. -- Free Software Foundation, Inc.,
  21. -- 51 Franklin Street, Fifth Floor,
  22. -- Boston, MA 02110-1301, USA.
  23. --
  24. --
  25. -------------------------------------------------------------------------------
  26. Prat:AddModuleToLoad(function()
  27. local PRAT_MODULE = Prat:RequestModuleName("Filtering")
  28. if PRAT_MODULE == nil then
  29. return
  30. end
  31. local L = Prat:GetLocalizer({})
  32. --@debug@
  33. L:AddLocale("enUS", {
  34. ["Filtering"] = true,
  35. ["A module to provide basic chat filtering."] = true,
  36. ["leavejoin_name"] = "Filter Channel Leave/Join",
  37. ["leavejoin_desc"] = "Filter out channel leave/join spam",
  38. ["notices_name"] = "Filter Channel Notices",
  39. ["notices_desc"] = "Filter out other custom channel notification messages, e.g. moderator changes.",
  40. ["bgjoin_name"] = "Filter BG Leave/Join",
  41. ["bgjoin_desc"] = "Filter out channel Battleground leave/join spam",
  42. ["tradespam_name"] = "Throttle Spam",
  43. ["tradespam_desc"] = "Throttle messages to prevent the same message from being repeated multiple times",
  44. ["afkdnd_name"] = "Throttle AFK and DND messages.",
  45. ["afkdnd_desc"] = "Throttle AFK and DND messages."
  46. })
  47. --@end-debug@
  48. -- These Localizations are auto-generated. To help with localization
  49. -- please go to http://www.wowace.com/projects/prat-3-0/localization/
  50. --[===[@non-debug@
  51. L:AddLocale("enUS",
  52. --@localization(locale="enUS", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  53. )
  54. L:AddLocale("itIT",
  55. --@localization(locale="itIT", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  56. )
  57. L:AddLocale("ptBR",
  58. --@localization(locale="ptBR", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  59. )
  60. L:AddLocale("frFR",
  61. --@localization(locale="frFR", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  62. )
  63. L:AddLocale("deDE",
  64. --@localization(locale="deDE", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  65. )
  66. L:AddLocale("koKR",
  67. --@localization(locale="koKR", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  68. )
  69. L:AddLocale("esMX",
  70. --@localization(locale="esMX", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  71. )
  72. L:AddLocale("ruRU",
  73. --@localization(locale="ruRU", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  74. )
  75. L:AddLocale("zhCN",
  76. --@localization(locale="zhCN", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  77. )
  78. L:AddLocale("esES",
  79. --@localization(locale="esES", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  80. )
  81. L:AddLocale("zhTW",
  82. --@localization(locale="zhTW", format="lua_table", same-key-is-true=true, namespace="Filtering")@
  83. )
  84. --@end-non-debug@]===]
  85. local module = Prat:NewModule(PRAT_MODULE, "AceEvent-3.0")
  86. Prat:SetModuleDefaults(module, {
  87. profile = {
  88. on = false,
  89. leavejoin = true,
  90. notices = true,
  91. tradespam = false,
  92. afkdnd = true,
  93. }
  94. } )
  95. Prat:SetModuleOptions(module, {
  96. name = L["Filtering"] ,
  97. desc = L["A module to provide basic chat filtering."],
  98. type = "group",
  99. args = {
  100. -- leavejoin = {
  101. -- name = L["leavejoin_name"],
  102. -- desc = L["leavejoin_desc"],
  103. -- type = "toggle",
  104. -- order = 100
  105. -- },
  106. notices = {
  107. name = L["notices_name"],
  108. desc = L["notices_desc"],
  109. type = "toggle",
  110. order = 110
  111. },
  112. tradespam = {
  113. name = L["tradespam_name"],
  114. desc = L["tradespam_desc"],
  115. type = "toggle",
  116. order = 115
  117. },
  118. afkdnd = {
  119. name = L["afkdnd_name"],
  120. desc = L["afkdnd_desc"],
  121. type = "toggle",
  122. order = 115
  123. }
  124. -- bgjoin = {
  125. -- name = L["bgjoin_name"],
  126. -- desc = L["bgjoin_desc"],
  127. -- type = "toggle",
  128. -- order = 111
  129. -- },
  130. }
  131. }
  132. )
  133. local THROTTLE_TIME = 120
  134. MessageTime = {}
  135. local function cleanText(msg, author)
  136. local cleanmsg = msg:gsub("...hic!",""):gsub("%d",""):gsub("%c",""):gsub("%p",""):gsub("%s",""):upper():gsub("SH","S");
  137. return (author and author:upper() or "") .. cleanmsg;
  138. end
  139. --function tradeSpamFilter(frame, event, ...)
  140. -- local arg1, arg2 = ...
  141. -- local block = false;
  142. -- local msg = cleanText(arg1, arg2);
  143. --
  144. -- if arg2 == UnitName("player") then
  145. -- return false, ...
  146. -- end
  147. --
  148. -- if MessageTime[msg] then
  149. -- if difftime(time(), MessageTime[msg]) <= THROTTLE_TIME then
  150. -- block = true;
  151. -- else
  152. -- MessageTime[msg] = nil
  153. -- end
  154. -- else
  155. -- MessageTime[msg] = time();
  156. -- end
  157. --
  158. -- if block then
  159. -- print("Filtered: "..msg)
  160. -- return true
  161. -- end
  162. --
  163. --
  164. --
  165. -- return false, ...
  166. --end
  167. --[[------------------------------------------------
  168. Module Event Functions
  169. ------------------------------------------------]]--
  170. function module:OnModuleEnable()
  171. self.throttleFrame = self.throttleFrame or CreateFrame("FRAME");
  172. self.throttle = THROTTLE_TIME
  173. self.throttleFrame:SetScript("OnUpdate",
  174. function(frame, elapsed)
  175. self.throttle = self.throttle - elapsed
  176. if frame:IsShown() and self.throttle < 0 then
  177. self.throttle = THROTTLE_TIME
  178. self:PruneMessages()
  179. end
  180. end)
  181. -- ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", tradeSpamFilter)
  182. -- ChatFrame_AddMessageEventFilter("CHAT_MSG_YELL", tradeSpamFilter)
  183. Prat.RegisterChatEvent(self, "Prat_FrameMessage")
  184. end
  185. -- things to do when the module is disabled
  186. function module:OnModuleDisable()
  187. -- ChatFrame_RemoveMessageEventFilter("CHAT_MSG_CHANNEL", tradeSpamFilter)
  188. -- ChatFrame_RemoveMessageEventFilter("CHAT_MSG_YELL", tradeSpamFilter)
  189. Prat.UnregisterAllChatEvents(self)
  190. end
  191. --[[------------------------------------------------
  192. Core Functions
  193. ------------------------------------------------]]--
  194. function module:PruneMessages()
  195. for k,v in pairs(MessageTime) do
  196. if difftime(time(), v) > THROTTLE_TIME then
  197. MessageTime[k] = nil
  198. end
  199. end
  200. end
  201. function module:Prat_FrameMessage(arg, message, frame, event)
  202. local newEvent = true
  203. if Prat.EVENT_ID and
  204. Prat.EVENT_ID == self.lastevent and
  205. self.lasteventtype == event then
  206. newEvent = false
  207. end
  208. if self.db.profile.tradespam then
  209. if event == "CHAT_MSG_CHANNEL" or event == "CHAT_MSG_YELL" then
  210. local msg = cleanText(message.ORG.MESSAGE, message.ORG.PLAYER)
  211. if message.ORG.PLAYER ~= UnitName("player") then
  212. if newEvent and MessageTime[msg] then
  213. if difftime(time(), MessageTime[msg]) <= THROTTLE_TIME then
  214. message.DONOTPROCESS = true
  215. else
  216. MessageTime[msg] = nil
  217. end
  218. else
  219. self.lasteventtype = event
  220. self.lastevent = Prat.EVENT_ID
  221. MessageTime[msg] = time();
  222. end
  223. end
  224. end
  225. end
  226. if self.db.profile.afkdnd then
  227. if event == "CHAT_MSG_AFK" or event == "CHAT_MSG_DND" then
  228. local msg = cleanText(message.ORG.MESSAGE, message.ORG.PLAYER)
  229. if newEvent and MessageTime[msg] then
  230. if difftime(time(), MessageTime[msg]) <= THROTTLE_TIME then
  231. message.DONOTPROCESS = true
  232. else
  233. MessageTime[msg] = nil
  234. end
  235. else
  236. self.lasteventtype = event
  237. self.lastevent = Prat.EVENT_ID
  238. MessageTime[msg] = time();
  239. end
  240. end
  241. end
  242. if self.db.profile.notices then
  243. if event == "CHAT_MSG_CHANNEL_NOTICE_USER" or event == "CHAT_MSG_CHANNEL_NOTICE" then
  244. message.DONOTPROCESS = true
  245. end
  246. end
  247. end
  248. return
  249. end ) -- Prat:AddModuleToLoad