1
0
Fork 0
mirror of https://github.com/mautrix/signal.git synced 2026-05-15 05:36:53 -04:00
mautrix-signal/pkg/libsignalgo/sessionstore.go

74 lines
3 KiB
Go
Raw Permalink Normal View History

2023-12-17 15:54:35 +02:00
// mautrix-signal - A Matrix-signal puppeting bridge.
// Copyright (C) 2023 Sumner Evans
2025-01-17 17:49:37 +02:00
// Copyright (C) 2025 Tulir Asokan
2023-12-17 15:54:35 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package libsignalgo
/*
#include "./libsignal-ffi.h"
2026-02-09 13:54:02 +02:00
extern int signal_load_session_callback(void *store_ctx, SignalMutPointerSessionRecord *recordp, SignalMutPointerProtocolAddress address);
extern int signal_store_session_callback(void *store_ctx, SignalMutPointerProtocolAddress address, SignalMutPointerSessionRecord record);
extern void signal_destroy_session_store_callback(void *store_ctx);
*/
import "C"
import (
"context"
"unsafe"
)
type SessionStore interface {
2024-01-04 01:06:45 +02:00
LoadSession(ctx context.Context, address *Address) (*SessionRecord, error)
StoreSession(ctx context.Context, address *Address, record *SessionRecord) error
}
//export signal_load_session_callback
2026-02-09 13:54:02 +02:00
func signal_load_session_callback(storeCtx unsafe.Pointer, recordp *C.SignalMutPointerSessionRecord, address C.SignalMutPointerProtocolAddress) C.int {
2024-01-04 01:06:45 +02:00
return wrapStoreCallback(storeCtx, func(store SessionStore, ctx context.Context) error {
2026-02-09 13:54:02 +02:00
record, err := store.LoadSession(ctx, &Address{ptr: address.raw})
if err == nil && record != nil {
record.CancelFinalizer()
2026-02-09 13:54:02 +02:00
recordp.raw = record.ptr
}
return err
})
}
//export signal_store_session_callback
2026-02-09 13:54:02 +02:00
func signal_store_session_callback(storeCtx unsafe.Pointer, address C.SignalMutPointerProtocolAddress, sessionRecord C.SignalMutPointerSessionRecord) C.int {
2024-01-04 01:06:45 +02:00
return wrapStoreCallback(storeCtx, func(store SessionStore, ctx context.Context) error {
2026-02-09 13:54:02 +02:00
record := SessionRecord{ptr: sessionRecord.raw}
cloned, err := record.Clone()
if err != nil {
return err
}
2026-02-09 13:54:02 +02:00
return store.StoreSession(ctx, &Address{ptr: address.raw}, cloned)
})
}
2026-02-09 13:54:02 +02:00
//export signal_destroy_session_store_callback
func signal_destroy_session_store_callback(storeCtx unsafe.Pointer) {
// No-op: Go's garbage collector handles cleanup
}
2025-01-17 17:49:37 +02:00
func (ctx *CallbackContext) wrapSessionStore(store SessionStore) C.SignalConstPointerFfiSessionStoreStruct {
return C.SignalConstPointerFfiSessionStoreStruct{&C.SignalSessionStore{
2024-01-04 01:06:45 +02:00
ctx: wrapStore(ctx, store),
2026-02-09 13:54:02 +02:00
load_session: C.SignalFfiBridgeSessionStoreLoadSession(C.signal_load_session_callback),
store_session: C.SignalFfiBridgeSessionStoreStoreSession(C.signal_store_session_callback),
destroy: C.SignalFfiBridgeSessionStoreDestroy(C.signal_destroy_session_store_callback),
2025-01-17 17:49:37 +02:00
}}
}