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 <[email protected]>
  • Loading branch information
sumnerevans committed Jan 2, 2024
1 parent 76901b1 commit 9cd79ba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
4 changes: 0 additions & 4 deletions imessage/ffi/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,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
35 changes: 0 additions & 35 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 All @@ -40,7 +36,6 @@ import (
"github.com/beeper/imessage/imessage"
"github.com/beeper/imessage/imessage/appleid"
"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/httputil"
"github.com/beeper/imessage/imessage/ffi/imux"
Expand Down Expand Up @@ -137,36 +132,6 @@ func init() {
event.TypeMap[EventTypeMarkedUnreadIPC] = reflect.TypeOf(MarkedUnreadEventContent{})
gob.Register(&RoomAvatarEventContent{})
gob.Register(&MarkedUnreadEventContent{})

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 9cd79ba

Please sign in to comment.