16 irc "github.com/fluffle/goirc/client"
17 "golang.org/x/net/html"
21 baseurl = flag.String("baseurl", "gemini://m2i.omarpolo.com", "base url")
22 matrixOutDir = flag.String("matrix-out", "", "matrix out directory")
24 msgRe = regexp.MustCompile(`https://.*/[^\s]+\.(txt|png|jpg|jpeg|gif)`)
25 channel = "#gemini-it"
27 tooLongRe = regexp.MustCompile(`full message at (https://libera.ems.host/.*)[)]`)
29 httplink = regexp.MustCompile(`https?://[^\s]+`)
32 func matrix2gemini(conn *irc.Conn, line *irc.Line) {
33 matches := msgRe.FindAllString(line.Text(), -1)
35 // it's not a good idea to defer inside a loop, but we know
36 // len(matches) is small (usually just 1). Morover, I like
39 for _, link := range matches {
40 resp, err := http.Get(link)
44 fmt.Sprintf("failed to download %q: %s", link, err),
48 defer resp.Body.Close()
51 tmpfile, err := ioutil.TempFile(*matrixOutDir, "message-*"+ext)
53 conn.Privmsg(channel, fmt.Sprintf("failed to tmpfile: %s", err))
58 io.Copy(tmpfile, resp.Body)
65 filepath.Base(tmpfile.Name()),
71 func messageTooLong(conn *irc.Conn, line *irc.Line) {
72 matches := tooLongRe.FindStringSubmatch(line.Text())
73 if len(matches) != 2 {
79 resp, err := http.Get(url)
83 fmt.Sprintf("failed to download %q: %s", url, err),
87 defer resp.Body.Close()
89 sb := &strings.Builder{}
90 if _, err := io.Copy(sb, resp.Body); err != nil {
93 fmt.Sprintf("failed to read body of %q: %s", url, err),
98 conn.Privmsg(channel, fmt.Sprintf("%s ha detto:", line.Nick))
99 for _, line := range strings.Split(sb.String(), "\n") {
107 func stringifyNode(node *html.Node) string {
110 if node.Type == html.TextNode {
114 for child := node.FirstChild; child != nil; child = child.NextSibling {
115 s += stringifyNode(child)
121 func getPageTitle(node *html.Node) string {
122 for child := node.FirstChild; child != nil; child = child.NextSibling {
123 if child.Type == html.ElementNode && child.Data == "title" {
124 text := stringifyNode(node)
125 return strings.Trim(text, " \t\n")
131 func getPageHead(node *html.Node) *html.Node {
132 if node.Type == html.ElementNode && node.Data == "head" {
136 for child := node.FirstChild; child != nil; child = child.NextSibling {
137 if n := getPageHead(child); n != nil {
144 func pagetitle(conn *irc.Conn, line *irc.Line) {
145 matches := httplink.FindAllString(line.Text(), -1)
147 for _, link := range matches {
148 log.Println("fetching", link, "...")
150 resp, err := http.Get(link)
154 defer resp.Body.Close()
156 doc, err := html.Parse(resp.Body)
161 head := getPageHead(doc)
165 if title := getPageTitle(head); title != "" {
166 conn.Privmsg(channel, title)
171 func dostuff(conn *irc.Conn, line *irc.Line) {
172 matrix2gemini(conn, line)
173 messageTooLong(conn, line)
174 pagetitle(conn, line)
181 cfg := irc.NewConfig("gemitbot")
183 cfg.SSLConfig = &tls.Config{ServerName: "irc.libera.chat"}
184 cfg.Server = "irc.libera.chat:7000"
185 cfg.NewNick = func(n string) string { return n + "^" }
189 c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
190 log.Println("connected, joining", channel)
194 c.HandleFunc(irc.PRIVMSG, dostuff)
195 c.HandleFunc(irc.ACTION, dostuff)
197 quit := make(chan bool)
199 c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
203 if err := c.Connect(); err != nil {
204 log.Fatalln("connection error:", err)