aboutsummaryrefslogtreecommitdiff
path: root/src/session/mod.rs
blob: 0a305c7878368f3e108a737e40b432114a10f0a9 (plain)
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
use crate::message::{objects::{*, Types}, InitData, StatefulSyncableClient, SyncMessage, Syncable, Class};

// TODO implement nested types init and sync like BufferViewConfig in BufferViewManager

#[derive(Default, Debug)]
pub struct Session {
    pub alias_manager: AliasManager,
    pub buffer_syncer: BufferSyncer,
    pub backlog_manager: BacklogManager,
    pub buffer_view_manager: BufferViewManager,
    pub cert_manager: CertManager,
    pub core_info: CoreInfo,
    pub highlight_rule_manager: HighlightRuleManager,
    pub identity: Identity,
    pub ignore_list_manager: IgnoreListManager,
}

/// The Session Trait is the main point of entry and implements the basic logic
pub trait SessionManager {
    fn alias_manager(&mut self) -> &mut AliasManager;
    fn buffer_syncer(&mut self) -> &mut BufferSyncer;
    fn backlog_manager(&mut self) -> &mut BacklogManager;
    fn buffer_view_manager(&mut self) -> &mut BufferViewManager;
    fn cert_manager(&mut self) -> &mut CertManager;
    fn core_info(&mut self) -> &mut CoreInfo;
    fn highlight_rule_manager(&mut self) -> &mut HighlightRuleManager;
    fn identity(&mut self) -> &mut Identity;
    fn ignore_list_manager(&mut self) -> &mut IgnoreListManager;

    fn sync(&mut self, msg: SyncMessage)
    where
        Self: Sized,
    {
        match msg.class_name {
            Class::AliasManager => self.alias_manager().sync(msg),
            Class::BufferSyncer => self.buffer_syncer().sync(msg),
            Class::BufferViewConfig => (),
            Class::BufferViewManager => self.buffer_view_manager().sync(msg),
            Class::CoreInfo => self.core_info().sync(msg),
            Class::CoreData => (),
            Class::HighlightRuleManager => self.highlight_rule_manager().sync(msg),
            Class::Identity => self.identity().sync(msg),
            Class::IgnoreListManager => self.ignore_list_manager().sync(msg),
            Class::CertManager => self.cert_manager().sync(msg),
            Class::Network => (),
            Class::NetworkInfo => (),
            Class::NetworkConfig => (),
            Class::IrcChannel => {
                let mut object_name = msg.object_name.split('/');
                let network_id: i32 = object_name.next().unwrap().parse().unwrap();
                let channel = object_name.next().unwrap();

                debug!("Syncing IrcChannel {} in Network {:?}", channel, network_id);

                if let Some(network) = self.network(network_id) {
                    if network.irc_channels.get_mut(channel).is_none() {
                        warn!(
                            "Could not find IrcChannel {} in Network {:?}",
                            channel, network_id
                        )
                    } else {
                        match msg.slot_name.as_str() {
                            "addChannelMode" => {
                                let mut msg = msg.clone();
                                let mode: char = get_param!(msg);
                                let mode_type: ChannelModeType =
                                    network.get_channel_mode_type(mode);

                                network
                                    .irc_channels
                                    .get_mut(channel)
                                    .unwrap()
                                    .add_channel_mode(mode_type, mode, get_param!(msg));
                            }
                            "removeChannelMode" => {
                                let mut msg = msg.clone();
                                let mode: char = get_param!(msg);
                                let mode_type: ChannelModeType =
                                    network.get_channel_mode_type(mode);

                                network
                                    .irc_channels
                                    .get_mut(channel)
                                    .unwrap()
                                    .remove_channel_mode(mode_type, mode, get_param!(msg));
                            }
                            _ => network
                                .irc_channels
                                .get_mut(channel)
                                .unwrap()
                                .sync(msg.clone()),
                        }
                    }
                } else {
                    warn!("Could not find Network {:?}", network_id)
                }
            }
            Class::IrcUser => (),
            Class::Unknown => (),
        }
    }

    fn init(&mut self, data: InitData) {
        match data.init_data {
            Types::AliasManager(data) => {self.alias_manager().init(data)}
            Types::BufferSyncer(data) => self.buffer_syncer().init(data),
            Types::BufferViewConfig(_) => (),
            Types::BufferViewManager(data) => self.buffer_view_manager().init(data),
            Types::CoreData(data) => self.core_info().set_core_data(data),
            Types::HighlightRuleManager(data) => self.highlight_rule_manager().init(data),
            Types::IgnoreListManager(data) => self.ignore_list_manager().init(data),
            Types::CertManager(data) => self.cert_manager().init(data),
            Types::Network(_) => (),
            Types::NetworkInfo(_) => (),
            Types::NetworkConfig(_) => (),
            Types::Unknown(_) => (),
        }
    }
}

impl SessionManager for Session {
    fn alias_manager(&mut self) -> &mut AliasManager {
        &mut self.alias_manager
    }

    fn buffer_syncer(&mut self) -> &mut BufferSyncer {
        &mut self.buffer_syncer
    }

    fn backlog_manager(&mut self) -> &mut BacklogManager {
        &mut self.backlog_manager
    }

    fn buffer_view_manager(&mut self) -> &mut BufferViewManager {
        &mut self.buffer_view_manager
    }

    fn cert_manager(&mut self) -> &mut CertManager {
        &mut self.cert_manager
    }

    fn core_info(&mut self) -> &mut CoreInfo {
        &mut self.core_info
    }

    fn highlight_rule_manager(&mut self) -> &mut HighlightRuleManager {
        &mut self.highlight_rule_manager
    }

    fn identity(&mut self) -> &mut Identity {
        &mut self.identity
    }

    fn ignore_list_manager(&mut self) -> &mut IgnoreListManager {
        &mut self.ignore_list_manager
    }
}