PtokaX forum

Archive => Archived 5.1 boards => Finished Scripts => Topic started by: BluePanther on 16 November, 2006, 18:51:36

Title: ClipBoard
Post by: BluePanther on 16 November, 2006, 18:51:36
Hi,

requesting a small Blackboard for my HUB (http://forum.ptokax.org/index.php?topic=6561.0) I got some help and so I modded the CodeBoard from plop and Herodes. Basicly I throw out a lot to make it easier to use (by cuting features).
Now there is only one logfile. You can use it as an offline chat or newsboard from and to the user. After correcting the help cmd by Dessamator the scripts runs on Lua 5 & 5.1 I called it ClipBoard, so I didn't need to change hole the CBs...
Feel free to criticise, I'd like to learn. Thanks for your help.


--- ClipBoard 1.0 for LUA 5 & 5.1 by BluePanther
--- based on code board V2 by plop and ver v3 by Herodes, thx to Dessamator

--- Start the script ...
function Main()
Bot = "ClipBoard"
tUsers = {}
logfile = "clipboard.txt"
cbcmd = "cb"
frmHub:RegBot(Bot, 1, "","")
end


--- Main Handling done here ...
function CB(user, data, how)
local ok, len = 1, (string.len(cbcmd) +1)
s,e,chat = string.find(data, "%b<>%s+(.*)$")
s,e,cmd,args = string.find(data, "%b<>%s+%p(%S+)%s*(.*)$")
if cmd and (string.sub(cmd, 1,string.len(cbcmd)) == cbcmd) then
s,e,cmd,args = string.find(data, "%b<>%s+%p(%S+)%s*(.*)$")
if (cmd == cbcmd.."r") then SendBack(ReadIt(logfile), user, how);
ok = nil; return 1
elseif (cmd == cbcmd.."n") then
ChangeWay(user, how); ok = nil; return 1
elseif (cmd == cbcmd.."p") then
DoPost(user, string.sub(chat, len+2, -1), how);
ok = nil;return 1
elseif (cmd == cbcmd.."h") then
CBHelp(user, how); ok = nil; return 1
end
end
if ok and how then
DoPost(user, chat, how); return 1
end
end

--- Read the posts from logfile ...
function ReadIt(file)
local msg = "\r\n"
if ( io.input(file) ) then
text = io.input(file)
for line in text:lines() do msg = msg..line.."\r\n" end
text:close()
return msg
else
return file.." does not exist"
end
end


--- Choose the way the CB will be annoying a user ...
function ChangeWay(user, how)
local tMsgs =
{ "You are now going to be receiving the cb messages in main",
"You are now going to be receiving the cb messages in pm",
"You are not going to be receiving the cb messages",}
tUsers[user.sName] = tUsers[user.sName] + 1
if tUsers[user.sName] > 3 then tUsers[user.sName] = 1 end
SendBack(tMsgs[tUsers[user.sName]], user, how)
end


--- Take care of posting in the CB ...
function DoPost(user, data, out)
local line = user.sName.."  "..data
local handle, err = io.open(logfile, "a+")
if handle then
handle:write("\r\n"..line)
handle:close()
end
for a,b in pairs(frmHub:GetOnlineUsers()) do
if b.sName ~= user.sName then
if tUsers[b.sName] then
if tUsers[b.sName] == 1 then SendBack( line, b )
elseif tUsers[b.sName] == 2 then SendBack( line, b , true )
else SendBack( "*** "..user.sName.." has posted on the ClipBoard", b )
end
else
tUsers[b.sName] = 1
SendBack ( line , b, true )
end
else
SendBack ( "Your Posting has been sent!", user, out )
end
end
end


--- Help text ..
function CBHelp(user, how)
local msg, tC = "\r\n\r\n\t These commands are available for the "..Bot.."\r\n\t"..string.rep("- ", 40).."\r\n",
  { ["r"] = "read the ClipBoard log",
["p"] = "post in ClipBoard",
["n"] = "ClipBoard messages in pm/main/off",
["h"] = "this text",}
for cmd, explain in pairs(tC) do
msg = msg.."\t\t+"..cbcmd..cmd.."\t"..explain.."\r\n"
end
SendBack(msg, user, how)
end

--- Utility functions ...
function SendBack(msg, user, out)
if out then user:SendPM(Bot, msg)
else user:SendData(Bot, msg)
end
end


function Serialize(tTable, sTableName, sTab)
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

--- When Mainchat msg arrives ...
function ChatArrival( user, data )
if not tUsers[user.sName] then tUsers[user.sName] = 1 end
return CB( user, string.sub( data, 1, -2 ))
end

--- When a pm arrives ...
function ToArrival( user, data )
if not tUsers[user.sName] then tUsers[user.sName] = 1 end
if (string.sub( data, 1, string.len(Bot)+5) == "$To: "..Bot) then
local s,e,data = string.find(data, "%$(%b<>.*)$")
return CB( user, string.sub( data, 1, -2 ), true )
end
end