Refresh in adding ips to a table in this script Plz
 

Refresh in adding ips to a table in this script Plz

Started by ((UKSN))shad_dow, 04 January, 2005, 22:15:15

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

((UKSN))shad_dow

Hi All

right i get to the point cos every sec counts

i found this scaript (below ) , ok i need to be hinted at how to add ips to the Table , ive been away while and need a refresh on it Plz

--------------------------
-- simple ipban script
-- made on request of erik
--------------------------
-- bans are stored in an external file
-- use 1 IP per line
-- or IP|IP for ranges
--------------------------

sBanMsg = "Your ass is banned!!!"

function Main()
   tBanIp = {}
   if readfrom("ipban/ipban.txt") then
      while 1 do
         local line = read()
         if line == nil then 
            break 
         end
         if strfind(line, "|") then
            local _,_,sStart,sEnd = strfind(line, "^([%.%d])|([%.%d])$")
            tBanIp[(IPtoDec(sStart))]=(IPtoDec(sEnd))
         else
            tBanIp[line]=1
         end
      end
      readfrom()
   else
      execute("mkdir ipban")
      writeto("ipban/ipban.txt")
      writeto()
   end
end

function IPtoDec(IP)
   local s,e,dIP1,dIP2,dIP3,dIP4 = strfind(IP, "^(%d+)%.(%d+)%.(%d+)%.(%d+)$")
   IP = (tonumber(dIP1) * 16777216) + (tonumber(dIP2) *65536) + (tonumber(dIP3) * 256) + (tonumber(dIP4))
   return IP
end

function NewUserConnected(user) 
   if tBanIp[user.sIP] then
      user:SendData(sBanMsg)
      user:Disconnect()
   else
      local sIp = IPtoDec(user.sIP)
      for a,b in tBanIp do
         if b ~= 1 and sIp >= a and sIp <= b then
            user:SendData(sBanMsg)
            user:Disconnect()
         end
      end
   end
end

cheers

shad
creator of Therapy-X? bot

??????Hawk??????

#1
hi m8  ...

your answer is lookin at you  ..
in these 2 lines
tBanIp[(IPtoDec(sStart))]=(IPtoDec(sEnd))

tBanIp[line]=1
you all ready Know how to save a table to a file

so using the commands,

!addip [SingleIP]   using   tBanIp[ip]=1

and

!addrange [Ip1-Ip2]      using     tBanIp[IP1]=Ip2    with a simple    strfind to grab the two ip's using the  -  as a  fix point

then call the  Save Table to file  function..

hope  this helps  :-)


HAVE  FUN...........

bastya_elvtars

Everything could have been anything else and it would have just as much meaning.

??????Hawk??????

#3
hey  shad  ...  

dont look at  Bastya's link yet  ..  :P

I KNOW  you can  do it ive seen your scripts...

have a look at it once youve  sussed it for yourself..

 :P  :P  :P  :P  :P  :P  :P  :P  :P  :P

bastya_elvtars

QuoteOriginally posted by ??????Hawk??????
hey  shad  ...  

dont look at  Bastya's link yet  ..  :P

I KNOW  you can  do it ive seen your scripts...

have a look at it once youve  sussed it for yourself..

 :P  :P  :P  :P  :P  :P  :P  :P  :P  :P

i knew you posted the right answer but a little self-commercial is never harmful ;p
Everything could have been anything else and it would have just as much meaning.

??????Hawk??????

#5
;) ;)  ;)

((UKSN))shad_dow

ok hawk and bastya_elvtars


i going to give it a whill and if i get stuck il have a loo at range fucker :)

thank u for the convdience

yours

shad
creator of Therapy-X? bot

((UKSN))shad_dow

yo guys


from what i can make out with wot u sugest hawk , should be easy since it read the ip.txt file and loads the ip in to the Table . mmm ok think i found  the way

il post here with my progress

yours

shad
creator of Therapy-X? bot

((UKSN))shad_dow

hi hawk and bastya

heres the modifed code

--------------------------
-- simple ipban script
-- made on request of erik
--------------------------
-- bans are stored in an external file
-- use 1 IP per line
-- or IP|IP for ranges
--------------------------
-- Modifed By Shad_dow For Therapy-XS
-- Added command !addip 
-- Added cammand !addrange 
--------------------------

