Blame


1 7cc05214 2021-04-27 op package main
2 7cc05214 2021-04-27 op
3 7cc05214 2021-04-27 op import (
4 7cc05214 2021-04-27 op "crypto/tls"
5 7cc05214 2021-04-27 op "flag"
6 7cc05214 2021-04-27 op "fmt"
7 7cc05214 2021-04-27 op "io"
8 7cc05214 2021-04-27 op "io/ioutil"
9 7cc05214 2021-04-27 op "log"
10 7cc05214 2021-04-27 op "net/http"
11 ae97f748 2021-07-30 op "path"
12 5ad39add 2021-04-27 op "path/filepath"
13 7cc05214 2021-04-27 op "regexp"
14 88370dfe 2021-11-07 op "strings"
15 7cc05214 2021-04-27 op
16 7cc05214 2021-04-27 op irc "github.com/fluffle/goirc/client"
17 7cc05214 2021-04-27 op )
18 7cc05214 2021-04-27 op
19 7cc05214 2021-04-27 op var (
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")
22 7cc05214 2021-04-27 op
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"
25 88370dfe 2021-11-07 op
26 88370dfe 2021-11-07 op tooLongRe = regexp.MustCompile(`full message at (https://libera.ems.host/.*)[)]`)
27 7cc05214 2021-04-27 op )
28 7cc05214 2021-04-27 op
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)
31 7cc05214 2021-04-27 op
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!
35 7cc05214 2021-04-27 op
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 {
39 7cc05214 2021-04-27 op conn.Privmsg(
40 7cc05214 2021-04-27 op channel,
41 7cc05214 2021-04-27 op fmt.Sprintf("failed to download %q: %s", link, err),
42 7cc05214 2021-04-27 op )
43 7cc05214 2021-04-27 op continue
44 7cc05214 2021-04-27 op }
45 7cc05214 2021-04-27 op defer resp.Body.Close()
46 7cc05214 2021-04-27 op
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))
51 7cc05214 2021-04-27 op return
52 7cc05214 2021-04-27 op }
53 7cc05214 2021-04-27 op defer tmpfile.Close()
54 7cc05214 2021-04-27 op
55 7cc05214 2021-04-27 op io.Copy(tmpfile, resp.Body)
56 7cc05214 2021-04-27 op
57 7cc05214 2021-04-27 op conn.Privmsg(
58 7cc05214 2021-04-27 op channel,
59 5ad39add 2021-04-27 op fmt.Sprintf(
60 5ad39add 2021-04-27 op "better: %s/%s",
61 5ad39add 2021-04-27 op *baseurl,
62 5ad39add 2021-04-27 op filepath.Base(tmpfile.Name()),
63 5ad39add 2021-04-27 op ),
64 7cc05214 2021-04-27 op )
65 7cc05214 2021-04-27 op }
66 7cc05214 2021-04-27 op }
67 7cc05214 2021-04-27 op
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 {
71 88370dfe 2021-11-07 op return
72 88370dfe 2021-11-07 op }
73 88370dfe 2021-11-07 op
74 88370dfe 2021-11-07 op url := matches[1]
75 88370dfe 2021-11-07 op
76 88370dfe 2021-11-07 op resp, err := http.Get(url)
77 88370dfe 2021-11-07 op if err != nil {
78 88370dfe 2021-11-07 op conn.Privmsg(
79 88370dfe 2021-11-07 op channel,
80 88370dfe 2021-11-07 op fmt.Sprintf("failed to download %q: %s", url, err),
81 88370dfe 2021-11-07 op )
82 88370dfe 2021-11-07 op return
83 88370dfe 2021-11-07 op }
84 88370dfe 2021-11-07 op defer resp.Body.Close()
85 88370dfe 2021-11-07 op
86 88370dfe 2021-11-07 op sb := &strings.Builder{}
87 88370dfe 2021-11-07 op if _, err := io.Copy(sb, resp.Body); err != nil {
88 88370dfe 2021-11-07 op conn.Privmsg(
89 88370dfe 2021-11-07 op channel,
90 88370dfe 2021-11-07 op fmt.Sprintf("failed to read body of %q: %s", url, err),
91 88370dfe 2021-11-07 op )
92 88370dfe 2021-11-07 op return
93 88370dfe 2021-11-07 op }
94 88370dfe 2021-11-07 op
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") {
97 88370dfe 2021-11-07 op conn.Privmsg(
98 88370dfe 2021-11-07 op channel,
99 88370dfe 2021-11-07 op line,
100 88370dfe 2021-11-07 op )
101 88370dfe 2021-11-07 op }
102 88370dfe 2021-11-07 op }
103 88370dfe 2021-11-07 op
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)
107 7cc05214 2021-04-27 op // ...
108 7cc05214 2021-04-27 op }
109 7cc05214 2021-04-27 op
110 7cc05214 2021-04-27 op func main() {
111 7cc05214 2021-04-27 op flag.Parse()
112 7cc05214 2021-04-27 op
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 + "^" }
118 7cc05214 2021-04-27 op
119 7cc05214 2021-04-27 op c := irc.Client(cfg)
120 7cc05214 2021-04-27 op
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)
124 7cc05214 2021-04-27 op })
125 7cc05214 2021-04-27 op
126 7cc05214 2021-04-27 op c.HandleFunc(irc.PRIVMSG, dostuff)
127 7cc05214 2021-04-27 op c.HandleFunc(irc.ACTION, dostuff)
128 7cc05214 2021-04-27 op
129 7cc05214 2021-04-27 op quit := make(chan bool)
130 7cc05214 2021-04-27 op
131 7cc05214 2021-04-27 op c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
132 7cc05214 2021-04-27 op quit <- true
133 7cc05214 2021-04-27 op })
134 7cc05214 2021-04-27 op
135 7cc05214 2021-04-27 op if err := c.Connect(); err != nil {
136 7cc05214 2021-04-27 op log.Fatalln("connection error:", err)
137 7cc05214 2021-04-27 op }
138 7cc05214 2021-04-27 op
139 7cc05214 2021-04-27 op <-quit
140 7cc05214 2021-04-27 op }