diff options
-rw-r--r-- | cacheDatabase.go | 11 | ||||
-rw-r--r-- | client.go | 6 | ||||
-rw-r--r-- | database.go | 51 | ||||
-rw-r--r-- | databaseschema.psql | 4 | ||||
-rw-r--r-- | main.go | 91 | ||||
-rw-r--r-- | static/admin.html | 77 | ||||
-rw-r--r-- | static/faq.html | 2 | ||||
-rw-r--r-- | static/nadmin.html | 53 | ||||
-rw-r--r-- | static/ncatalog.html | 10 |
9 files changed, 201 insertions, 104 deletions
diff --git a/cacheDatabase.go b/cacheDatabase.go index 7ab3b9e..2fe0a38 100644 --- a/cacheDatabase.go +++ b/cacheDatabase.go @@ -5,6 +5,12 @@ import "database/sql" import _ "github.com/lib/pq" func WriteObjectToCache(db *sql.DB, obj ObjectBase) ObjectBase { + + if(IsPostBlacklist(db, obj.Content)){ + fmt.Println("\n\nBlacklist post blocked\n\n") + return obj + } + if len(obj.Attachment) > 0 { if obj.Preview.Href != "" { WritePreviewToCache(db, *obj.Preview) @@ -31,6 +37,11 @@ func WriteObjectToCache(db *sql.DB, obj ObjectBase) ObjectBase { } func WriteActorObjectToCache(db *sql.DB, obj ObjectBase) ObjectBase { + + if(IsPostBlacklist(db, obj.Content)){ + return obj + } + if len(obj.Attachment) > 0 { if IsIDLocal(db, obj.Id) { @@ -63,6 +63,7 @@ type AdminPage struct { Reported []Report Domain string IsLocal bool + PostBlacklist []PostBlacklist } type Report struct { @@ -84,6 +85,11 @@ type NewsItem struct { Time int } +type PostBlacklist struct { + Id int + Regex string +} + func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { t := template.Must(template.New("").Funcs(template.FuncMap{ "mod": func(i, j int) bool { return i%j == 0 }, diff --git a/database.go b/database.go index 5668a4d..0e53142 100644 --- a/database.go +++ b/database.go @@ -1598,3 +1598,54 @@ func WriteNewsToDB(db *sql.DB, news NewsItem) { CheckError(err, "error writing news item") } + +func WriteRegexBlacklistDB(db *sql.DB, regex string) { + query := `select from postblacklist where regex=$1` + + rows, err := db.Query(query, regex) + + CheckError(err, "error select from postblacklist db") + + var re string + defer rows.Close() + rows.Next() + rows.Scan(&re) + + if re != "" { + return + } + + query = `insert into postblacklist (regex) values ($1)` + + _, err = db.Exec(query, regex) + + CheckError(err, "error inserting postblacklist into db") +} + +func GetRegexBlacklistDB(db *sql.DB) []PostBlacklist { + query := `select id, regex from postblacklist` + + rows, err := db.Query(query) + + CheckError(err, "error with select all from postblacklist db") + + var List []PostBlacklist + + defer rows.Close() + for rows.Next() { + var temp PostBlacklist + rows.Scan(&temp.Id, &temp.Regex) + + List = append(List, temp) + } + + return List +} + +func DeleteRegexBlacklistDB(db *sql.DB, id int) { + query := `delete from postblacklist where id=$1` + + _, err := db.Exec(query, id) + + CheckError(err, "error with delete from postblacklist") +} diff --git a/databaseschema.psql b/databaseschema.psql index 02c229b..8c0c0d4 100644 --- a/databaseschema.psql +++ b/databaseschema.psql @@ -227,3 +227,7 @@ ALTER TABLE actor ADD COLUMN IF NOT EXISTS publicKeyPem varchar(100) default ''; ALTER TABLE activitystream ADD COLUMN IF NOT EXISTS sensitive boolean default false; ALTER TABLE cacheactivitystream ADD COLUMN IF NOT EXISTS sensitive boolean default false; +CREATE TABLE IF NOT EXISTS postblacklist( +id serial primary key, +regex varchar(200) +);
\ No newline at end of file @@ -322,6 +322,12 @@ func main() { file, header, _ := r.FormFile("file") + if(IsPostBlacklist(db, r.FormValue("comment"))){ + fmt.Println("\n\nBlacklist post blocked\n\n") + http.Redirect(w, r, Domain + "/", http.StatusMovedPermanently) + return + } + if(file != nil && header.Size > (7 << 20)){ w.Write([]byte("7MB max file size")) return @@ -559,7 +565,9 @@ func main() { adminData.Board.TP = TP adminData.Board.Post.Actor = actor.Id - + + + t.ExecuteTemplate(w, "layout", adminData) } else if admin || actor.Id == Domain { @@ -591,7 +599,9 @@ func main() { adminData.Boards = Boards - adminData.Board.Post.Actor = actor.Id + adminData.Board.Post.Actor = actor.Id + + adminData.PostBlacklist = GetRegexBlacklistDB(db) t.ExecuteTemplate(w, "layout", adminData) } @@ -1203,14 +1213,56 @@ func main() { if Domain != "https://fchan.xyz" { return } - - go AddInstanceToIndexDB(db, actor) }) + http.HandleFunc("/blacklist", func(w http.ResponseWriter, r *http.Request) { + + id, _ := GetPasswordFromSession(r) + + actor := GetActorFromDB(db, Domain) + + if id == "" || (id != actor.Id && id != Domain) { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + + if r.Method == "GET" { + id := r.URL.Query().Get("remove") + + if id != "" { + i, _ := strconv.Atoi(id) + DeleteRegexBlacklistDB(db, i) + } + + } else { + regex := r.FormValue("regex") + testCase := r.FormValue("testCase") + + if regex == "" { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + + r.ParseForm() + + re := regexp.MustCompile(regex) + + if testCase == "" { + WriteRegexBlacklistDB(db, regex) + } else if re.MatchString(testCase) { + WriteRegexBlacklistDB(db, regex) + } + } + + http.Redirect(w, r, "/" + *Key + "#regex", http.StatusSeeOther) + }) + http.HandleFunc("/api/media", func(w http.ResponseWriter, r *http.Request) { - RouteImages(w, r.URL.Query().Get("hash")) + if r.URL.Query().Get("hash") != "" { + RouteImages(w, r.URL.Query().Get("hash")) + } }) fmt.Println("Server for " + Domain + " running on port " + Port) @@ -2590,8 +2642,19 @@ func RouteImages(w http.ResponseWriter, media string) { resp, err := http.DefaultClient.Do(req) - CheckError(err, "error with Route Images resp") - + if err != nil { + fmt.Println("error with Route Images resp " + MediaHashs[media]) + } + + if err != nil || resp.StatusCode == 404 { + fileBytes, err := ioutil.ReadFile("./static/notfound.png") + + CheckError(err, "could not get /static/notfound.png file bytes") + + w.Write(fileBytes) + return + } + defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) @@ -2603,3 +2666,17 @@ func RouteImages(w http.ResponseWriter, media string) { w.Write(body) } + +func IsPostBlacklist(db *sql.DB, comment string) bool { + postblacklist := GetRegexBlacklistDB(db) + + for _, e := range postblacklist { + re := regexp.MustCompile(e.Regex) + + if re.MatchString(comment) { + return true + } + } + + return false +} diff --git a/static/admin.html b/static/admin.html deleted file mode 100644 index f238ec0..0000000 --- a/static/admin.html +++ /dev/null @@ -1,77 +0,0 @@ -<!DOCTYPE html> -<html> - <head> - <title></title> - </head> - <body> - <div style="margin: 0 auto; width: 400px;"> - <h3>Add Board</h3> - <form id="new-board" action="/{{ .Key }}/addboard" method="post" enctype="application/x-www-form-urlencoded"> - <label>Name:</label><br> - <input type="text" name="name" placeholder="g" required><br> - <label>Prefered Name:</label><br> - <input type="text" name="prefname" placeholder="Technology" required><input type="submit" value="Add"><br> - <label>Summary:</label><br> - <textarea name="summary" rows="8" cols="50"></textarea><br> - <label>Restricted:</label><br> - <select name="restricted"> - <option value="True">True</option> - <option value="False">False</option> - </select> - </form> - - <ul style="display: inline-block; padding: 0;"> - <li style="display: inline-block;"><a href="javascript:show('following')">Subscribed</a></li> - <!-- <li style="display: inline-block;"><a href="javascript:show('followers')">Followers</a></li> --> - <li style="display: inline-block;"><a href="javascript:show('reported')">Reported</a></li> - </ul> - - </div> - - <div id="following"> - <h4>Following</h4> - <form id="follow-form" action="/{{ .Key }}/follow" method="post" enctype="application/x-www-form-urlencoded"> - <label>Subscribe:</label><br> - <input id="follow" name="follow" style="margin-bottom: 12px;" placeholder="http://localhost:3000/g"></input><input type="submit" value="Subscribe"><br> - <input type="hidden" name="actor" value="{{ .Actor.Id }}"> - </form> - <ul style="display: inline-block; padding: 0; margin: 0;"> - {{ $actor := .Actor.Id }} - {{ $key := .Key }} - {{ range .Following }} - <li><a href="/{{ $key }}/follow?follow={{ . }}&actor={{ $actor }}">[Unfollow]</a><a href="{{ . }}">{{ . }}</a></li> - {{ end }} - </ul> - </div> - - <div id="followers" style="display: none;"> - <h4>Followers</h4> - <ul style="display: inline-block; padding: 0; margin: 0;"> - {{ range .Followers }} - <li><a href="http://localhost:3000/g">{{ . }}</a></li> - {{ end }} - </ul> - </div> - - <div id="reported" style="display: none;"> - <h4>Reported</h4> - <ul style="display: inline-block; padding: 0; margin: 0;"> - </ul> - </div> - </body> -</html> - -<script> - function show(element) - { - var following = document.getElementById("following"); - // var followers = document.getElementById("followers"); - var reported = document.getElementById("reported"); - - following.style.display = "none"; - // followers.style.display = "none"; - reported.style.display = "none"; - - document.getElementById(element).style.display = "block"; - } -</script> diff --git a/static/faq.html b/static/faq.html index 95e51c5..5d70e8e 100644 --- a/static/faq.html +++ b/static/faq.html @@ -56,7 +56,7 @@ <p>Soon™.</p> <h4 id="version">What version is this FChannel instance?</h4> - <p>v0.0.8-release</p> + <p>v0.0.9-dev</p> </div> <div style="width: 500px; margin:0 auto; margin-top: 50px; text-align: center;"> <a href="/">[Home]</a><a href="/static/rules.html">[Rules]</a><a href="/static/faq.html">[FAQ]</a> diff --git a/static/nadmin.html b/static/nadmin.html index 88b92c9..5e38151 100644 --- a/static/nadmin.html +++ b/static/nadmin.html @@ -11,30 +11,20 @@ <input type="text" name="prefname" placeholder="Technology" required><input type="submit" value="Add"><br> <label>Summary:</label><br> <textarea name="summary" rows="8" cols="50"></textarea><br> - <label>Restricted:</label><br> + <label>Restricted (i.e SFW):</label><br> <select name="restricted"> <option value="True">True</option> <option value="False">False</option> </select> </form> <ul style="display: inline-block; padding: 0;"> - <li style="display: inline-block;"><a href="#following">Subscribed</a></li> + <li style="display: inline-block;"><a href="#news">[Create News]</a></li> + <li style="display: inline-block;"><a href="#regex">[Post Blacklist]</a></li> <!-- <li style="display: inline-block;"><a href="javascript:show('followers')">Followers</a></li> --> - <li style="display: inline-block;"><a href="#reported">Reported</a></li> + <!-- <li style="display: inline-block;"><a href="#reported">Reported</a></li> --> </ul> </div> -<div class="popup-box" style="margin-bottom: 25px; padding: 12px;"> - <h3>Post News</h3> - <form id="news" action="/{{ .Key }}/postnews" method="post" enctype="application/x-www-form-urlencoded"> - <label>Title:</label><br> - <input type="text" name="title" placeholder="New Board" required><input type="submit" value="Post"><br> - <label>Content:</label><br> - <textarea name="summary" rows="8" cols="50"></textarea><br> - </form> -</div> - - <div id="following" class="popup-box" style="margin-bottom: 25px; padding: 12px;"> <h4 style="margin: 0; margin-bottom: 5px;">Subscribed</h4> @@ -60,11 +50,38 @@ </ul> </div> -<div id="reported" class="popup-box" style="margin-bottom: 25px; padding: 12px;"> - <h4 style="margin: 0; margin-bottom: 5px;">Reported</h4> - <ul style="display: inline-block; padding: 0; margin: 0; list-style-type: none;"> - </ul> +<div class="popup-box" style="margin-bottom: 25px; padding: 12px;"> + <h3>Create News</h3> + <form id="news" action="/{{ .Key }}/postnews" method="post" enctype="application/x-www-form-urlencoded"> + <label>Title:</label><br> + <input type="text" name="title" placeholder="New Board" required><input type="submit" value="Post"><br> + <label>Content:</label><br> + <textarea name="summary" rows="8" cols="50"></textarea><br> + </form> </div> + +<div id="regex" class="popup-box" style="margin-bottom: 25px; padding: 12px;"> + <h3>Regex Post Blacklist</h3> + <form id="blacklist" action="/blacklist" method="post" enctype="application/x-www-form-urlencoded"> + <label>Regex:</label><br> + <input type="text" name="regex" placeholder="(?i)(?s)(.+)?stuff?(.+)to(.+)?block(.+)?https?://(.+)?" size="38" required><input style="margin-left: 5px;" type="submit" value="Post"><br> + <label>Test Case:</label><br> + <textarea name="testCase" rows="8" cols="50" placeholder="enter a test case to block, if it passes the regex will be added to the blacklist. (?i) for case insesitive (?s) to span multiple lines"></textarea><br> + </form> + {{ if .PostBlacklist }} + <ul style="display: inline-block; padding: 0; margin: 0; margin-top: 25px; list-style-type: none;"> + {{ range .PostBlacklist }} + <li>{{ .Regex }} <a href="/blacklist?remove={{ .Id }}">[remove]</a></li> + {{ end }} + </ul> + {{ end }} +</div> + +<!-- <div id="reported" class="popup-box" style="margin-bottom: 25px; padding: 12px;"> --> +<!-- <h4 style="margin: 0; margin-bottom: 5px;">Reported</h4> --> +<!-- <ul style="display: inline-block; padding: 0; margin: 0; list-style-type: none;"> --> +<!-- </ul> --> +<!-- </div> --> {{ end }} {{ define "bottom" }}{{ end }} diff --git a/static/ncatalog.html b/static/ncatalog.html index 4c6c589..68bb01a 100644 --- a/static/ncatalog.html +++ b/static/ncatalog.html @@ -67,7 +67,15 @@ img.style = "max-width: 180px; max-height: 180px;" img.setAttribute("id", "img") img.setAttribute("main", "1") - img.setAttribute("src", "{{ proxy (index .Attachment 0).Href }}") + img.setAttribute("enlarge", "0") + img.setAttribute("attachment", "{{ proxy (index .Attachment 0).Href }}") + {{ if .Preview.Href }} + img.setAttribute("src", "{{ proxy .Preview.Href }}") + img.setAttribute("preview", "{{ proxy .Preview.Href }}") + {{ else }} + img.setAttribute("src", "{{ proxy (index .Attachment 0).Href }}") + img.setAttribute("preview", "{{ proxy (index .Attachment 0).Href }}") + {{ end }} media.appendChild(img) } |