PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: Endless on 18 May, 2004, 11:18:56

Title: Messages for offline users
Post by: Endless on 18 May, 2004, 11:18:56
I've been looking around on this board and can't seem to find what I'm looking for, and I'm no good on lua so even if I did find exactly what I need, I may have overlooked it.  I hope this doesn't seem like too tall an order...

I'm trying to find a script for ptokax that will let me or any other user send a pm to a specified user when he connects.  I tried plop's offline message script, but I was really looking for something less complicated.  I don't want an inbox for the user, just to send them a private message the next time they connect, one time.

Something like, I type to a bot: "!postmsg User4 I have a file for you, search blah.jpg", and next time that user comes online, they get a pm from the bot saying "Message from Endless: I have a file for you, search blah.jpg".  The message is only given to them them once, and they don't need to type something like !readmsg #, etc. Being able to handle mulitple messages would be cool too, like this:

Message from User50, : I figured you may be interested in this: http://fakelink.com
Message from Endless, : I have a file for you, search blah.jpg
Message from User10, : I found those files you needed, I've got them shared now

Does a script like this already exist?

Thanks to anyone who can help :-)

Endless
Title: Offline messages
Post by: Yokozuna on 18 May, 2004, 12:20:20
This is a part of a script I use:


BOTNAME1 = "Messagebot" -- change to a name of your choice
PREFIX = "+" -- change if you use ! or something else as command prefix

function NewUserConnected(curUser)
DeliverMessage(curUser)
end

function OpConnected(curUser)
DeliverMessage(curUser)
end

function Savemessage(user,data)
if (strsub(data,1,strlen(BOTNAME1)+5) == "$To: "..BOTNAME1) then
data = strsub(data,1,strlen(data)-1)
local s,e,cmd = strfind(data,"%b<>%s+(%S+)")
if (cmd == PREFIX.."savemessage") then
s,e,arg,arg2 = strfind(data,"%b<>%s+%S+%s+(%S+)%s+([%S%s]+)")
local handle = openfile("messages/"..arg..".msg","a")
write(handle,date("%d").."."..date("%m").."."..date("%Y").." at "..date("%T").."\tfrom "..user.sName.." : "..arg2.."?")
closefile(handle)
user:SendPM(BOTNAME1,"Your offline message for "..arg.." has been saved and will be delivered as soon as "..arg.." logs in again. You will be notified when the message has been delivered.")
end
end
end

function tokenize(inString,token)
WORDS = { }
local matcher = "([^?"..token.."]+)"
gsub(inString,matcher,function(w) tinsert(WORDS,w)end)
return WORDS
end

function DeliverMessage(curUser)
handle2 = openfile("messages/"..curUser.sName..".msg","r")
if handle2 ~= nil then
line = read(handle2,"*a")
s,e,from = strfind(line,"from%s(%S+)")
line = strsub(line,1,strlen(line)-1)
linearray = tokenize(line,"?")
for i = 1,linearray.n do
curUser:SendPM(BOTNAME1,linearray[i])
end
closefile(handle2)
a,b = remove("messages/"..curUser.sName..".msg")
if not from then
return 1
end
sender = GetItemByName(from)
if sender then
sender:SendPM(BOTNAME1,"Your offline message has been delivered to "..curUser.sName.." !")
else
local handle = openfile("messages/"..from..".msg","a")
write(handle,date("%d").."."..date("%m").."."..date("%Y").." at "..date("%T").." : Offline message has been delivered to "..curUser.sName.."?")
closefile(handle)
end
end
end


Syntax is +savemessage user message
It must be sent as a PM to the bot !
The sending user will be notified when the offline message has been delivered.



Yokozuna
Title:
Post by: sander815 on 18 May, 2004, 14:19:09
i don't see a bot here
Title:
Post by: plop on 18 May, 2004, 14:39:20
QuoteOriginally posted by Yokozuna
This is a part of a script I use:

Yokozuna
don't post a part of a script, specialy not when the credits are missing.
beside that you forgot the function Main().

plop
Title:
Post by: Yokozuna on 18 May, 2004, 15:06:17
@sander

Sory, you are right.

Add

function main()
      frmHub:RegBot(BOTNAME1)
end


@plop
This script is part of a complete hubscript made by myself. This part is based on a old script made by amutex and nathanos, but I have changed it so it it notifies the users when the message is delivered !
You are right that I should have included the original credits, but I simply copied and pasted it from my scripteditor in PtokaX


