From d5ae0bcb0c67c66215db899915352f7112e0848f Mon Sep 17 00:00:00 2001 From: Andriy Cherniy Date: Fri, 5 Jan 2024 00:44:33 +0200 Subject: [PATCH] add whitelist and replace old blacklist files with one --- .gitignore | 4 ++-- main.go | 36 +++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 469158f..6243184 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .env -domains.txt -nicknames.txt +blacklist.txt +whitelist.txt uabot diff --git a/main.go b/main.go index 7b6d8d1..dd8875f 100755 --- a/main.go +++ b/main.go @@ -67,13 +67,13 @@ func main() { log.Fatal(err) } - domainRegexes, err := parseRegexFile(path.Join(ex, "domains.txt")) + blacklistRegexes, err := parseRegexFile(path.Join(ex, "blacklist.txt")) if err != nil { log.Fatal(err) } - nicknameRegexes, err := parseRegexFile(path.Join(ex, "nicknames.txt")) + whitelistRegexes, err := parseRegexFile(path.Join(ex, "whitelist.txt")) if err != nil { log.Fatal(err) @@ -213,23 +213,13 @@ notif_loop: // log.Println(notif) if notif.Type == "follow" { - acct_parsed := strings.Split(notif.Account.Acct, "@") + // acct_parsed := strings.Split(notif.Account.Acct, "@") - for _, regex := range domainRegexes { - if len(acct_parsed) == 2 && regex.MatchString(acct_parsed[1]) { + for _, regex := range blacklistRegexes { + if regex.MatchString(notif.Account.Acct) { _, err := masto_client.AccountBlock(ctx, notif.Account.ID) if err == nil { - matrix_client.SendText(m_id.RoomID(ROOM_ID), fmt.Sprintf("%s заблокований автоматично регексом доменів: %s", notif.Account.Acct, regex.String())) - } - continue notif_loop - } - } - - for _, regex := range nicknameRegexes { - if regex.MatchString(acct_parsed[0]) { - _, err := masto_client.AccountBlock(ctx, notif.Account.ID) - if err == nil { - matrix_client.SendText(m_id.RoomID(ROOM_ID), fmt.Sprintf("%s заблокований автоматично регексом нікнеймів: %s", notif.Account.Acct, regex.String())) + matrix_client.SendText(m_id.RoomID(ROOM_ID), fmt.Sprintf("%s заблокований автоматично регулярним виразом: %s", notif.Account.Acct, regex.String())) } continue notif_loop } @@ -265,7 +255,19 @@ actor: %v`, notif.Status.ID, notif.Status.Content, notif.Account.Acct) } if ok && notif.Status.Visibility == "public" { - log.Debugf("post %v is public and has subscription", notif.Status.ID) + log.Debugf("post %v is public and user has subscription", notif.Status.ID) + + for _, regex := range whitelistRegexes { + if regex.MatchString(notif.Account.Acct) { + _, err = masto_client.Reblog(ctx, notif.Status.ID) + if err != nil { + log.Error(err) + } + log.Debugf("post %v belongs to whitelisted user (%v) by regex: %v", notif.Status.ID, notif.Account.Acct, regex.String()) + continue notif_loop + } + } + if err := db.Create(&Post{ PostId: string(notif.Status.ID), }).Error; err != nil {