gruik

Fork of GCU-Squad's RSS-to-IRC bridge
git clone https://git.instinctive.eu/gruik.git
Log | Files | Refs | README | LICENSE

commit 167eed43e004013c8fa3e4f934dd8cfad16b0e4d
parent 7b52af3365e4c832904ffac23d8d287b5a44854f
Author: Emile "iMil" Heitor <imil@netbsd.org>
Date:   Sun, 26 May 2024 18:06:34 +0000

Merge branch 'secondary' into 'main'

Secondary

See merge request gcu-squad/gruik!6
Diffstat:
Mmain.go | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 63 insertions(+), 16 deletions(-)

diff --git a/main.go b/main.go @@ -26,6 +26,16 @@ 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) { + if viper.GetBool("irc.dry") { + log.Printf("ircMessage to %s: %s", target, message) + } else { + client.Cmd.Message(target, message) + } +} // Create a hash out of link func mkHash(s string) string { @@ -44,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, "#", "") @@ -108,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() @@ -142,15 +176,15 @@ func newsFetch(client *girc.Client, channel string) { break } - client.Cmd.Message(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) + 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")) + saveNews(news) } } // save news list to disk to avoid repost when restarting @@ -162,7 +196,7 @@ func newsFetch(client *girc.Client, channel string) { } encoder := json.NewEncoder(f) if err = encoder.Encode(newsList); err != nil { - client.Cmd.Message(channel, "could write newsList") + ircMessage(client, channel, "could not write newsList") } time.Sleep(viper.GetDuration("feeds.frequency")) } @@ -175,6 +209,8 @@ func confDefault() { "irc.channel": "goaste", "irc.xchannels": []string{"goaste2"}, "irc.debug": false, + "irc.dry": false, + "irc.secondary": false, "irc.port": 6667, "irc.delay": "2s", "irc.colors.origin": "pink", @@ -270,14 +306,25 @@ 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) - c.Cmd.Message(dest, n+". "+f) + ircMessage(c, dest, n+". "+f) 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 @@ -286,7 +333,7 @@ func main() { post := fmt.Sprintf(" {r}(from %s on %s)", e.Source.Name, channel) message := fmtNews(news) + girc.Fmt(post) for _, xchan := range viper.GetStringSlice("irc.xchannels") { - c.Cmd.Message(xchan, message) + ircMessage(c, xchan, message) time.Sleep(viper.GetDuration("irc.delay")) } } @@ -294,7 +341,7 @@ func main() { if strings.HasPrefix(e.Last(), "!latest") && e.Params[0] != channel { args := strings.SplitN(e.Last(), " ", 3) if len(args) < 2 { - c.Cmd.Message(dest, "usage: !latest <number> [origin]") + ircMessage(c, dest, "usage: !latest <number> [origin]") time.Sleep(viper.GetDuration("irc.delay")) return } @@ -302,7 +349,7 @@ func main() { // n == number of news to show n, err := strconv.Atoi(args[1]) if err != nil || n <= 0 { - c.Cmd.Message(dest, "conversion error") + ircMessage(c, dest, "conversion error") time.Sleep(viper.GetDuration("irc.delay")) return } @@ -319,7 +366,7 @@ func main() { // check if some news are available if numNews < 1 { - c.Cmd.Message(dest, "no news available") + ircMessage(c, dest, "no news available") time.Sleep(viper.GetDuration("irc.delay")) return } @@ -331,7 +378,7 @@ func main() { numNews-- for i := 0; i < n; i++ { fmt.Println(i) - c.Cmd.Message(dest, fmtNews(showNews[numNews-i])) + ircMessage(c, dest, fmtNews(showNews[numNews-i])) time.Sleep(viper.GetDuration("irc.delay")) } }