working on troubes with toiml parsing

This commit is contained in:
Paul Wilde 2021-08-16 13:21:30 +01:00
parent acef6e76d7
commit c7bf57dab3
7 changed files with 260 additions and 28 deletions

View file

@ -0,0 +1,13 @@
# Sample config.ini file.
# Copy this file to "config/config.ini" and adjust the settings to your requirements
# The URL of this application
BaseURL = "https://autoconfig.example.com"
# Set the email domains for use with this service. The first one is primary.
# each will need their own DNS A record for autoconfig.domain.name
Domains = ["example.com","example2,com"]
# If you use a different domain to authenticate with, enter it here
LogonDomain = "example.local"

View file

@ -0,0 +1,121 @@
# The Incoming mail Server Config
[InMail]
# Enable this service
Enabled = true
# Mail Type, i.e. IMAP, POP3
Type = "IMAP"
# Your IMAP server
Server = "imap.example.com"
# Your IMAP port
Port = 993
# The socket type = SSL, STARTTLS, or NONE
SocketType = "SSL"
# Use Secure Password Authentication
SPA = false
# Change to true if you need the domain/logondomain to form part of the username
UsernameIsFQDN = false
# Do you need to authenticate to your mail server? You should! so this should be false!
NoAuthRequired = false
# Authentication type,
#"password-cleartext" = Send password in the clear
# (dangerous, if SSL isn't used either).
# AUTH PLAIN, LOGIN or protocol-native login.
#"password-encrypted" = A secure encrypted password mechanism.
# Can be CRAM-MD5 or DIGEST-MD5. Not NTLM.
#"NTLM"= Use NTLM (or NTLMv2 or successors),
# the Windows login mechanism.
Authentication = "password-cleartext"
#
#
# # The Outgoing mail server config
# [OutMail]
# Name = "OutMail"
#
# # Enable this service
# Enabled = true
#
# # Mail type, likely to only be SMTP
# Type = "SMTP"
#
# # Your SMTP server
# Server = "smtp.example.com"
#
# # Your SMTP port
# Port = 465
#
# # The socket type = SSL, STARTTLS or NONE
# SocketType = "SSL"
#
# # See InMail > Authentication
# Authentication = "password-cleartext"
#
# # Use Secure Password Authentication
# SPA = false
#
# # Change to true if you need the domain/logondomain to form part of the username
# UsernameIsFQDN = false
#
# # Do you need to authenticate to your mail server? You should! so this should be false!
# NoAuthRequired = false
#
# # Use POP Authentication? You probably shouldn't be.
# POPAuth = false
#
# # This setting is here to limit errors, I'm not sure what it does yet.
# SMTPLast = false
#
#
# # Currently not implemented, see https://wiki.mozilla.org/Thunderbird=Autoconfiguration=ConfigFileFormat
# [Calendar]
# Name = "Calendar"
# # Disable this service
# Enabled = false
# Server = "https://example.com/remote.php/dav/"
# Port = 443
# Type = "CalDAV"
# Authentication = "http-basic"
# UsernameIsFQDN = false
#
# # Currently not implemented, see https://wiki.mozilla.org/Thunderbird=Autoconfiguration=ConfigFileFormat
# [AddressBook]
# Name = "AddressBook"
# # Disable this service
# Enabled = false
# Server = "https://example.com/remote.php/dav/"
# Port = 443
# Type = "CardDAV"
# Authentication = "http-basic"
# UsernameIsFQDN = false
#
# # Currently not implemented, see https://wiki.mozilla.org/Thunderbird=Autoconfiguration=ConfigFileFormat
# [WebMail]
# Name = "WebMail"
# # Disable this service
# Enabled = false
# Server = "https://mail.example.com"
# UsernameDivID = "username"
# UsernameDivName = "username"
# PasswordDivName = "password"
# SubmitButtonID = "submit"
# SubmitButtonName = "submit"
# UsernameIsFQDN = false
#
#
# # In theory, additional custom services can be configured and will be displayed with
# # their options on the /get/all URL of this service. The third-party clients would need to
# # check this service as part of their development for this to work
# # Will not be shown in autodiscover.xml/json or config-v1.1.xml/autoconfig.xml
# # i.e Nextcloud - ideally a nextcloud client could check autoconfig for this URL for ease of set up
# #[Nextcloud]
# # Name = "NextCloud"
# # Server = "https://nextcloud.example.com"

View file

