aboutsummaryrefslogtreecommitdiff
path: root/plugins/omemo/src/jingle/jingle_helper.vala
blob: 4b6ed6467e7a92933a5fa098554574ed513f5eac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Dino.Entities;
using Xmpp;

namespace Dino.Plugins.JetOmemo {
public class EncryptionHelper : JingleFileEncryptionHelper, Object {
    private StreamInteractor stream_interactor;

    public EncryptionHelper(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;
    }

    public bool can_transfer(Conversation conversation) {
        return true;
    }

    public async bool can_encrypt(Conversation conversation, FileTransfer file_transfer, Jid? full_jid) {
        XmppStream? stream = stream_interactor.get_stream(conversation.account);
        if (stream == null) return false;

        Gee.List<Jid>? resources = stream.get_flag(Presence.Flag.IDENTITY).get_resources(conversation.counterpart);
        if (resources == null) return false;

        if (full_jid == null) {
            foreach (Jid test_jid in resources) {
                if (yield stream.get_module(Module.IDENTITY).is_available(stream, test_jid)) {
                    return true;
                }
            }
        } else {
            if (yield stream.get_module(Module.IDENTITY).is_available(stream, full_jid)) {
                return true;
            }
        }
        return false;
    }

    public string? get_precondition_name(Conversation conversation, FileTransfer file_transfer) {
        return Xep.Jet.NS_URI;
    }

    public Object? get_precondition_options(Conversation conversation, FileTransfer file_transfer) {
        return new Xep.Jet.Options(Omemo.NS_URI, AES_128_GCM_URI);
    }

    public FileMeta complete_meta(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta, Xmpp.Xep.JingleFileTransfer.FileTransfer jingle_transfer) {
        Xep.Jet.SecurityParameters? security = jingle_transfer.security as Xep.Jet.SecurityParameters;
        if (security != null && security.encoding.get_type_uri() == Omemo.NS_URI) {
            file_transfer.encryption = Encryption.OMEMO;
        }
        return file_meta;
    }
}
}