darkirc2.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi::{
  19. event_graph::{self},
  20. net::transport::Dialer,
  21. system::ExecutorPtr,
  22. util::path::expand_path,
  23. Error, Result,
  24. };
  25. use darkfi_serial::{
  26. async_trait, deserialize_async_partial, AsyncDecodable, AsyncEncodable, Encodable,
  27. SerialDecodable, SerialEncodable,
  28. };
  29. use evgrd::{FetchEventsMessage, LocalEventGraph, VersionMessage, MSG_EVENT, MSG_FETCHEVENTS};
  30. use log::{error, info};
  31. use sled_overlay::sled;
  32. use smol::fs;
  33. use url::Url;
  34. use crate::scene::SceneNodePtr;
  35. #[cfg(target_os = "android")]
  36. const EVGRDB_PATH: &str = "/data/data/darkfi.darkwallet/evgr/";
  37. #[cfg(target_os = "linux")]
  38. const EVGRDB_PATH: &str = "~/.local/darkfi/darkwallet/evgr/";
  39. #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
  40. pub struct Privmsg {
  41. pub channel: String,
  42. pub nick: String,
  43. pub msg: String,
  44. }
  45. pub async fn receive_msgs(sg_root: SceneNodePtr, ex: ExecutorPtr) -> Result<()> {
  46. let chatview_node = sg_root.lookup_node("/window/view/chatty").ok_or(Error::ConnectFailed)?;
  47. info!(target: "darkirc", "Instantiating DarkIRC event DAG");
  48. let datastore = expand_path(EVGRDB_PATH)?;
  49. fs::create_dir_all(&datastore).await?;
  50. let sled_db = sled::open(datastore)?;
  51. let evgr = LocalEventGraph::new(sled_db.clone(), "darkirc_dag", 1, ex.clone()).await?;
  52. let endpoint = "tcp://127.0.0.1:5588";
  53. let endpoint = Url::parse(endpoint)?;
  54. let dialer = Dialer::new(endpoint.clone(), None).await?;
  55. let timeout = std::time::Duration::from_secs(60);
  56. let mut stream = dialer.dial(Some(timeout)).await?;
  57. info!(target: "darkirc", "Connected to the backend: {endpoint}");
  58. let version = VersionMessage::new();
  59. version.encode_async(&mut stream).await?;
  60. let server_version = VersionMessage::decode_async(&mut stream).await?;
  61. info!(target: "darkirc", "Backend server version: {}", server_version.protocol_version);
  62. let unref_tips = evgr.unreferenced_tips.read().await.clone();
  63. let fetchevs = FetchEventsMessage::new(unref_tips);
  64. MSG_FETCHEVENTS.encode_async(&mut stream).await?;
  65. fetchevs.encode_async(&mut stream).await?;
  66. loop {
  67. let msg_type = u8::decode_async(&mut stream).await?;
  68. debug!(target: "darkirc", "Received: {msg_type:?}");
  69. if msg_type != MSG_EVENT {
  70. error!(target: "darkirc", "Received invalid msg_type: {msg_type}");
  71. return Err(Error::MalformedPacket)
  72. }
  73. let ev = event_graph::Event::decode_async(&mut stream).await?;
  74. let genesis_timestamp = evgr.current_genesis.read().await.clone().timestamp;
  75. let ev_id = ev.id();
  76. if evgr.dag.contains_key(ev_id.as_bytes()).unwrap() ||
  77. !ev.validate(&evgr.dag, genesis_timestamp, evgr.days_rotation, None).await?
  78. {
  79. error!(target: "darkirc", "Event is invalid! {ev:?}");
  80. continue
  81. }
  82. debug!(target: "darkirc", "got {ev:?}");
  83. evgr.dag_insert(&[ev.clone()]).await.unwrap();
  84. let privmsg: Privmsg = match deserialize_async_partial(ev.content()).await {
  85. Ok((v, _)) => v,
  86. Err(e) => {
  87. error!(target: "darkirc", "Failed deserializing incoming Privmsg event: {e}");
  88. continue
  89. }
  90. };
  91. debug!(target: "darkirc", "privmsg: {privmsg:?}");
  92. if privmsg.channel != "random" {
  93. continue
  94. }
  95. let response_fn = Box::new(|_| {});
  96. let mut arg_data = vec![];
  97. ev.timestamp.encode(&mut arg_data).unwrap();
  98. ev.id().as_bytes().encode(&mut arg_data).unwrap();
  99. privmsg.nick.encode(&mut arg_data).unwrap();
  100. privmsg.msg.encode(&mut arg_data).unwrap();
  101. chatview_node.call_method("insert_line", arg_data, response_fn).unwrap();
  102. }
  103. }