aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/nntp/camel-nntp-store.c
diff options
context:
space:
mode:
Diffstat (limited to 'camel/providers/nntp/camel-nntp-store.c')
-rw-r--r--camel/providers/nntp/camel-nntp-store.c259
1 files changed, 140 insertions, 119 deletions
diff --git a/camel/providers/nntp/camel-nntp-store.c b/camel/providers/nntp/camel-nntp-store.c
index 96d11b4915..a7b8f7df01 100644
--- a/camel/providers/nntp/camel-nntp-store.c
+++ b/camel/providers/nntp/camel-nntp-store.c
@@ -55,8 +55,98 @@ static CamelServiceClass *service_class = NULL;
#define CF_CLASS(so) CAMEL_FOLDER_CLASS (GTK_OBJECT(so)->klass)
#define CNNTPF_CLASS(so) CAMEL_NNTP_FOLDER_CLASS (GTK_OBJECT(so)->klass)
-static gboolean nntp_connect (CamelService *service, CamelException *ex);
-static gboolean nntp_disconnect (CamelService *service, CamelException *ex);
+static gboolean ensure_news_dir_exists (CamelNNTPStore *store);
+
+static gboolean
+nntp_store_connect (CamelService *service, CamelException *ex)
+{
+ struct hostent *h;
+ struct sockaddr_in sin;
+ int fd;
+ char *buf;
+ CamelNNTPStore *store = CAMEL_NNTP_STORE (service);
+
+ if (!ensure_news_dir_exists(store)) {
+ camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
+ "Could not open directory for news server: %s",
+ strerror (errno));
+ return FALSE;
+ }
+
+ if (!service_class->connect (service, ex))
+ return FALSE;
+
+ h = camel_service_gethost (service, ex);
+ if (!h)
+ return FALSE;
+
+ sin.sin_family = h->h_addrtype;
+ sin.sin_port = htons (service->url->port ? service->url->port : NNTP_PORT);
+ memcpy (&sin.sin_addr, h->h_addr, sizeof (sin.sin_addr));
+
+ fd = socket (h->h_addrtype, SOCK_STREAM, 0);
+ if (fd == -1 ||
+ connect (fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
+ camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
+ "Could not connect to %s (port %s): %s",
+ service->url->host, service->url->port,
+ strerror(errno));
+ if (fd > -1)
+ close (fd);
+ return FALSE;
+ }
+
+ store->ostream = camel_stream_fs_new_with_fd (fd);
+ store->istream = camel_stream_buffer_new (store->ostream,
+ CAMEL_STREAM_BUFFER_READ);
+
+ /* Read the greeting */
+ buf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (store->istream));
+ if (!buf) {
+ return -1;
+ }
+
+ g_free (buf);
+
+ /* get a list of extensions that the server supports */
+ if (CAMEL_NNTP_OK == camel_nntp_command (store, NULL, "LIST EXTENSIONS")) {
+ char *ext_response = camel_nntp_command_get_additional_data(store);
+
+ g_free (ext_response);
+ }
+
+ return TRUE;
+}
+
+static gboolean
+nntp_store_disconnect (CamelService *service, CamelException *ex)
+{
+ CamelNNTPStore *store = CAMEL_NNTP_STORE (service);
+
+ if (!service->connected)
+ return TRUE;
+
+ camel_nntp_command (store, NULL, "QUIT");
+
+ if (store->newsrc)
+ camel_nntp_newsrc_write (store->newsrc);
+
+ if (!service_class->disconnect (service, ex))
+ return FALSE;
+
+ gtk_object_unref (GTK_OBJECT (store->ostream));
+ gtk_object_unref (GTK_OBJECT (store->istream));
+ store->ostream = NULL;
+ store->istream = NULL;
+ return TRUE;
+}
+
+static char *
+nntp_store_get_name (CamelService *service, gboolean brief)
+{
+ /* Same info for long and brief... */
+ return g_strdup_printf ("USENET news via %s", service->url->host);
+}
static CamelFolder *
nntp_store_get_folder (CamelStore *store, const gchar *folder_name,
@@ -68,7 +158,8 @@ nntp_store_get_folder (CamelStore *store, const gchar *folder_name,
/* if we haven't already read our .newsrc, read it now */
if (!nntp_store->newsrc)
- nntp_store->newsrc = camel_nntp_newsrc_read_for_server (CAMEL_SERVICE(store)->url->host);
+ nntp_store->newsrc =
+ camel_nntp_newsrc_read_for_server (CAMEL_SERVICE(store)->url->host);
/* check if folder has already been created */
/* call the standard routine for that when */
@@ -83,11 +174,10 @@ nntp_store_get_folder (CamelStore *store, const gchar *folder_name,
*/
CF_CLASS (new_folder)->init (new_folder, store, NULL,
folder_name, ".", FALSE, ex);
-
+
return new_folder;
}
-
static char *
nntp_store_get_folder_name (CamelStore *store, const char *folder_name,
CamelException *ex)
@@ -95,23 +185,31 @@ nntp_store_get_folder_name (CamelStore *store, const char *folder_name,
return g_strdup (folder_name);
}
-static char *
-nntp_store_get_name (CamelService *service, gboolean brief)
+static void
+finalize (GtkObject *object)
{
- /* Same info for long and brief... */
- return g_strdup_printf ("USENET news via %s", service->url->host);
-}
+ CamelException ex;
+ camel_exception_init (&ex);
+ nntp_store_disconnect (CAMEL_SERVICE (object), &ex);
+ camel_exception_clear (&ex);
+}
static void
camel_nntp_store_class_init (CamelNNTPStoreClass *camel_nntp_store_class)
{
+ GtkObjectClass *object_class =
+ GTK_OBJECT_CLASS (camel_nntp_store_class);
CamelStoreClass *camel_store_class = CAMEL_STORE_CLASS (camel_nntp_store_class);
CamelServiceClass *camel_service_class = CAMEL_SERVICE_CLASS (camel_nntp_store_class);
service_class = gtk_type_class (camel_service_get_type ());
/* virtual method overload */
+ object_class->finalize = finalize;
+
+ camel_service_class->connect = nntp_store_connect;
+ camel_service_class->disconnect = nntp_store_disconnect;
camel_service_class->get_name = nntp_store_get_name;
camel_store_class->get_folder = nntp_store_get_folder;
@@ -152,107 +250,6 @@ camel_nntp_store_get_type (void)
return camel_nntp_store_type;
}
-/**
- * camel_nntp_store_open: Connect to the server if we are currently
- * disconnected.
- * @store: the store
- * @ex: a CamelException
- *
- **/
-void
-camel_nntp_store_open (CamelNNTPStore *store, CamelException *ex)
-{
- CamelService *service = CAMEL_SERVICE (store);
- if (!camel_service_is_connected (service))
- nntp_connect (service, ex);
-}
-
-/**
- * camel_nntp_store_close: Close the connection to the server
- * @store: the store
- * @ex: a CamelException
- *
- **/
-void
-camel_nntp_store_close (CamelNNTPStore *store, gboolean expunge,
- CamelException *ex)
-{
- camel_nntp_command (store, NULL, "QUIT");
-
- nntp_disconnect (CAMEL_SERVICE (store), ex);
-}
-
-static gboolean
-nntp_connect (CamelService *service, CamelException *ex)
-{
- struct hostent *h;
- struct sockaddr_in sin;
- int fd;
- char *buf;
- CamelNNTPStore *store = CAMEL_NNTP_STORE (service);
-
- if (!service_class->connect (service, ex))
- return FALSE;
-
- h = camel_service_gethost (service, ex);
- if (!h)
- return FALSE;
-
- sin.sin_family = h->h_addrtype;
- sin.sin_port = htons (service->url->port ? service->url->port : NNTP_PORT);
- memcpy (&sin.sin_addr, h->h_addr, sizeof (sin.sin_addr));
-
- fd = socket (h->h_addrtype, SOCK_STREAM, 0);
- if (fd == -1 ||
- connect (fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
- camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
- "Could not connect to %s (port %s): %s",
- service->url->host, service->url->port,
- strerror(errno));
- if (fd > -1)
- close (fd);
- return FALSE;
- }
-
- store->ostream = camel_stream_fs_new_with_fd (fd);
- store->istream = camel_stream_buffer_new (store->ostream,
- CAMEL_STREAM_BUFFER_READ);
-
- /* Read the greeting */
- buf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (store->istream));
- if (!buf) {
- return -1;
- }
-
- g_free (buf);
-
- /* get a list of extensions that the server supports */
- if (CAMEL_NNTP_OK == camel_nntp_command (store, NULL, "LIST EXTENSIONS")) {
- char *ext_response = camel_nntp_command_get_additional_data(store);
-
- g_free (ext_response);
- }
-
- return TRUE;
-}
-
-static gboolean
-nntp_disconnect (CamelService *service, CamelException *ex)
-{
- CamelNNTPStore *store = CAMEL_NNTP_STORE (service);
-
- if (!service->connected)
- return TRUE;
-
- if (!service_class->disconnect (service, ex))
- return FALSE;
-
- gtk_object_unref (GTK_OBJECT (store->ostream));
- gtk_object_unref (GTK_OBJECT (store->istream));
- store->ostream = NULL;
- store->istream = NULL;
- return TRUE;
-}
/**
* camel_nntp_command: Send a command to a NNTP server.
@@ -291,7 +288,7 @@ camel_nntp_command (CamelNNTPStore *store, char **ret, char *fmt, ...)
/* make sure we're connected */
if (store->ostream == NULL)
- nntp_connect (CAMEL_SERVICE (store), ex);
+ nntp_store_connect (CAMEL_SERVICE (store), ex);
if (camel_exception_get_id (ex)) {
camel_exception_free (ex);
@@ -479,18 +476,42 @@ gchar *
camel_nntp_store_get_toplevel_dir (CamelNNTPStore *store)
{
CamelURL *url = CAMEL_SERVICE (store)->url;
- char *news_dir;
char *top_dir;
+ extern char *evolution_dir;
g_assert(url != NULL);
- news_dir = gnome_util_prepend_user_home ("evolution/news");
-
- top_dir = g_strdup_printf( "%s/%s",
- news_dir,
+ top_dir = g_strdup_printf( "%s/news/%s",
+ evolution_dir,
url->host );
- g_free (news_dir);
-
return top_dir;
}
+
+static gboolean
+ensure_news_dir_exists (CamelNNTPStore *store)
+{
+ gchar *dir = camel_nntp_store_get_toplevel_dir (store);
+ struct stat sb;
+ int rv;
+
+ rv = stat (dir, &sb);
+ if (-1 == rv && errno != ENOENT) {
+ g_free (dir);
+ return FALSE;
+ }
+
+ if (S_ISDIR (sb.st_mode)) {
+ g_free (dir);
+ return TRUE;
+ }
+ else {
+ rv = mkdir (dir, 0777);
+ g_free (dir);
+
+ if (-1 == rv)
+ return FALSE;
+ else
+ return TRUE;
+ }
+}