mirror of https://github.com/mautrix/go.git
Allow sending messages in example
parent
f14b046a85
commit
fcdec23ab8
|
@ -177,6 +177,16 @@ func (helper *CryptoHelper) Init() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (helper *CryptoHelper) Close() error {
|
||||
if helper.dbForManagedStores != nil {
|
||||
err := helper.dbForManagedStores.RawDB.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (helper *CryptoHelper) verifyDeviceKeysOnServer() error {
|
||||
helper.log.Debugfln("Making sure our device has the expected keys on the server")
|
||||
resp, err := helper.client.QueryKeys(&mautrix.ReqQueryKeys{
|
||||
|
|
|
@ -8,6 +8,7 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
github.com/chzyer/readline v1.5.1 // indirect
|
||||
github.com/gorilla/mux v1.8.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
|
||||
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
|
||||
github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=
|
||||
github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk=
|
||||
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
|
||||
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
|
@ -33,6 +37,7 @@ golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
|
|||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
|
|
|
@ -9,15 +9,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/chzyer/readline"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/crypto/cryptohelper"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
var homeserver = flag.String("homeserver", "", "Matrix homeserver")
|
||||
|
@ -39,10 +44,20 @@ func main() {
|
|||
}
|
||||
//client.Logger = maulogger.DefaultLogger
|
||||
//maulogger.DefaultLogger.PrintLevel = 0
|
||||
rl, err := readline.New("[no room]> ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer rl.Close()
|
||||
stdout := rl.Stdout()
|
||||
|
||||
var lastRoomID id.RoomID
|
||||
|
||||
syncer := client.Syncer.(*mautrix.DefaultSyncer)
|
||||
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
||||
fmt.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
|
||||
lastRoomID = evt.RoomID
|
||||
rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID))
|
||||
_, _ = fmt.Fprintf(stdout, "<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
|
||||
})
|
||||
|
||||
cryptoHelper, err := cryptohelper.NewCryptoHelper(client, []byte("meow"), *database)
|
||||
|
@ -54,11 +69,11 @@ func main() {
|
|||
//client.UserID = "..."
|
||||
//client.DeviceID = "..."
|
||||
//client.AccessToken = "..."
|
||||
// You don't need to set a device ID in LoginAs because the crypto helper will set it for you if necessary.
|
||||
cryptoHelper.LoginAs = &mautrix.ReqLogin{
|
||||
Type: mautrix.AuthTypePassword,
|
||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: *username},
|
||||
Password: *password,
|
||||
StoreCredentials: true,
|
||||
Type: mautrix.AuthTypePassword,
|
||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: *username},
|
||||
Password: *password,
|
||||
}
|
||||
// If you want to use multiple clients with the same DB, you should set a distinct database account ID for each one.
|
||||
//cryptoHelper.DBAccountID = ""
|
||||
|
@ -66,10 +81,42 @@ func main() {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Set the client crypto helper in order to automatically encrypt outgoing messages
|
||||
client.Crypto = cryptoHelper
|
||||
|
||||
fmt.Println("Now running")
|
||||
err = client.Sync()
|
||||
_, _ = fmt.Fprintln(stdout, "Now running")
|
||||
syncCtx, cancelSync := context.WithCancel(context.Background())
|
||||
var syncStopWait sync.WaitGroup
|
||||
syncStopWait.Add(1)
|
||||
|
||||
go func() {
|
||||
err = client.SyncWithContext(syncCtx)
|
||||
defer syncStopWait.Done()
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
line, err := rl.Readline()
|
||||
if err != nil { // io.EOF
|
||||
break
|
||||
}
|
||||
if lastRoomID == "" {
|
||||
_, _ = fmt.Fprintln(stdout, "Wait for an incoming message before sending messages")
|
||||
continue
|
||||
}
|
||||
resp, err := client.SendText(lastRoomID, line)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintln(stdout, "Failed to send:", err)
|
||||
} else {
|
||||
_, _ = fmt.Fprintln(stdout, "Sent", resp.EventID)
|
||||
}
|
||||
}
|
||||
cancelSync()
|
||||
syncStopWait.Wait()
|
||||
err = cryptoHelper.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
_, _ = fmt.Fprintln(stdout, "Error closing db:", err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue