diff --git a/cmd/gochan/server.go b/cmd/gochan/server.go index 5ca81e02..872f3dda 100755 --- a/cmd/gochan/server.go +++ b/cmd/gochan/server.go @@ -311,7 +311,7 @@ func utilHandler(writer http.ResponseWriter, request *http.Request) { } if err = gcsql.UpdatePost(postid, request.FormValue("editemail"), request.FormValue("editsubject"), - posting.FormatMessage(request.FormValue("editmsg")), request.FormValue("editmsg")); err != nil { + posting.FormatMessage(request.FormValue("editmsg"), board.Dir), request.FormValue("editmsg")); err != nil { serverutil.ServeErrorPage(writer, gclog.Print(gclog.LErrorLog, "Unable to edit post: ", err.Error())) return diff --git a/pkg/gcsql/queries.go b/pkg/gcsql/queries.go index ff925668..794c3fd4 100644 --- a/pkg/gcsql/queries.go +++ b/pkg/gcsql/queries.go @@ -23,8 +23,9 @@ var ( // GetAllNondeletedMessageRaw gets all the raw message texts from the database, saved per id func GetAllNondeletedMessageRaw() ([]MessagePostContainer, error) { - const sql = `select posts.id, posts.message, posts.message_raw from DBPREFIXposts as posts - WHERE posts.is_deleted = FALSE` + const sql = `SELECT posts.id, posts.message, posts.message_raw, DBPREFIXboards.dir as dir + FROM DBPREFIXposts as posts, DBPREFIXboards + where posts.is_deleted = FALSE` rows, err := QuerySQL(sql) if err != nil { return nil, err @@ -33,7 +34,7 @@ func GetAllNondeletedMessageRaw() ([]MessagePostContainer, error) { for rows.Next() { var message MessagePostContainer var formattedHTML template.HTML - if err = rows.Scan(&message.ID, &formattedHTML, &message.MessageRaw); err != nil { + if err = rows.Scan(&message.ID, &formattedHTML, &message.MessageRaw, &message.Board); err != nil { return nil, err } message.Message = template.HTML(formattedHTML) @@ -447,19 +448,19 @@ func (wf *WordFilter) StaffName() string { return staff.Username } -// Apply runs the current wordfilter on the given post, without checking the board or (re)building the post +// Apply runs the current wordfilter on the given string, without checking the board or (re)building the post // It returns an error if it is a regular expression and regexp.Compile failed to parse it -func (wf *WordFilter) Apply(p *Post) error { +func (wf *WordFilter) Apply(message string) (string, error) { if wf.IsRegex { re, err := regexp.Compile(wf.Search) if err != nil { - return err + return message, err } - p.MessageText = re.ReplaceAllString(p.MessageText, wf.ChangeTo) + message = re.ReplaceAllString(message, wf.ChangeTo) } else { - p.MessageText = strings.Replace(p.MessageText, wf.Search, wf.ChangeTo, -1) + message = strings.Replace(message, wf.Search, wf.ChangeTo, -1) } - return nil + return message, nil } //getDatabaseVersion gets the version of the database, or an error if none or multiple exist diff --git a/pkg/gcsql/tables.go b/pkg/gcsql/tables.go index 9778bd19..59ac6325 100644 --- a/pkg/gcsql/tables.go +++ b/pkg/gcsql/tables.go @@ -386,6 +386,7 @@ type MessagePostContainer struct { ID int MessageRaw string Message template.HTML + Board string } // Deprecated. Struct was made for use with old database, deprecated since refactor of april 2020. diff --git a/pkg/manage/actions.go b/pkg/manage/actions.go index bc85c550..d817f21e 100644 --- a/pkg/manage/actions.go +++ b/pkg/manage/actions.go @@ -599,7 +599,7 @@ var actions = []Action{ } for i := range messages { - messages[i].Message = posting.FormatMessage(messages[i].MessageRaw) + messages[i].Message = posting.FormatMessage(messages[i].MessageRaw, messages[i].Board) } if err = gcsql.SetFormattedInDatabase(messages); err != nil { return "", err diff --git a/pkg/posting/formatting.go b/pkg/posting/formatting.go index 2a042c95..d6cb23cf 100644 --- a/pkg/posting/formatting.go +++ b/pkg/posting/formatting.go @@ -32,9 +32,6 @@ type MessageFormatter struct { } func (mf *MessageFormatter) InitBBcode() { - if config.GetBoardConfig("").DisableBBcode { - return - } mf.bbCompiler = bbcode.NewCompiler(true, true) mf.bbCompiler.SetTag("center", nil) mf.bbCompiler.SetTag("code", nil) @@ -44,15 +41,34 @@ func (mf *MessageFormatter) InitBBcode() { mf.bbCompiler.SetTag("size", nil) } -func (mf *MessageFormatter) Compile(msg string) string { - if config.GetBoardConfig("").DisableBBcode { +func (mf *MessageFormatter) ApplyWordFilters(message string, boardDir string) (string, error) { + var filters []gcsql.WordFilter + var err error + if boardDir == "" { + filters, err = gcsql.GetWordFilters() + } else { + filters, err = gcsql.GetBoardWordFilters(boardDir) + } + if err != nil { + return message, err + } + for _, wf := range filters { + if message, err = wf.Apply(message); err != nil { + return message, err + } + } + return message, nil +} + +func (mf *MessageFormatter) Compile(msg string, boardDir string) string { + if config.GetBoardConfig(boardDir).DisableBBcode { return msg } return mf.bbCompiler.Compile(msg) } -func FormatMessage(message string) template.HTML { - message = msgfmtr.Compile(message) +func FormatMessage(message string, boardDir string) template.HTML { + message = msgfmtr.Compile(message, boardDir) // prepare each line to be formatted postLines := strings.Split(message, "
") for i, line := range postLines { diff --git a/pkg/posting/post.go b/pkg/posting/post.go index 232eed20..d91a9bbe 100644 --- a/pkg/posting/post.go +++ b/pkg/posting/post.go @@ -51,6 +51,13 @@ func MakePost(writer http.ResponseWriter, request *http.Request) { post.ParentID, _ = strconv.Atoi(request.FormValue("threadid")) post.BoardID, _ = strconv.Atoi(request.FormValue("boardid")) + var postBoard gcsql.Board + postBoard, err := gcsql.GetBoardFromID(post.BoardID) + if err != nil { + serverutil.ServeErrorPage(writer, gclog.Print(gclog.LErrorLog, + "Error getting board info: ", err.Error())) + return + } var emailCommand string formName = request.FormValue("postname") @@ -79,7 +86,7 @@ func MakePost(writer http.ResponseWriter, request *http.Request) { post.Subject = request.FormValue("postsubject") post.MessageText = strings.Trim(request.FormValue("postmsg"), "\r\n") - var err error + if maxMessageLength, err = gcsql.GetMaxMessageLength(post.BoardID); err != nil { serverutil.ServeErrorPage(writer, gclog.Print(gclog.LErrorLog, "Error getting board info: ", err.Error())) @@ -89,7 +96,8 @@ func MakePost(writer http.ResponseWriter, request *http.Request) { serverutil.ServeErrorPage(writer, "Post body is too long") return } - post.MessageHTML = FormatMessage(post.MessageText) + + post.MessageHTML = FormatMessage(post.MessageText, postBoard.Dir) password := request.FormValue("postpassword") if password == "" { password = gcutil.RandomString(8) @@ -159,7 +167,6 @@ func MakePost(writer http.ResponseWriter, request *http.Request) { boards, _ := gcsql.GetAllBoards() - postBoard, _ := gcsql.GetBoardFromID(post.BoardID) if banStatus != nil && banStatus.IsBanned(postBoard.Dir) { var banpageBuffer bytes.Buffer