Skip to content
This repository was archived by the owner on Apr 19, 2025. It is now read-only.

Commit 379d902

Browse files
committed
ffi: expose init_config function
Signed-off-by: Sumner Evans <sumner@beeper.com>
1 parent 688129a commit 379d902

3 files changed

Lines changed: 41 additions & 38 deletions

File tree

imessage/ffi/events.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,10 +824,6 @@ func handleDelivered(status *direct.MessageDelivered) {
824824
}
825825
}
826826

827-
type ReqStarted struct {
828-
LoggedIn bool `json:"logged_in"`
829-
}
830-
831827
func handleEvent(evt any) {
832828
switch typedEvt := evt.(type) {
833829
case *imessage.Message:

imessage/ffi/global.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ import (
2020
"context"
2121
_ "embed"
2222
"encoding/gob"
23-
"encoding/json"
2423
"errors"
25-
"fmt"
26-
"os"
27-
"path/filepath"
2824
"reflect"
2925
"strings"
3026
"sync/atomic"
@@ -139,36 +135,6 @@ func init() {
139135

140136
// Beeper Mini is back to using Mac identifiers
141137
ids.PreferiPhoneVersions = false
142-
143-
must(0, json.Unmarshal(must(os.ReadFile("config.json")), &global.Cfg))
144-
145-
global.NAC = &nacserv.Client{
146-
URL: global.Cfg.NACServURL,
147-
Token: global.Cfg.NACServToken,
148-
IsRelay: global.Cfg.NACServIsRelay,
149-
150-
BeeperToken: global.Cfg.IMAToken,
151-
}
152-
153-
global.IM = direct.NewConnector(global.NAC, handleEvent, nil, nil, global.Cfg.EnablePairECSending, nil, manualLookupRatelimiter)
154-
global.IM.LoginTestConfig = global.Cfg.LoginTest
155-
156-
global.SecondaryIM = direct.NewConnector(global.NAC, handleSecondaryEvent, nil, nil, false, nil, manualLookupRatelimiter)
157-
if global.Cfg.AttachmentDir == "" {
158-
global.Cfg.AttachmentDir = "attachments"
159-
}
160-
if global.Cfg.DeviceName != "" {
161-
ids.DeviceName = global.Cfg.DeviceName
162-
}
163-
var err error
164-
global.Cfg.AttachmentDir, err = filepath.Abs(global.Cfg.AttachmentDir)
165-
if err != nil {
166-
panic(fmt.Errorf("failed to get absolute path of attachment directory: %w", err))
167-
}
168-
err = os.MkdirAll(global.Cfg.AttachmentDir, 0700)
169-
if err != nil {
170-
panic(fmt.Errorf("failed to create attachment directory: %w", err))
171-
}
172138
}
173139

174140
func (imc *IMContext) GetMatrixReply(ctx context.Context, msg *imessage.Message) (threadRoot, replyFallback id.EventID) {

imessage/ffi/main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@ import "C"
2020
import (
2121
"context"
2222
_ "embed"
23+
"encoding/json"
24+
"fmt"
2325
"io"
2426
"os"
2527
"os/signal"
28+
"path/filepath"
2629
"runtime"
2730
"slices"
2831
"strings"
2932
"sync"
3033
"sync/atomic"
3134
"syscall"
3235
"time"
36+
"unsafe"
3337

3438
"github.com/rs/zerolog"
3539
deflog "github.com/rs/zerolog/log"
@@ -40,7 +44,9 @@ import (
4044

4145
"github.com/beeper/imessage/analytics"
4246
"github.com/beeper/imessage/database"
47+
"github.com/beeper/imessage/imessage/direct"
4348
"github.com/beeper/imessage/imessage/direct/ids"
49+
"github.com/beeper/imessage/imessage/direct/nacserv"
4450
"github.com/beeper/imessage/imessage/direct/util/uri"
4551
"github.com/beeper/imessage/ipc"
4652
"github.com/beeper/imessage/msgconv"
@@ -356,3 +362,38 @@ type ReqStarted struct {
356362

357363
PendingNACURL bool `json:"pending_nac_url"`
358364
}
365+
366+
// init_config is called over FFI to initialize the bridge configuration.
367+
//
368+
//export init_config
369+
func init_config(data *C.char, n C.int) {
370+
must(0, json.Unmarshal(C.GoBytes(unsafe.Pointer(data), n), &global.Cfg))
371+
372+
global.NAC = &nacserv.Client{
373+
URL: global.Cfg.NACServURL,
374+
Token: global.Cfg.NACServToken,
375+
IsRelay: global.Cfg.NACServIsRelay,
376+
377+
BeeperToken: global.Cfg.IMAToken,
378+
}
379+
380+
global.IM = direct.NewConnector(global.NAC, handleEvent, nil, nil, global.Cfg.EnablePairECSending, nil, manualLookupRatelimiter)
381+
global.IM.LoginTestConfig = global.Cfg.LoginTest
382+
383+
global.SecondaryIM = direct.NewConnector(global.NAC, handleSecondaryEvent, nil, nil, false, nil, manualLookupRatelimiter)
384+
if global.Cfg.AttachmentDir == "" {
385+
global.Cfg.AttachmentDir = "attachments"
386+
}
387+
if global.Cfg.DeviceName != "" {
388+
ids.DeviceName = global.Cfg.DeviceName
389+
}
390+
var err error
391+
global.Cfg.AttachmentDir, err = filepath.Abs(global.Cfg.AttachmentDir)
392+
if err != nil {
393+
panic(fmt.Errorf("failed to get absolute path of attachment directory: %w", err))
394+
}
395+
err = os.MkdirAll(global.Cfg.AttachmentDir, 0700)
396+
if err != nil {
397+
panic(fmt.Errorf("failed to create attachment directory: %w", err))
398+
}
399+
}

0 commit comments

Comments
 (0)