Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Add support for sending voice notes
Browse files Browse the repository at this point in the history
  • Loading branch information
nanu-c committed Oct 13, 2020
1 parent db4ebd9 commit 0039002
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
32 changes: 31 additions & 1 deletion attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,37 @@ func uploadAttachment(r io.Reader, ct string) (*att, error) {
return nil, err
}

return &att{id, ct, keys, digest, uint32(plaintextLength)}, nil
return &att{id, ct, keys, digest, uint32(plaintextLength), false}, nil
}
func uploadVoiceNote(r io.Reader, ct string) (*att, error) {
//combined AES-256 and HMAC-SHA256 key
keys := make([]byte, 64)
randBytes(keys)

b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

plaintextLength := len(b)

e, err := aesEncrypt(keys[:32], b)
if err != nil {
return nil, err
}

m := appendMAC(keys[32:], e)

id, location, err := allocateAttachment()
if err != nil {
return nil, err
}
digest, err := putAttachment(location, m)
if err != nil {
return nil, err
}

return &att{id, ct, keys, digest, uint32(plaintextLength), true}, nil
}

// ErrInvalidMACForAttachment signals that the downloaded attachment has an invalid MAC.
Expand Down
9 changes: 8 additions & 1 deletion groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,14 @@ func SendGroupAttachment(hexid string, msg string, r io.Reader, timer uint32) (u
}
return sendGroupHelper(hexid, msg, a, timer)
}

func SendGroupVoiceNote(hexid string, msg string, r io.Reader, timer uint32) (uint64, error) {
ct, r := MIMETypeFromReader(r)
a, err := uploadVoiceNote(r, ct)
if err != nil {
return 0, err
}
return sendGroupHelper(hexid, msg, a, timer)
}
func newGroupID() []byte {
id := make([]byte, 16)
randBytes(id)
Expand Down
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,10 @@ func createMessage(msg *outgoingMessage) *signalservice.DataMessage {
Size: &msg.attachment.size,
},
}
if msg.attachment.voiceNote {
var flag uint32 = 1
dm.Attachments[0].Flags = &flag
}
}
if msg.group != nil {
dm.Group = &signalservice.GroupContext{
Expand Down
9 changes: 7 additions & 2 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func handleSyncMessage(src string, timestamp uint64, sm *signalservice.SyncMessa
func handleSyncSent(s *signalservice.SyncMessage_Sent, ts uint64) error {
dm := s.GetMessage()
dest := s.GetDestinationE164()
timestamp := s.GetTimestamp()

if dm == nil {
return fmt.Errorf("DataMessage was nil for SyncMessage_Sent")
Expand Down Expand Up @@ -92,8 +91,14 @@ func handleSyncSent(s *signalservice.SyncMessage_Sent, ts uint64) error {
attachments: atts,
group: gr,
contact: cs,
timestamp: timestamp,
flags: flags,
expireTimer: dm.GetExpireTimer(),
profileKey: dm.GetProfileKey(),
timestamp: *dm.Timestamp,
quote: dm.GetQuote(),
preview: dm.GetPreview(),
sticker: dm.GetSticker(),
reaction: dm.GetReaction(),
}

if client.SyncSentHandler != nil {
Expand Down
27 changes: 21 additions & 6 deletions textsecure.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ func needsRegistration() bool {
var identityKey *axolotl.IdentityKeyPair

type att struct {
id uint64
ct string
keys []byte
digest []byte
size uint32
id uint64
ct string
keys []byte
digest []byte
size uint32
voiceNote bool
}

type outgoingMessage struct {
Expand Down Expand Up @@ -164,6 +165,20 @@ func SendAttachment(tel, msg string, r io.Reader, timer uint32) (uint64, error)
}
return sendMessage(omsg)
}
func SendVoiceNote(tel, msg string, r io.Reader, timer uint32) (uint64, error) {
ct, r := MIMETypeFromReader(r)
a, err := uploadVoiceNote(r, ct)
if err != nil {
return 0, err
}
omsg := &outgoingMessage{
tel: tel,
msg: msg,
attachment: a,
expireTimer: timer,
}
return sendMessage(omsg)
}

// EndSession terminates the session with the given peer.
func EndSession(tel string, msg string) (uint64, error) {
Expand Down Expand Up @@ -534,7 +549,7 @@ func handleReceiptMessage(src string, timestamp uint64, cm *signalservice.Receip
msg := &Message{
source: src,
message: "sentReceiptMessage",
timestamp: cm.Timestamp[0],
timestamp: cm.GetTimestamp()[0],
}
if *cm.Type == signalservice.ReceiptMessage_READ {
msg.message = "readReceiptMessage"
Expand Down

0 comments on commit 0039002

Please sign in to comment.