#!/usr/bin/env lua local posix = require "posix" local usage = [[Usage: obbs [-c config_file] COMMAND [arguments ...] Here COMMAND is one of: newcfg init You can also use the -c option to specify an alternative configuration file. (The default is the file "config.lua" in the current directory.)]] obbs = {} local config_file = "config.lua" local function load_config () io.write("Loading config from file '" .. config_file .. "'... ") dofile(config_file) print("done.") end --- Utility functions --- local function print_table (t, lev) lev = lev or 0 local ind = string.rep("\t", lev) for k,v in pairs(t) do if type(v)=="table" then print(ind ..k .. "= {") print_table(v, lev+1) print(ind .. "}") else print(ind .. k .. "=" .. tostring(v)) end end end local fs = {} function fs.exists(filename) return io.open(filename, "r") end function fs.mkdir(dirname) posix.mkdir(dirname) end function fs.rmdir(dirname) posix.rmdir(dirname) end function fs.copy(filename, destfilename) src = assert(io.open(filename,"r")) dest = assert(io.open(destfilename,"w")) dest:write(src:read("*all")) dest:close() src:close() end function fs.touch(filename) fh = assert(io.open(filename,"w")) fh:close() end function fs.mktempdir() return posix.mkdtemp("/tmp/obbs-XXXXXX") end --- Commands --- local cmd = {} -- Send default configuration to stdout function cmd.newcfg () print [[ -- OBBS Configuration File obbs.name = "My OBBS Name" obbs.bbsid = "MYOBBSID" -- max 8 char obbs.sysop = "Sysop Name" -- obbs.hello_file = "hello" -- obbs.news_file = "news" -- obbs.goodbye_file = "goodbye" obbs.conferences = { "Announcements", "General", "Meta" } ]] end -- Initialise conference directory structure function cmd.init () load_config() if fs.exists("conferences") or fs.exists("incoming") or fs.exists("outgoing") then print("One or more OBBS directories already exist. Aborting.") return end fs.mkdir("conferences") for i,v in ipairs(obbs.conferences) do fs.mkdir("conferences/" .. v) end fs.mkdir("incoming") fs.mkdir("outgoing") end -- Pack function cmd.pack () load_config() if not arg[1] then print "Usage: obbs pack USER" return end local user_name = arg[1] local dir = fs.mktempdir() -- CONTROL.DAT local cf = assert(io.open(dir .. "/CONTROL.DAT", "w")) cf:write(obbs.name .. "\r\n") cf:write("\r\n") -- BBS location cf:write("\r\n") -- BBS phone number cf:write(obbs.sysop .. "\r\n") cf:write("," .. obbs.bbsid .. "\r\n") cf:write(os.date("%d-%m-%Y,%X") .. "\r\n") -- packet creation time cf:write(user_name .. "\r\n") cf:write("\r\n") cf:write(0 .. "\r\n") cf:write(0 .. "\r\n") -- TODO: Number of messages in packet cf:write(#obbs.conferences-1 .. "\r\n") -- Index of final conference for i,v in ipairs(obbs.conferences) do cf:write(i-1 .. "\r\n") cf:write(v .. "\r\n") end cf:write("HELLO\r\n") cf:write("NEWS\r\n") cf:write("GOODBYE\r\n") cf:close() -- Copy BBS welcome, news and goodbye files if obbs.hello and fs.exists(obbs.hello) then fs.copy(obbs.hello, dir .. "/HELLO") else fs.touch(dir .. "/HELLO") end if obbs.news and fs.exists(obbs.news) then fs.copy(obbs.news, dir .. "/NEWS") else fs.touch(dir .. "/NEWS") end if obbs.news and fs.exists(obbs.goodbye) then fs.copy(obbs.goodbye, dir .. "/GOODBYE") else fs.touch(dir .. "/GOODBYE") end -- Pack messages -- Create archive in outgoing os.execute("zip -rj outgoing/" .. obbs.bbsid .. ".qwk " .. dir) fs.rmdir(dir) end -- Main local function main() if #arg > 2 and arg[1]=="-c" then config_file = arg[2] table.remove(arg, 1) table.remove(arg, 1) end if #arg < 1 then print(usage) else if cmd[arg[1]] then local f = cmd[arg[1]] table.remove(arg,1) f() else print("Unknown command '" .. arg[1] .. "'") print(usage) end end end main() .