Can somebody convert ?this script to api2
 

Can somebody convert ?this script to api2

Started by Mr.T, 30 August, 2008, 13:52:53

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mr.T

--[[  
 
	Birthday Man v.4.04 - LUA 5.0/5.1 by jiten (4/15/2006)
	
	Translation, bugfixes, Tablesorting, formated text and more by Herman and KiviE 
 
	Based on: BirthdayMan v 3.5 by Herodes and mods by C??o?y?? 
 
	Changelog: 
	 
	Rewritten: Whole code; 
 
	Changed: Command structure to tables (Function, Permissions, RightClick and Ddescription); 
	Changed: B-Day table structure to Julian values; 
	Removed: File content that was sent on B-Day; 
	Removed: Allowed Age; 
	Removed: Topic updater (maybe in the future); 
	Changed: Whole B-Day counting system to os.*; 
	Changed: Commands returned according to input (Main or PM); 
	Changed: Many other mods that can't recall at the moment (12-04-2006); 
	Added: addbirthday command - requested by TT (13-04-2006); 
	Fixed: Zodiac Sign Dates - thanks to Walazo; 
	Changed: Error message on empty !bornon result - thanks to Walazo; 
	Changed: !birthdays' content structure to fix bad tabbing; 
	Changed: Settings.iVer; 
	Added: string.lower checks - thanks to Walazo (4/14/2006) 
	Fixed: !bornon Year function - thanks to Walazo 
	Added: sNick for each index to keep original nick - requested by TT (4/15/2006) 
 
]]-- 
 
-- Edit at will 
Settings = { 
	-- Bot's Name 
	sBot = "??Queen??", 
	-- Script version 
	iVer = "4.04", 
	-- Register in userlist 
	bReg = true, 
	-- Minimum allowed year 
	iMin = 1900, 
	-- Maximum allowed year 
	iMax = 2006, 
	-- Time to check for B-Dayers (in hours) 
	iTime = 4, 
	-- B-Day Man DB 
	fBirth = "tBirthday.tbl", 
} 
 
tBirthday = {} 
 
Main = function() 
	if loadfile(Settings.fBirth) then dofile(Settings.fBirth) end 
	if (Settings.sBot ~= frmHub:GetHubBotName()) and Settings.bReg then frmHub:RegBot(Settings.sBot) end 
	SetTimer(Settings.iTime*60*60*1000)  
	StartTimer() 
end 
 
 
 
OnTimer = function() 
	-- Check B-Days 
	BDayCheck("OnTimer") 
 
end 
 
NewUserConnected = function(user) 
	if user.bUserCommand then 
		for i,v in pairs(tCommands) do 
			if v.tLevels[user.iProfile] then 
				local sRC = string.gsub(v.tRC,"{}",i) 
				user:SendData("$UserCommand 1 3 #F?delsedags Bot\\"..sRC.."|") 
			end 
		end 
	end 
	-- Check B-Days on connect 
	BDayCheck("OnConnect",user) 
end 
 
OpConnected = NewUserConnected 
 
ChatArrival = function(user,data) 
	local s,e,to = string.find(data,"^$To:%s(%S+)%s+From:") 
	local s,e,cmd = string.find(data,"%b<>%s+[%!%+](%S+).*|$")  
	if cmd then 
		if tCommands[string.lower(cmd)] then 
			cmd = string.lower(cmd) 
			if to == Settings.sBot then user.SendMessage = user.SendPM else user.SendMessage = user.SendData end 
			if tCommands[cmd].tLevels[user.iProfile] then 
				return tCommands[cmd].tFunc(user,data), 1 
			else 
				return user:SendMessage(Settings.sBot,"*** Error: Du har inte till?telse att anv?nda det h?r kommandot"), 1 
			end 
		end 
	end 
end 
 
ToArrival = ChatArrival 
 
