Skip to content

Commit

Permalink
ffi: expose init_config function
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <sumner@beeper.com>
  • Loading branch information
sumnerevans committed Dec 20, 2023
1 parent 1d41744 commit b36c234
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
4 changes: 0 additions & 4 deletions imessage/ffi/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 0 additions & 34 deletions imessage/ffi/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ import (
"context"
_ "embed"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 41 additions & 0 deletions imessage/ffi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ import "C"
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"

"github.com/rs/zerolog"
deflog "github.com/rs/zerolog/log"
Expand All @@ -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"
Expand Down Expand Up @@ -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))
}
}

0 comments on commit b36c234

Please sign in to comment.