package config import ( "errors" "fmt" "io/ioutil" "net/url" "os" "os/exec" "path" "regexp" "sort" "strconv" "strings" "time" "unicode" "github.com/gdamore/tcell/v2" "github.com/go-ini/ini" "github.com/imdario/mergo" "github.com/kyoh86/xdg" "git.sr.ht/~rjarry/aerc/lib/templates" ) type GeneralConfig struct { DefaultSavePath string `ini:"default-save-path"` } type UIConfig struct { IndexFormat string `ini:"index-format"` TimestampFormat string `ini:"timestamp-format"` ThisDayTimeFormat string `ini:"this-day-time-format"` ThisWeekTimeFormat string `ini:"this-week-time-format"` ThisYearTimeFormat string `ini:"this-year-time-format"` ShowHeaders []string `delim:","` RenderAccountTabs string `ini:"render-account-tabs"` PinnedTabMarker string `ini:"pinned-tab-marker"` SidebarWidth int `ini:"sidebar-width"` PreviewHeight int `ini:"preview-height"` EmptyMessage string `ini:"empty-message"` EmptyDirlist string `ini:"empty-dirlist"` MouseEnabled bool `ini:"mouse-enabled"` ThreadingEnabled bool `ini:"threading-enabled"` NewMessageBell bool `ini:"new-message-bell"` Spinner string `ini:"spinner"` SpinnerDelimiter string `ini:"spinner-delimiter"` DirListFormat string `ini:"dirlist-format"` Sort []string `delim:" "` NextMessageOnDelete bool `ini:"next-message-on-delete"` CompletionDelay time.Duration `ini:"completion-delay"` CompletionPopovers bool `ini:"completion-popovers"` StyleSetDirs []string `ini:"stylesets-dirs" delim:":"` StyleSetName string `ini:"styleset-name"` style StyleSet // customize border appearance BorderCharVertical rune `ini:"-"` BorderCharHorizontal rune `ini:"-"` } type ContextType int const ( UI_CONTEXT_FOLDER ContextType = iota UI_CONTEXT_ACCOUNT UI_CONTEXT_SUBJECT BIND_CONTEXT_ACCOUNT ) type UIConfigContext struct { ContextType ContextType Regex *regexp.Regexp UiConfig UIConfig } const ( FILTER_MIMETYPE = iota FILTER_HEADER ) type AccountConfig struct { Archive string CopyTo string Default string Postpone string From string Aliases string Name string Source string SourceCredCmd string Folders []string FoldersExclude []string Params map[string]string Outgoing string OutgoingCredCmd string SignatureFile string SignatureCmd string EnableFoldersSort bool `ini:"enable-folders-sort"` FoldersSort []string `ini:"folders-sort" delim:","` } type BindingConfig struct { Global *KeyBindings AccountWizard *KeyBindings Compose *KeyBindings ComposeEditor *KeyBindings ComposeReview *KeyBindings MessageList *KeyBindings MessageView *KeyBindings Terminal *KeyBindings } type BindingConfigContext struct { ContextType ContextType Regex *regexp.Regexp Bindings *KeyBindings BindContext string } type ComposeConfig struct { Editor string `ini:"editor"` HeaderLayout [][]string `ini:"-"` AddressBookCmd string `ini:"address-book-cmd"` } type FilterConfig struct { FilterType int Filter string Command string Header string Regex *regexp.Regexp } type ViewerConfig struct { Pager string Alternatives []string ShowHeaders bool `ini:"show-headers"` AlwaysShowMime bool `ini:"always-show-mime"` HeaderLayout [][]string `ini:"-"` } type TriggersConfig struct { NewEmail string `ini:"new-email"` ExecuteCommand func(command []string) error } type TemplateConfig struct { TemplateDirs []string `ini:"template-dirs", delim:":"` QuotedReply string `ini:"quoted-reply"` Forwards string `ini:"forwards"` } type AercConfig struct { Bindings BindingConfig ContextualBinds []BindingConfigContext Compose ComposeConfig Ini *ini.File `ini:"-"` Accounts []AccountConfig `ini:"-"` Filters []FilterConfig `ini:"-"` Viewer ViewerConfig `ini:"-"` Triggers TriggersConfig `ini:"-"` Ui UIConfig ContextualUis []UIConfigContext General GeneralConfig Templates TemplateConfig } // Input: TimestampFormat // Output: timestamp-format func mapName(raw string) string { newstr := make([]rune, 0, len(raw)) for i, chr := range raw { if isUpper := 'A' <= chr && chr <= 'Z'; isUpper { if i > 0 { newstr = append(newstr, '-') } } newstr = append(newstr, unicode.ToLower(chr)) } return string(newstr) } func loadAccountConfig(path string) ([]AccountConfig, error) { file, err := ini.Load(path) if err != nil { // No config triggers account configuration wizard return nil, nil } file.NameMapper = mapName var accounts []AccountConfig for _, _sec := range file.SectionStrings() { if _sec == "DEFAULT" { continue } sec := file.Section(_sec) account := AccountConfig{ Archive: "Archive", Default: "INBOX", Postpone: "Drafts", Name: _sec, Params: make(map[string]string), EnableFoldersSort: true, } if err = sec.MapTo(&account); err != nil { return nil, err } for key, val := range sec.KeysHash() { if key == "folders" { folders := strings.Split(val, ",") sort.Strings(folders) account.Folders = folders } else if key == "folders-exclude" { folders := strings.Split(val, ",") sort.Strings(folders) account.FoldersExclude = folders } else if key == "source-cred-cmd" { account.SourceCredCmd = val } else if key == "outgoing" { account.Outgoing = val } else if key == "outgoing-cred-cmd" { account.OutgoingCredCmd = val } else if key == "from" { account.From = val } else if key == "aliases" { account.Aliases = val } else if key == "copy-to" { account.CopyTo = val } else if key == "archive" { account.Archive = val } else if key == "enable-folders-sort" { account.EnableFoldersSort, _ = strconv.ParseBool(val) } else if key != "name" { account.Params[key] = val } } if account.Source == "" { return nil, fmt.Errorf("Expected source for account %s", _sec) } if account.From == "" { return nil, fmt.Errorf("Expected from for account %s", _sec) } source, err := parseCredential(account.Source, account.SourceCredCmd) if err != nil { return nil, fmt.Errorf("Invalid source credentials for %s: %s", _sec, err) } account.Source = source outgoing, err := parseCredential(account.Outgoing, account.OutgoingCredCmd) if err != nil { return nil, fmt.Errorf("Invalid outgoing credentials for %s: %s", _sec, err) } account.Outgoing = outgoing accounts = append(accounts, account) } return accounts, nil } func parseCredential(cred, command string) (string, error) { if cred == "" || command == "" { return cred, nil } u, err := url.Parse(cred) if err != nil { return "", err } // ignore the command if a password is specified if _, exists := u.User.Password(); exists { return cred, nil } // don't attempt to parse the command if the url is a path (ie /usr/bin/sendmail) if !u.IsAbs() { return cred, nil } cmd := exec.Command("sh", "-c", command) cmd.Stdin = os.Stdin output, err := cmd.Output() if err != nil { return "", fmt.Errorf("failed to read password: %s", err) } pw := strings.TrimSpace(string(output)) u.User = url.UserPassword(u.User.Username(), pw) return u.String(), nil } func installTemplate(root, sharedir, name string) error { if _, err := os.Stat(root); os.IsNotExist(err) { err := os.MkdirAll(root, 0755) if err != nil { return err } } data, err := ioutil.ReadFile(path.Join(sharedir, name)) if err != nil { return err } err = ioutil.WriteFile(path.Join(root, name), data, 0644) if err != nil { return err } return nil } func (config *AercConfig) LoadConfig(file *ini.File) error { if filters, err := file.GetSection("filters"); err == nil { // TODO: Parse the filter more finely, e.g. parse the regex for _, match := range filters.KeyStrings() { cmd := filters.KeysHash()[match] filter := FilterConfig{ Command: cmd, Filter: match, } if strings.Contains(match, ",~") { filter.FilterType = FILTER_HEADER header := filter.Filter[:strings.Index(filter.Filter, ",")] regex := filter.Filter[strings.Index(filter.Filter, "~")+1:] filter.Header = strings.ToLower(header) filter.Regex, err = regexp.Compile(regex) if err != nil { return err } } else if strings.ContainsRune(match, ',') { filter.FilterType = FILTER_HEADER header := filter.Filter[:strings.Index(filter.Filter, ",")] value := filter.Filter[strings.Index(filter.Filter, ",")+1:] filter.Header = strings.ToLower(header) filter.Regex, err = regexp.Compile(regexp.QuoteMeta(value)) if err != nil { return err } } else { filter.FilterType = FILTER_MIMETYPE } config.Filters = append(config.Filters, filter) } } if viewer, err := file.GetSection("viewer"); err == nil { if err := viewer.MapTo(&config.Viewer); err != nil { return err } for key, val := range viewer.KeysHash() { switch key { case "alternatives": config.Viewer.Alternatives = strings.Split(val, ",") case "header-layout": config.Viewer.HeaderLayout = parseLayout(val) } } } if compose, err := file.GetSection("compose"); err == nil { if err := compose.MapTo(&config.Compose); err != nil { return err } for key, val := range compose.KeysHash() { switch key { case "header-layout": config.Compose.HeaderLayout = parseLayout(val) } } } if ui, err := file.GetSection("ui"); err == nil { if err := ui.MapTo(&config.Ui); err != nil { return err } if err := validateBorderChars(ui, &config.Ui); err != nil { return err } } for _, sectionName := range file.SectionStrings() { if !strings.Contains(sectionName, "ui:") { continue } uiSection, err := file.GetSection(sectionName) if err != nil { return err } uiSubConfig := UIConfig{} if err := uiSection.MapTo(&uiSubConfig); err != nil { return err } if err := validateBorderChars(uiSection, &uiSubConfig); err != nil { return err } contextualUi := UIConfigContext{ UiConfig: uiSubConfig, } var index int if strings.Contains(sectionName, "~") { index = strings.Index(sectionName, "~") regex := string(sectionName[index+1:]) contextualUi.Regex, err = regexp.Compile(regex) if err != nil { return err } } else if strings.Contai
*.pyc
*.pyo
*~
*.orig
/.cache
/build
/build_scripts
/dist
/doc/ranger.1.html
/install_log.txt
/pytestdebug.log
/ranger_fm.egg-info
/stuff/*
.idea
.pytest_cache