tCommands = { 
	mybirthday = { 
		tFunc = function(user,data) 
			if tBirthday[string.lower(user.sName)] then 
				local sDate = JulianToDate(tBirthday[string.lower(user.sName)]) 
				user:SendMessage(Settings.sBot,"*** Error: Jag har redan din f?delsedag p? "..sDate.. 
				"! Ifall den inte var r?tt var v?nlig och kontakta en OP f?r hj?lp!") 
			else 
				local s,e,args = string.find(data,"^%b<>%s+%S+%s+(.*)|$") 
				if args then 
					local s,e,d,m,y = string.find(args,"^(%d%d)\/(%d%d)\/(%d%d%d%d)$") 
					if d and m and y then 
						AddBirth(user,user.sName,args,d,m,y) 
					else 
						user:SendMessage(Settings.sBot,"*** Error: F?delsedags syntax m?ste vara - dd/mm/yyyy") 
					end 
				else 
					user:SendMessage(Settings.sBot,"*** Syntax Error: Skriv !mybirthday dd/mm/yyyy") 
				end 
			end 
		end, 
		tLevels = { 
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, 
		}, 
		sDesc = "\tL?gger till din f?delsedag", 
		tRC = "L?gg till din f?delsedag$<%[mynick]> !{} %[line:dd/mm/yyyy]" 
	}, 
	addbirthday = { 
		tFunc = function(user,data) 
			local s,e,args = string.find(data,"^%b<>%s+%S+%s+(.*)|$") 
			if args then 
				local s,e,nick,d,m,y = string.find(args,"^(%S+)%s+(%d%d)\/(%d%d)\/(%d%d%d%d)$") 
				if d and m and y and nick then 
					if tBirthday[string.lower(nick)] then 
						local sDate = JulianToDate(tBirthday[string.lower(nick)]) 
						user:SendMessage(Settings.sBot,"*** Error: Jag har redan "..nick.."'s f?desledag p? "..sDate.. 
						"! Ifall den inte st?mmer var v?nlig och kontakta en OP f?r hj?lp!") 
					else 
						AddBirth(user,nick,args,d,m,y) 
					end 
				else 
					user:SendMessage(Settings.sBot,"*** Error: F?delsedags syntax m?ste vara: <nick> dd/mm/yyyy") 
				end 
			else 
				user:SendMessage(Settings.sBot,"*** Syntax Error: Skriv !addbirthday <nick> dd/mm/yyyy") 
			end 
		end, 
		tLevels = { 
			[0] = 1, [1] = 1, [4] = 1, [5] = 1, 
		}, 
		sDesc = "\tL?gg till en annans anv?ndares f?delsedag", 
		tRC = "L?gg till en annans anv?ndares f?delsedag$<%[mynick]> !{} %[line:Nick] %[line:dd/mm/yyyy]" 
	}, 
	delbirthday = { 
		tFunc = function(user,data) 
			local s,e,nick = string.find(data,"^%b<>%s+%S+%s+(%S+).*|$") 
			if nick then 
				if tBirthday[string.lower(nick)] then 
					tBirthday[string.lower(nick)] = nil; SaveToFile(Settings.fBirth,tBirthday,"tBirthday") 
					user:SendMessage(Settings.sBot,"*** Success: "..nick.."'s f?delsedag ?r borttagen fr?n databasen!") 
				else 
					user:SendMessage(Settings.sBot,"*** Error: "..nick.."'s f?delsedag finns inte med i databasen ?n, eller d?r ?r en Syntax Error: Skriv !delbirthday <nick>") 
				end 
			else 
				user:SendMessage(Settings.sBot,"*** Syntax Error: Skriv !delbirthday <nick>") 
			end 
		end, 
		tLevels = { 
			[0] = 1, [5] = 1, 
		}, 
		sDesc = "\tTa bort en specifik f?delsedag", 
		tRC = "Ta bort anv?ndares f?delsedag$<%[mynick]> !{} %[line:Nick]" 
	}, 
	birthdays = { 
		tFunc = function(user) 
			if next(tBirthday) then 
				local sMsg, n = "\r\n\t"..string.rep("=",105).."\r\n\tNr.\tStatus:\t\tStj?rntecken:\tVeckodag:".. 
				"\tF?delsedag:\t?lder:\tNamn:\r\n\t"..string.rep("-",210).."\r\n", 0 
				for i,v in pairsByKeys(tBirthday) do 
					local sStatus = "*Offline*" 
					local iAge = JulianToTimeE(v)
					n = n + 1; if GetItemByName(i) then sStatus= "*Online*" end;  
					local sDate, sWDay, sZodiac = JulianToDate(v) 
					sMsg = sMsg.."\t"..n..".\t"..sStatus.."\t\t"..sZodiac.."\t"..sWDay.."\t\t"..sDate.."\t"..iAge.."\t"..v.sNick.."\r\n" 
				end 
				user:SendPM(Settings.sBot,sMsg) 
			else 
				user:SendMessage(Settings.sBot,"*** Error: Det finns inga sparade f?delsedagar!") 
			end 
		end, 
		tLevels = { 
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, 
		}, 
		sDesc = "\t\tVisar alla f?delsedagar", 
		tRC = "Visa alla f?delsedagar$<%[mynick]> !{}" 
	}, 
	myage = { 
		tFunc = function(user) 
			if tBirthday[string.lower(user.sName)] then 
				user:SendMessage(Settings.sBot,"*** Du ?r "..JulianToTime(user).."!") 
			else 
				user:SendMessage(Settings.sBot,"*** Error: Var v?nlig och l?gg till din f?delsedag innan du anv?nder det h?r kommandot. Skriv !".. 
				"bhelp f?r mer information!") 
			end 
		end, 
		tLevels = { 
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, 
		}, 
		sDesc = "\t\tVisar din exakta ?lder", 
		tRC = "Visa din ?lder$<%[mynick]> !{}" 
	}, 
	bhelp = { 
		tFunc = function(user) 
			local sMsg = "\r\n\t\t"..string.rep("-", 190).."\r\n"..string.rep("\t",5).."BirthdayMain v.".. 
			Settings.iVer.." by jiten; based on: Herodes'\t\t\t\r\n\t\t"..string.rep("-",190).."\r\n\t\tTillg?ngliga kommandon".."\r\n\r\n" 
			for i,v in pairs(tCommands) do 
				if v.tLevels[user.iProfile] then 
					sMsg = sMsg.."\t\t!"..i.."\t\t"..v.sDesc.."\r\n" 
				end 
			end 
			user:SendMessage(Settings.sBot, sMsg.."\t\t"..string.rep("-",190)); 
		end, 
		tLevels = { 
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, 
		}, 
		sDesc = "\t\tVisar det h?r hj?lp meddelandet", 
		tRC = "Visa dina kommandon$<%[mynick]> !{}" 
	}, 
	bornon = { 
		tFunc = function(user) 
			if tBirthday[string.lower(user.sName)] then 
				local sMsg, tSame, tUser = "", {}, tBirthday[string.lower(user.sName)] 
				local tTable = {  
					["%m"] = { sDesc = "M?nad" }, ["%d"] = { sDesc = "Dag" },  
					["%y"] = { sDesc = "?r", bExtend = true } 
				} 
				for i,v in pairs(tBirthday) do 
					if i ~= string.lower(user.sName) then 
						for a,b in pairs(tTable) do 
							local T,L = os.date("*t",v.iJulian), os.date("*t",tUser.iJulian) 
							local tFunction = function() 
								if tSame[b.sDesc] then tSame[b.sDesc] = tSame[b.sDesc].."; "..v.sNick else tSame[b.sDesc] = v.sNick end 
							end 
							if b.bExtend then 
								if T.year - v.iAdjust == L.year - tUser.iAdjust then tFunction() end 
							else 
								if os.date(a,v.iJulian) == os.date(a,tUser.iJulian) then tFunction() end 
							end 
						end 
					end 
				end 
				for i,v in pairs(tSame) do sMsg = sMsg.."\t"..i.."\t"..v.."\r\n" end; 
				if sMsg ~= "" then 
					user:SendPM(Settings.sBot,"\r\n\t"..string.rep("=",105).. 
					"\r\n\t\t\t\t\t\tHubbkompisar f?dda med samma \"uppgifter\" som du:\r\n\tUppgifter:".. 
					"\tNick:\r\n\t"..string.rep("-",210).."\r\n"..sMsg)  
				else 
					user:SendMessage(Settings.sBot,"*** Error: Det finns inga gemensamma f?delesedagsuppgifter!")  
				end 
			else  
				user:SendMessage(Settings.sBot,"*** Error: Var v?nlig och l?gg till din f?delsedag innan du anv?nder det h?r kommandot. Skriv !".. 
				"bhelp f?r mer information!") 
			end 
		end, 
		tLevels = { 
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, 
		}, 
		sDesc = "\t\tVisa lista med anv?ndare som delar samma f?delsedag som dig", 
		tRC = "Visa lista med anv?ndare som delar dina uppgifter$<%[mynick]> !{}" 
	}, 
} 
 
