summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-07-09 19:21:32 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-07-09 19:21:32 +0200
commit1ea2e5c0937ef86b20a55b34d4a9a495b3dffa6a (patch)
treebc51557e6833c951998af7a315dc48e33bc01786 /main.go
parentc3b81b0f04f35c8be63b8daff04829382050644a (diff)
Add top10
Diffstat (limited to 'main.go')
-rw-r--r--main.go129
1 files changed, 87 insertions, 42 deletions
diff --git a/main.go b/main.go
index d5e7b9c..b1ff41b 100644
--- a/main.go
+++ b/main.go
@@ -20,27 +20,28 @@ var (
name = flag.String("name", "mincom", "Bots Name")
)
-const (
- lor = `https://www.linux.org.ru/section-rss.jsp?section=1`
- undeadly = `http://undeadly.org/cgi?action=rss`
-)
-
type Commander interface {
irc.Handler
fmt.Stringer
}
+type Command struct {
+ Help string
+ Arg string
+}
+
type (
- Last string
- News string
- BSD string
- Theo string
- Help string
- Metar string
+ 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)
)
@@ -48,7 +49,8 @@ func Register(cmd string, f Commander) {
commands[cmd] = f
}
-func (v Last) String() string { return string(v) }
+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 {
@@ -61,35 +63,17 @@ func (_ Last) Handle(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(), theo.Theo())
}
-func (v News) String() string { return string(v) }
-func (_ News) Handle(conn *irc.Conn, line *irc.Line) {
- news, err := rss.Fetch(lor)
- if err != nil {
- conn.Privmsg(line.Target(), err.Error())
- return
- }
- if line.Public() {
- 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 (v BSD) String() string { return string(v) }
-func (_ BSD) Handle(conn *irc.Conn, line *irc.Line) {
- news, err := rss.Fetch(undeadly)
+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() {
+ if line.Public() && len(news.Channel.Items) > 3 {
news.Channel.Items = news.Channel.Items[:3]
}
for n, i := range news.Channel.Items {
@@ -98,7 +82,6 @@ func (_ BSD) Handle(conn *irc.Conn, line *irc.Line) {
}
}
-func (v Help) String() string { return string(v) }
func (_ Help) Handle(conn *irc.Conn, line *irc.Line) {
var msg []string
for k, v := range commands {
@@ -110,7 +93,6 @@ func (_ Help) Handle(conn *irc.Conn, line *irc.Line) {
}
}
-func (v Metar) String() string { return string(v) }
func (_ Metar) Handle(conn *irc.Conn, line *irc.Line) {
f := strings.Fields(line.Text())
if len(f) > 1 {
@@ -131,18 +113,38 @@ func (_ Metar) Handle(conn *irc.Conn, line *irc.Line) {
}
}
+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) {
- log.Println(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)
}
}
@@ -159,12 +161,55 @@ 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("LOR news (msg private to see all)"))
- Register("bsd", BSD("Undeadly news (msg private to see all)"))
- Register("help", Help("This help"))
- Register("metar", Metar("Weather (argument: ICAO-Code, msg private for verbose output)"))
+ 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() {