130 lines
2.9 KiB
Go
130 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"maunium.net/go/mautrix"
|
|
)
|
|
|
|
type Config struct {
|
|
Userid string
|
|
Server string
|
|
Token string
|
|
Owner string
|
|
}
|
|
|
|
type Bot struct {
|
|
Client *mautrix.Client
|
|
Rooms map[string]int
|
|
Conf *Config
|
|
}
|
|
|
|
func main() {
|
|
c := &Config{
|
|
Userid: "@testbot:saintnet.tech",
|
|
Server: "matrix.saintnet.tech",
|
|
Token: "MDAxYmxvY2F0aW9uIHNhaW50bmV0LnRlY2gKMDAxM2lkZW50aWZpZXIga2V5CjAwMTBjaWQgZ2VuID0gMQowMDI5Y2lkIHVzZXJfaWQgPSBAdGVzdGJvdDpzYWludG5ldC50ZWNoCjAwMTZjaWQgdHlwZSA9IGFjY2VzcwowMDIxY2lkIG5vbmNlID0gemlKZGs9bmJhcjp1eThIXgowMDJmc2lnbmF0dXJlIE4x0sbUS9lN-fi0KjJmEmpx6_wpYzgvk4k2Eugtkva7Cg",
|
|
Owner: "@stryan:saintnet.tech",
|
|
}
|
|
b := NewBot(c)
|
|
syncer := NewCustomSyncer("@testbot:saintnet.tech", b.Client.Store)
|
|
syncer.OnEventType(mautrix.EventMessage, b.handleMessage)
|
|
syncer.OnEventType(mautrix.StateMember, b.handleMember)
|
|
|
|
b.Client.Syncer = syncer
|
|
go func() {
|
|
err := b.Client.Sync()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}()
|
|
fmt.Println("Syncing enabled")
|
|
resp, err := b.Client.JoinedRooms()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, v := range resp.JoinedRooms {
|
|
//fmt.Printf("Bot is in Room %v\n", v)
|
|
|
|
mem, err := b.Client.JoinedMembers(v)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if len(mem.Joined) > 1 {
|
|
b.Rooms[v] = len(mem.Joined)
|
|
fmt.Printf("Bot is in Room %v\n", v)
|
|
} else {
|
|
_, err := b.Client.LeaveRoom(v)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
_, err = b.Client.ForgetRoom(v)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("Bot was in Room %v, left due to being last one there\n", v)
|
|
}
|
|
|
|
}
|
|
fmt.Println("Begining main loop")
|
|
for {
|
|
time.Sleep(2 * time.Second)
|
|
fmt.Println("Checking")
|
|
}
|
|
|
|
}
|
|
|
|
func NewBot(c *Config) *Bot {
|
|
client, err := mautrix.NewClient(c.Server, c.Userid, c.Token)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
b := &Bot{Client: client, Rooms: make(map[string]int), Conf: c}
|
|
return b
|
|
}
|
|
|
|
func (b *Bot) handleMessage(e *mautrix.Event) {
|
|
if e.Sender == b.Conf.Userid {
|
|
return //we don't care about our own messages
|
|
}
|
|
body := e.Content.Body
|
|
_, err := b.Client.SendText(e.RoomID, body)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func (b *Bot) handleMember(e *mautrix.Event) {
|
|
mbr := e.Content.Membership
|
|
if mbr == mautrix.MembershipInvite {
|
|
if e.Sender == b.Conf.Owner && e.GetStateKey() == b.Conf.Userid {
|
|
fmt.Println("Joining room:", e.RoomID)
|
|
_, err := b.Client.JoinRoom(e.RoomID, "", nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
}
|
|
}
|
|
if mbr == mautrix.MembershipJoin {
|
|
log.Println("A user joined a room we care about")
|
|
b.Rooms[e.RoomID] = b.Rooms[e.RoomID] + 1
|
|
}
|
|
if mbr == mautrix.MembershipLeave && e.Sender != b.Conf.Userid {
|
|
b.Rooms[e.RoomID] = b.Rooms[e.RoomID] - 1
|
|
if b.Rooms[e.RoomID] < 2 {
|
|
log.Println("Last in room, leaving")
|
|
_, err := b.Client.LeaveRoom(e.RoomID)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
_, err = b.Client.ForgetRoom(e.RoomID)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
}
|