Blob


1 package main
3 import (
4 "crypto/tls"
5 "flag"
6 "fmt"
7 "io"
8 "io/ioutil"
9 "log"
10 "net/http"
11 "path"
12 "path/filepath"
13 "regexp"
14 "strings"
16 "github.com/andybalholm/cascadia"
17 irc "github.com/fluffle/goirc/client"
18 "golang.org/x/net/html"
19 )
21 var (
22 baseurl = flag.String("baseurl", "gemini://m2i.omarpolo.com", "base url")
23 matrixOutDir = flag.String("matrix-out", "", "matrix out directory")
25 msgRe = regexp.MustCompile(`https://.*/[^\s]+\.(txt|png|jpg|jpeg|gif)`)
26 channel = "#gemini-it"
28 tooLongRe = regexp.MustCompile(`full message at (https://libera.ems.host/.*)[)]`)
30 httplink = regexp.MustCompile(`https?://[^\s)]+`)
31 gemlink = regexp.MustCompile(`gemini://[^\s)]+`)
32 )
34 func matrix2gemini(conn *irc.Conn, line *irc.Line) {
35 matches := msgRe.FindAllString(line.Text(), -1)
37 // it's not a good idea to defer inside a loop, but we know
38 // len(matches) is small (usually just 1). Morover, I like
39 // living in danger!
41 for _, link := range matches {
42 resp, err := http.Get(link)
43 if err != nil {
44 conn.Privmsg(
45 channel,
46 fmt.Sprintf("failed to download %q: %s", link, err),
47 )
48 continue
49 }
50 defer resp.Body.Close()
52 ext := path.Ext(link)
53 tmpfile, err := ioutil.TempFile(*matrixOutDir, "message-*"+ext)
54 if err != nil {
55 conn.Privmsg(channel, fmt.Sprintf("failed to tmpfile: %s", err))
56 return
57 }
58 defer tmpfile.Close()
60 io.Copy(tmpfile, resp.Body)
62 conn.Privmsg(
63 channel,
64 fmt.Sprintf(
65 "better: %s/%s",
66 *baseurl,
67 filepath.Base(tmpfile.Name()),
68 ),
69 )
70 }
71 }
73 func messageTooLong(conn *irc.Conn, line *irc.Line) {
74 matches := tooLongRe.FindStringSubmatch(line.Text())
75 if len(matches) != 2 {
76 return
77 }
79 url := matches[1]
81 resp, err := http.Get(url)
82 if err != nil {
83 conn.Privmsg(
84 channel,
85 fmt.Sprintf("failed to download %q: %s", url, err),
86 )
87 return
88 }
89 defer resp.Body.Close()
91 sb := &strings.Builder{}
92 if _, err := io.Copy(sb, resp.Body); err != nil {
93 conn.Privmsg(
94 channel,
95 fmt.Sprintf("failed to read body of %q: %s", url, err),
96 )
97 return
98 }
100 conn.Privmsg(channel, fmt.Sprintf("%s ha detto:", line.Nick))
101 for _, line := range strings.Split(sb.String(), "\n") {
102 conn.Privmsg(
103 channel,
104 line,
109 func stringifyNode(node *html.Node) string {
110 s := ""
112 if node.Type == html.TextNode {
113 return node.Data
116 for child := node.FirstChild; child != nil; child = child.NextSibling {
117 s += stringifyNode(child)
120 return s
123 func wwwpagetitle(conn *irc.Conn, line *irc.Line) {
124 matches := httplink.FindAllString(line.Text(), -1)
126 for _, link := range matches {
127 log.Println("fetching", link, "...")
129 resp, err := http.Get(link)
130 if err != nil {
131 continue
133 defer resp.Body.Close()
135 doc, err := html.Parse(resp.Body)
136 if err != nil {
137 continue
140 sel := cascadia.MustCompile("head > title")
141 n := cascadia.Query(doc, sel)
143 if n == nil {
144 continue
147 title := stringifyNode(n)
148 if len(title) > 50 {
149 title = title[:50]
151 conn.Privmsg(channel, stringifyNode(n))
155 func gempagetitle(conn *irc.Conn, line *irc.Line) {
156 matches := gemlink.FindAllString(line.Text(), -1)
158 for _, link := range matches {
159 log.Println("fetching", link, "...")
160 title, err := geminiTitle(link)
161 if err != nil {
162 continue
165 conn.Privmsg(channel, title)
169 func dostuff(conn *irc.Conn, line *irc.Line) {
170 matrix2gemini(conn, line)
171 messageTooLong(conn, line)
172 wwwpagetitle(conn, line)
173 gempagetitle(conn, line)
174 // ...
177 func main() {
178 flag.Parse()
180 cfg := irc.NewConfig("gemitbot")
181 cfg.SSL = true
182 cfg.SSLConfig = &tls.Config{ServerName: "irc.libera.chat"}
183 cfg.Server = "irc.libera.chat:7000"
184 cfg.NewNick = func(n string) string { return n + "^" }
186 c := irc.Client(cfg)
188 c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
189 log.Println("connected, joining", channel)
190 conn.Join(channel)
191 })
193 c.HandleFunc(irc.PRIVMSG, dostuff)
194 c.HandleFunc(irc.ACTION, dostuff)
196 quit := make(chan bool)
198 c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
199 quit <- true
200 })
202 if err := c.Connect(); err != nil {
203 log.Fatalln("connection error:", err)
206 <-quit