aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go93
1 files changed, 93 insertions, 0 deletions
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
+}