12 5ad39add 2021-04-27 op "path/filepath"
16 7cc05214 2021-04-27 op irc "github.com/fluffle/goirc/client"
20 88370dfe 2021-11-07 op baseurl = flag.String("baseurl", "gemini://m2i.omarpolo.com", "base url")
21 7cc05214 2021-04-27 op matrixOutDir = flag.String("matrix-out", "", "matrix out directory")
23 88370dfe 2021-11-07 op msgRe = regexp.MustCompile(`https://.*/[^\s]+\.(txt|png|jpg|jpeg|gif)`)
24 c6fd4def 2021-07-30 op channel = "#gemini-it"
26 88370dfe 2021-11-07 op tooLongRe = regexp.MustCompile(`full message at (https://libera.ems.host/.*)[)]`)
29 7cc05214 2021-04-27 op func matrix2gemini(conn *irc.Conn, line *irc.Line) {
30 ae97f748 2021-07-30 op matches := msgRe.FindAllString(line.Text(), -1)
32 7cc05214 2021-04-27 op // it's not a good idea to defer inside a loop, but we know
33 7cc05214 2021-04-27 op // len(matches) is small (usually just 1). Morover, I like
34 7cc05214 2021-04-27 op // living in danger!
36 7cc05214 2021-04-27 op for _, link := range matches {
37 7cc05214 2021-04-27 op resp, err := http.Get(link)
38 7cc05214 2021-04-27 op if err != nil {
41 7cc05214 2021-04-27 op fmt.Sprintf("failed to download %q: %s", link, err),
45 7cc05214 2021-04-27 op defer resp.Body.Close()
47 ae97f748 2021-07-30 op ext := path.Ext(link)
48 88370dfe 2021-11-07 op tmpfile, err := ioutil.TempFile(*matrixOutDir, "message-*"+ext)
49 7cc05214 2021-04-27 op if err != nil {
50 7cc05214 2021-04-27 op conn.Privmsg(channel, fmt.Sprintf("failed to tmpfile: %s", err))
53 7cc05214 2021-04-27 op defer tmpfile.Close()
55 7cc05214 2021-04-27 op io.Copy(tmpfile, resp.Body)
60 5ad39add 2021-04-27 op "better: %s/%s",
62 5ad39add 2021-04-27 op filepath.Base(tmpfile.Name()),
68 88370dfe 2021-11-07 op func messageTooLong(conn *irc.Conn, line *irc.Line) {
69 88370dfe 2021-11-07 op matches := tooLongRe.FindStringSubmatch(line.Text())
70 88370dfe 2021-11-07 op if len(matches) != 2 {
74 88370dfe 2021-11-07 op url := matches[1]
76 88370dfe 2021-11-07 op resp, err := http.Get(url)
77 88370dfe 2021-11-07 op if err != nil {
80 88370dfe 2021-11-07 op fmt.Sprintf("failed to download %q: %s", url, err),
84 88370dfe 2021-11-07 op defer resp.Body.Close()
86 88370dfe 2021-11-07 op sb := &strings.Builder{}
87 88370dfe 2021-11-07 op if _, err := io.Copy(sb, resp.Body); err != nil {
90 88370dfe 2021-11-07 op fmt.Sprintf("failed to read body of %q: %s", url, err),
95 88370dfe 2021-11-07 op conn.Privmsg(channel, fmt.Sprintf("%s ha detto:", line.Nick))
96 88370dfe 2021-11-07 op for _, line := range strings.Split(sb.String(), "\n") {
104 7cc05214 2021-04-27 op func dostuff(conn *irc.Conn, line *irc.Line) {
105 7cc05214 2021-04-27 op matrix2gemini(conn, line)
106 88370dfe 2021-11-07 op messageTooLong(conn, line)
110 7cc05214 2021-04-27 op func main() {
113 7cc05214 2021-04-27 op cfg := irc.NewConfig("gemitbot")
114 7cc05214 2021-04-27 op cfg.SSL = true
115 d0286003 2021-07-30 op cfg.SSLConfig = &tls.Config{ServerName: "irc.libera.chat"}
116 d0286003 2021-07-30 op cfg.Server = "irc.libera.chat:7000"
117 7cc05214 2021-04-27 op cfg.NewNick = func(n string) string { return n + "^" }
119 7cc05214 2021-04-27 op c := irc.Client(cfg)
121 7cc05214 2021-04-27 op c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
122 7cc05214 2021-04-27 op log.Println("connected, joining", channel)
123 7cc05214 2021-04-27 op conn.Join(channel)
126 7cc05214 2021-04-27 op c.HandleFunc(irc.PRIVMSG, dostuff)
127 7cc05214 2021-04-27 op c.HandleFunc(irc.ACTION, dostuff)
129 7cc05214 2021-04-27 op quit := make(chan bool)
131 7cc05214 2021-04-27 op c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
135 7cc05214 2021-04-27 op if err := c.Connect(); err != nil {
136 7cc05214 2021-04-27 op log.Fatalln("connection error:", err)