package main import ( "container/ring" "flag" "fmt" "log" "sort" "strings" "time" "dim13.org/rss" "dim13.org/theo" irc "github.com/fluffle/goirc/client" ) var ( server = flag.String("server", "irc.rusnet.org.ru:6660", "IRC Server") room = flag.String("room", "#lor", "IRC Channel") name = flag.String("name", "mincom", "Bots Name") ) type Commander interface { irc.Handler fmt.Stringer } type Command struct { Help string Arg string } type ( Last struct{ Command } RSS struct{ Command } Theo struct{ Command } Help struct{ Command } Metar struct{ Command } Top struct{ Command } ) var ( commands = make(map[string]Commander) top = make(map[string]int) buffer = ring.New(10) ) func Register(cmd string, f Commander) { commands[cmd] = f } func (v Command) String() string { return v.Help } func (_ Last) Handle(conn *irc.Conn, line *irc.Line) { buffer.Do(func(v interface{}) { if v != nil { l := v.(*irc.Line) s := fmt.Sprintf("%v <%v> %v", l.Time.Format(time.Kitchen), l.Nick, l.Text()) conn.Privmsg(line.Nick, s) } }) } func (_ Theo) Handle(conn *irc.Conn, line *irc.Line) { conn.Privmsg(line.Target(), theo.Theo()) } func (v RSS) Handle(conn *irc.Conn, line *irc.Line) { news, err := rss.Fetch(v.Arg) if err != nil { conn.Privmsg(line.Target(), err.Error()) return } if line.Public() && len(news.Channel.Items) > 3 { news.Channel.Items = news.Channel.Items[:3] } for n, i := range news.Channel.Items { s := fmt.Sprintf("%2d. %v", n+1, i.Title) conn.Privmsg(line.Target(), s) } } func (_ Help) Handle(conn *irc.Conn, line *irc.Line) { var msg []string for k, v := range commands { msg = append(msg, fmt.Sprintf("%-8s%v", k, v)) } sort.Sort(sort.StringSlice(msg)) for _, s := range msg { conn.Privmsg(line.Target(), s) } } func (_ Metar) Handle(conn *irc.Conn, line *irc.Line) { f := strings.Fields(line.Text()) if len(f) > 1 { var m []string var err error if line.Public() { m, err = MetarStation(f[1]) } else { m, err = MetarDecoded(f[1]) } if err != nil { conn.Privmsg(line.Target(), err.Error()) return } for _, s := range m { conn.Privmsg(line.Target(), s) } } } func (_ Top) Handle(conn *irc.Conn, line *irc.Line) { score := make(map[int]string) var selector []int for nick, lines := range top { score[lines] = nick selector = append(selector, lines) } sort.Sort(sort.Reverse(sort.IntSlice(selector))) if line.Public() && len(selector) > 10 { selector = selector[:10] } else if len(selector) > 20 { selector = selector[:20] } for n, lines := range selector { s := fmt.Sprintf("%2d. %v (%v)", n+1, score[lines], lines) conn.Privmsg(line.Target(), s) } } func privmsg(conn *irc.Conn, line *irc.Line) { f := strings.Fields(line.Text()) if line.Public() && line.Nick != conn.Me().Nick { buffer.Value = line buffer = buffer.Next() top[line.Nick]++ } // lookup command if len(f) > 0 { if c, ok := commands[f[0]]; ok { log.Println(line) c.Handle(conn, line) } } // extract single link and fetch title for _, v := range f { if strings.HasPrefix(v, "http") { if t, err := FetchTitle(v); err == nil { conn.Privmsg(line.Target(), t) } } } } func init() { flag.Parse() Register("last", Last{ Command{ Help: "Return last 10 messages", }, }) Register("theo", Theo{ Command{ Help: "Quote Theo De Raadt", }, }) Register("news", RSS{ Command{ Help: "LOR news (msg private to see all)", Arg: `https://www.linux.org.ru/section-rss.jsp?section=1`, }, }) Register("forum", RSS{ Command{ Help: "LOR forum (msg private to see all)", Arg: `https://www.linux.org.ru/section-rss.jsp?section=2`, }, }) Register("gallery", RSS{ Command{ Help: "LOR gallery (msg private to see all)", Arg: `https://www.linux.org.ru/section-rss.jsp?section=3`, }, }) Register("bsd", RSS{ Command{ Help: "Undeadly news (msg private to see all)", Arg: `http://undeadly.org/cgi?action=rss`, }, }) Register("help", Help{ Command{ Help: "This help", }, }) Register("top", Top{ Command{ Help: "Top 10 flooder", }, }) Register("metar", Metar{ Command{ Help: "Weather (argument: ICAO-Code, msg private for verbose output)", }, }) } func main() { c := irc.SimpleClient(*name) c.EnableStateTracking() quit := make(chan bool) c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) { quit <- true }) c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) { conn.Join(*room) }) c.HandleFunc(irc.PRIVMSG, privmsg) if err := c.ConnectTo(*server); err != nil { log.Fatal(err) } <-quit }