From 7e006245775bc0cb14ac097f65ba57d3e3774bd8 Mon Sep 17 00:00:00 2001 From: fade Date: Sun, 28 Aug 2022 12:37:43 -0400 Subject: [PATCH] add logging --- .gitignore | 2 + README.md | 8 ++-- bot.go | 44 +++++++++++++++------ config.go | 14 ++++--- limits.go | 34 ++++++++-------- logger.go | 22 +++++++++++ main.go | 4 +- services/openrc/mastodon-group-bot | 1 + services/systemd/mastodon-group-bot.service | 2 +- 9 files changed, 91 insertions(+), 40 deletions(-) create mode 100644 logger.go diff --git a/.gitignore b/.gitignore index 03cc852..b02bf8a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ go.mod go.sum *.db +*.log +/mastodon-group-bot diff --git a/README.md b/README.md index 3b093ff..78f2128 100644 --- a/README.md +++ b/README.md @@ -36,13 +36,15 @@ go build ``` # Setup services -For first copy config, binary and make dirs +For first make dirs, copy config and binary ``` mkdir /etc/mastodon-group-bot mkdir /var/lib/mastodon-group-bot +mkdir /var/log/mastodon-group-bot chown nobody /var/lib/mastodon-group-bot -cp mastodon-group-bot /usr/bin/mastodon-group-bot +chown nobody /var/log/mastodon-group-bot cp config.json /etc/mastodon-group-bot/config.json +cp mastodon-group-bot /usr/bin/mastodon-group-bot ``` ## Systemd @@ -57,5 +59,5 @@ cp ./services/openrc/mastodon-group-bot /etc/init.d/mastodon-group-bot # Usage ``` -mastodon-group-bot -config -db +mastodon-group-bot -config -db -log ``` \ No newline at end of file diff --git a/bot.go b/bot.go index 8f0a8d8..90d5690 100644 --- a/bot.go +++ b/bot.go @@ -3,14 +3,15 @@ package main import ( "context" "fmt" - "log" "regexp" "strings" "github.com/mattn/go-mastodon" ) -func run_bot(Conf Config, DB string) { +func RunBot(Conf Config) { + logger_init() + c := mastodon.NewClient(&mastodon.Config{ Server: Conf.Server, ClientID: Conf.ClientID, @@ -21,16 +22,16 @@ func run_bot(Conf Config, DB string) { ctx := context.Background() events, err := c.StreamingUser(ctx) if err != nil { - log.Fatal(err) + ErrorLogger.Println("Streaming") } my_account, err := c.GetAccountCurrentUser(ctx) if err != nil { - log.Fatal(err) + ErrorLogger.Println("Fetch account info") } followers, err := c.GetAccountFollowers(ctx, my_account.ID, &mastodon.Pagination{Limit: 60}) if err != nil { - log.Fatal(err) + ErrorLogger.Println("Fetch followers") } // Run bot @@ -55,10 +56,18 @@ func run_bot(Conf Config, DB string) { // New follower if notif.Type == "follow" { acct := notif.Account.Acct - if !followed(acct, DB) { // Add to db and post welcome message - add_to_db(acct, Conf.Max_toots, DB) + if !followed(acct) { // Add to db and post welcome message + InfoLogger.Printf("%s followed", acct) + + add_to_db(acct, Conf.Max_toots) + InfoLogger.Printf("%s added to database", acct) + var message = fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct) - postToot(message, "public") + err := postToot(message, "public") + if err != nil { + ErrorLogger.Println("Post welcome message") + } + InfoLogger.Printf("%s was welcomed", acct) } } @@ -69,13 +78,20 @@ func run_bot(Conf Config, DB string) { 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, DB) { // Add to db if needed - add_to_db(acct, Conf.Max_toots, DB) + if !followed(acct) { // Add to db if needed + add_to_db(acct, Conf.Max_toots) + InfoLogger.Printf("%s added to database", acct) } - if check_ticket(acct, Conf.Max_toots, Conf.Toots_interval, DB) > 0 { // Limit - take_ticket(acct, DB) + if check_ticket(acct, Conf.Max_toots, Conf.Toots_interval) > 0 { // Limit + take_ticket(acct) + InfoLogger.Printf("Ticket of %s was taken", acct) c.Reblog(ctx, notif.Status.ID) + InfoLogger.Printf("Toot %s of %s was rebloged", notif.Status.URL, acct) + } else { + WarnLogger.Printf("%s haven't tickets", acct) } + } else { + WarnLogger.Printf("%s is reply and not boosted", notif.Status.URL) } } else if notif.Status.Visibility == "direct" { // Admin commands for y := 0; y < len(Conf.Admins); y++ { @@ -98,8 +114,12 @@ func run_bot(Conf Config, DB string) { c.AccountUnblock(ctx, mID) } } + } else { + break } } + } else { + break } } } diff --git a/config.go b/config.go index 5b503d0..d6d930f 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,12 @@ import ( "os" ) +var ( + ConfPath = flag.String("config", "config.json", "Path to config") + DBPath = flag.String("db", "limits.db", "Path to database") + LogPath = flag.String("log", "mastodon-group-bot.log", "Path to log") +) + type Config struct { Server string `json:"Server"` ClientID string `json:"ClientID"` @@ -18,18 +24,16 @@ type Config struct { Admins []string `json:"Admins"` } -func read_conf() (Config, *string) { - ConfPath := flag.String("config", "config.json", "Path to config") - DBPath := flag.String("db", "limits.db", "Path to database") +func ReadConf() Config { flag.Parse() data, err := os.ReadFile(*ConfPath) if err != nil { - log.Fatal(err) + log.Fatal("Failed to read config") } var Conf Config json.Unmarshal(data, &Conf) - return Conf, DBPath + return Conf } diff --git a/limits.go b/limits.go index c3b36af..3722f2b 100644 --- a/limits.go +++ b/limits.go @@ -3,22 +3,22 @@ package main import ( "database/sql" "fmt" - "log" + "time" _ "github.com/mattn/go-sqlite3" ) // Init database -func init_limit_db(DBPath string) *sql.DB { - db, err := sql.Open("sqlite3", DBPath) +func init_limit_db() *sql.DB { + db, err := sql.Open("sqlite3", *DBPath) if err != nil { - log.Fatal(err) + ErrorLogger.Println("Open database") } cmd := `CREATE TABLE IF NOT EXISTS Limits (id INTEGER PRIMARY KEY AUTOINCREMENT, acct TEXT, ticket INTEGER, time TEXT)` stat, err := db.Prepare(cmd) if err != nil { - log.Fatal(err) + ErrorLogger.Println("Create database") } stat.Exec() @@ -26,19 +26,19 @@ func init_limit_db(DBPath string) *sql.DB { } // Add account to database -func add_to_db(acct string, limit uint16, DBPath string) { - db := init_limit_db(DBPath) +func add_to_db(acct string, limit uint16) { + db := init_limit_db() cmd := `INSERT INTO Limits (acct, ticket) VALUES (?, ?)` stat, err := db.Prepare(cmd) if err != nil { - log.Fatal(err) + ErrorLogger.Println("Add account to databse") } stat.Exec(acct, limit) } // Take ticket for tooting -func take_ticket(acct string, DBPath string) { - db := init_limit_db(DBPath) +func take_ticket(acct string) { + db := init_limit_db() cmd1 := `SELECT ticket FROM Limits WHERE acct = ?` cmd2 := `UPDATE Limits SET ticket = ?, time = ? WHERE acct = ?` @@ -50,7 +50,7 @@ func take_ticket(acct string, DBPath string) { stat, err := db.Prepare(cmd2) if err != nil { - log.Fatal(err) + ErrorLogger.Println("Take ticket") } now := time.Now() @@ -60,13 +60,13 @@ func take_ticket(acct string, DBPath string) { } // Check followed once -func followed(acct string, DBPath string) bool { - db := init_limit_db(DBPath) +func followed(acct string) bool { + db := init_limit_db() cmd := `SELECT acct FROM Limits WHERE acct = ?` err := db.QueryRow(cmd, acct).Scan(&acct) if err != nil { if err != sql.ErrNoRows { - log.Fatal(err) + ErrorLogger.Println("Check followed") } return false @@ -76,8 +76,8 @@ func followed(acct string, DBPath string) bool { } // Check ticket availability -func check_ticket(acct string, ticket uint16, toots_interval uint16, DBPath string) uint16 { - db := init_limit_db(DBPath) +func check_ticket(acct string, ticket uint16, toots_interval uint16) uint16 { + db := init_limit_db() cmd1 := `SELECT ticket FROM Limits WHERE acct = ?` cmd2 := `SELECT time FROM Limits WHERE acct = ?` @@ -97,7 +97,7 @@ func check_ticket(acct string, ticket uint16, toots_interval uint16, DBPath stri cmd := `UPDATE Limits SET ticket = ? WHERE acct = ?` stat, err := db.Prepare(cmd) if err != nil { - log.Fatal(err) + ErrorLogger.Println("Check ticket availability") } stat.Exec(ticket, acct) diff --git a/logger.go b/logger.go new file mode 100644 index 0000000..60e785e --- /dev/null +++ b/logger.go @@ -0,0 +1,22 @@ +package main + +import ( + "log" + "os" +) + +var ( + InfoLogger *log.Logger + WarnLogger *log.Logger + ErrorLogger *log.Logger +) + +func logger_init() { + file, err := os.OpenFile(*LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) + if err != nil { + log.Fatal("Failed to read log file") + } + InfoLogger = log.New(file, "[INFO] ", log.LstdFlags|log.Lshortfile) + WarnLogger = log.New(file, "[WARNING] ", log.LstdFlags|log.Lshortfile) + ErrorLogger = log.New(file, "[ERROR] ", log.LstdFlags|log.Lshortfile) +} diff --git a/main.go b/main.go index 137856e..7f5823e 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main func main() { - config, db := read_conf() + config := ReadConf() - run_bot(config, *db) + RunBot(config) } diff --git a/services/openrc/mastodon-group-bot b/services/openrc/mastodon-group-bot index f042b4a..1040f76 100644 --- a/services/openrc/mastodon-group-bot +++ b/services/openrc/mastodon-group-bot @@ -3,6 +3,7 @@ name=$RC_SVCNAME command="/usr/bin/$name" command_arg1="-config /etc/$name/config.json" command_arg2="-db /var/lib/$name/limits.db" +command_arg3="-log /var/log/$name/$name.log" pidfile="/run/$name.pid" user="nobody" description="Mastodon group bot which reposts toots" diff --git a/services/systemd/mastodon-group-bot.service b/services/systemd/mastodon-group-bot.service index d2a780d..64d950b 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/config.json -db /var/lib/mastodon-group-bot/limits.db +ExecStart=/usr/bin/mastodon-group-bot -config /etc/mastodon-group-bot/config.json -db /var/lib/mastodon-group-bot/limits.db -log /var/log/mastodon-group-bot/mastodon-group-bot.log [Install] WantedBy=multi-user.target \ No newline at end of file