PtokaX forum

Lua 5.3/5.2/5.1 Scripts (for PtokaX 0.4.0.0 and newer) => Conversion Requests => Topic started by: DeAn on 09 July, 2009, 19:17:52

Title: Away script
Post by: DeAn on 09 July, 2009, 19:17:52
I would like this script to be converted in API 2.


--[[
Awaybot 1.0 by bastya_elvtars
From LawMaker.
Sends away message.
No tag and other things like this.
You can see others' away messages w/o PMing them if you want.
Enjoy.
]]

-- // CONFIG
------------

-- The bot's data (will show in userlist):

Bot={
      name="AwayBot",
      desc="Away bot.",
      email="awaybot@bastya-editor.click",
    }

--[[
----------------------------------------------------------------------------------------
NO levels. If anyone is allowed to use it, I do not consider it a security risk myself.
Commands must be added without prefix, bot will detect them with prefix.
Syntax:

{"command","help text"},
----------------------------------------------------------------------------------------
]]

cmd={

-- Go away
afk={"afk"," Go away and leave a message (that's optional.)"},

-- Come back
back={"back","Come back if away."},

-- Shows whether you are away
mystatus={"mystatus","Shows whether you are away."},

-- Shows a specific user's or all users' away messages
showaways={"showaways"," Shows away messages of a user, with no option it shows all users'."},

-- guess :-]
awayhelp={"awayhelp","Guess! :-]"},

}
-------------------
-- // END OF CONFIG

afk={}

function away(user,data,env)
  if not afk[user.sName] then
    local _,_,why=string.find(data,"%b<>%s+%S+%s+(.+)")
    if not why then
      SendTxt(user,env,Bot.name,"You have been set to away.")
      SendToAll(Bot.name,"User "..user.sName.." went away.")
      afk[user.sName]=os.date("%A, %c")
      saveaway()
    else
      SendTxt(user,env,Bot.name,"You have been set to away. Reason: "..why)
      SendToAll(Bot.name,"User "..user.sName.." went away, because: \""..why.."\".")
      afk[user.sName]=why.."|"..os.date("%A, %c")
      saveaway()
    end
  else
    SendToAll(Bot.name,"User "..user.sName.." has come back now.")
    local _,_,why=string.find(data,"%b<>%s+%S+%s+(.+)")
    if not why then
      SendTxt(user,env,Bot.name,"You have been set to away.")
      SendToAll(Bot.name,"User "..user.sName.." went away.")
      afk[user.sName]=os.date("%A, %c")
      saveaway()
    else
      SendTxt(user,env,Bot.name,"You have been set to away. Reason: "..why)
      SendToAll(Bot.name,"User "..user.sName.." went away, because: \""..why.."\".")
      afk[user.sName]=why.."|"..os.date("%A, %c")
      saveaway()
    end
  end
end

function returned(user,data,env)
  if afk[user.sName] then
    SendTxt(user,env,Bot.name,"You are no longer away and have been set back to normal.")
    SendToAll(Bot.name,"User "..user.sName.." has came back now.")
    afk[user.sName]=nil
    saveaway()
  else
    SendTxt(user,env,Bot.name,"You weren't away, baby.")
  end
end

function awaymsg(user,whoTo)
  if not string.find(afk[whoTo],"%|") then
    user:SendPM(whoTo,"I went away on "..afk[whoTo]..".")
  else
    local _,_,why,dat=string.find(afk[whoTo],"(.+)%|(.+)")
    user:SendPM(whoTo,"I went away on "..dat..", because: "..why..".")
  end
end

function myawaystatus(user,env)
  if afk[user.sName] then
    if not string.find(afk[user.sName],"%|") then
      SendTxt(user,env,Bot.name,"You went away on "..afk[user.sName]..".")
    else
      local _,_,why,dat=string.find(afk[user.sName],"(.+)%|(.+)")
      SendTxt(user,env,Bot.name,"You went away on "..dat..", because: "..why..".")
    end
  else
    SendTxt(user,env,Bot.name,"You are not away.")
  end
end

