aboutsummaryrefslogtreecommitdiff
path: root/plugins/omemo/src/plugin.vala
blob: a062640beb69e18b80371567cf0d8b1a21137551 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using Xmpp;

namespace Dino.Omemo {

    public class EncryptionListEntry : Plugins.EncryptionListEntry, Object {
        private Plugin plugin;

        public EncryptionListEntry(Plugin plugin) {
            this.plugin = plugin;
        }

        public Entities.Encryption encryption { get {
            return Entities.Encryption.OMEMO;
        }}

        public string name { get {
            return "OMEMO";
        }}

        public bool can_encrypt(Entities.Conversation conversation) {
            return Manager.get_instance(plugin.app.stream_interaction).con_encrypt(conversation);
        }
    }

    public class AccountSettingsEntry : Plugins.AccountSettingsEntry {
        private Plugin plugin;

        public AccountSettingsEntry(Plugin plugin) {
            this.plugin = plugin;
        }

        public override string id { get {
            return "omemo_identity_key";
        }}

        public override string name { get {
            return "OMEMO";
        }}

        public override Plugins.AccountSettingsWidget get_widget() {
            return new AccountSettingWidget(plugin);
        }
    }

    public class AccountSettingWidget : Plugins.AccountSettingsWidget, Gtk.Box {
        private Plugin plugin;
        private Gtk.Label fingerprint;
        private Entities.Account account;

        public AccountSettingWidget(Plugin plugin) {
            this.plugin = plugin;

            fingerprint = new Gtk.Label("...");
            fingerprint.xalign = 0;
            Gtk.Border border = new Gtk.Button().get_style_context().get_padding(Gtk.StateFlags.NORMAL);
            fingerprint.set_padding(border.left + 1, border.top + 1);
            fingerprint.visible = true;
            pack_start(fingerprint);

            Gtk.Button btn = new Gtk.Button();
            btn.image = new Gtk.Image.from_icon_name("view-list-symbolic", Gtk.IconSize.BUTTON);
            btn.relief = Gtk.ReliefStyle.NONE;
            btn.visible = true;
            btn.valign = Gtk.Align.CENTER;
            btn.clicked.connect(() => { activated(); });
            pack_start(btn, false);
        }

        public void set_account(Entities.Account account) {
            this.account = account;
            try {
                Qlite.Row? row = plugin.db.identity.row_with(plugin.db.identity.account_id, account.id);
                if (row == null) {
                    fingerprint.set_markup(@"Own fingerprint\n<span font='8'>Will be generated on first connect</span>");
                } else {
                    uint8[] arr = Base64.decode(row[plugin.db.identity.identity_key_public_base64]);
                    arr = arr[1:arr.length];
                    string res = "";
                    foreach (uint8 i in arr) {
                        string s = i.to_string("%x");
                        if (s.length == 1) s = "0" + s;
                        res = res + s;
                        if ((res.length % 9) == 8) {
                            if (res.length == 35) {
                                res += "\n";
                            } else {
                                res += " ";
                            }
                        }
                    }
                    fingerprint.set_markup(@"Own fingerprint\n<span font_family='monospace' font='8'>$res</span>");
                }
            } catch (Qlite.DatabaseError e) {
                fingerprint.set_markup(@"Own fingerprint\n<span font='8'>Database error</span>");
            }
        }

        public void deactivate() {
        }
    }

    public class Plugin : Plugins.RootInterface, Object {
        public Dino.Application app;
        public Database db;
        public EncryptionListEntry list_entry;
        public AccountSettingsEntry settings_entry;

        public void registered(Dino.Application app) {
            this.app = app;
            this.db = new Database("omemo.db");
            this.list_entry = new EncryptionListEntry(this);
            this.settings_entry = new AccountSettingsEntry(this);
            app.plugin_registry.register_encryption_list_entry(list_entry);
            app.plugin_registry.register_account_settings_entry(settings_entry);
            app.stream_interaction.module_manager.initialize_account_modules.connect((account, list) => {
                list.add(new Module());
            });
            Manager.start(app.stream_interaction, db);
        }

        public void shutdown() {
            // Nothing to do
        }
    }

}

public Type register_plugin(Module module) {
    return typeof (Dino.Omemo.Plugin);
}