summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--blame.go25
-rw-r--r--command.go39
-rw-r--r--duck.go30
-rw-r--r--help.go29
-rw-r--r--last.go33
-rw-r--r--main.go180
-rw-r--r--rfc.go52
-rw-r--r--rss.go52
-rw-r--r--theo.go20
-rw-r--r--top.go26
10 files changed, 307 insertions, 179 deletions
diff --git a/blame.go b/blame.go
new file mode 100644
index 0000000..6085459
--- /dev/null
+++ b/blame.go
@@ -0,0 +1,25 @@
+package main
+
+import (
+ irc "github.com/fluffle/goirc/client"
+)
+
+type Blame struct{ Command }
+
+func (_ Blame) Handle(conn *irc.Conn, line *irc.Line) {
+ src := []string{
+ "Source: http://cgit.dim13.org/bot.git",
+ "Install: go get dim13.org/bot",
+ }
+ for _, s := range src {
+ conn.Privmsg(line.Target(), s)
+ }
+}
+
+func init() {
+ Register("blame", &Blame{
+ Command{
+ Help: "Blame author and return link to source code",
+ },
+ })
+}
diff --git a/command.go b/command.go
new file mode 100644
index 0000000..1dcb9f1
--- /dev/null
+++ b/command.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "time"
+
+ irc "github.com/fluffle/goirc/client"
+)
+
+type Commander interface {
+ irc.Handler
+ fmt.Stringer
+ Timeout() bool
+ WithArgs(int) bool
+}
+
+type Command struct {
+ Help string
+ Arg string
+ Last time.Time
+}
+
+var commands = make(map[string]Commander)
+
+func Register(cmd string, f Commander) {
+ commands[cmd] = f
+}
+
+func (v Command) String() string { return v.Help }
+func (v *Command) Timeout() bool {
+ log.Println("timeout:", time.Since(v.Last))
+ if time.Since(v.Last) > time.Minute {
+ v.Last = time.Now()
+ return false
+ }
+ return true
+}
+func (_ Command) WithArgs(n int) bool { return n == 1 }
diff --git a/duck.go b/duck.go
new file mode 100644
index 0000000..83e4440
--- /dev/null
+++ b/duck.go
@@ -0,0 +1,30 @@
+package main
+
+import (
+ "strings"
+
+ "dim13.org/duck"
+ irc "github.com/fluffle/goirc/client"
+)
+
+type Duck struct{ Command }
+
+func (_ Duck) Timeout() bool { return false }
+func (_ Duck) WithArgs(_ int) bool { return true }
+func (_ Duck) Handle(conn *irc.Conn, line *irc.Line) {
+ if q := strings.SplitN(line.Text(), " ", 2); len(q) == 2 {
+ if a, err := duck.Abstract(q[1]); err != nil {
+ conn.Privmsg(line.Target(), err.Error())
+ } else {
+ conn.Privmsg(line.Target(), a)
+ }
+ }
+}
+
+func init() {
+ Register("define", &Duck{
+ Command{
+ Help: "Perform duckduckgo instant answer search",
+ },
+ })
+}
diff --git a/help.go b/help.go
new file mode 100644
index 0000000..22fa60e
--- /dev/null
+++ b/help.go
@@ -0,0 +1,29 @@
+package main
+
+import (
+ "fmt"
+ "sort"
+
+ irc "github.com/fluffle/goirc/client"
+)
+
+type Help struct{ Command }
+
+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.Nick, s)
+ }
+}
+
+func init() {
+ Register("help", &Help{
+ Command{
+ Help: "This help",
+ },
+ })
+}
diff --git a/last.go b/last.go
new file mode 100644
index 0000000..c80e540
--- /dev/null
+++ b/last.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+ "container/ring"
+ "fmt"
+ "time"
+
+ irc "github.com/fluffle/goirc/client"
+)
+
+type Last struct{ Command }
+
+var buffer = ring.New(10)
+
+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 init() {
+ Register("last", &Last{
+ Command{
+ Help: "Return last 10 messages",
+ },
+ })
+}
diff --git a/main.go b/main.go
index ad32fed..319413b 100644
--- a/main.go
+++ b/main.go
@@ -1,17 +1,11 @@
package main
import (
- "container/ring"
"flag"
- "fmt"
"log"
"sort"
"strings"
- "time"
- "dim13.org/duck"
- "dim13.org/rss"
- "dim13.org/theo"
irc "github.com/fluffle/goirc/client"
)
@@ -21,122 +15,6 @@ var (
name = flag.String("name", "dim13", "Bots Name")
)
-type Commander interface {
- irc.Handler
- fmt.Stringer
- Timeout() bool
- WithArgs(int) bool
-}
-
-type Command struct {
- Help string
- Arg string
- Last time.Time
-}
-
-type (
- Last struct{ Command }
- RSS struct{ Command }
- Theo struct{ Command }
- Help struct{ Command }
- Top struct{ Command }
- Duck struct{ Command }
- Blame struct{ Command }
-)
-
-var (
- commands = make(map[string]Commander)
- buffer = ring.New(10)
-)
-
-func Register(cmd string, f Commander) {
- commands[cmd] = f
-}
-
-func (v Command) String() string { return v.Help }
-func (v *Command) Timeout() bool {
- log.Println("timeout:", time.Since(v.Last))
- if time.Since(v.Last) > time.Minute {
- v.Last = time.Now()
- return false
- }
- return true
-}
-func (_ Command) WithArgs(n int) bool { return n == 1 }
-
-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 - %v", n+1, i.Title, i.Link)
- 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.Nick, s)
- }
-}
-
-func (_ Top) Handle(conn *irc.Conn, line *irc.Line) {
- n := 100
- if line.Public() {
- n = 10
- }
- s := fmt.Sprint(NewScores(n))
- conn.Privmsg(line.Target(), s)
-}
-
-func (_ Duck) Timeout() bool { return false }
-func (_ Duck) WithArgs(_ int) bool { return true }
-func (_ Duck) Handle(conn *irc.Conn, line *irc.Line) {
- if q := strings.SplitN(line.Text(), " ", 2); len(q) == 2 {
- if a, err := duck.Abstract(q[1]); err != nil {
- conn.Privmsg(line.Target(), err.Error())
- } else {
- conn.Privmsg(line.Target(), a)
- }
- }
-}
-
-func (_ Blame) Handle(conn *irc.Conn, line *irc.Line) {
- src := []string{
- "Source: http://cgit.dim13.org/bot.git",
- "Install: go get dim13.org/bot",
- }
- for _, s := range src {
- conn.Privmsg(line.Target(), s)
- }
-}
-
func MedianLength(v []string) int {
if len(v) == 0 {
return 0
@@ -191,65 +69,9 @@ func privmsg(conn *irc.Conn, line *irc.Line) {
}
}
-func init() {
+func main() {
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 (msg private to see top 100)",
- },
- })
- Register("define", &Duck{
- Command{
- Help: "Perform duckduckgo instant answer search",
- },
- })
- Register("blame", &Blame{
- Command{
- Help: "Blame author and return link to source code",
- },
- })
-}
-func main() {
c := irc.SimpleClient(*name)
c.EnableStateTracking()
diff --git a/rfc.go b/rfc.go
new file mode 100644
index 0000000..69c0b7e
--- /dev/null
+++ b/rfc.go
@@ -0,0 +1,52 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "strconv"
+ "strings"
+
+ "dim13.org/rfc"
+ irc "github.com/fluffle/goirc/client"
+)
+
+var rfcMap = make(map[int]rfc.Entry)
+
+type RFC struct{ Command }
+
+func (_ RFC) Timeout() bool { return false }
+func (_ RFC) WithArgs(n int) bool { return n == 2 }
+func (_ RFC) Handle(conn *irc.Conn, line *irc.Line) {
+ if q := strings.Fields(line.Text()); len(q) == 2 {
+ if id, err := strconv.Atoi(q[1]); err != nil {
+ conn.Privmsg(line.Target(), err.Error())
+ } else {
+ if entry, ok := rfcMap[id]; ok {
+ s := fmt.Sprint(entry)
+ conn.Privmsg(line.Target(), s)
+ } else {
+ conn.Privmsg(line.Target(), "not found")
+ }
+ }
+ }
+}
+
+func init() {
+ fd, err := rfc.Open()
+ if err != nil {
+ log.Fatal(err)
+ }
+ entries, err := rfc.Decode(fd)
+ if err != nil {
+ log.Fatal(err)
+ }
+ for _, e := range entries {
+ id := e.ID()
+ rfcMap[id] = e
+ }
+ Register("rfc", &RFC{
+ Command{
+ Help: "Perform RFC lookup",
+ },
+ })
+}
diff --git a/rss.go b/rss.go
new file mode 100644
index 0000000..2dc45a6
--- /dev/null
+++ b/rss.go
@@ -0,0 +1,52 @@
+package main
+
+import (
+ "fmt"
+
+ "dim13.org/rss"
+ irc "github.com/fluffle/goirc/client"
+)
+
+type RSS struct{ Command }
+
+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 - %v", n+1, i.Title, i.Link)
+ conn.Privmsg(line.Target(), s)
+ }
+}
+
+func init() {
+ 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`,
+ },
+ })
+}
diff --git a/theo.go b/theo.go
new file mode 100644
index 0000000..fe246d7
--- /dev/null
+++ b/theo.go
@@ -0,0 +1,20 @@
+package main
+
+import (
+ "dim13.org/theo"
+ irc "github.com/fluffle/goirc/client"
+)
+
+type Theo struct{ Command }
+
+func (_ Theo) Handle(conn *irc.Conn, line *irc.Line) {
+ conn.Privmsg(line.Target(), theo.Theo())
+}
+
+func init() {
+ Register("theo", &Theo{
+ Command{
+ Help: "Quote Theo De Raadt",
+ },
+ })
+}
diff --git a/top.go b/top.go
new file mode 100644
index 0000000..10a557f
--- /dev/null
+++ b/top.go
@@ -0,0 +1,26 @@
+package main
+
+import (
+ "fmt"
+
+ irc "github.com/fluffle/goirc/client"
+)
+
+type Top struct{ Command }
+
+func (_ Top) Handle(conn *irc.Conn, line *irc.Line) {
+ n := 100
+ if line.Public() {
+ n = 10
+ }
+ s := fmt.Sprint(NewScores(n))
+ conn.Privmsg(line.Target(), s)
+}
+
+func init() {
+ Register("top", &Top{
+ Command{
+ Help: "Top 10 flooder (msg private to see top 100)",
+ },
+ })
+}