FilterUtil.lua 4.8 KB

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