start work
This commit is contained in:
parent
9f558ac277
commit
65444071c0
@ -1,9 +1,9 @@
|
||||
package main
|
||||
package botlib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"maunium.net/go/mautrix"
|
||||
)
|
||||
@ -23,64 +23,6 @@ type Bot struct {
|
||||
ManagementRoomID string
|
||||
}
|
||||
|
||||
func main() {
|
||||
c := &Config{
|
||||
Userid: "@testbot:saintnet.tech",
|
||||
Server: "matrix.saintnet.tech",
|
||||
Token: "MDAxYmxvY2F0aW9uIHNhaW50bmV0LnRlY2gKMDAxM2lkZW50aWZpZXIga2V5CjAwMTBjaWQgZ2VuID0gMQowMDI5Y2lkIHVzZXJfaWQgPSBAdGVzdGJvdDpzYWludG5ldC50ZWNoCjAwMTZjaWQgdHlwZSA9IGFjY2VzcwowMDIxY2lkIG5vbmNlID0gemlKZGs9bmJhcjp1eThIXgowMDJmc2lnbmF0dXJlIE4x0sbUS9lN-fi0KjJmEmpx6_wpYzgvk4k2Eugtkva7Cg",
|
||||
Name: "TestBot",
|
||||
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)
|
||||
}
|
||||
}()
|
||||
log.Println("Syncing enabled")
|
||||
resp, err := b.Client.JoinedRooms()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for _, v := range resp.JoinedRooms {
|
||||
|
||||
mem, err := b.Client.JoinedMembers(v)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if len(mem.Joined) > 1 {
|
||||
b.Rooms[v] = len(mem.Joined)
|
||||
log.Printf("%v is in Room %v\n", b.Conf.Name, v)
|
||||
} else {
|
||||
_, err := b.Client.LeaveRoom(v)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
_, err = b.Client.ForgetRoom(v)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("%v was in Room %v, left due to being last one there\n", b.Conf.Name, v)
|
||||
}
|
||||
|
||||
}
|
||||
err = b.createManagementRoom()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Begining main loop")
|
||||
for {
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func NewBot(c *Config) *Bot {
|
||||
client, err := mautrix.NewClient(c.Server, c.Userid, c.Token)
|
||||
if err != nil {
|
||||
@ -90,6 +32,36 @@ func NewBot(c *Config) *Bot {
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Bot) LeaveEmptyRooms() error {
|
||||
resp, err := b.Client.JoinedRooms()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range resp.JoinedRooms {
|
||||
|
||||
mem, err := b.Client.JoinedMembers(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(mem.Joined) > 1 {
|
||||
b.Rooms[v] = len(mem.Joined)
|
||||
log.Printf("%v is in Room %v\n", b.Conf.Name, v)
|
||||
} else {
|
||||
_, err := b.Client.LeaveRoom(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = b.Client.ForgetRoom(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("%v was in Room %v, left due to being last one there\n", b.Conf.Name, v)
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bot) createManagementRoom() error {
|
||||
if b.ManagementRoomID != "" {
|
||||
log.Printf("%v already has management room %v\n", b.Conf.Name, b.ManagementRoomID)
|
||||
@ -99,7 +71,7 @@ func (b *Bot) createManagementRoom() error {
|
||||
respCreateRoom, err := b.Client.CreateRoom(&mautrix.ReqCreateRoom{
|
||||
Preset: "trusted_private_chat",
|
||||
Visibility: "private",
|
||||
Name: "TestBot Control Room",
|
||||
Name: fmt.Sprintf("%v Control Room", b.Conf.Name),
|
||||
Invite: []string{b.Conf.Owner},
|
||||
IsDirect: true,
|
||||
})
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package botlib
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
5
botlib/go.mod
Normal file
5
botlib/go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module botlib
|
||||
|
||||
go 1.14
|
||||
|
||||
require maunium.net/go/mautrix v0.1.0-beta.2
|
8
botlib/go.sum
Normal file
8
botlib/go.sum
Normal file
@ -0,0 +1,8 @@
|
||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
maunium.net/go/mautrix v0.1.0-beta.2 h1:RxYTqTzW6iXu83gf8ucqGwYx8JLa+a17LWjiPkVV/fU=
|
||||
maunium.net/go/mautrix v0.1.0-beta.2/go.mod h1:YFMU9DBeXH7cqx7sJLg0DkVxwNPbih8QbpUTYf/IjMM=
|
62
botlib/message.go
Normal file
62
botlib/message.go
Normal file
@ -0,0 +1,62 @@
|
||||
package botlib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type MessageType int
|
||||
|
||||
const (
|
||||
MessageRequest = 1
|
||||
MessageResponse = 2
|
||||
)
|
||||
|
||||
type InputPair struct {
|
||||
Param string `json:"param"`
|
||||
Argument string `json:"argument"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Type MessageType `json:"type"`
|
||||
Inputs []InputPair `json:"inputs"`
|
||||
Sender string `json:"sender"`
|
||||
Receiver string `json:"receiver"`
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
func Knock(name string) error {
|
||||
path, exists := Registry[name]
|
||||
if !exists {
|
||||
return errors.New("Unknown knock target")
|
||||
}
|
||||
/*knock_msg := Message{
|
||||
Type: MessageRequest,
|
||||
Sender: "One-Who-Knocks",
|
||||
Receiver: name,
|
||||
}*/
|
||||
if path.Hostname() == "localhost" || path.Hostname() == "127.0.0.1" {
|
||||
//Local delivery
|
||||
if path.EscapedPath() == "" {
|
||||
if path.Port() == "" {
|
||||
//no path means it's not a binary but no port means it's not listening
|
||||
//invalid, can't knock
|
||||
return errors.New("Can't knock, invalid URL")
|
||||
} else {
|
||||
//no path but port, local network connection
|
||||
return errors.New("Unsupposed: Local network") //TODO
|
||||
}
|
||||
} else {
|
||||
//path on localhost means local bin file
|
||||
binpath := path.EscapedPath()
|
||||
cmd := exec.Command(binpath)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return errors.New("Network messaging not supported yet")
|
||||
}
|
||||
return nil
|
||||
}
|
11
botlib/registry.go
Normal file
11
botlib/registry.go
Normal file
@ -0,0 +1,11 @@
|
||||
package botlib
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
var Registry map[string]url.URL
|
||||
|
||||
func InitRegistry() {
|
||||
Registry = make(map[string]url.URL)
|
||||
}
|
5
testbot/go.mod
Normal file
5
testbot/go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module testbot
|
||||
|
||||
go 1.14
|
||||
|
||||
require maunium.net/go/mautrix v0.1.0-beta.2
|
8
testbot/go.sum
Normal file
8
testbot/go.sum
Normal file
@ -0,0 +1,8 @@
|
||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
maunium.net/go/mautrix v0.1.0-beta.2 h1:RxYTqTzW6iXu83gf8ucqGwYx8JLa+a17LWjiPkVV/fU=
|
||||
maunium.net/go/mautrix v0.1.0-beta.2/go.mod h1:YFMU9DBeXH7cqx7sJLg0DkVxwNPbih8QbpUTYf/IjMM=
|
45
testbot/main.go
Normal file
45
testbot/main.go
Normal file
@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.saintnet.tech/botlib"
|
||||
"maunium.net/go/mautrix"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := &botlib.Config{
|
||||
Userid: "@testbot:saintnet.tech",
|
||||
Server: "matrix.saintnet.tech",
|
||||
Token: "MDAxYmxvY2F0aW9uIHNhaW50bmV0LnRlY2gKMDAxM2lkZW50aWZpZXIga2V5CjAwMTBjaWQgZ2VuID0gMQowMDI5Y2lkIHVzZXJfaWQgPSBAdGVzdGJvdDpzYWludG5ldC50ZWNoCjAwMTZjaWQgdHlwZSA9IGFjY2VzcwowMDIxY2lkIG5vbmNlID0gemlKZGs9bmJhcjp1eThIXgowMDJmc2lnbmF0dXJlIE4x0sbUS9lN-fi0KjJmEmpx6_wpYzgvk4k2Eugtkva7Cg",
|
||||
Name: "TestBot",
|
||||
Owner: "@stryan:saintnet.tech",
|
||||
}
|
||||
b := botlib.NewBot(c)
|
||||
syncer := botlib.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)
|
||||
}
|
||||
}()
|
||||
log.Println("Syncing enabled")
|
||||
err := b.LeaveEmptyRooms()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = b.createManagementRoom()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Begining main loop")
|
||||
for {
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user