mailutils, SDK.

version 0.0.9b, 14 October 2001

Alain Magloire et al.


Table of Contents


Introduction

GNU MAILUTILS offers a general purpose library whose aim is to provide a rich set of functions for accessing different mailbox formats and mailers.

References

For more information on,

Concrete API

POP3

/* Prefix pop3_ is reserved */
#include <mailutils/pop3.h>

The purpose of the Post Office Protocol Version 3 (POP3) is to permit a client to download a maildrop from a remote server. It does not provide complex or extensive operation on the maildrop. When the client successfully authenticates, the server acquires an exclusive access lock on the mailbox and holds it the entire duration of the session. After the authentication, the session enters transaction state and the client may issues commands to access messages in the mailbox.

When the command Quit is issue the session enters the update state. The servers removes all messages marked deleted, releases the exclusive lock and closes the TCP connection.

Commands

An opaque structure pop3_t is use as a handle for the session, it is allocated and initialized by calling pop3_create (). All Functions will wait for a reply from the POP3 server before returning. The duration of the wait can be set by calling pop3_set_timeout (), the default is 10 minutes(1). Once a successful connection is established with pop3_connect (), two builtins authentications are provided pop3_apop () or pop3_user ()/pop3_pass (). The pop3_stat () and pop3_list () functions can be use to get the number and size of messages. Downloading of messages is done via a stream provided by pop3_retr () or pop3_top ()(2). The stream_t should be destroyed to indicate to the library that the action is finished. POP3 only provide a single channel for operation, it means only one operation can be done at a time, all the functions will return MU_ERROR_OPERATION_IN_PROGRESS if call during another operation. The functions pop3_list_all (), pop3_uidl_all () and pop3_capa () return iterators pop3_list_current (), pop3_uidl_current () are provided as cover function to format the string return by iterator_current (), iterator_destroy () must be call to release any resources.

In a multithreaded application, only one thread should access pop3_t handles.

Initialization

Function: int pop3_create (pop3_t * pop3)

Allocate and initialize a pop3 handle.

MU_ERROR_NO_MEMORY
MU_ERROR_INVALID_PARAMETER

Function: void pop3_destroy (pop3_t *pop3)

When a POP3 session is finished, the structure must be free ()'ed to reclaim memory.

Function: int pop3_connect (pop3_t, const char *host, unsigned port, int flags)

A connection is established by calling pop3d_open (), the previous connection is close first. If non-blocking the function should be recalled until the return value is not MU_ERROR_TRY_AGAIN or MU_ERROR_IN_PROGRESS.

MU_ERROR_NO_MEMORY
MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_IN_PROGRESS
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED
MU_ERROR_TIMEOUT
MU_ERROR_NO_LOCK

Carrier

Function: int pop3_set_carrier (pop3_t, stream_t carrier);

The type of stream use to contact as server will be set to carrier in the pop3_t handle. Any previous carrier stream in the handle, will be close and release.

MU_ERROR_INVALID_PARAMETER

Function: int pop3_get_carrier (pop3_t, stream_t *carrier);

Return the pop3_t carrier. If none was set, a new tcp stream will be created.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_NO_MEMORY

Apop

Function: int pop3_apop (pop3_t, const char *user, const char *secret)

Apop offers an alternative to User/Pass authentication. For intermittent use of POP3, like checking for new mail, it is the preferred the authentication. It reduces the risk of password capture. The user and the shared secret are pass to pop3_apop (), the MD5 digest is calculated by applying the times tamp given by the server in the greeting followed by the secret.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Capa

Function: int pop3_capa (pop3_t, pop3_capa_iterator_t *iterator)

The Capa command is send to the sever and the list of capabilities is return in an iterator. iterator_current () gives an allocated string that should be free ()'ed. Caution: The iterator must be destroy after use, it will discard any remaining responses from CAPA and clear the way for new operations.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED
#include <stdio.h>
#include <stdlib.h>
#include <mailutils/pop3.h>

void print_capabilities (pop3_t pop3)
{
   iterator_t iterator;
   status = pop3_capa (pop3, &iterator);
   if (status == 0)
    {
        for (iterator_first (iterator);
             !iterator_is_done (iterator);
             iterator_next (iterator))
          {
              char *capa;
              if (iterator_current (iterator, &capa) == 0)
               {;
                   printf ("CAPA: %s\n", capa);
                   free (capa);
               }
          }
        pop3_capa_destroy (&iterator);
    }
   else
     printf ("NONE\n");
}

Dele

Function: int pop3_dele (pop3_t, unsigned msgno)

Sends a Dele to the servers who will mark the msgno for deletion. The msgno may not refer to a message already marked deleted. If successful any future reference to msgno in other operations will be an error, unless unmarked by RSET.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

List

Function: int pop3_list (pop3_t, unsigned msgno, size_t *size)

Sends a List for a specific msgno and returns the size.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Function: int pop3_list_all (pop3_t, iterator_t *iterator)

Sends A List with no argument to the server. The iterator must be destroy after use, it will discard any remaining response from LIST and clear the way for new operations. A cover function pop3_list_current () around to scan properly the string return by the iterator_current ().

#include <stdio.h>
#include <stdlib.h>
#include <mailutils/pop3.h>

void print_list (pop3_t pop3)
{
   iterator_t iterator;
   status = pop3_list_all (pop3, &iterator);
   if (status == 0)
    {
        for (iterator_first (iterator);
             !iterator_is_done (iterator);
             iterator_next (iterator))
          {
              unsigned int msgno, size;
              if (pop3_list_current (iterator, &msgno, &size) == 0)
               {
                   printf ("LIST: %d %d\n", msgno, size);
               }
          }
        iterator (&iterator);
    }
   else
     printf ("NONE\n");
}
MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Function: int pop3_list_current (pop3_t, unsigned int *msgno, size_t *size)

Cover functions around iterator_current () from an iterator created by pop3_list_all () to format the result.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Noop

Function: int pop3_noop (pop3_t)

Sends a NOOP, useful to reset the timeout on the server.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Pass

Function: int pop3_pass (pop3_t, const char *passwd)

Sends the PASS, to authenticate after the USER command.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Quit

Function: int pop3_quit (pop3_t)

Enter the UPDATE state. The server will delete all messages marked deleted before closing the connection.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Retr

Function: int pop3_retr (pop3_t, unsigned msgno, stream_t *)

If successful a stream_t is created to allow downloading of the message, byte-stuff lines or handle internally, CRLFs are converted to LF. All other operations will fail until the stream is destroyed by the caller.

#include <stdio.h>
#include <mailutils/pop3.h>

int
print_message (pop3_t pop3, unsigned int msgno)
{
   stream_t stream;
   int status = pop3_retr (pop3, msgno, &stream);
   if (status == 0)
    {
       size_t n = 0;
       char buf[128];
       while ((stream_readline (stream, buf, sizeof buf, &n) == 0)
              && n)
         printf ("%s", buf);
       stream_release (stream);
    }
   return status;
}
MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Rset

Function: int pop3_rset (pop3_t)

Sends a RSET to unmark the messages marked as deleted.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Stat

Function: int pop3_stat (pop3_t, unsigned msgno, unsigned *msg_count, size_t *size)

The number of messages in the mailbox and the size of the mailbox in octets. Caution: The size is in RFC822 where line termination is CRLF, messages marked as deleted are not counted in either total.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Top

Function: int pop3_top (pop3_t, unsigned int msgno, unsigned int lines, stream_t *stream)

If successful a stream is created to allow downloading of the header, byte-stuff lines or handle internally, CRLFs are converted to LF. All other operations will failed until the stream is destroyed by the caller.

#include <stdio.h>
#include <mailutils/pop3.h>

