From 6644f90e6a7e29a862cee4c0138bfe322deff24a Mon Sep 17 00:00:00 2001 From: Eggbertx Date: Thu, 15 Jun 2023 11:43:05 -0700 Subject: [PATCH] Replace link TLD checking Go plugin with Lua plugin --- sample-plugins/check-link-tlds/main.go | 67 -------------------------- sample-plugins/check_links.lua | 55 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 67 deletions(-) delete mode 100644 sample-plugins/check-link-tlds/main.go create mode 100644 sample-plugins/check_links.lua diff --git a/sample-plugins/check-link-tlds/main.go b/sample-plugins/check-link-tlds/main.go deleted file mode 100644 index 0d05055e..00000000 --- a/sample-plugins/check-link-tlds/main.go +++ /dev/null @@ -1,67 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "regexp" - - "github.com/gochan-org/gochan/pkg/events" - "github.com/gochan-org/gochan/pkg/gcsql" -) - -var ( - basicUrlRE = regexp.MustCompile(`https?://\S+\.(\S+)`) - recognizedTLDs = []string{"com", "net", "org", "edu", "gov", "us", "uk"} - errUntrustedURLs = errors.New("post contains one or more untrusted links") -) - -func hasUntrustedTLD(msg string) bool { - urls := basicUrlRE.FindAllStringSubmatch(msg, -1) - for _, match := range urls { - tld := match[1] - trusted := false - for _, recognized := range recognizedTLDs { - if tld == recognized { - trusted = true - break - } - } - if !trusted { - return true - } - } - return false -} - -func isNewPoster(post *gcsql.Post) (bool, error) { - var ipCount int - err := gcsql.QueryRowSQL(`SELECT COUNT(*) FROM DBPREFIXposts WHERE ip = ?`, []any{post.IP}, []any{&ipCount}) - return ipCount == 0, err -} - -func InitPlugin() error { - events.RegisterEvent([]string{"message-pre-format"}, func(trigger string, args ...interface{}) error { - if len(args) == 0 { - return nil - } - - post, ok := args[0].(*gcsql.Post) - if !ok { - // argument isn't actually a post - return fmt.Errorf(events.InvalidArgumentErrorStr, trigger) - } - - newPoster, err := isNewPoster(post) - if err != nil { - return err - } - - if newPoster && hasUntrustedTLD(post.MessageRaw) { - return errUntrustedURLs - } - - return nil - }) - - return nil -} diff --git a/sample-plugins/check_links.lua b/sample-plugins/check_links.lua new file mode 100644 index 00000000..623d80c0 --- /dev/null +++ b/sample-plugins/check_links.lua @@ -0,0 +1,55 @@ +local string = require("string") + +local recognized_tlds = {"com", "net", "org", "edu", "gov", "us", "uk"} + +local function is_new_poster(ip) + rows, err = db_query("SELECT COUNT(*) FROM DBPREFIXposts WHERE ip = ?", {ip}) + if(err ~= nil) then + return true, err + end + + is_new = true + while rows:Next() do + rows_table = {} + err = db_scan_rows(rows, rows_table) + if(err ~= nil) then + rows:Close() + return true, err + end + if(rows_table["COUNT(*)"] > 0) then + is_new = false + break + end + end + rows:Close() + return is_new +end + + +event_register({"message-pre-format"}, function(tr, post) + is_new, err = is_new_poster(post.IP) + if(err ~= nil) then + error_log(err:Error()) + :Str("lua", "check_links.lua") + :Str("event", tr) + :Send() + return err:Error() + end + if(is_new == false) then + -- Not a new poster, skip TLD check + return + end + + for tld in string.gmatch(post.MessageRaw, "%a+://%w+.(%w+)") do + found = false + for _, recognized in pairs(recognized_tlds) do + if(tld == recognized) then + found = true + break + end + end + if(found == false) then + return "post contains one or more untrusted links" + end + end +end)