all repos — emessage @ 0cbb6ef61f1c6f3f3a199c14add57b3daed63684

The EMessage email client

app.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
package main

import (
	"context"
	"errors"
	"strings"

	"github.com/adrg/xdg"
	_ "github.com/emersion/go-imap/v2"
	"github.com/emersion/go-imap/v2/imapclient"
	"github.com/zalando/go-keyring"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type AppConfig struct {
	Title string
}

type App struct {
	ctx         context.Context
	Config      AppConfig
	DB          *gorm.DB
	UserContact Contact
}

func NewApp(config AppConfig) *App {
	app := &App{Config: config}
	db, err := gorm.Open(sqlite.Open(app.GetDataFile()), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	app.DB = db
	app.migrate()
	return app
}

func (app *App) startup(ctx context.Context) {
	app.ctx = ctx
}

func (app *App) shutdown(ctx context.Context) {
	return
}

func (app *App) migrate() {
	app.DB.AutoMigrate(&Contact{})
	app.DB.AutoMigrate(&EmailAddress{})
	app.DB.AutoMigrate(&PhoneNumber{})
	app.DB.AutoMigrate(&URL{})
	app.DB.AutoMigrate(&StreetAddress{})
	app.DB.AutoMigrate(&Date{})
	app.DB.AutoMigrate(&OtherField{})
	app.DB.FirstOrCreate(
		&app.UserContact,
		Contact{Model: gorm.Model{ID: 1}},
	)
}

func (app *App) GetTitle() string {
	return app.Config.Title
}

func (app *App) GetDataFile() string {
	if file, err := xdg.DataFile(app.GetTitle() + "/data.db"); err != nil {
		panic(err)
	} else {
		return file
	}
}

func (app *App) GetMailServerKeyringService() string {	
	title := app.GetTitle()
	return title + "-mail-server"
}

func (app *App) GetMailPasswordKeyringService() string {	
	title := app.GetTitle()
	return title + "-mail-password"
}

type MailConnection struct {
	Client *imapclient.Client
	err error
}
var mailConnections = make(map[string]MailConnection)

func (app *App) UserMailAuth() {
	for _, email := range app.UserContact.Emails {
		app.MailAuth(email)
	}
}

func (app *App) MailAuth(email EmailAddress) (conn MailConnection) {
	mailServer, err := keyring.Get(app.GetMailServerKeyringService(), email.Value)
	if err != nil {
		conn.err = err
		return
	}
	if mailServer == "" {
		conn.err = errors.New("No mail server found")
		return
	}
	
	mailPassword, err := keyring.Get(app.GetMailPasswordKeyringService(), email.Value)
	if err != nil {
		conn.err = err
		return
	}
	if mailPassword == "" {
		conn.err = errors.New("No password found")
		return
	}

	client, err := imapclient.DialTLS(mailServer + ":993", nil)
	if err != nil {
		conn.err = err
		return
	}
	conn.Client = client

	if err := client.Login(strings.Split(email.Value, "@")[0], mailPassword).Wait(); err != nil {
		conn.err = err
		return
	}
	
	return
}