From 379d902df49d25aac275cdac67d2ee6c86f2dee9 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Tue, 19 Dec 2023 16:13:25 -0700 Subject: [PATCH] ffi: expose init_config function Signed-off-by: Sumner Evans --- imessage/ffi/events.go | 4 ---- imessage/ffi/global.go | 34 ---------------------------------- imessage/ffi/main.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/imessage/ffi/events.go b/imessage/ffi/events.go index 5146740..378defe 100644 --- a/imessage/ffi/events.go +++ b/imessage/ffi/events.go @@ -824,10 +824,6 @@ func handleDelivered(status *direct.MessageDelivered) { } } -type ReqStarted struct { - LoggedIn bool `json:"logged_in"` -} - func handleEvent(evt any) { switch typedEvt := evt.(type) { case *imessage.Message: diff --git a/imessage/ffi/global.go b/imessage/ffi/global.go index 89bea89..2fc6b1d 100644 --- a/imessage/ffi/global.go +++ b/imessage/ffi/global.go @@ -20,11 +20,7 @@ import ( "context" _ "embed" "encoding/gob" - "encoding/json" "errors" - "fmt" - "os" - "path/filepath" "reflect" "strings" "sync/atomic" @@ -139,36 +135,6 @@ func init() { // Beeper Mini is back to using Mac identifiers ids.PreferiPhoneVersions = false - - must(0, json.Unmarshal(must(os.ReadFile("config.json")), &global.Cfg)) - - global.NAC = &nacserv.Client{ - URL: global.Cfg.NACServURL, - Token: global.Cfg.NACServToken, - IsRelay: global.Cfg.NACServIsRelay, - - BeeperToken: global.Cfg.IMAToken, - } - - global.IM = direct.NewConnector(global.NAC, handleEvent, nil, nil, global.Cfg.EnablePairECSending, nil, manualLookupRatelimiter) - global.IM.LoginTestConfig = global.Cfg.LoginTest - - global.SecondaryIM = direct.NewConnector(global.NAC, handleSecondaryEvent, nil, nil, false, nil, manualLookupRatelimiter) - if global.Cfg.AttachmentDir == "" { - global.Cfg.AttachmentDir = "attachments" - } - if global.Cfg.DeviceName != "" { - ids.DeviceName = global.Cfg.DeviceName - } - var err error - global.Cfg.AttachmentDir, err = filepath.Abs(global.Cfg.AttachmentDir) - if err != nil { - panic(fmt.Errorf("failed to get absolute path of attachment directory: %w", err)) - } - err = os.MkdirAll(global.Cfg.AttachmentDir, 0700) - if err != nil { - panic(fmt.Errorf("failed to create attachment directory: %w", err)) - } } func (imc *IMContext) GetMatrixReply(ctx context.Context, msg *imessage.Message) (threadRoot, replyFallback id.EventID) { diff --git a/imessage/ffi/main.go b/imessage/ffi/main.go index 1abcb33..d39f022 100644 --- a/imessage/ffi/main.go +++ b/imessage/ffi/main.go @@ -20,9 +20,12 @@ import "C" import ( "context" _ "embed" + "encoding/json" + "fmt" "io" "os" "os/signal" + "path/filepath" "runtime" "slices" "strings" @@ -30,6 +33,7 @@ import ( "sync/atomic" "syscall" "time" + "unsafe" "github.com/rs/zerolog" deflog "github.com/rs/zerolog/log" @@ -40,7 +44,9 @@ import ( "github.com/beeper/imessage/analytics" "github.com/beeper/imessage/database" + "github.com/beeper/imessage/imessage/direct" "github.com/beeper/imessage/imessage/direct/ids" + "github.com/beeper/imessage/imessage/direct/nacserv" "github.com/beeper/imessage/imessage/direct/util/uri" "github.com/beeper/imessage/ipc" "github.com/beeper/imessage/msgconv" @@ -356,3 +362,38 @@ type ReqStarted struct { PendingNACURL bool `json:"pending_nac_url"` } + +// init_config is called over FFI to initialize the bridge configuration. +// +//export init_config +func init_config(data *C.char, n C.int) { + must(0, json.Unmarshal(C.GoBytes(unsafe.Pointer(data), n), &global.Cfg)) + + global.NAC = &nacserv.Client{ + URL: global.Cfg.NACServURL, + Token: global.Cfg.NACServToken, + IsRelay: global.Cfg.NACServIsRelay, + + BeeperToken: global.Cfg.IMAToken, + } + + global.IM = direct.NewConnector(global.NAC, handleEvent, nil, nil, global.Cfg.EnablePairECSending, nil, manualLookupRatelimiter) + global.IM.LoginTestConfig = global.Cfg.LoginTest + + global.SecondaryIM = direct.NewConnector(global.NAC, handleSecondaryEvent, nil, nil, false, nil, manualLookupRatelimiter) + if global.Cfg.AttachmentDir == "" { + global.Cfg.AttachmentDir = "attachments" + } + if global.Cfg.DeviceName != "" { + ids.DeviceName = global.Cfg.DeviceName + } + var err error + global.Cfg.AttachmentDir, err = filepath.Abs(global.Cfg.AttachmentDir) + if err != nil { + panic(fmt.Errorf("failed to get absolute path of attachment directory: %w", err)) + } + err = os.MkdirAll(global.Cfg.AttachmentDir, 0700) + if err != nil { + panic(fmt.Errorf("failed to create attachment directory: %w", err)) + } +}