Yokozuna
Title:
Post by: sander815 on 18 May, 2004, 15:48:06
like this?:
caus ei still don't see it




BOTNAME1 = "--Messagebot--" -- change to a name of your choice
PREFIX = "+" -- change if you use ! or something else as command prefix
function main()
frmHub:RegBot(BOTNAME1)
end

function NewUserConnected(curUser)
    DeliverMessage(curUser)
end

function OpConnected(curUser)
    DeliverMessage(curUser)
end
function main()
frmHub:RegBot(BOTNAME1)
end
function Savemessage(user,data)
    if (strsub(data,1,strlen(BOTNAME1)+5) == "$To: "..BOTNAME1) then
        data = strsub(data,1,strlen(data)-1)
        local s,e,cmd = strfind(data,"%b<>%s+(%S+)")
        if (cmd == PREFIX.."savemessage") then
            s,e,arg,arg2 = strfind(data,"%b<>%s+%S+%s+(%S+)%s+([%S%s]+)")
            local handle = openfile("messages/"..arg..".msg","a")
            write(handle,date("%d").."."..date("%m").."."..date("%Y").." at "..date("%T").."tfrom "..user.sName.." : "..arg2.."?")
            closefile(handle)
            user:SendPM(BOTNAME1,"Your offline message for "..arg.." has been saved and will be delivered as soon as "..arg.." logs in again. You will be notified when the message has been delivered.")
        end
    end
end

function tokenize(inString,token)
    WORDS = { }
    local matcher = "([^?"..token.."]+)"
    gsub(inString,matcher,function(w) tinsert(WORDS,w)end)
    return WORDS
end

function DeliverMessage(curUser)
    handle2 = openfile("messages/"..curUser.sName..".msg","r")
    if handle2 ~= nil then
        line = read(handle2,"*a")
        s,e,from = strfind(line,"from%s(%S+)")
        line = strsub(line,1,strlen(line)-1)
        linearray = tokenize(line,"?")
        for i = 1,linearray.n do
            curUser:SendPM(BOTNAME1,linearray[i])
        end
        closefile(handle2)
        a,b = remove("messages/"..curUser.sName..".msg")
        if not from then
            return 1
        end
        sender = GetItemByName(from)
        if sender then
            sender:SendPM(BOTNAME1,"Your offline message has been delivered to "..curUser.sName.." !")
        else
            local handle = openfile("messages/"..from..".msg","a")
            write(handle,date("%d").."."..date("%m").."."..date("%Y").." at "..date("%T").." : Offline message has been delivered to "..curUser.sName.."?")
            closefile(handle)
        end
    end
end

Title:
Post by: Yokozuna on 18 May, 2004, 16:13:11
One main function is enough :-)

I have just checked, main must be written this way: Main
Otherwise it is not recognized.
Title:
Post by: sander815 on 18 May, 2004, 16:27:50
if i'd want a description for the bot, how can i add that?
Title:
Post by: sander815 on 18 May, 2004, 16:30:52
+savemessage test in PM to the bot doesn't produce anything ....
Title:
Post by: Yokozuna on 18 May, 2004, 16:45:34
Sorry, another error on my side

Add

function DataArrival(user,data)
    Savemessage(user,data)
end

to get it work.
And you need a directory named messages in your scripting directory of PtokaX !
I will post the whole debugged script with the description you have asked for in some minutes.
Title:
Post by: Yokozuna on 18 May, 2004, 17:20:26
Here is the debugged script:

-- Thanks to amutex and nathanos for the original offline messages bot
-- modifications made by Yokozuna
PREFIX = "+" -- change if you use ! or something else as command prefix
BOTNAME1 = "--Messagebot--" -- change to a name of your choice
BOTDESCR = "Offline Messages" -- change to someting you like
BOTTAG = ""
BOTSPEED = "BOT"
BOTEMAIL = ""
BOTSHARE = "0"
SBOTINFO = "$MyINFO $ALL "..BOTNAME1.." "..BOTDESCR..BOTTAG.."$ $"..BOTSPEED..strchar(1).."$"..BOTEMAIL.."$"..BOTSHARE.."$"

function Main()
frmHub:RegBot(BOTNAME1)
UpdateBotInfo()
end

function DataArrival(user,data)
Savemessage(user,data)
SendBotInfo(curUser)
end

