chated.lua 1.43 KB
local skynet = require "skynet"
require "skynet.manager"
local crab = require "crab.c"

local table_insert = table.insert
local table_unpack = table.unpack
local mode, id, dict = ...
local hideCode = utf8.codepoint("*")


local function toutf8(name)
	local t = {}
	for _, v in utf8.codes(name) do
		table_insert(t, v)
	end
	return t
end

local function fix(a, b)
	for i, v in pairs(a) do
		if b[i] == hideCode then
			a[i] = hideCode
		end
	end
	return utf8.char(table_unpack(a))
end


if mode == "sub" then
	local CMD = {}
	dict = tonumber(dict)

	function CMD.check(name)
		if name:find("%c") then
			return false
		end
		local utftb = toutf8(name)
		local utftbU = toutf8(string.upper(name))
		if crab.filter(dict, utftbU) then
			return false, fix(utftb, utftbU)
		end
		return true
	end

	skynet.start(function()
		skynet.dispatch("lua", function(_, _, command, ...)
			local f = CMD[command]
			skynet.ret(skynet.pack(f(...)))
		end)

		skynet.register(string.format(".chated%d", id))
	end)
else
	skynet.start(function()
		local csvdb = require "shared.csvdata"
		local forbidNames = csvdb["forbid_chatCsv"]
		if not forbidNames then forbidNames = {} end

		local words = {}
		for _, data in ipairs(forbidNames) do
			local ok, utftb = pcall(toutf8, string.upper(data.name))
			if ok then table.insert(words, utftb) end
		end
		local d = crab.open(words)

		for i = 1, 5 do
			skynet.newservice(SERVICE_NAME, "sub", i, d)
		end
		skynet.exit()
	end)
end