Bot = "Therapy-IPBlocker"
FILE = "ipban/ipban.txt"

function Main() 
	frmHub:RegBot(Bot) 
tBanIp = {}
   if readfrom("ipban/ipban.txt") then
      while 1 do
         local line = read()
         if line == nil then 
            break 
         end
         if strfind(line, "|") then
            local _,_,sStart,sEnd = strfind(line, "^([%.%d])|([%.%d])$")
            tBanIp[(IPtoDec(sStart))]=(IPtoDec(sEnd))
         else
            tBanIp[line]=1
         end
      end
      readfrom()
   else
      execute("mkdir ipban")
      writeto("ipban/ipban.txt")
      
   end
end 



function NewUserConnected(curUser) 
   if tBanIp[curUser.sIP] then
      curUser:SendData(sBanMsg)
      curUser:Disconnect()
   else
      local sIp = IPtoDec(curUser.sIP)
      for a,b in tBanIp do
         if b ~= 1 and sIp >= a and sIp <= b then
            curUser:SendData(sBanMsg)
            curUser:Disconnect()
         end
      end
   end
end 

function DataArrival (curUser, data)
if (strsub(data,1,1) == "<")  then
data=strsub(data,1,strlen(data)-1)
local s,v, cmd = strfind(data, "%b<>%s+(%S+)")
	if curUser.bOperator then
		if cmd == "!addip" then
			dowritesingle(curUser,data)

			
return 1 

	elseif  cmd == "!addrange" then
			dowriterange(curUser,data)
			
return 1 
		end
	end
end
	
	end	



function IPtoDec(IP)
   local s,e,dIP1,dIP2,dIP3,dIP4 = strfind(IP, "^(%d+)%.(%d+)%.(%d+)%.(%d+)$")
   IP = (tonumber(dIP1) * 16777216) + (tonumber(dIP2) *65536) + (tonumber(dIP3) * 256) + (tonumber(dIP4))
   return IP
end



function dowriterange(curUser,data)
		arg1,arg2 = GetArgs2(data)
			if arg1 ~= nil and arg2 ~= nil then 
				local handle =appendto(FILE)
				
				write(handle,""..arg1.."|"..arg2.."\r\n")
				closefile(handle)
SendToAll(Bot,"IP Range Ban Added")
			elseif arg1 ~= nil and arg2 ~= nil then 
				curUser:SendPM(Bot,"Cmdet is !rangeban 0.0.0.1 0.0.0.2")
			end
		end

function dowritesingle(curUser,data)
		arg1 = GetArgs1(data)
			if arg1 ~= nil  then 
				
				local handle =appendto(FILE)
write(handle,""..arg1.."\r\n")
closefile(handle)
SendToAll(Bot,"IP  Ban Added")

			elseif arg1 == nil then 
				curUser:SendPM(Bot,"Cmdet is !ipban 0.0.0.1")
			end
		end


function GetArgs1(data)
	local _,_, arg1 = strfind(data,"%s+%S+%s+(.+)")
	return arg1
end
function GetArgs2(data)
	local _,_, arg1,arg2 = strfind(data,"%s+%S+%s+(%S+)%s+(.+)")
	return arg1,arg2
end

and heres the prob ipban.txt looks like this

7.0.0.2
6.32.53.1|6.32.53.254

when it lods it back in from main fuction i get this error



Syntax error: bad argument #1 to `strfind' (string expected, got nil)
stack traceback:
   1:  function `strfind' [C]
   2:  function `IPtoDec' at line 82 [file `...okax hubs\test hub\scripts\testingipbanning.lua']
   3:  function `Main' at line 28 [file `...okax hubs\test hub\scripts\testingipbanning.lua']

this is the lines
if strfind(line, "|") then
            local _,_,sStart,sEnd = strfind(line, "^([%.%d])|([%.%d])$")
            tBanIp[(IPtoDec(sStart))]=(IPtoDec(sEnd))
         else
            tBanIp[line]=1
         end
 

any help would be lovly

yours

shad
creator of Therapy-X? bot

??????Hawk??????

hi m8  

try the strfind  like this:-
local _,_,sStart,sEnd = strfind(line,"(%d+%.%d+%.%d+%.%d+)|(%d+%.%d+%.%d+%.%d+)")

SMF spam blocked by CleanTalk