PtokaX forum

Archive => Archived 5.0 boards => Help with scripts => Topic started by: bolamix on 28 February, 2005, 23:14:12

Title: Help converting offline-message script
Post by: bolamix on 28 February, 2005, 23:14:12
Hi,
I've converted the offline-message script i'm using to lua 5, and most of it works! Here it is:-- offlinemsg.lua, created by amutex 11.01.2003
-- thx to nathanos for the fine pm-parsing
-- bits and pieces added and deleted by bolamix over time, kudos to all scripters who helped!
-- attempted conversion to lua5 by bolamix Feb. 27 2005
-- successful conversion to lua5 by **your_name_here** :D

botname = frmHub:GetHubBotName()

function Get2Args(data)
s,e,whoTo,from,cmd,arg,arg2 = string.find(data,"$To:%s+(%S+)%s+From:%s+(%S+)%s+$%b<>%s+(%S+)%s+(%S+)%s+(.*)")
return arg,arg2
end

function ToArrival(user, data)
if(string.sub(data, 1, 4) == "$To:") then
data=string.sub(data,1,string.len(data)-1)
s,e,whoTo = string.find(data,"$To:%s+(%S+)")
if (whoTo == botname) then
s,e,whoTo,from,cmd = string.find(data,"$To:%s+(%S+)%s+From:%s+(%S+)%s+$%b<>%s+(%S+)")
if (cmd=="+note") then
arg,arg2 = Get2Args(data)
local f,e = io.open("messages/"..arg..".msg","a+")
if f then
   f:write(""..user.sName.." t'a envoy? ce message le "..GetDate().." ? "..GetTime().." : "..arg2.."?")
user:SendPM(botname,"Message enregistr?. Il sera transmis ? "..arg.." ? sa prochaine connexion.")
f:close()
end
end
end
end
end

function NewUserConnected(curUser)
if (io.open("messages/"..curUser.sName..".msg","r")~=nil) then
   showtext (curUser, curUser.sName)
io.close()
end
os.remove("messages/"..curUser.sName..".msg")
end

OpConnected = NewUserConnected

function showtext(user, file)
local contents =""
    for line in io.lines("messages/"..user.sName..".msg") do
        contents = contents..line.."\r\n"
    end
    user:SendPM("#Message_en_absence", contents.."\r\n|")
end

function GetTime()
s = os.date("%S")
h = os.date("%H")
m = os.date("%M")
Time = ""..h..":"..m..":"..s
return Time
end

function GetDate()
d = os.date("%d")
mm = os.date("%m")
y = os.date("%y")
Date = ""..d.."/"..mm.."/"..y
return Date
end
It works as it should, the only problem is that os.remove("messages/"..curUser.sName..".msg") seems to have no effect. I mean it doesn't make a difference whether the line is here or not, the file just stays put on my drive. I get no error in the script console. Anybody knows what's wrong? TIA
Title:
Post by: bolamix on 01 March, 2005, 19:19:47
^^ bump ^^

