commit 88370dfe7b3c35f7d841ab7fb32434be38812b25 from: Omar Polo date: Sun Nov 07 14:46:10 2021 UTC download messages that are too long and re-send them to irc commit - 29a1c0f1221ba2600ad3f55ecd547357b86906cf commit + 88370dfe7b3c35f7d841ab7fb32434be38812b25 blob - b83b09fcf42c5d16a15177396bd631c1e2ece8f5 blob + bb46e0405388b275de9287aecdea2a0fd347c5a3 --- main.go +++ main.go @@ -11,16 +11,19 @@ import ( "path" "path/filepath" "regexp" + "strings" irc "github.com/fluffle/goirc/client" ) var ( - baseurl = flag.String("baseurl", "gemini://m2i.omarpolo.com", "base url") + baseurl = flag.String("baseurl", "gemini://m2i.omarpolo.com", "base url") matrixOutDir = flag.String("matrix-out", "", "matrix out directory") - msgRe = regexp.MustCompile(`https://.*/[^\s]+\.(txt|png|jpg|jpeg|gif)`) + msgRe = regexp.MustCompile(`https://.*/[^\s]+\.(txt|png|jpg|jpeg|gif)`) channel = "#gemini-it" + + tooLongRe = regexp.MustCompile(`full message at (https://libera.ems.host/.*)[)]`) ) func matrix2gemini(conn *irc.Conn, line *irc.Line) { @@ -42,7 +45,7 @@ func matrix2gemini(conn *irc.Conn, line *irc.Line) { defer resp.Body.Close() ext := path.Ext(link) - tmpfile, err := ioutil.TempFile(*matrixOutDir, "message-*" + ext) + tmpfile, err := ioutil.TempFile(*matrixOutDir, "message-*"+ext) if err != nil { conn.Privmsg(channel, fmt.Sprintf("failed to tmpfile: %s", err)) return @@ -62,8 +65,45 @@ func matrix2gemini(conn *irc.Conn, line *irc.Line) { } } +func messageTooLong(conn *irc.Conn, line *irc.Line) { + matches := tooLongRe.FindStringSubmatch(line.Text()) + if len(matches) != 2 { + return + } + + url := matches[1] + + resp, err := http.Get(url) + if err != nil { + conn.Privmsg( + channel, + fmt.Sprintf("failed to download %q: %s", url, err), + ) + return + } + defer resp.Body.Close() + + sb := &strings.Builder{} + if _, err := io.Copy(sb, resp.Body); err != nil { + conn.Privmsg( + channel, + fmt.Sprintf("failed to read body of %q: %s", url, err), + ) + return + } + + conn.Privmsg(channel, fmt.Sprintf("%s ha detto:", line.Nick)) + for _, line := range strings.Split(sb.String(), "\n") { + conn.Privmsg( + channel, + line, + ) + } +} + func dostuff(conn *irc.Conn, line *irc.Line) { matrix2gemini(conn, line) + messageTooLong(conn, line) // ... }