AddBirth = function(user,nick,args,d,m,y)
	if tonumber(y) < Settings.iMax then  
		if tonumber(y) > Settings.iMin then 
			local tmp 
			if tonumber(y) < 1970 then tmp = 1970 - y; y = 1970 else tmp = 0 end 
			local tTable = { day = tonumber(d), month = tonumber(m), year = tonumber(y) } 
			tBirthday[string.lower(nick)] = { sNick = nick, iJulian = os.time(tTable), iAdjust = tmp } 
			SaveToFile(Settings.fBirth,tBirthday,"tBirthday") 
			local sDate, sWDay, sZodiac = JulianToDate(tBirthday[string.lower(nick)]) 
			user:SendMessage(Settings.sBot,"*** "..nick.."'s f?delsedag ?r p? "..sDate.."! Jag visste inte ".. 
			"att "..nick.." var f?dd i "..Zodiac(tBirthday[string.lower(nick)].iJulian).."'s stj?rntecken!") 
			user:SendMessage(Settings.sBot,"*** Ny f?delsedag till lagd av "..user.sName.." p?: "..sDate); 
		else 
			user:SendMessage(Settings.sBot,"*** Error: Minsta till?tna ?r ?r "..Settings.iMin.."!") 
		end
	else 
		user:SendMessage(Settings.sBot,"*** Error: St?rsta till?tna ?r ?r "..Settings.iMax.."!") 
	end  
end 
 
BDayCheck = function(Mode,user) 
	-- Check B-Days on connect 
	for i,v in pairs(tBirthday) do 
		if os.date("%m%d",v.iJulian) == (os.date("%m%d")) then 
			local tTable = { 
 				["OnConnect"] = function() 
					if i ~= string.lower(user.sName) then 
						local T = os.date("*t",os.difftime(os.time(os.date("*t")),os.time(os.date("*t",v.iJulian)))) 
						user:SendData(Settings.sBot,"*** Det ?r "..v.sNick.."'s F?delsedag idag! :D Han/Hon fyller ".. 
						(T.year-1970+v.iAdjust).." idag! S? skicka en lycko?nskning :)") 
					else 
						user:SendData(Settings.sBot,"**** Hey, Jag vet! Du har din f?delsedag IDAG! Gladaste av f?delsedagar!") 
						SendToAll(Settings.sBot, "*** Hubbers!!! "..user.sName.." ?r h?r! Vad s?ger vi? :)") 
					end 
				end, 
				["OnTimer"] = function() 
					local tNow = {} 
					if GetItemByName(i) then 
						local user = GetItemByName(i) 
						local T = os.date("*t",os.difftime(os.time(os.date("*t")),os.time(os.date("*t",v.iJulian)))) 
						local iAge = (T.year - 1970 + v.iAdjust) 
 						local tSurprise = { 
							user.sName.." kommer att ha en FEST idag! Han/Hon fyller ".. 
							iAge.."! Grattis!!!", 
							"Alla spammar "..user.sName.." med f?delsedagspm ;) ... Han/Hon fyller ".. 
							iAge.." idag!!!", 
							"Vem fyller "..iAge.." idag? :D Dagen och natten tillh?r ".. 
							user.sName.."!", 
							"Grattis p? f?delsedagen, k?ra "..user.sName.. 
							", vi alla ?nskar att "..iAge..":de f?delsedagen ska bli b?ttre ?n ".. 
							(iAge-1)..":de! :)", 
							"Jag tror "..user.sName.." har sin f?delsedag idag. Han/Hon borde ".. 
							"fylla "..iAge.." idag ;D", 
							"En "..Zodiac(v.iJulian).." fyller "..iAge 
							.." idag! Det ?r "..user.sName.."'s f?delsedag!!!" 
						} 
						SendToAll(Settings.sBot,tSurprise[math.random(1,table.getn(tSurprise))]) 
					end 
				end, 
			} 
			if tTable[Mode] then 
					 tTable[Mode]()  
			end 
		end 
	end 
