--lua 5 by Dessamator
--## Made by nErBoS
--## Requested by gba1620
--## Commands:
--## !write - To create a new post
--## !read - Shows all post of the HUB
--## !delall - TO DELETE ALL POSTS OF THE HUB
--## !delpost - To delete a certain post
arrPost = {}
fPost = "post.dat"
mins = 60
hours =mins*60
days = 24*hours
--## Configuration ##--
AutoDel = true -- true if you want automatically delete all posts after some time(iPostDel)
iPostDel = 1*hours -- Number of (secs/minutes/hours/days) that you want the post to hold on before been deleted
sBot = frmHub:GetHubBotName() -- Bot Name
--## END ##--
function Main()
frmHub:RegBot(sBot)
LoadFromFile(fPost)
end
function OnExit()
SaveToFile(fPost , arrPost , "arrPost")
end
function ChatArrival(user, data)
local function WorkTime(string1, string2)
if not(string1 == nil) then
local hour1,min1,sec1 = string.sub(string1,1,2),string.sub(string1,4,5),string.sub(string1,7,8)
local hour2,min2,sec2 = string.sub(string2,1,2),string.sub(string2,4,5),string.sub(string2,7,8)
local hour = tonumber(hour2) - tonumber(hour1)
local min = tonumber(min2) - tonumber(min1)
local sec = tonumber(sec2) - tonumber(sec1)
hour = hour*3600
min = min*60
return (hour + min + sec)
end
end
local function Refresh()
local ctime,time,aux = os.date("%X")
if AutoDel then
for time, aux in arrPost do
if (WorkTime(arrPost[time]["time"], ctime) >= iPostDel) then
arrPost[time] = nil
end
end
end
end
--Cmds --
data=string.sub(data,1,string.len(data)-1)
s,e,cmd = string.find(data, "%b<>%s+(%S+)")
if (string.lower(cmd) == "!write") then
WritePost(user, data)
return 1
elseif (string.lower(cmd) == "!read") then
Refresh()
ReadPost(user)
return 1
elseif (string.lower(cmd) == "!delpost") then
DoDelPost(user,data)
return 1
elseif (string.lower(cmd) == "!delall") then
DoDelall(user)
return 1
end
end
function ToArrival(user,data)
if string.sub(data,6,5+(string.len(sBot))) == sBot then
ChatArrival(user,data)
end
end
--------->>>>> Write & Read functions
function WritePost(user, data)
local s,e,post = string.find(data, "%b<>%s+%S+%s+(.+)")
--Check if post exists --
local function Checkpost()
local check= true
for pos, aux in arrPost do
if (arrPost[pos]["post"] == post) then
check=false
end
end
return check
end
--GetPosition Function --
local function GetPosition(table)
local pos = 0
while 1 do
if (table[pos] == nil) then
return pos
else
pos = pos + 1
end
end
end
if not(post == nil)and Checkpost() then
local pos = GetPosition(arrPost)
arrPost[pos] = {}
arrPost[pos]["post"] = post
arrPost[pos]["by"] = user.sName
arrPost[pos]["time"] = os.date("%X")
arrPost[pos]["date"] = os.date("Day: %d/%m/20%y")
user:SendPM(sBot, "Your post has been saved.")
SendToAll(sBot, "A New Post has been posted, type !read to see.")
elseif not Checkpost() then
user:SendPM(sBot, "That post already exists.")
else
user:SendPM(sBot, "Syntax Error, !write , you must write a post.")
end
end
function ReadPost(user)
local sTmp,pos,table = "The Post of the HUB:\r\n\r\n"
for pos, table in arrPost do
sTmp = sTmp.."Subject : "..table.post.."\r\n"
sTmp = sTmp.. string.rep("-",30).."\r\n"
sTmp = sTmp.."ID: "..pos.."\r\n"
sTmp = sTmp.."By: "..table.by.."\r\n"
sTmp = sTmp.."Time: "..table.time.."\r\n"
sTmp = sTmp.."Date: "..table.date.."\r\n"
end
user:SendPM(sBot, sTmp)
end
-------->>>>End write & read Functions
------->>>>Del Functions
function DoDelPost(user,data)
local s,e,id = string.find(data, "%b<>%s+%S+%s+(.+)")
if id == nil or tonumber(id)==nil then
user:SendPM(sBot, "Syntax Error, !delpost , the id must be specified.")
elseif (arrPost[tonumber(id)] == nil) then
user:SendPM(sBot, "That post doesnt exist")
elseif (arrPost[tonumber(id)]["by"]==user.sName or user.bOperator) then
arrPost[tonumber(id)] = nil
user:SendPM(sBot, "The post has been deleted")
else
user:SendPM(sBot, "You are not authorized to delete the post !")
end
end
function DoDelall(user)
if user.bOperator then
arrPost = nil
arrPost = {}
user:SendPM(sBot, " All posts have been removed")
else
user:SendPM(sBot, "You are not authorized to delete the post !")
end
end
----------->>>End del Functions
---------->>>>>>>>>>>>OTHER FUNCTIONS <<<<<<<<<<<<<<<<<<---------------
function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end
function SaveToFile(file , table , tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end
function LoadFromFile(file)
local handle = io.open(file,"r")
if (handle ~= nil) then
dofile(file)
handle:flush()
handle:close()
end
end
--------------------------
Kool script :) But it doesnt seem to display the Date and time correctly. It displays the date.....only the time is beside the date...lol but it doesnt display the actual date at all. See below:
ID: 0
By: TEST
Date: 02:32:43
- This is the post info for the rest of the hub members.
--------------
It should be Date then time.
O is there anyways to put the subject by itself, then the post appear directly underneath, rather then putting it all together. Maybe a simple way to seperate it. Thanks again for the kool script.
QuoteOriginally posted by XPMAN
Kool script :) But it doesnt seem to display the Date and time correctly. It displays the date.....only the time is beside the date...lol but it doesnt display the actual date at all. See below:
ID: 0
By: TEST
Date: 02:32:43
--------------
It should be Date then time.
O is there anyways to put the subject by itself, then the post appear directly underneath, rather then putting it all together. Maybe a simple way to seperate it. Thanks again for the kool script.
first part of ur request is done, it should work now,
post edited
now the second part
u mean something like this ::
- This is the post info for the rest of the hub members.
--------------------------------------------------------------------
ID: 0
By: TEST
Time: 02:32:43
Date: xx/xx/xx
??
yes yes :) exactly like that my friend. If ya get a chance then maybe you can do that. Thanx again :)
done !
script edited, post updated :]
Thank u again for taking the time to do this for me, I really appreciate it :)
ur welcome !