/
types.go
770 lines (688 loc) · 28.6 KB
/
types.go
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
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"html"
"io/ioutil"
"math/rand"
"os"
"path"
"strconv"
"time"
"github.com/frustra/bbcode"
)
const (
dirIsAFileStr = "unable to create \"%s\", path exists and is a file"
pathExistsStr = "unable to create \"%s\", path already exists"
genericErrStr = "unable to create \"%s\": %s"
)
var (
config GochanConfig
readBannedIPs []string
msgfmtr MsgFormatter
version *GochanVersion
)
type MsgFormatter struct {
// Go's garbage collection does weird things with bbcode's internal tag map.
// Moving the bbcode compiler isntance (and eventually a Markdown compiler) to a struct
// appears to fix this
bbCompiler bbcode.Compiler
}
func (mf *MsgFormatter) InitBBcode() {
if config.DisableBBcode {
return
}
mf.bbCompiler = bbcode.NewCompiler(true, true)
mf.bbCompiler.SetTag("center", nil)
mf.bbCompiler.SetTag("code", nil)
mf.bbCompiler.SetTag("color", nil)
mf.bbCompiler.SetTag("img", nil)
mf.bbCompiler.SetTag("quote", nil)
mf.bbCompiler.SetTag("size", nil)
}
func (mf *MsgFormatter) Compile(msg string) string {
if config.DisableBBcode {
return msg
}
return mf.bbCompiler.Compile(msg)
}
type RecentPost struct {
BoardName string
BoardID int
PostID int
ParentID int
Name string
Tripcode string
Message string
Filename string
ThumbW int
ThumbH int
IP string
Timestamp time.Time
}
func (p *RecentPost) GetURL(includeDomain bool) string {
postURL := ""
if includeDomain {
postURL += config.SiteDomain
}
idStr := strconv.Itoa(p.PostID)
postURL += config.SiteWebfolder + p.BoardName + "/res/"
if p.ParentID == 0 {
postURL += idStr + ".html#" + idStr
} else {
postURL += strconv.Itoa(p.ParentID) + ".html#" + idStr
}
return postURL
}
type Thread struct {
OP Post `json:"-"`
NumReplies int `json:"replies"`
NumImages int `json:"images"`
OmittedPosts int `json:"omitted_posts"`
OmittedImages int `json:"omitted_images"`
BoardReplies []Post `json:"-"`
Sticky int `json:"sticky"`
Locked int `json:"locked"`
ThreadPage int `json:"-"`
}
// SQL Table structs
type Announcement struct {
ID uint `json:"no"`
Subject string `json:"sub"`
Message string `json:"com"`
Poster string `json:"name"`
Timestamp time.Time
}
type BanAppeal struct {
ID int
Ban int
Message string
Denied bool
StaffResponse string
}
func (a *BanAppeal) GetBan() (*BanInfo, error) {
var ban BanInfo
err := queryRowSQL("SELECT * FROM DBPREFIXbanlist WHERE id = ? LIMIT 1",
[]interface{}{a.ID}, []interface{}{
&ban.ID, &ban.AllowRead, &ban.IP, &ban.Name, &ban.NameIsRegex, &ban.SilentBan,
&ban.Boards, &ban.Staff, &ban.Timestamp, &ban.Expires, &ban.Permaban, &ban.Reason,
&ban.StaffNote, &ban.AppealAt},
)
return &ban, err
}
type BanInfo struct {
ID uint
AllowRead bool
IP string
Name string
NameIsRegex bool
SilentBan uint8
Boards string
Staff string
Timestamp time.Time
Expires time.Time
Permaban bool
Reason string
Type int
StaffNote string
AppealAt time.Time
CanAppeal bool
}
type BannedHash struct {
ID uint
Checksum string
Description string
}
type Board struct {
ID int `json:"-"`
CurrentPage int `json:"-"`
NumPages int `json:"pages"`
ListOrder int `json:"-"`
Dir string `json:"board"`
Type int `json:"-"`
UploadType int `json:"-"`
Title string `json:"title"`
Subtitle string `json:"meta_description"`
Description string `json:"-"`
Section int `json:"-"`
MaxFilesize int `json:"max_filesize"`
MaxPages int `json:"max_pages"`
DefaultStyle string `json:"-"`
Locked bool `json:"is_archived"`
CreatedOn time.Time `json:"-"`
Anonymous string `json:"-"`
ForcedAnon bool `json:"-"`
MaxAge int `json:"-"`
AutosageAfter int `json:"bump_limit"`
NoImagesAfter int `json:"image_limit"`
MaxMessageLength int `json:"max_comment_chars"`
EmbedsAllowed bool `json:"-"`
RedirectToThread bool `json:"-"`
ShowID bool `json:"-"`
RequireFile bool `json:"-"`
EnableCatalog bool `json:"-"`
EnableSpoileredImages bool `json:"-"`
EnableSpoileredThreads bool `json:"-"`
Worksafe bool `json:"ws_board"`
ThreadPage int `json:"-"`
Cooldowns BoardCooldowns `json:"cooldowns"`
ThreadsPerPage int `json:"per_page"`
}
// AbsolutePath returns the full filepath of the board directory
func (board *Board) AbsolutePath(subpath ...string) string {
return path.Join(config.DocumentRoot, board.Dir, path.Join(subpath...))
}
// WebPath returns a string that represents the file's path as accessible by a browser
// fileType should be "boardPage", "threadPage", "upload", or "thumb"
func (board *Board) WebPath(fileName string, fileType string) string {
var filePath string
switch fileType {
case "":
fallthrough
case "boardPage":
filePath = path.Join(config.SiteWebfolder, board.Dir, fileName)
case "threadPage":
filePath = path.Join(config.SiteWebfolder, board.Dir, "res", fileName)
case "upload":
filePath = path.Join(config.SiteWebfolder, board.Dir, "src", fileName)
case "thumb":
filePath = path.Join(config.SiteWebfolder, board.Dir, "thumb", fileName)
}
return filePath
}
func (board *Board) PagePath(pageNum interface{}) string {
var page string
pageNumStr := fmt.Sprintf("%v", pageNum)
if pageNumStr == "prev" {
if board.CurrentPage < 2 {
page = "1"
} else {
page = strconv.Itoa(board.CurrentPage - 1)
}
} else if pageNumStr == "next" {
if board.CurrentPage >= board.NumPages {
page = strconv.Itoa(board.NumPages)
} else {
page = strconv.Itoa(board.CurrentPage + 1)
}
} else {
page = pageNumStr
}
return board.WebPath(page+".html", "boardPage")
}
// Build builds the board and its thread files
// if newBoard is true, it adds a row to DBPREFIXboards and fails if it exists
// if force is true, it doesn't fail if the directories exist but does fail if it is a file
func (board *Board) Build(newBoard bool, force bool) error {
var err error
if board.Dir == "" {
return errors.New("board must have a directory before it is built")
}
if board.Title == "" {
return errors.New("board must have a title before it is built")
}
dirPath := board.AbsolutePath()
resPath := board.AbsolutePath("res")
srcPath := board.AbsolutePath("src")
thumbPath := board.AbsolutePath("thumb")
dirInfo, _ := os.Stat(dirPath)
resInfo, _ := os.Stat(resPath)
srcInfo, _ := os.Stat(srcPath)
thumbInfo, _ := os.Stat(thumbPath)
if dirInfo != nil {
if !force {
return fmt.Errorf(pathExistsStr, dirPath)
}
if !dirInfo.IsDir() {
return fmt.Errorf(dirIsAFileStr, dirPath)
}
} else {
if err = os.Mkdir(dirPath, 0666); err != nil {
return fmt.Errorf(genericErrStr, dirPath, err.Error())
}
}
if resInfo != nil {
if !force {
return fmt.Errorf(pathExistsStr, resPath)
}
if !resInfo.IsDir() {
return fmt.Errorf(dirIsAFileStr, resPath)
}
} else {
if err = os.Mkdir(resPath, 0666); err != nil {
return fmt.Errorf(genericErrStr, resPath, err.Error())
}
}
if srcInfo != nil {
if !force {
return fmt.Errorf(pathExistsStr, srcPath)
}
if !srcInfo.IsDir() {
return fmt.Errorf(dirIsAFileStr, srcPath)
}
} else {
if err = os.Mkdir(srcPath, 0666); err != nil {
return fmt.Errorf(genericErrStr, srcPath, err.Error())
}
}
if thumbInfo != nil {
if !force {
return fmt.Errorf(pathExistsStr, thumbPath)
}
if !thumbInfo.IsDir() {
return fmt.Errorf(dirIsAFileStr, thumbPath)
}
} else {
if err = os.Mkdir(thumbPath, 0666); err != nil {
return fmt.Errorf(genericErrStr, thumbPath, err.Error())
}
}
if newBoard {
var numRows int
queryRowSQL("SELECT COUNT(*) FROM DBPREFIXboards WHERE `dir` = ?",
[]interface{}{board.Dir},
[]interface{}{&numRows},
)
if numRows > 0 {
return errors.New("board already exists in database")
}
board.CreatedOn = time.Now()
var result sql.Result
if result, err = execSQL("INSERT INTO DBPREFIXboards "+
"(list_order,dir,type,upload_type,title,subtitle,description,"+
"section,max_file_size,max_pages,default_style,locked,created_on,"+
"anonymous,forced_anon,max_age,autosage_after,no_images_after,max_message_length,"+
"embeds_allowed,redirect_to_thread,require_file,enable_catalog) "+
"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
board.ListOrder, board.Dir, board.Type, board.UploadType,
board.Title, board.Subtitle, board.Description, board.Section,
board.MaxFilesize, board.MaxPages, board.DefaultStyle,
board.Locked, getSpecificSQLDateTime(board.CreatedOn), board.Anonymous,
board.ForcedAnon, board.MaxAge, board.AutosageAfter,
board.NoImagesAfter, board.MaxMessageLength, board.EmbedsAllowed,
board.RedirectToThread, board.RequireFile, board.EnableCatalog,
); err != nil {
return err
}
boardID, _ := result.LastInsertId()
board.ID = int(boardID)
} else {
if err = board.UpdateID(); err != nil {
return err
}
}
buildBoardPages(board)
buildThreads(true, board.ID, 0)
resetBoardSectionArrays()
buildFrontPage()
if board.EnableCatalog {
buildCatalog(board.ID)
}
buildBoardListJSON()
return nil
}
// PopulateData gets the board data from the database and sets the respective properties.
// if id > -1, the ID will be used to search the database. Otherwise dir will be used
func (board *Board) PopulateData(id int, dir string) error {
queryStr := "SELECT * FROM DBPREFIXboards WHERE id = ?"
var values []interface{}
if id > -1 {
values = append(values, id)
} else {
queryStr = "SELECT * FROM DBPREFIXboards WHERE dir = ?"
values = append(values, dir)
}
return queryRowSQL(queryStr, values, []interface{}{
&board.ID, &board.ListOrder, &board.Dir, &board.Type, &board.UploadType,
&board.Title, &board.Subtitle, &board.Description, &board.Section,
&board.MaxFilesize, &board.MaxPages, &board.DefaultStyle, &board.Locked,
&board.CreatedOn, &board.Anonymous, &board.ForcedAnon, &board.MaxAge,
&board.AutosageAfter, &board.NoImagesAfter, &board.MaxMessageLength,
&board.EmbedsAllowed, &board.RedirectToThread, &board.RequireFile,
&board.EnableCatalog})
}
func (board *Board) SetDefaults() {
board.ListOrder = 0
board.Section = 1
board.MaxFilesize = 4096
board.MaxPages = 11
board.DefaultStyle = config.DefaultStyle
board.Locked = false
board.Anonymous = "Anonymous"
board.ForcedAnon = false
board.MaxAge = 0
board.AutosageAfter = 200
board.NoImagesAfter = 0
board.MaxMessageLength = 8192
board.EmbedsAllowed = true
board.RedirectToThread = false
board.ShowID = false
board.RequireFile = false
board.EnableCatalog = true
board.EnableSpoileredImages = true
board.EnableSpoileredThreads = true
board.Worksafe = true
board.ThreadsPerPage = 10
}
func (board *Board) UpdateID() error {
return queryRowSQL("SELECT id FROM DBPREFIXboards WHERE dir = ?",
[]interface{}{board.Dir},
[]interface{}{&board.ID})
}
type BoardSection struct {
ID int
ListOrder int
Hidden bool
Name string
Abbreviation string
}
// Post represents each post in the database
type Post struct {
ID int `json:"no"`
ParentID int `json:"resto"`
CurrentPage int `json:"-"`
BoardID int `json:"-"`
Name string `json:"name"`
Tripcode string `json:"trip"`
Email string `json:"email"`
Subject string `json:"sub"`
MessageHTML string `json:"com"`
MessageText string `json:"-"`
Password string `json:"-"`
Filename string `json:"tim"`
FilenameOriginal string `json:"filename"`
FileChecksum string `json:"md5"`
FileExt string `json:"extension"`
Filesize int `json:"fsize"`
ImageW int `json:"w"`
ImageH int `json:"h"`
ThumbW int `json:"tn_w"`
ThumbH int `json:"tn_h"`
IP string `json:"-"`
Capcode string `json:"capcode"`
Timestamp time.Time `json:"time"`
Autosage bool `json:"-"`
DeletedTimestamp time.Time `json:"-"`
Bumped time.Time `json:"last_modified"`
Stickied bool `json:"-"`
Locked bool `json:"-"`
Reviewed bool `json:"-"`
}
func (p *Post) GetURL(includeDomain bool) string {
postURL := ""
if includeDomain {
postURL += config.SiteDomain
}
var board Board
if err := board.PopulateData(p.BoardID, ""); err != nil {
return postURL
}
idStr := strconv.Itoa(p.ID)
postURL += config.SiteWebfolder + board.Dir + "/res/"
if p.ParentID == 0 {
postURL += idStr + ".html#" + idStr
} else {
postURL += strconv.Itoa(p.ParentID) + ".html#" + idStr
}
return postURL
}
// Sanitize escapes HTML strings in a post. This should be run immediately before
// the post is inserted into the database
func (p *Post) Sanitize() {
p.Name = html.EscapeString(p.Name)
p.Email = html.EscapeString(p.Email)
p.Subject = html.EscapeString(p.Subject)
p.Password = html.EscapeString(p.Password)
}
type Report struct {
ID uint
Board string
PostID uint
Timestamp time.Time
IP string
Reason string
Cleared bool
IsTemp bool
}
type LoginSession struct {
ID uint
Name string
Data string
Expires string
}
// Staff represents a single staff member's info stored in the database
type Staff struct {
ID int
Username string
PasswordChecksum string
Rank int
Boards string
AddedOn time.Time
LastActive time.Time
}
type WordFilter struct {
ID int
From string
To string
Boards string
RegEx bool
}
type BoardCooldowns struct {
NewThread int `json:"threads"`
Reply int `json:"replies"`
ImageReply int `json:"images"`
}
type Style struct {
Name string
Filename string
}
// GochanConfig stores crucial info and is read from/written to gochan.json
type GochanConfig struct {
ListenIP string
Port int
FirstPage []string
Username string
UseFastCGI bool
DebugMode bool `description:"Disables several spam/browser checks that can cause problems when hosting an instance locally."`
DocumentRoot string
TemplateDir string
LogDir string
DBtype string
DBhost string
DBname string
DBusername string
DBpassword string
DBprefix string
Lockdown bool `description:"Disables posting." default:"unchecked"`
LockdownMessage string `description:"Message displayed when someone tries to post while the site is on lockdown."`
Sillytags []string `description:"List of randomly selected staff tags separated by line, e.g. <span style=\"color: red;\">## Mod</span>, to be randomly assigned to posts if UseSillytags is checked. Don't include the \"## \""`
UseSillytags bool `description:"Use Sillytags" default:"unchecked"`
Modboard string `description:"A super secret clubhouse board that only staff can view/post to." default:"staff"`
SiteName string `description:"The name of the site that appears in the header of the front page." default:"Gochan"`
SiteSlogan string `description:"The text that appears below SiteName on the home page"`
SiteHeaderURL string `description:"To be honest, I'm not even sure what this does. It'll probably be removed later."`
SiteWebfolder string `description:"The HTTP root appearing in the browser (e.g. https://gochan.org/<SiteWebFolder>" default:"/"`
SiteDomain string `description:"The server's domain (duh). Do not edit this unless you know what you are doing or BAD THINGS WILL HAPPEN!" default:"127.0.0.1" critical:"true"`
DomainRegex string `description:"Regular expression used for incoming request validation. Do not edit this unless you know what you are doing or BAD THINGS WILL HAPPEN!" default:"(https|http):\\\\/\\\\/(gochan\\\\.lunachan\\.net|gochan\\\\.org)\\/(.*)" critical:"true"`
Styles []Style `description:"List of styles (one per line) that should be accessed online at <SiteWebFolder>/css/<Style>/"`
DefaultStyle string `description:"Filename of the default Style. This should appear in the list above or bad things might happen."`
AllowDuplicateImages bool `description:"Disabling this will cause gochan to reject a post if the image has already been uploaded for another post.<br />This may end up being removed or being made board-specific in the future." default:"checked"`
AllowVideoUploads bool `description:"Allows users to upload .webm videos. <br />This may end up being removed or being made board-specific in the future."`
NewThreadDelay int `description:"The amount of time in seconds that is required before an IP can make a new thread.<br />This may end up being removed or being made board-specific in the future." default:"30"`
ReplyDelay int `description:"Same as the above, but for replies." default:"7"`
MaxLineLength int `description:"Any line in a post that exceeds this will be split into two (or more) lines.<br />I'm not really sure why this is here, so it may end up being removed." default:"150"`
ReservedTrips []string `description:"Secure tripcodes (!!Something) can be reserved here.<br />Each reservation should go on its own line and should look like this:<br />TripPassword1##Tripcode1<br />TripPassword2##Tripcode2"`
ThumbWidth int `description:"OP thumbnails use this as their max width.<br />To keep the aspect ratio, the image will be scaled down to the ThumbWidth or ThumbHeight, whichever is larger." default:"200"`
ThumbHeight int `description:"OP thumbnails use this as their max height.<br />To keep the aspect ratio, the image will be scaled down to the ThumbWidth or ThumbHeight, whichever is larger." default:"200"`
ThumbWidth_reply int `description:"Same as ThumbWidth and ThumbHeight but for reply images." default:"125"`
ThumbHeight_reply int `description:"Same as ThumbWidth and ThumbHeight but for reply images." default:"125"`
ThumbWidth_catalog int `description:"Same as ThumbWidth and ThumbHeight but for catalog images." default:"50"`
ThumbHeight_catalog int `description:"Same as ThumbWidth and ThumbHeight but for catalog images." default:"50"`
ThreadsPerPage int `default:"15"`
RepliesOnBoardPage int `description:"Number of replies to a thread to show on the board page." default:"3"`
StickyRepliesOnBoardPage int `description:"Same as above for stickied threads." default:"1"`
BanColors []string `description:"Colors to be used for public ban messages (e.g. USER WAS BANNED FOR THIS POST).<br />Each entry should be on its own line, and should look something like this:<br />username1:#FF0000<br />username2:#FAF00F<br />username3:blue<br />Invalid entries/nonexistent usernames will show a warning and use the default red."`
BanMsg string `description:"The default public ban message." default:"USER WAS BANNED FOR THIS POST"`
EmbedWidth int `description:"The width for inline/expanded webm videos." default:"200"`
EmbedHeight int `description:"The height for inline/expanded webm videos." default:"164"`
ExpandButton bool `description:"If checked, adds [Embed] after a Youtube, Vimeo, etc link to toggle an inline video frame." default:"checked"`
ImagesOpenNewTab bool `description:"If checked, thumbnails will open the respective image/video in a new tab instead of expanding them." default:"unchecked"`
MakeURLsHyperlinked bool `description:"If checked, URLs in posts will be turned into a hyperlink. If unchecked, ExpandButton and NewTabOnOutlinks are ignored." default:"checked"`
NewTabOnOutlinks bool `description:"If checked, links to external sites will open in a new tab." default:"checked"`
DisableBBcode bool `description:"If checked, gochan will not compile bbcode into HTML" default:"unchecked"`
MinifyHTML bool `description:"If checked, gochan will minify html files when building" default:"checked"`
MinifyJS bool `description:"If checked, gochan will minify js and json files when building" default:"checked"`
UseMinifiedGochanJS bool `json:"-"`
DateTimeFormat string `description:"The format used for dates. See <a href=\"https://golang.org/pkg/time/#Time.Format\">here</a> for more info." default:"Mon, January 02, 2006 15:04 PM"`
AkismetAPIKey string `description:"The API key to be sent to Akismet for post spam checking. If the key is invalid, Akismet won't be used."`
UseCaptcha bool `description:"If checked, a captcha will be generated"`
CaptchaWidth int `description:"Width of the generated captcha image" default:"240"`
CaptchaHeight int `description:"Height of the generated captcha image" default:"80"`
CaptchaMinutesExpire int `description:"Number of minutes before a user has to enter a new CAPTCHA before posting. If <1 they have to submit one for every post." default:"15"`
EnableGeoIP bool `description:"If checked, this enables the usage of GeoIP for posts." default:"checked"`
GeoIPDBlocation string `description:"Specifies the location of the GeoIP database file. If you're using CloudFlare, you can set it to cf to rely on CloudFlare for GeoIP information." default:"/usr/share/GeoIP/GeoIP.dat"`
MaxRecentPosts int `description:"The maximum number of posts to show on the Recent Posts list on the front page." default:"3"`
RecentPostsWithNoFile bool `description:"If checked, recent posts with no image/upload are shown on the front page (as well as those with images" default:"unchecked"`
EnableAppeals bool `description:"If checked, allow banned users to appeal their bans.<br />This will likely be removed (permanently allowing appeals) or made board-specific in the future." default:"checked"`
MaxLogDays int `description:"The maximum number of days to keep messages in the moderation/staff log file."`
RandomSeed string `critical:"true"`
TimeZone int `json:"-"`
}
func (cfg *GochanConfig) CheckString(val, defaultVal string, critical bool, msg string) string {
if val == "" {
val = defaultVal
flags := lStdLog | lErrorLog
if critical {
flags |= lFatal
}
gclog.Print(flags, msg)
}
return val
}
func (cfg *GochanConfig) CheckInt(val, defaultVal int, critical bool, msg string) int {
if val == 0 {
val = defaultVal
flags := lStdLog | lErrorLog
if critical {
flags |= lFatal
}
gclog.Print(flags, msg)
}
return val
}
func initConfig() {
cfgPath := findResource("gochan.json", "/etc/gochan/gochan.json")
if cfgPath == "" {
fmt.Println("gochan.json not found")
os.Exit(1)
}
jfile, err := ioutil.ReadFile(cfgPath)
if err != nil {
fmt.Printf("Error reading %s: %s\n", cfgPath, err.Error())
os.Exit(1)
}
if err = json.Unmarshal(jfile, &config); err != nil {
fmt.Printf("Error parsing %s: %s\n", cfgPath, err.Error())
os.Exit(1)
}
config.LogDir = findResource(config.LogDir, "log", "/var/log/gochan/")
if gclog, err = initLogs(
path.Join(config.LogDir, "access.log"),
path.Join(config.LogDir, "error.log"),
path.Join(config.LogDir, "staff.log"),
); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
config.CheckString(config.ListenIP, "", true, "ListenIP not set in gochan.json, halting.")
if config.Port == 0 {
config.Port = 80
}
if len(config.FirstPage) == 0 {
config.FirstPage = []string{"index.html", "board.html", "firstrun.html"}
}
config.Username = config.CheckString(config.Username, "gochan", false, "Username not set in gochan.json, using 'gochan' as default")
config.DocumentRoot = config.CheckString(config.DocumentRoot, "gochan", true, "DocumentRoot not set in gochan.json, halting.")
wd, wderr := os.Getwd()
if wderr == nil {
_, staterr := os.Stat(path.Join(wd, config.DocumentRoot, "css"))
if staterr == nil {
config.DocumentRoot = path.Join(wd, config.DocumentRoot)
}
}
config.TemplateDir = config.CheckString(
findResource(config.TemplateDir, "templates", "/usr/local/share/gochan/templates/", "/usr/share/gochan/templates/"),
"", true, "Unable to locate template directory, halting.")
config.CheckString(config.DBtype, "", true, "DBtype not set in gochan.json, halting (currently supported values: mysql,postgresql,sqlite3)")
config.CheckString(config.DBhost, "", true, "DBhost not set in gochan.json, halting.")
config.DBname = config.CheckString(config.DBname, "gochan", false,
"DBname not set in gochan.json, setting to 'gochan'")
config.CheckString(config.DBusername, "", true, "DBusername not set in gochan, halting.")
config.CheckString(config.DBpassword, "", true, "DBpassword not set in gochan, halting.")
config.LockdownMessage = config.CheckString(config.LockdownMessage,
"The administrator has temporarily disabled posting. We apologize for the inconvenience", false, "")
config.CheckString(config.SiteName, "", true, "SiteName not set in gochan.json, halting.")
config.CheckString(config.SiteDomain, "", true, "SiteName not set in gochan.json, halting.")
if config.SiteWebfolder == "" {
gclog.Print(lErrorLog|lStdLog, "SiteWebFolder not set in gochan.json, using / as default.")
} else if string(config.SiteWebfolder[0]) != "/" {
config.SiteWebfolder = "/" + config.SiteWebfolder
}
if config.SiteWebfolder[len(config.SiteWebfolder)-1:] != "/" {
config.SiteWebfolder += "/"
}
config.CheckString(config.DomainRegex, "", true, `DomainRegex not set in gochan.json. Consider using something like "(https|http):\\/\\/(gochan\\.org)\\/(.*)"`)
//config.DomainRegex = "(https|http):\\/\\/(" + config.SiteDomain + ")\\/(.*)"
if config.Styles == nil {
gclog.Print(lErrorLog|lStdLog|lFatal, "Styles not set in gochan.json, halting.")
}
config.DefaultStyle = config.CheckString(config.DefaultStyle, config.Styles[0].Filename, false, "")
config.NewThreadDelay = config.CheckInt(config.NewThreadDelay, 30, false, "")
config.ReplyDelay = config.CheckInt(config.ReplyDelay, 7, false, "")
config.MaxLineLength = config.CheckInt(config.MaxLineLength, 150, false, "")
//ReservedTrips string //eventually this will be map[string]string
config.ThumbWidth = config.CheckInt(config.ThumbWidth, 200, false, "")
config.ThumbHeight = config.CheckInt(config.ThumbHeight, 200, false, "")
config.ThumbWidth_reply = config.CheckInt(config.ThumbWidth_reply, 125, false, "")
config.ThumbHeight_reply = config.CheckInt(config.ThumbHeight_reply, 125, false, "")
config.ThumbWidth_catalog = config.CheckInt(config.ThumbWidth_catalog, 50, false, "")
config.ThumbHeight_catalog = config.CheckInt(config.ThumbHeight_catalog, 50, false, "")
config.ThreadsPerPage = config.CheckInt(config.ThreadsPerPage, 10, false, "")
config.RepliesOnBoardPage = config.CheckInt(config.RepliesOnBoardPage, 3, false, "")
config.StickyRepliesOnBoardPage = config.CheckInt(config.StickyRepliesOnBoardPage, 1, false, "")
/*config.BanColors, err = c.GetString("threads", "ban_colors") //eventually this will be map[string] string
if err != nil {
config.BanColors = "admin:#CC0000"
}*/
config.BanMsg = config.CheckString(config.BanMsg, "(USER WAS BANNED FOR THIS POST)", false, "")
config.DateTimeFormat = config.CheckString(config.DateTimeFormat, "Mon, January 02, 2006 15:04 PM", false, "")
config.CaptchaWidth = config.CheckInt(config.CaptchaWidth, 240, false, "")
config.CaptchaHeight = config.CheckInt(config.CaptchaHeight, 80, false, "")
if config.EnableGeoIP {
if config.GeoIPDBlocation == "" {
gclog.Print(lErrorLog|lStdLog, "GeoIPDBlocation not set in gochan.json, disabling EnableGeoIP")
config.EnableGeoIP = false
}
}
if config.MaxLogDays == 0 {
config.MaxLogDays = 15
}
if config.RandomSeed == "" {
gclog.Print(lErrorLog|lStdLog, "RandomSeed not set in gochan.json, Generating a random one.")
for i := 0; i < 8; i++ {
num := rand.Intn(127-32) + 32
config.RandomSeed += fmt.Sprintf("%c", num)
}
configJSON, _ := json.MarshalIndent(config, "", "\t")
if err = ioutil.WriteFile(cfgPath, configJSON, 0777); err != nil {
gclog.Printf(lErrorLog|lStdLog|lFatal, "Unable to write %s with randomly generated seed: %s", cfgPath, err.Error())
}
}
_, zoneOffset := time.Now().Zone()
config.TimeZone = zoneOffset / 60 / 60
msgfmtr.InitBBcode()
version = ParseVersion(versionStr)
version.Normalize()
}