imapmq

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

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

Go to latest
Published: Nov 4, 2016 License: MIT Imports: 11 Imported by: 8

README

IMAPMQ is an IMAP based message broker client. It provides a simple interface for publishing, subscribing and dequeuing messages. It also supports concurrent access to the same message queue. Based on go-imap.

Go Report Card GoDoc

How it works

IMAPMQ treats IMAP mailboxes as queues. In order to add a message to a queue, IMAPMQ appends an email to the mailbox.

Features

  • IMAPMQ can connect to any IMAPv4rev1 server with the CONDSTORE extension
  • Publish/Subscribe
  • Message Queue
  • Message format agnostic
  • No polling, the IMAP server notifies the client of new messages thanks to the IDLE command
  • Concurrency aware: multiple dequeuing instances can work on the same queue
  • Bring your own GUI: any IMAP client would do

Installing

$ go get github.com/mikaa123/imapmq

Example: A simple chat

The following example connects to an IMAP account, and creates a queue based on the INBOX mailbox. It spawns a goroutine that subscribes to the "chat" topic and listens to the returned channel. Anytime a user writes something and press enter, a new "chat" message is published to the queue.

You need to have an IMAP server to connect to. If you don't, you can create a gmail account. Make sure you enable IMAP (more info here) if you do.

package main

import (
	"bufio"
	"log"
	"os"

	"github.com/mikaa123/imapmq"
)

func main() {
	// Create a new IMAPMQ client
	mq, err := imapmq.New(imapmq.Config{
		Login: "login",
		Passwd: "password",
		URL: "imap.gmail.com",
	})
	if err != nil {
		log.Panic(err)
	}
	defer mq.Close()

	// Create a queue based on INBOX
	q, err := mq.Queue("INBOX")
	if err != nil {
		log.Panic(err)
	}

	go func() {
		// Subscribe to messages with the "chat" subject
		c := q.Sub("chat")
		for msg := range c { // msg is a mail.Message instance.
			log.Printf("%s", msg.Header.Get("Subject"))
		}
	}()

	// We scan stdin for user input
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		// Publish a message with the "chat" subject in the INBOX queue
		q.Pub("chat", []byte(scanner.Text()))
	}
}

Documentation

https://godoc.org/github.com/mikaa123/imapmq

License

MIT © Michael Sokol

Documentation

Overview

Package imapmq is an IMAP based message broker client.

Index

Constants

This section is empty.

Variables

View Source
var ErrParseMail = errors.New("mail parse error")

ErrParseMail is returned when the mail returned by IMAP server couldn't be parsed correctly.

Functions

This section is empty.

Types

type Config

type Config struct {
	Login, Passwd, URL string
}

Config holds configuration data to connect to the IMAP server.

type IMAPMQ

type IMAPMQ struct {
	// contains filtered or unexported fields
}

IMAPMQ is an imapmq broker client. It manages queues and coordinate low-level operations through a worker that performs operations requested by queues synchronously.

func New

func New(cfg Config) (*IMAPMQ, error)

New creates a IMAPMQ instance. It initializes the instance's worker.

func (*IMAPMQ) Close

func (mq *IMAPMQ) Close()

Close disconnects the IMAPMQ client and releases its worker and observers.

func (*IMAPMQ) Errs

func (mq *IMAPMQ) Errs() <-chan error

Errs return the error channel. Any asynchronous task will send an error down this channel.

func (*IMAPMQ) Queue

func (mq *IMAPMQ) Queue(mailbox string) (*Queue, error)

Queue creates or retrieve a `Queue` instance based on the mailbox name passed.

type Message

type Message mail.Message

Message type is an email. Queues (mailboxes) store messages. Messages will be passed when listening to a subscription channel, or when dequeuing.

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue represents a message queue based on a mailbox. A queue allows you to publish, subscribe or dequeue `Message` (emails).

func (*Queue) Dequeue

func (q *Queue) Dequeue() (*Message, error)

Dequeue fetches and removes the oldest message from the queue. When no more messages are available, it returnes io.EOF.

func (*Queue) Pub

func (q *Queue) Pub(subject string, body []byte)

Pub publishes the `Message` to the queue. It appends a new email in the queue's mailbox by using the provided subject and body.

func (*Queue) Sub

func (q *Queue) Sub(s string) <-chan *Message

Sub adds a subscription to a subject on the queue. Use "*" to receive every new message from the queue. Returns a channel where the messages will be passed.

Jump to

Keyboard shortcuts

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