Parcourir la source

Indexes fixed a little bit + removed unused properties in config file

Yurii Sokolovskyi il y a 1 semaine
Parent
commit
5f078e6b44
3 fichiers modifiés avec 5 ajouts et 101 suppressions
  1. 1 9
      crabmail.conf
  2. 2 82
      src/config.rs
  3. 2 10
      src/indexes.rs

+ 1 - 9
crabmail.conf

@@ -1,9 +1,3 @@
-base_url=http://localhost:8000
-# Use %s to represent list name
-email_fmt=lists+%s@somthing
-title_fmt=%s public inbox
-description=All my lists
-
 imap_domain=outlook.office365.com or imap.gmail.com
 imap_port=993
 username=emailAddress
@@ -17,6 +11,4 @@ smtp_port=smtpPort
 
 sonic_search_addr=127.0.0.1
 sonic_search_port=5041
-sonic_search_password=SecretPassword
-
-reply_add_link=false
+sonic_search_password=SecretPassword

+ 2 - 82
src/config.rs

@@ -1,37 +1,15 @@
-// This file is licensed under the terms of 0BSD:
-//
-// Permission to use, copy, modify, and/or distribute this software for any purpose with or without
-// fee is hereby granted.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
-// SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
-// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
-// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
-// OF THIS SOFTWARE.
-
 use std::sync::{Mutex, OnceLock};
 use std::fs::File;
 use std::io::{self, BufRead, BufReader, BufWriter, Write};
 use std::path::{Path, PathBuf};
 use crate::arg;
 
-// Ini-like configuration, with sections.
-// Global config first, then config for each subsection
 // key=value
-//
-// [section]
-// key2=value2
 #[derive(Default, Debug)]
 pub struct Config {
     pub maildir: PathBuf,
-    pub email_fmt: String,
-    pub title_fmt: String,
-    pub base_url: String,
-    pub description: String,
     pub include_html: bool,
     pub out_dir: PathBuf,
-    pub subsections: Vec<Subsection>,
     pub imap_domain: String,
     pub imap_port: u32,
     pub username: Mutex<String>,
@@ -46,13 +24,8 @@ pub struct Config {
 }
 
 impl Config {
-    // TODO defaults here
     pub fn match_kv(&mut self, key: &str, value: &str) {
         match key {
-            "email_fmt" => self.email_fmt = value.to_string(),
-            "title_fmt" => self.title_fmt = value.to_string(),
-            "base_url" => self.base_url = value.to_string(),
-            "description" => self.description = value.to_string(),
             "imap_domain" => self.imap_domain = value.to_string(),
             "imap_port" => self.imap_port = value.parse().unwrap(),
             "username" => self.username = Mutex::new(value.to_string()),
@@ -70,26 +43,6 @@ impl Config {
     }
 }
 
-#[derive(Default, Clone, Debug)]
-pub struct Subsection {
-    pub name: String,  // something
-    pub title: String, // something mail archive
-    pub email: String,
-    pub description: String,
-}
-
-impl Subsection {
-    // TODO defaults here
-    fn match_kv(&mut self, key: &str, value: &str) {
-        match key {
-            "title" => self.title = value.to_string(),
-            "email" => self.email = value.to_string(),
-            "description" => self.description = value.to_string(),
-            _ => {}
-        }
-    }
-}
-
 pub static INSTANCE: OnceLock<Config> = OnceLock::new();
 
 impl Config {
@@ -97,56 +50,23 @@ impl Config {
         INSTANCE.get().expect("Config is not initialized")
     }
 
-    pub fn default_subsection(&self, name: &str) -> Subsection {
-        Subsection {
-            name: name.to_owned(),
-            title: self.title_fmt.replace("%s", name),
-            email: self.email_fmt.replace("%s", name),
-            description: String::new(),
-        }
-    }
-
-    pub fn get_subsection(&self, name: &str) -> Option<Subsection> {
-        self.subsections
-            .iter()
-            .find(|sub| sub.name == name)
-            .cloned()
-    }
-
     pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config, std::io::Error> {
         let file = File::open(path)?;
-        // let sub_sections = vec![];
         let mut conf = Config::default();
-        let mut current_section = None;
 
         for l in io::BufReader::new(file).lines() {
             let line = l?;
-            if line.starts_with('[') && line.ends_with(']') {
-                let name = &line[1..line.len() - 1];
-                // Defaults from global config
-                if let Some(section) = current_section {
-                    conf.subsections.push(section);
-                }
-                current_section = Some(conf.default_subsection(name))
-            }
             if line.is_empty() {
                 continue;
             }
             if let Some(i) = line.find('=') {
                 let key = &line[..i];
                 let value = &line[i + 1..];
-                if let Some(ref mut s) = current_section {
-                    s.match_kv(key, value);
-                } else {
-                    conf.match_kv(key, value);
-                }
+                conf.match_kv(key, value);
             } else {
-                // panic!("Invalid config")
+                println!("Invalid config file on line {line}")
             }
         }
-        if let Some(section) = current_section {
-            conf.subsections.push(section);
-        }
         Ok(conf)
     }
     

+ 2 - 10
src/indexes.rs

@@ -338,7 +338,6 @@ impl Indexes {
     }
 
     pub fn find_by_id(messages: &Vec<SerializableMessage>, search_id: String) -> Option<SerializableMessage> {
-        // TODO remove messages from arguments
         for message in messages {
             if message.id == search_id {
                 return Some(message.clone());
@@ -348,7 +347,6 @@ impl Indexes {
     }
 
     pub fn find_by_uid(messages: &Vec<SerializableMessage>, search_uid: u32) -> Option<SerializableMessage> {
-        // TODO remove messages from arguments
         for message in messages {
             if message.uid == search_uid {
                 return Some(message.clone());
@@ -402,11 +400,8 @@ impl Indexes {
     }
 
     pub fn delete_from_messages(message_id: String) -> anyhow::Result<SerializableMessage> {
-        // TODO change to get_messages();
         let path = Config::global().out_dir.clone().join("messages.json");
-        let file = File::open(&path)?;
-        let reader = BufReader::new(file);
-        let mut messages: Vec<SerializableMessage> = serde_json::from_reader(reader)?;
+        let mut messages: Vec<SerializableMessage> = Indexes::get_messages()?;
         let message_for_deletion;
         if let Some(pos) = messages.iter().position(|message| message.id == message_id) {
             message_for_deletion = messages.remove(pos)
@@ -420,11 +415,8 @@ impl Indexes {
     }
 
     pub fn delete_from_threads(message: SerializableMessage) -> anyhow::Result<()> {
-        // TODO change to get_threads();
         let path = Config::global().out_dir.clone().join("threads.json");
-        let file = File::open(&path)?;
-        let reader = BufReader::new(file);
-        let mut threads: Vec<SerializableThread> = serde_json::from_reader(reader)?;
+        let mut threads: Vec<SerializableThread> = Indexes::get_threads()?;
         let mut i = 0;
         while i < threads.len() {
             if let Some(pos) = threads[i].messages.iter().position(|s| *s == message.id) {