mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-09-05 11:06:23 -07:00
Replace link TLD checking Go plugin with Lua plugin
This commit is contained in:
parent
b1d182b4eb
commit
6644f90e6a
2 changed files with 55 additions and 67 deletions
|
@ -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
|
||||
}
|
55
sample-plugins/check_links.lua
Normal file
55
sample-plugins/check_links.lua
Normal file
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue