Blob
Date:
Fri Jul 30 11:10:48 2021 UTC
Message:
switch to libera
package main
import (
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"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 = "#gemini-italia"
)
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,
filepath.Base(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.libera.chat"}
cfg.Server = "irc.libera.chat: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
}
Omar Polo