Commit 8f6a087a 8f6a087af2063a2c9e368301799968d995f87d08 by Alain Magloire

Implement the mailbox framework for NNTP.

1 parent ebf3a875
......@@ -56,4 +56,8 @@ libmu_nntp_la_SOURCES = \
nntp_sendline.c \
nntp_stat.c \
nntp_stream.c \
nntp_timeout.c
nntp_timeout.c \
\
url.c \
folder.c \
mbox.c
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2004 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef ENABLE_NNTP
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/nntp.h>
#include <mailutils/errno.h>
#include <mailutils/mailbox.h>
#include <mailutils/registrar.h>
#include <folder0.h>
#include "nntp0.h"
/* We export url parsing and the initialisation of
the mailbox, via the register entry/record. */
static struct _record _nntp_record =
{
MU_NNTP_URL_SCHEME,
_nntp_url_init, /* Url init. */
_nntp_mailbox_init, /* Mailbox init. */
NULL, /* Mailer init. */
_nntp_folder_init, /* Folder init. */
NULL, /* No need for an back pointer. */
NULL, /* _is_scheme method. */
NULL, /* _get_url method. */
NULL, /* _get_mailbox method. */
NULL, /* _get_mailer method. */
NULL /* _get_folder method. */
};
record_t nntp_record = &_nntp_record;
static int nntp_folder_open __P ((folder_t, int));
static int nntp_folder_close __P ((folder_t));
static void nntp_folder_destroy __P ((folder_t folder));
static int nntp_folder_list __P ((folder_t folder, const char *ref, const char *name, struct folder_list *pflist));
int
_nntp_folder_init (folder_t folder)
{
int status;
f_nntp_t f_nntp;
f_nntp = folder->data = calloc (1, sizeof (*f_nntp));
if (f_nntp == NULL)
return ENOMEM;
f_nntp->folder = folder;
folder->_destroy = nntp_folder_destroy;
folder->_open = nntp_folder_open;
folder->_close = nntp_folder_close;
folder->_list = nntp_folder_list;
/* Not supported.
folder->_lsub = folder_nntp_lsub;
folder->_subscribe = folder_nntp_subscribe;
folder->_unsubscribe = folder_nntp_unsubscribe;
folder->_delete = folder_nntp_delete;
folder->_rename = folder_nntp_rename;
*/
return 0;
}
static int
nntp_folder_open (folder_t folder, int flags)
{
f_nntp_t f_nntp = folder->data;
stream_t carrier = NULL;
char *host;
long port = MU_NNTP_DEFAULT_PORT; /* default nntp port. */
int status = 0;
size_t len = 0;
/* If we are already open for business, noop. */
monitor_wrlock (folder->monitor);
if (f_nntp->isopen)
{
monitor_unlock (folder->monitor);
return 0;
}
monitor_unlock (folder->monitor);
/* Fetch the server name and the port in the url_t. */
status = url_get_host (folder->url, NULL, 0, &len);
if (status != 0)
return status;
host = alloca (len + 1);
url_get_host (folder->url, host, len + 1, NULL);
url_get_port (folder->url, &port);
folder->flags = flags;
/* Create the networking stack. */
status = tcp_stream_create (&carrier, host, port, folder->flags);
if (status != 0)
return status;
/* Ask for the stream internal buffering mechanism scheme. */
stream_setbufsiz (carrier, BUFSIZ);
FOLDER_DEBUG2 (folder, MU_DEBUG_PROT, "folder_nntp_open (%s:%d)\n", host, port);
status = mu_nntp_create (&f_nntp->nntp);
if (status == 0)
{
status = mu_nntp_set_carrier (f_nntp->nntp, carrier);
if (status == 0)
{
status = mu_nntp_connect (f_nntp->nntp);
if (status == 0)
{
monitor_wrlock (folder->monitor);
f_nntp->isopen++;
monitor_unlock (folder->monitor);
}
}
}
return status;
}
static int
nntp_folder_close (folder_t folder)
{
f_nntp_t f_nntp = folder->data;
int status = 0;
monitor_wrlock (folder->monitor);
f_nntp->isopen--;
if (f_nntp->isopen)
{
monitor_unlock (folder->monitor);
return 0;
}
monitor_unlock (folder->monitor);
status = mu_nntp_quit (f_nntp->nntp);
f_nntp->selected = NULL;
return status;
}
/* Destroy the folder resources. */
static void
nntp_folder_destroy (folder_t folder)
{
if (folder->data)
{
f_nntp_t f_nntp = folder->data;
if (f_nntp->nntp)
mu_nntp_destroy (&f_nntp->nntp);
free (f_nntp);
folder->data = NULL;
}
}
static int
nntp_folder_list (folder_t folder, const char *ref, const char *name, struct folder_list *pflist)
{
return ENOTSUP;
}
#else
#include <stdio.h>
#include <registrar0.h>
record_t nntp_record = NULL;
#endif
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2004 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
struct _nntp_folder;
struct _nntp_mailbox;
struct _nntp_message;
typedef struct _nntp_folder *f_nntp_t;
typedef struct _nntp_mailbox *m_nntp_t;
typedef struct _nntp_message *msg_nntp_t;
extern int _nntp_url_init __P ((url_t));
extern int _nntp_mailbox_init __P ((mailbox_t));
extern int _nntp_folder_init __P ((folder_t));
struct _nntp_folder
{
/* Refcount. */
int isopen;
/* Back pointer. */
folder_t folder;
/* Selected newsgroup. */
m_nntp_t selected;
/* NNTP object. */
mu_nntp_t nntp;
};
struct _nntp_mailbox
{
char status;
char *name;
/* Pointer back to the mailbox/newsgroup. */
mailbox_t mailbox;
/* Our nntp folder. */
f_nntp_t f_nntp;
/* Read Messages on the newsgroup. */
msg_nntp_t *messages;
size_t messages_count;
/* Estimated number of articles in the group. */
unsigned long number;
/* High water mark from "GROUP" command */
unsigned long high;
/* Low water mark from "GROUP" command */
unsigned long low;
};
struct _nntp_message
{
/* Back pointer. */
message_t message;
/* Our nntp folder. */
m_nntp_t m_nntp;
/* Message id. */
char *mid;
/* mesgno of the post. */
unsigned long msgno;
/* Stream for message. */
stream_t mstream;
/* Stream for body. */
stream_t bstream;
/* Stream for header. */
stream_t hstream;
};
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2004 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef ENABLE_NNTP
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/nntp.h>
#include <url0.h>
static void url_nntp_destroy (url_t url);
static void
url_nntp_destroy (url_t url ARG_UNUSED)
{
}
/*
POP URL:
nntp://<host>:<port>/<newsgroup-name>/<article-number>
*/
int
_nntp_url_init (url_t url)
{
int status = 0;
url->_destroy = url_nntp_destroy;
status = url_parse(url);
if(status)
return status;
/* is it nntp? */
if (strcmp (MU_NNTP_URL_SCHEME, url->scheme) != 0)
return EINVAL;
/* not valid in a nntp url */
if (!url->host || !url->path)
return EINVAL;
if (url->port == 0)
url->port = MU_NNTP_DEFAULT_PORT;
return status;
}
#endif