gmessages/gmtest/main.go

141 lines
3 KiB
Go
Raw Normal View History

2023-06-30 13:27:50 +00:00
package main
import (
"bufio"
2023-07-01 09:51:22 +00:00
"encoding/json"
2023-06-30 13:27:50 +00:00
"errors"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/mdp/qrterminal/v3"
"github.com/rs/zerolog"
"go.mau.fi/mautrix-gmessages/libgm"
"go.mau.fi/mautrix-gmessages/libgm/events"
2023-07-17 13:51:31 +00:00
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
2023-06-30 13:27:50 +00:00
)
func must(err error) {
if err != nil {
panic(err)
}
}
func mustReturn[T any](val T, err error) T {
must(err)
return val
}
var cli *libgm.Client
var log zerolog.Logger
var sess libgm.AuthData
2023-06-30 13:27:50 +00:00
func main() {
log = zerolog.New(zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
w.Out = os.Stdout
w.TimeFormat = time.Stamp
})).With().Timestamp().Logger()
2023-07-01 09:51:22 +00:00
file, err := os.Open("session.json")
2023-07-16 12:55:30 +00:00
var doLogin bool
2023-06-30 13:27:50 +00:00
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
panic(err)
}
2023-07-16 11:36:13 +00:00
sess = *libgm.NewAuthData()
2023-07-16 12:55:30 +00:00
doLogin = true
2023-06-30 13:27:50 +00:00
} else {
2023-07-01 09:51:22 +00:00
must(json.NewDecoder(file).Decode(&sess))
2023-06-30 13:27:50 +00:00
log.Info().Msg("Loaded session?")
}
_ = file.Close()
cli = libgm.NewClient(&sess, log)
2023-06-30 13:27:50 +00:00
cli.SetEventHandler(evtHandler)
2023-07-16 12:55:30 +00:00
if doLogin {
qr := mustReturn(cli.StartLogin())
qrterminal.GenerateHalfBlock(qr, qrterminal.L, os.Stdout)
go func() {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for range ticker.C {
if sess.Browser != nil {
return
}
qr := mustReturn(cli.RefreshPhoneRelay())
qrterminal.GenerateHalfBlock(qr, qrterminal.L, os.Stdout)
}
}()
} else {
must(cli.Connect())
}
2023-06-30 13:27:50 +00:00
c := make(chan os.Signal)
input := make(chan string)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
defer close(input)
scan := bufio.NewScanner(os.Stdin)
for scan.Scan() {
line := strings.TrimSpace(scan.Text())
if len(line) > 0 {
input <- line
}
}
}()
defer saveSession()
for {
select {
case <-c:
log.Info().Msg("Interrupt received, exiting")
return
case cmd := <-input:
if len(cmd) == 0 {
log.Info().Msg("Stdin closed, exiting")
return
}
args := strings.Fields(cmd)
cmd = args[0]
args = args[1:]
switch cmd {
//case "getavatar":
// _, err := cli.GetFullSizeImage(args)
// fmt.Println(err)
case "listcontacts":
cli.ListContacts()
case "topcontacts":
cli.ListTopContacts()
case "getorcreate":
cli.GetOrCreateConversation(args)
}
2023-06-30 13:27:50 +00:00
//go handleCmd(strings.ToLower(cmd), args)
}
}
}
func saveSession() {
2023-07-01 09:51:22 +00:00
file := mustReturn(os.OpenFile("session.json", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600))
must(json.NewEncoder(file).Encode(sess))
2023-06-30 13:27:50 +00:00
_ = file.Close()
}
func evtHandler(rawEvt any) {
switch evt := rawEvt.(type) {
case *events.ClientReady:
2023-07-01 09:51:22 +00:00
log.Debug().Any("data", evt).Msg("Client is ready!")
2023-06-30 13:27:50 +00:00
case *events.PairSuccessful:
log.Debug().Any("data", evt).Msg("Pair successful")
saveSession()
log.Debug().Msg("Wrote session")
2023-07-17 13:51:31 +00:00
case *gmproto.Message:
2023-06-30 13:27:50 +00:00
log.Debug().Any("data", evt).Msg("Message event")
2023-07-17 13:51:31 +00:00
case *gmproto.Conversation:
2023-06-30 13:27:50 +00:00
log.Debug().Any("data", evt).Msg("Conversation event")
case *events.BrowserActive:
log.Debug().Any("data", evt).Msg("Browser active")
default:
log.Debug().Any("data", evt).Msg("Unknown event")
}
}