nickserv.rs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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::BTreeMap;
  19. use crate::PrivMsgEvent;
  20. #[derive(Debug, Clone, Default)]
  21. pub struct NickServ {
  22. _db: BTreeMap<Vec<u8>, Vec<u8>>,
  23. }
  24. impl NickServ {
  25. fn usage() -> Vec<String> {
  26. let r = vec![
  27. "***** nickserv help *****",
  28. "",
  29. "nickserv allows clients to 'register' an account. An account",
  30. "registration is necessary to be able to join and send messages",
  31. "to the p2p network.",
  32. "",
  33. "The following commands are available:",
  34. "",
  35. " CREATE Create a new account",
  36. " LIST List available accounts",
  37. " REGISTER Register a new account",
  38. " IDENTIFY Identify and pick an account to use",
  39. "",
  40. "***** end of help *****",
  41. ];
  42. r.iter().map(|x| x.to_string()).collect()
  43. }
  44. // Here because we might consider returning the actual full protocol PRIVMSG
  45. // in a vec. So we can use the result of this and feed it directly to the
  46. // client as messages. Dunno if necessary, just a thought.
  47. fn reply(msg: String) -> Vec<String> {
  48. vec![msg]
  49. }
  50. /// Parse an incoming nickserv message
  51. pub fn act(&mut self, ev: PrivMsgEvent) -> Result<Vec<String>, Vec<String>> {
  52. assert_eq!(ev.target.to_lowercase().as_str(), super::NICK_NICKSERV);
  53. let parts: Vec<String> = ev.msg.split(' ').map(|x| x.to_string()).collect();
  54. match parts[0].to_uppercase().as_str() {
  55. "CREATE" => self.create(),
  56. "LIST" => self.list(),
  57. "REGISTER" => self.register(),
  58. "IDENTIFY" => self.identify(),
  59. "HELP" => Ok(Self::usage()),
  60. c => Err(vec![format!("Invalid command {}", c), "Type HELP to get help".to_string()]),
  61. }
  62. }
  63. /// Create a new account
  64. fn create(&mut self) -> Result<Vec<String>, Vec<String>> {
  65. Ok(Self::reply("Account created successfully.".to_string()))
  66. }
  67. /// List available accounts
  68. fn list(&self) -> Result<Vec<String>, Vec<String>> {
  69. Ok(Self::reply("Accounts: ...".to_string()))
  70. }
  71. /// Register a created but unregistered account
  72. fn register(&mut self) -> Result<Vec<String>, Vec<String>> {
  73. Ok(Self::reply("Account created successfully.".to_string()))
  74. }
  75. /// Pick an account to use
  76. fn identify(&mut self) -> Result<Vec<String>, Vec<String>> {
  77. Ok(Self::reply("Using account 0.".to_string()))
  78. }
  79. }