update logging, minor fixes
continuous-integration/drone/push Build is passing Details

This commit is contained in:
stryan 2023-10-10 21:01:01 -04:00
parent 80adf6214e
commit 40ec2610df
5 changed files with 102 additions and 41 deletions

13
go.mod
View File

@ -8,8 +8,19 @@ require (
)
require (
github.com/stretchr/testify v1.8.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.8.0 // indirect
github.com/charmbracelet/log v0.2.5 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

26
go.sum
View File

@ -4,20 +4,46 @@ git.saintnet.tech/stryan/matrixbotlib v0.1.2 h1:kmldNm1xyKm2t8vPUqyQ+7AvhQ0CeT/m
git.saintnet.tech/stryan/matrixbotlib v0.1.2/go.mod h1:Id9JBCt3YqOViUwSVXQomGysaWPVyoDk2fbmOf9L264=
git.saintnet.tech/stryan/matrixbotlib v0.1.3 h1:SJ80s6J3/wBwHCj78mXmFxHhLc/EQ9Tt6O/Y8hsRs2Q=
git.saintnet.tech/stryan/matrixbotlib v0.1.3/go.mod h1:Id9JBCt3YqOViUwSVXQomGysaWPVyoDk2fbmOf9L264=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdyCQk84RU=
github.com/charmbracelet/lipgloss v0.8.0/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU=
github.com/charmbracelet/log v0.2.5 h1:1yVvyKCKVV639RR4LIq1iy1Cs1AKxuNO+Hx2LJtk7Wc=
github.com/charmbracelet/log v0.2.5/go.mod h1:nQGK8tvc4pS9cvVEH/pWJiZ50eUq1aoXUOjGpXvdD0k=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20220708220712-1185a9018129 h1:vucSRfWwTsoXro7P+3Cjlr6flUMtzCwzlvkxEQtHHB0=
golang.org/x/net v0.0.0-20220708220712-1185a9018129/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

66
main.go
View File

@ -1,29 +1,30 @@
package main
import (
"log"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"github.com/charmbracelet/log"
mbl "git.saintnet.tech/stryan/matrixbotlib"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
//GitTag is current git tag
// GitTag is current git tag
var GitTag string
//GitCommit is current git commit
// GitCommit is current git commit
var GitCommit string
func main() {
conf, err := mbl.LoadMatrixClientConfig("config.yaml")
if err != nil {
log.Println("no local config found, checking /etc/nunbot")
log.Info("no local config found, checking /etc/nunbot")
conf, err = mbl.LoadMatrixClientConfig("/etc/nunbot/config.yaml")
if err != nil {
panic(err)
@ -46,7 +47,7 @@ func main() {
store := mautrix.NewAccountDataStore("nun.batch", matrixClient)
fID, err := matrixClient.CreateFilter(dataFilter)
if err != nil {
//don't want to continue if we can't keep state
// don't want to continue if we can't keep state
panic(err)
}
var nun *nunWatch
@ -55,9 +56,9 @@ func main() {
matrixClient.Store = store
syncer := matrixClient.Syncer.(*mautrix.DefaultSyncer)
mbl.AcceptAllRoomInvites(matrixClient)
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
syncer.OnEventType(event.EventMessage, func(_ mautrix.EventSource, evt *event.Event) {
if evt.Sender == matrixClient.UserID {
return //ignore events from self
return // ignore events from self
}
cmd, err := mbl.ParseCommand(evt, "nun")
if err != nil {
@ -70,27 +71,48 @@ func main() {
case "version":
// print version
if GitTag != "" {
matrixClient.SendText(evt.RoomID, "NunBot version "+GitTag)
_, err := matrixClient.SendText(evt.RoomID, "NunBot version "+GitTag)
if err != nil {
log.Warn(err)
}
} else {
matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit)
_, err := matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit)
if err != nil {
log.Warn(err)
}
}
case "timer":
if len(cmd) <= 1 {
matrixClient.SendText(evt.RoomID, "!nun timer [time in minutes to wait between tries]")
_, err := matrixClient.SendText(evt.RoomID, "!nun timer [time in minutes to wait between tries]")
if err != nil {
log.Warn(err)
}
return
}
time, err := strconv.Atoi(cmd[1])
if err != nil {
matrixClient.SendText(evt.RoomID, "!nun timer needs integer timer")
_, err = matrixClient.SendText(evt.RoomID, "!nun timer needs integer timer")
if err != nil {
log.Warn(err)
}
return
}
nun.SetTimer(time)
matrixClient.SendText(evt.RoomID, "timer set")
_, err = matrixClient.SendText(evt.RoomID, "timer set")
if err != nil {
log.Warn(err)
}
case "help":
matrixClient.SendText(evt.RoomID, "Supported commands: version, help,timer")
_, err := matrixClient.SendText(evt.RoomID, "Supported commands: version, help,timer")
if err != nil {
log.Warn(err)
}
default:
//command not found
matrixClient.SendText(evt.RoomID, "command not recognized")
// command not found
_, err := matrixClient.SendText(evt.RoomID, "command not recognized")
if err != nil {
log.Warn(err)
}
}
})
var wg sync.WaitGroup
@ -99,7 +121,7 @@ func main() {
signal.Notify(c, os.Interrupt)
go func() {
<-c
log.Println("trying to shutdown cleanly")
log.Info("trying to shutdown cleanly")
matrixClient.StopSync()
stop <- true
}()
@ -107,23 +129,21 @@ func main() {
nun = newNunWatch(stop, matrixClient, 2)
wg.Add(1)
go func() {
err = matrixClient.Sync()
if err != nil {
log.Println(err)
log.Warn(err)
}
wg.Done()
log.Println("matrix client shutdown")
log.Info("matrix client shutdown")
}()
wg.Add(1)
go func() {
nun.Main()
wg.Done()
log.Println("nun watch shutdown")
log.Info("nun watch shutdown")
}()
log.Println("nunbot running")
log.Info("nunbot running")
wg.Wait()
log.Println("shutting down")
log.Info("shutting down")
}

28
nun.go
View File

@ -2,9 +2,10 @@ package main
import (
"fmt"
"log"
"time"
"github.com/charmbracelet/log"
"maunium.net/go/mautrix"
)
@ -36,25 +37,25 @@ func (n *nunWatch) Main() {
newPost, err := getNewestPost("LittleNuns")
if err != nil {
log.Printf("error getting newest post: %v", err)
log.Println("skipping this cycle, incrementing fail count")
log.Warn("error getting newest post:", "error", err)
log.Warn("skipping this cycle, incrementing fail count")
n.fail++
if n.fail > 5 {
log.Fatal("fail count too high; ending loop")
log.Fatal("failgithub.com/charmbracelet/log count too high; ending loop")
return
} else if n.fail > 3 {
log.Printf("over three failures, increasing tick time to %v minutes", 3*n.timer)
log.Warnf("over three failures, increasing tick time to %v minutes", 3*n.timer)
ticker.Reset(3 * time.Duration(n.timer) * time.Minute)
break
} else {
log.Printf("fail count %v", n.fail)
log.Warnf("fail count %v", n.fail)
break
}
}
n.fail = 0
ticker.Reset(time.Duration(n.timer) * time.Minute)
if curT != n.timer {
log.Println("updating nunwatch timer")
log.Info("updating nunwatch timer")
ticker.Reset(time.Duration(n.timer) * time.Minute)
n.fail = 0
curT = n.timer
@ -65,23 +66,26 @@ func (n *nunWatch) Main() {
var cur post
err := n.client.GetAccountData("nun.newest", &cur)
if err != nil {
log.Printf("tried to get saved account data but failed %v", err)
log.Warnf("tried to get saved account data but failed %v", err)
cur.Number = 0
}
roomResp, err := n.client.JoinedRooms()
if err != nil {
log.Printf("error getting joined rooms: %v", err)
log.Warnf("error getting joined rooms: %v", err)
break
}
rooms := roomResp.JoinedRooms
for _, room := range rooms {
log.Printf("posting %v", n.curPost.Number)
if n.curPost.Number != cur.Number {
n.client.SendText(room, fmt.Sprintf("%v\n%v", n.curPost.Title, n.curPost.Link))
log.Infof("posting %v", n.curPost.Number)
_, err := n.client.SendText(room, fmt.Sprintf("%v\n%v", n.curPost.Title, n.curPost.Link))
if err != nil {
log.Printf("err sending to room: %v", err)
}
if n.curPost.Number > 0 {
err := n.client.SetAccountData("nun.newest", n.curPost)
if err != nil {
log.Printf("error saving current post: %v", err)
log.Warnf("error saving current post: %v", err)
}
}
}

View File

@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"regexp"
"strconv"
@ -21,12 +21,12 @@ type post struct {
func getNewestPost(subreddit string) (post, error) {
var resp redditResp
re := regexp.MustCompile("([0-9]+)")
//building request from scratch because reddit api is weird
// building request from scratch because reddit api is weird
url := fmt.Sprintf("https://www.reddit.com/r/%v/new.json?sort=new&limit=1", subreddit)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "Custom Agent")
req.Header.Set("Host", "reddit.com")
var defaultClient = http.Client{
defaultClient := http.Client{
Transport: &http.Transport{
TLSNextProto: map[string]func(authority string, c *tls.Conn) http.RoundTripper{},
},
@ -40,7 +40,7 @@ func getNewestPost(subreddit string) (post, error) {
}
}
defer res.Body.Close()
body, readErr := ioutil.ReadAll(res.Body)
body, readErr := io.ReadAll(res.Body)
if readErr != nil {
return post{}, err
}
@ -53,7 +53,7 @@ func getNewestPost(subreddit string) (post, error) {
}
reSearch := re.FindStringSubmatch(resp.Data.Children[0].Data.Title)
if len(reSearch) == 0 {
return post{}, fmt.Errorf("Something strange in data from reddit")
return post{}, fmt.Errorf("something strange in data from reddit")
}
numS := reSearch[0]
num, err := strconv.Atoi(numS)