2020-01-29 16:57:42 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-02-03 16:18:41 -07:00
|
|
|
"os"
|
2020-02-26 16:15:22 -07:00
|
|
|
"regexp"
|
2020-02-24 18:05:00 -07:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-01-29 16:57:42 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrix"
|
2020-02-11 17:49:33 -07:00
|
|
|
"suah.dev/mcchunkie/plugins"
|
2020-01-29 16:57:42 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-02-12 20:11:38 -07:00
|
|
|
var username, password, userID, accessToken, server, db, avatar, botOwner string
|
2020-02-05 17:35:16 -07:00
|
|
|
var key, value, get string
|
2020-03-04 07:15:51 -07:00
|
|
|
var setup, doc, debug bool
|
2020-01-29 16:57:42 -07:00
|
|
|
|
2020-02-11 07:56:19 -07:00
|
|
|
flag.BoolVar(&doc, "doc", false, "print plugin information and exit")
|
|
|
|
flag.BoolVar(&setup, "s", false, "setup account")
|
2020-03-04 07:15:51 -07:00
|
|
|
flag.BoolVar(&debug, "debug", false, "print debug messages")
|
2020-02-11 07:56:19 -07:00
|
|
|
|
|
|
|
flag.StringVar(&avatar, "avatar", "", "set the avatar of the bot to specified url")
|
|
|
|
flag.StringVar(&db, "db", "db", "full path to database directory")
|
2020-02-05 17:35:16 -07:00
|
|
|
flag.StringVar(&get, "get", "", "grab an entry from the store")
|
|
|
|
flag.StringVar(&key, "key", "", "create an entry in the data store listed under 'key'")
|
2020-01-29 16:57:42 -07:00
|
|
|
flag.StringVar(&server, "server", "", "matrix server")
|
2020-02-11 07:56:19 -07:00
|
|
|
flag.StringVar(&username, "user", "", "username to connect to matrix server with")
|
|
|
|
flag.StringVar(&value, "value", "", "set the value of 'key' to be stored")
|
2020-01-29 16:57:42 -07:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2020-02-03 20:42:19 -07:00
|
|
|
pledge("stdio unveil rpath wpath cpath flock dns inet tty")
|
2020-02-02 11:13:43 -07:00
|
|
|
unveil("/etc/resolv.conf", "r")
|
|
|
|
unveil("/etc/ssl/cert.pem", "r")
|
|
|
|
unveil(db, "rwc")
|
|
|
|
unveilBlock()
|
|
|
|
|
2020-02-26 16:15:22 -07:00
|
|
|
var help = `^help: (\w+)$`
|
|
|
|
var helpRE = regexp.MustCompile(help)
|
2020-02-02 11:13:43 -07:00
|
|
|
var store, err = NewStore(db)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s\n", err)
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:35:16 -07:00
|
|
|
if key != "" && value != "" {
|
2020-02-05 22:04:04 -07:00
|
|
|
store.Set(key, value)
|
2020-02-05 17:35:16 -07:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2020-02-11 07:56:19 -07:00
|
|
|
if doc {
|
|
|
|
fmt.Println("|Plugin Name|Match|Description|")
|
2020-02-11 07:58:48 -07:00
|
|
|
fmt.Println("|----|---|---|")
|
2020-02-11 07:56:19 -07:00
|
|
|
for _, p := range plugins.Plugs {
|
|
|
|
fmt.Printf("|%s|`%s`|%s|\n", p.Name(), p.Re(), p.Descr())
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:35:16 -07:00
|
|
|
if get != "" {
|
2020-02-05 22:04:04 -07:00
|
|
|
val, err := store.Get(get)
|
2020-02-05 17:35:16 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s\n", err)
|
|
|
|
}
|
|
|
|
fmt.Println(val)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2020-01-29 16:57:42 -07:00
|
|
|
if server == "" {
|
2020-02-05 22:04:04 -07:00
|
|
|
server, err = store.Get("server")
|
2020-02-10 16:08:33 -07:00
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s\n", err)
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 16:57:42 -07:00
|
|
|
if server == "" {
|
|
|
|
log.Fatalln("please specify a server")
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:12:41 -07:00
|
|
|
} else {
|
2020-02-05 22:04:04 -07:00
|
|
|
store.Set("server", server)
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("connecting to %s\n", server)
|
|
|
|
|
|
|
|
cli, err := gomatrix.NewClient(
|
|
|
|
server,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
)
|
2020-02-10 16:08:33 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s\n", err)
|
|
|
|
}
|
2020-01-29 16:57:42 -07:00
|
|
|
|
|
|
|
if setup {
|
|
|
|
log.Println("requesting access token")
|
|
|
|
password, err = prompt(fmt.Sprintf("Password for '%s': ", username))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
resp, err := cli.Login(&gomatrix.ReqLogin{
|
|
|
|
Type: "m.login.password",
|
|
|
|
User: username,
|
|
|
|
Password: password,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2020-02-03 20:42:19 -07:00
|
|
|
// No longer need tty now that we have our info
|
|
|
|
pledge("stdio unveil rpath wpath cpath flock dns inet")
|
|
|
|
|
2020-02-05 22:04:04 -07:00
|
|
|
store.Set("username", username)
|
|
|
|
store.Set("access_token", resp.AccessToken)
|
|
|
|
store.Set("user_id", resp.UserID)
|
2020-02-02 13:41:00 -07:00
|
|
|
|
|
|
|
accessToken = resp.AccessToken
|
|
|
|
userID = resp.UserID
|
2020-01-29 16:57:42 -07:00
|
|
|
} else {
|
2020-02-05 22:04:04 -07:00
|
|
|
username, _ = store.Get("username")
|
|
|
|
accessToken, _ = store.Get("access_token")
|
|
|
|
userID, _ = store.Get("user_id")
|
2020-02-12 20:11:38 -07:00
|
|
|
botOwner, _ = store.Get("bot_owner")
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cli.SetCredentials(userID, accessToken)
|
|
|
|
cli.Store = store
|
|
|
|
syncer := gomatrix.NewDefaultSyncer(username, store)
|
|
|
|
cli.Client = http.DefaultClient
|
|
|
|
cli.Syncer = syncer
|
|
|
|
|
2020-02-12 20:11:38 -07:00
|
|
|
syncer.OnEventType("m.room.member", func(ev *gomatrix.Event) {
|
|
|
|
if ev.Sender == username {
|
|
|
|
return
|
2020-01-30 06:48:37 -07:00
|
|
|
}
|
2020-02-12 20:11:38 -07:00
|
|
|
|
|
|
|
if ev.Sender == botOwner && ev.Content["membership"] == "invite" {
|
|
|
|
log.Printf("Joining %s (invite from %s)\n", ev.RoomID, ev.Sender)
|
|
|
|
if _, err := cli.JoinRoom(ev.RoomID, "", nil); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2020-02-01 20:56:55 -07:00
|
|
|
}
|
2020-02-12 20:11:38 -07:00
|
|
|
})
|
2020-01-29 16:57:42 -07:00
|
|
|
|
2020-02-24 18:05:00 -07:00
|
|
|
go func() {
|
|
|
|
errataCount := 0
|
|
|
|
storeCount, _ := store.Get("errata_count")
|
|
|
|
openbsdRelease, _ := store.Get("openbsd_release")
|
|
|
|
errataCount, err = strconv.Atoi(storeCount)
|
|
|
|
for {
|
|
|
|
got, err := ParseRemoteErrata(
|
|
|
|
fmt.Sprintf("https://www.openbsd.org/errata%s.html", openbsdRelease),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
l := got.Length
|
|
|
|
if l > errataCount {
|
|
|
|
alertRooms, _ := store.Get("errata_rooms")
|
|
|
|
c := 0
|
|
|
|
for _, errata := range got.List {
|
|
|
|
if c+1 > errataCount {
|
|
|
|
log.Printf("%03d: %s - %s\n", errata.ID, errata.Type, errata.Desc)
|
|
|
|
for _, room := range strings.Split(alertRooms, ",") {
|
|
|
|
cli.SendNotice(room, PrintErrata(&errata))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c = c + 1
|
|
|
|
}
|
|
|
|
errataCount = l
|
|
|
|
}
|
2020-02-26 16:10:43 -07:00
|
|
|
store.Set("errata_count", strconv.Itoa(l))
|
2020-02-24 18:05:00 -07:00
|
|
|
time.Sleep(2 * time.Hour)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-01-29 16:57:42 -07:00
|
|
|
syncer.OnEventType("m.room.message", func(ev *gomatrix.Event) {
|
2020-01-29 17:12:11 -07:00
|
|
|
if ev.Sender == username {
|
2020-01-29 16:57:42 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-26 16:15:22 -07:00
|
|
|
// Sending a response per plugin hits issues, so save them and
|
|
|
|
// send as one message.
|
|
|
|
helps := []string{}
|
2020-01-31 19:57:40 -07:00
|
|
|
for _, p := range plugins.Plugs {
|
2020-02-02 19:43:26 -07:00
|
|
|
var post string
|
|
|
|
var ok bool
|
|
|
|
if post, ok = ev.Body(); !ok {
|
|
|
|
// Invaild body, for some reason
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if mtype, ok := ev.MessageType(); ok {
|
|
|
|
switch mtype {
|
|
|
|
case "m.text":
|
2020-02-26 16:15:22 -07:00
|
|
|
if helpRE.Match([]byte(post)) {
|
|
|
|
pn := p.Name()
|
|
|
|
hName := helpRE.ReplaceAllString(post, "$1")
|
|
|
|
if hName == pn || hName == "puke" {
|
|
|
|
helps = append(helps, fmt.Sprintf("**%s**: `%s` - _%s_\n", p.Name(), p.Re(), p.Descr()))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-02-10 17:10:57 -07:00
|
|
|
if p.Match(username, post) {
|
2020-03-04 07:15:51 -07:00
|
|
|
log.Printf("%s: responding to '%s'", p.Name(), ev.Sender)
|
2020-02-10 17:10:57 -07:00
|
|
|
p.SetStore(store)
|
2020-03-04 07:15:51 -07:00
|
|
|
|
|
|
|
start := time.Now()
|
2020-02-10 17:10:57 -07:00
|
|
|
p.RespondText(cli, ev, username, post)
|
2020-03-04 07:15:51 -07:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
if debug {
|
|
|
|
log.Printf("%s took %s to run\n", p.Name(), elapsed)
|
|
|
|
}
|
2020-02-10 17:10:57 -07:00
|
|
|
}
|
2020-02-02 19:43:26 -07:00
|
|
|
}
|
|
|
|
}
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
2020-02-26 16:15:22 -07:00
|
|
|
if len(helps) > 0 {
|
|
|
|
plugins.SendMD(cli, ev.RoomID, strings.Join(helps, "\n"))
|
|
|
|
}
|
2020-01-29 16:57:42 -07:00
|
|
|
})
|
|
|
|
|
2020-02-03 16:18:41 -07:00
|
|
|
if avatar != "" {
|
2020-01-29 16:57:42 -07:00
|
|
|
log.Printf("Setting avatar to: '%s'", avatar)
|
2020-02-03 16:18:41 -07:00
|
|
|
rmu, err := cli.UploadLink(avatar)
|
2020-01-29 16:57:42 -07:00
|
|
|
if err != nil {
|
2020-02-03 16:18:41 -07:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
2020-02-03 16:18:41 -07:00
|
|
|
|
|
|
|
err = cli.SetAvatarURL(rmu.ContentURI)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
log.Println("syncing..")
|
|
|
|
if err := cli.Sync(); err != nil {
|
|
|
|
fmt.Println("Sync() returned ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|