From d1439f87ac0474aa3d0ec17730d59ae2a92ebd29 Mon Sep 17 00:00:00 2001 From: fade Date: Fri, 26 Aug 2022 06:56:21 -0400 Subject: [PATCH] some fixes --- README.md | 17 ++++++++++----- bot.go | 24 +++++++++++++-------- config.go | 5 +++-- limits.go | 20 ++++++++--------- main.go | 4 ++-- services/openrc/mastodon-group-bot | 10 ++++----- services/systemd/mastodon-group-bot.service | 4 ++-- 7 files changed, 49 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 0b976c0..3b093ff 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,12 @@ This is a bot which implements group functionality in Mastodon. * Limit of toots per hour * Admin commands +### Admin commands +* unboost \ +* delete \ +* block \ +* unblock \ + # Configuration The bot is configured in a JSON file that looks like this: ``` @@ -30,10 +36,13 @@ go build ``` # Setup services -For first copy config and binary +For first copy config, binary and make dirs ``` +mkdir /etc/mastodon-group-bot +mkdir /var/lib/mastodon-group-bot +chown nobody /var/lib/mastodon-group-bot cp mastodon-group-bot /usr/bin/mastodon-group-bot -cp config.json /etc/mastodon-group-bot.json +cp config.json /etc/mastodon-group-bot/config.json ``` ## Systemd @@ -48,7 +57,5 @@ cp ./services/openrc/mastodon-group-bot /etc/init.d/mastodon-group-bot # Usage ``` -Usage of mastodon-group-bot: - -config string - Path to config (default "config.json") +mastodon-group-bot -config -db ``` \ No newline at end of file diff --git a/bot.go b/bot.go index 8e72c4d..c7ebfc3 100644 --- a/bot.go +++ b/bot.go @@ -10,7 +10,7 @@ import ( "github.com/mattn/go-mastodon" ) -func run_bot(Conf Config) { +func run_bot(Conf Config, DB string) { c := mastodon.NewClient(&mastodon.Config{ Server: Conf.Server, ClientID: Conf.ClientID, @@ -24,8 +24,14 @@ func run_bot(Conf Config) { log.Fatal(err) } - my_account, _ := c.GetAccountCurrentUser(ctx) - followers, _ := c.GetAccountFollowers(ctx, my_account.ID, &mastodon.Pagination{Limit: 60}) + my_account, err := c.GetAccountCurrentUser(ctx) + if err != nil { + log.Fatal(err) + } + followers, err := c.GetAccountFollowers(ctx, my_account.ID, &mastodon.Pagination{Limit: 60}) + if err != nil { + log.Fatal(err) + } // Run bot for { @@ -49,8 +55,8 @@ func run_bot(Conf Config) { // New follower if notif.Type == "follow" { acct := notif.Account.Acct - if !followed(acct) { // Add to db and post welcome message - add_to_db(acct, Conf.Max_toots) + if !followed(acct, DB) { // Add to db and post welcome message + add_to_db(acct, Conf.Max_toots, DB) var message = fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct) postToot(message, "public") } @@ -63,11 +69,11 @@ func run_bot(Conf Config) { if acct == string(followers[i].Acct) { // Follow check if notif.Status.Visibility == "public" { // Reblog toot if notif.Status.InReplyToID == nil { // Not boost replies - if !followed(acct) { // Add to db if needed - add_to_db(acct, Conf.Max_toots) + if !followed(acct, DB) { // Add to db if needed + add_to_db(acct, Conf.Max_toots, DB) } - if check_ticket(acct, Conf.Max_toots, Conf.Toots_interval) > 0 { // Limit - take_ticket(acct) + if check_ticket(acct, Conf.Max_toots, Conf.Toots_interval, DB) > 0 { // Limit + take_ticket(acct, DB) c.Reblog(ctx, notif.Status.ID) } } diff --git a/config.go b/config.go index 1c88362..5b503d0 100644 --- a/config.go +++ b/config.go @@ -18,8 +18,9 @@ type Config struct { Admins []string `json:"Admins"` } -func read_conf() Config { +func read_conf() (Config, *string) { ConfPath := flag.String("config", "config.json", "Path to config") + DBPath := flag.String("db", "limits.db", "Path to database") flag.Parse() data, err := os.ReadFile(*ConfPath) @@ -30,5 +31,5 @@ func read_conf() Config { var Conf Config json.Unmarshal(data, &Conf) - return Conf + return Conf, DBPath } diff --git a/limits.go b/limits.go index dd84e1b..c3b36af 100644 --- a/limits.go +++ b/limits.go @@ -10,8 +10,8 @@ import ( ) // Init database -func init_limit_db() *sql.DB { - db, err := sql.Open("sqlite3", "limits.db") +func init_limit_db(DBPath string) *sql.DB { + db, err := sql.Open("sqlite3", DBPath) if err != nil { log.Fatal(err) } @@ -26,8 +26,8 @@ func init_limit_db() *sql.DB { } // Add account to database -func add_to_db(acct string, limit uint16) { - db := init_limit_db() +func add_to_db(acct string, limit uint16, DBPath string) { + db := init_limit_db(DBPath) cmd := `INSERT INTO Limits (acct, ticket) VALUES (?, ?)` stat, err := db.Prepare(cmd) if err != nil { @@ -37,8 +37,8 @@ func add_to_db(acct string, limit uint16) { } // Take ticket for tooting -func take_ticket(acct string) { - db := init_limit_db() +func take_ticket(acct string, DBPath string) { + db := init_limit_db(DBPath) cmd1 := `SELECT ticket FROM Limits WHERE acct = ?` cmd2 := `UPDATE Limits SET ticket = ?, time = ? WHERE acct = ?` @@ -60,8 +60,8 @@ func take_ticket(acct string) { } // Check followed once -func followed(acct string) bool { - db := init_limit_db() +func followed(acct string, DBPath string) bool { + db := init_limit_db(DBPath) cmd := `SELECT acct FROM Limits WHERE acct = ?` err := db.QueryRow(cmd, acct).Scan(&acct) if err != nil { @@ -76,8 +76,8 @@ func followed(acct string) bool { } // Check ticket availability -func check_ticket(acct string, ticket uint16, toots_interval uint16) uint16 { - db := init_limit_db() +func check_ticket(acct string, ticket uint16, toots_interval uint16, DBPath string) uint16 { + db := init_limit_db(DBPath) cmd1 := `SELECT ticket FROM Limits WHERE acct = ?` cmd2 := `SELECT time FROM Limits WHERE acct = ?` diff --git a/main.go b/main.go index 31444bc..137856e 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main func main() { - config := read_conf() + config, db := read_conf() - run_bot(config) + run_bot(config, *db) } diff --git a/services/openrc/mastodon-group-bot b/services/openrc/mastodon-group-bot index 4cf5242..f042b4a 100644 --- a/services/openrc/mastodon-group-bot +++ b/services/openrc/mastodon-group-bot @@ -1,18 +1,18 @@ #!/sbin/openrc-run name=$RC_SVCNAME command="/usr/bin/$name" -command_args="-config" -cfgfile="/etc/$name.json" +command_arg1="-config /etc/$name/config.json" +command_arg2="-db /var/lib/$name/limits.db" pidfile="/run/$name.pid" -user="nobody:nobody" +user="nobody" description="Mastodon group bot which reposts toots" start() { ebegin "Starting $name" - start-stop-daemon -bm -S -u $user -p $pidfile -x $command -- $command_args $cfgfile + start-stop-daemon -bm -S -u $user -p $pidfile -x $command -- $command_arg1 $command_arg2 eend $? } stop() { ebegin "Stopping $name" start-stop-daemon -K -p $pidfile eend $? -} +} \ No newline at end of file diff --git a/services/systemd/mastodon-group-bot.service b/services/systemd/mastodon-group-bot.service index ae3cc86..d2a780d 100644 --- a/services/systemd/mastodon-group-bot.service +++ b/services/systemd/mastodon-group-bot.service @@ -6,7 +6,7 @@ Wants=network-online.target [Service] Type=simple User=nobody -ExecStart=/usr/bin/mastodon-group-bot -config /etc/mastodon-group-bot.json +ExecStart=/usr/bin/mastodon-group-bot -config /etc/mastodon-group-bot/config.json -db /var/lib/mastodon-group-bot/limits.db [Install] -WantedBy=multi-user.target +WantedBy=multi-user.target \ No newline at end of file