12 5ad39add 2021-04-27 op "path/filepath"
16 deee939b 2022-01-08 op "github.com/andybalholm/cascadia"
17 7cc05214 2021-04-27 op irc "github.com/fluffle/goirc/client"
18 5659492e 2021-12-02 op "golang.org/x/net/html"
22 88370dfe 2021-11-07 op baseurl = flag.String("baseurl", "gemini://m2i.omarpolo.com", "base url")
23 7cc05214 2021-04-27 op matrixOutDir = flag.String("matrix-out", "", "matrix out directory")
25 88370dfe 2021-11-07 op msgRe = regexp.MustCompile(`https://.*/[^\s]+\.(txt|png|jpg|jpeg|gif)`)
26 c6fd4def 2021-07-30 op channel = "#gemini-it"
28 88370dfe 2021-11-07 op tooLongRe = regexp.MustCompile(`full message at (https://libera.ems.host/.*)[)]`)
30 4852f411 2021-12-02 op httplink = regexp.MustCompile(`https?://[^\s)]+`)
33 7cc05214 2021-04-27 op func matrix2gemini(conn *irc.Conn, line *irc.Line) {
34 ae97f748 2021-07-30 op matches := msgRe.FindAllString(line.Text(), -1)
36 7cc05214 2021-04-27 op // it's not a good idea to defer inside a loop, but we know
37 7cc05214 2021-04-27 op // len(matches) is small (usually just 1). Morover, I like
38 7cc05214 2021-04-27 op // living in danger!
40 7cc05214 2021-04-27 op for _, link := range matches {
41 7cc05214 2021-04-27 op resp, err := http.Get(link)
42 7cc05214 2021-04-27 op if err != nil {
45 7cc05214 2021-04-27 op fmt.Sprintf("failed to download %q: %s", link, err),
49 7cc05214 2021-04-27 op defer resp.Body.Close()
51 ae97f748 2021-07-30 op ext := path.Ext(link)
52 88370dfe 2021-11-07 op tmpfile, err := ioutil.TempFile(*matrixOutDir, "message-*"+ext)
53 7cc05214 2021-04-27 op if err != nil {
54 7cc05214 2021-04-27 op conn.Privmsg(channel, fmt.Sprintf("failed to tmpfile: %s", err))
57 7cc05214 2021-04-27 op defer tmpfile.Close()
59 7cc05214 2021-04-27 op io.Copy(tmpfile, resp.Body)
64 5ad39add 2021-04-27 op "better: %s/%s",
66 5ad39add 2021-04-27 op filepath.Base(tmpfile.Name()),
72 88370dfe 2021-11-07 op func messageTooLong(conn *irc.Conn, line *irc.Line) {
73 88370dfe 2021-11-07 op matches := tooLongRe.FindStringSubmatch(line.Text())
74 88370dfe 2021-11-07 op if len(matches) != 2 {
78 88370dfe 2021-11-07 op url := matches[1]
80 88370dfe 2021-11-07 op resp, err := http.Get(url)
81 88370dfe 2021-11-07 op if err != nil {
84 88370dfe 2021-11-07 op fmt.Sprintf("failed to download %q: %s", url, err),
88 88370dfe 2021-11-07 op defer resp.Body.Close()
90 88370dfe 2021-11-07 op sb := &strings.Builder{}
91 88370dfe 2021-11-07 op if _, err := io.Copy(sb, resp.Body); err != nil {
94 88370dfe 2021-11-07 op fmt.Sprintf("failed to read body of %q: %s", url, err),
99 88370dfe 2021-11-07 op conn.Privmsg(channel, fmt.Sprintf("%s ha detto:", line.Nick))
100 88370dfe 2021-11-07 op for _, line := range strings.Split(sb.String(), "\n") {
101 88370dfe 2021-11-07 op conn.Privmsg(
108 3adf60f8 2021-12-02 op func stringifyNode(node *html.Node) string {
111 3adf60f8 2021-12-02 op if node.Type == html.TextNode {
112 3adf60f8 2021-12-02 op return node.Data
115 3adf60f8 2021-12-02 op for child := node.FirstChild; child != nil; child = child.NextSibling {
116 3adf60f8 2021-12-02 op s += stringifyNode(child)
122 5659492e 2021-12-02 op func pagetitle(conn *irc.Conn, line *irc.Line) {
123 4852f411 2021-12-02 op log.Println("text is", line.Text())
124 5659492e 2021-12-02 op matches := httplink.FindAllString(line.Text(), -1)
126 5659492e 2021-12-02 op for _, link := range matches {
127 d26637f1 2021-12-02 op log.Println("fetching", link, "...")
129 5659492e 2021-12-02 op resp, err := http.Get(link)
130 5659492e 2021-12-02 op if err != nil {
133 5659492e 2021-12-02 op defer resp.Body.Close()
135 5659492e 2021-12-02 op doc, err := html.Parse(resp.Body)
136 5659492e 2021-12-02 op if err != nil {
140 deee939b 2022-01-08 op sel := cascadia.MustCompile("head > title")
141 deee939b 2022-01-08 op n := cascadia.Query(doc, sel)
143 deee939b 2022-01-08 op if n == nil {
147 deee939b 2022-01-08 op conn.Privmsg(channel, stringifyNode(n))
151 7cc05214 2021-04-27 op func dostuff(conn *irc.Conn, line *irc.Line) {
152 7cc05214 2021-04-27 op matrix2gemini(conn, line)
153 88370dfe 2021-11-07 op messageTooLong(conn, line)
154 5659492e 2021-12-02 op pagetitle(conn, line)
158 7cc05214 2021-04-27 op func main() {
161 7cc05214 2021-04-27 op cfg := irc.NewConfig("gemitbot")
162 7cc05214 2021-04-27 op cfg.SSL = true
163 d0286003 2021-07-30 op cfg.SSLConfig = &tls.Config{ServerName: "irc.libera.chat"}
164 d0286003 2021-07-30 op cfg.Server = "irc.libera.chat:7000"
165 7cc05214 2021-04-27 op cfg.NewNick = func(n string) string { return n + "^" }
167 7cc05214 2021-04-27 op c := irc.Client(cfg)
169 7cc05214 2021-04-27 op c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
170 7cc05214 2021-04-27 op log.Println("connected, joining", channel)
171 7cc05214 2021-04-27 op conn.Join(channel)
174 7cc05214 2021-04-27 op c.HandleFunc(irc.PRIVMSG, dostuff)
175 7cc05214 2021-04-27 op c.HandleFunc(irc.ACTION, dostuff)
177 7cc05214 2021-04-27 op quit := make(chan bool)
179 7cc05214 2021-04-27 op c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
183 7cc05214 2021-04-27 op if err := c.Connect(); err != nil {
184 7cc05214 2021-04-27 op log.Fatalln("connection error:", err)