commit 3e2e7cc2aaf59272f3889c594e4b3d2629d05943
parent b00450896733ff825538921421ababfb544d77b1
Author: Emile 'iMil' Heitor <imil@NetBSD.org>
Date: Fri, 14 Jul 2023 12:16:04 +0200
feat: !latest private message with the latest news
Diffstat:
M | README.md | | | 1 | + |
M | main.go | | | 59 | +++++++++++++++++++++++++++++++++++++++++++++++++---------- |
2 files changed, 50 insertions(+), 10 deletions(-)
diff --git a/README.md b/README.md
@@ -62,6 +62,7 @@ For everybody:
* `!lsfeeds` lists available RSS feeds, can be asked privately
* `!xpost <news hash>` posts news with hash `<news hash>` to `xchannels`
+* `!latest <number> [origin]` will post `number` of latest news from optional `origin` in a query message
For ops:
diff --git a/main.go b/main.go
@@ -26,7 +26,7 @@ type News struct {
const maxNews = 100
-var newslist []News
+var newsList []News
func mkHash(s1, s2 string) string {
s := s1 + s2
@@ -36,7 +36,7 @@ func mkHash(s1, s2 string) string {
}
func newsExists(news News) bool {
- for _, n := range newslist {
+ for _, n := range newsList {
if n.Hash == news.Hash {
return true
}
@@ -48,7 +48,7 @@ func getNewsByHash(hash string) News {
if strings.Contains(hash, "#") {
hash = hash[1:]
}
- for _, n := range newslist {
+ for _, n := range newsList {
if n.Hash == hash {
return n
}
@@ -56,6 +56,17 @@ func getNewsByHash(hash string) News {
return News{}
}
+func getNewsByOrigin(origin string) []News {
+ resNews := []News{}
+
+ for _, n := range newsList {
+ if n.Origin == origin {
+ resNews = append(resNews, n)
+ }
+ }
+ return resNews
+}
+
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"),
@@ -71,7 +82,7 @@ func fmtNews(news News) string {
// Fetch and post news from RSS feeds
func newsFetch(client *girc.Client, channel string) {
- newslist = make([]News, 0)
+ newsList = make([]News, 0)
for {
if client.IsConnected() && len(client.ChannelList()) != 0 {
@@ -91,7 +102,7 @@ func newsFetch(client *girc.Client, channel string) {
defer f.Close()
// read news from disk
decoder := json.NewDecoder(f)
- if err := decoder.Decode(&newslist); err != nil {
+ if err := decoder.Decode(&newsList); err != nil {
client.Cmd.Message(channel, "could not load news list, empty?")
}
@@ -134,10 +145,10 @@ func newsFetch(client *girc.Client, channel string) {
time.Sleep(viper.GetDuration("irc.delay"))
// Mark item as posted
- if len(newslist) < maxNews {
- newslist = append(newslist, news)
+ if len(newsList) < maxNews {
+ newsList = append(newsList, news)
} else {
- newslist = append(newslist[1:], news)
+ newsList = append(newsList[1:], news)
}
}
}
@@ -149,8 +160,8 @@ func newsFetch(client *girc.Client, channel string) {
log.Fatal(err)
}
encoder := json.NewEncoder(f)
- if err = encoder.Encode(newslist); err != nil {
- client.Cmd.Message(channel, "could write newslist")
+ if err = encoder.Encode(newsList); err != nil {
+ client.Cmd.Message(channel, "could write newsList")
}
time.Sleep(viper.GetDuration("feeds.frequency"))
}
@@ -313,6 +324,34 @@ func main() {
}
}
}
+ 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 {
+ c.Cmd.Message(dest, "usage: !latest <number> [origin]")
+ return
+ }
+ n, err := strconv.Atoi(p[1])
+ if err != nil {
+ c.Cmd.Message(dest, "conversion error")
+ return
+ }
+ showNews := newsList
+ if len(p) > 2 {
+ showNews = getNewsByOrigin(p[2])
+ numNews = len(showNews)
+ }
+ if n > numNews {
+ n = numNews
+ }
+ for i := 0; i < n; i++ {
+ c.Cmd.Message(dest, girc.Fmt(fmtNews(showNews[i])))
+ }
+ }
})
go newsFetch(client, channel)