int
print_top (pop3_t pop3, unsigned int msgno, unsigned int lines)
{
    stream_t stream;
    int status = pop3_top (pop3, msgno, lines, &stream);
    if (status == 0)
     {
        size_t n = 0;
        char buf[128];
        while ((stream_readline (stream, buf, sizeof buf, &n) == 0)
               && n)
          printf ("%s", buf);
        stream_release (stream);
     }
  return status;
}
MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Uidl

Function: int pop3_uidl (pop3_t, unsigned int msgno, char **uidl)

The Uniq Identifier is return in uidl, the string must be free ()'ed, by the caller.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Function: int pop3_uidl_all (pop3_t, iterator_t * iterator)

An iterator object is return to iterate through the response and must be destroyed by the caller.

#include <stdio.h>
#include <stdlib.h>
#include <mailutils/pop3.h>

void print_uidl (pop3_t pop3)
{
   iterator_t iterator;
   status = pop3_uidl_all (pop3, &iterator);
   if (status == 0)
    {
        for (iterator_first (iterator);
             !iterator_is_done (iterator);
             iterator_next (iterator))
          {
              unsigned int msgno;
              char *uidl;
              if (pop3_uidl_current (iterator, &msgno, &uidl) == 0)
               {
                   printf ("LIST: %d %s\n", msgno, uidl);
                   free (uidl);
               }
          }
        iterator (&iterator);
    }
   else
     printf ("NONE\n");
}
MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Function: int pop3_uidl_current (iterator_t iterator, unsigned int *msgno, char **uidl)

Cover functions around iterator_current () from an iterator created by pop3_uidl_all () to format the result. The variable uidl should be free ()'ed.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

User

Function: int pop3_user (pop3_t, const char *user)

Sends the User command.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Help functions

Function: int pop3_writeline (pop3_t, const char *format, ...);

Copy in the internal buffer of pop3_t the string, pop3_send () should be called later to flush the string to the POP3 server.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Function: int pop3_sendline (pop3_t, const char *cmd);

Cover function for pop3_writeline () and pop3_send ().

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Function: int pop3_send (pop3_t, const char *cmd);

Flushes out the strings written by pop3_writeline () in the internal buffer to the channel.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Function: int pop3_response (pop3_t, char *buffer, size_t len, size_t *plen)

The last response from the last command is save and can be examine after a failure or success.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_IO
MU_ERROR_TIMEOUT
MU_ERROR_TRY_AGAIN
MU_ERROR_OPERATION_DENIED

Timeout

Function: int pop3_set_timeout (pop3_t, int timeout)

Set the timeout time for I/O on the carrier. The default is 10 minutes. The timeout is given in milliseconds.

Function: int pop3_get_timeout (pop3_t, int *timeout)

Get the timeout time for I/O on the carrier. The timeout is given in milliseconds.

IMAP4

/* Prefix imap4_ is reserved */
#include <mailutils/imap4.h>

Internet Message Access Protocol - Version (4rev1). Not implemented.

Commands

Initialization

Function: int imap4_create (imap4_t *)

Function: int imap4_open (imap4_t, const char *hostname, unsigned int port, int flags)

Function: int imap4d_set_timeout (imap4_t, unsigned int seconds)

Append

Function: int imap4_append (imap4_t)

Capability

Function: int imap4_capability (imap4_t)

Create

Function: int imap4_create_mailbox (imap4_t, const char *mbox)

Check

Function: int imap4_check (imap4_t)

Close

Function: int imap4_close (imap4_t)

Copy

Function: int imap4_copy (imap4_t)

UID Copy

Function: int imap4_uid_copy (imap4_t)

Delete

Function: int imap4_delete (imap4_t)

Fetch

Function: int imap4_fetch (imap4_t)

UID Fetch

Function: int imap4_uid_fetch (imap4_t)

Examine

Function: int imap4_examine (imap4_t)

Expunge

Function: int imap4_expunge (imap4_t)

List

Function: int imap4_list (imap4_t)

Lsub

Function: int imap4_lsub (imap4_t)

Namespace

Function: int imap4_namespace (imap4_t)

Rename

Function: int imap4_rename (imap4_t)

Search

Function: int imap4_search (imap4_t)

UID Search

Function: int imap4_uid_search (imap4_t)

Select

Function: int imap4_select (imap4_t)

Status

Function: int imap4_status (imap4_t)

Store

Function: int imap4_store (imap4_t)

UID Store

Function: int imap4_uid_store (imap4_t)

Subscribe

Function: int imap4_subscribe (imap4_t)

Unsubscribe

Function: int imap4_unsubscribe (imap4_t)

Mbox

/* Prefix mbox_ is reserved */
#include <mailutils/mbox.h>

The most common mailbox format on UNIX platform is mbox. Mbox file is messages separated by the special format string.

   From SP envelope-sender SP date [SP moreinfo]
"From "
is sometimes call the From_.
envelope-sender
is a word with no space.
date
has the same format as asctime ()
moreinfo
are optional values that are seldom used.

A mbox_t is created, initialized and destroyed by mbox_create () and mbox_destroy (). When opening, mbox_open () will do a quick check to see if the format is a valid format or an empty file. The scanning of the mailbox is done by mbox_scan (). The function, mbox_scan (), takes callback functions called during the scanning to provide information. The scanning will cache some of the headers fields for speed. Closing the mailbox, mbox_close () will free any resources like, headers cache, locks etc ... All the messages with attributes marked deleted will only be removed on mbox_expunge (). If only the attributes need to be save but the messages not removed, this can be done by mbox_save_attributes (). New messages are added with mbox_append (). Attributes are saved in the Status: header field, Read is 'R', Seen is 'O', Deleted is 'd' and Reply is 'r'.

Initialization

Function: int mbox_create (mbox_t *mbox)

Allocate and initialize a mbox handle.

MU_ERROR_NO_MEMORY
MU_ERROR_INVALID_PARAMETER

Function: void mbox_destroy (mbox_t mbox)

When a POP3 session is finished, the structure must be free ()'ed to reclaim memory.

Carrier

Function: int mbox_set_carrier (mbox_t, stream_t carrier);

Another type of stream can be provided to work on, the carrier is set in the mbox_t handle. Any previous carrier stream in the handle, will be close and release. Since the parsing code maintain only the offsets off the message the carrier stream must be seekable.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_get_carrier (mbox_t, stream_t *carrier);

Return the mbox_t carrier. If none was set, a new file stream will be created.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_NO_MEMORY

Function: int mbox_open (mbox_t, const char *filename, int flags)

Open carrier stream with filename and flags. The stream will be quickly examine to see if it is a mbox format.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_NO_MEMORY
MU_ERROR_NO_ENTRY
MU_ERROR_NO_ACCESS
MU_ERROR_NOT_SUPPORTED

Function: int mbox_close (mbox_t)

Close the carrier stream and resources particular to the mailbox.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_NO_MEMORY

Function: int mbox_uidnext (mbox_t, unsigned long *uidnext)

Return the uidnext, if the mbox_t was not scan mbox_scan () is called first.

MU_ERROR_INVALID_PARAMETER
same as mbox_scan ()

Function: int mbox_uidvalidity (mbox_t, unsigned long *uidvalidity)

Return the uidvalidity, if the mbox_t was not scan mbox_scan () is called first.

MU_ERROR_INVALID_PARAMETER
same as mbox_scan ()

Function: int mbox_get_attribute (mbox_t, unsigned int msgno, attribute_t *attribute)

Return an attribute to indicate the status of message number msgno.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_NO_MEMORY

Function: int mbox_get_separator (mbox_t, unsigned int msgno, char **sep)