function showawaymsgs(user,data,env)
  awayarray={}
  local _,_,nick=string.find(data,"%b<>%s+%S+%s+(%S+)")
  if nick then
    if afk[nick] then
      if not string.find(afk[nick],"%|") then
        user:SendPM(Bot.name,nick.." went away at "..afk[nick].." and left no message.")
      else
        local _,_,why,dat=string.find(afk[nick],"(.+)|(.+)")
        user:SendPM(Bot.name,nick.." went away at "..dat.." and left this away message: "..why)
      end
    else
      user:SendPM(Bot.name,nick.." is not away or offline.")
    end
  else
    local msg="\r\n\r\n\t\t\t\t\t\tALL USERS' AWAY MESSAGES\r\n=================================================================================================================================\r\n\r\n"
    for a,b in afk do
      table.insert(awayarray,a)
      table.sort(awayarray)
    end
      if table.getn(awayarray)==0 then
        msg=msg.."Nobody is away.\r\n\r\n"
      else
        for a=1,table.getn(awayarray) do
        if not string.find(afk[awayarray[a]],"%|") then
          msg=msg..awayarray[a].." went away at "..afk[awayarray[a]].." and left no message.\r\n\r\n"
else
          local _,_,why,dat=string.find(afk[awayarray[a]],"(.+)|(.+)")
          msg=msg..awayarray[a].." went away at "..dat.." and left this away message: "..why.."\r\n\r\n"
end
      end
    end
    msg=msg.."================================================================================================================================="
  user:SendPM(Bot.name,msg)
  end; awayarray=nil; collectgarbage(); io.flush()
end

function saveaway()
  local f=io.open("away.dat","w+")
  for a,b in afk do
    f:write(a.."|"..b.."\n")
  end
  f:close()
end

function loadaway()
  local f=io.open("away.dat","r")
  if f then
    for line in f:lines() do
      local _,_,nick,msg=string.find(line,"(%S+)|(.+|.+)")
      afk[nick]=msg
    end
    f:close()
  end
end

function help(user)
  local msg=""
  for a,b in cmd do
    msg=msg.."\r\n"..b[1].." - "..b[2]
  end
  user:SendPM(Bot.name,msg)
end

function ParseCmd(user,data,c,env)
  local tmptbl= {
                  [cmd.afk[1]]={away,{user,data,env}},
                 
                  [cmd.back[1]]={returned,{user,data,env}},
                 
                  [cmd.mystatus[1]]={myawaystatus,{user,data,env}},
                 
                  [cmd.showaways[1]]={showawaymsgs,{user,data,env}},
                 
                  [cmd.awayhelp[1]]={help,{user,data,env}},
                }
  if tmptbl[c] then
    tmptbl[c][1](unpack(tmptbl[c][2]))
    return 1
  end
end

function Main()
  loadaway()
  frmHub:RegBot(Bot.name,1,Bot.desc,Bot.email)
end

function SendTxt(user,env,bot,text)
  if env=="PM" then
    user:SendPM(bot,text)
  else
    user:SendData(bot,text)
  end
end

function ToArrival(user,data)
  data=string.sub(data,1,string.len(data)-1)
  local _,_,whoTo = string.find(data,"$To:%s+(%S+)")
  if (whoTo == Bot) then
    local _,_,c = string.find(data,"$To:%s+%S+%s+From:%s+%S+%s+$%b<>%s+[%!%+%#](%S+)")
    if c then
      return ParseCmd(user,data,string.lower(c),"PM")
    end
  elseif afk[whoTo] then
    awaymsg(user,whoTo)
  end
end

function ChatArrival(user,data)
  data=string.sub(data,1,string.len(data)-1)
  local _,_,c = string.find(data,"%b<>%s+[%!%+%#](%S+)")
  if c then
    return ParseCmd(user,data,string.lower(c),"MAIN")
  end
end

function OnExit()
  saveaway()
end
Title: Re: Away script
Post by: TZB on 09 July, 2009, 21:04:39
There is an awayer Script by madman you can use that instead of conversion.
Title: Re: Away script
Post by: DeAn on 12 July, 2009, 06:33:49
Thanks.. But can this script be a little modified so it probably looks like this..

[09:56:28] <Awayer> Operator DeAn went away at 09:56:28 - Message : brb
[09:56:50] <Awayer> Operator DeAn returned at 09:56:19 after being away for 22 seconds.

Is it possible to display the user's profile before the users nick ?


-- Awayer 1.2
-- Made by Madman
-- Requested by Powerpuffan

-- Allows a users to set away mode with an message
-- Message can be Optional with/without an default message
-- Message can be forced without default message
-- Sends a message that user is away if pm is recived
-- Away user can be listed
-- Return user on back command or chat in main
-- Users can update there away message
-- Informs how long user been away upon return
-- Sends rightclick to userlist and hubtab

