From 3b8ee25e5d5cfdb7920e5bfb7ff99a35c5b26345 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 6 Jul 2019 17:35:43 +0200 Subject: split more --- room.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 room.go (limited to 'room.go') 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) + } +} -- cgit v1.2.3