Return an allocated string in sep containing the value "From " separating each message in Unix mbox format. The string should be free ()ed by the caller.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_NO_MEMORY

Function: int mbox_set_separator (mbox_t, unsigned int msgno, const char *sep)

The variable sep should contain a valid "From " separator that will be use when the expunging.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_NO_MEMORY

Function: int mbox_get_hstream (mbox_t, unsigned int msgno, stream_t *stream)

Return a stream to read the header of message msgno. The stream should be destroy after usage.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_NO_MEMORY

Function: int mbox_set_hstream (mbox_t, unsigned int msgno, stream_t stream)

Use stream when expunging for message msgno.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_set_hsize (mbox_t, unsigned int msgno, unsigned int *size)

Return the size of message msgno.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_set_hlines (mbox_t, unsigned int msgno, unsigned int *size)

Return the number of lines of message msgno.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_get_bstream (mbox_t, unsigned int msgno, stream_t *stream)

Return a stream to read the body of message msgno. The stream should be destroy after usage.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_NO_MEMORY

Function: int mbox_set_bstream (mbox_t, unsigned int msgno, stream_t stream)

Use stream when expunging for message msgno.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_set_bsize (mbox_t, unsigned int msgno, unsigned int *size)

Return the size of message msgno.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_set_blines (mbox_t, unsigned int msgno, unsigned int *size)

Return the number of lines of message msgno.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_get_size (mbox_t, unsigned int *size)

Return the size of mailbox.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_save (mbox_t)

Save the changes to the messages back to the mailbox, but do not remove messages mark for deletion in the process.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_mak_deleted (mbox_t, unsigned int msgno)

Mark msgno for deletion.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_unmak_deleted (mbox_t, unsigned int msgno)

Unmark msgno if it was marked for deletion.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_expunge (mbox_t)

Save the changes to the mailbox and in the process remove all messages marked for deletion.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_append (mbox_t, const char *sep, stream_t stream)

Append to the mailbox an rfc822 message represented by stream. The variable sep should contain a valid "From " separator or NULL to get the default.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_append_hb (mbox_t, const char *sep, stream_t hstream, stream_t bstream)

Append to the mailbox an rfc822 message represented by a header, hstream, and a body, bstream. The variable sep should contain a valid "From " separator or NULL to get the default.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_scan (mbox_t, unsigned int start, unsigned int *count)

Start scanning the mailbox for new messages. The variable start can be a message number starting point. The result of the scanning will be in count. The scanning will trigger the mbox_newmsg_cb() callback for each new message and mbox_progress_cb () at different interval to notify progression. The return values of the those callback should be 0 is different then 0 the scanning will be stop an the function returns MU_ERROR_INTERRUPTED.

MU_ERROR_INVALID_PARAMETER
MU_ERROR_INTERRUPTED
MU_ERROR_NO_MEMORY

Function: int mbox_set_progress_cb (mbox_t, int (*callback) (int, void *)), void *arg)

Set the callback function for progress. The variable arg will be pass back in the callback as the second argument.

MU_ERROR_INVALID_PARAMETER

Function: int mbox_set_newmsg_cb (mbox_t, int (*callback) (int, void *)), void *arg)

Set the callback function for new messages. The variable arg will be pass back in the callback as the second argument.

MU_ERROR_INVALID_PARAMETER

Mh

/* Prefix mh_ is reserved */
#include <mailutils/mh.h>

Mail Handler mailbox format. Not implemented.

Maildir

/* Prefix maildir_ is reserved */
#include <mailutils/maildir.h>

QMail mailbox format. Not implemented.

SMTP

/* Prefix smtp_ is reserved */
#include <mailutils/smtp.h>

Simple Mail Transfer Protocol. Not implemented.

Commands

Initialization

Function: int smtp_create (smtp_t *)

Function: void smtp_destroy (smtp_t *)

Function: int smtp_open (smtp_t, const char *host, unsigned int port, int flags)

Data

Function: int smtp_data (smtp_t, stream_t stream)

Helo

Function: int smtp_helo (smtp_t, const char *domain)

Function: int smtp_ehlo (smtp_t, const char *domain)

Expn

Function: int smtp_expn (smtp_t, const char *list, iterator_t *)

Help

Function: int smtp_help (smtp_t, const char *help, iterator_t *)

Mail From

Function: int smtp_mail_from (smtp_t, const char *address, const char *param)

Noop

Function: int smtp_noop (smtp_t)

Quit

Function: int smtp_quit (smtp_t)

Recpt To

Function: int smtp_rcpt_to (smtp_t, const char *address, const char *param)

Reset

Function: int smtp_reset (smtp_t)

Verify

Function: int smtp_verify (smtp_t, const char *user)

Help functions

Function: extern int smtp_readline (smtp_t, char *buffer, size_t len, size_t *len)

Function: extern int smtp_response (smtp_t, char *buffer, size_t len, size_t *len)

Function: extern int smtp_writeline (smtp_t, const char *format, ...)

Function: extern int smtp_sendline (smtp_t, const char *line)

