Help converting offline-message script
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Help converting offline-message script

Started by bolamix, 28 February, 2005, 23:14:12

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bolamix

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
Sharing is of the essence!

Live music >> Aiwadirock! live music hub
PtokaX knowledge >> The PtokaX Wiki

bolamix

^^ bump ^^

... no-one?
Sharing is of the essence!

Live music >> Aiwadirock! live music hub
PtokaX knowledge >> The PtokaX Wiki

bastya_elvtars

I will post mine converted to lua5. Hope its better.
Everything could have been anything else and it would have just as much meaning.

bolamix

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 ^^
Sharing is of the essence!

Live music >> Aiwadirock! live music hub
PtokaX knowledge >> The PtokaX Wiki

bolamix

#4
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)
Sharing is of the essence!

Live music >> Aiwadirock! live music hub
PtokaX knowledge >> The PtokaX Wiki

Jelf

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.

bolamix

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
Sharing is of the essence!

Live music >> Aiwadirock! live music hub
PtokaX knowledge >> The PtokaX Wiki

Jelf

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 :)

bolamix

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 ;)
Sharing is of the essence!

Live music >> Aiwadirock! live music hub
PtokaX knowledge >> The PtokaX Wiki

SMF spam blocked by CleanTalk