Код
path = debug.getinfo(1).source:match("^@?(.+[/\\]).-$"):gsub("\\","/")
Ranges = path.."raddilka.txt"
sMsg = "Текст для юзеров"
hFileName = "rasslka.dbs"
function string.InRange(ip,rnum)
local iIP = ip:iptonumber()
for i,v in ipairs(tRanges[rnum]) do
if v[1] <= iIP and iIP <= v[2] then
return true
end
end
end
function string.iptonumber(ip)
local i1, i2, i3, i4 = ip:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")
if i1 then
local r = i1*16777216+i2*65536+i3*256+i4
if r < 0 then error(ip) end
return r
end
end
function LoadRanges()
local f = io.open(Ranges,"r")
if f then
tRanges,tRangeNames = {},{}
local rnum = 0
for line in f:lines() do
local ip1,ip2 = line:match("^(%d+%.%d+%.%d+%.%d+)%-(%d+%.%d+%.%d+%.%d+)")
if ip1 and ip2 then
if rnum > 0 then
table.insert(tRanges[rnum],{ip1:iptonumber(),ip2:iptonumber()})
end
else
local rname = line:match("^%[(.+)%]")
if rname then
rnum = rnum+1
tRanges[rnum] = {}
tRangeNames[rnum] = rname
end
end
end
f:close()
end
end
function OnStartup()
hFileName = Core.GetPtokaXPath().."scripts/"..hFileName
bot = SetMan.GetString(21)
assert(LoadRanges(),"Не удалось загрузить список диапазонов")
if loadfile(hFileName) then
dofile(hFileName)
end
if not tIpBase then tIpBase = {} end
end
function UserConnected(tUser)
if tUser.sIP:InRange(rnum) then
if not tIpBase[tUser.sIP] then
Core.SendPmToUser(tUser, sBot, sMsg)
tIpBase[tUser.sIP] = 1
SaveToFile(tIpBase, "tIpBase", hFileName)
end
end
end
function SaveToFile(tTable, sTableName, sFile)
local f = io.open(sFile, "r+")
if f then
Serialize(tTable, sTableName, f)
f:flush()
f:close()
end
end
function Serialize(tTable, sTableName, hFile, sTab)
sTab=sTab or ""
hFile:write(sTab..sTableName.." = {\n")
for i,v in pairs(tTable) do
local sKey=(type(i)=="string") and string.format("[%q]",i) or string.format("[%d]",i)
if(type(v)=="table") then Serialize(v,sKey,hFile,sTab.."\t")
else
local sValue=(type(value)=="string") and string.format("%q",v) or tostring(v)
hFile:write(sTab.."\t"..sKey.." = "..sValue)
end
hFile:write(",\n")
end
hFile:write(sTab.."}")
end
RegConnected = UserConnected