diff options
author | 2019-12-02 19:53:09 +0100 | |
---|---|---|
committer | 2019-12-02 19:53:09 +0100 | |
commit | 25c63d05302fef11f79c30270a6e911da9010a38 (patch) | |
tree | 3dff86e0200dc6ac672093fb853acd095002203f /server.go | |
parent | fce17c9733eb38636603e8c508c2e7936426bf2c (diff) | |
download | alps-25c63d05302fef11f79c30270a6e911da9010a38.tar.gz alps-25c63d05302fef11f79c30270a6e911da9010a38.tar.bz2 alps-25c63d05302fef11f79c30270a6e911da9010a38.zip |
Add basic message view
Diffstat (limited to 'server.go')
-rw-r--r-- | server.go | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -4,6 +4,8 @@ import ( "fmt" "net/http" "net/url" + "strconv" + "strings" "time" "github.com/labstack/echo/v4" @@ -94,6 +96,34 @@ func handleLogin(ectx echo.Context) error { return ctx.Render(http.StatusOK, "login.html", nil) } +func parseUid(s string) (uint32, error) { + uid, err := strconv.ParseUint(s, 10, 32) + if err != nil { + return 0, err + } + if uid == 0 { + return 0, fmt.Errorf("UID must be non-zero") + } + return uint32(uid), nil +} + +func parsePartPath(s string) ([]int, error) { + l := strings.Split(s, ".") + path := make([]int, len(l)) + for i, s := range l { + var err error + path[i], err = strconv.Atoi(s) + if err != nil { + return nil, err + } + + if path[i] <= 0 { + return nil, fmt.Errorf("part num must be strictly positive") + } + } + return path, nil +} + func New(imapURL string) *echo.Echo { e := echo.New() @@ -149,11 +179,37 @@ func New(imapURL string) *echo.Echo { } return ctx.Render(http.StatusOK, "mailbox.html", map[string]interface{}{ + "Mailbox": ctx.conn.Mailbox(), "Mailboxes": mailboxes, "Messages": msgs, }) }) + e.GET("/message/:mbox/:uid", func(ectx echo.Context) error { + ctx := ectx.(*context) + + uid, err := parseUid(ctx.Param("uid")) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, err) + } + // TODO: handle messages without a text part + part, err := parsePartPath(ctx.QueryParam("part")) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, err) + } + + msg, body, err := getMessage(ctx.conn, ctx.Param("mbox"), uid, part) + if err != nil { + return err + } + + return ctx.Render(http.StatusOK, "message.html", map[string]interface{}{ + "Mailbox": ctx.conn.Mailbox(), + "Message": msg, + "Body": body, + }) + }) + e.GET("/login", handleLogin) e.POST("/login", handleLogin) |