Skip to content

Commit

Permalink
Store everything in XDG_CONFIG_HOME, add status command
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Mar 22, 2019
1 parent acf2a5e commit e6f8149
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
30 changes: 27 additions & 3 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/openpgp"

"github.com/emersion/hydroxide/config"
"github.com/emersion/hydroxide/protonmail"
)

const authFile = "auth.json"
func authFilePath() (string, error) {
return config.Path("auth.json")
}

type CachedAuth struct {
protonmail.Auth
Expand All @@ -26,7 +29,11 @@ type CachedAuth struct {
}

func readCachedAuths() (map[string]string, error) {
f, err := os.Open(authFile)
p, err := authFilePath()
if err != nil {
return nil, err
}
f, err := os.Open(p)
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
Expand All @@ -40,7 +47,11 @@ func readCachedAuths() (map[string]string, error) {
}

func saveAuths(auths map[string]string) error {
f, err := os.Create(authFile)
p, err := authFilePath()
if err != nil {
return err
}
f, err := os.Create(p)
if err != nil {
return err
}
Expand Down Expand Up @@ -123,6 +134,19 @@ func authenticate(c *protonmail.Client, cachedAuth *CachedAuth, username string)
return c.Unlock(auth, cachedAuth.MailboxPassword)
}

func ListUsernames() ([]string, error) {
auths, err := readCachedAuths()
if err != nil {
return nil, err
}

l := make([]string, 0, len(auths))
for username, _ := range auths {
l = append(l, username)
}
return l, nil
}

func GeneratePassword() (secretKey *[32]byte, password string, err error) {
var key [32]byte
if _, err = io.ReadFull(rand.Reader, key[:]); err != nil {
Expand Down
14 changes: 14 additions & 0 deletions cmd/hydroxide/hydroxide.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ func main() {
}

fmt.Println("Bridge password:", bridgePassword)
case "status":
usernames, err := auth.ListUsernames()
if err != nil {
log.Fatal(err)
}

if len(usernames) == 0 {
fmt.Printf("No logged in user.\n")
} else {
fmt.Printf("%v logged in user(s):\n", len(usernames))
for _, u := range usernames {
fmt.Printf("- %v\n", u)
}
}
case "smtp":
port := os.Getenv("PORT")
if port == "" {
Expand Down
27 changes: 27 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

import (
"errors"
"os"
"path/filepath"
)

func Path(filename string) (string, error) {
configHome := os.Getenv("XDG_CONFIG_HOME")
if configHome == "" {
home := os.Getenv("HOME")
if home == "" {
return "", errors.New("HOME not set")
}
configHome = filepath.Join(home, ".config")
}

p := filepath.Join(configHome, "hydroxide", filename)

dirname, _ := filepath.Split(p)
if err := os.MkdirAll(dirname, 0700); err != nil {
return "", err
}

return p, nil
}
10 changes: 8 additions & 2 deletions imap/database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/boltdb/bolt"

"github.com/emersion/hydroxide/config"
"github.com/emersion/hydroxide/protonmail"
)

Expand Down Expand Up @@ -223,8 +224,13 @@ func (u *User) Close() error {
return u.db.Close()
}

func Open(path string) (*User, error) {
db, err := bolt.Open(path, 0700, nil)
func Open(filename string) (*User, error) {
p, err := config.Path(filename)
if err != nil {
return nil, err
}

db, err := bolt.Open(p, 0700, nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e6f8149

Please sign in to comment.