2022-12-11 04:08:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
"go.arsenm.dev/go-lemmy"
|
|
|
|
"go.arsenm.dev/go-lemmy/types"
|
|
|
|
"go.arsenm.dev/logger/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
configPath := pflag.StringP("config", "c", "./lemmy-reply-bot.toml", "Path to the config file")
|
|
|
|
dryRun := pflag.BoolP("dry-run", "D", false, "Don't actually send comments, just check for matches")
|
|
|
|
pflag.Parse()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err := loadConfig(*configPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error loading config file").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
2022-12-13 02:23:29 +00:00
|
|
|
c, err := lemmy.NewWebSocket(cfg.Lemmy.InstanceURL)
|
2022-12-11 04:08:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error creating new Lemmy API client").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Login(ctx, types.Login{
|
|
|
|
UsernameOrEmail: cfg.Lemmy.Account.UserOrEmail,
|
|
|
|
Password: cfg.Lemmy.Account.Password,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error logging in to Lemmy instance").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Successfully logged in to Lemmy instance").Send()
|
|
|
|
|
2022-12-13 02:23:29 +00:00
|
|
|
err = c.Request(types.UserOpUserJoin, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error joining WebSocket user context").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Request(types.UserOpCommunityJoin, types.CommunityJoin{
|
|
|
|
CommunityID: 0,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error joining WebSocket community context").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
2022-12-11 04:08:29 +00:00
|
|
|
replyCh := make(chan replyJob, 200)
|
|
|
|
|
|
|
|
if !*dryRun {
|
|
|
|
go commentReplyWorker(ctx, c, replyCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
commentWorker(ctx, c, replyCh)
|
|
|
|
}
|
|
|
|
|
2022-12-13 02:23:29 +00:00
|
|
|
func commentWorker(ctx context.Context, c *lemmy.WSClient, replyCh chan<- replyJob) {
|
2022-12-11 04:08:29 +00:00
|
|
|
repliedIDs := map[int]struct{}{}
|
|
|
|
|
|
|
|
repliedStore, err := os.Open("replied.bin")
|
|
|
|
if err == nil {
|
|
|
|
err = msgpack.NewDecoder(repliedStore).Decode(&repliedIDs)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Error decoding reply store").Err(err).Send()
|
|
|
|
}
|
|
|
|
repliedStore.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(10 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
2022-12-13 02:23:29 +00:00
|
|
|
case res := <-c.Responses():
|
|
|
|
// Check which operation has been sent from the server
|
|
|
|
switch res.Op {
|
|
|
|
case types.UserOpCreateComment, types.UserOpEditComment:
|
|
|
|
var cr types.CommentResponse
|
|
|
|
err = lemmy.DecodeResponse(res.Data, &cr)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Error while trying to decode comment").Err(err).Send()
|
|
|
|
continue
|
|
|
|
}
|
2022-12-11 04:08:29 +00:00
|
|
|
|
2022-12-13 02:23:29 +00:00
|
|
|
if _, ok := repliedIDs[cr.CommentView.Comment.ID]; ok {
|
2022-12-11 04:08:29 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, reply := range cfg.Replies {
|
|
|
|
re := compiledRegexes[reply.Regex]
|
2022-12-13 02:23:29 +00:00
|
|
|
if !re.MatchString(cr.CommentView.Comment.Content) {
|
2022-12-11 04:08:29 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Matched comment body").
|
|
|
|
Int("reply-index", i).
|
2022-12-13 02:23:29 +00:00
|
|
|
Int("comment-id", cr.CommentView.Comment.ID).
|
2022-12-11 04:08:29 +00:00
|
|
|
Send()
|
|
|
|
|
|
|
|
job := replyJob{
|
2022-12-13 02:23:29 +00:00
|
|
|
CommentID: cr.CommentView.Comment.ID,
|
|
|
|
PostID: cr.CommentView.Comment.PostID,
|
2022-12-11 04:08:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 02:23:29 +00:00
|
|
|
matches := re.FindStringSubmatch(cr.CommentView.Comment.Content)
|
2022-12-11 04:08:29 +00:00
|
|
|
job.Content = expandStr(reply.Msg, func(s string) string {
|
|
|
|
i, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
2022-12-11 21:53:26 +00:00
|
|
|
log.Debug("Message variable is not an integer, returning empty string").Str("var", s).Send()
|
2022-12-11 04:08:29 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-12-11 21:53:26 +00:00
|
|
|
if i+1 > len(matches) {
|
|
|
|
log.Debug("Message variable exceeds match length").Int("length", len(matches)).Int("var", i).Send()
|
2022-12-11 04:08:29 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-12-11 21:53:26 +00:00
|
|
|
log.Debug("Message variable found, returning").Int("var", i).Str("found", matches[i]).Send()
|
2022-12-11 04:08:29 +00:00
|
|
|
return matches[i]
|
|
|
|
})
|
|
|
|
|
|
|
|
replyCh <- job
|
|
|
|
|
2022-12-13 02:23:29 +00:00
|
|
|
repliedIDs[cr.CommentView.Comment.ID] = struct{}{}
|
2022-12-11 04:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
repliedStore, err := os.Create("replied.bin")
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Error creating reply store file").Err(err).Send()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = msgpack.NewEncoder(repliedStore).Encode(repliedIDs)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Error encoding replies to reply store").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
repliedStore.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type replyJob struct {
|
|
|
|
Content string
|
|
|
|
CommentID int
|
|
|
|
PostID int
|
|
|
|
}
|
|
|
|
|
2022-12-13 02:23:29 +00:00
|
|
|
func commentReplyWorker(ctx context.Context, c *lemmy.WSClient, ch <-chan replyJob) {
|
2022-12-11 04:08:29 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case reply := <-ch:
|
2022-12-13 02:23:29 +00:00
|
|
|
err := c.Request(types.UserOpCreateComment, types.CreateComment{
|
2022-12-11 04:08:29 +00:00
|
|
|
PostID: reply.PostID,
|
|
|
|
ParentID: types.NewOptional(reply.CommentID),
|
|
|
|
Content: reply.Content,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Error while trying to create new comment").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Created new comment").
|
|
|
|
Int("post-id", reply.PostID).
|
|
|
|
Int("parent-id", reply.CommentID).
|
|
|
|
Send()
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandStr(s string, mapping func(string) string) string {
|
|
|
|
strings.ReplaceAll(s, "$$", "${_escaped_dollar_symbol}")
|
|
|
|
return os.Expand(s, func(s string) string {
|
|
|
|
if s == "_escaped_dollar_symbol" {
|
|
|
|
return "$"
|
|
|
|
}
|
|
|
|
return mapping(s)
|
|
|
|
})
|
|
|
|
}
|