end 
 
JulianToDate = function(v) 
	local iYear = (os.date("%Y",v.iJulian) - v.iAdjust) 
	local sDate = os.date("%b %d, "..iYear,v.iJulian) 
	local sWDay, sZodiac = os.date("%a",v.iJulian), Zodiac(v.iJulian) 
	return sDate, sWDay, sZodiac 
end 
 
Zodiac = function(iJulian) 
	local tZodiac = { 
		[01] = { 20, "Stenbocken  ", "Vattumannen " }, [02] = { 19, "Vattumannen ", "Fiskarna    " }, 
		[03] = { 21, "Fiskarna    ",  "V?duren    " }, [04] = { 20, "V?duren     ", "Oxen        " },  
		[05] = { 21, "Oxen        ", "Tvillingarna" }, [06] = { 21, "Tvillingarna", "Kr?ftan     " }, 
		[07] = { 23, "Kr?ftan     ", "Lejonet     " }, [08] = { 23, "Lejonet     ", "Jungfrun    " }, 
		[09] = { 23, "Jungfrun    ", "V?gen       " }, [10] = { 23, "V?gen       ", "Skorpionen  " }, 
		[11] = { 22, "Skorpionen  ", "Skytten     " }, [12] = { 22, "Skytten     ", "Stenbocken  "}, 
	} 
	local tTmp = tonumber(os.date("%m",iJulian)) 
	if tZodiac[tTmp][1] > tonumber(os.date("%d",iJulian)) then return tZodiac[tTmp][2] else  return tZodiac[tTmp][3] end 
end 
 
JulianToTime = function(user) 
	local iDiff = os.difftime(os.time(os.date("*t")),os.time(os.date("*t",tBirthday[string.lower(user.sName)].iJulian))) 
	if iDiff > 0 then 
		local T = os.date("*t",iDiff) 
		return string.format("%i ?r, %i m?nader och %i dagar", (T.year-1970+tBirthday[string.lower(user.sName)].iAdjust), (T.month-1), (T.day-1), T.hour, T.min, T.sec) 
	end 
end

JulianToTimeE = function(v) 
	local iDiff = os.difftime(os.time(os.date("*t")),os.time(os.date("*t",v.iJulian))) 
	if iDiff > 0 then 
		local T = os.date("*t",iDiff) 
		return (T.year-1970+v.iAdjust)
	else
		return "err." 
	end 
end  
 
Serialize = function(tTable,sTableName,hFile,sTab) 
	sTab = sTab or ""; 
	hFile:write(sTab..sTableName.." = {\n"); 
	for key,value in pairs(tTable) do 
		if (type(value) ~= "function") then 
			local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key); 
			if(type(value) == "table") then 
				Serialize(value,sKey,hFile,sTab.."\t"); 
			else 
				local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value); 
				hFile:write(sTab.."\t"..sKey.." = "..sValue); 
			end 
			hFile:write(",\n"); 
		end 
	end 
	hFile:write(sTab.."}"); 
end 
 
SaveToFile = function(file,table,tablename) 
	local hFile = io.open(file,"w+") Serialize(table,tablename,hFile); hFile:close()  
end 

function pairsByKeys (t, f)
  local Ttab = {}
  for n in pairs(t) do 
  	table.insert(Ttab, n)
  	--table.insert(Ttab,{i,f.sNick,f.iJulian,f.iAdjust})
  end
  table.sort(Ttab, f)
  --table.sort(Ttab,function(a,b) return (a.iJulian > b.iJulian) end)
  local i = 0      -- iterator variable
  local iter = function ()   -- iterator function
    i = i + 1
    if Ttab[i] == nil then return nil
    else return Ttab[i], t[Ttab[i]]
    end
  end
  return iter
end

--- http://lua-users.org/wiki/OrderedTable
--- http://forum.ptokax.org/index.php?topic=5023.0

atonal

