gruik

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

commit 712931d77ee49e16d0ef7335f062a6df099ae038
parent fed197cd524705b4a84ea22bf9578540d2da4856
Author: Emile "iMil" Heitor <imil@netbsd.org>
Date:   Tue, 18 Jul 2023 11:12:20 +0000

Merge branch 'modif' into 'main'

Modif

See merge request gcu-squad/gruik!2
Diffstat:
Mmain.go | 153+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 87 insertions(+), 66 deletions(-)

diff --git a/main.go b/main.go @@ -68,15 +68,17 @@ func getNewsByOrigin(origin string) []News { } func fmtNews(news News) string { - return fmt.Sprintf("[{%s}%s{r}] {%s}%s{r} {%s}%s{r} {%s}#%s{r}", - viper.GetString("irc.colors.origin"), - news.Origin, - viper.GetString("irc.colors.title"), - news.Title, - viper.GetString("irc.colors.link"), - news.Link, - viper.GetString("irc.colors.hash"), - news.Hash) + colorReset := girc.Fmt("{c}") + colorOrigin := girc.Fmt(fmt.Sprintf("{%s}", viper.GetString("irc.colors.origin"))) + colorTitle := girc.Fmt(fmt.Sprintf("{%s}", viper.GetString("irc.colors.title"))) + colorLink := girc.Fmt(fmt.Sprintf("{%s}", viper.GetString("irc.colors.link"))) + colorHash := girc.Fmt(fmt.Sprintf("{%s}", viper.GetString("irc.colors.hash"))) + + return fmt.Sprintf("[%s%s%s] %s%s%s %s%s%s %s#%s%s", + colorOrigin, news.Origin, colorReset, + colorTitle, news.Title, colorReset, + colorLink, news.Link, colorReset, + colorHash, news.Hash, colorReset) } // Fetch and post news from RSS feeds @@ -140,8 +142,7 @@ func newsFetch(client *girc.Client, channel string) { break } - client.Cmd.Message(channel, girc.Fmt(fmtNews(news))) - + client.Cmd.Message(channel, fmtNews(news)) time.Sleep(viper.GetDuration("irc.delay")) // Mark item as posted @@ -268,9 +269,7 @@ func main() { if len(e.Params) > 0 && e.Params[0] != channel { dest = e.Source.Name } - if isOp(e.Source.Name) && strings.HasPrefix(e.Last(), "!die") { - c.Close() - } + if strings.HasPrefix(e.Last(), "!lsfeeds") { for i, f := range viper.GetStringSlice("feeds.urls") { n := strconv.Itoa(i + 1) @@ -278,75 +277,53 @@ func main() { time.Sleep(viper.GetDuration("irc.delay")) } } - if isOp(e.Source.Name) && strings.HasPrefix(e.Last(), "!addfeed") { - s := getParam(e.Last()) - if s == "" { - return - } - ss := append(viper.GetStringSlice("feeds.urls"), s) - viper.Set("feeds.urls", ss) - c.Cmd.ReplyTo(e, girc.Fmt("feed {b}{green}added{c}{b}")) - if err := viper.WriteConfig(); err != nil { - c.Cmd.ReplyTo(e, girc.Fmt("adding feed {b}{red}failed{c}{b}")) - } - } - if isOp(e.Source.Name) && strings.HasPrefix(e.Last(), "!rmfeed") { - s := getParam(e.Last()) - if s == "" { - return - } - ss := viper.GetStringSlice("feeds.urls") - i, err := strconv.Atoi(s) - if err != nil { - c.Cmd.ReplyTo(e, "index conversion failed") - return - } - if i < 1 || i > len(ss) { - c.Cmd.ReplyTo(e, "bad index number") - return - } - ss = append(ss[:i-1], ss[i:]...) - viper.Set("feeds.urls", ss) - c.Cmd.ReplyTo(e, girc.Fmt("feed {b}{green}removed{c}{b}")) - if err := viper.WriteConfig(); err != nil { - c.Cmd.ReplyTo(e, girc.Fmt("removing feed {b}{red}failed{c}{b}")) - } - } if strings.HasPrefix(e.Last(), "!xpost") && e.Params[0] == channel { - s := getParam(e.Last()) - if s == "" { + requestedHash := getParam(e.Last()) + if requestedHash == "" { return } - for _, xchan := range viper.GetStringSlice("irc.xchannels") { - if news := getNewsByHash(s); news.Hash != "" { - post := fmt.Sprintf(" {r}(from %s on %s)", e.Source.Name, channel) - c.Cmd.Message(xchan, girc.Fmt(fmtNews(news)+post)) + if news := getNewsByHash(requestedHash); news.Hash != "" { + 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) + time.Sleep(viper.GetDuration("irc.delay")) } } } if strings.HasPrefix(e.Last(), "!latest") && e.Params[0] != channel { - numNews := len(newsList) - if numNews < 1 { - c.Cmd.Message(dest, "no news available") - return - } - p := strings.SplitN(e.Last(), " ", 3) - if len(p) < 2 { + args := strings.SplitN(e.Last(), " ", 3) + if len(args) < 2 { c.Cmd.Message(dest, "usage: !latest <number> [origin]") + time.Sleep(viper.GetDuration("irc.delay")) return } + // n == number of news to show - n, err := strconv.Atoi(p[1]) - if err != nil { + n, err := strconv.Atoi(args[1]) + if err != nil || n <= 0 { c.Cmd.Message(dest, "conversion error") + time.Sleep(viper.GetDuration("irc.delay")) return } + + // default to all news showNews := newsList + numNews := len(showNews) + // there was a second parameter, specific origin - if len(p) > 2 { - showNews = getNewsByOrigin(p[2]) + if len(args) > 2 { + showNews = getNewsByOrigin(args[2]) numNews = len(showNews) } + + // check if some news are available + if numNews < 1 { + c.Cmd.Message(dest, "no news available") + time.Sleep(viper.GetDuration("irc.delay")) + return + } + // user gave a greater number that we have news if n > numNews { n = numNews @@ -354,10 +331,54 @@ func main() { numNews-- for i := 0; i < n; i++ { fmt.Println(i) - c.Cmd.Message(dest, girc.Fmt(fmtNews(showNews[numNews-i]))) + c.Cmd.Message(dest, fmtNews(showNews[numNews-i])) time.Sleep(viper.GetDuration("irc.delay")) } } + + + // All commands below requires OP + if ! isOp(e.Source.Name) { + return + } + + if strings.HasPrefix(e.Last(), "!die") { + c.Close() + } + if strings.HasPrefix(e.Last(), "!addfeed") { + url := getParam(e.Last()) + if url == "" { + return + } + feeds := append(viper.GetStringSlice("feeds.urls"), url) + viper.Set("feeds.urls", feeds) + c.Cmd.ReplyTo(e, girc.Fmt("feed {b}{green}added{c}{b}")) + if err := viper.WriteConfig(); err != nil { + c.Cmd.ReplyTo(e, girc.Fmt("adding feed {b}{red}failed{c}{b}")) + } + } + if strings.HasPrefix(e.Last(), "!rmfeed") { + feeds := viper.GetStringSlice("feeds.urls") + s := getParam(e.Last()) + if s == "" { + return + } + i, err := strconv.Atoi(s) + if err != nil { + c.Cmd.ReplyTo(e, "index conversion failed") + return + } + if i < 1 || i > len(feeds) { + c.Cmd.ReplyTo(e, "bad index number") + return + } + feeds = append(feeds[:i-1], feeds[i:]...) + viper.Set("feeds.urls", feeds) + c.Cmd.ReplyTo(e, girc.Fmt("feed {b}{green}removed{c}{b}")) + if err := viper.WriteConfig(); err != nil { + c.Cmd.ReplyTo(e, girc.Fmt("removing feed {b}{red}failed{c}{b}")) + } + } }) go newsFetch(client, channel)