Help needed to convert Guest Welcome 1.0b LUA 5.0/5.1 by Mutor
 

Help needed to convert Guest Welcome 1.0b LUA 5.0/5.1 by Mutor

Started by miago, 21 April, 2008, 12:25:52

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

miago

Yes, me again.
This is for new users who connect to the hub, they get a welcomgreeting.
Thx

--[[
	Guest Welcome 1.0b LUA 5.0/5.1

	By Mutor		01/11/07

	Requested by speedX

	Welcomes new users to the hub for a specified number of days
	-Option for scripted message or message file

	Changes from 1.0	02/03/07
		+Added option for day/hour/min. Request by speedX
		+Added option for main chat message. Request by miago

	1.0b Notes:
		-Dump your old Cfg.File before running.

]]

Cfg = {
-- Botname pulled from the hub or use "CustomName"
Bot = frmHub:GetHubBotName(),
-- Bot description
Desc = "I send a message to new users.",
-- Bot email address
Mail = "user@domain.com",
-- Admins nick for status / error messages
OpNick = "-Admin",
-- File to save user data to
File= "Guests.dat",
-- MainMsg & Msg var notes: usr shall be replaced with user's name, prof with user's profile
-- Message sent to all in main chat at user login ["" = none/disabled]
MainMsg = "      " ..frmHub:GetHubName().."   asks you all to welcome prof usr to the hub!!  :D",
-- Welcome message, 
Msg = "Welcome to    "..frmHub:GetHubName()..".    We're glad you could join us prof usr. Enjoy your stay! :D",
-- Send this file [path/file] instead of message above ["" = none/use Msg above]
MessageFile = "welcome_user.txt",
-- Increment of time to use for welcome message ["days"/"hours"/"minutes"]
Increment = "hours",
-- Number of 'increments' above new users should receive the welcome message
Count = 4,
}

local Inc = {
	days = ((60^2)* 24),
	hours =(60^2),
	minutes = 60,
	}

Main = function()
	if Cfg.Bot ~= frmHub:GetHubBotName() or frmHub:GetHubBot()== 0 then
		frmHub:RegBot(Cfg.Bot, 1, Cfg.Desc.." PtokaX ".._VERSION, Cfg.Mail)
	end
	local LuaVer = string.sub(_VERSION,1,7)
	if LuaVer == "Lua 5.1" then
		Cfg.Gc,Cfg.No,Cfg.Pat,Cfg.MemOp = "collect",table.maxn,string.gmatch,collectgarbage("count")
	elseif LuaVer == "Lua 5.0" then
		Cfg.Gc,Cfg.No,Cfg.Pat,Cfg.MemOp = nil,table.getn,string.gfind,Cfg.Gcinfo()
	else
		return OnError("This script is incompatible with ".._VERSION),1
	end
	if Cfg.MessageFile ~= "" then
		local f,e = io.open(Cfg.MessageFile)
		if f then
			Cfg.Msg = f:read("*a")
			f:close()
		else
			Cfg.Msg = "Welcome to "..frmHub:GetHubName()..
			". We're glad you could join us usr."
			OnError(string.sub(e,1,-2)..". Default message loaded.")
		end
	end
	if Inc[Cfg.Increment] then
		Cfg.Time = Inc[Cfg.Increment] * Cfg.Count
	end
	if loadfile(Cfg.File) then
		dofile(Cfg.File)
		OnError(Cfg.File.." was found and executed. Memory usage: "..CleanMem())
	else
		Cfg.Guests = {}
		SaveToFile(Cfg.File,Cfg.Guests,"Cfg.Guests")
		OnError(Cfg.File.." was not found. This file has been created for you. Memory usage: "..CleanMem())
	end
	OnError("New user welcome period set to: "..Cfg.Count.." "..Cfg.Increment..".")
end

OnError = function(msg)
	SendToNick(Cfg.OpNick,"<"..Cfg.Bot.."> "..msg)
end

NewUserConnected = function(user)
	local Profile = GetProfileName(user.iProfile) or "Unregistered User"
	if not Cfg.Guests[user.sName] then
		Cfg.Guests[user.sName] = os.time()
		SaveToFile(Cfg.File,Cfg.Guests,"Cfg.Guests")
	end
	if Cfg.Guests[user.sName] then
		if Cfg.Guests[user.sName]~= 0 then
			local z = os.time()- Cfg.Guests[user.sName]
			if z < Cfg.Time then
				if Cfg.MainMsg ~= "" then
					local mainmsg = string.gsub(Cfg.MainMsg,"usr",user.sName)
					mainmsg = string.gsub(mainmsg,"prof",Profile)
					SendToAll(Cfg.Bot,mainmsg)
				end
				local msg = string.gsub(Cfg.Msg,"usr",user.sName)
				msg = string.gsub(msg,"prof",Profile)
				user:SendPM(Cfg.Bot,msg)
			else
				Cfg.Guests[user.sName] = 0
				SaveToFile(Cfg.File,Cfg.Guests,"Cfg.Guests")
			end
		end
	end
end
OpConnected = NewUserConnected

function Serialize(tTable, sTableName, sTab)
	assert(tTable, "tTable equals nil")
	assert(sTableName, "sTableName equals nil")
	assert(type(tTable) == "table", "tTable must be a table!")
	assert(type(sTableName) == "string", "sTableName must be a string!")
	sTab = sTab or ""
	sTmp = ""
	sTmp = sTmp..sTab..sTableName.." = {\n"
	for key, value in pairs(tTable) do
		local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key)
		if(type(value) == "table") then
			sTmp = sTmp..Serialize(value, sKey, sTab.."\t")
		else
			local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value)
			sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
		end
		sTmp = sTmp..",\n"
	end
	sTmp = sTmp..sTab.."}"
	return sTmp
end

function SaveToFile(file , table , tablename)
	local handle = io.open(file,"wb")
	handle:write(Serialize(table, tablename))
	handle:flush()
	handle:close()
	collectgarbage(Cfg.Gc)
end

CleanMem = function()
	Cfg.Mem = string.format("%-.2f Kb.",Cfg.MemOp)
	return Cfg.Mem
end
Being a biatch aint easy ;)

miago

Your just great Mutor....gonna try them tomorrow as soon as I can...tnx
Humble regards
Miago
Being a biatch aint easy ;)

miago

Hiya.

Have added the welcome_user.txt file to scripts folder, changed the opnick in the lua.
When starting the script it creates the file guests2.dat as its supposed to.
The first person who enters the hub (PCadm in this case), gets the welcomeusertext in pm. The others can thereafter see in mc when this users reconnects with the welcomemsgline from script.
But, it only registers the very first person to enter the hub in guests2.dat, when second, third or fourth new person enters nothing happens and the guests2.dat file doesnt fill up with their nicks.

This is the content of guests2.dat atm after 4 new persons have entered the hub:
Cfg.Guests = {
   ["PCadm"] = 1208846660,
}

Regards
miago
Being a biatch aint easy ;)

miago

Being a biatch aint easy ;)

SMF spam blocked by CleanTalk