aboutsummaryrefslogtreecommitdiff
path: root/telem.lua
blob: 7ecf1fa85dfe874e08bf5b4a0cdefc60ed628f7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/lua5.2
telemver = "0.3 // 2018-05-04"

file = require("pl.file")
io = require("io")
lfs = require("lfs")
os = require("os")
path = require("pl.path")
string = require("string")
table = require("table")

_BBS_ROOT = "/var/bbs/"

bad_topics = {}
bad_topics["."] = true
bad_topics[".."] = true
bad_topics["topic"] = true

-- This is, obvviously, not secure and will need to be updated
username = os.getenv("USER")

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(path.join(_BBS_ROOT, "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(path.join(_BBS_ROOT, "boards", board)) do
		if bad_topics[topic] == nil then
			thread = {}
			thread.filename = topic
			thread.directory = path.join(_BBS_ROOT, "boards", board, topic)
			_, _, timestamp, thread.author = string.find(topic, "(%d+)-(%a+)")
			thread.timestamp = tonumber(timestamp)

			io.input(thread.directory.."/subject")
			thread.subject = io.read("*line")
			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 goto continue end
		if reply == "subject" then goto continue end
		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)
		::continue::
	end
	table.sort(posts, function(x,y) return x.timestamp < y.timestamp end)
	return posts
end

function do_board()
	-- Creates a new (empty) board

	-- Get details
	print("New board name?  Max 19 chars") -- TODO: Check and enforce 19 chars
	board = string.upper(io.read())
	print("Description?")
	desc = io.read()
	-- Create directory
	board_dir = path.join(_BBS_ROOT, "boards", board)
	lfs.mkdir(board_dir)
	-- Write topic file
	topic_file = path.join(board_dir, "topic")
	file.write(topic_file, desc)
	-- Update representation of BBS
	boards[board] = true
	table.insert(board_names, board)
	table.sort(board_names)
	-- Done!
	print("Board created.")
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(path.join(_BBS_ROOT, "docs", "help"))
end

function do_help2()
	cat_file(path.join(_BBS_ROOT, "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(path.join(_BBS_ROOT, "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

function do_new()
	if current_board == "" then
		print("Not at any board")
		return
	end
	-- Get subject for new thread
	print("Subject?")
	subject = io.read()
	-- Save body of post to temp file
	filename = os.tmpname()
	os.execute("$EDITOR " .. filename)
	-- TODO: show and confirm
	-- Make thread dir
	timestamp = tostring(os.time())
	thread_dir = timestamp .. "-" .. username
	thread_path = path.join(_BBS_ROOT, "boards", current_board, thread_dir)
	lfs.mkdir(thread_path)
	-- Write subject file
	file.write(path.join(thread_path, "subject"),
		subject)
	-- Move post file
	post_file = thread_dir -- first post and thread directory names are the same!
	newpath = path.join(thread_path, post_file)
	ret, str = file.move(filename, newpath)
	if not ret then
		print(str)
	end
	-- Done!
	print("Post submitted.")
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()
	filename = os.tmpname()
	os.execute("$EDITOR " .. filename)
	timestamp = tostring(os.time())
	newfilename = timestamp .. "-" .. username
	newpath = path.join(thread.directory, newfilename)
	ret, str = file.move(filename, newpath)
	if not ret then
		print(str)
	end
	current_thread_posts = get_posts(thread)
	current_thread_index = #current_thread_posts
	do_type_show_post(current_thread_index)
end

type_dispatch = {}
type_dispatch["f"] = do_type_first
type_dispatch["n"] = do_type_next
type_dispatch["p"] = do_type_prev
type_dispatch["r"] = do_type_reply
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_subject = 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("[f]irst, [n]ext, [p]rev, [r]eply, [d]one >")
end

function do_type_show_post(index)
	local post = current_thread_posts[index]
	print("SUBJECT:    " .. current_thread_subject)
	print("AUTHOR:     " .. post.author)
	print("POSTED:     " .. os.date("%H:%M %B %d, %Y", post.timestamp))
	print("--------------------------------")
	cat_file(post.filename)
	print("--------------------------------")
end

function do_quit()
	print("Goodbye!")
end

function do_reply()
	if current_board == "" then
		print("Not at any board!")
		return
	end
	print("Reply to which thread?")
	local thread_id = string.upper(io.read())
	thread = current_thread_index[tonumber(thread_id)]
	current_thread_subject = thread.subject
	current_thread_posts = get_posts(thread)
	current_thread_index = #current_thread_posts
	do_type_reply()
end

function do_rules()
	cat_file(path.join(_BBS_ROOT, "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(path.join(_BBS_ROOT, "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_new
dispatch["r"] = do_reply
dispatch["b"] = do_board
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"