idle

package module
v0.0.0-...-0663836 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2020 License: MIT Imports: 8 Imported by: 0

README

go-imap-idle

GoDoc

IDLE extension for go-imap.

Usage

Client
// Let's assume c is an IMAP client
var c *client.Client

// Select a mailbox
if _, err := c.Select("INBOX", false); err != nil {
	log.Fatal(err)
}

idleClient := idle.NewClient(c)

// Create a channel to receive mailbox updates
updates := make(chan client.Update)
c.Updates = updates

// Start idling
done := make(chan error, 1)
go func() {
	done <- idleClient.IdleWithFallback(nil, 0)
}()

// Listen for updates
for {
	select {
	case update := <-updates:
		log.Println("New update:", update)
	case err := <-done:
		if err != nil {
			log.Fatal(err)
		}
		log.Println("Not idling anymore")
		return
	}
}
Server
s.Enable(idle.NewExtension())

License

MIT

Documentation

Overview

Implements the IMAP IDLE extension, as defined in RFC 2177.

Index

Examples

Constants

View Source
const Capability = "IDLE"

The IDLE capability.

Variables

This section is empty.

Functions

func NewExtension

func NewExtension() server.Extension

Types

type Client

type Client struct {

	// LogoutTimeout is used to avoid being logged out by the server when idling.
	// Each LogoutTimeout, the idle command is restarted. If set to zero, this
	// behavior is disabled.
	LogoutTimeout time.Duration
	// contains filtered or unexported fields
}

Client is an IDLE client.

func NewClient

func NewClient(c *client.Client) *Client

NewClient creates a new client.

func (*Client) Idle

func (c *Client) Idle(stop <-chan struct{}) error

Idle indicates to the server that the client is ready to receive unsolicited mailbox update messages. When the client wants to send commands again, it must first close stop.

Example
// Let's assume c is an IMAP client
var c *client.Client

// Select a mailbox
if _, err := c.Select("INBOX", false); err != nil {
	log.Fatal(err)
}

idleClient := idle.NewClient(c)

// Create a channel to receive mailbox updates
updates := make(chan client.Update)
c.Updates = updates

// Check support for the IDLE extension
if ok, err := idleClient.SupportIdle(); err == nil && ok {
	// Start idling
	stopped := false
	stop := make(chan struct{})
	done := make(chan error, 1)
	go func() {
		done <- idleClient.Idle(stop)
	}()

	// Listen for updates
	for {
		select {
		case update := <-updates:
			log.Println("New update:", update)
			if !stopped {
				close(stop)
				stopped = true
			}
		case err := <-done:
			if err != nil {
				log.Fatal(err)
			}
			log.Println("Not idling anymore")
			return
		}
	}
} else {
	// Fallback: call periodically c.Noop()
}
Output:

func (*Client) IdleWithFallback

func (c *Client) IdleWithFallback(stop <-chan struct{}, pollInterval time.Duration) error

IdleWithFallback tries to idle if the server supports it. If it doesn't, it falls back to polling. If pollInterval is zero, a sensible default will be used.

Example
// Let's assume c is an IMAP client
var c *client.Client

// Select a mailbox
if _, err := c.Select("INBOX", false); err != nil {
	log.Fatal(err)
}

idleClient := idle.NewClient(c)

// Create a channel to receive mailbox updates
updates := make(chan client.Update)
c.Updates = updates

// Start idling
done := make(chan error, 1)
go func() {
	done <- idleClient.IdleWithFallback(nil, 0)
}()

// Listen for updates
for {
	select {
	case update := <-updates:
		log.Println("New update:", update)
	case err := <-done:
		if err != nil {
			log.Fatal(err)
		}
		log.Println("Not idling anymore")
		return
	}
}
Output:

func (*Client) SupportIdle

func (c *Client) SupportIdle() (bool, error)

SupportIdle checks if the server supports the IDLE extension.

type Command

type Command struct{}

An IDLE command. Se RFC 2177 section 3.

func (*Command) Command

func (cmd *Command) Command() *imap.Command

func (*Command) Parse

func (cmd *Command) Parse(fields []interface{}) error

type Handler

type Handler struct {
	Command
}

func (*Handler) Handle

func (h *Handler) Handle(conn server.Conn) error

type IdleClient

type IdleClient = Client

IdleClient is an alias used to compose multiple client extensions.

type Response

type Response struct {
	RepliesCh chan []byte
	Stop      <-chan struct{}
	// contains filtered or unexported fields
}

An IDLE response.

func (*Response) Handle

func (r *Response) Handle(resp imap.Resp) error

func (*Response) Replies

func (r *Response) Replies() <-chan []byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL