lib.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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 sled_overlay::sled;
  19. use smol::lock::Mutex;
  20. use std::{collections::HashSet, path::PathBuf};
  21. use darkfi::{
  22. event_graph::EventGraphPtr, net::P2pPtr, rpc::jsonrpc::JsonSubscriber, system::StoppableTaskPtr,
  23. };
  24. use darkfi_serial::{async_trait, SerialDecodable, SerialEncodable};
  25. /// IRC server and client handler implementation
  26. pub mod irc;
  27. use irc::server::IrcServer;
  28. use crate::irc::server::MAX_NICK_LEN;
  29. /// Cryptography utilities
  30. pub mod crypto;
  31. /// Pregenerated DarkIRC RLN identity commitments.
  32. pub mod genesis_commits;
  33. /// JSON-RPC methods
  34. pub mod rpc;
  35. /// Settings utilities
  36. pub mod settings;
  37. /// IRC PRIVMSG
  38. #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
  39. pub struct Privmsg {
  40. pub version: u8,
  41. pub msg_type: u8,
  42. pub channel: String,
  43. pub nick: String,
  44. pub msg: String,
  45. }
  46. pub struct DarkIrc {
  47. /// P2P network pointer
  48. p2p: P2pPtr,
  49. /// Sled DB (also used in event_graph and for RLN)
  50. sled: sled::Db,
  51. /// Event Graph instance
  52. event_graph: EventGraphPtr,
  53. /// JSON-RPC connection tracker
  54. rpc_connections: Mutex<HashSet<StoppableTaskPtr>>,
  55. /// dnet JSON-RPC subscriber
  56. dnet_sub: JsonSubscriber,
  57. /// deg JSON-RPC subscriber
  58. deg_sub: JsonSubscriber,
  59. /// Gource visualization JSON-RPC subscriber
  60. gource_sub: JsonSubscriber,
  61. /// Replay logs (DB) path
  62. replay_datastore: PathBuf,
  63. }
  64. impl DarkIrc {
  65. pub fn new(
  66. p2p: P2pPtr,
  67. sled: sled::Db,
  68. event_graph: EventGraphPtr,
  69. dnet_sub: JsonSubscriber,
  70. deg_sub: JsonSubscriber,
  71. gource_sub: JsonSubscriber,
  72. replay_datastore: PathBuf,
  73. ) -> Self {
  74. Self {
  75. p2p,
  76. sled,
  77. event_graph,
  78. rpc_connections: Mutex::new(HashSet::new()),
  79. dnet_sub,
  80. deg_sub,
  81. gource_sub,
  82. replay_datastore,
  83. }
  84. }
  85. }
  86. pub fn pad(string: &str) -> Vec<u8> {
  87. let mut bytes = string.as_bytes().to_vec();
  88. bytes.resize(MAX_NICK_LEN, 0x00);
  89. bytes
  90. }
  91. pub fn unpad(vec: &mut Vec<u8>) {
  92. if let Some(i) = vec.iter().rposition(|x| *x != 0) {
  93. let new_len = i + 1;
  94. vec.truncate(new_len);
  95. }
  96. }