#!/usr/bin/lua5.2 telemver = "0.3 // 2018-05-04" io = require("io") os = require("os") lfs = require("lfs") string = require("string") table = require("table") bad_topics = {} bad_topics["."] = true bad_topics[".."] = true bad_topics["topic"] = true current_board = "" current_thread_index = nil function cat_file(filename) io.input(filename) welcome = io.read("*all") io.input(io.stdin) io.write(welcome) end function show_welcome() print("::::::::::: This is telem ver."..telemver.." :::::::::::") cat_file("/var/bbs/docs/welcome") end function show_prompt() io.write("["..current_board.."] COMMAND :> ") end function getchar() local char os.execute("/bin/stty -icanon") char = io.read(1) os.execute("/bin/stty icanon") return char end function get_threads(board) local threads = {} for topic in lfs.dir("/var/bbs/boards/"..board) do if bad_topics[topic] == nil then thread = {} thread.filename = topic thread.directory = "/var/bbs/boards/"..board.."/"..topic _, _, timestamp, thread.author = string.find(topic, "(%d+)-(%a+)") thread.timestamp = tonumber(timestamp) io.input(thread.directory.."/original") io.read("*line") thread.subject = io.read("*line") _, _, thread.subject = string.find(thread.subject, "%[SUBJECT%] (.+)") io.input(io.stdin) posts = get_posts(thread) thread.post_count = #posts thread.updated = 0 for _, post in ipairs(posts) do if post.timestamp > thread.updated then thread.updated = post.timestamp end end table.insert(threads, thread) end end table.sort(threads, function(x,y) return x.updated > y.updated end) return threads end function get_posts(thread) local posts = {} for reply in lfs.dir(thread.directory) do if string.sub(reply, 1,1) ~= "." then post = {} post.filename = thread.directory .. "/" .. reply if reply == "original" then post.author = thread.author post.timestamp = thread.timestamp else _, _, timestamp, post.author = string.find(reply, "(%d+)-(%a+)") post.timestamp = tonumber(timestamp) end table.insert(posts, post) end end table.sort(posts, function(x,y) return x.timestamp < y.timestamp end) return posts end function do_go() print("Which board?") board = string.upper(io.read()) -- TODO: display list of boards only if no board entered if board == "" then do_list() elseif boards[board] == nil then print("No such board") else current_board = board end end function do_help() cat_file("/var/bbs/docs/help") end function do_help2() cat_file("/var/bbs/docs/help2") end function do_unimplemented() print("Sorry, this command is not (yet) implemented!") end function do_list() for _,b in pairs(board_names) do posts = -3 -- Don't want to count "topic" file or "." or ".." for topic in lfs.dir("/var/bbs/boards/"..b) do posts = posts +1 end print(string.format("%s [%d posts]", b, posts)) end end function do_messages() if current_board == "" then print("Not at any board") else threads = get_threads(current_board) for i, thread in ipairs(threads) do print(tostring(i), os.date("%x %H:%M", thread.timestamp), thread.author, thread.subject) --, tostring(thread.post_count)) end end current_thread_index = threads end -- Type stuff below function do_type_first() current_thread_index = 1 do_type_show_post(current_thread_index) print(string.format("Viewing post %d of %d in thread", current_thread_index, #current_thread_posts)) end function do_type_next() if current_thread_index ~= #current_thread_posts then current_thread_index = current_thread_index + 1 end do_type_show_post(current_thread_index) print(string.format("Viewing post %d of %d in thread", current_thread_index, #current_thread_posts)) end function do_type_prev() if current_thread_index ~= 1 then current_thread_index = current_thread_index - 1 end do_type_show_post(current_thread_index) print(string.format("Viewing post %d of %d in thread", current_thread_index, #current_thread_posts)) end function do_type_reply() end type_dispatch = {} type_dispatch["f"] = do_type_first type_dispatch["n"] = do_type_next type_dispatch["p"] = do_type_prev type_dispatch["d"] = function() return end function do_type() print("Which thread?") local thread_id = string.upper(io.read()) thread = current_thread_index[tonumber(thread_id)] print("I've been asked to type the thread: " .. thread.subject) current_thread_posts = get_posts(thread) current_thread_index = #current_thread_posts do_type_show_post(current_thread_index) print(string.format("Viewing post %d of %d in thread", current_thread_index, #current_thread_posts)) -- for i, post in ipairs(posts) do -- print(tostring(i), os.date("%x %H:%M", post.timestamp), post.author) --cat_file(post.filename) -- end repeat show_type_prompt() c = getchar() io.write("\n") if type_dispatch[c] == nil then print("Eh?") else type_dispatch[c]() end until c == "d" end function show_type_prompt() io.write("type prompt ([f]irst, [n]ext, [p]rev, [d]one)>") end function do_type_show_post(index) local post = current_thread_posts[index] cat_file(post.filename) end function do_quit() print("Goodbye!") end function do_rules() cat_file("/var/bbs/docs/rules") end function do_stub() print("Stub!") end function do_who() os.execute("/usr/bin/w") end -- MAIN PROGRAM BODY BELOW show_welcome() boards, board_names = {}, {} for board in lfs.dir("/var/bbs/boards") do if string.sub(board, 1, 1) ~= "." then boards[board] = true table.insert(board_names, board) end end table.sort(board_names) message_index = {} -- Build dispatch table mapping chars to functions dispatch = {} dispatch["h"] = do_help dispatch["g"] = do_go dispatch["l"] = do_list dispatch["m"] = do_messages dispatch["q"] = do_quit dispatch["t"] = do_type dispatch["?"] = do_help2 dispatch["!"] = do_rules dispatch["w"] = do_who dispatch["s"] = do_unimplemented dispatch["="] = do_unimplemented dispatch["M"] = do_unimplemented dispatch["n"] = do_unimplemented dispatch["r"] = do_unimplemented dispatch["b"] = do_unimplemented dispatch["c"] = do_unimplemented -- Infinite loop of command dispatch repeat show_prompt() c = getchar() io.write("\n") if dispatch[c] == nil then print("What?") else dispatch[c]() end until c == "q"