... no-one?
Title:
Post by: bastya_elvtars on 02 March, 2005, 00:49:21
I will post mine converted to lua5. Hope its better.
Title:
Post by: bolamix on 02 March, 2005, 04:52:23
Thanks bast ;)
I'd really like to know why os.remove doesn't work though... I like it when I understand what's going on ^^
Title: further testing
Post by: bolamix on 09 March, 2005, 05:11:32
After trying things out a bit I noticed that os.remove("messages/"..curUser.sName..".msg") actually locks the file on my 'puter, i.e. if i try to delete it manually, I get a "sharing violation, can't delete file" error.
If I remove the line, I can delete the file. But whatever I try, the script by itself still can't :( Weird...
(btw I'm using 16.05 dbg)
Title:
Post by: Jelf on 09 March, 2005, 12:43:10
Heres the script..
-- offlinemsg.lua, created by amutex 11.01.2003
-- thx to nathanos for the fine pm-parsing
-- bits and pieces added and deleted by bolamix over time, kudos to all scripters who helped!
-- attempted conversion to lua5 by bolamix Feb. 27 2005
-- successful conversion to lua5 by Jelf :D

path = "messages"

os.execute("mkdir ".."\""..string.gsub(path, "/", "\\").."\"")

botname = frmHub:GetHubBotName()

function Get2Args(data)
s,e,whoTo,from,cmd,arg,arg2 = string.find(data,"$To:%s+(%S+)%s+From:%s+(%S+)%s+$%b<>%s+(%S+)%s+(%S+)%s+(.*)")
return arg,arg2
end

function ToArrival(user, data)
if(string.sub(data, 1, 4) == "$To:") then
data=string.sub(data,1,string.len(data)-1)
s,e,whoTo = string.find(data,"$To:%s+(%S+)")
if (whoTo == botname) then
s,e,whoTo,from,cmd = string.find(data,"$To:%s+(%S+)%s+From:%s+(%S+)%s+$%b<>%s+(%S+)")
if (cmd=="+note") then
arg,arg2 = Get2Args(data)
local f,e = io.open("messages/"..arg..".msg","a+")
if f then
   f:write(""..user.sName.." t'a envoy? ce message le "..GetDate().." ? "..GetTime().." : "..arg2)
user:SendPM(botname,"Message enregistr?. Il sera transmis ? "..arg.." ? sa prochaine connexion.")
f:close()
end
end
end
end
end

function NewUserConnected(curUser)

local handle = io.open(path.. "/" ..curUser.sName ..".msg","r")

if (handle ~= nil) then
local line = handle:read()
curUser:SendPM(botname, line)
handle:close()
os.remove(path.."/" ..curUser.sName ..".msg")
else
end
end

OpConnected = NewUserConnected

function showtext(user, file)
local contents =""
    for line in io.lines("messages/"..user.sName..".msg") do
        contents = contents..line.."\r\n"
    end
    user:SendPM("#Message_en_absence", contents.."\r\n|")
end

function GetTime()
s = os.date("%S")
h = os.date("%H")
m = os.date("%M")
Time = ""..h..":"..m..":"..s
return Time
end

function GetDate()
d = os.date("%d")
mm = os.date("%m")
y = os.date("%y")
Date = ""..d.."/"..mm.."/"..y
return Date
end
..and yes there is a better offline message script than this.
Title:
Post by: bolamix on 09 March, 2005, 14:56:02
Sweeeeeeet thx a lot Jelf, it does work!

Couple of questions though:

1) The offline message is sent from the hub's bot, and not from "#Message_en_absence", which means the line
user:SendPM("#Message_en_absence", contents.."\r\n|") is somehow wrong, or at least partially ignored by the hub. And I don't understand why :/
I'd like it to be sent from that username since my hub-bot already sends a couple messages (chat history and hub rules) on entry and users might miss
the actual offline message. Maybe I should add that "#Message_en_absence" (which means #Offline_Message btw) is not in the userlist,
so that probably further complicates matters...

2) Can you please explain the purpose of os.execute("mkdir ".."\""..string.gsub(path, "/", "\\").."\""), and also why "..path.." is necessary in
os.remove(path.."/" ..curUser.sName ..".msg") but not in for line in io.lines("messages/"..user.sName..".msg") do?
Once again my limited understanding of lua fails me... I'd be very grateful if you (or someone else) could enlighten me.

And 3) I'm looking forward to seeing this "better offline message script" :D

Thanks in advance, I'm learning, little by little!

PS: I know that the whole if(string.sub(data, 1, 4) == "$To:") then is not necessary. I'll remove it.
PS2: I've searched for answers on lua.org, but I have no background in programming and a lot of the documentation makes no sense to me,
seems I need examples and explanations... sorry :s
Title:
Post by: Jelf on 09 March, 2005, 17:03:04
1) Woops, sorry. Change this..
curUser:SendPM(botname, line)to this..
curUser:SendPM("#Message_en_absence", line)2) The purpose is so that the folder "messages" is created when the script is started, so it doesnt give an error if the folder isnt there. I used path as was easier to just change that.
Yup your right with..
QuotePS: I know that the whole if(string.sub(data, 1, 4) == "$To:") then is not necessary. I'll remove it.
I just was too lazy to take it out :)
Title:
Post by: bolamix on 09 March, 2005, 22:54:38
DOH! *I* am the sorry one m8, can't believe I didn't see that line.... I read and re-read the script, missed and re-missed the line :p
QuoteThe purpose is so that the folder "messages" is created when the script is started, so it doesnt give an error if the folder isnt there.
k, i understand the logic behind it. What was especially bugging me was that my previous script returned no error. The folder was there to begin with anyway, but i guess it doesn't matter for the script. The dos window I see popping up probably says "folder already exists" (too fast to read). The trick was to use "local handle = " and "handle.close()". This obviously allows "os.remove" to work as it should. Question, out of curiosity: could "os.execute("del ".."\"blablabla)" be used in this case too?

I have trouble figuring out regexp, but i'll look hard at this os.execute function with help from lua.org and with any luck I'll manage to understand it eventually ;)

Thanks a whole lot :D I'll make the final corrections and post it in Finished Scripts. Might be useful for someone until that better script is unearthed ;)