summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2019-07-06 17:17:46 +0200
committerDimitri Sokolyuk <demon@dim13.org>2019-07-06 17:17:46 +0200
commit1d2ca509c77cbb2af0475b1319cd840f8ce9a1d0 (patch)
tree05a838baaf4f96fbcce03d06090a2403ee56c878
parent87e820722cf02054225b47a58f97d0824118292f (diff)
Split in packages
-rw-r--r--internal/feeds/feeds.go (renamed from feeds.go)4
-rw-r--r--internal/feeds/rss.go (renamed from rss.go)12
-rw-r--r--internal/flood/flood.go (renamed from flood.go)6
-rw-r--r--internal/flood/flood_test.go (renamed from flood_test.go)10
-rw-r--r--internal/href/href.go (renamed from href.go)6
-rw-r--r--internal/href/href_test.go (renamed from href_test.go)4
-rw-r--r--internal/href/testdata/deepnested.html (renamed from testdata/deepnested.html)0
-rw-r--r--internal/href/testdata/linux.org.ru (renamed from testdata/linux.org.ru)0
-rw-r--r--internal/href/testdata/openbsd.org (renamed from testdata/openbsd.org)0
-rw-r--r--internal/href/testdata/opennet.ru (renamed from testdata/opennet.ru)0
-rw-r--r--internal/href/testdata/undeadly.org (renamed from testdata/undeadly.org)0
-rw-r--r--internal/re/re.go (renamed from re.go)4
-rw-r--r--internal/re/re_test.go (renamed from re_test.go)4
-rw-r--r--main.go18
14 files changed, 34 insertions, 34 deletions
diff --git a/feeds.go b/internal/feeds/feeds.go
index 668a7c4..8c9bd72 100644
--- a/feeds.go
+++ b/internal/feeds/feeds.go
@@ -1,8 +1,8 @@
-package main
+package feeds
import "time"
-var feeds = []feed{
+var Feeds = []Feed{
{
Name: "LOR News",
URL: `https://www.linux.org.ru/section-rss.jsp?section=1`,
diff --git a/rss.go b/internal/feeds/rss.go
index d14db94..f3dd39c 100644
--- a/rss.go
+++ b/internal/feeds/rss.go
@@ -1,4 +1,4 @@
-package main
+package feeds
import (
"fmt"
@@ -9,14 +9,14 @@ import (
"dim13.org/rss"
)
-type feed struct {
+type Feed struct {
Name string
URL string
Every time.Duration
}
type news struct {
- feed
+ Feed
rss.Item
}
@@ -31,7 +31,7 @@ func (n news) String() string {
return s
}
-func (f feed) watch(w io.Writer) {
+func (f Feed) Watch(w io.Writer) {
ticker := time.NewTicker(f.Every)
defer ticker.Stop()
for t := range ticker.C {
@@ -49,8 +49,8 @@ func (f feed) watch(w io.Writer) {
}
}
-func watch(w io.Writer, feeds []feed) {
+func Watch(w io.Writer, feeds []Feed) {
for _, feed := range feeds {
- go feed.watch(w)
+ go feed.Watch(w)
}
}
diff --git a/flood.go b/internal/flood/flood.go
index d7fd05d..b8a71d1 100644
--- a/flood.go
+++ b/internal/flood/flood.go
@@ -1,4 +1,4 @@
-package main
+package flood
import (
"math"
@@ -11,7 +11,7 @@ import (
const (
runes = 6
words = 6
- black = "\u24B6\u262D\u272F\u262E\u2721\u5350\u534D\u2719\u0FD5\u0FD6\u16CB\u16CB\uA5A6\u0FD7\u0FD8"
+ black = "\u24B6\u262D\u272F\u262E\u2721\u5350\u534D\u2719\u0FD5\u0FD6\u16CB\uA5A6\u0FD7\u0FD8"
)
func entropy(s string) (e float64) {
@@ -25,7 +25,7 @@ func entropy(s string) (e float64) {
return e
}
-func isFlood(s string) bool {
+func Is(s string) bool {
if strings.ContainsAny(s, black) {
return true
}
diff --git a/flood_test.go b/internal/flood/flood_test.go
index 8f5045c..c16da7c 100644
--- a/flood_test.go
+++ b/internal/flood/flood_test.go
@@ -1,11 +1,11 @@
-package main
+package flood
import "testing"
func TestFlood(t *testing.T) {
testCases := []struct {
- line string
- isFlood bool
+ line string
+ ok bool
}{
{`! ! ! ! ! ! ! !`, true},
{`test test test test abc abc`, true},
@@ -20,8 +20,8 @@ func TestFlood(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.line, func(t *testing.T) {
- if isFlood(tc.line) != tc.isFlood {
- t.Errorf("want %v", tc.isFlood)
+ if Is(tc.line) != tc.ok {
+ t.Errorf("want %v", tc.ok)
}
})
}
diff --git a/href.go b/internal/href/href.go
index 007731f..d134f67 100644
--- a/href.go
+++ b/internal/href/href.go
@@ -1,4 +1,4 @@
-package main
+package href
import (
"context"
@@ -51,7 +51,7 @@ func title(r io.Reader) (string, error) {
return "", errNoTitle
}
-func getTitle(uri string) (string, error) {
+func Title(uri string) (string, error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return "", err
@@ -86,7 +86,7 @@ func getTitle(uri string) (string, error) {
return title(r)
}
-func getLinks(s string) (ret []string) {
+func Links(s string) (ret []string) {
for _, v := range strings.Fields(s) {
switch {
case strings.HasPrefix(v, "www."):
diff --git a/href_test.go b/internal/href/href_test.go
index fb4667c..ed2bc40 100644
--- a/href_test.go
+++ b/internal/href/href_test.go
@@ -1,4 +1,4 @@
-package main
+package href
import (
"io"
@@ -51,7 +51,7 @@ func TestTitle(t *testing.T) {
io.Copy(w, fd)
}))
defer ts.Close()
- title, err := getTitle(ts.URL)
+ title, err := Title(ts.URL)
if err != nil {
t.Error(tc.fixture, err)
}
diff --git a/testdata/deepnested.html b/internal/href/testdata/deepnested.html
index 7322d10..7322d10 100644
--- a/testdata/deepnested.html
+++ b/internal/href/testdata/deepnested.html
diff --git a/testdata/linux.org.ru b/internal/href/testdata/linux.org.ru
index 7b9c5c8..7b9c5c8 100644
--- a/testdata/linux.org.ru
+++ b/internal/href/testdata/linux.org.ru
diff --git a/testdata/openbsd.org b/internal/href/testdata/openbsd.org
index a18d934..a18d934 100644
--- a/testdata/openbsd.org
+++ b/internal/href/testdata/openbsd.org
diff --git a/testdata/opennet.ru b/internal/href/testdata/opennet.ru
index d6baff5..d6baff5 100644
--- a/testdata/opennet.ru
+++ b/internal/href/testdata/opennet.ru
diff --git a/testdata/undeadly.org b/internal/href/testdata/undeadly.org
index ba7b7ef..ba7b7ef 100644
--- a/testdata/undeadly.org
+++ b/internal/href/testdata/undeadly.org
diff --git a/re.go b/internal/re/re.go
index fab6386..b235282 100644
--- a/re.go
+++ b/internal/re/re.go
@@ -1,4 +1,4 @@
-package main
+package re
import (
"errors"
@@ -8,7 +8,7 @@ import (
var errNotRE = errors.New("not re")
-func re(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/re_test.go b/internal/re/re_test.go
index 251de25..9824392 100644
--- a/re_test.go
+++ b/internal/re/re_test.go
@@ -1,4 +1,4 @@
-package main
+package re
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 := re(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/main.go b/main.go
index 3e63b63..105893a 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,10 @@ import (
"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"
)
@@ -49,20 +53,16 @@ func privmsg(room string) irc.HandlerFunc {
}
}()
t := line.Text()
- l, okLast := last.Get(line.Nick)
switch {
case line.Nick == conn.Me().Nick:
// ignore self
- case isFlood(t):
+ case flood.Is(t):
log.Println("flood", line.Nick)
conn.Kick(room, line.Nick)
- case okLast && noSpaceCompare(t, l.(string)):
- log.Println("kick", 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(tofix.(string), t[1:], global)
+ 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)
@@ -72,11 +72,11 @@ func privmsg(room string) irc.HandlerFunc {
}
fallthrough
default:
- for _, v := range getLinks(t) {
+ for _, v := range href.Links(t) {
title, ok := titles.Get(v)
if !ok {
var err error
- title, err = getTitle(v)
+ title, err = href.Title(v)
if err != nil {
log.Println(v, err)
}
@@ -121,7 +121,7 @@ func main() {
conn.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, _ *irc.Line) {
conn.Join(*room)
- watch(newNotify(conn, *room), feeds)
+ feeds.Watch(newNotify(conn, *room), feeds.Feeds)
})
conn.HandleBG(irc.PRIVMSG, privmsg(*room))