From 3186856eab08d9af406516731caea7671a09f139 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 9 Apr 2024 01:13:38 -0700 Subject: [PATCH] message handlers also supply behaviour and mutable refs --- examples/echobot.rs | 2 +- src/imp.rs | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/examples/echobot.rs b/examples/echobot.rs index 405594b..7924968 100644 --- a/examples/echobot.rs +++ b/examples/echobot.rs @@ -26,7 +26,7 @@ fn main() { } impl imp::EventHandler for Echobot { - fn on_new_message_from_contact(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: ConversationID, _handle: String, _timestamp_received: DateTime, message: MessageWrapper) { + fn on_new_message_from_contact(&mut self, cwtch: &dyn libcwtch::CwtchLib, behaviour: &mut Behaviour, profile: &Profile, conversation_id: ConversationID, _handle: String, _timestamp_received: DateTime, message: MessageWrapper) { let response = MessageWrapper { o: MessageType::TextMessage, d: message.d, diff --git a/src/imp.rs b/src/imp.rs index be19f0d..187a7d5 100644 --- a/src/imp.rs +++ b/src/imp.rs @@ -1,3 +1,4 @@ +use std::ops::Index; use chrono::{DateTime, FixedOffset}; use libcwtch::structs::*; use libcwtch::CwtchLib; @@ -18,10 +19,10 @@ pub trait EventHandler { #[allow(unused_variables)] fn on_new_contact(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, convo_id: ConversationID) {} #[allow(unused_variables)] - fn on_new_message_from_contact(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: ConversationID, handle: String, timestamp_received: DateTime, message: MessageWrapper) {} + fn on_new_message_from_contact(&mut self, cwtch: &dyn libcwtch::CwtchLib, behaviour: &mut Behaviour, profile: &Profile, conversation_id: ConversationID, handle: String, timestamp_received: DateTime, message: MessageWrapper) {} #[allow(unused_variables)] - fn on_new_message_from_group(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: ConversationID, contact: ContactIdentity, timestamp_sent: DateTime, message: MessageWrapper) {} + fn on_new_message_from_group(&mut self, cwtch: &dyn libcwtch::CwtchLib, behaviour: &mut Behaviour, profile: &Profile, conversation_id: ConversationID, contact: ContactIdentity, timestamp_sent: DateTime, message: MessageWrapper) {} } /// Cwtch bot @@ -54,6 +55,19 @@ impl Imp { }; } + pub fn behaviour_mut(&self) -> &Behaviour { + &self.behaviour + } + pub fn behaviour_allowlist_add(&mut self, contact: ContactIdentity) { + self.behaviour.allow_list.peers.push(contact); + } + + pub fn behaviour_allowlist_rm(&mut self, contact: ContactIdentity) { + if let Some(index) = self.behaviour.allow_list.peers.iter().position(|c| *c == contact) { + self.behaviour.allow_list.peers.remove(index); + } + } + /// The main event loop handler for the bot, supply your own customer handler to handle events after the imp's automatic handling has processed the event #[allow(unused_variables, unused_mut)] pub fn event_loop(&mut self, handler: &mut T) @@ -205,16 +219,16 @@ impl Imp { if let Some(profile) = self.profile.as_ref() { match &self.behaviour.contact_interaction_policy { ContactInteractionPolicy::Ignore => (), - ContactInteractionPolicy::Accept => handler.on_new_message_from_contact(self.cwtch.as_ref(), profile, conversation_id.clone(), nick.clone(), timestamp_received.clone(), message.clone()), + ContactInteractionPolicy::Accept => handler.on_new_message_from_contact(self.cwtch.as_ref(), &mut self.behaviour, profile, conversation_id.clone(), nick.clone(), timestamp_received.clone(), message.clone()), ContactInteractionPolicy::AcceptFromContact=> { if profile.conversations[conversation_id].accepted { - handler.on_new_message_from_contact(self.cwtch.as_ref(), profile, conversation_id.clone(), nick.clone(), timestamp_received.clone(), message.clone()); + handler.on_new_message_from_contact(self.cwtch.as_ref(), &mut self.behaviour, profile, conversation_id.clone(), nick.clone(), timestamp_received.clone(), message.clone()); } }, ContactInteractionPolicy::AllowList => { if let Some(conversation) = profile.conversations.get(&conversation_id) { if self.behaviour.allow_list.peers.contains(&conversation.contact_id) { - handler.on_new_message_from_contact(self.cwtch.as_ref(), profile, conversation_id.clone(), nick.clone(), timestamp_received.clone(), message.clone()); + handler.on_new_message_from_contact(self.cwtch.as_ref(), &mut self.behaviour, profile, conversation_id.clone(), nick.clone(), timestamp_received.clone(), message.clone()); } } } @@ -226,17 +240,17 @@ impl Imp { if let Some(profile) = self.profile.as_ref() { match &self.behaviour.group_interaction_policy { GroupInteractionPolicy::Ignore => (), - GroupInteractionPolicy::Accept => handler.on_new_message_from_group(self.cwtch.as_ref(), profile, conversation_id.clone(), contact_id.clone(), timestamp_sent.clone(), message.clone()), + GroupInteractionPolicy::Accept => handler.on_new_message_from_group(self.cwtch.as_ref(), &mut self.behaviour, profile, conversation_id.clone(), contact_id.clone(), timestamp_sent.clone(), message.clone()), GroupInteractionPolicy::AcceptFromContact=> { if let Some(contact_convo_id) = profile.find_conversation_id_by_handle(contact_id.clone()) { if profile.conversations[&contact_convo_id].accepted { - handler.on_new_message_from_group(self.cwtch.as_ref(), profile, conversation_id.clone(), contact_id.clone(), timestamp_sent.clone(), message.clone()) + handler.on_new_message_from_group(self.cwtch.as_ref(), &mut self.behaviour, profile, conversation_id.clone(), contact_id.clone(), timestamp_sent.clone(), message.clone()) } } }, GroupInteractionPolicy::AllowList => { if self.behaviour.allow_list.peers.contains(&contact_id) { - handler.on_new_message_from_group(self.cwtch.as_ref(), profile, conversation_id.clone(), contact_id.clone(), timestamp_sent.clone(), message.clone()) + handler.on_new_message_from_group(self.cwtch.as_ref(), &mut self.behaviour, profile, conversation_id.clone(), contact_id.clone(), timestamp_sent.clone(), message.clone()) } } }