aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSolderpunk <solderpunk@sdf.org>2018-12-08 21:25:37 +0000
committerSolderpunk <solderpunk@sdf.org>2018-12-08 21:25:37 +0000
commit74a24ca239c3ca5dbeda2873cc8d04d8a8f653ed (patch)
treece8ce346e427e1e23b10c503f112709cdae2d1d6
parentAdd 'whole' and 'save' commands to 'type', dealing with whole-thread viewing. (diff)
Make current_board a board table, not just the name of the board.
-rwxr-xr-xtelem.lua43
1 files changed, 25 insertions, 18 deletions
diff --git a/telem.lua b/telem.lua
index c68ab8d..e31c9f5 100755
--- a/telem.lua
+++ b/telem.lua
@@ -16,9 +16,9 @@ _BBS_ROOT = "/var/bbs/"
-- Global var declarations
username = os.getenv("USER") -- TODO: This is, obviously, not secure and will need to be updated
-boards = {} -- Array of board names, alphaetically sorted
-board_names = {} -- Set of board names, must always be consistent with boards
-current_board = "" -- String, name of current board, must be in boards and board_names
+boards = {} -- Array of board tables, alphaetically sorted
+boards_by_name = {} -- Set of board names, values are tables, must always be consistent with boards
+current_board = nil -- Active board table
current_board_threads = {} -- Array of thread tables, containing threads associated with current_board
current_thread = {} -- Active thread table, always an element of current_board_threads
current_thread_posts = {} -- Array of post tables, containing posts associated with current_thread
@@ -58,12 +58,12 @@ end
function update_boards()
for boarddir in lfs.dir(path.join(_BBS_ROOT, "boards")) do
- if string.sub(boarddir, 1, 1) ~= "." and not board_names[boarddir] then
+ if string.sub(boarddir, 1, 1) ~= "." and not boards_by_name[boarddir] then
local board = {}
board.name = boarddir
board.directory = path.join(_BBS_ROOT, "boards", boarddir)
board.topic = file.read(path.join(board.directory, "topic"))
- board_names[board.name] = true
+ boards_by_name[board.name] = board
table.insert(boards, board)
end
end
@@ -71,19 +71,19 @@ function update_boards()
end
function check_at_board()
- if current_board == "" then
+ if current_board == nil then
print("Not at any board! Hit `l` to list boards, `g` to go to one.")
end
- return current_board ~= ""
+ return current_board ~= nil
end
function get_threads(board)
local threads = {}
- for threaddir in lfs.dir(path.join(_BBS_ROOT, "boards", board)) do
+ for threaddir in lfs.dir(board.directory) do
if string.sub(threaddir, 1,1) == "." then goto continue end
if threaddir == "topic" then goto continue end
local thread = {}
- thread.directory = path.join(_BBS_ROOT, "boards", board, threaddir)
+ thread.directory = path.join(board.directory, threaddir)
local _, _, timestamp, author = string.find(threaddir, "(%d+)-(%g+)")
thread.timestamp = tonumber(timestamp)
thread.author = author
@@ -138,7 +138,7 @@ function do_board()
elseif string.len(board) == 0 then
print("New board cancelled.")
return
- elseif board_names[board] ~= nil then
+ elseif boards_by_name[board] ~= nil then
print("Board " .. board .. " already exists!")
return
end
@@ -165,8 +165,9 @@ function do_go()
local board = string.upper(io.read())
if board == "" then
do_list()
- elseif board_names[board] ~= nil then
- current_board = board
+ elseif boards_by_name[board] ~= nil then
+ current_board = boards_by_name[board]
+ current_board_threads = get_threads(current_board)
elseif tonumber(board) == nil then
print("No such board! Hit `l` to list boards.")
else
@@ -174,10 +175,10 @@ function do_go()
if index < 0 or index > #boards then
print("No such board! Hit `l` to list boards.")
else
- current_board = boards[index].name
+ current_board = boards[index]
+ current_board_threads = get_threads(current_board)
end
end
- current_board_threads = get_threads(current_board)
end
function do_help()
@@ -250,7 +251,7 @@ function do_new()
-- Make thread dir
local timestamp = tostring(os.time())
local thread_dir = timestamp .. "-" .. username
- local thread_path = path.join(_BBS_ROOT, "boards", current_board, thread_dir)
+ local thread_path = path.join(current_board.directory, thread_dir)
lfs.mkdir(thread_path)
-- Write subject file
file.write(path.join(thread_path, "subject"),
@@ -441,6 +442,12 @@ dispatch["b"] = do_board
dispatch["c"] = do_unimplemented
-- Infinite loop of command dispatch
-dispatch_loop(dispatch, "q",
- function() return "["..current_board.."] COMMAND :> " end,
- "Unrecognised command, hit `h` for help.")
+function prompt()
+ if current_board then
+ return "["..current_board.name.."] COMMAND :> "
+ else
+ return "[] COMMAND :> "
+ end
+end
+
+dispatch_loop(dispatch, "q", prompt, "Unrecognised command, hit `h` for help.")