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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
(defpackage :cl-bbs-admin
(:use :cl)
(:export #:main))
(in-package :cl-bbs-admin)
(defun expand-tilde (path)
(if (and (>= (length path) 2) (string= (subseq path 0 2) "~/"))
(concatenate 'string (namestring (user-homedir-pathname)) (subseq path 2))
path))
(defun get-data-dir ()
(let ((env (uiop:getenv "SBBS_DATADIR")))
(if (and env (not (string= env "")))
(namestring (uiop:ensure-absolute-pathname (expand-tilde env) (uiop:getcwd)))
(let ((root-dir (asdf:system-source-directory :cl-bbs/server)))
(if root-dir
(namestring (merge-pathnames "data/" root-dir))
(namestring (merge-pathnames "bbs/" (user-homedir-pathname))))))))
(defun lookup-def (key alist)
(let ((p (assoc key alist :test (lambda (k1 k2) (string-equal (string k1) (string k2))))))
(if p
(let ((res (cdr p)))
(cond ((string-equal (string key) "posts") res)
((not (consp res)) res)
((null (cdr res)) (car res))
(t res)))
nil)))
(defun last-element (lst)
(car (last lst)))
(defun take-right (lst n)
(let ((len (length lst)))
(if (<= len n)
lst
(nthcdr (- len n) lst))))
(defun take (lst n)
(if (or (null lst) (<= n 0))
nil
(cons (car lst) (take (cdr lst) (1- n)))))
(defun latest-posts (posts)
(if (> (length posts) 6)
`((cl-bbs/models:truncated . ,(mapcar #'car (butlast (cdr posts) 5)))
(cl-bbs/models:posts ,(cons (car posts) (take-right posts 5))))
`((cl-bbs/models:truncated . nil) (cl-bbs/models:posts ,posts))))
(defun get-flat-posts (posts)
(when posts
(let* ((cdr-val posts)
(cadr-val (and (listp cdr-val) (car cdr-val))))
(if (and (listp cadr-val) (listp (car cadr-val)))
cadr-val
cdr-val))))
(defun build-list-entry (t-entry)
(let* ((id (car t-entry))
(thread-data (cdr t-entry))
(headline (lookup-def 'cl-bbs/models:headline thread-data))
(posts (get-flat-posts (lookup-def 'cl-bbs/models:posts thread-data)))
(last-post (last-element posts))
(date (lookup-def 'cl-bbs/models:date (cdr last-post)))
(messages (length posts)))
`(,id (cl-bbs/models:headline . ,headline) (cl-bbs/models:date . ,date) (cl-bbs/models:messages . ,messages))))
(defun build-index-entry (t-entry)
(let* ((id (car t-entry))
(thread-data (cdr t-entry))
(headline (lookup-def 'cl-bbs/models:headline thread-data))
(posts (get-flat-posts (lookup-def 'cl-bbs/models:posts thread-data))))
`(,id (cl-bbs/models:headline . ,headline) . ,(latest-posts posts))))
(defun read-sexp-file (path)
(with-open-file (stream path :direction :input :if-does-not-exist nil)
(if stream
(let ((*read-eval* nil))
(read stream nil nil))
nil)))
(defun write-sexp-file (path data)
(with-open-file (stream path :direction :output :if-exists :supersede
:if-does-not-exist :create
:external-format :utf-8)
(write data :stream stream :pretty t)
(terpri stream)))
(defun get-threads (dir)
(let ((threads nil))
(loop for i from 1
for misses = 0 then (if exists 0 (1+ misses))
for filepath = (format nil "~A~D" dir i)
for exists = (probe-file filepath)
while (<= misses 200)
do (when exists
(let ((data (read-sexp-file filepath)))
(when data
(push (cons i data) threads)))))
threads))
(defun generate-index (board)
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(dir (format nil "~A/sexp/~A/" data-dir board))
(threads-data (get-threads dir)))
(unless threads-data
(format t "No threads found for board ~A.~%" board)
(return-from generate-index nil))
(let* ((sorted-threads
(sort threads-data
(lambda (a b)
(let* ((posts-a (get-flat-posts (lookup-def 'cl-bbs/models:posts (cdr a))))
(posts-b (get-flat-posts (lookup-def 'cl-bbs/models:posts (cdr b))))
(date-a (or (lookup-def 'cl-bbs/models:date (cdr (last-element posts-a))) ""))
(date-b (or (lookup-def 'cl-bbs/models:date (cdr (last-element posts-b))) "")))
(string> date-a date-b)))))
(list-entries (mapcar #'build-list-entry sorted-threads))
(frontpage-count 10)
(index-entries (mapcar #'build-index-entry
(if (> (length sorted-threads) frontpage-count)
(take sorted-threads frontpage-count)
sorted-threads)))
(list-path (format nil "~Alist" dir))
(index-path (format nil "~Aindex" dir)))
(write-sexp-file list-path list-entries)
(write-sexp-file index-path index-entries)
(format t "Generated list and index for ~A~%" board))))
(defun get-iso-datetime ()
(multiple-value-bind (second minute hour date month year)
(get-decoded-time)
(format nil "~4,'0D-~2,'0D-~2,'0DT~2,'0D:~2,'0D:~2,'0D"
year month date hour minute second)))
(defun read-backup-metadata (archive-name)
(let ((output (make-string-output-stream)))
(handler-case
(progn
(uiop:run-program (list "tar" "-xzf" archive-name "-O" "metadata.txt")
:output output :error-output nil)
(string-trim '(#\Space #\Tab #\Newline #\Return) (get-output-stream-string output)))
(error () ""))))
(defun list-backups ()
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(backup-dir (format nil "~A/backup" data-dir))
(backups (and (probe-file backup-dir)
(sort (directory (format nil "~A/*.tar.gz" backup-dir))
#'string> :key #'namestring))))
(if backups
(progn
(format t "Available backups:~%")
(dolist (b backups)
(let ((meta (read-backup-metadata (namestring b))))
(format t " ~A ~A~%" (file-namestring b)
(if (and meta (not (string= meta "")))
(format nil "- ~A" meta)
"")))))
(format t "No backups found.~%"))))
(defun ask-confirmation (prompt)
(format t "~A [y/N]: " prompt)
(force-output)
(let ((response (read-line *standard-input* nil "")))
(or (string-equal response "y")
(string-equal response "yes"))))
(defun backup (&optional arg1)
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(backup-dir (format nil "~A/backup" data-dir))
(is-filename (and arg1 (uiop:string-suffix-p arg1 ".tar.gz")))
(message (if is-filename nil arg1))
(archive-name (if is-filename
arg1
(format nil "~A/sbbs-~A.tar.gz" backup-dir (get-iso-datetime))))
(metadata-file (format nil "~A/metadata.txt" backup-dir)))
(ensure-directories-exist (format nil "~A/" backup-dir))
(when message
(with-open-file (stream metadata-file :direction :output :if-does-not-exist :create :if-exists :supersede)
(write-line message stream)))
(if message
(uiop:run-program (list "tar" "-czf" archive-name "-C" data-dir "sexp" "-C" backup-dir "metadata.txt")
:output *standard-output* :error-output *error-output*)
(uiop:run-program (list "tar" "-czf" archive-name "-C" data-dir "sexp")
:output *standard-output* :error-output *error-output*))
(when (and message (probe-file metadata-file))
(delete-file metadata-file))
(format t "Backup created: ~A~%" archive-name)))
(defun restore (&optional archive-name)
(if archive-name
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(backup-dir (format nil "~A/backup" data-dir))
(archive-path (if (probe-file archive-name)
archive-name
(format nil "~A/~A" backup-dir archive-name))))
(if (probe-file archive-path)
(progn
(uiop:run-program (list "tar" "-xzf" archive-path "-C" data-dir)
:output *standard-output* :error-output *error-output*)
(format t "Restore completed from: ~A~%" archive-path))
(format t "Archive not found: ~A~%" archive-name)))
(list-backups)))
(defun remove-post (board post-id)
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(sexp-file (format nil "~A/sexp/~A/~A" data-dir board post-id)))
(if (probe-file sexp-file)
(let* ((data (read-sexp-file sexp-file))
(headline (lookup-def 'cl-bbs/models:headline data))
(posts (get-flat-posts (lookup-def 'cl-bbs/models:posts data)))
(first-post (car posts))
(date (lookup-def 'cl-bbs/models:date (cdr first-post)))
(content (lookup-def 'cl-bbs/models:content (cdr first-post))))
(format t "Post Headline: ~A~%" headline)
(format t "Post Date: ~A~%" date)
(format t "Post Content:~%~A~%" content)
(when (ask-confirmation (format nil "Are you sure you want to remove thread ~A?" post-id))
(backup (format nil "Before removing thread ~A from board ~A" post-id board))
(delete-file sexp-file)
(format t "Removed thread ~A.~%" sexp-file)
(generate-index board)))
(format t "Thread ~A does not exist.~%" sexp-file))))
(defun remove-comment (board post-id comment-id-str)
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(sexp-file (format nil "~A/sexp/~A/~A" data-dir board post-id))
(comment-id (parse-integer comment-id-str :junk-allowed t)))
(unless (probe-file sexp-file)
(format t "Thread ~A does not exist.~%" sexp-file)
(return-from remove-comment nil))
(let* ((data (read-sexp-file sexp-file))
(headline (lookup-def 'cl-bbs/models:headline data))
(posts (get-flat-posts (lookup-def 'cl-bbs/models:posts data)))
(comment (find comment-id posts :key #'car)))
(if (not comment)
(format t "Comment ~A not found in thread ~A.~%" comment-id post-id)
(let ((comment-content (lookup-def 'cl-bbs/models:content (cdr comment)))
(comment-date (lookup-def 'cl-bbs/models:date (cdr comment))))
(format t "Post Headline: ~A~%" headline)
(format t "Comment Date: ~A~%" comment-date)
(format t "Comment Content:~%~A~%" comment-content)
(when (ask-confirmation (format nil "Are you sure you want to remove comment ~A from thread ~A?"
comment-id post-id))
(backup (format nil "Before removing comment ~A from thread ~A on board ~A" comment-id post-id board))
(let ((new-posts (remove-if (lambda (p) (= (car p) comment-id)) posts)))
(if (null new-posts)
(progn
(when (probe-file sexp-file) (delete-file sexp-file))
(format t "Removed thread ~A entirely as it has no remaining comments.~%" post-id))
(progn
(write-sexp-file sexp-file `((cl-bbs/models:headline . ,headline)
(cl-bbs/models:posts ,new-posts)))
(format t "Removed comment ~A from thread ~A.~%" comment-id post-id)))
(generate-index board))))))))
(defun edit-comment (board post-id comment-id-str)
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(sexp-file (format nil "~A/sexp/~A/~A" data-dir board post-id))
(comment-id (parse-integer comment-id-str :junk-allowed t)))
(unless (probe-file sexp-file)
(format t "Thread ~A does not exist.~%" sexp-file)
(return-from edit-comment nil))
(let* ((data (read-sexp-file sexp-file))
(headline (lookup-def 'cl-bbs/models:headline data))
(posts (get-flat-posts (lookup-def 'cl-bbs/models:posts data)))
(comment (find comment-id posts :key #'car)))
(if (not comment)
(format t "Comment ~A not found in thread ~A.~%" comment-id post-id)
(let* ((content-cell (assoc 'cl-bbs/models:content (cdr comment)))
(content-value (cdr content-cell)))
(uiop:with-temporary-file (:pathname tmp-path :keep t)
(with-open-file (stream tmp-path :direction :output :if-exists :supersede
:external-format :utf-8)
(write-line content-value stream))
(let ((editor (or (uiop:getenv "EDITOR") "vi")))
(format t "Opening ~A with ~A...~%" tmp-path editor)
(uiop:run-program (format nil "~A ~A" editor (namestring tmp-path))
:output :interactive
:input :interactive
:error-output :interactive)
(let ((new-content-value (uiop:read-file-string tmp-path)))
(delete-file tmp-path)
(when (and new-content-value (string/= new-content-value ""))
(backup (format nil "Before editing comment ~A from thread ~A on board ~A"
comment-id post-id board))
(let ((new-posts (mapcar (lambda (p)
(if (= (car p) comment-id)
(cons (car p)
(mapcar (lambda (kv)
(if (eq (car kv) 'cl-bbs/models:content)
(cons (car kv)
(string-right-trim '(#\Newline #\Return)
new-content-value))
kv))
(cdr p)))
p))
posts)))
(write-sexp-file sexp-file `((cl-bbs/models:headline . ,headline)
(cl-bbs/models:posts ,new-posts)))
(format t "Edited comment ~A from thread ~A.~%" comment-id post-id)
(generate-index board)))))))))))
(defun get-next-post-id (board)
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(sexp-dir (format nil "~A/sexp/~A/" data-dir board))
(files (and (probe-file sexp-dir)
(uiop:directory-files (uiop:ensure-absolute-pathname sexp-dir (uiop:getcwd)))))
(max-id 0))
(dolist (file files)
(let ((id (handler-case (parse-integer (pathname-name file))
(error () nil))))
(when (and id (> id max-id))
(setf max-id id))))
(1+ max-id)))
(defun move-post (source-board post-id target-board)
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(source-file (format nil "~A/sexp/~A/~A" data-dir source-board post-id))
(target-dir (format nil "~A/sexp/~A/" data-dir target-board)))
(if (probe-file source-file)
(progn
(unless (uiop:directory-exists-p (uiop:ensure-absolute-pathname target-dir (uiop:getcwd)))
(format t "Target board ~A does not exist.~%" target-board)
(return-from move-post nil))
(let* ((new-id (get-next-post-id target-board))
(target-file (format nil "~A~A" target-dir new-id)))
(backup (format nil "Before moving thread ~A from ~A to ~A as ~A" post-id source-board target-board new-id))
(uiop:copy-file source-file target-file)
(delete-file source-file)
(format t "Moved thread ~A from ~A to ~A as thread ~A.~%" post-id source-board target-board new-id)
(generate-index source-board)
(generate-index target-board)))
(format t "Thread ~A does not exist in board ~A.~%" post-id source-board))))
(defun parse-post-date (date-string)
(let ((year (parse-integer date-string :start 0 :end 4))
(month (parse-integer date-string :start 5 :end 7))
(day (parse-integer date-string :start 8 :end 10))
(hour (parse-integer date-string :start 11 :end 13))
(minute (parse-integer date-string :start 14 :end 16)))
(encode-universal-time 0 minute hour day month year 0)))
(defun format-post-date (universal-time)
(multiple-value-bind (second minute hour day month year)
(decode-universal-time universal-time 0)
(declare (ignore second))
(format nil "~4,'0D-~2,'0D-~2,'0D ~2,'0D:~2,'0D" year month day hour minute)))
(defun add-timezone-offset (date-string offset-hours)
(let* ((ut (parse-post-date date-string))
(new-ut (+ ut (* offset-hours 3600))))
(format-post-date new-ut)))
(defun list-all (&optional board post-id-str)
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(sexp-dir (format nil "~A/sexp/" data-dir)))
(cond
((null board)
(let ((boards (and (probe-file sexp-dir)
(uiop:subdirectories (uiop:ensure-absolute-pathname sexp-dir (uiop:getcwd))))))
(if boards
(progn
(format t "Boards:~%")
(dolist (b-path boards)
(format t " ~A~%" (car (last (pathname-directory b-path))))))
(format t "No boards found in ~A~%" sexp-dir))))
((and board post-id-str)
(let ((sexp-file (format nil "~A/sexp/~A/~A" data-dir board post-id-str))
(post-id (parse-integer post-id-str :junk-allowed t)))
(if (and post-id (probe-file sexp-file))
(let* ((data (read-sexp-file sexp-file))
(headline (lookup-def 'cl-bbs/models:headline data))
(posts (get-flat-posts (lookup-def 'cl-bbs/models:posts data))))
(format t "Thread ~A: ~A~%" post-id-str headline)
(dolist (p posts)
(let* ((cid (car p))
(pdata (cdr p))
(date (lookup-def 'cl-bbs/models:date pdata))
(author (lookup-def 'cl-bbs/models:name pdata))
(content (lookup-def 'cl-bbs/models:content pdata)))
(format t "~%[~A] #~A by ~A~%" date cid (or author "Anonymous"))
(format t "~A~%" content))))
(format t "Thread ~A not found in board ~A.~%" post-id-str board))))
(board
(let* ((board-dir (format nil "~A/sexp/~A/" data-dir board))
(threads-data (get-threads board-dir)))
(if threads-data
(let ((sorted (sort threads-data
(lambda (a b)
(let* ((posts-a (get-flat-posts (lookup-def 'cl-bbs/models:posts (cdr a))))
(last-a (last-element posts-a))
(date-a (lookup-def 'cl-bbs/models:date (cdr last-a)))
(ut-a (parse-post-date date-a))
(posts-b (get-flat-posts (lookup-def 'cl-bbs/models:posts (cdr b))))
(last-b (last-element posts-b))
(date-b (lookup-def 'cl-bbs/models:date (cdr last-b)))
(ut-b (parse-post-date date-b)))
(> ut-a ut-b))))))
(progn
(format t "Threads in board ~A:~%" board)
(format t "~10A ~20A ~A~%" "ID" "Last Update" "Headline")
(format t "~v@{~A~:*~}~%" 60 "-")
(dolist (t-data sorted)
(let* ((tid (car t-data))
(t-content (cdr t-data))
(headline (lookup-def 'cl-bbs/models:headline t-content))
(posts (get-flat-posts (lookup-def 'cl-bbs/models:posts t-content)))
(last-date (lookup-def 'cl-bbs/models:date (cdr (last-element posts)))))
(format t "~10D ~20A ~A~%" tid last-date headline)))))
(format t "No threads found in board ~A.~%" board)))))))
(defun set-timezone (offset-hours)
(backup (format nil "Before setting timezone offset to ~D" offset-hours))
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(sexp-dir (format nil "~A/sexp/" data-dir))
(boards (uiop:subdirectories (uiop:ensure-absolute-pathname sexp-dir (uiop:getcwd)))))
(dolist (board-path boards)
(let ((board (car (last (pathname-directory board-path))))
(threads (uiop:directory-files board-path)))
(dolist (thread threads)
(when (handler-case (parse-integer (pathname-name thread))
(error () nil))
(let* ((data (read-sexp-file thread))
(headline (lookup-def 'cl-bbs/models:headline data))
(posts (get-flat-posts (lookup-def 'cl-bbs/models:posts data)))
(new-posts (mapcar (lambda (p)
(let* ((post-id (car p))
(post-data (cdr p))
(old-date (lookup-def 'cl-bbs/models:date post-data))
(new-date (add-timezone-offset old-date offset-hours)))
(cons post-id
(mapcar (lambda (kv)
(if (eq (car kv) 'cl-bbs/models:date)
(cons (car kv) new-date)
kv))
post-data))))
posts)))
(write-sexp-file thread `((cl-bbs/models:headline . ,headline) (cl-bbs/models:posts ,new-posts)))))
(generate-index board))))))
(defun find-duplicates (posts)
(let ((duplicates nil)
(prev-post (car posts)))
(dolist (curr-post (cdr posts))
(let ((prev-content (lookup-def 'cl-bbs/models:content (cdr prev-post)))
(curr-content (lookup-def 'cl-bbs/models:content (cdr curr-post))))
(if (equal prev-content curr-content)
(push curr-post duplicates)
(setf prev-post curr-post))))
(nreverse duplicates)))
(defun remove-duplicates-command ()
(let* ((data-dir (string-right-trim "/" (get-data-dir)))
(sexp-dir (format nil "~A/sexp/" data-dir))
(boards (uiop:subdirectories (uiop:ensure-absolute-pathname sexp-dir (uiop:getcwd))))
(all-duplicates nil))
(dolist (board-path boards)
(let ((board (car (last (pathname-directory board-path))))
(threads (uiop:directory-files board-path)))
(dolist (thread-path threads)
(let ((thread-id (pathname-name thread-path)))
(when (handler-case (parse-integer thread-id)
(error () nil))
(let* ((data (read-sexp-file thread-path))
(posts (get-flat-posts (lookup-def 'cl-bbs/models:posts data)))
(dups (find-duplicates posts)))
(when dups
(push (list board thread-id dups thread-path data) all-duplicates)))))))
(if (null all-duplicates)
(format t "No sequential duplicate comments found.~%")
(progn
(format t "Found sequential duplicate comments:~%~%")
(dolist (entry (reverse all-duplicates))
(destructuring-bind (board thread-id dups thread-path data) entry
(declare (ignore thread-path data))
(format t "Board: ~A, Thread: ~A~%" board thread-id)
(dolist (dup dups)
(let ((comment-id (car dup))
(content (lookup-def 'cl-bbs/models:content (cdr dup))))
(format t " - Duplicate Comment ID: ~A~%" comment-id)
(format t " Content: ~A~%" content)))))
(format t "~%")
(when (ask-confirmation "Are you sure you want to delete these duplicate comments?")
(backup "Before removing sequential duplicate comments")
(let ((affected-boards nil))
(dolist (entry all-duplicates)
(destructuring-bind (board thread-id dups thread-path data) entry
(let* ((headline (lookup-def 'cl-bbs/models:headline data))
(posts (get-flat-posts (lookup-def 'cl-bbs/models:posts data)))
(dup-ids (mapcar #'car dups))
(new-posts (remove-if (lambda (p) (member (car p) dup-ids)) posts)))
(write-sexp-file thread-path `((cl-bbs/models:headline . ,headline)
(cl-bbs/models:posts ,new-posts)))
(pushnew board affected-boards :test #'string-equal)
(format t "Removed ~D duplicates from ~A/~A~%" (length dups) board thread-id))))
(dolist (board affected-boards)
(generate-index board)))))))))
(defun print-help ()
(format t "Usage: cl-bbs-admin <command> [args...]~%~%")
(format t "Commands:~%")
(format t " generate-index <board> Regenerate index and list files for a board~%")
(format t " list [board] [post-id] List boards, threads in a board, or comments in a thread~%")
(format t " remove-post <board> <post-id> Remove an entire thread/post~%")
(format t " remove-duplicates Remove sequential duplicate comments across all boards~%")
(format t " remove-comment <board> <post-id> <comment-id> Remove a specific comment from a thread~%")
(format t " edit <board> <post-id> <comment-id> Edit a specific comment using $EDITOR~%")
(format t " move <source-board> <post-id> <target-board> Move a thread to a different board~%")
(format t " backup [\"description msg\"] Backup the sexp directory to a tarball~%")
(format t " restore [archive.tar.gz] Restore or list backups~%")
(format t " set-timezone <int> Offset all posts timezone and recreate indices~%~%")
(format t "Environment Variables:~%")
(format t " SBBS_DATADIR Path to data directory (default: project data/)~%"))
(defun main (&rest argv)
"Main entry point for cl-bbs-admin command line interface, parsing and dispatching command line arguments ARGV."
(handler-case
(if (null argv)
(print-help)
(let ((cmd (first argv)))
(cond
((string= cmd "list")
(list-all (second argv) (third argv)))
((string= cmd "generate-index")
(if (>= (length argv) 2)
(generate-index (second argv))
(format t "Usage: cl-bbs-admin generate-index <board>~%")))
((string= cmd "remove-post")
(if (>= (length argv) 3)
(remove-post (second argv) (third argv))
(format t "Usage: cl-bbs-admin remove-post <board> <post-id>~%")))
((string= cmd "remove-duplicates")
(remove-duplicates-command))
((string= cmd "remove-comment")
(if (>= (length argv) 4)
(remove-comment (second argv) (third argv) (fourth argv))
(format t "Usage: cl-bbs-admin remove-comment <board> <post-id> <comment-id>~%")))
((string= cmd "edit")
(if (>= (length argv) 4)
(edit-comment (second argv) (third argv) (fourth argv))
(format t "Usage: cl-bbs-admin edit <board> <post-id> <comment-id>~%")))
((string= cmd "move")
(if (>= (length argv) 4)
(move-post (second argv) (third argv) (fourth argv))
(format t "Usage: cl-bbs-admin move <source-board> <post-id> <target-board>~%")))
((string= cmd "backup")
(if (>= (length argv) 2)
(backup (format nil "~{~A~^ ~}" (cdr argv)))
(backup)))
((string= cmd "restore")
(if (>= (length argv) 2)
(restore (second argv))
(restore)))
((string= cmd "set-timezone")
(if (>= (length argv) 2)
(let ((offset (parse-integer (second argv) :junk-allowed t)))
(if offset
(set-timezone offset)
(format t "Invalid timezone offset: ~A~%" (second argv))))
(format t "Usage: cl-bbs-admin set-timezone <int>~%")))
(t
(format t "Unknown command: ~A~%" cmd)
(print-help)))))
(#+sbcl sb-sys:interactive-interrupt
#+ccl ccl:interrupt-condition
#+clisp system:simple-interrupt-condition
#+ecl ext:interactive-interrupt
#+allegro excl:interrupt-signal
()
(progn
(format *error-output* "~&Interrupted.~%")
(uiop:quit 1)))))
|