commit code

This commit is contained in:
Cadey Ratio 2020-04-27 10:38:00 -04:00
parent e144663dbe
commit 39828b7985
6 changed files with 124 additions and 0 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
eval "$(lorri direnv)"

2
.gitignore vendored
View File

@ -15,3 +15,5 @@
# Dependency directories (remove the comment below to include it)
# vendor/
# Credentials
.env

82
cmd/xatci/main.go Normal file
View File

@ -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)
}

12
go.mod Normal file
View File

@ -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
)

18
go.sum Normal file
View File

@ -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=

9
shell.nix Normal file
View File

@ -0,0 +1,9 @@
let
pkgs = import <nixpkgs> { };
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 ];
}