function NewUserConnected(curUser)
DeliverMessage(curUser)
SendBotInfo(curUser)
end

function OpConnected(curUser)
DeliverMessage(curUser)
end

function UpdateBotInfo()
SBOTINFO = "$MyINFO $ALL "..BOTNAME1.." "..BOTDESCR..BOTTAG.."$ $"..BOTSPEED..strchar(1).."$"..BOTEMAIL.."$"..BOTSHARE.."$"
end

function SendBotInfo(curUser)
SendToAll(SBOTINFO)
end

function Savemessage(user,data)
    if (strsub(data,1,strlen(BOTNAME1)+5) == "$To: "..BOTNAME1) then
        data = strsub(data,1,strlen(data)-1)
        local s,e,cmd = strfind(data,"%b<>%s+(%S+)")
        if (cmd == PREFIX.."savemessage") then
            s,e,arg,arg2 = strfind(data,"%b<>%s+%S+%s+(%S+)%s+([%S%s]+)")
            local handle = openfile("messages/"..arg..".msg","a")
            write(handle,date("%d").."."..date("%m").."."..date("%Y").." at "..date("%T").."\tfrom "..user.sName.." : "..arg2.."?")
            closefile(handle)
            user:SendPM(BOTNAME1,"Your offline message for "..arg.." has been saved and will be delivered as soon as "..arg.." logs in again. You will be notified when the message has been delivered.")
        end
    end
end

function tokenize(inString,token)
    WORDS = { }
    local matcher = "([^?"..token.."]+)"
    gsub(inString,matcher,function(w) tinsert(WORDS,w)end)
    return WORDS
end

function DeliverMessage(curUser)
    handle2 = openfile("messages/"..curUser.sName..".msg","r")
    if handle2 ~= nil then
        line = read(handle2,"*a")
        s,e,from = strfind(line,"from%s(%S+)")
        line = strsub(line,1,strlen(line)-1)
        linearray = tokenize(line,"?")
        for i = 1,linearray.n do
            curUser:SendPM(BOTNAME1,linearray[i])
        end
        closefile(handle2)
        a,b = remove("messages/"..curUser.sName..".msg")
        if not from then
            return 1
        end
        sender = GetItemByName(from)
        if sender then
            sender:SendPM(BOTNAME1,"Your offline message has been delivered to "..curUser.sName.." !")
        else
            local handle = openfile("messages/"..from..".msg","a")
            write(handle,date("%d").."."..date("%m").."."..date("%Y").." at "..date("%T").." : Offline message has been delivered to "..curUser.sName.."?")
            closefile(handle)
        end
    end
end

Make sure you have a directory named messages in your scripts directory of PtokaX !

I have tested it on my side and didn't get any error.


Yokozuna
Title:
Post by: Endless on 18 May, 2004, 23:14:28
This is PERFECT, Yokozuna.   Exactly what I was looking for and it's working great.

I have one question, I wanted to shorten the command a little, so I changed it to "msg", is below the only part I would have to change?  because I saw several other instances of "savemessage" and I'm not sure if they need to be changed too.

function Savemessage(user,data)
    if (strsub(data,1,strlen(BOTNAME1)+5) == "$To: "..BOTNAME1) then
        data = strsub(data,1,strlen(data)-1)
        local s,e,cmd = strfind(data,"%b<>%s+(%S+)")
        if (cmd == PREFIX.."msg") then


And thanks again!

Endless
Title:
Post by: Yokozuna on 19 May, 2004, 18:16:32
QuoteI have one question, I wanted to shorten the command a little, so I changed it to "msg", is below the only part I would have to change? because I saw several other instances of "savemessage" and I'm not sure if they need to be changed too.

function Savemessage(user,data)
if (strsub(data,1,strlen(BOTNAME1)+5) == "$To: "..BOTNAME1) then
data = strsub(data,1,strlen(data)-1)
local s,e,cmd = strfind(data,"%b<>%s+(%S+)")
if (cmd == PREFIX.."msg") then

Yes, it is the only part you need to change.



Yokozuna
Title:
Post by: Endless on 19 May, 2004, 22:48:19
Perfect.... Thanks again.... E
Title:
Post by: QuikThinker on 28 June, 2004, 21:47:02
Can any1 use this script or just OPs coz if possible i'd like it so it IS just the OPs, Masters & VIPs that can use it?

