From 39828b798513f72f0ecf3df3c309ac16047dea7c Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 27 Apr 2020 10:38:00 -0400 Subject: [PATCH] commit code --- .envrc | 1 + .gitignore | 2 ++ cmd/xatci/main.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 12 +++++++ go.sum | 18 +++++++++++ shell.nix | 9 ++++++ 6 files changed, 124 insertions(+) create mode 100644 .envrc create mode 100644 cmd/xatci/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 shell.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..be81fed --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +eval "$(lorri direnv)" \ No newline at end of file diff --git a/.gitignore b/.gitignore index f4d432a..402973f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ +# Credentials +.env diff --git a/cmd/xatci/main.go b/cmd/xatci/main.go new file mode 100644 index 0000000..bd14a77 --- /dev/null +++ b/cmd/xatci/main.go @@ -0,0 +1,82 @@ +package main + +import ( + "flag" + "log" + "runtime/debug" + "time" + + "github.com/facebookarchive/flagenv" + _ "github.com/joho/godotenv/autoload" + "github.com/matrix-org/gomatrix" +) + +var ( + username = flag.String("matrix-username", "xatci", "Matrix bot username") + password = flag.String("matrix-password", "hunter2", "Matrix bot password") + homeserver = flag.String("matrix-homeserver", "", "Matrix homeserver") + botName = flag.String("bot-name", "xatci", "Bot name") +) + +func main() { + flagenv.Parse() + flag.Parse() + + cli, err := gomatrix.NewClient(*homeserver, "", "") + if err != nil { + panic(err) + } + + resp, err := cli.Login(&gomatrix.ReqLogin{ + Type: "m.login.password", + User: *username, + Password: *password, + }) + if err != nil { + panic(err) + } + cli.SetCredentials(resp.UserID, resp.AccessToken) + log.Printf("logged in as %s to %s", resp.UserID, *homeserver) + + x := Xatci{Client: cli} + + cli.Syncer.(*gomatrix.DefaultSyncer).OnEventType("m.room.member", func(event *gomatrix.Event) { + // If I was invited + if event.Content["membership"] == "invite" && *event.StateKey == cli.UserID { + if _, err := cli.JoinRoom(event.RoomID, "", nil); err != nil { + log.Printf("joining %s", event.RoomID) + log.Println("Join error: ", err) + } + } + }) + + cli.Syncer.(*gomatrix.DefaultSyncer).OnEventType("m.room.message", func(event *gomatrix.Event) { + // Tell the room if the callback went south + defer func() { + if r := recover(); r != nil { + log.Printf("%s: %s", r, debug.Stack()) + cli.SendText(event.RoomID, "Argh! I panicked.") + } + }() + // If I did not send this message + if event.Sender != cli.UserID { + if body, ok := event.Body(); ok { + x.onText(event.RoomID, event.Sender, body) + } + } + }) + + for range time.Tick(250 * time.Millisecond) { + if err := cli.Sync(); err != nil { + log.Println("Sync error: ", err) + } + } +} + +type Xatci struct { + *gomatrix.Client +} + +func (x *Xatci) onText(roomID string, userID string, text string) { + log.Printf("%s: <%s> %s", roomID, userID, text) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ebf10a0 --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module tulpa.dev/maxtriski/xatci + +go 1.14 + +require ( + github.com/binaryplease/matrix-bot v0.0.0-20190422182545-e58f16f86ca7 + github.com/dryvenn/mbot v0.0.0-20170114155006-512c4909c81b + github.com/facebookarchive/flagenv v0.0.0-20160425205200-fcd59fca7456 + github.com/joho/godotenv v1.3.0 + github.com/matrix-org/gomatrix v0.0.0-20200408155310-408fff5e6a97 + github.com/sirupsen/logrus v1.5.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..77ac782 --- /dev/null +++ b/go.sum @@ -0,0 +1,18 @@ +github.com/binaryplease/matrix-bot v0.0.0-20190422182545-e58f16f86ca7 h1:DaAe5WJc6OZlwBICe1nXU2qqZZ3Pfo8ueQKjz+UE5ls= +github.com/binaryplease/matrix-bot v0.0.0-20190422182545-e58f16f86ca7/go.mod h1:OoTSJ08SgAIAZFhlnq7/yx3QFUwlZChDCV5LTREdtMc= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dryvenn/mbot v0.0.0-20170114155006-512c4909c81b h1:VnhGv4hK1LPrbqPM90d6u6klr116vQNwfSnDmEagKm4= +github.com/dryvenn/mbot v0.0.0-20170114155006-512c4909c81b/go.mod h1:Rxj/s+zE+HskmZwiHIELeRYlu/Sf1YI8C52QwMaQqqU= +github.com/facebookarchive/flagenv v0.0.0-20160425205200-fcd59fca7456 h1:1NzATV1A9LeTWAuAJGd7vE/wYSO08EJQTrwKcBBR6Cw= +github.com/facebookarchive/flagenv v0.0.0-20160425205200-fcd59fca7456/go.mod h1:Lbo+w7sC1xznzryrqUDoBBT2nLKV9JnDOzdiVMHYX/8= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/matrix-org/gomatrix v0.0.0-20200408155310-408fff5e6a97 h1:2VTKQTleZHUUGJpjAB5opejRYlCfbDqPe7s+WjxQHnQ= +github.com/matrix-org/gomatrix v0.0.0-20200408155310-408fff5e6a97/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q= +github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..7e51531 --- /dev/null +++ b/shell.nix @@ -0,0 +1,9 @@ +let + pkgs = import { }; + nur = import (builtins.fetchTarball + "https://github.com/nix-community/NUR/archive/master.tar.gz") { + inherit pkgs; + }; +in pkgs.mkShell { + buildInputs = with pkgs; with nur.repos.xe; [ go goimports gopls ]; +}