aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service/module_manager.vala
blob: 896fb1e4ca7a289fc8ac039f3a0f86f630e843e1 (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
using Gee;

using Dino.Entities;
using Xmpp;

namespace Dino {

public class ModuleManager {
    private HashMap<Account, ArrayList<Core.XmppStreamModule>> module_map = new HashMap<Account, ArrayList<Core.XmppStreamModule>>();

    private AvatarStorage avatar_storage = new AvatarStorage("./");
    private EntityCapabilitiesStorage entity_capabilities_storage;

    public signal void initialize_account_modules(Account account, ArrayList<Core.XmppStreamModule> modules);

    public ModuleManager(Database db) {
        entity_capabilities_storage = new EntityCapabilitiesStorage(db);
    }

    public T? get_module<T>(Account account, Core.ModuleIdentity<T> identity) {
        if (identity == null) return null;
        lock (module_map) {
            if (!module_map.has_key(account)) {
                initialize(account);
            }
            var res = module_map[account].filter((module) => identity.matches(module));
            if (res != null && res.next()) {
                return identity.cast(res.get());
            }
        }
        return null;
    }

    public ArrayList<Core.XmppStreamModule> get_modules(Account account, string? resource = null) {
        ArrayList<Core.XmppStreamModule> modules = new ArrayList<Core.XmppStreamModule>();
        lock (module_map) {
            if (!module_map.has_key(account)) {
                initialize(account);
            }
            foreach (Core.XmppStreamModule module in module_map[account]) {
                if (Bind.Module.IDENTITY.matches(module)) {
                    // TODO: argh?!
                    modules.add(new Bind.Module(resource == null ? account.resourcepart : resource));
                } else {
                    modules.add(module);
                }
            }
        }
        return modules;
    }

    public void initialize(Account account) {
        lock(module_map) {
            module_map[account] = new ArrayList<Core.XmppStreamModule>();
            module_map[account].add(new Tls.Module());
            module_map[account].add(new PlainSasl.Module(account.bare_jid.to_string(), account.password));
            module_map[account].add(new Bind.Module(account.resourcepart));
            module_map[account].add(new Roster.Module());
            module_map[account].add(new Xep.ServiceDiscovery.Module.with_identity("client", "pc"));
            module_map[account].add(new Xep.PrivateXmlStorage.Module());
            module_map[account].add(new Xep.Bookmarks.Module());
            module_map[account].add(new Presence.Module());
            module_map[account].add(new Xmpp.Message.Module());
            module_map[account].add(new Xep.MessageCarbons.Module());
            module_map[account].add(new Xep.Muc.Module());
            module_map[account].add(new Xep.Pubsub.Module());
            module_map[account].add(new Xep.EntityCapabilities.Module(entity_capabilities_storage));
            module_map[account].add(new Xep.UserAvatars.Module(avatar_storage));
            module_map[account].add(new Xep.VCard.Module(avatar_storage));
            module_map[account].add(new Xep.MessageDeliveryReceipts.Module());
            module_map[account].add(new Xep.ChatStateNotifications.Module());
            module_map[account].add(new Xep.ChatMarkers.Module());
            module_map[account].add(new Xep.Ping.Module());
            module_map[account].add(new Xep.DelayedDelivery.Module());
            module_map[account].add(new StreamError.Module());
            initialize_account_modules(account, module_map[account]);
        }
    }
}

}