#1
--[[ 

	Birthday Man v.4.08 - LUA 5.0/5.1 by jiten (11/29/2007)
	?????????????????????????????????????????????????????
	Based on: BirthdayMan v 3.5 by Herodes and mods by C??o?y??

	CHANGELOG:
	??????????
	Rewritten: Whole code;

	Changed: Command structure to tables (Function, Permissions, RightClick and Description);
	Changed: B-Day table structure to Julian values;
	Removed: File content that was sent on B-Day;
	Removed: Allowed Age;
	Removed: Topic updater (maybe in the future);
	Changed: Whole B-Day counting system to os.*;
	Changed: Commands returned according to input (Main or PM);
	Changed: Many other mods that can't recall at the moment (4/12/2006);
	Added: addbirthday command - requested by TT (4/13/2006);
	Fixed: Zodiac Sign Dates - thanks to Walazo;
	Changed: Error message on empty !bornon result - thanks to Walazo;
	Changed: !birthdays' content structure to fix bad tabbing;
	Changed: Settings.iVer;
	Added: string.lower checks - thanks to Walazo (4/14/2006)
	Fixed: !bornon Year function - thanks to Walazo
	Added: sNick to keep original nick - requested by TT (4/15/2006);
	Changed: *t to !*t;
	Fixed: Birthday is at midnight - reported by TT (6/25/2006)
	Added: Send Text File content - requested by TT (7/6/2006)
	Added: Hub Topic updater - requested by UwV (7/11/2006)
	Added: Celebrity's feature - requested by UwV (7/15/2006)
	Fixed: BDayCheck function - reported by TT (7/16/2006)
	Fixed: Added correct tabbing for messy !birthdays - reported by [NL]_Yvisaur and UwV;
	Changed: !birthdays is sorted from Jan to Dez - requested by UwV;
	Added: Details and 'All' field to !bornon command - requested by UwV;
	Added: Common fields to !celeb command - requested by UwV (7/24/2006)
	Added: %ptodaybirthdays - requested by Yahoo (11/28/2007)
	
	Script Converted to New API by Hungarista's Convertor and a few Mods by speedX

]]--

-- Edit at will
Settings = {
	-- Bot's Name
	sBot = "!!!Tu.Cumple.A?os!!!",
	-- Script version
	iVer = "4.08",
	-- Register in userlist
	bReg = true,

	-- Send File Content (true = on, false = off)
	bSendText = true,
	-- Birthday Text File
	fText = "cake.txt",

	-- Update hub topic (true = on, false = off)
	bUpdateTopic = true,
	-- Use Celebrity's command (true = on, false = off)
	bCelebrity = true,

	-- Minimum allowed year
	iMin = 1900,
	-- Time to check for B-Dayers (in hours)
	iTime = 12,
	-- B-Day Man DB
	fBirth = "tBirthday.tbl",
	-- Celebrity's DB
	fCelebrity = "tCelebrity.tbl"
}

tBirthday = {}

