-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Very early NOTIFY support, hopefully fix -password on Windows
- Loading branch information
1 parent
4c07013
commit 63de5e8
Showing
8 changed files
with
141 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,40 @@ | ||
package imapserver | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
idle "github.com/emersion/go-imap-idle" | ||
"github.com/emersion/go-imap/server" | ||
"github.com/emersion/go-sasl" | ||
) | ||
|
||
type IMAPServer struct { | ||
server *server.Server | ||
backend *Backend | ||
} | ||
|
||
func NewIMAPServer(backend *Backend) (*IMAPServer, error) { | ||
func NewIMAPServer(backend *Backend, addr string, insecure bool) (*IMAPServer, *IMAPNotify, error) { | ||
s := &IMAPServer{ | ||
server: server.New(backend), | ||
backend: backend, | ||
} | ||
notify := NewIMAPNotify(s.server, backend.Log) | ||
s.server.Addr = addr | ||
s.server.AllowInsecureAuth = insecure | ||
s.server.Debug = os.Stdout | ||
s.server.Enable(idle.NewExtension()) | ||
return s, nil | ||
s.server.Enable(notify) | ||
s.server.EnableAuth(sasl.Login, func(conn server.Conn) sasl.Server { | ||
return sasl.NewLoginServer(func(username, password string) error { | ||
_, err := s.backend.Login(nil, username, password) | ||
return err | ||
}) | ||
}) | ||
go func() { | ||
if err := s.server.ListenAndServe(); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
return s, notify, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package imapserver | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/emersion/go-imap" | ||
"github.com/emersion/go-imap/server" | ||
) | ||
|
||
type IMAPNotifyHandler struct { | ||
imap.Command | ||
} | ||
|
||
func (h *IMAPNotifyHandler) Handle(conn server.Conn) error { | ||
// TODO: Support setting NOTIFY subscriptions or not | ||
return nil | ||
} | ||
|
||
type IMAPNotify struct { | ||
server *server.Server | ||
log *log.Logger | ||
} | ||
|
||
func (ext *IMAPNotify) Capabilities(c server.Conn) []string { | ||
if c.Context().State&imap.AuthenticatedState != 0 { | ||
return []string{"NOTIFY"} | ||
} | ||
return nil | ||
} | ||
|
||
func (ext *IMAPNotify) Command(name string) server.HandlerFactory { | ||
if name != "NOTIFY" { | ||
return nil | ||
} | ||
return func() server.Handler { | ||
return &IMAPNotifyHandler{} | ||
} | ||
} | ||
|
||
func (ext *IMAPNotify) NotifyNew(id, count int) error { | ||
ext.server.ForEachConn(func(c server.Conn) { | ||
var resptype imap.StatusRespType | ||
if mailbox := c.Context().Mailbox; mailbox != nil && mailbox.Name() == "INBOX" { | ||
resptype = imap.StatusRespType( | ||
fmt.Sprintf("EXISTS %d", id), | ||
) | ||
} else { | ||
resptype = imap.StatusRespType( | ||
fmt.Sprintf("STATUS INBOX (UIDNEXT %d MESSAGES %d)", id+1, count), | ||
) | ||
} | ||
_ = c.WriteResp(&imap.StatusResp{ | ||
Type: resptype, | ||
}) | ||
}) | ||
return nil | ||
} | ||
|
||
func NewIMAPNotify(s *server.Server, log *log.Logger) *IMAPNotify { | ||
return &IMAPNotify{ | ||
server: s, | ||
log: log, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters