Blame
Date:
Sun Nov 7 14:46:10 2021 UTC
Message:
download messages that are too long and re-send them to irc
001
2021-04-27
op
package main
002
2021-04-27
op
003
2021-04-27
op
import (
004
2021-04-27
op
"crypto/tls"
005
2021-04-27
op
"flag"
006
2021-04-27
op
"fmt"
007
2021-04-27
op
"io"
008
2021-04-27
op
"io/ioutil"
009
2021-04-27
op
"log"
010
2021-04-27
op
"net/http"
011
2021-07-30
op
"path"
012
2021-04-27
op
"path/filepath"
013
2021-04-27
op
"regexp"
014
2021-11-07
op
"strings"
015
2021-04-27
op
016
2021-04-27
op
irc "github.com/fluffle/goirc/client"
017
2021-04-27
op
)
018
2021-04-27
op
019
2021-04-27
op
var (
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")
022
2021-04-27
op
023
2021-11-07
op
msgRe = regexp.MustCompile(`https://.*/[^\s]+\.(txt|png|jpg|jpeg|gif)`)
024
2021-07-30
op
channel = "#gemini-it"
025
2021-11-07
op
026
2021-11-07
op
tooLongRe = regexp.MustCompile(`full message at (https://libera.ems.host/.*)[)]`)
027
2021-04-27
op
)
028
2021-04-27
op
029
2021-04-27
op
func matrix2gemini(conn *irc.Conn, line *irc.Line) {
030
2021-07-30
op
matches := msgRe.FindAllString(line.Text(), -1)
031
2021-04-27
op
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!
035
2021-04-27
op
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(
040
2021-04-27
op
channel,
041
2021-04-27
op
fmt.Sprintf("failed to download %q: %s", link, err),
042
2021-04-27
op
)
043
2021-04-27
op
continue
044
2021-04-27
op
}
045
2021-04-27
op
defer resp.Body.Close()
046
2021-04-27
op
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))
051
2021-04-27
op
return
052
2021-04-27
op
}
053
2021-04-27
op
defer tmpfile.Close()
054
2021-04-27
op
055
2021-04-27
op
io.Copy(tmpfile, resp.Body)
056
2021-04-27
op
057
2021-04-27
op
conn.Privmsg(
058
2021-04-27
op
channel,
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()),
063
2021-04-27
op
),
064
2021-04-27
op
)
065
2021-04-27
op
}
066
2021-04-27
op
}
067
2021-04-27
op
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 {
071
2021-11-07
op
return
072
2021-11-07
op
}
073
2021-11-07
op
074
2021-11-07
op
url := matches[1]
075
2021-11-07
op
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(
079
2021-11-07
op
channel,
080
2021-11-07
op
fmt.Sprintf("failed to download %q: %s", url, err),
081
2021-11-07
op
)
082
2021-11-07
op
return
083
2021-11-07
op
}
084
2021-11-07
op
defer resp.Body.Close()
085
2021-11-07
op
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(
089
2021-11-07
op
channel,
090
2021-11-07
op
fmt.Sprintf("failed to read body of %q: %s", url, err),
091
2021-11-07
op
)
092
2021-11-07
op
return
093
2021-11-07
op
}
094
2021-11-07
op
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(
098
2021-11-07
op
channel,
099
2021-11-07
op
line,
100
2021-11-07
op
)
101
2021-11-07
op
}
102
2021-11-07
op
}
103
2021-11-07
op
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)
107
2021-04-27
op
// ...
108
2021-04-27
op
}
109
2021-04-27
op
110
2021-04-27
op
func main() {
111
2021-04-27
op
flag.Parse()
112
2021-04-27
op
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 + "^" }
118
2021-04-27
op
119
2021-04-27
op
c := irc.Client(cfg)
120
2021-04-27
op
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)
124
2021-04-27
op
})
125
2021-04-27
op
126
2021-04-27
op
c.HandleFunc(irc.PRIVMSG, dostuff)
127
2021-04-27
op
c.HandleFunc(irc.ACTION, dostuff)
128
2021-04-27
op
129
2021-04-27
op
quit := make(chan bool)
130
2021-04-27
op
131
2021-04-27
op
c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
132
2021-04-27
op
quit <- true
133
2021-04-27
op
})
134
2021-04-27
op
135
2021-04-27
op
if err := c.Connect(); err != nil {
136
2021-04-27
op
log.Fatalln("connection error:", err)
137
2021-04-27
op
}
138
2021-04-27
op
139
2021-04-27
op
<-quit
140
2021-04-27
op
}
Omar Polo