aboutsummaryrefslogtreecommitdiff
path: root/plugins/omemo/src/ui/bad_messages_populator.vala
blob: 5a881c250865a1835a77aa2e94f1f3c4133ff39e (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using Gee;
using Gtk;
using Qlite;

using Dino.Entities;
using Xmpp;

namespace Dino.Plugins.Omemo {

public enum BadnessType {
    UNTRUSTED,
    UNDECRYPTABLE
}

public class BadMessagesPopulator : Plugins.ConversationItemPopulator, Plugins.ConversationAdditionPopulator, Object {

    public string id { get { return "bad_omemo_messages"; } }

    private StreamInteractor stream_interactor;
    private Plugin plugin;
    private Database db;

    private Conversation? current_conversation;
    private Plugins.ConversationItemCollection? item_collection;

    private Gee.List<BadMessageItem> bad_items = new ArrayList<BadMessageItem>();

    public BadMessagesPopulator(StreamInteractor stream_interactor, Plugin plugin) {
        this.stream_interactor = stream_interactor;
        this.plugin = plugin;
        this.db = plugin.db;

        plugin.trust_manager.bad_message_state_updated.connect((account, jid, device_id) => {
            clear_state();
            init_state();
        });
    }

    private void init_state() {

        if (current_conversation.type_ == Conversation.Type.GROUPCHAT_PM) return;

        var qry = db.identity_meta.select()
            .join_with(db.identity, db.identity.id, db.identity_meta.identity_id)
            .with(db.identity.account_id, "=", current_conversation.account.id)
            .where("last_message_untrusted is not NULL OR last_message_undecryptable is not NULL");

        switch (current_conversation.type_) {
            case Conversation.Type.CHAT:
                qry.with(db.identity_meta.address_name, "=", current_conversation.counterpart.to_string());
                break;
            case Conversation.Type.GROUPCHAT:
                bool is_private = stream_interactor.get_module(MucManager.IDENTITY).is_private_room(current_conversation.account, current_conversation.counterpart);
                if (!is_private) return;

                var list = stream_interactor.get_module(MucManager.IDENTITY).get_offline_members(current_conversation.counterpart, current_conversation.account);
                if (list == null || list.is_empty) return;

                var selection = new StringBuilder();
                string[] selection_args = {};
                foreach (Jid jid in list) {
                    if (selection.len == 0) {
                        selection.append(@" ($(db.identity_meta.address_name) = ?");
                    } else {
                        selection.append(@" OR $(db.identity_meta.address_name) = ?");
                    }
                    selection_args += jid.to_string();
                }
                selection.append(")");
                qry.where(selection.str, selection_args);
                break;
        }

        foreach (Row row in qry) {
            Jid jid = new Jid(row[db.identity_meta.address_name]);
            if (!db.identity_meta.last_message_untrusted.is_null(row)) {
                DateTime time = new DateTime.from_unix_utc(row[db.identity_meta.last_message_untrusted]);
                var item = new BadMessageItem(plugin, current_conversation, jid, time, BadnessType.UNTRUSTED);
                bad_items.add(item);
                item_collection.insert_item(item);
            }
            if (!db.identity_meta.last_message_undecryptable.is_null(row)) {
                DateTime time = new DateTime.from_unix_utc(row[db.identity_meta.last_message_undecryptable]);
                var item = new BadMessageItem(plugin, current_conversation, jid, time, BadnessType.UNDECRYPTABLE);
                bad_items.add(item);
                item_collection.insert_item(item);
            }
        }
    }

    private void clear_state() {
        foreach (BadMessageItem bad_item in bad_items) {
            item_collection.remove_item(bad_item);
        }
    }

    public void init(Conversation conversation, Plugins.ConversationItemCollection item_collection, Plugins.WidgetType type) {
        current_conversation = conversation;
        this.item_collection = item_collection;

        init_state();
    }

    public void close(Conversation conversation) { }

    public void populate_timespan(Conversation conversation, DateTime after, DateTime before) { }
}

public class BadMessageItem : Plugins.MetaConversationItem {

    private Plugin plugin;
    private Conversation conversation;
    private DateTime date;
    private Jid problem_jid;
    private BadnessType badness_type;

    public BadMessageItem(Plugin plugin, Conversation conversation, Jid jid, DateTime date, BadnessType badness_type) {
        this.plugin = plugin;
        this.conversation = conversation;
        this.problem_jid = jid;
        this.date = date;
        this.sort_time = date;
        this.badness_type = badness_type;
    }

    public override Object? get_widget(Plugins.WidgetType widget_type) {
        return new BadMessagesWidget(plugin, conversation, problem_jid, badness_type);
    }

    public override Gee.List<Plugins.MessageAction>? get_item_actions(Plugins.WidgetType type) { return null; }
}

public class BadMessagesWidget : Box {
    public BadMessagesWidget(Plugin plugin, Conversation conversation, Jid jid, BadnessType badness_type) {
        Object(orientation:Orientation.HORIZONTAL, spacing:5);

        this.halign = Align.CENTER;
        this.visible = true;

        var sb = new StringBuilder();
        string who = "Your contact";
        if (conversation.type_ == Conversation.Type.GROUPCHAT) {
            var occupants = plugin.app.stream_interactor.get_module(MucManager.IDENTITY).get_occupants(conversation.counterpart, conversation.account);
            if (occupants == null) return;
            foreach (Jid occupant in occupants) {
                if (jid.equals_bare(plugin.app.stream_interactor.get_module(MucManager.IDENTITY).get_real_jid(occupant, conversation.account))) {
                    who = occupant.resourcepart;
                }
            }
        }
        if (badness_type == BadnessType.UNTRUSTED) {
            sb.append("%s has been using an untrusted device. You won't see messages from devices that you do not trust.".printf(who));
            sb.append(" <a href=\"\">%s</a>".printf("Manage devices"));
        } else {
            sb.append("%s does not trust this device. That means, you might be missing messages.".printf(who));
        }
        Label label = new Label(sb.str) { margin_start=70, margin_end=70, justify=Justification.CENTER, use_markup=true, selectable=true, wrap=true, wrap_mode=Pango.WrapMode.WORD_CHAR, hexpand=true, visible=true };
        label.get_style_context().add_class("dim-label");
        this.add(label);

        label.activate_link.connect(() => {
            if (badness_type == BadnessType.UNTRUSTED) {
                ContactDetailsDialog dialog = new ContactDetailsDialog(plugin, conversation.account, jid);
                dialog.set_transient_for((Window) get_toplevel());
                dialog.present();
            }

            return false;
        });
    }
}

}