This commit is contained in:
fade 2022-09-08 14:28:32 -04:00
parent 660e3758ca
commit 30a93f2466
4 changed files with 90 additions and 80 deletions

156
bot.go
View File

@ -24,6 +24,8 @@ var (
) )
func RunBot() { func RunBot() {
LoggerInit()
events, err := c.StreamingUser(ctx) events, err := c.StreamingUser(ctx)
if err != nil { if err != nil {
ErrorLogger.Println("Streaming") ErrorLogger.Println("Streaming")
@ -45,7 +47,7 @@ func RunBot() {
// New follower // New follower
if notif.Type == "follow" { if notif.Type == "follow" {
acct := notif.Status.Account.Acct acct := notif.Account.Acct
if !exist_in_database(acct) { // Add to db and post welcome message if !exist_in_database(acct) { // Add to db and post welcome message
InfoLogger.Printf("%s followed", acct) InfoLogger.Printf("%s followed", acct)
@ -68,94 +70,92 @@ func RunBot() {
content := notif.Status.Content content := notif.Status.Content
tooturl := notif.Status.URL tooturl := notif.Status.URL
for i := range followers { // Follow check
if acct == string(followers[i].Acct) { // Follow check if check_following(followers, acct) {
if notif.Status.Visibility == "public" { // Reblog toot if notif.Status.Visibility == "public" { // Reblog toot
if notif.Status.InReplyToID == nil { // Not boost replies if notif.Status.InReplyToID == nil { // Not boost replies
// Duplicate protection // Duplicate protection
content_hash := sha512.New() content_hash := sha512.New()
content_hash.Write([]byte(content)) content_hash.Write([]byte(content))
hash := fmt.Sprintf("%x", content_hash.Sum(nil)) hash := fmt.Sprintf("%x", content_hash.Sum(nil))
if !check_msg_hash(hash) { if !check_msg_hash(hash) {
save_msg_hash(hash) save_msg_hash(hash)
InfoLogger.Printf("Hash of %s added to database", tooturl) InfoLogger.Printf("Hash of %s added to database", tooturl)
} else {
WarnLogger.Printf("%s is a duplicate and not boosted", tooturl)
break
}
// Add to db if needed
if !exist_in_database(acct) {
add_to_db(acct)
InfoLogger.Printf("%s added to database", acct)
}
// Message order
if check_order(acct) < Conf.Order_limit {
if check_ticket(acct) > 0 { // Message limit
take_ticket(acct)
InfoLogger.Printf("Ticket of %s was taken", acct)
count_order(acct)
InfoLogger.Printf("Order of %s was counted", acct)
c.Reblog(ctx, notif.Status.ID)
InfoLogger.Printf("Toot %s of %s was rebloged", tooturl, acct)
} else {
WarnLogger.Printf("%s haven't tickets", acct)
}
} else {
WarnLogger.Printf("%s order limit", acct)
}
} else { } else {
WarnLogger.Printf("%s is reply and not boosted", tooturl) WarnLogger.Printf("%s is a duplicate and not boosted", tooturl)
} }
} else if notif.Status.Visibility == "direct" { // Admin commands
for y := 0; y < len(Conf.Admins); y++ {
if acct == Conf.Admins[y] {
recmd := regexp.MustCompile(`<[^>]+>`)
command := recmd.ReplaceAllString(content, "")
args := strings.Split(command, " ")
if len(args) == 3 { // Add to db if needed
mID := mastodon.ID((args[2]))
switch args[1] {
case "unboost":
c.Unreblog(ctx, mID)
WarnLogger.Printf("%s was unrebloged", mID)
case "delete":
c.DeleteStatus(ctx, mID)
WarnLogger.Printf("%s was deleted", mID)
}
}
} else {
continue
}
}
} else {
WarnLogger.Printf("%s is not public toot and not boosted", tooturl)
break
}
}
if i == len(followers)-1 { // Notify user
if got_notice(acct) == 0 {
if !exist_in_database(acct) { if !exist_in_database(acct) {
add_to_db(acct) add_to_db(acct)
InfoLogger.Printf("%s added to database", acct) InfoLogger.Printf("%s added to database", acct)
} }
message := fmt.Sprintf("@%s %s", acct, Conf.NotFollowedMessage) // Message order
_, err := postToot(message, "direct") if check_order(acct) < Conf.Order_limit {
if err != nil { if check_ticket(acct) > 0 { // Message limit
ErrorLogger.Printf("Notify %s", acct) take_ticket(acct)
} InfoLogger.Printf("Ticket of %s was taken", acct)
InfoLogger.Printf("%s has been notified", acct)
mark_notice(acct) count_order(acct)
InfoLogger.Printf("%s marked notification in database", acct) InfoLogger.Printf("Order of %s was counted", acct)
c.Reblog(ctx, notif.Status.ID)
InfoLogger.Printf("Toot %s of %s was rebloged", tooturl, acct)
} else {
WarnLogger.Printf("%s haven't tickets", acct)
}
} else {
WarnLogger.Printf("%s order limit", acct)
}
} else {
WarnLogger.Printf("%s is reply and not boosted", tooturl)
} }
} else if notif.Status.Visibility == "direct" { // Admin commands
for y := range Conf.Admins {
if acct == Conf.Admins[y] {
recmd := regexp.MustCompile(`<[^>]+>`)
command := recmd.ReplaceAllString(content, "")
args := strings.Split(command, " ")
if len(args) == 3 {
mID := mastodon.ID((args[2]))
switch args[1] {
case "unboost":
c.Unreblog(ctx, mID)
WarnLogger.Printf("%s was unrebloged", mID)
case "delete":
c.DeleteStatus(ctx, mID)
WarnLogger.Printf("%s was deleted", mID)
default:
WarnLogger.Printf("%s entered wrong command", acct)
}
} else {
WarnLogger.Printf("%s entered wrong command", acct)
}
}
}
} else {
WarnLogger.Printf("%s is not public toot and not boosted", tooturl)
}
} else { // Notify user
if got_notice(acct) == 0 {
if !exist_in_database(acct) {
add_to_db(acct)
InfoLogger.Printf("%s added to database", acct)
}
message := fmt.Sprintf("@%s %s", acct, Conf.NotFollowedMessage)
_, err := postToot(message, "direct")
if err != nil {
ErrorLogger.Printf("Notify %s", acct)
}
InfoLogger.Printf("%s has been notified", acct)
mark_notice(acct)
InfoLogger.Printf("%s marked notification in database", acct)
} }
} }
} }

View File

@ -15,6 +15,8 @@ var (
func DeleteNotices() { func DeleteNotices() {
wg.Done() wg.Done()
LoggerInit()
for { for {
statuses, err := c.GetAccountStatuses(ctx, my_account.ID, &mastodon.Pagination{Limit: 60}) statuses, err := c.GetAccountStatuses(ctx, my_account.ID, &mastodon.Pagination{Limit: 60})
if err != nil { if err != nil {

View File

@ -1,8 +1,6 @@
package main package main
func main() { func main() {
LoggerInit()
wg.Add(1) wg.Add(1)
go DeleteNotices() go DeleteNotices()

View File

@ -11,3 +11,13 @@ func postToot(toot string, vis string) (*mastodon.Status, error) {
status, err := c.PostStatus(ctx, &conToot) status, err := c.PostStatus(ctx, &conToot)
return status, err return status, err
} }
// Check following
func check_following(followers []*mastodon.Account, acct string) bool {
for i := range followers {
if acct == string(followers[i].Acct) {
return true
}
}
return false
}