Compare commits

...

1 Commits
v0.3.0 ... main

Author SHA1 Message Date
Dan Ballard 3186856eab message handlers also supply behaviour and mutable refs 2024-04-09 01:13:38 -07:00
2 changed files with 23 additions and 9 deletions

View File

@ -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<FixedOffset>, 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<FixedOffset>, message: MessageWrapper) {
let response = MessageWrapper {
o: MessageType::TextMessage,
d: message.d,

View File

@ -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<FixedOffset>, 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<FixedOffset>, 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<FixedOffset>, 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<FixedOffset>, 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<T>(&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())
}
}
}