commit 772bb3ff1fd51f87e1aeb1eaa79bcf533d149c6f
parent bb3687e36df819100055904a2fc9e462d6756946
Author: Natasha Kerensikova <natacha@instinctive.eu>
Date: Sun, 26 May 2024 11:23:04 +0200
feat: secondary bot
Diffstat:
M | main.go | | | 53 | +++++++++++++++++++++++++++++++++++++++++++++-------- |
1 file changed, 45 insertions(+), 8 deletions(-)
diff --git a/main.go b/main.go
@@ -26,6 +26,7 @@ type News struct {
}
var newsList []News
+var pendingList []News
// Send an IRC ircMessage, unless in dry-run mode
func ircMessage(client *girc.Client, target, message string) {
@@ -53,6 +54,14 @@ func newsExists(news News) bool {
return false
}
+func saveNews(news News) {
+ if len(newsList) < viper.GetInt("feeds.ringsize") {
+ newsList = append(newsList, news)
+ } else {
+ newsList = append(newsList[1:], news)
+ }
+}
+
// Retrieve a news by its hash
func getNewsByHash(hash string) News {
hash = strings.ReplaceAll(hash, "#", "")
@@ -117,6 +126,22 @@ func newsFetch(client *girc.Client, channel string) {
}
for {
+
+ if viper.GetBool("irc.secondary") {
+ // Post news from previous round if still fresh
+ for _, news := range pendingList {
+ if newsExists(news) {
+ log.Printf("already caught-up %s (%s)\n", news.Title, news.Hash)
+ continue
+ }
+
+ ircMessage(client, channel, fmtNews(news))
+ time.Sleep(viper.GetDuration("irc.delay"))
+ saveNews(news)
+ }
+ pendingList = pendingList[:0]
+ }
+
for _, feedURL := range viper.GetStringSlice("feeds.urls") {
log.Printf("fetching %s...\n", feedURL)
fp := gofeed.NewParser()
@@ -151,15 +176,15 @@ func newsFetch(client *girc.Client, channel string) {
break
}
+ if viper.GetBool("irc.secondary") {
+ // Hold the news for one whole cycle
+ pendingList = append(pendingList, news)
+ continue
+ }
+
ircMessage(client, channel, fmtNews(news))
time.Sleep(viper.GetDuration("irc.delay"))
-
- // Mark item as posted
- if len(newsList) < viper.GetInt("feeds.ringsize") {
- newsList = append(newsList, news)
- } else {
- newsList = append(newsList[1:], news)
- }
+ saveNews(news)
}
}
// save news list to disk to avoid repost when restarting
@@ -185,6 +210,7 @@ func confDefault() {
"irc.xchannels": []string{"goaste2"},
"irc.debug": false,
"irc.dry": false,
+ "irc.secondary": false,
"irc.port": 6667,
"irc.delay": "2s",
"irc.colors.origin": "pink",
@@ -280,6 +306,17 @@ func main() {
dest = e.Source.Name
}
+ if viper.GetBool("irc.secondary") {
+ _, suffix, found := strings.Cut(e.Last(), "#")
+ if found && len(suffix) >= 8 {
+ log.Printf("Received hash %s from %s", suffix[:8], e.Source.Name)
+ news := News{ Hash: suffix[:8] }
+ if !newsExists(news) {
+ saveNews(news)
+ }
+ }
+ }
+
if strings.HasPrefix(e.Last(), "!lsfeeds") {
for i, f := range viper.GetStringSlice("feeds.urls") {
n := strconv.Itoa(i + 1)
@@ -287,7 +324,7 @@ func main() {
time.Sleep(viper.GetDuration("irc.delay"))
}
}
- if strings.HasPrefix(e.Last(), "!xpost") && e.Params[0] == channel {
+ if strings.HasPrefix(e.Last(), "!xpost") && e.Params[0] == channel && !viper.GetBool("irc.secondary") {
requestedHash := getParam(e.Last())
if requestedHash == "" {
return