@ -1,8 +1,11 @@
package global package global
import ( import (
. "mailautoconf/structs" . "mailautoconf/structs"
"github.com/vaughan0/go-ini"
"fmt" "fmt"
"github.com/pelletier/go-toml/v2"
"io/ioutil"
"os"
) )
// Global variables // Global variables
@ -12,20 +15,86 @@ const defaultConfigDir string = "default-config/"
const configDir string = "config/" const configDir string = "config/"
func NewConfig() Config { func NewConfig() Config {
fmt.Println("Loading Config…") MainConfig = loadConfig()
cfg := "default-config/config.default.ini" return MainConfig
conf, err := ini.LoadFile(cfg) }
if err != nil { func loadConfig() Config {
fmt.Println(err.Error()) cfg := Config{}
}
fmt.Println(conf) fmt.Println("Loading Default Config…")
fmt.Println("Loading Services…") cfgfile := defaultConfigDir + "config.default.toml"
srv := "default-config/services.default.ini" unmarshalConfig(cfgfile, &cfg)
serv, err2 := ini.LoadFile(srv) fmt.Println(cfg)
if err2 != nil { customcfgfile := configDir + "config.toml"
fmt.Println(err2.Error()) unmarshalConfig(customcfgfile, &cfg)
} fmt.Println(cfg)
fmt.Println(serv) svcfile := defaultConfigDir + "services.default.toml"
newcfg := Config{} // cfg.Services = []Service{
return newcfg // Service{
// Name : "first",
// },
// Service{
// Name : "second",
// },
// Service{
// Name : "third",
// },
// }
// data, _ := toml.Marshal(cfg)
// ioutil.WriteFile(svcfile, data, 0)
unmarshalServices(svcfile, &cfg)
customsvcfile := configDir + "services.toml"
unmarshalServices(customsvcfile, &cfg)
// fmt.Println(cfg)
fmt.Println("\r\nOur Config :")
fmt.Println(cfg)
return cfg
}
func unmarshalConfig(file string, cfg *Config) {
if fileExists(file) {
content, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println("Error reading config :", file, " : ", err)
}
err2 := toml.Unmarshal(content, &cfg)
if err2 != nil {
fmt.Println("Error unmarshalling config :", file, " : ", err2)
}
}
}
func unmarshalServices(file string, cfg *Config) {
if fileExists(file) {
content, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println("Error reading services :", file, " : ", err)
}
customsvcfile := configDir + "services.toml"
content2, err2 := ioutil.ReadFile(file)
if err2 != nil {
fmt.Println("Error reading services :", customsvcfile, " : ", err2)
}
content = []byte(fmt.Sprintf(string(content),string(content2)))
var x map[string]interface{}
err3 := toml.Unmarshal(content, &x)
if err3 != nil {
fmt.Println("Error unmarshalling services :", file, " : ", err3)
}
fmt.Println(x)
}
}
func fileExists(file string) bool {
exists := false
if _, err := os.Stat(file); err == nil {
exists = true
} else {
fmt.Println(err)
fmt.Printf("File %s does not exist\n", file);
}
return exists
} }

View file

@ -3,5 +3,6 @@ module mailautoconf
go 1.16 go 1.16
require ( require (
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec // indirect github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pelletier/go-toml/v2 v2.0.0-beta.3 // indirect
) )

View file

@ -1,2 +1,10 @@
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec h1:DGmKwyZwEB8dI7tbLt/I/gQuP559o/0FrAkHKlQM/Ks= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec/go.mod h1:owBmyHYMLkxyrugmfwE/DLJyW8Ro9mkphwuVErQ0iUw= github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.0-beta.3 h1:PNCTU4naEJ8mKal97P3A2qDU74QRQGlv4FXiL1XDqi4=
github.com/pelletier/go-toml/v2 v2.0.0-beta.3/go.mod h1:aNseLYu/uKskg0zpr/kbr2z8yGuWtotWf/0BpGIAL2Y=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -5,12 +5,12 @@ import (
"net/http" "net/http"
"log" "log"
"mailautoconf/web/handler" "mailautoconf/web/handler"
. "mailautoconf/global" "mailautoconf/global"
) )
func main() { func main() {
MainConfig = NewConfig() global.NewConfig()
http.HandleFunc("/", handler.WebHandler) http.HandleFunc("/", handler.WebHandler)
fmt.Println("Starting up Web Listener on port 8080") fmt.Println("Starting up Web Listener on port 8010")
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(":8010", nil))
} }

View file

@ -1,9 +1,8 @@
package structs package structs
// I don't like the name of this package, consider naming it "core" or
// separating out the structs and core functions
import "net/http" import "net/http"
type Session struct { type Session struct {
ResponseWriter http.ResponseWriter ResponseWriter http.ResponseWriter
Request *http.Request Request *http.Request
@ -11,8 +10,29 @@ type Session struct {
WebContent string WebContent string
} }
type Config struct { type Config struct {
Services []Service BaseURL string
Domains []string
LogonDomain string
Services []interface{}
} }
type Service struct { type Service struct {
Name string
Enabled bool
Type string
Server string
Port int
SocketType string
SPA bool
UsernameIsFQDN bool
NoAuthRequired bool
Authentication string
// For Outgoing Mail
POPAuth bool
SMTPLast bool
// For WebMail (Unused)
UsernameDivID string
UsernameDivName string
PasswordDivName string
SubmitButtonID string
SubmitButtonName string
} }