OnStartup = function()
	if loadfile(Settings.fBirth) then dofile(Settings.fBirth) end
	-- garbagecollect method (Based on Mutor's)
	gc = nil; if _VERSION == "Lua 5.1" then gc = "collect" end
	if (Settings.sBot ~= SetMan.GetString(21)) or Settings.bReg then Core.RegBot(Settings.sBot,"","",true) end

end

OnTimer = function(tmr)
	-- Check B-Days
	tFunctions.BDayCheck("OnTimer")
	-- Collect garbage
	collectgarbage(gc); io.flush()
end

UserConnected = function(user)
	-- Supports UserCommands
	if Core.GetUserValue(user, 12) then
		for i, v in pairs(tCommands) do
			if v.tLevels[user.iProfile] then
				local sRC = string.gsub(v.tRC, "{}", i)
				Core.SendToNick(user.sNick,"$UserCommand 1 3 Escrive-Tu-Cumple-a?os\\"..sRC.."&#124;")
			end
		end
	end
	-- Check B-Days on connect
	tFunctions.BDayCheck("OnConnect", user)
end

OpConnected = UserConnected

ChatArrival = function(user,data)
	local _,_, to = string.find(data, "^$To:%s(%S+)%s+From:")
	local _,_, cmd = string.find(data, "%b<>%s+[%!%+](%S+).*|$") 
	if cmd then
		if tCommands[string.lower(cmd)] then
			cmd = string.lower(cmd)
			if tCommands[cmd].tLevels[user.iProfile] then
				return tCommands[cmd].tFunc(user, data), 1
			else
				return Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: You are not allowed to use this command!"), 1
			end
		end
	end
end

ToArrival = ChatArrival

tCommands = {
	mybirthday = {
		tFunc = function(user, data)
			if tBirthday[string.lower(user.sNick)] then
				local sDate = tFunctions.JulianToDate(tBirthday[string.lower(user.sNick)])
				Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: I already have your Birthday on "..sDate..
				"! If it wasn't correctly set, ask Operators for help!")
			else
				local _,_, args = string.find(data, "^%b<>%s+%S+%s+(.*)|$")
				if args then
					local _,_, d, m, y = string.find(args, "^(%d%d)\/(%d%d)\/(%d%d%d%d)$")
					if d and m and y then
						tFunctions.AddBirth(user, user.sNick, args, d, m, y)
					else
						Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: Birthday syntax must be - dd/mm/yyyy")
					end
				else
					Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Syntax Error: Type !mybirthday dd/mm/yyyy")
				end
			end
		end,
		tLevels = {
			[-1] = 0, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
		},
		sDesc = "\tAdds your B-Day",
		tRC = "Add your B-Day$<%[mynick]> !{} %[line:dd/mm/yyyy]"
	},
	addbirthday = {
		tFunc = function(user, data)
			local _,_, args = string.find(data, "^%b<>%s+%S+%s+(.*)|$")
			if args then
				local _,_, nick,d,m,y = string.find(args, "^(%S+)%s+(%d%d)\/(%d%d)\/(%d%d%d%d)$")
				if d and m and y and nick then
					if tBirthday[string.lower(nick)] then
						local sDate = tFunctions.JulianToDate(tBirthday[string.lower(nick)])
						Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: I already have "..nick.."'s Birthday on "..sDate..
						"! If it wasn't correctly set, ask Operators for help!")
					else
						tFunctions.AddBirth(user, nick, args, d, m, y)
					end
				else
					Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: Birthday syntax must be: <nick> dd/mm/yyyy")
				end
			else
				Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Syntax Error: Type !addbirthday <nick> dd/mm/yyyy")
			end
		end,
		tLevels = {
			[0] = 0, [1] = 1, [4] = 1, [5] = 1,
		},
		sDesc = "\tAdds another user's B-Day",
		tRC = "Add another user's B-Day$<%[mynick]> !{} %[line:Nick] %[line:dd/mm/yyyy]"
	},
	delbirthday = {
		tFunc = function(user, data)
			local _,_, nick = string.find(data, "^%b<>%s+%S+%s+(%S+).*|$")
			if nick then
				if tBirthday[string.lower(nick)] then
					tBirthday[string.lower(nick)] = nil; tFunctions.SaveToFile(Settings.fBirth, tBirthday, "tBirthday")
					Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Success: "..nick.."'s Birthday was successfully deleted from the DB!")
				else
					Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: "..nick.."'s Birthday hasn't been added to the DB yet; or there's a Syntax Error: Type !delbirthday <nick>")
				end
			else
				Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Syntax Error: Type !delbirthday <nick>")
			end
		end,
		tLevels = {
			[0] = 1, [5] = 1,
		},
		sDesc = "\tDelete a specific B-Day",
		tRC = "Delete user's B-Day$<%[mynick]> !{} %[line:Nick]"
	},
	birthdays = {
		tFunc = function(user)
			tFunctions.Birthdays(user)
		end,
		tLevels = {
			[-1] = 0, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
		},
		sDesc = "\t\tShows all Birthdays",
		tRC = "Show all B-Days$<%[mynick]> !{}"
	},
	todaybirthdays = {
		tFunc = function(user)
			tFunctions.Birthdays(user, true)
		end,
		tLevels = {
			[-1] = 0, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
		},
		sDesc = "\t\tDisplays today's birthdays",
		tRC = "Show today's birthdays$<%[mynick]> !{}"
	},
	myage = {
		tFunc = function(user)
			if tBirthday[string.lower(user.sNick)] then
				Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** You're "..tFunctions.JulianToTime(user).." old according to the Hub's clock!")
			else
				Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: Please add your Birthday before using this command. Type !"..
				"bhelp for more details!")
			end
		end,
		tLevels = {
			[-1] = 0, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
		},
		sDesc = "\t\tDisplays your exact age",
		tRC = "Show your age$<%[mynick]> !{}"
	},
	bhelp = {
		tFunc = function(user)
			local sMsg = "\r\n\t\t"..string.rep("-", 190).."\r\n"..string.rep("\t", 5).."BirthdayMan v."..
			Settings.iVer.." by jiten; based on: Herodes'\t\t\t\r\n\t\t"..string.rep("-", 190).."\r\n\t\tAvailable Commands:".."\r\n\r\n"
			for i, v in pairs(tCommands) do
				if v.tLevels[user.iProfile] then
					sMsg = sMsg.."\t\t!"..i.."\t\t"..v.sDesc.."\r\n"
				end
			end
			Core.SendToNick(user.sNick,"<"..Settings.sBot.."> "..sMsg.."\t\t"..string.rep("-", 190));
		end,
		tLevels = {
			[-1] = 0, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
		},
		sDesc = "\t\tDisplays this help message",
		tRC = "Show command list$<%[mynick]> !{}"
	},
	bornon = {
		tFunc = function(user)
			tFunctions.Common(user, tBirthday, "Users")
		end,
		tLevels = {
			[-1] = 0, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
		},
		sDesc = "\t\tShows users with whom you share your Birthday",
		tRC = "Show list of users you share dates$<%[mynick]> !{}"
	},
	celeb = {
		tFunc = function(user)
			if Settings.bCelebrity then
				if loadfile(Settings.fCelebrity) then dofile(Settings.fCelebrity) end
				tFunctions.Common(user, tCelebrity, "Celebrities")
			else
				Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: Celebrity's command is disabled!")
			end
		end,
		tLevels = {
			[-1] = 0, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
		},
		sDesc = "\t\tShows a list of celebrities that share your birthday",
		tRC = "Shows celebrities that share your birthday$<%[mynick]> !{}"
	}
}

