/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2024 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
use std::sync::{Arc, Mutex as SyncMutex};
use darkfi::{
event_graph::{self, proto::ProtocolEventGraph, EventGraph, EventGraphPtr},
net::{session::SESSION_DEFAULT, settings::Settings as NetSettings, P2p, P2pPtr},
system::{sleep, Subscription},
Error,
};
use darkfi_serial::{
async_trait, deserialize_async, serialize_async, Encodable, SerialDecodable, SerialEncodable,
};
use crate::{scene::SceneGraphPtr2, ExecutorPtr};
#[cfg(target_os = "android")]
const EVGRDB_PATH: &str = "/data/data/darkfi.darkwallet/evgrdb/";
#[cfg(target_os = "linux")]
const EVGRDB_PATH: &str = "evgrdb";
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct Privmsg {
pub channel: String,
pub nick: String,
pub msg: String,
}
async fn relay_darkirc_events(sg: SceneGraphPtr2, ev_sub: Subscription) {
loop {
let ev = ev_sub.receive().await;
// Try to deserialize the `Event`'s content into a `Privmsg`
let privmsg: Privmsg = match deserialize_async(ev.content()).await {
Ok(v) => v,
Err(e) => {
error!("[IRC CLIENT] Failed deserializing incoming Privmsg event: {}", e);
continue
}
};
if privmsg.channel != "#random" {
continue
}
info!(target: "darkirc", "ev_id={:?}", ev.id());
info!(target: "darkirc", "ev: {:?}", ev);
info!(target: "darkirc", "privmsg: {:?}", privmsg);
info!(target: "darkirc", "");
let response_fn = Box::new(|_| {});
let mut arg_data = vec![];
ev.timestamp.encode(&mut arg_data).unwrap();
ev.id().as_bytes().encode(&mut arg_data).unwrap();
privmsg.nick.encode(&mut arg_data).unwrap();
privmsg.msg.encode(&mut arg_data).unwrap();
let mut sg = sg.lock().await;
let chatview_node = sg.lookup_node_mut("/window/view/chatty").unwrap();
chatview_node.call_method("insert_line", arg_data, response_fn).unwrap();
drop(sg);
}
}
pub type DarkIrcBackendPtr = Arc;
struct DarkIrcData {
p2p: P2pPtr,
event_graph: EventGraphPtr,
#[allow(dead_code)]
ev_task: smol::Task<()>,
db: sled::Db,
}
pub struct DarkIrcBackend(SyncMutex