Quik.
Title:
Post by: Yokozuna on 28 June, 2004, 23:54:37
Hi there

Anyone can use this command.

If you want to change the script in a way that only special users are allowed to use it, do the following:

Change the function

function Savemessage(user,data)
    if (strsub(data,1,strlen(BOTNAME1)+5) == "$To: "..BOTNAME1) then
        data = strsub(data,1,strlen(data)-1)
        local s,e,cmd = strfind(data,"%b<>%s+(%S+)")
        if (cmd == PREFIX.."savemessage") then
            s,e,arg,arg2 = strfind(data,"%b<>%s+%S+%s+(%S+)%s+([%S%s]+)")
            local handle = openfile("messages/"..arg..".msg","a")
            write(handle,date("%d").."."..date("%m").."."..date("%Y").." at "..date("%T").."\tfrom "..user.sName.." : "..arg2.."?")
            closefile(handle)
            user:SendPM(BOTNAME1,"Your offline message for "..arg.." has been saved and will be delivered as soon as "..arg.." logs in again. You will be notified when the message has been delivered.")
        end
    end
end

to this

function Savemessage(user,data)
if (strsub(data,1,strlen(BOTNAME1)+5) == "$To: "..BOTNAME1) then
data = strsub(data,1,strlen(data)-1)
local s,e,cmd = strfind(data,"%b<>%s+(%S+)")
if (cmd == PREFIX.."savemessage") then
userok = 0
for every, profile in PROFILES do
if user.iProfile == tonumber(profile) then
userok = 1
end
end
if userok == 1 then
        s,e,arg,arg2 = strfind(data,"%b<>%s+%S+%s+(%S+)%s+([%S%s]+)")
local handle = openfile("messages/"..arg..".msg","a")
write(handle,date("%d").."."..date("%m").."."..date("%Y").." at "..date("%T").."\tfrom "..user.sName.." : "..arg2.."?")
closefile(handle)
user:SendPM(BOTNAME1,"Your offline message for "..arg.." has been saved and will be delivered as soon as "..arg.." logs in again. You will be notified when the message has been delivered.")
end
end
end
end

and add this:
PROFILES = { "0","1","2" } -- add the profiles here that are allowed to use the script
at the beginning of the script

That is all  :)



Yokozuna
Title:
Post by: OneHero on 06 July, 2004, 20:07:19
how can i use this script without have to PM the bot?

so that i just can type +msg in the main chat?
Title:
Post by: nErBoS on 06 July, 2004, 21:15:57
Hi,

Change this....

function Savemessage(user,data)
    if (strsub(data,1,strlen(BOTNAME1)+5) == "$To: "..BOTNAME1) then
        data = strsub(data,1,strlen(data)-1)
        local s,e,cmd = strfind(data,"%b<>%s+(%S+)")
        if (cmd == PREFIX.."savemessage") then
            s,e,arg,arg2 = strfind(data,"%b<>%s+%S+%s+(%S+)%s+([%S%s]+)")
            local handle = openfile("messages/"..arg..".msg","a")
            write(handle,date("%d").."."..date("%m").."."..date("%Y").." at "..date("%T").."\tfrom "..user.sName.." : "..arg2.."?")
            closefile(handle)
            user:SendPM(BOTNAME1,"Your offline message for "..arg.." has been saved and will be delivered as soon as "..arg.." logs in again. You will be notified when the message has been delivered.")
        end
    end
end

to this...

function Savemessage(user,data)
    if (strsub(data,1,1) == "<" or strsub(data,1,strlen(BOTNAME1)+5) == "$To: "..BOTNAME1) then
        data = strsub(data,1,strlen(data)-1)
        local s,e,cmd = strfind(data,"%b<>%s+(%S+)")
        if (cmd == PREFIX.."savemessage") then
            s,e,arg,arg2 = strfind(data,"%b<>%s+%S+%s+(%S+)%s+([%S%s]+)")
            local handle = openfile("messages/"..arg..".msg","a")
            write(handle,date("%d").."."..date("%m").."."..date("%Y").." at "..date("%T").."\tfrom "..user.sName.." : "..arg2.."?")
            closefile(handle)
            user:SendPM(BOTNAME1,"Your offline message for "..arg.." has been saved and will be delivered as soon as "..arg.." logs in again. You will be notified when the message has been delivered.")
        end
    end
end

Best regards, nErBoS