-- PostMan Bot 0.1 by bastya_elvtars (the rock n' roll doctor) -- offline message system -- you can set the inbox size and the bot name -- code ripped from law maker bot -- comands can be PMed or typed in main, the bot responds to them according to the environment (sometimes at least :D) -- the commands are case insensitive, the parameters aren't :) -- 0.2: added function so ppl cannot post 2 offline users ------------- settings Bot="PostMan" -- he he he inboxsize=10 -- the maximum amount of messages users can have in their inbox ------------ end of settings function Main() if not readfrom("offline.ini") then message={} else dofile("offline.ini") end frmHub:RegBot(Bot) end function loadlist(file) -- to see if the user was in the hub at all table=nil table={} readfrom(file) while 1 do local line = read() if line == nil then readfrom() break end table[line] = 1 end readfrom() if table then return table else return {} end end function savefile(table,file) -- save users names who were in the hub writeto(file) for a,b in table do write(a.."\n") end writeto() end function postmsg(user,data,env) local _,_,nick,msg=strfind(data,"%b<>%s+%S+%s+(%S+)%s+(.*)") if nick then if not GetItemByName(nick) then local washere=loadlist("washere.lst") if not washere then washere={} end if washere[nick] then if not message[nick] then message[nick]={[1]={["message"]=msg,["who"]=user.sName,["when"]=date("%Y. %m. %d. %X"),["read"]=0}} user:SendData(parseenv(user,env,Bot).."Successfully sent the message!") savemsg() else if checksize(nick)>=inboxsize then -- >= only for safety lol user:SendData(parseenv(user,env,Bot).."Sorry, but "..nick.." has a full inbox. Try again later.") else message[nick][checksize(nick)+1]={["message"]=msg,["who"]=user.sName,["when"]=date("%Y. %m. %d. %X"),["read"]=0} user:SendData(parseenv(user,env,Bot).."Successfully sent the message!") savemsg() end end else user:SendData(parseenv(user,env,Bot).."User "..nick.." has never been in the hub.") end else user:SendData(parseenv(user,env,Bot)..nick.." is online! PM would be simpler in this case...") end else user:SendData(parseenv(user,env,Bot).."Bad syntax! Usage: !postmsg ") end end function readmsg (user,data,env) if message[user.sName] then local _,_,args=strfind(data,"%b<>%s+%S+%s+(.+)") if args then gsub(args,"(%d+)",function(nmbr) nmbr=tonumber(nmbr) if message[%user.sName][nmbr] then local msg="\r\n\r\n\t\t\t\t\t\t\tMessage #"..nmbr.."\r\n=================================================================================================================================\r\n\r\nFrom: "..message[%user.sName][nmbr]["who"].."\r\nTime of sending: "..message[%user.sName][nmbr]["when"].. "\r\nMessage: "..message[%user.sName][nmbr]["message"].."\r\n\r\n=================================================================================================================================" %user:SendPM(Bot,msg) if message[%user.sName][nmbr]["read"]==0 then message[%user.sName][nmbr]["read"]=1 savemsg() end else %user:SendData(parseenv(%user,%env,Bot).."Message #"..nmbr.." does not exist!") end end) else user:SendData(parseenv(user,env,Bot).."Bad syntax! Usage: !showmsg . Multiple numbers can be added separated by spaces.") end else user:SendData(parseenv(user,env,Bot).."Your inbox is empty.") end end function numess(nick,a) if message[nick][a]["read"]==0 then return "no" else return "yes" end end function inbox (user,env) local msg="\r\n\r\n\t\t\t\t\t\t\tHere is your inbox:\r\n=================================================================================================================================".. "\r\nMessage #\t\t\tSender\t\t\t\t\tTime of sending\t\t\t\t\tRead\r\n=================================================================================================================================" if message[user.sName] then for a,b in message[user.sName] do msg=msg.."\r\n"..a.."\t\t\t\t"..b["who"].."\t\t\t"..b["when"].."\t\t\t\t"..numess(user.sName,a).."\r\n=================================================================================================================================" end user:SendPM(Bot,msg) user:SendPM(Bot,"Type !readmsg too see an individual message. Multiple numbers can be added separated by spaces.") if checksize(user.sName) >= inboxsize then user:SendPM(Bot,"Alert: Your inbox is full!") end else user:SendData(parseenv(user,env,Bot).."You have no messages.") end end function checksize(nick) local array={} for a,b in message[nick] do tinsert(array,a) end return getn(array) end function delmsg(user,data,env) if message[user.sName] then local _,_,args=strfind(data,"%b<>%s+%S+%s+(.+)") if args then gsub(args,"(%d+)",function (nmbr) nmbr=tonumber(nmbr) if message[%user.sName][nmbr] then message[%user.sName][nmbr]=nil %user:SendData(parseenv(%user,%env,Bot).."Message #"..nmbr.." has been successfully deleted!") else %user:SendData(parseenv(%user,%env,Bot).."Message #"..nmbr.." does not exist!") end end) if checksize(user.sName)==0 then message[user.sName]=nil savemsg() else local array={} for a,b in message[user.sName] do tinsert(array,b) end for i=1,getn(array) do message[user.sName]=resort(array) end savemsg() end else user:SendData(parseenv(user,env,Bot).."Bad syntax! Usage: !delmsg . Multiple numbers can be added separated by spaces.") end else user:SendData(parseenv(user,env,Bot).."Your inbox is empty.") end end function resort(array) local tbl={} for i=1,getn(array) do tbl[i]=array[i] end return tbl end -- nmbr, who, when, message function parse(tbl) local cnt="" for a,b in tbl do cnt=cnt.."\t\t\t\t\t\t\t\t\t["..a.."]=\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t[\"who\"]=\""..b["who"].."\",\n\t\t\t\t\t\t\t\t\t[\"when\"]=\""..b["when"].. "\",\n\t\t\t\t\t\t\t\t\t[\"message\"]=\""..b["message"].."\",\n\t\t\t\t\t\t\t\t\t},\n" end return cnt end function savemsg() writeto("offline.ini") local what="message=\n{" for a,b in message do what=what.."\n\t\t\t[\""..a.."\"]={\n"..parse(b).."\t\t\t}," end what=what.."\n}" write(what) writeto() end function parseenv(user,env,bot) if env=="PM" then return "$To: "..user.sName.." From: "..bot.." $<"..bot.."> " elseif env=="MAIN" then return "<"..bot.."> " end end function NewUserConnected(user) local washere=loadlist("washere.lst") if not washere then washere={} end if not washere[user.sName] then washere[user.sName]=1 savefile(washere,"washere.lst") end if message[user.sName] then local count={[user.sName]=0} for a,b in message[user.sName] do if b["read"]==0 then count[user.sName]=count[user.sName]+1 end end if count[user.sName]>0 then user:SendPM(Bot,"You have "..count[user.sName].." new messages. Type !inbox to see your inbox!") end end end function DataArrival(user,data) if strsub(data,1,1)=="<" then data=strsub(data,1,strlen(data)-1) local _,_,cmd = strfind(data,"%b<>%s+(%S+)") cmd=strlower(cmd) return parsecmds(user,data,cmd,"MAIN") elseif strsub(data, 1, 4) == "$To:" then data=strsub(data,1,strlen(data)-1) local _,_,whoTo = strfind(data,"$To:%s+(%S+)") if (whoTo == Bot) then local _,_,cmd = strfind(data,"$To:%s+%S+%s+From:%s+%S+%s+$%b<>%s+(%S+)") cmd=strlower(cmd) return parsecmds(user,data,cmd,"PM") end end end function parsecmds(user,data,cmd,env) local returndata=0 if cmd=="!postmsg" then postmsg(user,data,env) returndata=1 elseif cmd=="!readmsg" then readmsg (user,data,env) returndata=1 elseif cmd=="!inbox" then inbox(user,env) returndata=1 elseif cmd=="!delmsg" then delmsg(user,data, env) returndata=1 end return returndata end function UserDisconnected(user) local washere=loadlist("washere.lst") if not washere then washere={} end if not washere[user.sName] then washere[user.sName]=1 savefile(washere,"washere.lst") end end function OnExit() savemsg() end OpDisconnected=UserDisconnected OpConnected=NewUserConnected