tFunctions = {}
tFunctions.AddBirth = function(user, nick, args, d, m, y)
	if tonumber(y) > Settings.iMin then
		local tmp
		if tonumber(y) < 1970 then tmp = 1970 - y; y = 1970 else tmp = 0 end
		local tTable = { day = tonumber(d), month = tonumber(m), year = tonumber(y), hour = 0, min = 0, sec = 0 }
		tBirthday[string.lower(nick)] = { sNick = nick, iJulian = os.time(tTable), iAdjust = tmp }
		tFunctions.SaveToFile(Settings.fBirth, tBirthday, "tBirthday")
		local sDate, sWDay, sZodiac = tFunctions.JulianToDate(tBirthday[string.lower(nick)])
		Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** "..nick.."'s Birthday is on "..sDate.."! I didn't "..
		"know "..nick.." was a "..tFunctions.Zodiac(tBirthday[string.lower(nick)].iJulian).."!")
		Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** New Birthday added by "..user.sNick.." on: "..sDate);
	else
		Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: The miminum allowed year is "..Settings.iMin.."!")
	end
end

tFunctions.Birthdays = function(user, bFilter)
	if next(tBirthday) then
		local sMsg, n, tSort = "\r\n\t"..string.rep("=", 105).."\r\n\tNr.\tStatus:\t\tZodiac Sign:\tWeekday:"..
		"\tBirthdate:\t\t\tName:\r\n\t"..string.rep("-", 210).."\r\n", 0, {}
		for i, v in pairs(tBirthday) do
			table.insert(tSort, { sNick = v.sNick, iJulian = v.iJulian, iAdjust = v.iAdjust } )
		end
		table.sort(tSort, function(a, b) return (os.date("%m%d", a.iJulian) < os.date("%m%d", b.iJulian)) end)
		for i, v in ipairs(tSort) do
			local tContent = function()
				local sStatus, sTab = "*Offline*", "\t"
				n = n + 1; if Core.GetUser(v.sNick) then sStatus= "*Online*" end; 
				local sDate, sWDay, sZodiac = tFunctions.JulianToDate(v)
				if string.len(sZodiac) <= 9 then sTab = "\t\t" end
				sMsg = sMsg.."\t"..n..".\t"..sStatus.."\t\t"..sZodiac..sTab..sWDay.."\t\t"..sDate..
				"\t\t"..v.sNick.."\r\n"
			end
			if bFilter then if os.date("%m%d", v.iJulian) == (os.date("%m%d")) then	tContent() end else tContent() end
		end
		Core.SendToNick(user.sNick,"<"..Settings.sBot.."> "..sMsg)
	else
		Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: There are no saved Birthdays!")
	end
end

tFunctions.Common = function(user, tTempTable, sType)
	if tBirthday[string.lower(user.sNick)] then
		local sMsg, tSame, tUser = "", {}, tBirthday[string.lower(user.sNick)]
		local tTable = { 
			["%m"] = { sDesc = "Month" }, ["%d"] = { sDesc = "Day" }, 
			["%y"] = { sDesc = "Year", bExtend = true }, ["%d%m%y"] = { sDesc = "All", bAll = true },
		}
		for i, v in pairs(tTempTable) do
			if i ~= string.lower(user.sNick) then
				for a, b in pairs(tTable) do
					local T, L = os.date("!*t", v.iJulian), os.date("!*t", tUser.iJulian)
					local tFunction = function()
						if tSame[b.sDesc] then tSame[b.sDesc] = tSame[b.sDesc].."; "..v.sNick else tSame[b.sDesc] = v.sNick end
					end
					if b.bExtend then
						if T.year - v.iAdjust == L.year - tUser.iAdjust then tFunction() end
					else
						if b.bAll then
							if (T.year - v.iAdjust == L.year - tUser.iAdjust) and (os.date("%d%m", v.iJulian) == os.date("%d%m", tUser.iJulian)) then
								tFunction()
							end
						else
							if os.date(a, v.iJulian) == os.date(a, tUser.iJulian) then tFunction() end
						end
					end
				end
			end
		end
		for i, v in pairs(tSame) do sMsg = sMsg.."\t"..i.."\t"..v.."\r\n" end;
		if sMsg ~= "" then
			Core.SendToNick(user.sNick,"<"..Settings.sBot.."> \r\n\t"..string.rep("=", 105)..
			"\r\n\t\t\t\t\t"..sType.." born in the same \"Field\" as yours [ "..
			tFunctions.JulianToDate(tBirthday[string.lower(user.sNick)])..
			" ] :\r\n\tField:".."\tNick:\r\n\t"..string.rep("-", 210).."\r\n"..sMsg) 
		else
			Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: There are no common Birthday fields!") 
		end
	else 
		Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** Error: Please add your Birthday before using this command. Type !"..
		"bhelp for more details!")
	end
end

tFunctions.BDayCheck = function(Mode, nick)
	local sTopic = nil
	-- Check B-Days
	for i, v in pairs(tBirthday) do
		if os.date("%m%d", v.iJulian) == (os.date("%m%d")) then
			if sTopic then sTopic = sTopic.."; "..v.sNick else sTopic = v.sNick end
			if sTopic and tFunctions[Mode] then tFunctions[Mode](nick, i, v, sTopic) end
		end
	end
end

