PostMan 0.3 LUA5 - Page 2
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

PostMan 0.3 LUA5

Started by bastya_elvtars, 09 March, 2005, 14:06:02

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bastya_elvtars

0.51

    [*]bastya_elvtars put his hands back on this
    [*]script now creates an empty offline.dat if does not exist... you guys keep ignoring new Lua5 features :P
    [*]thx guys for the features you added, they are really eye-candy =)
    [/list]

    I recommend this. :P

       -- PostMan Bot 0.3 by bastya_elvtars (the rock n' roll doctor)
        -- offline message system
        -- 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 online users
    
    
    
        ------- 0.3:
        -- converted to lua5
        -- washere is now global, no more CPU spikes
    
    
    
        ------- 0.4:
        -- touched by Herodes (optimisation tsunami)
        -- added : more details on the bot
        -- added : message privacy ;)
        -- changed : better way to parseenv ;)
        -- changed : better way to display messages and inbox
        -- changed : better way to parsecmds ( thx bastya_elvtars ;)
    
        ------- 0.5:
        -- touched by Skippy84
        -- fix error: make offline.dat thx goto Herodes for the idea to fix the problem
        -- fix error: false command !showmsg
        -- added: commands can edit at the Settings section
        -- added: shown commands linked to the editable commands (so ther change on the fly)
    
        ------- 0.5:
        -- bastya_elvtars put his hands back on this
        -- script now creates an empty offline.dat if does not exist... you guys keep ignoring new Lua5 features :P
        -- thx guys for the features you added, they are really eye-candy
    
    
    
        --------------------------------SETTINGS----------------------------------------
    
        Bot = {
        name = "PostMan" , -- bot's name
        email = "postman@mail.me", -- bot's email
        desc = "Post messages to other users here..", -- bot's desc
        }
    
        inboxsize=10 -- the maximum amount of messages users can have in their inbox
    
        cmdpost = "postmsg" -- Shows the Hub Stats
        cmdread = "readmsg" -- Shows the Hub Stats
        cmdibox = "inbox" -- Shows the Hub Stats
        cmddbox = "delmsg" -- Shows the Hub Stats
    
    
    
        ----------------------END OF SETTINGS-------------------------------------------
    
    
    
    
    
        -------------------------------------- Utility Functions
    
        function cls()
        collectgarbage()
        io.flush()
        end
    
    
    
        function Main()
        local function load()
        local t = {}; local f = io.open("washere.lst", "r")
        if f then for l in f:lines() do t[l] = 1; end; f:close(); end
        return t;
        end
        washere = load()
        message = {}
    	if loadfile("offline.dat") then
    		dofile("offline.dat")
    	else
    		io.output("offline.dat")
    		io.write("")
    		io.output()
    	end
        frmHub:RegBot(Bot.name, 1, Bot.desc, Bot.email )
        Bot = Bot.name
        end
    
    
    
        -------------------------------------- Command Functions
    
        --- post msg
    
        function postmsg( user, data, how )
        local _,_,nick,msg = string.find(data,"%b<>%s+%S+%s+(%S+)%s+(.*)")
        if nick then
        if not GetItemByName(nick) then
        if washere[nick] then
        local function checksize(n) local cnt = 0; for a,b in message[n] do cnt = cnt + 1; end return cnt; end
        if not message[nick] then message[nick] = {}; end
        if (checksize(nick) < inboxsize) then
        table.insert( message[nick], { ["message"] = msg, ["who"] = user.sName, ["when"] = os.date("%Y. %m. %d. %X"), ["read"] = 0, } )
        SendBack( user, "Successfully sent the message!", Bot, how )
        savemsg()
        else
        SendBack( user, "Sorry, but "..nick.." has a full inbox. Try again later.", Bot, how )
        end
        else
        SendBack( user, "User "..nick.." has never been in the hub.", Bot, how )
        end
        else
        SendBack( user, nick.." is online! PM would be simpler in this case...", Bot, how )
        end
        else
        SendBack( user, "Bad syntax! Usage: !"..cmdpost.."  ", Bot, how )
        end
        cls(); return 1;
        end
    
    
    
        function delmsg( user, data, how )
        if message[user.sName] then
        local _,_,args = string.find(data,"%b<>%s+%S+%s+(.+)")
        if args then
        local function checksize(n) local cnt = 0; for a,b in message[n] do cnt = cnt + 1; end return cnt; end
        local function resort(t) local r ={}; for i,v in t do table.insert(r, v); end; return r; end
        for num in string.gfind( args, "%s-(%d+)%s-" ) do
        if num then
        num = tonumber(num);
        if message[user.sName][num] then
        message[user.sName][num] = nil
        SendBack( user, "Message #"..num.." has been successfully deleted!", Bot, how )
        else
        SendBack( user, "Message #"..num.." does not exist!", Bot, how )
        end
        end
        end
        message[user.sName] = resort(message[user.sName]);
        if checksize(user.sName) == 0 then message[user.sName] = nil; end
        savemsg()
        else
        SendBack( user, "Bad syntax! Usage: !"..cmddbox.." . Multiple numbers can be added separated by spaces.", Bot, how )
        end
        else
        SendBack( user, "Your inbox is empty.", Bot, how )
        end
        cls(); return 1;
        end
    
        ----------------------------------------------------------------------------------
        ---------------------------------------------------------------------------------
        ---------------------------------------------------------------------------------- show inbox
    
        function inbox( user, how )
        local sep, msg = string.rep( "=", 75 ), "\r\n\r\n\t\t\t\t\t\t\tHere is your inbox:\r\n"
        msg = msg..sep.."\r\n Msg#\tSender\tTime of sending\t\tRead\r\n"..sep
        if message[user.sName] then
        local function numess ( r ) if r == 0 then return "no"; end return "yes"; end
        local function checksize ( n ) local cnt = 0; for a,b in message[n] do cnt = cnt + 1; end return cnt; end
        local function dcode ( s ) if (string.sub(s,1,3) == "-n#") then s=string.sub(s,4,-1); local res = ""; for num in string.gfind( s, "#?(%d+)") do res = res..string.char(tonumber(num));end;return res;end;return s;end
        for num, t in message[user.sName] do
        msg=msg.."\r\n "..num.."\t"..dcode(t.who).."\t"..t.when.."\t"..numess(t.read).."\r\n"..sep
        end
        SendBack( user, msg, Bot, true )
        SendBack( user, "Type !"..cmdread.."  too see an individual message. Multiple numbers can be added separated by spaces.", Bot, true )
        if checksize(user.sName) >= inboxsize then SendBack( user, "Alert: Your inbox is full!", Bot, true ); end
        else
        SendBack( user, "You have no messages.", Bot, how )
        end
        cls(); return 1;
        end
    
    
    
        --- read msg(s)
    
        function readmsg( user, data, how )
        if message[user.sName] then
        local _,_,args=string.find(data,"%b<>%s+%S+%s+(.+)")
        if args then
        local function dcode(s) if (string.sub(s,1,3) == "-n#") then s = string.sub(s,4,-1);local res = ""; for num in string.gfind( s, "#?(%d+)") do res = res..string.char(tonumber(num));end;return res;end;return s;
        end
        for num in string.gfind( args, "%s-(%d+)%s-" ) do
        if num then num = tonumber(num) end
        if num and message[user.sName][num] then
        local t = message[user.sName][num]
        local msg, sep,set = "\r\n\r\n\t\t\t\t\t\t\tMessage #"..num.."\r\n", string.rep( "=", 100 ), string.rep("- ", 85)
        msg = msg..sep.."\r\n\r\nFrom: "..dcode(t.who).."\tTime: "..t.when.."\t\tMessage follows\r\n"..set.."[Message start]\r\n"..dcode(t.message).."\r\n"..set.."[Message end]\r\n"..sep
        SendBack( user, msg, Bot, true )
        if t.read == 0 then t.read = 1; savemsg(); end
        else
        SendBack( user, "Message #"..num.." does not exist!", Bot, how )
        end
        end
        else
        SendBack( user, "Bad syntax! Usage: !"..cmdread.." . Multiple numbers can be added separated by spaces.", Bot, how )
        end
        else
        SendBack( user, "Your inbox is empty.", Bot, how )
        end
        cls(); return 1;
        end
    
    
    
        function savemsg()
        local function parse(tbl)
        local str, tab ="", string.rep( "\t", 9)
        local function fquot(s) return string.format( "%q", s) end
        local function ncode( s ) if (s ~= "") and (string.sub(s,1,3) ~= "-n#") then local t = {}; for i = 1, string.len(s) do t[i] = string.byte(string.sub(s,i,i)); end; return "-n#"..table.concat(t, "#"); end; return s; end
        for a, t in tbl do
        str = str.."\t\t{ ["..fquot("read").."] = "..t.read..", ["..fquot("who").."] = "..fquot(ncode(t.who))..", ["..fquot("when").."] = "..fquot(t.when)..",\n\t\t["..fquot("message").."] = "..fquot(ncode(t.message)).." },\n"
        end
        return str
        end
        local f = io.open ( "offline.dat", "w+" )
        local s = "message = {"
        for name, t in message do
        s = s.."\n\t["..string.format( "%q", string.gsub( name, "\"", "\"")).."] = {\n"..parse(t).."\t},"
        end;
        f:write(s.."\n}")
        f:close()
        end
    
    
    
        function SendBack( user, msg, who, pm )
        if pm then user:SendPM ( who, msg ); else user:SendData( who, msg ); end
        end
    
    
    
        function NewUserConnected(user)
        if not washere[user.sName] then washere[user.sName] = 1 end
        if message[user.sName] then
        local cnt=0
        for a,b in message[user.sName] do if (b.read == 0) then cnt = cnt+1; end end
        if (cnt > 0) then SendBack( user, "You have "..cnt.." new messages. Type !"..cmdibox.." to see your inbox!", Bot, true ); end
        end
        end
    
    
    
        function ChatArrival(user,data)
        local data = string.sub( data, 1, -2 )
        local _,_,cmd = string.find(data,"%b<>%s+[!+.#?](%S+)")
        if cmd then return parsecmds( user, data, string.lower(cmd) ); end
        end
    
    
    
        function ToArrival(user,data)
        if (string.sub( data, 1, string.len(Bot)+5) == "$To: "..Bot) then
        local data = string.sub( data, 1, -2 )
        local _,_,cmd = string.find(data,"%$%b<>%s+[!+.#?](%S+)")
        if cmd then return parsecmds( user, data, string.lower(cmd), true ) end
        return 1
        end
        end
    
    
    
    
        function parsecmds( user, data, cmd, how )
        local t = {
        [cmdpost] = { postmsg, { user, data, how } },
        [cmdread] = { readmsg, { user, data, how } },
        [cmdibox] = { inbox, { user, how } },
        [cmddbox] = { delmsg, { user, data, how } },
        }
        if t[cmd] then
        return t[cmd][1]( unpack(t[cmd][2]) )
        end
        end
    
    
    
        function UserDisconnected(user)
        if not washere[user.sName] then washere[user.sName] = 1; end
        end
    
    
        function OnExit()
        savemsg()
        local f = io.open( "washere.lst", "w+")
        for a,b in washere do f:write(a.."\n"); end
        f:close()
        end
    
    
        OpDisconnected=UserDisconnected
        OpConnected=NewUserConnected
    
    Everything could have been anything else and it would have just as much meaning.

    SMF spam blocked by CleanTalk