From 04a3c5398af8ccabafafe3e684117d6e57ffe7b2 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 9 Jul 2015 17:44:15 +0200 Subject: Restructure code --- lor.go | 14 ------------ main.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------- rss.go | 19 ++++++++++++++++ 3 files changed, 83 insertions(+), 29 deletions(-) delete mode 100644 lor.go create mode 100644 rss.go diff --git a/lor.go b/lor.go deleted file mode 100644 index 021d831..0000000 --- a/lor.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import "dim13.org/rss" - -const newsRss = `https://www.linux.org.ru/section-rss.jsp?section=1` - -func FetchNews() (s []string) { - news, _ := rss.Fetch(newsRss) - items := news.Channel.Items - for _, i := range items { - s = append(s, i.Title) - } - return -} diff --git a/main.go b/main.go index e3ecd51..ad4f8a8 100644 --- a/main.go +++ b/main.go @@ -15,22 +15,33 @@ import ( var ( server = flag.String("server", "irc.rusnet.org.ru:6660", "IRC Server") room = flag.String("room", "#lor", "IRC Channel") - name = flag.String("name", "theo", "Bot Name") + name = flag.String("name", "mincom", "Bots Name") ) -var buffer = ring.New(10) - -func init() { - flag.Parse() +type Commander interface { + irc.Handler + fmt.Stringer } -var cmds = map[string]irc.HandlerFunc{ - "last": last, - "theo": theo, - "news": news, +type ( + Last string + News string + BSD string + Theo string + Help string +) + +var ( + commands = make(map[string]Commander) + buffer = ring.New(10) +) + +func Register(cmd string, f Commander) { + commands[cmd] = f } -func last(conn *irc.Conn, line *irc.Line) { +func (v Last) String() string { return string(v) } +func (_ Last) Handle(conn *irc.Conn, line *irc.Line) { buffer.Do(func(v interface{}) { if v != nil { l := v.(*irc.Line) @@ -42,12 +53,33 @@ func last(conn *irc.Conn, line *irc.Line) { }) } -func theo(conn *irc.Conn, line *irc.Line) { +func (v Theo) String() string { return string(v) } +func (_ Theo) Handle(conn *irc.Conn, line *irc.Line) { conn.Privmsg(line.Target(), tdr.Theo()) } -func news(conn *irc.Conn, line *irc.Line) { - n := FetchNews() +func (v News) String() string { return string(v) } +func (_ News) Handle(conn *irc.Conn, line *irc.Line) { + n, err := FetchNews(lor) + if err != nil { + conn.Privmsg(line.Target(), err.Error()) + return + } + if line.Public() { + n = n[:3] + } + for _, s := range n { + conn.Privmsg(line.Target(), s) + } +} + +func (v BSD) String() string { return string(v) } +func (_ BSD) Handle(conn *irc.Conn, line *irc.Line) { + n, err := FetchNews(undeadly) + if err != nil { + conn.Privmsg(line.Target(), err.Error()) + return + } if line.Public() { n = n[:3] } @@ -56,6 +88,14 @@ func news(conn *irc.Conn, line *irc.Line) { } } +func (v Help) String() string { return string(v) } +func (_ Help) Handle(conn *irc.Conn, line *irc.Line) { + for k, v := range commands { + s := fmt.Sprintf("%4s: %v", k, v) + conn.Privmsg(line.Target(), s) + } +} + func privmsg(conn *irc.Conn, line *irc.Line) { log.Println(line) txt := line.Text() @@ -65,8 +105,8 @@ func privmsg(conn *irc.Conn, line *irc.Line) { } // lookup command if f := strings.Fields(txt); len(f) > 0 { - if c, ok := cmds[f[0]]; ok { - c(conn, line) + if c, ok := commands[f[0]]; ok { + c.Handle(conn, line) } } // extract single link and fetch title @@ -83,6 +123,15 @@ func privmsg(conn *irc.Conn, line *irc.Line) { } } +func init() { + flag.Parse() + Register("last", Last("return last 10 messages")) + Register("theo", Theo("Quote Theo De Raadt")) + Register("news", News("Last LOR news (msg private to see all)")) + Register("bsd", BSD("Last Undeadly news (msg private to see all)")) + Register("help", Help("This help")) +} + func main() { c := irc.SimpleClient(*name) c.EnableStateTracking() diff --git a/rss.go b/rss.go new file mode 100644 index 0000000..abffafd --- /dev/null +++ b/rss.go @@ -0,0 +1,19 @@ +package main + +import "dim13.org/rss" + +const ( + lor = `https://www.linux.org.ru/section-rss.jsp?section=1` + undeadly = `http://undeadly.org/cgi?action=rss` +) + +func FetchNews(url string) (s []string, err error) { + news, err := rss.Fetch(url) + if err != nil { + return nil, err + } + for _, i := range news.Channel.Items { + s = append(s, i.Title) + } + return s, nil +} -- cgit v1.2.3