FilterUtil.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ----------------------------------
  2. -- 屏蔽词过滤
  3. -- filter 聊天信息过滤
  4. -- filterName 角色名过滤
  5. ----------------------------------
  6. local Dict = require("common.Dict")
  7. local Dict2 = require("common.Dict2")
  8. local Config = require("Config")
  9. local CommonDefine = require("common.CommonDefine")
  10. local function isAllowSpaceInName()
  11. return table.find(Config.SVR_CHANEL or {}, CommonDefine.CHANNEL_TAG_VN) ~= nil
  12. end
  13. -- 初始
  14. local dict_maxx = 0
  15. for k in pairs(Dict) do
  16. if type(k) == "string" then
  17. local klen = #k
  18. if klen < 1 then
  19. assert()
  20. end
  21. if dict_maxx < klen then
  22. dict_maxx = klen
  23. end
  24. end
  25. end
  26. local DictBol = {
  27. ["["] = true,
  28. ["]"]= true,
  29. ["\\"]= true,
  30. ["\""]= true,
  31. ["'"]= true,
  32. ["/"]= true,
  33. [" "]= true,
  34. [" "]= true,
  35. [" "]= true,
  36. ["@"]= true,
  37. ["&"]= true,
  38. ["`"]= true,
  39. ["~"]= true,
  40. ["$"]= true,
  41. ["%"]= true,
  42. ["^"]= true,
  43. ["*"]= true,
  44. ["("]= true,
  45. [")"]= true,
  46. ["-"]= true,
  47. ["{"]= true,
  48. ["}"]= true,
  49. [":"]= true,
  50. [";"]= true,
  51. ["|"]= true,
  52. ["<"]= true,
  53. [">"]= true,
  54. ["?"]= true,
  55. ["."]= true,
  56. ["="]= true}
  57. local tb_bol = {}
  58. function filter_bol(a, dict, dictNum)
  59. dict = dict or DictBol
  60. local tblen = len or 0
  61. local i = 1
  62. local alen = #a
  63. local ac
  64. while i <= alen do
  65. local j = math.min(alen, i + dict_maxx - 1)
  66. while i <= j do
  67. if dict[string.sub(a, i, j)] == true then
  68. break
  69. end
  70. if dictNum ~= nil and dictNum == true then
  71. if dict.filterNum[string.sub(a, i, j)] ~= nil then
  72. break
  73. end
  74. end
  75. j = j - 1
  76. end
  77. if j < i then
  78. tblen = tblen + 1
  79. tb_bol[tblen] = string.sub(a, i, i)
  80. i = i + 1
  81. else
  82. ac = true
  83. tblen = tblen + 1
  84. tb_bol[tblen] = ""
  85. i = j + 1
  86. end
  87. end
  88. if not ac then
  89. return a
  90. end
  91. local b = table.concat(tb_bol, nil, 1, tblen)
  92. return b
  93. end
  94. local tb_dest = {}
  95. function filter_dest(a, dict, dictNum)
  96. dict = dict or Dict
  97. local tblen = len or 0
  98. local i = 1
  99. local alen = #a
  100. local ac
  101. while i <= alen do
  102. local j = math.min(alen, i + dict_maxx - 1)
  103. while i <= j do
  104. if dict[string.sub(a, i, j)] == true then
  105. break
  106. end
  107. if dictNum ~= nil and dictNum == true then
  108. if dict.filterNum[string.sub(a, i, j)] ~= nil then
  109. break
  110. end
  111. end
  112. j = j - 1
  113. end
  114. if j < i then
  115. tblen = tblen + 1
  116. tb_dest[tblen] = string.sub(a, i, i)
  117. i = i + 1
  118. else
  119. ac = true
  120. tblen = tblen + 1
  121. tb_dest[tblen] = "*"
  122. i = j + 1
  123. end
  124. end
  125. if not ac then
  126. return a
  127. end
  128. local b = table.concat(tb_dest, nil, 1, tblen)
  129. return b
  130. end
  131. function filter(a, dict, dictNum)
  132. --a = filter_bol(a, dict, dictNum)
  133. a = filter_dest(a, dict, dictNum)
  134. a = filter_dest(a, Dict2, dictNum)
  135. return a
  136. end
  137. local fk_letter = {
  138. "[","]","\\","\"","'","/"," "," "," ","@","&","`","~","$","%","^","*","(",")","-","{","}",":",";","|","<",">","?",".","="}
  139. function filterName(name)
  140. local allowSpace = isAllowSpaceInName()
  141. local ret = filter_spec_chars(name, allowSpace)
  142. if ret ~= name then
  143. return
  144. end
  145. for _, v in ipairs(fk_letter) do
  146. if not (allowSpace and v == " ") and string.find(name, v, 1, true) then
  147. return
  148. end
  149. end
  150. return filter(name)
  151. end
  152. function filter_spec_chars(s, allowSpace)
  153. local ss = {}
  154. local k = 1
  155. while true do
  156. if k > #s then break end
  157. local c = string.byte(s,k)
  158. if not c then break end
  159. if c<192 then
  160. if (c>=48 and c<=57) or (c>= 65 and c<=90) or (c>=97 and c<=122) or (allowSpace and c == 32) then
  161. table.insert(ss, string.char(c))
  162. end
  163. k = k + 1
  164. elseif c<224 then
  165. local c1 = string.byte(s,k+1)
  166. if c1 then
  167. table.insert(ss, string.char(c, c1))
  168. end
  169. k = k + 2
  170. elseif c<240 then
  171. local c1 = string.byte(s,k+1)
  172. local c2 = string.byte(s,k+2)
  173. if c1 and c2 then
  174. local unic = (c % 0xe0) * 2 ^ 12 + (c1 % 0x80) * 2 ^ 6 + (c2 % 0x80)
  175. if unic >= 0x4e00 and unic <= 0x9FA5 then
  176. table.insert(ss, string.char(c, c1, c2))
  177. end
  178. end
  179. k = k + 3
  180. elseif c<248 then
  181. local c1 = string.byte(s,k+1)
  182. local c2 = string.byte(s,k+2)
  183. local c3 = string.byte(s,k+3)
  184. if c1 and c2 and c3 then
  185. table.insert(ss, string.char(c, c1, c2, c3))
  186. end
  187. k = k + 4
  188. elseif c<252 then
  189. local c1 = string.byte(s,k+1)
  190. local c2 = string.byte(s,k+2)
  191. local c3 = string.byte(s,k+3)
  192. local c4 = string.byte(s,k+4)
  193. if c1 and c2 and c3 and c4 then
  194. table.insert(ss, string.char(c, c1, c2, c3, c4))
  195. end
  196. k = k + 5
  197. elseif c<254 then
  198. local c1 = string.byte(s,k+1)
  199. local c2 = string.byte(s,k+2)
  200. local c3 = string.byte(s,k+3)
  201. local c4 = string.byte(s,k+4)
  202. local c5 = string.byte(s,k+5)
  203. if c1 and c2 and c3 and c4 and c5 then
  204. table.insert(ss, string.char(c, c1, c2, c3, c4, c5))
  205. end
  206. k = k + 6
  207. else
  208. assert(nil)
  209. end
  210. end
  211. return table.concat(ss)
  212. end