stripped from a.i..
-- offline message bot
-- stripped from artificial isanety
-- by plop
--
Bot = "offline_msg"
maxMsg = 10
Folder = "msg"
------------------------------------ what to do on startup
function Main()
if readfrom(Folder.."/offlineMSG.lua") then
dofile(Folder.."/offlineMSG.lua")
readfrom()
else
tMSG = {}
end
frmHub:RegBot(Bot)
end
------------------------------------ return the amout of msg's a user has (read and unread)
function getMsgAmount(name)
local C,R = 0,0
if tMSG[name] then
for a,b in tMSG[name] do
if tMSG[name][a] then
if tMSG[name][a]["read"]== 0 then
R = R +1
end
end
C=C+1
end
end
return C,R
end
------------------------------------ post a msg
function Post(user,data)
local s,e,to,msg = strfind(data,"%$%b<>%s+%S+%s*(%S*)%s*(.*)")
if to == "" then
return ("Lesson 1: post a msg to atleast a person!")
elseif msg == "" then
return ("How about telling @ least something?")
else
if tMSG[to] == nil then
tMSG[to] = {}
num = 1
else
num = getMsgAmount(to) + 1
end
local Total,Unread = getMsgAmount(to)
if Total >= maxMsg then
local toU = GetItemByName(to)
if toU then
toU:SendPM(Bot, user.sName.." tryed to mail you something but your mailbox is full! Pls delete 1 or more messeges!|")
end
return ("The mailbox from "..to.." is full! Try posting again later!")
end
msg = gsub(msg, "|","\r\n")
tMSG[to][num] = {}
tMSG[to][num]["who"]=user.sName
tMSG[to][num]["msg"]=msg
tMSG[to][num]["time"]=date()
tMSG[to][num]["read"]=0
SaveMsg()
local toU = GetItemByName(to)
if toU then
toU:SendPM(Bot, "There is a messege waiting for you! type !readmsg "..num..".|")
return ("msg posted to: "..toU.sName )
else
return ("msg posted to: "..to )
end
end
end
------------------------------------ reading msg's
function Read(user, data)
if tMSG[user.sName] == nil then
return ("You have no messages!")
else
local s,e,num = strfind(data,"%$%b<>%s+%S+%s*(%S*)")
num = tonumber(num)
if num == nil then
local Total,Unread = getMsgAmount(user.sName)
return ("you have "..Total.." messages! ".. Unread.." new")
elseif tMSG[user.sName][num] then
if tMSG[user.sName][num]["read"]== 0 then
tMSG[user.sName][num]["read"]=1
end
return ("\r\n\r\n "..tMSG[user.sName][num]["who"].." posted on "..tMSG[user.sName][num]["time"]..
"\r\n-----------------------------------------------------------------------\r\n"..format("%8s", tMSG[user.sName][num]["msg"])..
"\r\n-----------------------------------------------------------------------\r\n")
else
return ("unknown msg number "..num)
end
end
end
------------------------------------ bye bye msg
function Delete(user,data)
local s,e,num = strfind(data,"%$%b<>%s+%S+%s*(%S*)")
num = tonumber(num)
if num then
if tMSG[user.sName][num] then
tMSG[user.sName][num] = nil
Renumber(user.sName, num)
SaveMsg()
return ("messege number "..num.." deleted!")
else
return "unknow ID"
end
else
return "Not a number!"
end
end
------------------------------------ re-index after deleting
function Renumber(name, num)
for i=(num+1), maxMsg do
if tMSG[name][i] then
tMSG[name][(i-1)] = tMSG[name][i]
tMSG[name][i] = nil
else
break
end
end
end
------------------------------------ saving the table
function SaveMsg()
writeto(Folder.."/offlineMSG.lua")
write("\n--data file for the offline msg system\n\ntMSG={")
local t = ""
for a,b in tMSG do
write(t.."\n ["..format("%q", a).."]=")
if t == "" then t = "," end
if type(b) == "string" then
write(format("%q", b))
elseif type(b) == "number" then
write(b)
elseif type(b) == "table" then
t2 = ""
t3 = ""
write("{")
for c,d in b do
if type(c) =="number" then
write(t2.."\n ["..c.."]=")
if t2 == "" then t2 = "," end
if type(d) == "string" then
write(format("%q", d))
elseif type(d) == "number" then
write(d)
elseif type(d) == "table" then
write("{")
t4 = ""
for e,f in d do
if type(f) == "number" then
write(t4.."\n ["..format("%q", e).."]="..f )
else
write(t4.."\n ["..format("%q", e).."]="..format("%q", f) )
end
if t4 == "" then t4 = "," end
end
write("\n }")
else
SendToAll(Bot, "what the hell are you trying 2 save plop, thats no number/table or string!|")
end
elseif type(c) == "string" then
write(t3.."\n ["..format("%q", c).."]=")
if t3 == "" then t3 = "," end
if type(d) == "string" then
write(format("%q", d))
elseif type(d) == "number" then
write(d)
elseif type(d) == "table" then
write("{")
t5 = ""
for e,f in d do
write(t5.."\n ["..e.."]="..f )
if t5 == "" then t5 = "," end
end
write("\n }")
else
SendToAll(Bot, "what the hell are you trying 2 save plop, thats no number/table or string!|")
end
else
SendToAll(Bot, "what the hell are you trying 2 save plop, thats no number or string!|")
end
end
write("\n }")
else
SendToAll(Bot, "what the hell are you trying 2 save plop|")
end
end
write("\n}\n\n--ploppyrights reserved")
writeto()
return "Done"
end
------------------------------------ show if a user has msg's or not
function EnterMsg(user)
local Total,Unread = getMsgAmount(user.sName)
local line = ""
if Total >= maxMsg then
if Unread ~= 0 then
line = line.." ".. Unread.." new message(s)."
else
line = "Your mailbox is full! Pls delete 1 or more message(s)!"..line
end
elseif Unread ~= 0 then
line = line.."You have ".. Unread.." new message(s)."
end
if line ~= "" then
return line
end
end
------------------------------------ standard stuff
function DataArrival(user, data)
if(strsub(data, 1, 4) == "$To:") then
local s,e,whoTo = strfind(data,"$To:%s+(%S+)")
if whoTo == Bot then
data=strsub(data,1,strlen(data)-1)
local s,e,cmd = strfind(data,"%$%b<>%s+(%S+)")
if cmd == "!postmsg" then
user:SendPM(Bot, Post(user,data).."|")
return 1
elseif cmd == "!readmsg" then
user:SendPM(Bot, Read(user, data).."|")
return 1
elseif cmd == "!delmsg" then
user:SendPM(Bot, Delete(user, data).."|")
return 1
end
end
end
end
------------------------------------ hey a new user entered, maby he has msg's
function NewUserConnected(user)
local tmp = EnterMsg(user)
if tmp then
user:SendPM(Bot, tmp.."|")
end
end
------------------------------------ shouldn't forget the poor OP's
function OpConnected(user)
local tmp = EnterMsg(user)
if tmp then
user:SendPM(Bot, tmp.."|")
end
end
--ploppyrights reserved
plop
*Net nu ik Phatty's mailscript al aan het testen ben ..
*translation:
Grrr , just when i am putting Phatty's script through the test ..
;0)
QuoteOriginally posted by UwV?
*Net nu ik Phatty's mailscript al aan het testen ben ..
*translation:
Grrr , just when i am putting Phatty's script through the test ..
;0)
sorry, forgot about this thing a bit.
wanted 2 release it more then a week before i posted it.
i had the idea for something like this for a long time but seeing phatty's version convinced me 2 make it myself.
not that his version is bad, i just like 2 try and make things myself.
plop