rapture

moderation tools for thuja
git clone git://kqueue.dev/rapture.git
Log | Files | Refs | README | LICENSE

commit 28c9f43508c1eeb70a668d9db3c72be8a8eb198b
Author: kqueue <kqueue@cocaine.ninja>
Date:   Mon,  2 Jan 2023 15:43:13 -0500

first commit

Diffstat:
ALICENSE | 5+++++
AREADME.md | 11+++++++++++
Ago.mod | 8++++++++
Ago.sum | 4++++
Arapture.go | 127+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 155 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,5 @@ +Copyright 2022 kqueue <kqueue@cocaine.ninja> + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md @@ -0,0 +1,11 @@ +# Rapture is a moderation tool for Thuja +This is a program made to help moderate [thuja](https://kqueue.dev/git/thuja) servers. It can delete a post using it's id or a range of posts. It prints the IP the post was made by so you can add those to your firewall blacklists. + +Run rapture -help for more info + +# examples +delete post of id 3: +``./rapture -staticdir=/home/kqueue/thuja/public -db=/home/kqueue/thuja/my.db -b=Technology -id=3`` + +delete posts 1-5 +``./rapture -staticdir=/home/kqueue/thuja/public -db=/home/kqueue/thuja/my.db -b=Technology -range=1-5`` diff --git a/go.mod b/go.mod @@ -0,0 +1,8 @@ +module kqueue.dev/rapture + +go 1.18 + +require ( + go.etcd.io/bbolt v1.3.6 // indirect + golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d // indirect +) diff --git a/go.sum b/go.sum @@ -0,0 +1,4 @@ +go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= +go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= +golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d h1:L/IKR6COd7ubZrs2oTnTi73IhgqJ71c9s80WsQnh0Es= +golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/rapture.go b/rapture.go @@ -0,0 +1,127 @@ +package main +import ( + bolt "go.etcd.io/bbolt" + "fmt" + "flag" + "encoding/json" + "strconv" + "strings" + "os" +) +// defines json format for all posts made +type postform struct { + Title string `json:"title"` + Text string `json:"text"` + Id int `json:"id"` + Commentsnum int `json:"commentsnum"` + Comments []string `json:"comments"` + Ip string `json:"ip"` +} +var ( + dbpath *string + board *string + id *int + postrange *string + staticdir *string +) +// gets keys out the bolt database +func databaseget(key string, bucket string) (string, error) { + db, err := bolt.Open(*dbpath, 0600, nil) + errmsg := "something fucked up" + if err != nil { + return errmsg, err + } + defer db.Close() + tx, err := db.Begin(true) + if err != nil { + return errmsg, err + } + defer tx.Rollback() + // _, err = tx.CreateBucketIfNotExists([]byte(bucket)) + // if err != nil { + // return errmsg, err + // } + b := tx.Bucket([]byte(bucket)) + v := b.Get([]byte(key)) + if v == nil { + return "empty key", nil + } + return string(v), nil +} +func databaseoverwrite(key string, value string, bucket string) error { + db, err := bolt.Open(*dbpath, 0600, nil) + if err != nil { + return err + } + defer db.Close() + tx, err := db.Begin(true) + if err != nil { + return err + } + defer tx.Rollback() + _, err = tx.CreateBucketIfNotExists([]byte(bucket)) + if err != nil { + return err + } + b := tx.Bucket([]byte(bucket)) + err = b.Delete([]byte(key)) + if err != nil { + return err + } + err = b.Put([]byte(key), []byte(value)) + if err != nil { + return err + } + if err := tx.Commit(); err != nil { + return err + } + return nil +} +func main(){ + dbpath = flag.String("db", "/var/thuja/thuja.db", "where thuja's bolt database is stored, must be readable and writable by thuja") + board = flag.String("b", "nil", "the board to delete the post from") + staticdir = flag.String("staticdir", "/var/thuja/public", "the directory where index.html and image files are stored, must be readable and writable by thuja") + id = flag.Int("id", -1, "the id to delete") + postrange = flag.String("range", "nil", "deletes a range of posts") + flag.Parse() + var deleteid bool + var deleterange bool + if(*board == "nil"){ + fmt.Fprintf(os.Stderr, "specify the board to delete from\n") + return + } + if(*id != -1){ + deleteid = true + } + if(*postrange != "nil"){ + deleterange = true + } + if(*postrange == "nil" && *id == -1){ + fmt.Fprintf(os.Stderr, "either specify a post id or specify a post range\n") + return + } + if(deleteid == true){ + post, err := databaseget(strconv.Itoa(*id), *board) + if (err != nil){ + fmt.Fprintf(os.Stderr, "error occured\n") + return + } + var jsonpost postform + json.Unmarshal([]byte(post), &jsonpost) + fmt.Printf("%s\n", jsonpost.Ip) + databaseoverwrite(strconv.Itoa(*id), "NULL", *board) + path := fmt.Sprintf("%s/%s-%d.jpg", *staticdir, *board, *id) + os.Remove(path) + } + if(deleterange == true){ + v := strings.Split(*postrange, "-") + postid, _ := strconv.Atoi(v[0]) + postmax, _ := strconv.Atoi(v[1]) + for ; postid <= postmax; postid++{ + path := fmt.Sprintf("%s/%s-%d.jpg", *staticdir, *board, *id) + os.Remove(path) + databaseoverwrite(strconv.Itoa(postid), "NULL", *board) + } + } + return +}