tFunctions.OnConnect = function(user, i, v)
	if i ~= string.lower(user.sNick) then
		local T = os.date("!*t", os.difftime(os.time(os.date("!*t")), os.time(os.date("!*t", v.iJulian))))
		Core.SendToNick(user.sNick,"<"..Settings.sBot.."> *** It's "..v.sNick.."'s Birthday today! :D He/She is turning "..
		(T.year-1970+v.iAdjust).." today! Give a wish :)")
	else
		Core.SendToNick(user.sNick,"<"..Settings.sBot.."> **** Hey, I know! You have your Birthday TODAY! Happiest of Birthdays!")
		Core.SendToAll("<"..Settings.sBot.."> *** Guys!!! "..user.sNick.." is here! What do we say? :)")
	end
end

tFunctions.OnTimer = function(user, i, v, sTopic)
	if Core.GetUser(i,true) then
		-- Send File Content
		if Settings.bSendText then
			local sMsg = ""
			local f = io.open(Settings.fText)
			if f then sMsg = f:read("*all"); f:close() end
			Core.SendToAll("<"..Settings.sBot.."> \r\n\r\n"..sMsg)
		end
		-- Send Message
		local user = Core.GetUser(i,true)
		local T = os.date("!*t", os.difftime(os.time(os.date("!*t")), os.time(os.date("!*t", v.iJulian))))
		local iAge = (T.year - 1970 + v.iAdjust)
		local tSurprise = {
			user.sNick.." is gonna have a PAAARTY today! He/She is turning "..
			iAge.."! Happy Birthday!!!",
			"All of you: Spam "..user.sNick.." with Birthday messages ;) ... turning "..
			iAge.." today!!!",
			"Who's turning "..iAge.." today? :D The day AND the night belongs to "..
			user.sNick.."!",
			"Happy Birthday to you, Happy Birthday dear "..user.sNick..
			", we all wish you that "..iAge.." will be better than your "..
			(iAge-1).."! :)",
			"I think Mr "..user.sNick.." has his/her birthday today. He/She should be "..
			"turning "..iAge.." today ;D",
			"A "..tFunctions.Zodiac(v.iJulian).." is turning "..iAge
			.." today! It's "..user.sNick.."'s birthday!!!"
		}
		Core.SendToAll("<"..Settings.sBot.."> ".. tSurprise[math.random(1, #tSurprise)])
	end
	-- Update Topic
	if Settings.bUpdateTopic then
		local _,_, sHubTopic = string.find((SetMan.GetString(10) or ""), "(.-)%s-%sToday's.*")
		SetMan.SetString(10,(sHubTopic or "").."Today's birthday(s): "..sTopic)
	end
end

tFunctions.JulianToDate = function(v)
	local iYear = (os.date("%Y", v.iJulian) - v.iAdjust)
	local sDate = os.date("%b %d, "..iYear, v.iJulian)
	local sWDay, sZodiac = os.date("%a", v.iJulian), tFunctions.Zodiac(v.iJulian)
	return sDate, sWDay, sZodiac
end

tFunctions.Zodiac = function(iJulian)
	local tZodiac = {
		[01] = { 20, "Capricorn", "Aquarius" }, [02] = { 19, "Aquarius", "Pisces" },
		[03] = { 21, "Pisces",  "Aries" }, [04] = { 20, "Aries", "Taurus" }, 
		[05] = { 21, "Taurus", "Gemini" }, [06] = { 21, "Gemini", "Cancer" },
		[07] = { 23, "Cancer", "Leo" }, [08] = { 23, "Leo", "Virgo" },
		[09] = { 23, "Virgo", "Libra" }, [10] = { 23, "Libra", "Scorpio" },
		[11] = { 22, "Scorpio", "Sagittarius" }, [12] = { 22, "Sagittarius", "Capricorn"},
	}
	local tTmp = tonumber(os.date("%m", iJulian))
	if tZodiac[tTmp][1] > tonumber(os.date("%d", iJulian)) then return tZodiac[tTmp][2] else  return tZodiac[tTmp][3] end
end

tFunctions.JulianToTime = function(user)
	local iDiff = os.difftime(os.time(os.date("!*t")), os.time(os.date("!*t", tBirthday[string.lower(user.sNick)].iJulian)))
	if iDiff > 0 then
		local T = os.date("!*t", iDiff)
		return string.format("%i year(s), %i month(s) and %i day(s)", (T.year-1970+tBirthday[string.lower(user.sNick)].iAdjust), (T.month-1), (T.day-1), T.hour, T.min, T.sec)
	end
end

tFunctions.Serialize = function(tTable, sTableName, hFile, sTab)
	sTab = sTab or "";
	hFile:write(sTab..sTableName.." = {\n");
	for key, value in pairs(tTable) do
		if (type(value) ~= "function") then
			local sKey = (type(key) == "string") and string.format("[%q]", key) or string.format("[%d]", key);
			if(type(value) == "table") then
				tFunctions.Serialize(value, sKey, hFile, sTab.."\t");
			else
				local sValue = (type(value) == "string") and string.format("%q", value) or tostring(value);
				hFile:write(sTab.."\t"..sKey.." = "..sValue);
			end
			hFile:write(",\n");
		end
	end
	hFile:write(sTab.."}");
end

tFunctions.SaveToFile = function(file, table, tablename)
	local hFile = io.open(file, "w+") tFunctions.Serialize(table, tablename, hFile); hFile:close() 
end
RegConnected = UserConnected

SMF spam blocked by CleanTalk