-- Version 1.1
-- Fixed: File error in updateaway, reported by PowerPuffan
-- Fixed: Missing botname in update away
-- Added: Notify user about updated message

-- Version 1.2
-- Fixed bug in RC, reported by PowerPuffan

tConfig = {
Bot = SetMan.GetString(21), -- Bots name
SaveToFile = true, -- Save Away table to a file
File = "Away.msg", -- The file to save to
ForceMsg = false, -- User must enter an away message, UseDefaultMsg can not be true if this is
UseDefaultMsg = true, -- Use DefaultMsg if user don't enter a message
DefaultMsg = "[Default Message] I'm not here right now...", -- The default msg
SilentUpdate = true, -- Set to false if script should tell all that user updated his/hers message
Menu = "Awayer", -- Name of the RightClick menu
HubOwner = "Madman",
GetAways = { -- What profiles are allowed to list the away users
[0] = true, -- Master
[1] = true, -- Op
[2] = false, -- Vip
[3] = false, -- Reg
[-1] = false, -- Unreg
}
}

Path = Core.GetPtokaXPath().."scripts/"

function OnStartup()
if tConfig.SaveToFile then
local file = io.open(Path..tConfig.File)
if file then
file:close()
else
local file = io.open(Path..tConfig.File,"w+")
file:write("Away = {\n}")
file:close()
end
LoadFromFile(Path..tConfig.File)
else
Away = {}
end
if tConfig.ForceMsg and tConfig.UseDefaultMsg then
GoOff = math.random(1,2)
Off = { "ForceMsg","UseDefaultMsg"}
tConfig[(Off[GoOff])] = false
Msg = "<" ..tConfig.Bot.. "> ForceMsg and UseDefaultMsg is bot true in Awayer's Config. Only one of thoose can be on. " ..Off[GoOff].. " was randomly choose to be disabled."
if Core.GetUser(tConfig.HubOwner) then
Core.SendToNick(tConfig.HubOwner,Msg.." Please fix this error as soon as posible.")
else
Core.SendToOps(Msg.." Please inform the the HubOwner about this as as soon as posible.")
end
end
end

function UserConnected(user)
Core.GetUserData(user,12)
if user.bUserCommand then
RightClick(user)
end
end

RegConnected = UserConnected
OpConnected = UserConnected

function RightClick(user)
local tRC = {
["away"] = {"Go Away","%[line:Message]"},
["back"] = {"Return from away",""},
["getaways"] = {"List away users",""},
["updateaway"] = {"Updated your away message","%[line:Message]"},
}
for cmd,cd in pairs(tRC) do
Core.SendToUser(user,"$UserCommand 1 3 " ..tConfig.Menu.."\\"..cd[1].." $<%[mynick]> !"..cmd.." " ..cd[2].."&#124;|")
end
end

