summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2019-07-06 18:47:57 +0200
committerDimitri Sokolyuk <demon@dim13.org>2019-07-06 18:47:57 +0200
commitbe263b349d54318de512b25ba2488044c867df0d (patch)
treee270185c963fbad8aacab99e9cfcc0b5994d8be4
parent090d2fae173d241ee93966e6c6aa95e7d0ec9e86 (diff)
renamedev
-rw-r--r--internal/fix/re.go (renamed from internal/re/re.go)20
-rw-r--r--internal/fix/re_test.go (renamed from internal/re/re_test.go)4
-rw-r--r--internal/flood/flood.go19
-rw-r--r--internal/flood/flood_test.go23
-rw-r--r--internal/title/href.go (renamed from internal/href/href.go)6
-rw-r--r--internal/title/href_test.go (renamed from internal/href/href_test.go)2
-rw-r--r--internal/title/testdata/deepnested.html (renamed from internal/href/testdata/deepnested.html)0
-rw-r--r--internal/title/testdata/linux.org.ru (renamed from internal/href/testdata/linux.org.ru)0
-rw-r--r--internal/title/testdata/openbsd.org (renamed from internal/href/testdata/openbsd.org)0
-rw-r--r--internal/title/testdata/opennet.ru (renamed from internal/href/testdata/opennet.ru)0
-rw-r--r--internal/title/testdata/undeadly.org (renamed from internal/href/testdata/undeadly.org)0
-rw-r--r--limit.go13
-rw-r--r--main.go80
-rw-r--r--room.go40
14 files changed, 84 insertions, 123 deletions
diff --git a/internal/re/re.go b/internal/fix/re.go
index f7e9543..2a83a28 100644
--- a/internal/re/re.go
+++ b/internal/fix/re.go
@@ -1,4 +1,4 @@
-package re
+package fix
import (
"errors"
@@ -12,31 +12,31 @@ import (
var errNotRE = errors.New("not re")
-type RE struct {
+type Fix struct {
last *lru.Cache
w io.Writer
}
-func NewRE(w io.Writer) *RE {
+func New(w io.Writer) *Fix {
last, _ := lru.New(100)
- return &RE{last: last, w: w}
+ return &Fix{last: last, w: w}
}
-func (r *RE) Replace(text, nick string) {
- defer r.last.Add(nick, text)
+func (f *Fix) Fix(text, nick string) {
+ defer f.last.Add(nick, text)
if !strings.HasPrefix(text, "s") {
return
}
- if tofix, ok := r.last.Get(nick); ok {
+ if tofix, ok := f.last.Get(nick); ok {
global := strings.HasSuffix(text, "g")
- fixed, err := Replace(tofix.(string), text[1:], global)
+ fixed, err := replace(tofix.(string), text[1:], global)
if err == nil && fixed != tofix {
- fmt.Fprintf(r.w, "%v meant to say: %s", nick, fixed)
+ fmt.Fprintf(f.w, "%v meant to say: %s", nick, fixed)
}
}
}
-func Replace(s, r string, global bool) (string, error) {
+func replace(s, r string, global bool) (string, error) {
// min: at least two separators
if len(r) < 2 {
return "", errNotRE
diff --git a/internal/re/re_test.go b/internal/fix/re_test.go
index 9824392..fd716a6 100644
--- a/internal/re/re_test.go
+++ b/internal/fix/re_test.go
@@ -1,4 +1,4 @@
-package re
+package fix
import "testing"
@@ -22,7 +22,7 @@ func TestRE(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.r, func(t *testing.T) {
- res, err := Replace(tc.s, tc.r, tc.global)
+ res, err := replace(tc.s, tc.r, tc.global)
if err != tc.err {
t.Errorf("got %q, want %q", err, tc.err)
}
diff --git a/internal/flood/flood.go b/internal/flood/flood.go
index b16b948..fce81ae 100644
--- a/internal/flood/flood.go
+++ b/internal/flood/flood.go
@@ -4,7 +4,6 @@ import (
"math"
"sort"
"strings"
- "unicode"
"unicode/utf8"
)
@@ -18,15 +17,15 @@ type Kicker interface {
Kick(nick string, message ...string)
}
-type Checker struct {
+type Check struct {
k Kicker
}
-func New(k Kicker) *Checker {
- return &Checker{k: k}
+func New(k Kicker) *Check {
+ return &Check{k: k}
}
-func (c Checker) Check(text, nick string) {
+func (c Check) Check(text, nick string) {
if isFlood(text) {
c.k.Kick(nick)
}
@@ -68,13 +67,3 @@ func commonWord(v []string) int {
sort.Sort(sort.Reverse(sort.IntSlice(l)))
return l[0]
}
-
-func noSpaceCompare(a, b string) bool {
- dropSpaces := func(r rune) rune {
- if unicode.IsSpace(r) {
- return -1
- }
- return r
- }
- return strings.Map(dropSpaces, a) == strings.Map(dropSpaces, b)
-}
diff --git a/internal/flood/flood_test.go b/internal/flood/flood_test.go
index a09a3c4..1874254 100644
--- a/internal/flood/flood_test.go
+++ b/internal/flood/flood_test.go
@@ -26,26 +26,3 @@ func TestFlood(t *testing.T) {
})
}
}
-
-func TestNoSpaceCompare(t *testing.T) {
- testCases := []struct {
- a, b string
- ok bool
- }{
- {"", "", true},
- {"x", "", false},
- {"", "x", false},
- {"x ", "x", true},
- {" x", "x", true},
- {"x x", "xx", true},
- {"x x x", "x", false},
- }
- for _, tc := range testCases {
- t.Run(tc.a+"?"+tc.b, func(t *testing.T) {
- ok := noSpaceCompare(tc.a, tc.b)
- if ok != tc.ok {
- t.Errorf("got %v; want %v", ok, tc.ok)
- }
- })
- }
-}
diff --git a/internal/href/href.go b/internal/title/href.go
index f224d27..cae1a18 100644
--- a/internal/href/href.go
+++ b/internal/title/href.go
@@ -1,4 +1,4 @@
-package href
+package title
import (
"context"
@@ -30,12 +30,12 @@ type Titles struct {
w io.Writer
}
-func NewTitles(w io.Writer) *Titles {
+func New(w io.Writer) *Titles {
cache, _ := lru.New(100)
return &Titles{cache: cache, w: w}
}
-func (t *Titles) Resolve(text string) {
+func (t *Titles) Parse(text string) {
for _, v := range parseLinks(text) {
if v == "" {
continue
diff --git a/internal/href/href_test.go b/internal/title/href_test.go
index 7f5de3f..1e5745b 100644
--- a/internal/href/href_test.go
+++ b/internal/title/href_test.go
@@ -1,4 +1,4 @@
-package href
+package title
import (
"io"
diff --git a/internal/href/testdata/deepnested.html b/internal/title/testdata/deepnested.html
index 7322d10..7322d10 100644
--- a/internal/href/testdata/deepnested.html
+++ b/internal/title/testdata/deepnested.html
diff --git a/internal/href/testdata/linux.org.ru b/internal/title/testdata/linux.org.ru
index 7b9c5c8..7b9c5c8 100644
--- a/internal/href/testdata/linux.org.ru
+++ b/internal/title/testdata/linux.org.ru
diff --git a/internal/href/testdata/openbsd.org b/internal/title/testdata/openbsd.org
index a18d934..a18d934 100644
--- a/internal/href/testdata/openbsd.org
+++ b/internal/title/testdata/openbsd.org
diff --git a/internal/href/testdata/opennet.ru b/internal/title/testdata/opennet.ru
index d6baff5..d6baff5 100644
--- a/internal/href/testdata/opennet.ru
+++ b/internal/title/testdata/opennet.ru
diff --git a/internal/href/testdata/undeadly.org b/internal/title/testdata/undeadly.org
index ba7b7ef..ba7b7ef 100644
--- a/internal/href/testdata/undeadly.org
+++ b/internal/title/testdata/undeadly.org
diff --git a/limit.go b/limit.go
deleted file mode 100644
index 022b7fd..0000000
--- a/limit.go
+++ /dev/null
@@ -1,13 +0,0 @@
-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 f4f320e..c9ce13f 100644
--- a/main.go
+++ b/main.go
@@ -3,28 +3,75 @@ package main
import (
"crypto/tls"
"flag"
+ "io"
"log"
+ "sync"
"dim13.org/bot/internal/feeds"
+ "dim13.org/bot/internal/fix"
+ "dim13.org/bot/internal/flood"
+ "dim13.org/bot/internal/title"
irc "github.com/fluffle/goirc/client"
)
-type notify struct {
- conn *irc.Conn
- room string
+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)
+}
+
+type room struct {
+ conn *irc.Conn
+ target string
}
-func newNotify(conn *irc.Conn, room string) *notify {
- return &notify{conn: conn, room: room}
+func roomWriter(conn *irc.Conn, target string) *room {
+ return &room{conn: conn, target: target}
}
-func (n *notify) Write(p []byte) (int, error) {
- n.conn.Notice(n.room, limitString(p).String())
+func (n *room) Write(p []byte) (int, error) {
+ n.conn.Notice(n.target, limitString(p).String())
return len(p), nil
}
-func (n *notify) Kick(nick string, message ...string) {
- n.conn.Kick(n.room, nick, message...)
+func (n *room) Kick(nick string, message ...string) {
+ n.conn.Kick(n.target, nick, message...)
+}
+
+type message struct {
+ title *title.Titles
+ flood *flood.Check
+ fix *fix.Fix
+}
+
+type kickWriter interface {
+ io.Writer
+ flood.Kicker
+}
+
+func msgHandler(w kickWriter) *message {
+ return &message{
+ title: title.New(w),
+ flood: flood.New(w),
+ fix: fix.New(w),
+ }
+}
+
+func (r *message) Handle(conn *irc.Conn, line *irc.Line) {
+ text, nick := line.Text(), line.Nick
+ // ignore self
+ if nick == conn.Me().Nick {
+ return
+ }
+ r.flood.Check(text, nick)
+ r.title.Parse(text)
+ r.fix.Fix(text, nick)
}
func main() {
@@ -42,26 +89,27 @@ func main() {
}
conn := irc.Client(conf)
- done := make(chan struct{})
+ var wg sync.WaitGroup
+ wg.Add(1)
conn.HandleFunc(irc.DISCONNECTED, func(_ *irc.Conn, _ *irc.Line) {
- close(done)
+ wg.Done()
})
- n := newNotify(conn, *room)
+ w := roomWriter(conn, *room)
conn.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, _ *irc.Line) {
conn.Join(*room)
- feeds.Watch(n, feeds.Feeds)
+ feeds.Watch(w, feeds.Feeds)
})
- conn.HandleBG(irc.PRIVMSG, newRoom(n))
-
conn.HandleFunc(irc.KICK, func(conn *irc.Conn, _ *irc.Line) {
conn.Join(*room)
})
+ conn.HandleBG(irc.PRIVMSG, msgHandler(w))
+
if err := conn.Connect(); err != nil {
log.Fatal(err)
}
- <-done
+ wg.Wait()
}
diff --git a/room.go b/room.go
deleted file mode 100644
index 6e95413..0000000
--- a/room.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package main
-
-import (
- "io"
-
- "dim13.org/bot/internal/flood"
- "dim13.org/bot/internal/href"
- "dim13.org/bot/internal/re"
- irc "github.com/fluffle/goirc/client"
-)
-
-type Room struct {
- titles *href.Titles
- flood *flood.Checker
- re *re.RE
-}
-
-type KickWriter interface {
- io.Writer
- flood.Kicker
-}
-
-func newRoom(w KickWriter) *Room {
- return &Room{
- titles: href.NewTitles(w),
- flood: flood.New(w),
- re: re.NewRE(w),
- }
-}
-
-func (r *Room) Handle(conn *irc.Conn, line *irc.Line) {
- text, nick := line.Text(), line.Nick
- // ignore self
- if nick == conn.Me().Nick {
- return
- }
- r.flood.Check(text, nick)
- r.titles.Resolve(text)
- r.re.Replace(text, nick)
-}