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.
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 RFC 2368.
The POP3S URL scheme contains a POP server over SSL, optional port number and the authentication mechanism. The general form is
<pops://[user[;AUTH=auth]@]host[:port]> or <pops://[user[:passwd]@]host[:port]>
If :port is omitted the default value is 995.
The IMAP URL scheme contains an IMAP server, optional port number and the authentication mechanism. The general form is
<imap://[user[;AUTH=type]]@host[:port][/mailbox]> or <imap://[user[:passwd]]@host[:port][/mailbox]>
If :port is omitted the default value is 143. 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.
<imap://obelix@imap.gaulois.org> <imap://asterix;AUTH=*@imap.france.com> <imap://asterix:potion@imap.france.com>
For more complete information see RFC 2192.
The IMAPS URL scheme contains an IMAP server over SSL, optional port number and the authentication mechanism. The general form is
<imaps://[user[;AUTH=type]]@host[:port][/mailbox]> or <imaps://[user[:passwd]]@host[:port][/mailbox]>
If :port is omitted the default value is 993.
Local folder should be handle by this URL. It is preferable to let the mailbox recognize the type of mailbox and take the appropriate action.
<file://path> <file://var/mail/user> <file://home/obelix/Mail>
For MMDF, MH local mailboxes URLs are provided, but it is preferable to
use <file://path> and let the library figure out which one.
<mmdf://path> <mh://path>
After setting a mailer, <mailto:> is used to tell the mailer where
and to whom the message is for.
<mailto://hostname>
Mailto can be used to generate short messages, for example to subscribe to mailing lists.
<mailto://bug-mailutils@gnu.org?body=subscribe> <mailto://bug-mailutils@gnu.org?Subject=hello&body=subscribe>
For more complete information see RFC 2368.
Helper functions are provided to retrieve and set the URL fields.
Create the url data structure, but do not parse it.
Parses the url, after calling this the get functions can be called.
The syntax, condensed from RFC 1738, and extended with the ;auth= of RFC 2384 (for POP) and RFC 2192 (for IMAP) is:
url = scheme ":" [ "//" [ user [ ( ":" password ) | ( ";auth=" auth ) ] "@" ] host [ ":" port ] [ ( "/" urlpath ) | ( "?" query ) ] ]This is a generalized URL syntax, and may not be exactly appropriate for any particular scheme.
Decodes an RFC 1738 encoded string, returning the decoded string in allocated memory. If the string is not encoded, this degenerates to a
strdup().
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/url.h>
#include <mailutils/secret.h>
#define CAT2(a,b) a ## b
#define GET_AND_PRINT(field,u,buf,status) \
status = CAT2(mu_url_sget_,field) (u, &buf); \
if (status == MU_ERR_NOENT) \
buf = ""; \
else if (status) \
{ \
mu_error ("cannot get %s: %s", #field, mu_strerror (status)); \
exit (1); \
} \
printf ("\t" #field " <%s>\n", buf)
static void
print_fvpairs (mu_url_t url)
{
size_t fvc, i;
char **fvp;
int rc = mu_url_sget_fvpairs (url, &fvc, &fvp);
if (rc)
{
mu_error ("cannot get F/V pairs: %s", mu_strerror (rc));
exit (1);
}
if (fvc == 0)
return;
for (i = 0; i < fvc; i++)
printf ("\tparam[%d] <%s>\n", i, fvp[i]);
}
static void
print_query (mu_url_t url)
{
size_t qargc, i;
char **qargv;
int rc = mu_url_sget_query (url, &qargc, &qargv);
if (rc)
{
mu_error ("cannot get query: %s", mu_strerror (rc));
exit (1);
}
if (qargc == 0)
return;
for (i = 0; i < qargc; i++)
printf ("\tquery[%d] <%s>\n", i, qargv[i]);
}
int
main ()
{
char str[1024];
long port = 0;
mu_url_t u = NULL;
while (fgets (str, sizeof (str), stdin) != NULL)
{
int rc;
const char *buf;
mu_secret_t secret;
str[strlen (str) - 1] = '\0'; /* chop newline */
if (strspn (str, " \t") == strlen (str))
continue; /* skip empty lines */
if ((rc = mu_url_create (&u, str)) != 0)
{
fprintf (stderr, "mu_url_create %s ERROR: [%d] %s",
str, rc, mu_strerror (rc));
exit (1);
}
if ((rc = mu_url_parse (u)) != 0)
{
printf ("%s => FAILED: [%d] %s\n",
str, rc, mu_strerror (rc));
continue;
}
printf ("%s => SUCCESS\n", str);
GET_AND_PRINT (scheme, u, buf, rc);
GET_AND_PRINT (user, u, buf, rc);
rc = mu_url_get_secret (u, &secret);
if (rc == MU_ERR_NOENT)
printf ("\tpasswd <>\n");
else if (rc)
{
mu_error ("cannot get %s: %s", "passwd", mu_strerror (rc));
exit (1);
}
else
{
printf ("\tpasswd <%s>\n", mu_secret_password (secret));
mu_secret_password_unref (secret);
}
GET_AND_PRINT (auth, u, buf, rc);
GET_AND_PRINT (host, u, buf, rc);
rc = mu_url_get_port (u, &port);
if (rc)
{
mu_error ("cannot get %s: %s", "port", mu_strerror (rc));
exit (1);
}
printf ("\tport %ld\n", port);
GET_AND_PRINT (path, u, buf, rc);
print_fvpairs (u);
print_query (u);
mu_url_destroy (&u);
}
return 0;
}