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 resp, err := http.Get(link)
152 defer resp.Body.Close()
154 doc, err := html.Parse(resp.Body)
159 head := getPageHead(doc)
163 if title := getPageTitle(head); title != "" {
164 conn.Privmsg(channel, title)
169 func dostuff(conn *irc.Conn, line *irc.Line) {
170 matrix2gemini(conn, line)
171 messageTooLong(conn, line)
172 pagetitle(conn, line)
179 cfg := irc.NewConfig("gemitbot")
181 cfg.SSLConfig = &tls.Config{ServerName: "irc.libera.chat"}
182 cfg.Server = "irc.libera.chat:7000"
183 cfg.NewNick = func(n string) string { return n + "^" }
187 c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
188 log.Println("connected, joining", channel)
192 c.HandleFunc(irc.PRIVMSG, dostuff)
193 c.HandleFunc(irc.ACTION, dostuff)
195 quit := make(chan bool)
197 c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
201 if err := c.Connect(); err != nil {
202 log.Fatalln("connection error:", err)