summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2019-07-06 17:35:43 +0200
committerDimitri Sokolyuk <demon@dim13.org>2019-07-06 17:35:43 +0200
commit3b8ee25e5d5cfdb7920e5bfb7ff99a35c5b26345 (patch)
tree161b6e72c955baf2297c2a059d0522c5032d0a23
parent1d2ca509c77cbb2af0475b1319cd840f8ce9a1d0 (diff)
split more
-rw-r--r--limit.go13
-rw-r--r--main.go101
-rw-r--r--notify.go23
-rw-r--r--room.go74
4 files changed, 117 insertions, 94 deletions
diff --git a/limit.go b/limit.go
new file mode 100644
index 0000000..022b7fd
--- /dev/null
+++ b/limit.go
@@ -0,0 +1,13 @@
+package main
+
+const maxLen = 450
+
+type limitString string
+
+func (s limitString) String() string {
+ r := []rune(s)
+ if sz := len(r); sz > maxLen {
+ r = append(r[:maxLen-3], []rune("...")...)
+ }
+ return string(r)
+}
diff --git a/main.go b/main.go
index 105893a..a3e9f20 100644
--- a/main.go
+++ b/main.go
@@ -3,106 +3,17 @@ package main
import (
"crypto/tls"
"flag"
- "fmt"
"log"
- "strings"
"dim13.org/bot/internal/feeds"
- "dim13.org/bot/internal/flood"
- "dim13.org/bot/internal/href"
- "dim13.org/bot/internal/re"
irc "github.com/fluffle/goirc/client"
- lru "github.com/hashicorp/golang-lru"
)
-const maxLen = 450
-
-type notify struct {
- conn *irc.Conn
- target string
-}
-
-func newNotify(conn *irc.Conn, target string) *notify {
- return &notify{conn: conn, target: target}
-}
-
-func limit(n int, s string) string {
- r := []rune(s)
- if sz := len(r); sz > n {
- r = append(r[:n-3], []rune("...")...)
- }
- return string(r)
-}
-
-func (n *notify) Write(p []byte) (int, error) {
- s := limit(maxLen, string(p))
- log.Println("send", s)
- n.conn.Notice(n.target, s)
- return len(p), nil
-}
-
-func privmsg(room string) irc.HandlerFunc {
- var (
- last, _ = lru.New(100)
- titles, _ = lru.New(100)
- )
- return func(conn *irc.Conn, line *irc.Line) {
- defer func() {
- if r := recover(); r != nil {
- log.Println("panic", r)
- }
- }()
- t := line.Text()
- switch {
- case line.Nick == conn.Me().Nick:
- // ignore self
- case flood.Is(t):
- log.Println("flood", line.Nick)
- conn.Kick(room, line.Nick)
- case strings.HasPrefix(t, "s"):
- global := strings.HasSuffix(t, "g")
- if tofix, ok := last.Get(line.Nick); ok {
- fixed, err := re.Replace(tofix.(string), t[1:], global)
- if err == nil && fixed != tofix {
- log.Println("regexp", t)
- fmt.Fprintf(newNotify(conn, line.Target()), "%v meant to say: %s", line.Nick, fixed)
- return
- }
-
- }
- fallthrough
- default:
- for _, v := range href.Links(t) {
- title, ok := titles.Get(v)
- if !ok {
- var err error
- title, err = href.Title(v)
- if err != nil {
- log.Println(v, err)
- }
- titles.Add(v, title)
- }
- if title != "" {
- w := newNotify(conn, line.Target())
- if ok {
- fmt.Fprintf(w, "Title: %v (cached)", title)
- } else {
- fmt.Fprintf(w, "Title: %v", title)
- }
- }
- }
- last.Add(line.Nick, t)
- }
- }
-}
-
func main() {
- var (
- node = flag.String("node", "irc.freenode.org", "IRC server")
- notls = flag.Bool("notls", false, "disable TLS")
- room = flag.String("room", "#lor", "IRC channel")
- name = flag.String("name", "dim13", "bots name")
- )
+ node := flag.String("node", "irc.freenode.org", "IRC server")
+ notls := flag.Bool("notls", false, "disable TLS")
+ room := flag.String("room", "#lor", "IRC channel")
+ name := flag.String("name", "dim13", "bots name")
flag.Parse()
conf := irc.NewConfig(*name)
@@ -124,7 +35,9 @@ func main() {
feeds.Watch(newNotify(conn, *room), feeds.Feeds)
})
- conn.HandleBG(irc.PRIVMSG, privmsg(*room))
+ r := newRoom(*room, 100)
+
+ conn.HandleBG(irc.PRIVMSG, r)
conn.HandleFunc(irc.KICK, func(conn *irc.Conn, _ *irc.Line) {
conn.Join(*room)
diff --git a/notify.go b/notify.go
new file mode 100644
index 0000000..9234e20
--- /dev/null
+++ b/notify.go
@@ -0,0 +1,23 @@
+package main
+
+import (
+ "log"
+
+ irc "github.com/fluffle/goirc/client"
+)
+
+type notify struct {
+ conn *irc.Conn
+ target string
+}
+
+func newNotify(conn *irc.Conn, target string) *notify {
+ return &notify{conn: conn, target: target}
+}
+
+func (n *notify) Write(p []byte) (int, error) {
+ s := limitString(p)
+ log.Println("send", s)
+ n.conn.Notice(n.target, s.String())
+ return len(p), nil
+}
diff --git a/room.go b/room.go
new file mode 100644
index 0000000..a7c2ad0
--- /dev/null
+++ b/room.go
@@ -0,0 +1,74 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "strings"
+
+ "dim13.org/bot/internal/flood"
+ "dim13.org/bot/internal/href"
+ "dim13.org/bot/internal/re"
+ irc "github.com/fluffle/goirc/client"
+ lru "github.com/hashicorp/golang-lru"
+)
+
+type Room struct {
+ room string
+ last *lru.Cache
+ titles *lru.Cache
+}
+
+func newRoom(room string, sz int) *Room {
+ last, _ := lru.New(sz)
+ titles, _ := lru.New(sz)
+ return &Room{room: room, last: last, titles: titles}
+}
+
+func (r *Room) Handle(conn *irc.Conn, line *irc.Line) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Println("panic", r)
+ }
+ }()
+ t := line.Text()
+ switch {
+ case line.Nick == conn.Me().Nick:
+ // ignore self
+ case flood.Is(t):
+ log.Println("flood", line.Nick)
+ conn.Kick(r.room, line.Nick)
+ case strings.HasPrefix(t, "s"):
+ global := strings.HasSuffix(t, "g")
+ if tofix, ok := r.last.Get(line.Nick); ok {
+ fixed, err := re.Replace(tofix.(string), t[1:], global)
+ if err == nil && fixed != tofix {
+ log.Println("regexp", t)
+ fmt.Fprintf(newNotify(conn, line.Target()), "%v meant to say: %s", line.Nick, fixed)
+ return
+ }
+
+ }
+ fallthrough
+ default:
+ for _, v := range href.Links(t) {
+ title, ok := r.titles.Get(v)
+ if !ok {
+ var err error
+ title, err = href.Title(v)
+ if err != nil {
+ log.Println(v, err)
+ }
+ r.titles.Add(v, title)
+ }
+ if title != "" {
+ w := newNotify(conn, line.Target())
+ if ok {
+ fmt.Fprintf(w, "Title: %v (cached)", title)
+ } else {
+ fmt.Fprintf(w, "Title: %v", title)
+ }
+ }
+ }
+ r.last.Add(line.Nick, t)
+ }
+}