diff --git a/lib.go b/lib.go index f6e762f..c01444a 100644 --- a/lib.go +++ b/lib.go @@ -41,6 +41,17 @@ var acnQueue event.Queue var contactEventsQueue event.Queue var globalACN connectivity.ACN +// ChatMessage API currently not officially documented, see +// https://git.openprivacy.ca/cwtch.im/secure-development-handbook/issues/3 +// for latest updates for now +// +// A ChatMessage is the application-layer Cwtch message, delivered to the UI +// as serialized json. +type ChatMessage struct { + O int `json:"o"` + D string `json:"d"` +} + //export c_StartCwtch func c_StartCwtch(dir_c *C.char, len C.int, tor_c *C.char, torLen C.int) { dir := C.GoStringN(dir_c, len) @@ -516,6 +527,40 @@ func SendMessage(profileOnion, handle, msg string) { } } +//export c_SendInvitation +func c_SendInvitation(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, target_ptr *C.char, target_len C.int) { + profile := C.GoStringN(profile_ptr, profile_len) + handle := C.GoStringN(handle_ptr, handle_len) + target := C.GoStringN(target_ptr, target_len) + SendInvitation(profile, handle, target) +} + +// Send an invitation from `profileOnion` to contact `handle` (peer or group) +// asking them to add the contact `target` (also peer or group). +// For groups, the profile must already have `target` as a contact. +func SendInvitation(profileOnion, handle, target string) { + profile := application.GetPeer(profileOnion) + ph := utils.NewPeerHelper(profile) + + var invite ChatMessage + if ph.IsGroup(target) { + bundle, _ := profile.GetContact(profile.GetGroup(target).GroupServer).GetAttribute(string(model.BundleType)) + inviteStr, err := profile.GetGroup(target).Invite() + if err == nil { + invite = ChatMessage{O: 101, D: fmt.Sprintf("tofubundle:server:%s||%s", base64.StdEncoding.EncodeToString([]byte(bundle)), inviteStr)} + } + } else { + invite = ChatMessage{O: 100, D: target} + } + + inviteBytes, err := json.Marshal(invite) + if err != nil { + log.Errorf("malformed invite: %v", err) + } else { + SendMessage(profileOnion, handle, string(inviteBytes)) + } +} + //export c_ResetTor func c_ResetTor() { ResetTor()