2020-01-29 16:57:42 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-01-29 17:40:15 -07:00
|
|
|
"strings"
|
2020-01-29 16:57:42 -07:00
|
|
|
"time"
|
|
|
|
|
2020-01-31 19:57:40 -07:00
|
|
|
"git.sr.ht/~qbit/mcchunkie/plugins"
|
2020-01-29 16:57:42 -07:00
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
)
|
|
|
|
|
2020-01-29 17:40:15 -07:00
|
|
|
func messageToMe(sn, message string) bool {
|
|
|
|
return strings.Contains(message, sn)
|
|
|
|
}
|
|
|
|
|
2020-01-29 16:57:42 -07:00
|
|
|
func sendMessage(c *gomatrix.Client, roomID, message string) error {
|
|
|
|
_, err := c.UserTyping(roomID, true, 3)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SendText(roomID, message)
|
|
|
|
|
|
|
|
_, err = c.UserTyping(roomID, false, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var store, err = NewStore("mcchunkie.db")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s\n", err)
|
|
|
|
}
|
|
|
|
|
2020-01-31 19:57:40 -07:00
|
|
|
var username, password, userID, accessToken, server string
|
2020-01-29 16:57:42 -07:00
|
|
|
var setup bool
|
|
|
|
|
|
|
|
flag.StringVar(&username, "user", "", "username to connect to matrix server with")
|
|
|
|
flag.StringVar(&server, "server", "", "matrix server")
|
|
|
|
flag.BoolVar(&setup, "s", false, "setup account")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if server == "" {
|
2020-01-29 17:12:41 -07:00
|
|
|
server, err = store.get("config", "server")
|
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-01-29 16:57:42 -07:00
|
|
|
store.set("config", "server", server)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("connecting to %s\n", server)
|
|
|
|
|
|
|
|
cli, err := gomatrix.NewClient(
|
|
|
|
server,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
store.set("account", "username", username)
|
|
|
|
store.set("account", "access_token", resp.AccessToken)
|
|
|
|
store.set("account", "user_id", resp.UserID)
|
|
|
|
} else {
|
2020-01-29 17:12:11 -07:00
|
|
|
username, _ = store.get("account", "username")
|
2020-01-29 16:57:42 -07:00
|
|
|
accessToken, _ = store.get("account", "access_token")
|
|
|
|
userID, _ = store.get("account", "user_id")
|
|
|
|
}
|
|
|
|
|
|
|
|
cli.SetCredentials(userID, accessToken)
|
|
|
|
cli.Store = store
|
|
|
|
syncer := gomatrix.NewDefaultSyncer(username, store)
|
|
|
|
cli.Client = http.DefaultClient
|
|
|
|
cli.Syncer = syncer
|
|
|
|
|
|
|
|
/*
|
2020-01-31 19:57:40 -07:00
|
|
|
// TODO: Add ability to join / part rooms
|
|
|
|
|
2020-01-29 16:57:42 -07:00
|
|
|
if _, err := cli.JoinRoom("!tmCVBJAeuKjCfihUjb:cobryce.com", "", nil); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
if _, err := cli.JoinRoom("!sFPUeGfHqjiItcjNIN:matrix.org", "", nil); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
if _, err := cli.JoinRoom("!ALCZnrYadLGSySIFZr:matrix.org", "", nil); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2020-01-30 06:48:37 -07:00
|
|
|
if _, err := cli.JoinRoom("!LTxJpLHtShMVmlpwmZ:tapenet.org", "", nil); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2020-02-01 20:56:55 -07:00
|
|
|
if _, err := cli.JoinRoom("!TjjamgVanKpNiswkoJ:pintobyte.com", "", nil); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
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-01-31 19:57:40 -07:00
|
|
|
for _, p := range plugins.Plugs {
|
|
|
|
p.Respond(cli, ev, username)
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
avatar := "https://deftly.net/mcchunkie.png"
|
|
|
|
aurl, err := cli.GetAvatarURL()
|
|
|
|
|
|
|
|
if aurl != avatar {
|
|
|
|
log.Printf("Setting avatar to: '%s'", avatar)
|
|
|
|
err = cli.SetAvatarURL(avatar)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to set avatar: ", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("avatar already set")
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
log.Println("syncing..")
|
|
|
|
if err := cli.Sync(); err != nil {
|
|
|
|
fmt.Println("Sync() returned ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|