cwtch_imp/
behaviour.rs

1use libcwtch::event::{ContactIdentity};
2
3/// defines a locked list of allowed peers and groups the bot may communicate with
4/// others will be blocked, and peers listed here will be peered with actively
5pub struct AllowListMembers {
6    /// list of peers to allow by handle
7    pub peers: Vec<ContactIdentity>,
8}
9
10impl AllowListMembers {
11    /// constructs a new AllowListMembers struct
12    pub fn new(peers: Vec<ContactIdentity>) -> Self {
13        AllowListMembers {peers: peers}
14    }
15}
16
17/// How new contacts should be treated
18pub enum NewContactPolicy {
19    /// Do not react, leave it for the custom event handler
20    Ignore,
21    /// Block all new contacts
22    Block,
23    /// Accept all new contacts
24    Accept,
25    /// AllowList is a list of handles that connections will be allowed from and connected to, and will be accepted
26    ///   everything else will be ignored
27    AllowList
28}
29
30pub enum ContactInteractionPolicy {
31    /// Do not react, leave it for the custom event handler
32    Ignore,
33    /// Accept all messages
34    Accept,
35    /// Only accept messages from contacts. Pairs strongly with NewContactPolicy of AllowList
36    AcceptFromContact,
37    /// AllowList is a list of handles that connections will be allowed from and connected to, and will be accepted
38    ///   everything else will be ignored
39    AllowList
40}
41
42pub enum GroupInvitePolicy {
43    /// Ignore all group invites
44    Ignore,
45    /// Accept all group invites
46    Accept,
47    /// Only accept group invites from contacts. Pairs strongly with NewContactPolicy of AllowList
48    AcceptFromContact,
49    /// AllowList is a list of handles that group invites will be allowed from and connected to,
50    /// and will be accepted. Everything else will be ignored
51    AllowList,
52}
53
54pub enum GroupInteractionPolicy {
55    /// Do not react, leave it for the custom event handler
56    Ignore,
57    /// Accept all messages
58    Accept,
59    /// Only accept messages from contacts. Pairs strongly with NewContactPolicy of AllowList
60    AcceptFromContact,
61    /// AllowList is a list of handles that connections will be allowed from and connected to, and will be accepted
62    ///   everything else will be ignored
63    AllowList
64}
65
66/// Settings for the bot on how it should automatically behave
67pub struct Behaviour {
68    /// The bot will enable experimental feautres (required for any experiments to be used)
69    pub proto_experiments: bool,
70
71    /// The bot will enable the file sharing experiment
72    pub proto_experiment_fileshare: bool,
73
74    /// The bot will enable the groups experiment
75    pub proto_experiment_groups: bool,
76
77    /// The profile name the bot will share with accepted conversations
78    pub profile_name: String,
79    /// The profile pic the bot with share with accepted conversations IF the file share exoeriment is enabled
80    pub profile_pic_path: Option<String>,
81
82    /// Policy dictacting how the bot should automatically handle ContactCreated events
83    pub new_contant_policy: NewContactPolicy,
84
85    /// Policy dictating how the bot should handle messages from p2p contacts
86    pub contact_interaction_policy: ContactInteractionPolicy,
87
88    /// Policy dictating how the bot should handle Group Invites
89    pub group_invite_policy: GroupInvitePolicy,
90
91    /// Policy dictacting how the bot should respond to @ messages in groups
92    pub group_interaction_policy: GroupInteractionPolicy,
93
94    pub allow_list: AllowListMembers,
95}
96
97/// intermediary struct for building a Behaviour using builder patern
98pub struct BehaviourBuilder {
99    behaviour: Behaviour,
100}
101
102impl BehaviourBuilder {
103    /// Returns a new empty default off for features behaviour builder
104    pub fn new() -> Self {
105        return BehaviourBuilder {
106            behaviour: Behaviour {
107                proto_experiments: false,
108                proto_experiment_fileshare: false,
109                proto_experiment_groups: false,
110                new_contant_policy: NewContactPolicy::Ignore,
111                contact_interaction_policy: ContactInteractionPolicy::Ignore,
112                group_invite_policy: GroupInvitePolicy::Ignore,
113                group_interaction_policy: GroupInteractionPolicy::Ignore,
114                profile_name: "".to_string(),
115                profile_pic_path: None,
116                allow_list: AllowListMembers::new(vec!()),
117            },
118        };
119    }
120
121    /// Build the defined behaviours into a Behaviour struct
122    pub fn build(self) -> Behaviour {
123        self.behaviour
124    }
125
126    /// Control if the Behaviour of the bot should include groups (enabling experiments and the group experiment)
127    pub fn groups(mut self, val: bool) -> Self {
128        self.behaviour.proto_experiment_groups = val;
129        self.behaviour.proto_experiments = true;
130        self
131    }
132
133    /// Control if the Behaviour of the bot should include filesharing (enabling experiments and the filesharing experiment)
134    pub fn fileshare(mut self, val: bool) -> Self {
135        self.behaviour.proto_experiment_fileshare = val;
136        self.behaviour.proto_experiments = true;
137        self
138    }
139
140    /// Set a profile pic for the bot and enable the filesharing experiment
141    pub fn profile_pic_path(mut self, val: String) -> Self {
142        self.behaviour.profile_pic_path = Some(val);
143        self.behaviour.proto_experiment_fileshare = true;
144        self.behaviour.proto_experiments = true;
145        self
146    }
147
148    /// Set a name for the behaviour
149    pub fn name(mut self, val: String) -> Self {
150        self.behaviour.profile_name = val;
151        self
152    }
153
154    /// Set a new contact policy for the behaviour
155    pub fn new_contact_policy(mut self, val: NewContactPolicy) -> Self {
156        self.behaviour.new_contant_policy = val;
157        self
158    }
159
160    pub fn contact_interaction_policy(mut self, val: ContactInteractionPolicy) -> Self {
161        self.behaviour.contact_interaction_policy = val;
162        self
163    }
164
165    pub fn group_invite_policy(mut self, val: GroupInvitePolicy) -> Self {
166        self.behaviour.group_invite_policy = val;
167        self
168    }
169
170    pub fn group_interaction_policy(mut self, val: GroupInteractionPolicy) -> Self {
171        self.behaviour.group_interaction_policy = val;
172        self
173    }
174
175    pub fn allow_list(mut self, val: AllowListMembers) -> Self {
176        self.behaviour.allow_list = val;
177        self
178    }
179}