add plugins
This commit is contained in:
parent
7ad74ad4b2
commit
97956445da
23
main.go
23
main.go
@ -5,10 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.sr.ht/~qbit/mcchunkie/plugins"
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -37,9 +37,8 @@ func main() {
|
|||||||
log.Fatalf("%s\n", err)
|
log.Fatalf("%s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var username, shortName, password, userID, accessToken, server string
|
var username, password, userID, accessToken, server string
|
||||||
var setup bool
|
var setup bool
|
||||||
var nameRE = regexp.MustCompile(`@(.+):.+$`)
|
|
||||||
|
|
||||||
flag.StringVar(&username, "user", "", "username to connect to matrix server with")
|
flag.StringVar(&username, "user", "", "username to connect to matrix server with")
|
||||||
flag.StringVar(&server, "server", "", "matrix server")
|
flag.StringVar(&server, "server", "", "matrix server")
|
||||||
@ -87,7 +86,6 @@ func main() {
|
|||||||
store.set("account", "user_id", resp.UserID)
|
store.set("account", "user_id", resp.UserID)
|
||||||
} else {
|
} else {
|
||||||
username, _ = store.get("account", "username")
|
username, _ = store.get("account", "username")
|
||||||
shortName = nameRE.ReplaceAllString(username, "$1")
|
|
||||||
accessToken, _ = store.get("account", "access_token")
|
accessToken, _ = store.get("account", "access_token")
|
||||||
userID, _ = store.get("account", "user_id")
|
userID, _ = store.get("account", "user_id")
|
||||||
}
|
}
|
||||||
@ -99,6 +97,8 @@ func main() {
|
|||||||
cli.Syncer = syncer
|
cli.Syncer = syncer
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
// TODO: Add ability to join / part rooms
|
||||||
|
|
||||||
if _, err := cli.JoinRoom("!tmCVBJAeuKjCfihUjb:cobryce.com", "", nil); err != nil {
|
if _, err := cli.JoinRoom("!tmCVBJAeuKjCfihUjb:cobryce.com", "", nil); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
@ -118,22 +118,11 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if mtype, ok := ev.MessageType(); ok {
|
for _, p := range plugins.Plugs {
|
||||||
switch mtype {
|
p.Respond(cli, ev, username)
|
||||||
case "m.text":
|
|
||||||
if post, ok := ev.Body(); ok {
|
|
||||||
if messageToMe(shortName, post) {
|
|
||||||
sendMessage(cli, "!LTxJpLHtShMVmlpwmZ:tapenet.org", post)
|
|
||||||
}
|
|
||||||
log.Printf("%s: '%s'", ev.Sender, post)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
sendMessage(cli, "!LTxJpLHtShMVmlpwmZ:tapenet.org", "Typing hi!")
|
|
||||||
//sendMessage(cli, "!tmCVBJAeuKjCfihUjb:cobryce.com", "Butts")
|
|
||||||
|
|
||||||
avatar := "https://deftly.net/mcchunkie.png"
|
avatar := "https://deftly.net/mcchunkie.png"
|
||||||
aurl, err := cli.GetAvatarURL()
|
aurl, err := cli.GetAvatarURL()
|
||||||
|
|
||||||
|
47
plugins/plugins.go
Normal file
47
plugins/plugins.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package plugins
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/matrix-org/gomatrix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Plugin is an interface that specifies what a plugin needs to respond to.
|
||||||
|
type Plugin interface {
|
||||||
|
Respond(c *gomatrix.Client, ev *gomatrix.Event, user string)
|
||||||
|
Name() string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NameRE matches just the name of a matrix user
|
||||||
|
var NameRE = regexp.MustCompile(`@(.+):.+$`)
|
||||||
|
|
||||||
|
// ToMe returns true of the message pertains to the bot
|
||||||
|
func ToMe(user, message string) bool {
|
||||||
|
return strings.Contains(message, user)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendMessage sends a message to a given room
|
||||||
|
func SendMessage(c *gomatrix.Client, roomID, message string) error {
|
||||||
|
_, err := c.UserTyping(roomID, true, 3)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.SendText(roomID, message)
|
||||||
|
|
||||||
|
_, err = c.UserTyping(roomID, false, 0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Plugins area collection
|
||||||
|
type Plugins []Plugin
|
||||||
|
|
||||||
|
// Plugs are all of our plugins
|
||||||
|
var Plugs = Plugins{
|
||||||
|
&HighFive{},
|
||||||
|
&Hi{},
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user