2020-04-11 14:39:19 -04:00
|
|
|
package botlib
|
2020-04-05 18:50:32 -04:00
|
|
|
|
|
|
|
import (
|
2020-04-11 14:39:19 -04:00
|
|
|
"fmt"
|
2020-04-05 18:50:32 -04:00
|
|
|
"log"
|
2020-04-09 18:42:16 -04:00
|
|
|
"os"
|
2020-04-05 18:50:32 -04:00
|
|
|
|
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Userid string
|
|
|
|
Server string
|
|
|
|
Token string
|
2020-04-09 18:42:16 -04:00
|
|
|
Name string
|
2020-04-05 18:50:32 -04:00
|
|
|
Owner string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Bot struct {
|
2020-04-09 18:42:16 -04:00
|
|
|
Client *mautrix.Client
|
|
|
|
Rooms map[string]int
|
|
|
|
Conf *Config
|
|
|
|
ManagementRoomID string
|
2020-04-05 18:50:32 -04:00
|
|
|
}
|
|
|
|
|
2020-04-11 14:39:19 -04:00
|
|
|
func NewBot(c *Config) *Bot {
|
|
|
|
client, err := mautrix.NewClient(c.Server, c.Userid, c.Token)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2020-04-05 18:50:32 -04:00
|
|
|
}
|
2020-04-11 14:39:19 -04:00
|
|
|
b := &Bot{Client: client, Rooms: make(map[string]int), Conf: c, ManagementRoomID: "!fxioaRvcDbKKhCpHrZ:saintnet.tech"}
|
|
|
|
return b
|
|
|
|
}
|
2020-04-05 18:50:32 -04:00
|
|
|
|
2020-04-11 14:39:19 -04:00
|
|
|
func (b *Bot) LeaveEmptyRooms() error {
|
2020-04-05 18:50:32 -04:00
|
|
|
resp, err := b.Client.JoinedRooms()
|
|
|
|
if err != nil {
|
2020-04-11 14:39:19 -04:00
|
|
|
return err
|
2020-04-05 18:50:32 -04:00
|
|
|
}
|
|
|
|
for _, v := range resp.JoinedRooms {
|
|
|
|
|
|
|
|
mem, err := b.Client.JoinedMembers(v)
|
|
|
|
if err != nil {
|
2020-04-11 14:39:19 -04:00
|
|
|
return err
|
2020-04-05 18:50:32 -04:00
|
|
|
}
|
|
|
|
if len(mem.Joined) > 1 {
|
|
|
|
b.Rooms[v] = len(mem.Joined)
|
2020-04-09 18:42:16 -04:00
|
|
|
log.Printf("%v is in Room %v\n", b.Conf.Name, v)
|
2020-04-05 18:50:32 -04:00
|
|
|
} else {
|
|
|
|
_, err := b.Client.LeaveRoom(v)
|
|
|
|
if err != nil {
|
2020-04-11 14:39:19 -04:00
|
|
|
return err
|
2020-04-05 18:50:32 -04:00
|
|
|
}
|
|
|
|
_, err = b.Client.ForgetRoom(v)
|
|
|
|
if err != nil {
|
2020-04-11 14:39:19 -04:00
|
|
|
return err
|
2020-04-05 18:50:32 -04:00
|
|
|
}
|
2020-04-09 18:42:16 -04:00
|
|
|
log.Printf("%v was in Room %v, left due to being last one there\n", b.Conf.Name, v)
|
2020-04-05 18:50:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-11 14:39:19 -04:00
|
|
|
return nil
|
2020-04-05 18:50:32 -04:00
|
|
|
}
|
|
|
|
|
2020-04-09 18:42:16 -04:00
|
|
|
func (b *Bot) createManagementRoom() error {
|
|
|
|
if b.ManagementRoomID != "" {
|
|
|
|
log.Printf("%v already has management room %v\n", b.Conf.Name, b.ManagementRoomID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Printf("Creating Management Room for %v\n", b.Conf.Name)
|
|
|
|
respCreateRoom, err := b.Client.CreateRoom(&mautrix.ReqCreateRoom{
|
|
|
|
Preset: "trusted_private_chat",
|
|
|
|
Visibility: "private",
|
2020-04-11 14:39:19 -04:00
|
|
|
Name: fmt.Sprintf("%v Control Room", b.Conf.Name),
|
2020-04-09 18:42:16 -04:00
|
|
|
Invite: []string{b.Conf.Owner},
|
|
|
|
IsDirect: true,
|
|
|
|
})
|
|
|
|
b.ManagementRoomID = respCreateRoom.RoomID
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
log.Printf("%v created management room %v\n", b.Conf.Name, respCreateRoom.RoomID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 18:50:32 -04:00
|
|
|
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
|
2020-04-09 18:42:16 -04:00
|
|
|
if body == "stop" {
|
|
|
|
b.Client.SendText(e.RoomID, "Shutting down...")
|
|
|
|
b.Client.StopSync()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2020-04-05 18:50:32 -04:00
|
|
|
_, 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 {
|
2020-04-09 18:42:16 -04:00
|
|
|
log.Printf("%v joining room: %v\n", b.Conf.Name, e.RoomID)
|
2020-04-05 18:50:32 -04:00
|
|
|
_, err := b.Client.JoinRoom(e.RoomID, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mbr == mautrix.MembershipJoin {
|
2020-04-09 18:42:16 -04:00
|
|
|
log.Printf("A user joined a room %v cares about\n", b.Conf.Name)
|
2020-04-05 18:50:32 -04:00
|
|
|
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 {
|
2020-04-09 18:42:16 -04:00
|
|
|
log.Printf("%v is last in a room, leaving.\n", b.Conf.Name)
|
2020-04-05 18:50:32 -04:00
|
|
|
_, err := b.Client.LeaveRoom(e.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = b.Client.ForgetRoom(e.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|