Skip to content

Commit

Permalink
Censoring words
Browse files Browse the repository at this point in the history
  • Loading branch information
s-gv committed Nov 12, 2017
1 parent 6267aa9 commit 5c08005
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 25 deletions.
37 changes: 21 additions & 16 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
HeaderMsg string = "header_msg"
LoginMsg string = "login_msg"
SignupMsg string = "signup_msg"
CensoredWords string = "censored_words"
SignupDisabled string = "signup_disabled"
GroupCreationDisabled string = "group_creation_disabled"
ImageUploadEnabled string = "image_upload_enabled"
Expand Down Expand Up @@ -54,27 +55,31 @@ func Config(key string) string {
if key == LoginMsg {
return ""
}
if key == CensoredWords {
return ""
}
return "0"
}

func ConfigAllVals() map[string]interface{} {
vals := map[string]interface{}{
"forum_name": Config(ForumName),
"header_msg": Config(HeaderMsg),
"login_msg": Config(LoginMsg),
"signup_msg": Config(SignupMsg),
"signup_disabled": Config(SignupDisabled) == "1",
"group_creation_disabled": Config(GroupCreationDisabled) == "1",
"image_upload_enabled": Config(ImageUploadEnabled) == "1",
"allow_group_subscription": Config(AllowGroupSubscription) == "1",
"allow_topic_subscription": Config(AllowTopicSubscription) == "1",
"data_dir": Config(DataDir),
"body_appendage": Config(BodyAppendage),
"default_from_mail": Config(DefaultFromMail),
"smtp_host": Config(SMTPHost),
"smtp_port": Config(SMTPPort),
"smtp_user": Config(SMTPUser),
"smtp_pass": Config(SMTPPass),
ForumName: Config(ForumName),
HeaderMsg: Config(HeaderMsg),
LoginMsg: Config(LoginMsg),
SignupMsg: Config(SignupMsg),
CensoredWords: Config(CensoredWords),
SignupDisabled: Config(SignupDisabled) == "1",
GroupCreationDisabled: Config(GroupCreationDisabled) == "1",
ImageUploadEnabled: Config(ImageUploadEnabled) == "1",
AllowGroupSubscription: Config(AllowGroupSubscription) == "1",
AllowTopicSubscription: Config(AllowTopicSubscription) == "1",
DataDir: Config(DataDir),
BodyAppendage: Config(BodyAppendage),
DefaultFromMail: Config(DefaultFromMail),
SMTPHost: Config(SMTPHost),
SMTPPort: Config(SMTPPort),
SMTPUser: Config(SMTPUser),
SMTPPass: Config(SMTPPass),
}
return vals
}
4 changes: 4 additions & 0 deletions templates/adminindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const adminindexSrc = `
<th><label for="signup_msg">Signup message:</label></th>
<td><input type="text" name="signup_msg" id="signup_msg" value="{{ index .Config "signup_msg" }}"></td>
</tr>
<tr>
<th><label for="censored_words"><div class="col-label">Censored words:</label></th>
<td><textarea name="censored_words" id="censored_words" rows="4" placeholder="shit, bitch, poop">{{ index .Config "censored_words" }}</textarea></td>
</tr>
<tr>
<th><label for="body_appendage"><div class="col-label">Body Appendage:</label></th>
<td><textarea name="body_appendage" id="body_appendage" rows="4" placeholder="<script>Analytics or something</script>">{{ index .Config "body_appendage" }}</textarea></td>
Expand Down
5 changes: 5 additions & 0 deletions views/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ var SignupHandler = UA(func(w http.ResponseWriter, r *http.Request, sess Session
http.Redirect(w, r, "/signup", http.StatusSeeOther)
return
}
if censored := censor(userName); censored != userName {
sess.SetFlashMsg("Fix username: " + censored)
http.Redirect(w, r, "/signup", http.StatusSeeOther)
return
}
hasSpecial := false
for _, ch := range userName {
if (ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z') && ch != '_' && (ch < '0' || ch > '9') {
Expand Down
3 changes: 0 additions & 3 deletions views/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/s-gv/orangeforum/utils"
"strconv"
"strings"
"log"
)

var CommentIndexHandler = UA(func(w http.ResponseWriter, r *http.Request, sess Session) {
Expand Down Expand Up @@ -99,9 +98,7 @@ var CommentCreateHandler = A(func(w http.ResponseWriter, r *http.Request, sess S
if quoteContent[0] == '\n' {
quoteContent = quoteContent[1:]
}
log.Printf("before:\n%s", quoteContent)
quoteContent = quoteRe.ReplaceAllString(quoteContent, "$1> $2")
log.Printf("after:\n%s", quoteContent)
quoteContent = "```\n" + quotedUser + " wrote:\n" + quoteContent + "\n```\n"
}

Expand Down
15 changes: 13 additions & 2 deletions views/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var GroupIndexHandler = UA(func(w http.ResponseWriter, r *http.Request, sess Ses
t := Topic{}
rows.Scan(&t.ID, &t.Title, &t.IsDeleted, &t.IsClosed, &t.NumComments, &t.cDateUnix, &t.Owner)
t.CreatedDate = timeAgoFromNow(time.Unix(t.cDateUnix, 0))
t.Title = censor(t.Title)
topics = append(topics, t)
}

Expand All @@ -80,9 +81,9 @@ var GroupIndexHandler = UA(func(w http.ResponseWriter, r *http.Request, sess Ses
templates.Render(w, "groupindex.html", map[string]interface{}{
"Common": commonData,
"GroupName": name,
"GroupDesc": groupDesc,
"GroupDesc": censor(groupDesc),
"GroupID": groupID,
"HeaderMsg": headerMsg,
"HeaderMsg": censor(headerMsg),
"SubToken": subToken,
"Topics": topics,
"IsMod": isMod,
Expand Down Expand Up @@ -135,6 +136,11 @@ var GroupEditHandler = A(func(w http.ResponseWriter, r *http.Request, sess Sessi
http.Redirect(w, r, "/groups/edit", http.StatusSeeOther)
return
}
if censored := censor(name); censored != name {
sess.SetFlashMsg("Fix group name: " + censored)
http.Redirect(w, r, "/groups/edit", http.StatusSeeOther)
return
}
if len(desc) > 160 {
sess.SetFlashMsg("Group description should have less than 160 characters.")
http.Redirect(w, r, "/groups/edit", http.StatusSeeOther)
Expand Down Expand Up @@ -174,6 +180,11 @@ var GroupEditHandler = A(func(w http.ResponseWriter, r *http.Request, sess Sessi
http.Redirect(w, r, "/groups/edit?id="+groupID, http.StatusSeeOther)
return
}
if censored := censor(name); censored != name {
sess.SetFlashMsg("Fix group name: " + censored)
http.Redirect(w, r, "/groups/edit?id="+groupID, http.StatusSeeOther)
return
}
if len(desc) > 160 {
sess.SetFlashMsg("Group description should have less than 160 characters.")
http.Redirect(w, r, "/groups/edit?id="+groupID, http.StatusSeeOther)
Expand Down
4 changes: 4 additions & 0 deletions views/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var IndexHandler = UA(func(w http.ResponseWriter, r *http.Request, sess Session)
groups = append(groups, Group{})
g := &groups[len(groups)-1]
rows.Scan(&g.Name, &g.Desc, &g.IsSticky)
g.Desc = censor(g.Desc)
}
sort.Slice(groups, func(i, j int) bool {return groups[i].Name < groups[j].Name})
sort.Slice(groups, func(i, j int) bool {return groups[i].IsSticky > groups[j].IsSticky})
Expand All @@ -53,6 +54,7 @@ var IndexHandler = UA(func(w http.ResponseWriter, r *http.Request, sess Session)
var isTopicDeleted, isTopicClosed, isGroupClosed bool
trows.Scan(&t.ID, &t.Title, &t.NumComments, &cDate, &isTopicDeleted, &isTopicClosed, &t.GroupName, &isGroupClosed, &t.OwnerName)
t.CreatedDate = timeAgoFromNow(time.Unix(cDate, 0))
t.Title = censor(t.Title)
if !isTopicDeleted && !isTopicClosed && !isGroupClosed {
topics = append(topics, t)
}
Expand All @@ -77,6 +79,7 @@ var AdminIndexHandler = A(func (w http.ResponseWriter, r *http.Request, sess Ses
if r.Method == "POST" && linkID == "" {
forumName := r.PostFormValue("forum_name")
headerMsg := r.PostFormValue("header_msg")
censoredWords := r.PostFormValue("censored_words")
loginMsg := r.PostFormValue("login_msg")
signupMsg := r.PostFormValue("signup_msg")
signupDisabled := "0"
Expand Down Expand Up @@ -123,6 +126,7 @@ var AdminIndexHandler = A(func (w http.ResponseWriter, r *http.Request, sess Ses
models.WriteConfig(models.LoginMsg, loginMsg)
models.WriteConfig(models.SignupMsg, signupMsg)
models.WriteConfig(models.SignupDisabled, signupDisabled)
models.WriteConfig(models.CensoredWords, censoredWords)
models.WriteConfig(models.GroupCreationDisabled, groupCreationDisabled)
models.WriteConfig(models.ImageUploadEnabled, imageUploadEnabled)
models.WriteConfig(models.AllowGroupSubscription, allowGroupSubscription)
Expand Down
8 changes: 6 additions & 2 deletions views/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"time"
"github.com/s-gv/orangeforum/templates"
"html/template"
)

var UserProfileHandler = UA(func(w http.ResponseWriter, r *http.Request, sess Session) {
Expand Down Expand Up @@ -95,7 +96,7 @@ var UserCommentsHandler = UA(func(w http.ResponseWriter, r *http.Request, sess S

type Comment struct {
ID string
Content string
Content template.HTML
TopicID string
TopicName string
CreatedDate string
Expand All @@ -118,8 +119,10 @@ var UserCommentsHandler = UA(func(w http.ResponseWriter, r *http.Request, sess S
comments = append(comments, Comment{})
c := &comments[len(comments)-1]

rows.Scan(&c.TopicName, &c.TopicID, &c.ID, &c.Content, &c.ImgSrc, &cDate, &c.IsDeleted)
var content string
rows.Scan(&c.TopicName, &c.TopicID, &c.ID, &content, &c.ImgSrc, &cDate, &c.IsDeleted)
c.CreatedDate = timeAgoFromNow(time.Unix(cDate, 0))
c.Content = formatComment(content)
}

if len(comments) >= commentsPerPage {
Expand Down Expand Up @@ -169,6 +172,7 @@ var UserTopicsHandler = UA(func(w http.ResponseWriter, r *http.Request, sess Ses
t := &topics[len(topics)-1]
rows.Scan(&t.ID, &t.Title, &t.IsDeleted, &t.IsClosed, &cDate)
t.CreatedDate = timeAgoFromNow(time.Unix(cDate, 0))
t.Title = censor(t.Title)
}

if len(topics) >= numTopicsPerPage {
Expand Down
2 changes: 1 addition & 1 deletion views/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ var TopicIndexHandler = UA(func(w http.ResponseWriter, r *http.Request, sess Ses
"GroupID": groupID,
"TopicID": topicID,
"GroupName": groupName,
"TopicName": title,
"TopicName": censor(title),
"OwnerName": ownerName,
"CreatedDate": timeAgoFromNow(time.Unix(createdDate, 0)),
"SubToken": subToken,
Expand Down
30 changes: 29 additions & 1 deletion views/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ var boldRe *regexp.Regexp
var codeRe *regexp.Regexp
var oldCodeRe *regexp.Regexp
var quoteRe *regexp.Regexp
var censorRe *regexp.Regexp

var censored string

func init() {
linkRe = regexp.MustCompile("https?://([A-Za-z0-9\\-]+\\.[A-Za-z0-9\\-\\.]+|localhost)(:[0-9]+)?[a-zA-Z0-9@:%_\\+\\.~#?&/=;\\-]*[a-zA-Z0-9@:%_\\+~#?&/=;\\-]")
Expand Down Expand Up @@ -163,7 +166,32 @@ func formatComment(comment string) template.HTML {
comment = linkRe.ReplaceAllString(comment, "<a href=\"$0\">$0</a>")

formatted := "<p>" + comment + "</p>"
return template.HTML(formatted)
return template.HTML(censor(formatted))
}

func censor(content string) string {
cWords := models.Config(models.CensoredWords)
if cWords != censored {
censored = cWords
cWordList := strings.Split(censored, ",")
if len(cWordList) > 0 {
r := "(?i:"
for i, w := range cWordList {
r = r + strings.TrimSpace(w)
if i < len(cWordList)-1 {
r = r + "|"
}
}
r = r + ")"
censorRe = regexp.MustCompile(r)
} else {
censorRe = nil
}
}
if censorRe != nil {
return censorRe.ReplaceAllString(content, "****")
}
return content
}

func saveImage(r *http.Request) string {
Expand Down

0 comments on commit 5c08005

Please sign in to comment.