-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpicoengine.lua
1288 lines (1088 loc) · 45.2 KB
/
picoengine.lua
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Picochan Backend.
-- HAPAS ARE MENTALLY ILL DEGENERATES
local sqlite3 = require("lib.sqlite3")
local sha = require("lib.sha")
local argon2 = require("lib.argon2")
local random = require("lib.random")
require("lib.stringext")
local pico = {}
pico.global = {}
pico.account = {}
pico.board = {}
pico.board.stats = {}
pico.board.banner = {}
pico.file = {}
pico.post = {}
pico.thread = {}
pico.log = {}
pico.captcha = {}
pico.webring = {}
pico.webring.endpoint = {}
--
-- INITIALIZATION
--
function pico.initialize()
db = assert(sqlite3.open("picochan.db", "w"))
db:e("PRAGMA busy_timeout = 10000")
db:e("PRAGMA foreign_keys = ON")
db:e("PRAGMA recursive_triggers = ON")
db:e("PRAGMA secure_delete = ON")
db:e("PRAGMA case_sensitive_like = ON")
end
function pico.finalize()
db:close()
end
--
-- ACCOUNT MANAGEMENT FUNCTIONS
--
pico.account.current = nil
local function valid_account_name(name)
return type(name) == "string" and #name <= 16 and #name >= 1 and not name:match("[^%w]")
end
local function valid_account_type(type)
return type == "admin" or type == "gvol" or type == "bo" or type == "lvol"
end
local function valid_account_password(password)
return type(password) == "string" and #password >= 6 and #password <= 128
end
-- permclass is a space-separated list of one or more of the following:
-- admin gvol bo lvol
-- targettype may be one of the following:
-- acct board post
local function permit(permclass, targettype, targarg)
-- STEP 1. Check account type
if not pico.account.current then
return false, "Action not permitted (not logged in)"
elseif not permclass:match(pico.account.current.Type) then
return false, "Action not permitted (account type not authorized)"
end
-- STEP 2. Check targets
-- If no target, stop here
if not targettype then
return true
end
-- Special case: Admin can modify any target
if pico.account.current.Type == "admin" then
return true
end
if targettype == "acct" then
-- Special case: Anyone can modify their own account (password change)
if pico.account.current.Name == targarg then
return true
end
if pico.account.current.Type == "gvol" or pico.account.current.Type == "lvol" then
return false, "Action not permitted (account type not authorized)"
elseif pico.account.current.Type == "bo" then
local board = db:r1("SELECT Board FROM Accounts WHERE Name = ?", targarg)
if board == pico.account.current.Board then
return true
else
return false, "Action not permitted (attempt to modify account outside assigned board)"
end
end
elseif targettype == "board" then
if pico.account.current.Type == "gvol" or pico.account.current.Type == "lvol" then
return false, "Action not permitted (account type not authorized)"
elseif pico.account.current.Type == "bo" then
if targarg == pico.account.current.Board then
return true
else
return false, "Action not permitted (attempt to modify non-assigned board)"
end
end
elseif targettype == "post" then
if pico.account.current.Type == "gvol" then
return true
elseif (pico.account.current.Type == "bo")
or (pico.account.current.Type == "lvol") then
if targarg == pico.account.current.Board then
return true
else
return false, "Action not permitted (attempt to modify post outside assigned board)"
end
end
end
return false, "Action not permitted (unclassified denial: THIS IS A BUG, REPORT TO ADMINISTRATOR)"
end
function pico.account.create(name, password, type, board)
local auth, msg = permit("admin bo", "board", board)
if not auth then return auth, msg end
if not valid_account_name(name) then
return false, "Account name is invalid"
elseif not valid_account_type(type) then
return false, "Account type is invalid"
elseif not valid_account_password(password) then
return false, "Account password does not meet requirements"
elseif pico.account.exists(name) then
return false, "Account already exists"
elseif (type == "bo" or type == "lvol") then
if not board then
return false, "Board was not specified, but the account type requires it"
elseif not pico.board.exists(board) then
return false, "Account's specified board does not exist"
end
end
if type == "admin" or type == "gvol" then
board = nil
end
db:e("INSERT INTO Accounts (Name, Type, Board, PwHash) VALUES (?, ?, ?, ?)",
name, type, board, argon2.digest(password))
pico.log.insert(board, "Created new %s account '%s'", type, name)
return true, "Account created successfully"
end
function pico.account.delete(name, reason)
local auth, msg = permit("admin bo", "acct", name)
if not auth then return auth, msg end
local account_tbl = db:r("SELECT Type, Board FROM Accounts WHERE Name = ?", name)
if not account_tbl then
return false, "Account does not exist"
end
db:e("DELETE FROM Accounts WHERE Name = ?", name)
pico.log.insert(account_tbl.Board, "Deleted a %s account '%s' for reason: %s",
account_tbl.Type, name, reason)
return true, "Account deleted successfully"
end
function pico.account.changepass(name, password)
local auth, msg = permit("admin gvol bo lvol", "acct", name)
if not auth then return auth, msg end
local account_tbl = db:r("SELECT Board FROM Accounts WHERE Name = ?", name)
if not account_tbl then
return false, "Account does not exist"
elseif not valid_account_password(password) then
return false, "Account password does not meet requirements"
end
db:e("UPDATE Accounts SET PwHash = ? WHERE Name = ?",
argon2.digest(password), name)
pico.log.insert(account_tbl.Board, "Changed password of account '%s'", name)
return true, "Account password changed successfully"
end
-- log in an account. returns an authentication key which you can use to perform
-- mod-only actions.
function pico.account.login(name, password)
if not pico.account.exists(name)
or not argon2.verify(password, db:r1("SELECT PwHash FROM Accounts WHERE Name = ?", name)) then
return nil, "Invalid username or password"
end
local key = random.string(16)
db:e("INSERT INTO Sessions (Key, Account) VALUES (?, ?)", key, name)
pico.account.register_login(key)
return key
end
-- populate the account table using an authentication key (perhaps provided by a
-- session cookie, or by pico.account.login() above)
function pico.account.register_login(key)
if pico.account.current then
pico.account.logout(key)
end
pico.account.current = db:r("SELECT * FROM Accounts WHERE Name = (SELECT Account FROM Sessions " ..
"WHERE Key = ? AND ExpireDate > STRFTIME('%s', 'now'))", key)
db:e("UPDATE Sessions SET ExpireDate = STRFTIME('%s', 'now') + 86400 WHERE Key = ?", key)
end
function pico.account.logout(key)
if not pico.account.current then
return false, "No account logged in"
end
db:e("DELETE FROM Sessions WHERE Key = ?", key)
return true, "Account logged out successfully"
end
function pico.account.list()
return db:q1("SELECT Name FROM Accounts")
end
function pico.account.exists(name)
return db:b("SELECT TRUE FROM Accounts WHERE Name = ?", name)
end
--
-- GLOBAL CONFIGURATION FUNCTIONS
--
-- retrieve value of globalconfig variable or the default value if it doesn't exist
function pico.global.get(name, default)
local value = db:r1("SELECT Value FROM GlobalConfig WHERE Name = ?", name)
if value ~= nil then
return value
end
return default
end
-- setting a globalconfig variable to nil removes it.
function pico.global.set(name, value)
local auth, msg = permit("admin")
if not auth then return auth, msg end
db:e("DELETE FROM GlobalConfig WHERE Name = ?", name)
if value ~= nil then
db:e("INSERT INTO GlobalConfig VALUES (?, ?)", name, value)
end
pico.log.insert(nil, "Edited global configuration variable '%s'", name)
return true, "Global configuration modified"
end
--
-- BOARD MANAGEMENT FUNCTIONS
--
local function valid_board_name(name)
return type(name) == "string" and #name >= 1 and #name <= 8 and not name:match("[^%l%d]")
end
local function valid_board_title(title)
return type(title) == "string" and #title >= 1 and #title <= 32
end
local function valid_board_subtitle(subtitle)
return type(subtitle) == "string" and #subtitle >= 1 and #subtitle <= 64
end
function pico.board.create(name, title, subtitle)
local auth, msg = permit("admin")
if not auth then return auth, msg end
if pico.board.exists(name) then
return false, "Board already exists"
elseif not valid_board_name(name) then
return false, "Invalid board name"
elseif not valid_board_title(name) then
return false, "Invalid board title"
elseif subtitle and not valid_board_subtitle(subtitle) then
return false, "Invalid board subtitle"
end
db:e("INSERT INTO Boards (Name, Title, Subtitle) VALUES (?, ?, ?)",
name, title, subtitle)
pico.log.insert(nil, "Created a new board: /%s/ - %s", name, title)
return true, "Board created successfully"
end
function pico.board.delete(name, reason)
local auth, msg = permit("admin")
if not auth then return auth, msg end
if not pico.board.exists(name) then
return false, "Board does not exist"
end
db:e("DELETE FROM Boards WHERE Name = ?", name)
pico.log.insert(nil, "Deleted board /%s/ for reason: %s", name, reason)
pico.file.clean()
return true, "Board deleted successfully"
end
function pico.board.list()
return db:q("SELECT Name, Title, Subtitle FROM Boards ORDER BY DisplayOverboard DESC, MaxPostNumber DESC")
end
function pico.board.exists(name)
return db:b("SELECT TRUE FROM Boards WHERE Name = ?", name)
end
function pico.board.tbl(name)
return db:r("SELECT * FROM Boards WHERE Name = ?", name)
end
function pico.board.configure(board_tbl)
local auth, msg = permit("admin bo", "board", board_tbl.Name)
if not auth then return auth, msg end
if not board_tbl then
return false, "Board configuration not supplied"
elseif not pico.board.exists(board_tbl.Name) then
return false, "Board does not exist"
end
db:e("UPDATE Boards SET Title = ?, Subtitle = ?, Lock = ?, DisplayOverboard = ?, " ..
"PostMaxFiles = ?, ThreadMinLength = ?, PostMaxLength = ?, PostMaxNewlines = ?, " ..
"PostMaxDblNewlines = ?, TPHLimit = ?, PPHLimit = ?, ThreadCaptcha = ?, " ..
"PostCaptcha = ?, CaptchaTriggerTPH = ?, CaptchaTriggerPPH = ?, " ..
"BumpLimit = ?, PostLimit = ?, ThreadLimit = ? WHERE Name = ?",
board_tbl.Title, board_tbl.Subtitle,
board_tbl.Lock or 0, board_tbl.DisplayOverboard or 0,
board_tbl.PostMaxFiles, board_tbl.ThreadMinLength,
board_tbl.PostMaxLength, board_tbl.PostMaxNewlines,
board_tbl.PostMaxDblNewlines, board_tbl.TPHLimit,
board_tbl.PPHLimit, board_tbl.ThreadCaptcha or 0,
board_tbl.PostCaptcha or 0, board_tbl.CaptchaTriggerTPH,
board_tbl.CaptchaTriggerPPH, board_tbl.BumpLimit,
board_tbl.PostLimit, board_tbl.ThreadLimit,
board_tbl.Name)
pico.log.insert(board_tbl.Name, "Modified board configuration")
return true, "Board configured successfully"
end
function pico.board.catalog(name, page)
if name and not pico.board.exists(name) then
return nil, nil, "Board does not exist"
end
page = tonumber(page) or 1
local where = name and "Threads.Board = ? "
or "Threads.Board IN (SELECT Name FROM Boards WHERE DisplayOverboard) "
local sql = "SELECT Posts.*, LastBumpDate, Sticky, Lock, Autosage, Cycle, ReplyCount, File, Spoiler, Width AS FileWidth, Height AS FileHeight " ..
"FROM Threads JOIN Posts USING(Board, Number) LEFT JOIN FileRefs USING(Board, Number) LEFT JOIN Files ON FileRefs.File = Files.Name " ..
"WHERE (Sequence = 1 OR Sequence IS NULL) AND " ..
where ..
"ORDER BY " ..
(name and "Sticky DESC, LastBumpDate DESC, Threads.Number DESC "
or "LastBumpDate DESC ") ..
"LIMIT ? OFFSET ?"
local pagecount_sql = "SELECT ((COUNT(*) - 1) / CAST(? AS INTEGER)) + 1 FROM Threads WHERE " .. where
local catalog_tbl, pagecount
if name then
local pagesize = pico.global.get("catalogpagesize", 1000)
catalog_tbl = db:q(sql, name, pagesize, (page - 1) * pagesize)
pagecount = db:r1(pagecount_sql, pagesize, name)
else
local pagesize = pico.global.get("overboardpagesize", 100)
catalog_tbl = db:q(sql, pagesize, (page - 1) * pagesize)
pagecount = db:r1(pagecount_sql, pagesize)
end
return catalog_tbl, pagecount
end
function pico.board.index(name, page)
if name and not pico.board.exists(name) then
return nil, nil, "Board does not exist"
end
page = tonumber(page) or 1
local pagesize = pico.global.get("indexpagesize", 10)
local threadpagesize = pico.global.get("threadpagesize", 50)
local windowsize = pico.global.get("indexwindowsize", 5)
local where = name and "WHERE Board = ? " or ""
local sql = "SELECT Board, Number FROM Threads " ..
where ..
"ORDER BY " ..
(name and "Sticky DESC, LastBumpDate DESC, Threads.Number DESC "
or "LastBumpDate DESC ") ..
"LIMIT ? OFFSET ?"
local pagecount_sql = "SELECT ((COUNT(*) - 1) / CAST(? AS INTEGER)) + 1"
local thread_ops, pagecount
if name then
thread_ops = db:q(sql, name, pagesize, (page - 1) * pagesize)
pagecount = db:r1(pagecount_sql .. " FROM Threads " .. where, pagesize, name)
else
thread_ops = db:q(sql, pagesize, (page - 1) * pagesize)
pagecount = db:r1(pagecount_sql .. " FROM Threads " .. where, pagesize)
end
local index_tbl = {}
for i = 1, #thread_ops do
local op_tbl = thread_ops[i]
local thread_tbl = db:q("SELECT Posts.*, LastBumpDate, Sticky, Lock, Autosage, Cycle, ReplyCount, " ..
"IIF(ReplyCount > ?, ReplyCount - ?, 0) AS RepliesOmitted, (" ..
pagecount_sql .. " FROM Posts WHERE Board = Threads.Board AND Parent = Threads.Number) AS PageCount " ..
"FROM Threads JOIN Posts USING(Board, Number) " ..
"WHERE Board = ? AND Number = ? " ..
"UNION ALL " ..
"SELECT * FROM " ..
"(SELECT *, " ..
"NULL AS LastBumpDate, NULL AS Sticky, NULL AS Lock, " ..
"NULL AS Autosage, NULL AS Cycle, NULL AS ReplyCount, " ..
"NULL AS RepliesOmitted, NULL AS PageCount " ..
"FROM Posts " ..
"WHERE Board = ? AND Parent = ? ORDER BY Number DESC LIMIT ?) " ..
"ORDER BY Number ASC",
windowsize, windowsize, threadpagesize,
op_tbl.Board, op_tbl.Number,
op_tbl.Board, op_tbl.Number, windowsize)
for j = 1, #thread_tbl do
thread_tbl[j].Files = pico.file.list(thread_tbl[j].Board, thread_tbl[j].Number)
end
index_tbl[i] = thread_tbl
end
return index_tbl, pagecount
end
function pico.board.recent(name, page)
if name and not pico.board.exists(name) then
return nil, nil, "Board does not exist"
end
page = tonumber(page) or 1
local pagesize = pico.global.get("recentpagesize", 50)
local where = name and "WHERE Board = ? " or ""
local sql = "SELECT * FROM Posts " .. where .. "ORDER BY Date DESC LIMIT ? OFFSET ?"
local pagecount_sql = "SELECT ((COUNT(*) - 1) / CAST(? AS INTEGER)) + 1 FROM Posts " .. where
local recent_tbl, pagecount
if name then
recent_tbl = db:q(sql, name, pagesize, (page - 1) * pagesize)
pagecount = db:r1(pagecount_sql, pagesize, name)
else
recent_tbl = db:q(sql, pagesize, (page - 1) * pagesize)
pagecount = db:r1(pagecount_sql, pagesize)
end
for i = 1, #recent_tbl do
local post_tbl = recent_tbl[i]
post_tbl.Files = pico.file.list(post_tbl.Board, post_tbl.Number)
end
return recent_tbl, pagecount
end
function pico.board.banner.get(board)
if not pico.board.exists(board) then
return nil, "Board does not exist"
end
local file = db:r1("SELECT File FROM Banners WHERE Board = ? ORDER BY RANDOM() LIMIT 1", board)
if not file then
return nil, "Banner does not exist"
end
return file
end
function pico.board.banner.list(board)
if not pico.board.exists(board) then
return nil, "Board does not exist"
end
return db:q1("SELECT File FROM Banners WHERE Board = ?", board)
end
function pico.board.banner.exists(board, file)
return db:b("SELECT TRUE FROM Banners WHERE Board = ? AND File = ?", board, file)
end
function pico.board.banner.add(board, file)
local auth, msg = permit("admin bo", "board", board)
if not auth then return auth, msg end
if not pico.board.exists(board) then
return false, "Board does not exist"
elseif not pico.file.exists(file) then
return false, "File does not exist"
elseif pico.board.banner.exists(board, file) then
return false, "Banner already exists"
end
db:e("INSERT INTO Banners (Board, File) VALUES (?, ?)", board, file)
pico.log.insert(board, "Added banner %s", file)
return true, "Banner added successfully"
end
function pico.board.banner.delete(board, file, reason)
local auth, msg = permit("admin bo", "board", board)
if not auth then return auth, msg end
if not pico.board.exists(board) then
return false, "Board does not exist"
elseif not pico.file.exists(file) then
return false, "File does not exist"
elseif not pico.board.banner.exists(board, file) then
return false, "Banner does not exist"
end
db:e("DELETE FROM Banners WHERE Board = ? AND File = ?", board, file)
pico.log.insert(board, "Deleted banner %s for reason: %s", file, reason)
pico.file.clean()
return true, "Banner deleted successfully"
end
-- To get number of posts per hour over the last 12 hours:
-- * interval = 1 (hour)
-- * intervals = 12 (12 hours)
-- To get number of posts per day over 1 week:
-- * interval = 24 (hours)
-- * intervals = 7 (7 * 24 hours = 1 week)
function pico.board.stats.threadrate(board, interval, intervals)
return math.ceil(db:r1("SELECT (COUNT(*) / ?) FROM Posts WHERE Board = ? AND Parent IS NULL AND Date > (STRFTIME('%s', 'now') - (? * 3600))",
intervals, board, interval * intervals))
end
function pico.board.stats.postrate(board, interval, intervals)
return math.ceil(db:r1("SELECT (COUNT(*) / ?) FROM Posts WHERE Board = ? AND Date > (STRFTIME('%s', 'now') - (? * 3600))",
intervals, board, interval * intervals))
end
function pico.board.stats.totalposts(board)
return db:r1("SELECT MaxPostNumber FROM Boards WHERE Name = ?", board)
end
function pico.board.stats.lastbumpdate(board)
return db:r1("SELECT MAX(LastBumpDate) FROM Threads WHERE Board = ?", board)
end
--
-- FILE MANAGEMENT FUNCTIONS
--
-- return a file's extension based on its contents
local function identify_file(data)
if not data or #data == 0 then
return nil
end
if data:sub(1,8) == "\x89PNG\x0D\x0A\x1A\x0A" then
return "png"
elseif data:sub(1,3) == "\xFF\xD8\xFF" then
return "jpg"
elseif data:sub(1,6) == "GIF87a"
or data:sub(1,6) == "GIF89a" then
return "gif"
elseif data:sub(1,4) == "RIFF"
and data:sub(9,12) == "WEBP" then
return "webp"
elseif data:sub(1,4) == "\x1A\x45\xDF\xA3" then
return "webm"
elseif data:sub(5,12) == "ftypmp42"
or data:sub(5,12) == "ftypisom" then
return "mp4"
elseif data:sub(1,2) == "\xFF\xFB"
or data:sub(1,3) == "ID3" then
return "mp3"
elseif data:sub(1,4) == "OggS" then
return "ogg"
elseif data:sub(1,4) == "fLaC" then
return "flac"
elseif data:sub(1,4) == "%PDF" then
return "pdf"
elseif data:sub(1,4) == "\x25\x21\x50\x53" then
return "ps"
elseif data:sub(1,4) == "PK\x03\x04"
and data:sub(31,58) == "mimetypeapplication/epub+zip" then
return "epub"
elseif data:sub(1,3) == "\x1F\x8B\x08" then
return "gz"
elseif data:sub(1,3) == "BZh" then
return "bz2"
elseif data:sub(1,5) == "\xFD7zXZ" then
return "xz"
elseif data:sub(1,4) == "\x04\x22\x4D\x18" then
return "lz4"
elseif data:sub(1,4) == "\x28\xB5\x2F\xFD" then
return "zst"
elseif data:sub(258,262) == "ustar" then
return "tar"
elseif data:sub(1,4) == "PK\x03\x04" then
return "zip"
elseif data:sub(1,6) == "7z\xBC\xAF\x27\x1C" then
return "7z"
elseif data:sub(1,6) == "Rar!\x1A\x07" then
return "rar"
elseif data:find("DOCTYPE svg", 1, true)
or data:find("<svg", 1, true) then
return "svg"
elseif not data:find("[^%w%s%p]") then
return "txt"
end
return nil
end
-- return a file's extension based on its name
function pico.file.extension(filename)
return filename:match("%.([^.]-)$")
end
-- return a file's media type based on its extension
function pico.file.class(extension)
local lookup = {
["png"] = "image",
["jpg"] = "image",
["gif"] = "image",
["webp"] = "image",
["svg"] = "image",
["webm"] = "video",
["mp4"] = "video",
["mp3"] = "audio",
["ogg"] = "audio",
["flac"] = "audio",
["pdf"] = "document",
["ps"] = "document",
["epub"] = "document",
["txt"] = "document",
["gz"] = "archive",
["bz2"] = "archive",
["xz"] = "archive",
["lz4"] = "archive",
["zst"] = "archive",
["tar"] = "archive",
["zip"] = "archive",
["7z"] = "archive",
["rar"] = "archive",
}
return lookup[extension] or extension
end
-- Add a file to the media directory and return its hash reference.
-- Also add its information to the database.
function pico.file.add(f)
local size = assert(f:seek("end"))
if size > pico.global.get("maxfilesize", 16777216) then
f:close()
return nil, "File too large"
end
assert(f:seek("set"))
local data = assert(f:read("*a"))
f:close()
local extension = identify_file(data)
if not extension then
return nil, "Could not identify file type"
end
local class = pico.file.class(extension)
local hash = sha.hash("sha256", data)
local filename = hash .. "." .. extension
if pico.file.exists(filename) then
return filename, "File already existed and was not changed"
end
local newf = assert(io.open("Media/" .. filename, "w"))
assert(newf:write(data))
newf:close()
local p, width, height
if class == "video" or (class == "audio" and os.execute("exec ffmpeg -v quiet -i Media/" .. filename .. " -map 0:v:0 -f image2 - >/dev/null")) then
local ffmpeg = "ffmpeg -v quiet -i Media/" .. filename ..
(class == "video" and " -ss 00:00:00.500 -frames:v 1 -f image2 -"
or " -map 0:v:0 -f image2 -")
os.execute(ffmpeg .. " | magick - -filter Catrom -strip -thumbnail 200x200 jpg:Media/thumb/" .. filename)
os.execute(ffmpeg .. " | magick - -filter Catrom -quality 60 -strip -thumbnail 100x70 jpg:Media/icon/" .. filename)
p = io.popen("exec ffprobe -v quiet -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 " ..
"Media/" .. filename, "r")
elseif class == "image" or extension == "pdf" or extension == "ps" then
local prefix = (extension == "pdf" or extension == "ps" or extension == "svg") and "png:" or ""
local frame = (extension == "pdf" or extension == "ps") and "[0]" or ""
os.execute("exec magick Media/" .. filename .. frame ..
" -filter Catrom -strip -coalesce -thumbnail 200x200 " .. prefix .. "Media/thumb/" .. filename)
os.execute("exec magick Media/" .. filename ..
"[0] -filter Catrom -quality 60 -strip -coalesce -thumbnail 100x70 " .. prefix .. "Media/icon/" .. filename)
p = io.popen("exec magick identify -format '%wx%h' Media/" .. filename .. "[0]", "r")
end
if p then
local dimensions = p:read("*a"):tokenize("x")
p:close()
width, height = tonumber(dimensions[1]), tonumber(dimensions[2])
end
if (not width) or (not height) then
width, height = nil, nil
end
db:e("INSERT INTO Files VALUES (?, ?, ?, ?)", filename, size, width, height)
return filename, "File added successfully"
end
-- Delete a file from the media directory and remove its corresponding entries
-- in the database.
function pico.file.delete(filename, reason)
local auth, msg = permit("admin gvol")
if not auth then return auth, msg end
if not pico.file.exists(filename) then
return false, "File does not exist"
end
db:e("DELETE FROM Files WHERE Name = ?", filename)
os.remove("Media/" .. filename)
os.remove("Media/icon/" .. filename)
os.remove("Media/thumb/" .. filename)
pico.log.insert(nil, "Deleted file %s from all boards for reason: %s", filename, reason)
return true, "File deleted successfully"
end
function pico.file.clean()
local files = db:q1("SELECT Name FROM Files EXCEPT SELECT File FROM FileRefs EXCEPT SELECT File FROM Banners")
for i = 1, #files do
local file = files[i]
db:e("DELETE FROM Files WHERE Name = ?", file)
os.remove("Media/" .. file)
os.remove("Media/icon/" .. file)
os.remove("Media/thumb/" .. file)
end
end
function pico.file.list(board, number)
return db:q("SELECT Files.*, FileRefs.Name AS DownloadName, Spoiler " ..
"FROM FileRefs JOIN Files ON FileRefs.File = Files.Name " ..
"WHERE Board = ? AND Number = ? ORDER BY Sequence ASC",
board, number)
end
function pico.file.exists(name)
return db:b("SELECT TRUE FROM Files WHERE Name = ?", name)
end
function pico.file.create_refs(board, number, files)
if files then
for i = 1, #files do
local file = files[i]
if file.Hash and file.Hash ~= "" then
db:e("INSERT INTO FileRefs VALUES (?, ?, ?, ?, ?, ?)", board, number, file.Hash, file.Name, file.Spoiler, i)
end
end
end
end
--
-- POST ACCESS, CREATION AND DELETION FUNCTIONS
--
function pico.post.tbl(board, number, omit_files)
local post_tbl = db:r("SELECT * FROM Posts LEFT JOIN Threads USING(Board, Number) WHERE Board = ? AND Number = ?", board, number)
if post_tbl and not omit_files then
post_tbl.Files = pico.file.list(board, number)
end
return post_tbl
end
-- Return list of posts which >>reply to the specified post.
function pico.post.refs(board, number)
return db:q1("SELECT Referrer FROM Refs WHERE Board = ? AND Referee = ?", board, number)
end
-- Create a post and return its number
-- 'files' is an array with a collection of file hashes to attach to the post
function pico.post.create(board, parent, name, email, subject, comment, files, captcha_id, captcha_text)
local board_tbl = pico.board.tbl(board)
local is_thread = not parent
local capcode, capcode_board
if name == "##" and pico.account.current then
name = pico.account.current.Name
capcode = pico.account.current.Type
capcode_board = pico.account.current.Board
end
comment = comment and comment:gsub("[\1-\8\11-\31\127]", ""):gsub("^\n+", ""):gsub("%s+$", "") or ""
if not board_tbl then
return nil, "Board does not exist"
elseif board_tbl.Lock == 1 and not permit("admin gvol bo lvol", "board", board) then
return nil, "Board is locked"
elseif board_tbl.PPHLimit and pico.board.stats.postrate(board, 1, 1) > board_tbl.PPHLimit then
return nil, "Maximum post creation rate exceeded"
elseif #comment > board_tbl.PostMaxLength then
return nil, "Post text too long"
elseif select(2, comment:gsub("\r?\n", "")) > board_tbl.PostMaxNewlines then
return nil, "Post contained too many newlines"
elseif select(2, comment:gsub("\r?\n\r?\n", "")) > board_tbl.PostMaxDblNewlines then
return nil, "Post contained too many double newlines"
elseif name and #name > 64 then
return nil, "Name too long"
elseif email and #email > 64 then
return nil, "Email too long"
elseif subject and #subject > 64 then
return nil, "Subject too long"
elseif (not files or #files == 0) and comment == "" then
return nil, "Post is blank"
elseif ((is_thread and board_tbl.ThreadCaptcha == 1) or (not is_thread and board_tbl.PostCaptcha == 1))
and not permit("admin gvol bo lvol", "post", board)
and not pico.captcha.check(captcha_id, captcha_text) then
return nil, "Captcha is required but no valid captcha supplied"
elseif is_thread then
if board_tbl.TPHLimit and pico.board.stats.threadrate(board, 1, 1) > board_tbl.TPHLimit then
return nil, "Maximum thread creation rate exceeded"
elseif #comment < board_tbl.ThreadMinLength then
return nil, "Thread text too short"
end
else
local parent_tbl = pico.post.tbl(board, parent)
if not parent_tbl then
return nil, "Parent thread does not exist"
elseif parent_tbl.Parent then
return nil, "Parent post is not a thread"
elseif parent_tbl.Lock == 1 and not permit("admin gvol bo lvol", "post", board) then
return nil, "Parent thread is locked"
elseif parent_tbl.Cycle ~= 1 and board_tbl.PostLimit
and parent_tbl.ReplyCount >= board_tbl.PostLimit then
return nil, "Thread full"
end
end
db:e("BEGIN TRANSACTION")
db:e("INSERT INTO Posts (Board, Parent, Name, Email, Subject, Capcode, CapcodeBoard, Comment) " ..
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)", board, parent, name, email, subject, capcode, capcode_board, comment)
local number = db:r1("SELECT MaxPostNumber FROM Boards WHERE Name = ?", board)
pico.file.create_refs(board, number, files)
pico.post.create_refs(board, number, parent, email, comment)
db:e("END TRANSACTION")
return number
end
function pico.post.set(board, parent, date, name, email, subject, capcode, capcode_board, comment, files)
local auth, msg = permit("admin gvol")
if not auth then return auth, msg end
db:e("BEGIN TRANSACTION")
db:e("INSERT INTO Posts (Board, Parent, Date, Name, Email, Subject, Capcode, CapcodeBoard, Comment) " ..
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", board, parent, date, name, email, subject, capcode, capcode_board, comment)
local number = db:r1("SELECT MaxPostNumber FROM Boards WHERE Name = ?", board)
pico.file.create_refs(board, number, files)
pico.post.create_refs(board, number, parent, email, comment)
db:e("END TRANSACTION")
return number
end
function pico.post.create_refs(board, number, parent, email, comment)
if not email or not (email == "nofo" or email:match("^nofo ") or email:match(" nofo$") or email:match(" nofo ")) then
for ref in comment:gmatch(">>(%d+)") do
ref = tonumber(ref)
-- 1. Ensure that the reference doesn't already exist.
-- 2. Ensure that the post being referred to does exist.
-- 3. Ensure that the post being referred to is in the same thread as the referrer.
-- 4. Ensure that the post being referred to is not the same as the referrer.
if ref ~= number then
db:e("INSERT INTO Refs SELECT ?, ?, ? WHERE (SELECT COUNT(*) FROM Refs WHERE Board = ? AND Referee = ? AND Referrer = ?) = 0 " ..
"AND (SELECT TRUE FROM Posts WHERE Board = ? AND Number = ?) " ..
"AND ((SELECT Parent FROM Posts WHERE Board = ? AND Number = ?) = ? OR (? = ?))",
board, ref, number, board, ref, number, board, ref, board, ref, parent, ref, parent)
end
end
end
end
function pico.post.delete(board, number, reason)
local auth, msg = permit("admin gvol bo lvol", "post", board)
if not auth then return auth, msg end
if not db:b("SELECT TRUE FROM Posts WHERE Board = ? AND Number = ?", board, number) then
return false, "Post does not exist"
end
db:e("DELETE FROM Posts WHERE Board = ? AND Number = ?", board, number)
pico.log.insert(board, "Deleted post /%s/%d for reason: %s", board, number, reason)
pico.file.clean()
return true, "Post deleted successfully"
end
-- example: pico.post.multidelete("b", "31-57 459-1000", "33 35 48 466", "spam")
function pico.post.multidelete(board, include, exclude, reason)
local auth, msg = permit("admin bo", "board", board)
if not auth then return auth, msg end
if not include then return false, "Invalid include parameter" end
if not pico.board.exists(board) then
return false, "Board does not exist"
end
local sql = { "DELETE FROM Posts WHERE Board = ? AND (FALSE" }
local sqlp = { board }
local inclist = include:tokenize()
local function genspec(spec, sql, sqlp)
if spec:match("-") then
local spec_tbl = spec:tokenize("-")
if #spec_tbl ~= 2 then
return false, "Invalid range specification"
end
local start, finish = unpack(spec_tbl)
start, finish = tonumber(start), tonumber(finish)
if not start or not finish then
return false, "Invalid range specification"
end
sql[#sql + 1] = "OR Number BETWEEN ? AND ?"
sqlp[#sqlp + 1] = start
sqlp[#sqlp + 1] = finish
else
local number = tonumber(spec)
if not number then
return false, "Invalid single specification"
end
sql[#sql + 1] = "OR Number = ?"
sqlp[#sqlp + 1] = number
end
return true
end
for i = 1, #inclist do
local result, msg = genspec(inclist[i], sql, sqlp)
if not result then return result, msg end
end
sql[#sql + 1] = ") AND NOT (FALSE"
if exclude then
local exclist = exclude:tokenize()
for i = 1, #exclist do
local result, msg = genspec(exclist[i], sql, sqlp)
if not result then return result, msg end
end
end
sql[#sql + 1] = ")"
db:e(table.concat(sql, " "), unpack(sqlp))
pico.log.insert(board, "Deleted posts {%s}%s for reason: %s",
include, exclude and (" excluding {" .. exclude .. "}") or "", reason)
pico.file.clean()
return true, "Posts deleted successfully"
end
function pico.post.pattdelete(pattern, reason)
local auth, msg = permit("admin")
if not auth then return auth, msg end
if not pattern or #pattern < 6 then return false, "Invalid or too short include pattern" end
db:e("DELETE FROM Posts WHERE Comment LIKE ? ESCAPE '$'", pattern)
pico.log.insert(nil, "Deleted posts matching pattern '%s' for reason: %s", pattern, reason)
pico.file.clean()
return true, "Posts deleted successfully"
end
-- remove a file from a post without deleting it
function pico.post.unlink(board, number, file, reason)
local auth, msg = permit("admin gvol bo lvol", "post", board)
if not auth then return auth, msg end
if not db:b("SELECT TRUE FROM FileRefs WHERE Board = ? AND Number = ? AND File = ?",
board, number, file) then
return false, "No such file in that particular post"
end
db:e("DELETE FROM FileRefs WHERE Board = ? AND Number = ? AND File = ?", board, number, file)
pico.log.insert(board, "Unlinked file %s from /%s/%d for reason: %s",
file, board, number, reason)
pico.file.clean()
return true, "File unlinked successfully"
end