aboutsummaryrefslogtreecommitdiff
path: root/vala-xmpp/src/module/presence/flag.vala
blob: a92c9405b76d971bab4da61574883a03d7413a62 (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
using Gee;

using Xmpp.Core;

namespace Xmpp.Presence {

public class Flag : XmppStreamFlag {
    public const string ID = "presence";

    private HashMap<string, ConcurrentList<string>> resources = new HashMap<string, ConcurrentList<string>>();
    private HashMap<string, Presence.Stanza> presences = new HashMap<string, Presence.Stanza>();

    public Set<string> get_available_jids() {
        return resources.keys;
    }

    public Gee.List<string>? get_resources(string bare_jid) {
        return resources[bare_jid];
    }

    public Presence.Stanza? get_presence(string full_jid) {
        return presences[full_jid];
    }

    public void add_presence(Presence.Stanza presence) {
        string bare_jid = get_bare_jid(presence.from);
        if (!resources.has_key(bare_jid)) {
            resources[bare_jid] = new ConcurrentList<string>();
        }
        if (resources[bare_jid].contains(presence.from)) {
            resources[bare_jid].remove(presence.from);
        }
        resources[bare_jid].add(presence.from);
        presences[presence.from] = presence;
    }

    public void remove_presence(string jid) {
        string bare_jid = get_bare_jid(jid);
        if (resources.has_key(bare_jid)) {
            if (is_bare_jid(jid)) {
                foreach (string full_jid in resources[jid]) {
                    presences.unset(full_jid);
                }
                resources.unset(jid);
            } else {
                resources[bare_jid].remove(jid);
                if (resources[bare_jid].size == 0) {
                    resources.unset(bare_jid);
                }
                presences.unset(jid);
            }
        }
    }

    public static Flag? get_flag(XmppStream stream) { return (Flag?) stream.get_flag(NS_URI, ID); }

    public static bool has_flag(XmppStream stream) { return get_flag(stream) != null; }

    public override string get_ns() { return NS_URI; }

    public override string get_id() { return ID; }
}

}