001
2021-04-27
op
package main
004
2021-04-27
op
"crypto/tls"
008
2021-04-27
op
"io/ioutil"
010
2021-04-27
op
"net/http"
012
2021-04-27
op
"path/filepath"
014
2021-11-07
op
"strings"
016
2021-04-27
op
irc "github.com/fluffle/goirc/client"
020
2021-11-07
op
baseurl = flag.String("baseurl", "gemini://m2i.omarpolo.com", "base url")
021
2021-04-27
op
matrixOutDir = flag.String("matrix-out", "", "matrix out directory")
023
2021-11-07
op
msgRe = regexp.MustCompile(`https://.*/[^\s]+\.(txt|png|jpg|jpeg|gif)`)
024
2021-07-30
op
channel = "#gemini-it"
026
2021-11-07
op
tooLongRe = regexp.MustCompile(`full message at (https://libera.ems.host/.*)[)]`)
029
2021-04-27
op
func matrix2gemini(conn *irc.Conn, line *irc.Line) {
030
2021-07-30
op
matches := msgRe.FindAllString(line.Text(), -1)
032
2021-04-27
op
// it's not a good idea to defer inside a loop, but we know
033
2021-04-27
op
// len(matches) is small (usually just 1). Morover, I like
034
2021-04-27
op
// living in danger!
036
2021-04-27
op
for _, link := range matches {
037
2021-04-27
op
resp, err := http.Get(link)
038
2021-04-27
op
if err != nil {
039
2021-04-27
op
conn.Privmsg(
041
2021-04-27
op
fmt.Sprintf("failed to download %q: %s", link, err),
045
2021-04-27
op
defer resp.Body.Close()
047
2021-07-30
op
ext := path.Ext(link)
048
2021-11-07
op
tmpfile, err := ioutil.TempFile(*matrixOutDir, "message-*"+ext)
049
2021-04-27
op
if err != nil {
050
2021-04-27
op
conn.Privmsg(channel, fmt.Sprintf("failed to tmpfile: %s", err))
053
2021-04-27
op
defer tmpfile.Close()
055
2021-04-27
op
io.Copy(tmpfile, resp.Body)
057
2021-04-27
op
conn.Privmsg(
059
2021-04-27
op
fmt.Sprintf(
060
2021-04-27
op
"better: %s/%s",
061
2021-04-27
op
*baseurl,
062
2021-04-27
op
filepath.Base(tmpfile.Name()),
068
2021-11-07
op
func messageTooLong(conn *irc.Conn, line *irc.Line) {
069
2021-11-07
op
matches := tooLongRe.FindStringSubmatch(line.Text())
070
2021-11-07
op
if len(matches) != 2 {
074
2021-11-07
op
url := matches[1]
076
2021-11-07
op
resp, err := http.Get(url)
077
2021-11-07
op
if err != nil {
078
2021-11-07
op
conn.Privmsg(
080
2021-11-07
op
fmt.Sprintf("failed to download %q: %s", url, err),
084
2021-11-07
op
defer resp.Body.Close()
086
2021-11-07
op
sb := &strings.Builder{}
087
2021-11-07
op
if _, err := io.Copy(sb, resp.Body); err != nil {
088
2021-11-07
op
conn.Privmsg(
090
2021-11-07
op
fmt.Sprintf("failed to read body of %q: %s", url, err),
095
2021-11-07
op
conn.Privmsg(channel, fmt.Sprintf("%s ha detto:", line.Nick))
096
2021-11-07
op
for _, line := range strings.Split(sb.String(), "\n") {
097
2021-11-07
op
conn.Privmsg(
104
2021-04-27
op
func dostuff(conn *irc.Conn, line *irc.Line) {
105
2021-04-27
op
matrix2gemini(conn, line)
106
2021-11-07
op
messageTooLong(conn, line)
110
2021-04-27
op
func main() {
111
2021-04-27
op
flag.Parse()
113
2021-04-27
op
cfg := irc.NewConfig("gemitbot")
114
2021-04-27
op
cfg.SSL = true
115
2021-07-30
op
cfg.SSLConfig = &tls.Config{ServerName: "irc.libera.chat"}
116
2021-07-30
op
cfg.Server = "irc.libera.chat:7000"
117
2021-04-27
op
cfg.NewNick = func(n string) string { return n + "^" }
119
2021-04-27
op
c := irc.Client(cfg)
121
2021-04-27
op
c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
122
2021-04-27
op
log.Println("connected, joining", channel)
123
2021-04-27
op
conn.Join(channel)
126
2021-04-27
op
c.HandleFunc(irc.PRIVMSG, dostuff)
127
2021-04-27
op
c.HandleFunc(irc.ACTION, dostuff)
129
2021-04-27
op
quit := make(chan bool)
131
2021-04-27
op
c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
132
2021-04-27
op
quit <- true
135
2021-04-27
op
if err := c.Connect(); err != nil {
136
2021-04-27
op
log.Fatalln("connection error:", err)