function ChatArrival(user,data)
local data = data:sub(1,-2)
local s,e,cmd = data:find("%b<>%s+%p(%S+)")
if cmd then
tCmds = {
["away"] = function(user,data)
local s,e,msg = data:find("%b<>%s+%S+%s+(.*)")
if Away[user.sNick] then
Core.SendToUser(user,"<" ..tConfig.Bot.. "> You allready are away")
else
Away[user.sNick] = {}
Away[user.sNick]["Time"] = os.date("*t") -- Add time to table
if not msg then -- If we don't have msg
if tConfig.ForceMsg then -- Check if we must have
Core.SendToUser(user,"<" ..tConfig.Bot.. "> An away msg is requierd")
Away[user.sNick] = nil -- Clear table
return true
elseif tConfig.UseDefaultMsg then -- If we don't need msg and script should add it
Away[user.sNick]["Msg"] = tConfig.DefaultMsg -- Add default msg to table
msg = " and left the following message: " ..tConfig.DefaultMsg
end
else -- if we got msg
Away[user.sNick]["Msg"] = msg -- Add msg to table
msg = " and left the following message: " ..msg
end
if tConfig.SaveToFile then
SaveToFile(Path..tConfig.File,Away,"Away")
end
Core.SendToAll("<" ..tConfig.Bot.. "> " ..user.sNick.. " went away at " ..os.date("%X")..(msg or "!"))
end
end,
["updateaway"] = function(user,data)
if Away[user.sNick] then
local s,e,msg = data:find("%b<>%s+%S+%s+(.*)")
if msg then
Away[user.sNick]["Msg"] = msg
if not tConfig.SilentUpdate then
Core.SendToAll("<" ..tConfig.Bot.. "> " ..user.sNick.. " just updated his/hers awy message to "..msg)
end
if tConfig.SaveToFile then
SaveToFile(Path..tConfig.File,Away,"Away")
end
Core.SendToUser(user,"<" ..tConfig.Bot.. "> Message updated to " ..Away[user.sNick]["Msg"])
else
Core.SendToUser(user,"<" ..tConfig.Bot.. "> You need to enter a message to update your away status")
end
else
Core.SendToUser(user,"You are not away")
end
end,
["back"] = function(user,data)
if Away[user.sNick] then
Return(user)
else
Core.SendToUser(user,"<" ..tConfig.Bot.. "> You are not away")
end
end,
["getaways"] = function(user,data)
if tConfig.GetAways[user.iProfile] then
Msg = "\r\n\t" ..string.rep("-",50).. " Away Users " ..string.rep("-",50).."\r\n"
Msg = Msg.."\tNr\tUser\t\tTime away\t\tMessage\r\n"
Msg = Msg.."\t"..string.rep("-",121).."\r\n"
c = 0
for sNick,_ in pairs(Away) do
c = c + 1
Time = TimeConvert(os.time(Away[sNick]["Time"]))
Msg = Msg.. "\t" ..c..".\t" ..sNick.. "\t\t" ..Time.. "\t" ..(Away[sNick]["Msg"] or "").. "\r\n"
end
Msg = Msg.."\r\n\tTotalt users away: "..c
Core.SendToUser(user,"<" ..tConfig.Bot.. "> "..Msg)
else
Core.SendToUser(user,"<" ..tConfig.Bot.. "> This command is offlimit for you")
end
end,
}
if tCmds[cmd] then
return tCmds[cmd](user,data),true
end
end
if Away[user.sNick] then
Return(user)
end
end

function ToArrival(user,data)
local data = data:sub(1,-2)
local s,e,To = data:find("$To:%s+(%S+)")
if Away[To] then
if Away[To]["Msg"] then
msg = ", and left the following message: " ..Away[To]["Msg"]
end
Core.SendPmToUser(user,To,"Automated Message: " ..To.." is currently in away mode" ..(msg or "."))
end
end

function Return(user)
Time = TimeConvert(os.time(Away[user.sNick]["Time"]))
Away[user.sNick] = nil
if tConfig.SaveToFile then
SaveToFile(Path..tConfig.File,Away,"Away")
end
Core.SendToAll("<" ..tConfig.Bot.. "> " ..user.sNick.. " returned at " ..os.date("%X").. " after beeing away for " ..Time)
end

Serialize = function(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

SaveToFile = function(file , table , tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end

LoadFromFile = function(filename)
local f = io.open(filename)
if f then
local r = f:read("*a")
f:flush()
f:close()
local func,err = loadstring(r)
if func then x,err = pcall(func) end
end
end

function TimeConvert(time)
-- Code by Mutor
-- EventBot 1.0b LUA 5.11 [Strict][API 2]
if time then
local s,x,n = "",0,os.time()
local tab = {{31556926,"year"},{2592000,"month"},{604800,"week"},
{86400,"day"},{3600,"hour"},{60,"minute"},{1,"second"}}
if time > 0 then
if time < 2145876659 then
if n > time then
time = n - time
elseif n < time then
time = time - n
end
for i,v in ipairs(tab) do
if time > v[1] then
x = math.floor(time/v[1])
if x > 1 then v[2] = v[2].."s" end
if x > 0 then
s = s..x.." "..v[2]..", "
time = time-x*v[1]
end
end
end
collectgarbage("collect")
return s:sub(1,-3)
else
return "Invalid date or time supplied. [must be pre 12/31/2037]"
end
else
return "Invalid date or time supplied. [must be post 01/01/1970]"
end
else
return "Invalid date or time supplied."
end
end
Title: Re: Away script
Post by: TZB on 12 July, 2009, 08:58:48
Yes. It can be done.
Title: Re: Away script
Post by: DeAn on 12 July, 2009, 09:47:19
Thanks.. i would like to see the change in the script..