Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions db/table_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,29 @@ func (f *FilesTable) CountByThread(id ...interface{}) (int, error) {

return result, nil
}

func (f *FilesTable) GetInEmail(owner string, email string, name string) ([]*models.File, error) {
e, err := f.Emails.GetEmail(email)
if err != nil {
return nil, err
}

query, err := f.GetTable().Filter(func(row gorethink.Term) gorethink.Term {
return gorethink.And(
row.Field("owner").Eq(gorethink.Expr(owner)),
gorethink.Expr(e.Files).Contains(row.Field("id")),
row.Field("name").Eq(gorethink.Expr(name)),
)
}).Run(f.GetSession())
if err != nil {
return nil, err
}

var result []*models.File
err = query.All(&result)
if err != nil {
return nil, err
}

return result, nil
}
2 changes: 2 additions & 0 deletions models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
type Account struct {
Resource

StyledName string `json:"styled_name" gorethink:"styled_name"`

// Billing is a struct containing billing information.
// TODO Work in progress
Billing BillingData `json:"billing" gorethink:"billing"`
Expand Down
22 changes: 18 additions & 4 deletions routes/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func AccountsCreate(w http.ResponseWriter, r *http.Request) {
}

if requestType == "register" {
// Normalize the username
input.Username = utils.NormalizeUsername(input.Username)

// Ensure that the username is not used
if used, err := env.Accounts.IsUsernameUsed(input.Username); err != nil || used {
if err != nil {
Expand Down Expand Up @@ -116,10 +119,11 @@ func AccountsCreate(w http.ResponseWriter, r *http.Request) {

// Both username and email are filled, so we can create a new account.
account := &models.Account{
Resource: models.MakeResource("", input.Username),
Type: "beta", // Is this the proper value?
AltEmail: input.AltEmail,
Status: "registered",
Resource: models.MakeResource("", utils.RemoveDots(input.Username)),
StyledName: input.Username,
Type: "beta", // Is this the proper value?
AltEmail: input.AltEmail,
Status: "registered",
}

// Try to save it in the database
Expand All @@ -146,6 +150,9 @@ func AccountsCreate(w http.ResponseWriter, r *http.Request) {
return
} else if requestType == "verify" {
// We're pretty much checking whether an invitation code can be used by the user
input.Username = utils.RemoveDots(
utils.NormalizeUsername(input.Username),
)

// Fetch the user from database
account, err := env.Accounts.FindAccountByName(input.Username)
Expand Down Expand Up @@ -217,6 +224,9 @@ func AccountsCreate(w http.ResponseWriter, r *http.Request) {
} else if requestType == "setup" {
// User is setting the password in the setup wizard. This should be one of the first steps,
// as it's required for him to acquire an authentication token to configure their account.
input.Username = utils.RemoveDots(
utils.NormalizeUsername(input.Username),
)

// Fetch the user from database
account, err := env.Accounts.FindAccountByName(input.Username)
Expand Down Expand Up @@ -321,6 +331,10 @@ func AccountsCreate(w http.ResponseWriter, r *http.Request) {
Resource: models.MakeResource(account.ID, "Sent"),
Builtin: true,
},
&models.Label{
Resource: models.MakeResource(account.ID, "Drafts"),
Builtin: true,
},
&models.Label{
Resource: models.MakeResource(account.ID, "Trash"),
Builtin: true,
Expand Down
13 changes: 6 additions & 7 deletions routes/emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package routes
import (
//"bytes"
//"io"
"crypto/sha256"
"encoding/hex"
//"crypto/sha256"
//"encoding/hex"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -146,6 +146,8 @@ type EmailsCreateRequest struct {
Subject string `json:"subject"`
ContentType string `json:"content_type"`
ReplyTo string `json:"reply_to"`

SubjectHash string `json:"subject_hash"`
}

// EmailsCreateResponse contains the result of the EmailsCreate request.
Expand Down Expand Up @@ -272,11 +274,8 @@ func EmailsCreate(c web.C, w http.ResponseWriter, r *http.Request) {
})
return
}

}
} else {
hash := sha256.Sum256([]byte(input.Subject))

secure := "all"
if input.Kind == "raw" {
secure = "none"
Expand All @@ -288,7 +287,7 @@ func EmailsCreate(c web.C, w http.ResponseWriter, r *http.Request) {
Labels: []string{label.ID},
Members: append(append(input.To, input.CC...), input.BCC...),
IsRead: true,
SubjectHash: hex.EncodeToString(hash[:]),
SubjectHash: input.SubjectHash,
Secure: secure,
}

Expand All @@ -315,7 +314,7 @@ func EmailsCreate(c web.C, w http.ResponseWriter, r *http.Request) {
Kind: input.Kind,
Thread: input.Thread,

From: account.Name + "@" + env.Config.EmailDomain,
From: account.StyledName + "@" + env.Config.EmailDomain,
To: input.To,
CC: input.CC,
BCC: input.BCC,
Expand Down
17 changes: 14 additions & 3 deletions routes/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ type FilesListResponse struct {
func FilesList(c web.C, w http.ResponseWriter, r *http.Request) {
session := c.Env["token"].(*models.Token)

files, err := env.Files.GetOwnedBy(session.Owner)
query := r.URL.Query()
email := query.Get("email")
name := query.Get("name")

if email == "" || name == "" {
utils.JSONResponse(w, 400, &FilesListResponse{
Success: false,
Message: "No email or name in get params",
})
return
}

files, err := env.Files.GetInEmail(session.Owner, email, name)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
Expand Down Expand Up @@ -75,8 +87,7 @@ func FilesCreate(c web.C, w http.ResponseWriter, r *http.Request) {
session := c.Env["token"].(*models.Token)

// Ensure that the input data isn't empty
if input.Data == "" || input.Name == "" || input.Encoding == "" ||
input.PGPFingerprints == nil || len(input.PGPFingerprints) == 0 {
if input.Data == "" || input.Name == "" || input.Encoding == "" {
utils.JSONResponse(w, 400, &FilesCreateResponse{
Success: false,
Message: "Invalid request",
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions routes/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func KeysList(w http.ResponseWriter, r *http.Request) {
return
}

user = utils.RemoveDots(utils.NormalizeUsername(user))

account, err := env.Accounts.FindAccountByName(user)
if err != nil {
utils.JSONResponse(w, 409, &KeysListResponse{
Expand Down Expand Up @@ -214,6 +216,8 @@ func KeysGet(c web.C, w http.ResponseWriter, r *http.Request) {
// Who cares about the second part? I don't!
username := strings.Split(id, "@")[0]

username = utils.RemoveDots(utils.NormalizeUsername(username))

// Resolve account
account, err := env.Accounts.FindAccountByName(username)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions routes/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func TokensCreate(w http.ResponseWriter, r *http.Request) {
return
}

input.Username = utils.RemoveDots(
utils.NormalizeUsername(input.Username),
)

// Check if account exists
user, err := env.Accounts.FindAccountByName(input.Username)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func PrepareMux(flags *env.Flags) *web.Mux {
//Cache: redis,
}
env.Files = &db.FilesTable{
Emails: env.Emails,
RethinkCRUD: db.NewCRUDTable(
rethinkSession,
rethinkOpts.Database,
Expand Down
22 changes: 22 additions & 0 deletions utils/normalize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package utils

import (
"regexp"
"strings"
"unicode"
)

var (
rxNormalizeUsername = regexp.MustCompile(`[^\w\.]`)
)

func NormalizeUsername(input string) string {
return rxNormalizeUsername.ReplaceAllString(
strings.ToLowerSpecial(unicode.TurkishCase, input),
"",
)
}

func RemoveDots(input string) string {
return strings.Replace(input, ".", "", -1)
}