mod.rs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 std::collections::HashMap;
  19. use darkfi::{util::path::get_config_path, Result};
  20. use crate::{
  21. settings::{
  22. parse_configured_channels, parse_configured_contacts, Args, ChannelInfo, ContactInfo,
  23. CONFIG_FILE, MAXIMUM_LENGTH_OF_NICK_CHAN_CNT,
  24. },
  25. PrivMsgEvent,
  26. };
  27. mod client;
  28. pub use client::IrcClient;
  29. mod server;
  30. pub use server::IrcServer;
  31. #[derive(Clone)]
  32. pub struct IrcConfig {
  33. // init bool
  34. pub is_nick_init: bool,
  35. pub is_user_init: bool,
  36. pub is_registered: bool,
  37. pub is_cap_end: bool,
  38. pub is_pass_init: bool,
  39. // user config
  40. pub nick: String,
  41. pub pass: String,
  42. pub caps: HashMap<String, bool>,
  43. // channels and contacts
  44. pub auto_channels: Vec<String>,
  45. pub channels: HashMap<String, ChannelInfo>,
  46. pub contacts: HashMap<String, ContactInfo>,
  47. }
  48. impl IrcConfig {
  49. pub fn new(settings: &Args) -> Result<Self> {
  50. let pass = settings.password.as_ref().unwrap_or(&String::new()).clone();
  51. let mut auto_channels = settings.autojoin.clone();
  52. auto_channels.retain(|chan| chan.len() <= MAXIMUM_LENGTH_OF_NICK_CHAN_CNT);
  53. // Pick up channel settings from the TOML configuration
  54. let cfg_path = get_config_path(settings.config.clone(), CONFIG_FILE)?;
  55. let toml_contents = std::fs::read_to_string(cfg_path)?;
  56. let channels = parse_configured_channels(&toml_contents)?;
  57. let contacts = parse_configured_contacts(&toml_contents)?;
  58. let mut caps = HashMap::new();
  59. caps.insert("no-history".to_string(), false);
  60. Ok(Self {
  61. is_nick_init: false,
  62. is_user_init: false,
  63. is_registered: false,
  64. is_cap_end: true,
  65. is_pass_init: false,
  66. nick: "anon".to_string(),
  67. pass,
  68. auto_channels,
  69. channels,
  70. contacts,
  71. caps,
  72. })
  73. }
  74. }
  75. #[derive(Clone)]
  76. pub enum ClientSubMsg {
  77. Privmsg(PrivMsgEvent),
  78. Config(IrcConfig),
  79. }
  80. #[derive(Clone)]
  81. pub enum NotifierMsg {
  82. Privmsg(PrivMsgEvent),
  83. UpdateConfig,
  84. }