138 lines
3.3 KiB
Go
138 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
func slash(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
func main() {
|
|
|
|
var err error
|
|
var configuration BlogConfiguration
|
|
args := os.Args[1:]
|
|
|
|
home := os.Getenv("HOME")
|
|
err = os.MkdirAll(fmt.Sprintf("%s/.config/", home), 0600)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
file, err := os.Open(fmt.Sprintf("%s/.config/mailblog.json", home))
|
|
if err == nil {
|
|
defer file.Close()
|
|
decoder := json.NewDecoder(file)
|
|
err = decoder.Decode(&configuration)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
switch args[0] {
|
|
case "configure":
|
|
|
|
configuration.Title, err = prompt("Blog Title", nil, "", configuration.Title, false)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
configuration.ShortName, err = prompt("Blog Short Name (only letters and numbers, no special signes)", nil, "blog", configuration.ShortName, false)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if !regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString(configuration.ShortName) {
|
|
log.Fatal("Short Name invalid")
|
|
}
|
|
|
|
configuration.WWWRoot, err = prompt("Web Root directory", nil, fmt.Sprintf("/var/www/html/%s", configuration.ShortName), configuration.WWWRoot, false)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
configuration.MailBox.Server, err = prompt("IMAP Server Hostname or IP", nil, "", configuration.MailBox.Server, false)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
configuration.MailBox.Port, err = prompt("IMAP Server Port", nil, "993", configuration.MailBox.Port, false)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
_, err = strconv.ParseUint(configuration.MailBox.Port, 10, 16)
|
|
if err != nil {
|
|
log.Fatal("IMAP Server Port invalid")
|
|
}
|
|
configuration.MailBox.User, err = prompt("IMAP User Login", nil, "", configuration.MailBox.User, false)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
configuration.MailBox.Password, err = prompt("IMAP User Password", nil, "", configuration.MailBox.Password, false)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
options := [...]string{"SSL/TLS", "StartTLS", "NoTLS"}
|
|
configuration.MailBox.SSL, err = prompt("IMAP SSL/TLS", options[:], "SSL/TLS", configuration.MailBox.SSL, false)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
configuration.MailBox.InBox, err = prompt("IMAP INBOX", nil, "INBOX", configuration.MailBox.InBox, false)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
file, err = os.Create(fmt.Sprintf("%s/.config/mailblog.json", home))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
encoder := json.NewEncoder(file)
|
|
err = encoder.Encode(configuration)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("Configuration save in %s/.config/mailblob.json\n", home)
|
|
return
|
|
}
|
|
}
|
|
|
|
var mb MailBox
|
|
mb.Configure(&configuration.MailBox)
|
|
err = mb.Connect()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
folders, err := mb.ListFolders()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Println(folders)
|
|
msgs, err := mb.GetMessages()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, m := range msgs {
|
|
log.Println(m.Id)
|
|
log.Println(m.Title)
|
|
log.Println(m.Author)
|
|
log.Println("Text version")
|
|
log.Println(m.Text)
|
|
log.Println("HTML version")
|
|
log.Println(m.HTML)
|
|
log.Println(m.Id)
|
|
log.Println()
|
|
}
|
|
|
|
mb.Close()
|
|
|
|
http.HandleFunc("/", slash)
|
|
http.ListenAndServe("0.0.0.0:8080", nil)
|
|
}
|