aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--go.mod5
-rw-r--r--go.sum22
-rw-r--r--main.go93
3 files changed, 120 insertions, 0 deletions
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..2b8171e
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module gemitbot
+
+go 1.16
+
+require github.com/fluffle/goirc v1.1.1 // indirect
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..4400525
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,22 @@
+github.com/fluffle/goirc v1.1.1 h1:6nO+7rrED3Kp3mngoi9OmQmQHevNwDfjGpYUdWc1s0k=
+github.com/fluffle/goirc v1.1.1/go.mod h1:iRzPLv2vkuZEtbns5LioYguJkRh/bvshuWg7izf1yeE=
+github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
+github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
+golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew=
+golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..ca8b9a0
--- /dev/null
+++ b/main.go
@@ -0,0 +1,93 @@
+package main
+
+import (
+ "crypto/tls"
+ "flag"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "net/http"
+ "regexp"
+
+ irc "github.com/fluffle/goirc/client"
+)
+
+var (
+ baseurl = flag.String("baseurl", "gemini://m2i.omarpolo.com", "base url")
+ matrixOutDir = flag.String("matrix-out", "", "matrix out directory")
+
+ matrixRe = regexp.MustCompile(`https://matrix.org/[^\s]+\.txt`)
+ channel = "#kikuchiyo"
+)
+
+func matrix2gemini(conn *irc.Conn, line *irc.Line) {
+ matches := matrixRe.FindAllString(line.Text(), -1)
+
+ // it's not a good idea to defer inside a loop, but we know
+ // len(matches) is small (usually just 1). Morover, I like
+ // living in danger!
+
+ for _, link := range matches {
+ resp, err := http.Get(link)
+ if err != nil {
+ conn.Privmsg(
+ channel,
+ fmt.Sprintf("failed to download %q: %s", link, err),
+ )
+ continue
+ }
+ defer resp.Body.Close()
+
+ tmpfile, err := ioutil.TempFile(*matrixOutDir, "message-")
+ if err != nil {
+ conn.Privmsg(channel, fmt.Sprintf("failed to tmpfile: %s", err))
+ return
+ }
+ defer tmpfile.Close()
+
+ io.Copy(tmpfile, resp.Body)
+
+ conn.Privmsg(
+ channel,
+ fmt.Sprintf("better: %s/%s", *baseurl, tmpfile.Name()),
+ )
+ }
+}
+
+func dostuff(conn *irc.Conn, line *irc.Line) {
+ matrix2gemini(conn, line)
+ // ...
+}
+
+func main() {
+ flag.Parse()
+
+ cfg := irc.NewConfig("gemitbot")
+ cfg.SSL = true
+ cfg.SSLConfig = &tls.Config{ServerName: "irc.freenode.net"}
+ cfg.Server = "irc.freenode.net:7000"
+ cfg.NewNick = func(n string) string { return n + "^" }
+
+ c := irc.Client(cfg)
+
+ c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
+ log.Println("connected, joining", channel)
+ conn.Join(channel)
+ })
+
+ c.HandleFunc(irc.PRIVMSG, dostuff)
+ c.HandleFunc(irc.ACTION, dostuff)
+
+ quit := make(chan bool)
+
+ c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
+ quit <- true
+ })
+
+ if err := c.Connect(); err != nil {
+ log.Fatalln("connection error:", err)
+ }
+
+ <-quit
+}