Function: extern int smtp_send (smtp_t

Sendmail

/* Prefix sendmail_ is reserved */
#include <mailutils/sendmail.h>

Spawning Sendmail daemon to deliver mail. Not implemented.

NNTP

/* Prefix nntp_ is reserved */
#include <mailutils/nntp.h>

Network News Transfer Protocol. Not implemented.

Commands

Initialization

Function: int nntp_create (nnpt_t *)

Function: int nntp_destroy (nnpt_t *)

Function: int nntp_open (nnpt_t)

Article

Function: int nntp_article (nnpt_t)

Body

Function: int nntp_body (nntp_t)

Group

Function: int nntp_group (nntp_t)

Head

Function: int nntp_head (nntp_t)

Help

Function: int nntp_help (nntp_t)

IHave

Function: int nntp_ihave (nntp_t)

Last

Function: int nntp_last (nntp_t)

List

Function: int nntp_list (nntp_t)

NewGroups

Function: int nntp_newgroups (nntp_t)

NewNews

Function: int nntp_newnews (nntp_t)

Next

Function: int nntp_next (nntp_t)

Post

Function: int nntp_post (nntp_t)

Quit

Function: int nntp_quit (nntp_t)

Slave

Function: int nntp_slave (nntp_t)

Stat

Function: int nntp_stat (nntp_t)

Parse822

/* Prefix parse822_ is reserved */
#include <mailutils/parse822.h>

Internet Message Format, see Address node for the discussion.

Function: int parse822_address_list (address_t* a, const char* s)

Function: int parse822_mail_box (const char** p, const char* e, address_t* a)

Function: int parse822_group (const char** p, const char* e, address_t* a)

Function: int parse822_address (const char** p, const char* e, address_t* a)

Function: int parse822_route_addr (const char** p, const char* e, address_t* a)

Function: int parse822_route (const char** p, const char* e, char** route)

Function: int parse822_addr_spec (const char** p, const char* e, address_t* a)

Function: int parse822_unix_mbox (const char** p, const char* e, address_t* a)

Function: int parse822_local_part (const char** p, const char* e, char** local_part)

Function: int parse822_domain (const char** p, const char* e, char** domain)

Function: int parse822_sub_domain (const char** p, const char* e, char** sub_domain)

Function: int parse822_domain_ref (const char** p, const char* e, char** domain_ref)

Function: int parse822_domain_literal (const char** p, const char* e, char** domain_literal)

Function: int parse822_quote_string (char** quoted, const char* raw)

Function: int parse822_quote_local_part (char** quoted, const char* raw)

Function: int parse822_field_body (const char** p, const char *e, char** fieldbody)

Function: int parse822_field_name (const char** p, const char *e, char** fieldname)

Framework

Wherever the mail is and whatever format it is stored in, the same operations to manipulate emails are common. To unified the C API, GNU mailutils offers a heteroclite set of objects that work in aggregation to do operations on emails. Each object do a specific task and delegates non related tasks to others. The object comes alive by specifying a URL parameter when created, it will indicate the storage format or protocol (POP3, IMAP4, MH, MAILDIR, etc ..).


                          folder_t                  url_t
  -/var/mail-    +- .. ->+-----------------+   +-->+------------+
 (  alain *-)-+  |       |   url_t       *-|---+   |   port     |
  ----------- |  |       |-----------------|       |   hostname |
 (  jakob *-)-+--+       |   auth_t      *-|---+   |   file     |
  ----------- |          |-----------------|   |   |   ...      |
 (  jeff  *-)-+          |   stream_t      |   |   +------------+
  ----------- |          |-----------------|   |
 (  shaleh*-)-+          |   .....         |   |    auth_t
  ----------             |-----------------|   +-->+------------+
                     +---|-* mailbox_t[]   |       |   ticket_t |
  mailbox_t          |   +-----------------+       +------------+
 +----------------+<-+
 |   locker_t  *--|-------------+
 |----------------|             |
 |   url_t        |             |          locker_t
 |----------------|             +-------->+---------+
 |   stream_t     |                       | lock    |
 |----------------|                       | unlock  |
 |  message_t[] *-|-------+               +---------+
 +----------------+       |                     envelope_t
                          |          +-------->+-----------+
  message_t               |          |         | date      |
 +----------------+<------+          |         | from      |
 |   envelope_t *-|------------------+         | to        |
 |----------------|              header_t      +-----------+
 |   header_t   *-|------------>+--------------+
 |----------------|             |   stream_t   |
 |   body_t     *-|----+        +--------------+
 +----------------+    |    body_t
                       +-->+--------------+
                           |   stream_t   |
                           +--------------+

For example writing a simple from command that will list the From and Subject headers of every mail in a mailbox.

/* sfrom, Simple From */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/registrar.h>
#include <mailutils/list.h>
#include <mailutils/mailbox.h>

#define BUFFER_SIZE 64
int
main (int argc, const char **argv)
{
   char from[BUFFER_SIZE];
   char subject[BUFFER_SIZE];
   char *mail;
   mailbox_t mbox;
   int status
   size_t msgno, total = 0;

   mail = (argc == 2) ? argv[1] : getenv ("MAIL");

   /* Register the type of mailbox. IMAP4, POP3 and local format  */
   {
       list_t registrar;
       registrar_get_list (&registrar);
       list_append (registrar, imap_record);
       list_append (registrar, path_record);
       list_append (registrar, pop_record);
   }

   status = mailbox_create (&mbox, mail);
   if (status != 0)
     {
        fprintf (stderr, "mailbox_create: %s\n", strerror (status));
        exit (EXIT_FAILURE);
     }

   status = mailbox_open (mbox, MU_STREAM_READ);
   if (status != 0)
     {
        fprintf (stderr, "mailbox_open: %s\n", strerror (status));
        exit (EXIT_FAILURE);
     }

   mailbox_messages_count (mbox, &total);

   for (msgno = 1; msgno <= total; msgno++)
      {
         message_t msg;
         header_t hdr;

         if ((status = mailbox_get_message (mbox, msgno, &msg)) != 0
             || (status = message_get_header (msg, &hdr)) != 0)
           {
               fprintf (stderr, "Error message:%s\n",
                        strerror (status));
               exit (EXIT_FAILURE);
           }

         status = header_get_value (hdr, MU_HEADER_FROM, from,
                                    sizeof (from), NULL);
         if (status != 0)
            strcpy (from, "(NO FROM)");

         status = header_get_value (hdr, MU_HEADER_SUBJECT, subject,
                                    sizeof (subject), NULL);
         if (status != 0)
            strcpy (subject, "(NO SUBJECT)");

         printf ("%s\t%s\n", from, subject);
      }

    mailbox_close (mbox);
    mailbox_destroy (&mbox);
    return 0;
}
% MAIL=pop://alain@localhost ./sfrom
Passwd: xxxx
Jim Meyering <meyering@foo.org>      fetish(shellutils) beta
Fran@,{c}ois Pinard <pinard@bar.org> recode new alpha
...

Folder

/* Prefix folder_ is reserve */
#include <mailutils/folder.h>

                             folder_t                  url_t
  -/var/mail-    +---//---->/-----------------\    +-->/-----------\
 (  alain *-)-+  |          |  url_t        *-|----+   |  port     |
  ----------- |  |          |-----------------+        |  hostname |
 (  jakob *-)-+--+          |  observer_t   *-|        |  file     |
  ----------- |             |-----------------+        |  ...      |
 (  jeff  *-)-+             |  stream_t       |        \-----------/
  ----------- |             |-----------------|
 (  sean  *-)-+             |  auth_t         |
  ----------                |-----------------|
                            |  mailbox_t(1)   |
                            |-----------------|
                            |  mailbox_t(2)   |
                            |  ......         |
                            |  mailbox_t(n)   |
                            \-----------------/

Function: int folder_create (folder_t *, const char *url)

Function: void folder_destroy (folder_t *)

Function: int folder_open (folder_t, int flag)

Function: int folder_close (folder_t)

Function: int folder_delete (folder_t, const char *mailbox)

Function: int folder_rename (folder_t, const char *, const char *mailbox)

Function: int folder_subscribe (folder_t, const char *mailbox)

Function: int folder_unsubscribe (folder_t, const char *mailbox)

Function: int folder_list (folder_t, const char *ref, const char *wcard, iterator_t *)

Function: int folder_lsub (folder_t, const char *ref, const char *wcar, iterator_t *)

Function: int folder_get_stream (folder_t, stream_t *)

Function: int folder_set_stream (folder_t, stream_t)

Function: int folder_get_observable (folder_t, observable_t *)

Function: int folder_get_debug (folder_t, debug_t *)

Function: int folder_set_debug (folder_t, debug_t)

Function: int folder_get_authority (folder_t, authority_t *)

Function: int folder_set_authority (folder_t, authority_t)

Function: int folder_get_url (folder_t, url_t *)

Function: int folder_set_url (folder_t, url_t)

Mailbox

/* Prefix mailbox_ is reserved */
#include <mailutils/mailbox.h>

Data Type: mailbox_t
The mailbox_t object is used to hold information and it is an opaque data structure to the user. Functions are provided to retrieve information from the data structure.
                             mailbox_t                  url_t
  -/var/mail-    +---//---->/-----------------\    +-->/-----------\
 (  alain   )    |          |  url_t        *-|----+   |  port     |
  -----------    |          |-----------------+        |  hostname |
 (  jakob *-)----+          |  observer_t   *-|        |  file     |
  -----------               |-----------------+        |  ...      |
 (  jeff    )               |  stream_t       |        \-----------/
  -----------               |-----------------|
 (  sean    )               |  locker_t       |
  ----------                |-----------------|
                            |  message_t(1)   |
                            |-----------------|
                            |  message_t(2)   |
                            |  ......         |
                            |  message_t(n)   |
                            \-----------------/

Function: int mailbox_append_message (mailbox_t mbox, message_t message)
The message is appended to the mailbox mbox.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null or message is invalid.

Function: int mailbox_close (mailbox_t mbox)
The stream attach to mbox is closed.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_create (mailbox_t *pmbox, const char *name)
The function mailbox_create allocates and initializes pmbox. The concrete mailbox type instantiate is based on the scheme of the url name.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
The url name supplied is invalid or not supported. pmbox is NULL.
ENOMEM
Not enough memory to allocate resources.

Function: int mailbox_create_default (mailbox_t *pmbox, const char *name)
Create a mailbox with mailbox_create () based on the environment variable $MAIL or the string formed by _PATH_MAILDIR/user" or $LOGNAME if user is null,

Function: void mailbox_destroy (mailbox_t *pmbox)
Destroys and releases resources held by pmbox.

Function: int mailbox_expunge (mailbox_t mbox)
All messages marked for deletion are removed.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_get_folder (mailbox_t mbox, folder_t *folder)
Get the folder.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_get_debug (mailbox_t mbox, debug_t *debug)
Get a debug object. The return value is 0 on success and a code number on error conditions:
MU_ERROR_INVALID_PARAMETER
mbox is null.
ENOMEM

Function: int mailbox_get_locker (mailbox_t mbox, locker_t *plocker)
Return the locker object.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_get_message (mailbox_t mbox, size_t msgno, message_t *pmessage)
Retrieve message number msgno, pmessage is allocated and initialized.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null or msgno is invalid.
ENOMEM
Not enough memory.

Function: int mailbox_get_observable (mailbox_t mbox mbox, observable_t*observable)
Get the observable object.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.
ENOMEM
Not enough memory.

Function: int mailbox_get_property (mailbox_t mbox, property_t *property)
Get the property object. The return value is 0 on success and a code number on error conditions:
MU_ERROR_INVALID_PARAMETER
mbox is null.
ENOMEM

Function: int mailbox_get_size (mailbox_t mbox, off_t *psize)
Gives the mbox size.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_get_stream (mailbox_t mbox, stream_t *pstream)
The mailbox stream is put in pstream.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is invalid or pstream is NULL.

Function: int mailbox_get_ticket (mailbox_t mbox, ticket_t ticket)
The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_get_url (mailbox_t mbox, url_t *purl)
Gives the constructed url.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_is_modified (mailbox_t mbox)
Check if the mailbox been modified by an external source.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_message_unseen (mailbox_t mbox, size_t *pnumber);
Give the number of first unseen message in mbox.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_messages_count (mailbox_t mbox, size_t *pnumber);
Give the number of messages in mbox.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_messages_recent (mailbox_t mbox, size_t *pnumber);
Give the number of recent messages in mbox.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_open (mailbox_t mbox, int flag)
A connection is open, if no stream was provided, a stream is created based on the mbox type. The flag can be OR'ed. See stream_create for flag's description.

The return value is 0 on success and a code number on error conditions:

EAGAIN
EINPROGRESS
Operation in progress.
EBUSY
Resource busy.
MU_ERROR_INVALID_PARAMETER
mbox is null or flag is invalid.
ENOMEM
Not enough memory.

Function: int mailbox_scan (mailbox_t mbox, size_t msgno, size_t *pcount);
Scan the mailbox for new messages starting at message msgno.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.
ENOMEM
Not enough memory.

Function: int mailbox_set_locker (mailbox_t mbox, locker_t locker)
Set the type of locking done by the mbox.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_set_stream (mailbox_t mbox, stream_t stream)
Set the stream connection to use for the mailbox.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox or stream is NULL.

Function: int mailbox_set_ticket (mailbox_t mbox, ticket_t ticket)
The ticket will be set on the auth_t object on creation.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_uidnext (mailbox_t mbox, size_t *pnumber);
Give the next predicted uid for mbox.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Function: int mailbox_uidvalidity (mailbox_t mbox, size_t *pnumber);
Give the uid validity of mbox.

The return value is 0 on success and a code number on error conditions:

MU_ERROR_INVALID_PARAMETER
mbox is null.

Mailer

/* Prefix mailer_ is reserved */
#include <mailutils/mailer.h>

The API is still shaky and undefined.

Function: int mailer_create (mailer_t *, const char *)

Function: void mailer_destroy (mailer_t *)

Function: int mailer_open (mailer_t, int flags)

Function: int mailer_close (mailer_t)

Function: int mailer_send_message (mailer_t, message_t)

Function: int mailer_get_property (mailer_t, property_t *)

Function: int mailer_get_stream (mailer_t, stream_t *)

Function: int mailer_set_stream (mailer_t, stream_t)

Function: int mailer_get_debug (mailer_t, debug_t *)

Function: int mailer_set_debug (mailer_t, debug_t)

Function: int mailer_get_observable (mailer_t, observable_t *)

Function: int mailer_get_url (mailer_t, url_t *)

Message

#include <mailutils/message.h>
/* Prefix message_ is reserve */

The message_t object is a convenient way to manipulate messages. It encapsulates the envelope_t, the header_t and the body_t.

    mailbox_t
    __________                   message_t
   (message[1])        +------>+-----------------------+
    ----------         |       |  envelope_t           |
   (message[2])        |       |-----------------------|
    ----------         |       |  header_t             |
   (message[3])--------+       |-----------------------|
    ----------                 |  body_t               |
   (message[n])                |-----------------------|
    ----------                 |  attribute_t          |
                               |-----------------------|
                               |  stream_t             |
                               +-----------------------+

Function: void message_create (message_t *msg, void *owner)

Function: void message_destroy (message_t *msg, void *owner)
The resources allocate for msg are freed.

Function: int message_get_header (message_t msg, header_t *pheader)
Retrieve msg header.

Function: int message_set_header (message_t msg, header_t header, void *owner)

Function: int message_get_body (message_t msg, body_t *pbody)

Function: int message_set_body (message_t msg, body_t body, void *owner)

Function: int message_is_multipart (message_t msg)
Return non-zero value if message is multi-part.

Function: int message_get_num_parts (message_t msg, size_t *nparts)

Function: int message_get_part (message_t msg, size_t part, message_t *msg)

Function: int message_get_stream (message_t msg, stream_t *pstream)

Function: int message_set_stream (message_t msg, stream_t stream,void *owner )

Function: int message_get_attribute (message_t msg, attribute_t *pattribute)

Function: int message_set_attribute (message_t msg, attribute_t attribute, void *owner)

Function: int message_get_envelope (message_t msg, envelope_t *penvelope)

Function: int message_set_envelope (message_t msg, envelope_t envelope, void *owner)

Function: int message_get_uid (message_t msg, size_t *uid)

Function: int message_get_uidl (message_t msg, char *buffer, size_t buflen, size_t *pwriten)

Function: int message_set_uidl (message_t msg, int (*_get_uidl)(message_t, char *, size_t, size_t *), void *owner)

Function: int message_get_observable (message_t msg, observable_t *observable)

Function: int message_create_attachment (const char *content_type, const char *encoding, const char *filename, message_t *newmsg)

Function: int message_save_attachment (message_t msg, const char *filename, void **data)

Function: int message_encapsulate (message_t msg, message_t *newmsg, void **data)

Function: int message_unencapsulate (message_t msg, message_t *newmsg, void **data);

Envelope

/* Prefix envelope_ is reserved */
#include <mailutils/envelope.h>

Function: int envelope_date (envelope_t, char *, size_t, size_t *);

Get the date that the message was delivered to the mailbox, in something close to ANSI ctime() format: Mon Jul 05 13:08:27 1999.

Function: int envelope_sender (envelope_t, char *, size_t, size_t *);

Get the address that this message was reportedly received from. This would be the "mail from" argument if the message was delivered or received via SMTP, for example.

Function: int envelope_get_message (envelope_t, message_t *);

Function: int envelope_create (envelope_t *, void *);
Primarily for internal use.

Function: void envelope_destroy (envelope_t *, void *);
Primarily for internal use.

Function: int envelope_set_sender (envelope_t, int (*_sender) __P ((envelope_t, char *, size_t, size_t*)), void *);

Primarily for internal use. The implementation of envelope_t depends on the mailbox type, this allows the function which actually gets the sender to be set by the creator of an envelope_t.

Function: int envelope_set_date (envelope_t, int (*_date) __P ((envelope_t, char *, size_t, size_t *)), void *);

Primarily for internal use. The implementation of envelope_t depends on the mailbox type, this allows the function which actually gets the date to be set by the creator of an envelope_t.

Headers

/* Prefix header_ is reserved */
#include <mailutils/header.h>

So far we plan support for RFC822 and plan for RFC1522. with RFC1522 non ASCII characters will be encoded.

Function: int header_create (header_t *hdr, const char *blurb, size_t len, void *owner)
Initialize a hdr to a supported type. If blurb is not NULL, it is parsed.

Function: void header_destroy (header_t *hdr, void *owner)
The resources allocated for hdr are freed.

Function: int header_set_value (header_t hdr, const char *fn, const char *fv, size_t n, int replace)
Set the field-name fn to field-value fv of size n in hdr. If replace is non-zero the initial value is replaced, if zero it is appended.

Some basic macros are already provided for rfc822.

MU_HDR_RETURN_PATH
Return-Path
MU_HDR_RECEIVED
Received
MU_HDR_DATE
Date
MU_HDR_FROM
From
MU_HDR_RESENT_FROM
Resent-From
MU_HDR_SUBJECT
Subject
MU_HDR_SENDER
Sender
MU_HDR_RESENT_SENDER
Resent-SENDER
MU_HDR_TO
To
MU_HDR_RESENT_TO
Resent-To
MU_HDR_CC
Cc
MU_HDR_RESENT_CC
Resent-Cc
MU_HDR_BCC
Bcc
MU_HDR_RESENT_BCC
Resent-Bcc
MU_HDR_REPLY_TO
Reply-To
MU_HDR_RESENT_REPLY_TO
Resent-Reply-To
MU_HDR_MESSAGE_ID
Message-ID
MU_HDR_RESENT_MESSAGE_ID
Resent-Message-ID
MU_HDR_IN_REPLY_TO
In-Reply-To
MU_HDR_ENCRYPTED
Encrypted
MU_HDR_PRECEDENCE
Precedence
MU_HDR_STATUS
Status
MU_HDR_CONTENT_LENGTH
Content-Length
MU_HDR_CONTENT_TYPE
Content-Type
MU_HDR_MIME_VERSION
MIME-Version

Function: int header_get_value (header_t hdr, const char *fn, char *fv, size_t len, size_t *n)
Value of field-name fn is returned in buffer fv of size len. The number of bytes written is put in n.

Function: int header_aget_value (header_t hdr, const char *fn, char **fv)
The value is allocated.

Function: int header_get_stream (header_t hdr, stream_t *pstream)

Function: int header_set_size (header_t hdr, size_t *size)

Function: int header_set_lines (header_t hdr, size_t *lpines)

Body

/* Prefix body_ is reserved */
#include <mailutils/body.h>

Function: int body_create (body_t *body, void *owner)
Initialize an object bdy.

Function: void body_destroy (body_t *pbody)
The resources allocate are release.

Function: int body_get_stream (body_t body, stream_t *pstream)

Function: int body_set_stream (body_t body, stream_t stream, void *owner)

Function: int body_get_filename __P ((body_t body, char *buffer, size_tbuflen, size_t *pwriten)

Function: int body_set_filename (body_t body, const char*buffer)

Function: int body_size (body_t body, size_t*psize)

Function: int body_lines (body_t body, size_t *plines)

Attribute

/* Prefix attribute_ is reserved */
#include <mailutils/attribute.h>

Function: int attribute_create (attribute_t *pattribute)

Function: void attribute_destroy (attribute_t *pattribute)

Function: int attribute_is_seen (attribute_t attribute)

Function: int attribute_is_answered (attribute_t attribute)

Function: int attribute_is_flagged (attribute_t attribute)

Function: int attribute_is_deleted (attribute_t attribute)

Function: int attribute_is_draft (attribute_t attribute)

Function: int attribute_is_recent (attribute_t attribute)

Function: int attribute_is_read (attribute_t attribute)

Function: int attribute_set_seen (attribute_t attribute)

Function: int attribute_set_answered (attribute_t attribute)

Function: int attribute_set_flagged (attribute_t attribute)

Function: int attribute_set_deleted (attribute_t attribute)

Function: int attribute_set_draft (attribute_t attribute)

Function: int attribute_set_recent (attribute_t attribute)

Function: int attribute_set_read (attribute_t attribute)

Function: int attribute_unset_seen (attribute_t attribute)

Function: int attribute_unset_answered (attribute_t attribute)

Function: int attribute_unset_flagged (attribute_t attribute)

Function: int attribute_unset_deleted (attribute_t attribute)

Function: int attribute_unset_draft (attribute_t attribute)

Function: int attribute_unset_recent (attribute_t attribute)

Function: int attribute_unset_read (attribute_t attribute)

Function: int attribute_is_equal (attribute_t att1, attribute_t att2)

Function: int attribute_copy (attribute_t dst, attribute_t src)

Function: int string_to_attribute (const char *buf, attribute_t *pattr)

Function: int attribute_to_string (attribute_t attr, char *buf, size_t len, size_t *pwriten)

Stream

#include <mailutils/stream.h>

Function: int stream_create (stream_t *pstream, int flags, void *owner)
MU_STREAM_READ
The stream is open read only.
MU_STREAM_WRITE
The stream is open write only.
MU_STREAM_RDWR
The stream is open read and write.
MU_STREAM_APPEND
The stream is open in append mode for writing.
MU_STREAM_CREAT
The stream is created.
MU_STREAM_NONBLOCK
The stream is set non blocking.
MU_STREAM_NO_CHECK
Stream is destroyed without checking for the owner.

Function: void stream_destroy (stream_t *pstream, void *owner)

Function: int stream_open (stream_t stream, const char *name, intport, int flag)

Function: int stream_close (stream_t stream)

Function: int stream_get_fd (stream_t stream, int *pfd)

Function: int stream_read (stream_t stream, char *buffer, size_t buflen, off_t offset, size_t *pwriten)

Function: int stream_readline (stream_t stream, char *buffer, size_t buflen, off_t offset, size_t *pwriten)

Function: int stream_size (stream_t stream, off_t *psize)

Function: int stream_truncate (stream_t stream, off_t size)

Function: int stream_write (stream_t stream, const char *buffer, size_t buflen, off_t offset, size_t *pwriten)

Function: int stream_flush (stream_t stream)

Function: int stream_get_flags (stream_t stream, int *pflags)

Function: int stream_get_state (stream_t stream, int *pstate)
MU_STREAM_STATE_OPEN
Last action was stream_open.
MU_STREAM_STATE_READ
Last action was stream_read or stream_readline.
MU_STREAM_STATE_WRITE
Last action was stream_write.
MU_STREAM_STATE_CLOSE
Last action was stream_close.

Function: int file_stream_create (stream_t *pstream)

Function: int mapfile_stream_create (stream_t *pstream)

Function: int encoder_stream_create (stream_t *pstream, stream_t iostream, const char *encoding)

Function: int decoder_stream_create (stream_t *pstream, stream_t iostream, const char *encoding)

Function: int tcp_stream_create (stream_t *pstream)

An example using tcp_stream_create to make a simple web client:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>

#include <mailutils/io.h>

const char *wbuf = "GET / HTTP/1.0\r\n\r\n";
char rbuf[1024];

int
main(int argc, char **argv)
{
  int ret, off = 0, fd;
  stream_t stream;
  size_t nb;
  fd_set fds;

  argc = argc, argv = argv;

  ret = tcp_stream_create (&stream);
  if (ret != 0)
    {
       fprintf (stderr, "tcp_stream_create: %s\n",
                mailutils_error(ret));
       exit (EXIT_FAILURE);
    }

connect_again:
  ret = stream_open (stream, "www.netscape.com", 80,
                     MU_STREAM_NONBLOCK);
  if (ret != 0)
    {
       if (ret == MU_ERROR_EAGAIN)
         {
             ret = stream_get_fd(stream, &fd);
             if (ret != 0)
               {
                   fprintf (stderr, "stream_get_fd: %s\n",
                            mailutils_error(ret));
                   exit (EXIT_FAILURE);
               }
               FD_ZERO (&fds);
               FD_SET (fd, &fds);
               select (fd+1, NULL, &fds, NULL, NULL);
               goto connect_again;
          }
          fprintf (stderr, "stream_open: %s\n", mailutils_error (ret));
          exit (EXIT_FAILURE);
    }

    ret = stream_get_fd (stream, &fd);
    if (ret != 0)
      {
         fprintf(stderr, "stream_get_fd: %s\n", strerror(ret));
         exit (EXIT_FAILURE);
      }

write_again:
    ret = stream_write (stream, wbuf + off, strlen (wbuf), 0, &nb);
    if (ret != 0 )
      {
         if (ret == EAGAIN)
           {
              FD_ZERO (&fds);
              FD_SET (fd, &fds);
              select (fd + 1, NULL, &fds, NULL, NULL);
              off += nb;
              goto write_again;
           }
         fprintf (stderr, "stream_write: %s\n", strerror(ret));
         exit (EXIT_FAILURE)
      }

      if (nb != strlen (wbuf))
        {
           fprintf(stderr, "stream_write: %s\n", "nb != wbuf length");
           exit (EXIT_FAILURE);
        }

      do
        {
read_again:
           ret = stream_read (stream, rbuf, sizeof (rbuf), 0, &nb);
           if (ret != 0)
             {
                if (ret == EAGAIN)
                  {
                     FD_ZERO (&fds);
                     FD_SET (fd, &fds);
                     select (fd + 1, &fds, NULL, NULL, NULL);
                     goto read_again;
                   }
                 fprintf (stderr, "stream_read: %s\n", strerror(ret));
                 exit(EXIT_FAILURE);
              }
              write (2, rbuf, nb);
         } while (nb);

         ret = stream_close (stream);
         if (ret!= 0)
           {
              fprintf (stderr, "stream_close: %s\n", strerror(ret));
              exit (EXIT_FAILURE);
           }

         stream_destroy (&stream, NULL);
         exit (EXIT_SUCCESS);
}

Iterator

/* Prefix iterator_ is reserved */
#include <mailutils/iterator.h>

Function: int iterator_create (iterator_t *)

Function: void iterator_destroy (iterator_t *)

Function: int iterator_first (iterator_t)

Function: int iterator_next (iterator_t)

Function: int iterator_current (iterator_t, void **pitem)

Function: int iterator_is_done (iterator_t)

Authenticator

/* Prefix auth_ is reserved */
#include <mailutils/auth.h>

There are many ways to authenticate to a server. To be flexible the authentication process is provided by two objects auth_t and ticket_t. The auth_t can implement different protocol like APOP, MD5-AUTH, One Time Passwd etc .. By default if a mailbox does not understand or know how to authenticate it falls back to user/passwd authentication. The ticket_t is a way for Mailboxes and Mailers provide a way to authenticate when the URL does not contain enough information. The default action is to call the function auth_authenticate which will get the user and passwd if not set, this function can be overridden by a custom method.

Function: int auth_create (auth_t *pauth, void *owner)

Function: void auth_destroy (auth_t *pauth, void *owner)

Function: int auth_prologue (auth_t auth)

Function: int auth_authenticate (auth_t auth, char **user, char **passwd)

Function: int auth_epilogue (auth_t auth)

A simple example of an authenticate function:

#include <mailutils/auth.h>
#include <stdio.h>
#include <string.h>

int
my_authenticate (auth_t auth, char **user, char **passwd)
{
  char u[128] = "";
  char p[128] = "";

  /* prompt the user name */
  printf ("User: ");
  fflush (stdout);
  fgets (u, sizeof (u), stdin);
  u[strlen (u) - 1] = '\0'; /* nuke the trailing NL */

  /* prompt the passwd */
  printf ("Passwd: "); fflush (stdout);
  echo_off ();
  fgets (p, sizeof(p), stdin);
  echo_on ();
  p[strlen (p) - 1] = '\0';

  /* duplicate */
  *user = strdup (u);
  *passwd = strdup (p);
  return 0;
}

Address

/* Prefix address_ is reserved */
#include <mailutils/address.h>

The Internet address format is defined in RFC 822. RFC 822 has been updated, and is now superceeded by RFC 2822, which makes some corrections and clarifications. References to RFC 822 here apply equally to RFC 2822.

The RFC 822 format is more flexible than many people realize, here is a quick summary of the syntax this parser implements, see RFC 822 for the details. "[]" pairs mean "optional", "/" means "one or the other", and double-quoted characters are literals.

addr-spec    = local-part "@" domain
mailbox      = addr-spec ["(" display-name ")"] /
               [display-name] "<" [route] addr-spec ">"
mailbox-list = mailbox ["," mailbox-list]
group        = display-name ":" [mailbox-list] ";"
address      = mailbox / group / unix-mbox
address-list = address ["," address-list]

unix-mbox is a non-standard extension meant to deal with the common practice of using user names as addresses in mail utilities. It allows addresses such as "root" to be parsed correctly. These are NOT valid internet email addresses, they must be qualified before use.

Several address functions have a set of common arguments with consistent semantics, these are described here to avoid repetition.

Since an address-list may contain multiple addresses, they are accessed by a one-based index number, no. The index is one-based because pop, imap, and other message stores commonly use one-based counts to access messages and attributes of messages.

If len is greater than 0 it is the length of the buffer buf, and as much of the component as possible will be copied into the buffer. The buffer will be null terminated.

The size of a particular component may be queried by providing 0 for the len of the buffer, in which case the buffer is optional. In this case, if n is provided *n is assigned the length of the component string.

@macro ADDRESSENOMEM

  • ENOMEM Not enough memory to allocate resources. @macro ADDRESSEPARSE
  • ENOENT Invalid RFC822 syntax, parsing failed. @macro ADDRESSENOENT
  • ENOENT The index no is outside of the range of available addresses. @macro ADDRESSEINVAL
  • EINVAL Invalid usage, usually a required argument was null.
    Data Type: address_t
    The address_t object is used to hold information about a parsed RFC822 address list, and is an opaque data structure to the user. Functions are provided to retrieve information about an address in the address list.
    Function: int address_create (address_t *addr, const char *string)
    This function allocates and initializes addr by parsing the RFC822 address-list string. The return value is 0 on success and a code number on error conditions:
    @ADDRESSEINVAL @ADDRESSENOMEM @ADDRESSEPARSE
    Function: void address_destroy (address_t *addr)
    The addr is destroyed.
    Function: int address_get_email (address_t *addr, size_t no, char* buf, size_t len, size_t* n)
    Accesses the noth email address component of the address list. This address is the plain email address, correctly quoted, suitable for using in an smtp dialog, for example, or as the address part of a contact book entry. The return value is 0 on success and a code number on error conditions:
    @ADDRESSEINVAL @ADDRESSENOENT
    Function: int address_get_personal (address_t *addr, size_t no, char* buf, size_t len, size_t* n)
    Accesses the display-name describing the noth email address. This display-name is optional, so may not be present. If it is not present, but there is an RFC822 comment after the address, that comment will be returned as the personal phrase, as this is a common usage of the comment even though it is not defined in the internet mail standard. A group is a kind of a special case. It has a display-name, followed by an optional mailbox-list. The display-name will be allocated an address all it's own, but all the other elements (local-part, domain, etc.) will be zero-length. So "a group: ;" is valid, will have a count of 1, but address_get_email(), and all the rest, will return zero-length output. The return value is 0 on success and a code number on error conditions:
    @ADDRESSEINVAL @ADDRESSENOENT
    Function: int address_get_comments (address_t *addr, size_t no, char* buf, size_t len, size_t* n)
    Accesses the comments extracted while parsing the noth email address. These comments have no defined meaning, and are not currently collected. The return value is 0 on success and a code number on error conditions:
    @ADDRESSEINVAL @ADDRESSENOENT
    Function: int address_get_email (address_t *addr, size_t no, char* buf, size_t len, size_t* n)
    Accesses the email addr-spec extracted while parsing the noth email address. This will be 0 length for a unix-mbox. The return value is 0 on success and a code number on error conditions:
    @ADDRESSEINVAL @ADDRESSENOENT
    Function: int address_get_local_part (address_t *addr, size_t no, char* buf, size_t len, size_t* n)
    Accesses the local-part of an email addr-spec extracted while parsing the noth email address. The return value is 0 on success and a code number on error conditions:
    @ADDRESSEINVAL @ADDRESSENOENT
    Function: int address_get_domain (address_t *addr, size_t no, char* buf, size_t len, size_t* n)
    Accesses the domain of an email addr-spec extracted while parsing the noth email address. This will be 0 length for a unix-mbox. The return value is 0 on success and a code number on error conditions:
    @ADDRESSEINVAL @ADDRESSENOENT
    Function: int address_get_route (address_t *addr, size_t no, char* buf, size_t len, size_t* n)
    Accesses the route of an email addr-spec extracted while parsing the noth email address. This is a rarely used RFC822 address syntax, but is legal in SMTP as well. The entire route is returned as a string, those wishing to parse it should look at <mailutils/parse822.h>. The return value is 0 on success and a code number on error conditions:
    @ADDRESSEINVAL @ADDRESSENOENT
    Function: int address_is_group (address_t *addr, size_t no, size_t len, int* yes)
    Sets *yes to 1 if this address is just the name of a group, 0 otherwise. This is faster than checking if the address has a non-zero length personal, and a zero-length local_part and domain. yes can be null, though that doesn't serve much purpose other than determining that no refers to an address. Currently, there is no way to determine the end of the group. The return value is 0 on success and a code number on error conditions:
    @ADDRESSEINVAL @ADDRESSENOENT
    Function: int address_to_string (address_t *addr, char* buf, size_t len, size_t* n)
    Returns the entire address list as a single RFC822 formatted address list. The return value is 0 on success and a code number on error conditions:
    @ADDRESSEINVAL @ADDRESSENOMEM
    Function: int address_get_count (address_t addr, size_t* count)
    Returns a count of the addresses in the address list. If addr is null, the count is 0. If count is not null, the count will be written to *count. The return value is 0.

    Example

    #include <stdio.h>
    #include <errno.h>
    #include <mailutils/address.h>
    
    #define EPARSE ENOENT
    
    static const char* err_name(int e)
    {
        struct {
    	int e;
    	const char* s;
        } map[] = {
    #define E(e) { e, #e },
    	E(ENOENT)
    	E(EINVAL)
    	E(ENOMEM)
    #undef E
    	{ 0, NULL }
        };
        static char s[sizeof(int) * 8 + 3];
        int i;
    
        for(i = 0; map[i].s; i++) {
    	if(map[i].e == e)
    	    return map[i].s;
        }
        sprintf(s, "[%d]", e);
    
        return s;
    }
    
    static int parse(const char* str)
    {
        size_t no = 0;
        size_t pcount = 0;
        int status;
    
        char buf[BUFSIZ];
    
        address_t  address = NULL;
    
        status = address_create(&address, str);
    
        address_get_count(address, &pcount);
    
        if(status) {
    	printf("%s=> error %s\n\n", str, err_name(status));
    	return 0;
        } else {
    	printf("%s=> pcount %d\n", str, pcount);
        }
    
        for(no = 1; no <= pcount; no++) {
    	size_t got = 0;
    	int isgroup;
    
    	address_is_group(address, no, &isgroup);
    
    	printf("%d ", no);
    
    	if(isgroup) {
    	  address_get_personal(address, no, buf, sizeof(buf), &got);
    
    	  printf("group <%s>\n", buf);
    	} else {
    	  address_get_email(address, no, buf, sizeof(buf), 0);
    
    	  printf("email <%s>\n", buf);
    	}
    
    	address_get_personal(address, no, buf, sizeof(buf), &got);
    
    	if(got && !isgroup) printf("   personal <%s>\n", buf);
    
    	address_get_comments(address, no, buf, sizeof(buf), &got);
    
    	if(got) printf("   comments <%s>\n", buf);
    
    	address_get_local_part(address, no, buf, sizeof(buf), &got);
    
    	if(got) {
    	    printf("   local-part <%s>", buf);
    
    	    address_get_domain(address, no, buf, sizeof(buf), &got);
    
    	    if(got) printf(" domain <%s>", buf);
    
    	    printf("\n");
    	}
    
    	address_get_route(address, no, buf, sizeof(buf), &got);
    
    	if(got) printf("   route <%s>\n", buf);
        }
        address_destroy(&address);
    
        printf("\n");
    
        return 0;
    }
    
    static int parseinput(void)
    {
        char buf[BUFSIZ];
    
        while(fgets(buf, sizeof(buf), stdin) != 0) {
    	buf[strlen(buf) - 1] = 0;
    	parse(buf);
        }
    
        return 0;
    }
    
    int main(int argc, const char *argv[])
    {
        argc = 1;
    
        if(!argv[argc]) {
    	return parseinput();
        }
        for(; argv[argc]; argc++) {
    	if(strcmp(argv[argc], "-") == 0) {
    	    parseinput();
    	} else {
    	    parse(argv[argc]);
    	}
        }
    
        return 0;
    }
    

    Locker

    /* Prefix locker_ is reserved */
    #include <mailutils/locker.h>
    
    

    Function: int locker_create (locker_t * plocker, char *filename, size_t len, int flags)

    Function: void locker_destroy (locker_t * plocker)

    Function: int locker_lock (locker_t locker, int flag)
    MU_LOCKER_RDLOCK
    MU_LOCKER_WRLOCK
    MU_LOCKER_PID
    MU_LOCKER_FCNTL
    MU_LOCKER_TIME

    Function: int locker_touchlock (locker_t locker)

    Function: int locker_unlock (locker_t locker)

    URL

    A mailbox or a mailer can be described in a URL, the string will contain the necessary information to initialize mailbox_t, or mailer_t properly.

    POP3

    The POP URL scheme contains a POP server, optional port number and the authentication mechanism. The general form is

    pop://[<user>[;AUTH=<auth>]@]<host>[:<port>]
     or
    pop://[<user>[:<passwd>]@]<host>[:<port>]
    

    If :port is omitted the default value is 110. Different forms of authentication can be specified with ;AUTH=type. The special string ;AUTH=* indicates that the client will use a default scheme base on the capability of the server.

    pop://obelix@gaulois.org
    pop://asterix;AUTH=*@france.com
    pop://falbala;AUTH=+APOP@france.com
    pop://obelix;AUTH=+APOP@village.gaulois.org:2000
    pop://obelix:menhir@village.gaulois.org:2000
    

    For more complete information see rfc2368.

    IMAP

    The IMAP URL scheme contains an IMAP server, optional port number and