Compare commits

..

No commits in common. "master" and "v0.2.2" have entirely different histories.

9 changed files with 48 additions and 251 deletions

View File

@ -1,7 +0,0 @@
.git
.gitignore
Dockerfile*
README.md
LICENSE
*.yaml
statefile

View File

@ -1,20 +0,0 @@
steps:
- name: submodules
image: alpine/git
commands:
- git submodule update --init --recursive
- name: build-container
image: plugins/docker
settings:
repo: git.saintnet.tech/stryan/nunbot
registry: git.saintnet.tech
password:
from_secret: build_pass
username:
from_secret: build_username
dockerfile: Containerfile
tags: latest
layers: true
when:
- branch: master
event: push

View File

@ -1,13 +0,0 @@
FROM golang:1.18 as builder
WORKDIR /go/src/app
COPY . .
RUN apt update && apt upgrade -y
RUN go build
FROM alpine:latest as final
WORKDIR /srv/
RUN mkdir /srv/nunbot
RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
COPY --from=builder /go/src/app/nunbot /srv/nunbot/
CMD ["/srv/nunbot/nunbot"]

13
go.mod
View File

@ -8,19 +8,8 @@ require (
)
require (
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
github.com/stretchr/testify v1.8.0 // 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,46 +4,20 @@ 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=

View File

@ -1,14 +0,0 @@
[Unit]
Description=NunBot: Bringing nuns, ducks and love to Matrix
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker run --name nunbot --mount=type=bind,source=/etc/nunbot/config.yaml,destination=/etc/nunbot/config.yaml git.saintnet.tech/stryan/nunbot
ExecStop=/usr/bin/docker rm -f nunbot
[Install]
WantedBy=default.target

101
main.go
View File

@ -1,64 +1,40 @@
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.Info("no local config found, checking /etc/nunbot")
conf, err = mbl.LoadMatrixClientConfig("/etc/nunbot/config.yaml")
conf, err = mbl.LoadMatrixClientConfig("/etc/config.yaml")
if err != nil {
panic(err)
}
}
dataFilter := &mautrix.Filter{
AccountData: mautrix.FilterPart{
Limit: 20,
NotTypes: []event.Type{
event.NewEventType("nun.batch"),
},
},
}
matrixClient, err := mbl.NewMatrixClient(conf, nil)
store := mbl.NewLazyMemStore(conf.Statefile)
matrixClient, err := mbl.NewMatrixClient(conf, store)
if err != nil {
panic(err)
}
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
panic(err)
}
var nun *nunWatch
uid := id.NewUserID(strings.ToLower(conf.Username), strings.ToLower(conf.Domain))
store.SaveFilterID(uid, fID.FilterID)
matrixClient.Store = store
syncer := matrixClient.Syncer.(*mautrix.DefaultSyncer)
mbl.AcceptAllRoomInvites(matrixClient)
syncer.OnEventType(event.EventMessage, func(_ mautrix.EventSource, evt *event.Event) {
syncer.OnEventType(event.EventMessage, func(source 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 {
@ -67,52 +43,19 @@ func main() {
}
return
}
switch cmd[0] {
switch cmd[1] {
case "version":
// print version
if GitTag != "" {
_, err := matrixClient.SendText(evt.RoomID, "NunBot version "+GitTag)
if err != nil {
log.Warn(err)
}
matrixClient.SendText(evt.RoomID, "NunBot version "+GitTag)
} else {
_, err := matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit)
if err != nil {
log.Warn(err)
}
}
case "timer":
if len(cmd) <= 1 {
_, 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 {
_, err = matrixClient.SendText(evt.RoomID, "!nun timer needs integer timer")
if err != nil {
log.Warn(err)
}
return
}
nun.SetTimer(time)
_, err = matrixClient.SendText(evt.RoomID, "timer set")
if err != nil {
log.Warn(err)
matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit)
}
case "help":
_, err := matrixClient.SendText(evt.RoomID, "Supported commands: version, help,timer")
if err != nil {
log.Warn(err)
}
matrixClient.SendText(evt.RoomID, "Supported commands: version, help")
default:
// command not found
_, err := matrixClient.SendText(evt.RoomID, "command not recognized")
if err != nil {
log.Warn(err)
}
//command not found
matrixClient.SendText(evt.RoomID, "command not recognized")
}
})
var wg sync.WaitGroup
@ -121,29 +64,31 @@ func main() {
signal.Notify(c, os.Interrupt)
go func() {
<-c
log.Info("trying to shutdown cleanly")
log.Println("trying to shutdown cleanly")
matrixClient.StopSync()
stop <- true
}()
nun = newNunWatch(stop, matrixClient, 2)
nun := newNunWatch(stop, matrixClient)
wg.Add(1)
go func() {
err = matrixClient.Sync()
if err != nil {
log.Warn(err)
log.Println(err)
}
wg.Done()
log.Info("matrix client shutdown")
log.Println("matrix client shutdown")
}()
wg.Add(1)
go func() {
nun.Main()
wg.Done()
log.Info("nun watch shutdown")
log.Println("nun watch shutdown")
}()
log.Info("nunbot running")
log.Println("nunbot running")
wg.Wait()
log.Info("shutting down")
log.Println("shutting down")
}

70
nun.go
View File

@ -2,10 +2,9 @@ package main
import (
"fmt"
"log"
"time"
"github.com/charmbracelet/log"
"maunium.net/go/mautrix"
)
@ -14,81 +13,42 @@ type nunWatch struct {
curPost post
stop chan bool
client *mautrix.Client
timer int
}
func newNunWatch(stop chan bool, c *mautrix.Client, t int) *nunWatch {
return &nunWatch{0, post{}, stop, c, t}
}
func (n *nunWatch) SetTimer(t int) {
n.timer = t
n.fail = 0
func newNunWatch(stop chan bool, c *mautrix.Client) *nunWatch {
return &nunWatch{0, post{}, stop, c}
}
func (n *nunWatch) Main() {
ticker := time.NewTicker(time.Duration(n.timer) * time.Minute)
curT := n.timer
ticker := time.NewTicker(30 * time.Second)
for {
select {
case <-n.stop:
return
case <-ticker.C:
if n.fail > 5 {
log.Fatal("fail count too high; ending loop")
return
}
newPost, err := getNewestPost("LittleNuns")
if err != nil {
log.Warn("error getting newest post:", "error", err)
log.Warn("skipping this cycle, incrementing fail count")
log.Printf("error getting newest post: %v", err)
log.Println("skipping this cycle, incrementing fail count")
n.fail++
if n.fail > 5 {
log.Fatal("failgithub.com/charmbracelet/log count too high; ending loop")
return
} else if n.fail > 3 {
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.Warnf("fail count %v", n.fail)
break
}
continue
}
n.fail = 0
ticker.Reset(time.Duration(n.timer) * time.Minute)
if curT != n.timer {
log.Info("updating nunwatch timer")
ticker.Reset(time.Duration(n.timer) * time.Minute)
n.fail = 0
curT = n.timer
}
if n.curPost.Title != newPost.Title {
n.curPost = newPost
var cur post
err := n.client.GetAccountData("nun.newest", &cur)
if err != nil {
log.Warnf("tried to get saved account data but failed %v", err)
cur.Number = 0
}
roomResp, err := n.client.JoinedRooms()
if err != nil {
log.Warnf("error getting joined rooms: %v", err)
break
log.Printf("error getting joined rooms: %v", err)
continue
}
rooms := roomResp.JoinedRooms
for _, room := range rooms {
if n.curPost.Number != cur.Number {
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.Warnf("error saving current post: %v", err)
}
}
}
n.client.SendText(room, fmt.Sprintf("%v\n%v", n.curPost.Title, n.curPost.Link))
}
}

View File

@ -3,30 +3,25 @@ package main
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"time"
)
type post struct {
Title string
Link string
Number int
Title string
Link string
}
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")
defaultClient := http.Client{
var defaultClient = http.Client{
Transport: &http.Transport{
TLSNextProto: map[string]func(authority string, c *tls.Conn) http.RoundTripper{},
},
@ -40,7 +35,7 @@ func getNewestPost(subreddit string) (post, error) {
}
}
defer res.Body.Close()
body, readErr := io.ReadAll(res.Body)
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return post{}, err
}
@ -48,21 +43,9 @@ func getNewestPost(subreddit string) (post, error) {
if err != nil {
return post{}, err
}
if len(resp.Data.Children) < 1 {
return post{}, errors.New("bad data from reddit")
}
reSearch := re.FindStringSubmatch(resp.Data.Children[0].Data.Title)
if len(reSearch) == 0 {
return post{}, fmt.Errorf("something strange in data from reddit")
}
numS := reSearch[0]
num, err := strconv.Atoi(numS)
if err != nil || numS == "" {
num = -1
}
return post{
Title: resp.Data.Children[0].Data.Title,
Link: resp.Data.Children[0].Data.URL,
Number: num,
Title: resp.Data.Children[0].Data.Title,
Link: resp.Data.Children[0].Data.URL,
}, nil
}