/*
 * SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.com)
 * SPDX-License-Identifier: LGPL-2.0-or-later
 * SPDX-FileContributor: Bertrand Guiheneuf <bertrand@helixcode.com>
 * SPDX-FileContributor: Dan Winship <danw@ximian.com>
 * SPDX-FileContributor: Jeffrey Stedfast <fejj@ximian.com>
 */

/**
 * CamelProvider:
 *
 * Plugin interface for mail storage and transport backends.
 *
 * #CamelProvider is the plugin descriptor structure that describes a mail
 * storage or transport backend (or both). It is the first piece of information
 * loaded from an external backend shared library and serves as the entry point
 * for the plugin mechanism.
 *
 * From the client's perspective, a provider descriptor gives enough
 * information to present the user with the configuration questions required
 * to set up the service, including the list of supported authentication
 * mechanisms.
 *
 * Key fields in the #CamelProvider structure:
 * - `protocol`: The unique, lower-case URI scheme (e.g. `"imap"`).
 * - `name`: A short human-readable name (e.g. `"IMAP"`).
 * - `description`: A longer description of what the backend does.
 * - `object_types`: The #CamelStore and/or #CamelTransport GTypes to
 *   instantiate for this backend.
 * - `authtypes`: The list of supported authentication mechanisms.
 * - `url_flags`: Flags describing which URI fields are required or optional.
 *
 * Providers are loaded from shared libraries via camel_provider_load() and
 * registered with the session using camel_session_add_service(). Once
 * registered, #CamelSession uses the provider's `object_types` to instantiate
 * the appropriate #CamelService subclass when camel_session_get_service() is
 * called with a matching URI.
 **/

/* FIXME: Shouldn't we add a version number to providers ? */

#include "evolution-data-server-config.h"

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <gmodule.h>

#include "camel-provider.h"
#include "camel-string-utils.h"
#include "camel-vee-store.h"
#include "camel-win32.h"
#include "camel.h"

/* table of CamelProviderModule's */
static GHashTable *module_table;
/* table of CamelProvider's */
static GHashTable *provider_table;
static GRecMutex provider_lock;

#define LOCK()		(g_rec_mutex_lock(&provider_lock))
#define UNLOCK()	(g_rec_mutex_unlock(&provider_lock))

CamelProvider *	camel_provider_copy		(CamelProvider *provider);
void		camel_provider_free		(CamelProvider *provider);

G_DEFINE_BOXED_TYPE (CamelProvider, camel_provider, camel_provider_copy, camel_provider_free)

/*
 * camel_provider_copy:
 * @provider: a #CamelProvider to copy
 *
 * The function returns @provider, because providers are not allocated
 * on heap. It's defined only for the introspection purposes.
 *
 * Returns: (transfer full): the @provider
 *
 * Since: 3.24
 */
CamelProvider *
camel_provider_copy (CamelProvider *provider)
{
	return provider;
}

/*
 * camel_provider_free:
 * @provider: a #CamelProvider to copy
 *
 * The function does nothing, because providers are not allocated
 * on heap. It's defined only for the introspection purposes.
 *
 * Since: 3.24
 */
void
camel_provider_free (CamelProvider *provider)
{
}

/* The vfolder provider is always available */
static CamelProvider vee_provider = {
	.protocol = "vfolder",
	.name = N_("Virtual folder email provider"),

	.description = N_("For reading mail as a query of another set of folders"),

	.domain = "vfolder",

	.flags = CAMEL_PROVIDER_IS_STORAGE | CAMEL_PROVIDER_IS_LOCAL,
	.url_flags = CAMEL_URL_NEED_PATH,

	.extra_conf = NULL,

	.port_entries = NULL,

	/* ... */
};

static GOnce setup_once = G_ONCE_INIT;

static void
provider_register_internal (CamelProvider *provider)
{
	CamelProviderConfEntry *conf;
	CamelProviderPortEntry *port;
	GList *link;
	gint ii;

	g_return_if_fail (provider != NULL);
	g_return_if_fail (provider->protocol != NULL);

	LOCK ();

	if (g_hash_table_lookup (provider_table, provider->protocol) != NULL) {
		g_warning (
			"Trying to re-register CamelProvider for protocol '%s'",
			provider->protocol);
		UNLOCK ();
		return;
	}

	/* Translate all strings here */
#define P_(string) dgettext (provider->translation_domain, string)

	provider->name = P_(provider->name);
	if (provider->description)
		provider->description = P_(provider->description);

	conf = provider->extra_conf;
	if (conf != NULL) {
		for (ii = 0; conf[ii].type != CAMEL_PROVIDER_CONF_END; ii++) {
			if (conf[ii].text && conf[ii].text[0])
				conf[ii].text = P_(conf[ii].text);
		}
	}

	for (link = provider->authtypes; link != NULL; link = link->next) {
		CamelServiceAuthType *auth = link->data;

		auth->name = P_(auth->name);
		auth->description = P_(auth->description);
	}

	if (provider->port_entries != NULL) {
		provider->url_flags |= CAMEL_URL_NEED_PORT;
		port = provider->port_entries;
		for (ii = 0; port[ii].port != 0; ii++)
			if (port[ii].desc != NULL)
				port[ii].desc = P_(port[ii].desc);
	} else {
		provider->url_flags &= ~CAMEL_URL_NEED_PORT;
	}

	g_hash_table_insert (
		provider_table,
		(gpointer) provider->protocol, provider);

	UNLOCK ();
}

static gpointer
provider_setup (gpointer param)
{
	module_table = g_hash_table_new (
		(GHashFunc) camel_strcase_hash,
		(GEqualFunc) camel_strcase_equal);
	provider_table = g_hash_table_new (
		(GHashFunc) camel_strcase_hash,
		(GEqualFunc) camel_strcase_equal);

	vee_provider.object_types[CAMEL_PROVIDER_STORE] = CAMEL_TYPE_VEE_STORE;
	provider_register_internal (&vee_provider);

	return NULL;
}

static void
camel_provider_traverse_directory (const gchar *provider_dir,
				   gboolean warn_failure)
{
	GDir *dir;
	const gchar *entry;
	gchar *name, buf[80];

	dir = g_dir_open (provider_dir, 0, NULL);
	if (!dir) {
		if (warn_failure) {
			g_warning (
				"Could not open camel provider directory (%s): %s",
				provider_dir, g_strerror (errno));
		}

		return;
	}

	while ((entry = g_dir_read_name (dir))) {
		CamelProviderModule *m = NULL;
		FILE *fp;
		gchar *ptr;
		const gchar *ep;

		ep = strrchr (entry, '.');
		if (!ep || strcmp (ep, ".urls") != 0)
			continue;

		name = g_strdup_printf ("%s/%s", provider_dir, entry);
		fp = g_fopen (name, "r");
		if (!fp) {
			g_warning (
				"Could not read provider info file %s: %s",
				name, g_strerror (errno));
			g_free (name);
			continue;
		}

		ptr = strrchr (name, '.');
		if (ptr)
			strcpy (ptr, "." G_MODULE_SUFFIX);

		while ((fgets (buf, sizeof (buf), fp))) {
			buf[sizeof (buf) - 1] = '\0';
			ptr = strchr (buf, '\n');
			if (ptr)
				*ptr = '\0';

			if (*buf) {
				gchar *protocol = g_strdup (buf);

				if (!m) {
					m = g_malloc0 (sizeof (*m));
					m->path = name;
				}

				m->types = g_slist_prepend (m->types, protocol);
				g_hash_table_insert (module_table, protocol, m);
			}
		}

		if (!m)
			g_free (name);
		fclose (fp);
	}

	g_dir_close (dir);
}

/**
 * camel_provider_init:
 *
 * Initialize the Camel provider system by reading in the .urls
 * files in the provider directory and creating a hash table mapping
 * URLs to module names.
 *
 * A .urls file has the same initial prefix as the shared library it
 * correspond to, and consists of a series of lines containing the URL
 * protocols that that library handles.
 *
 * TODO: This should be pathed?
 * TODO: This should be plugin-d?
 **/
void
camel_provider_init (void)
{
	static gint loaded = 0;
	const gchar *provider_dir;
	gboolean is_default = FALSE;

	provider_dir = g_getenv (EDS_CAMEL_PROVIDER_DIR);
	if (!provider_dir) {
		provider_dir = CAMEL_PROVIDERDIR;
		is_default = TRUE;
	}

	g_once (&setup_once, provider_setup, NULL);

	if (loaded)
		return;

	loaded = 1;

	if (is_default) {
		GPtrArray *variants;

		variants = camel_util_get_directory_variants (provider_dir, E_DATA_SERVER_PREFIX, TRUE);

		if (variants) {
			guint ii;

			for (ii = 0; ii < variants->len; ii++) {
				const gchar *path = g_ptr_array_index (variants, ii);

				if (path && *path)
					camel_provider_traverse_directory (path, g_strcmp0 (provider_dir, path) == 0);
			}

			g_ptr_array_unref (variants);
		} else {
			camel_provider_traverse_directory (provider_dir, TRUE);
		}
	} else {
		camel_provider_traverse_directory (provider_dir, TRUE);
	}
}

/**
 * camel_provider_load:
 * @path: the path to a shared library
 * @error: return location for a #GError, or %NULL
 *
 * Loads the provider at @path, and calls its initialization function,
 * passing @session as an argument. The provider should then register
 * itself with @session.
 *
 * Returns: %TRUE on success, %FALSE on failure
 **/
gboolean
camel_provider_load (const gchar *path,
                     GError **error)
{
	GModule *module;
	CamelProvider *(*provider_module_init) (void);

	g_once (&setup_once, provider_setup, NULL);

	if (!g_module_supported ()) {
		g_set_error (
			error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			_("Could not load %s: Module loading "
			"not supported on this system."), path);
		return FALSE;
	}

	module = g_module_open (path, G_MODULE_BIND_LAZY);
	if (module == NULL) {
		g_set_error (
			error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			_("Could not load %s: %s"),
			path, g_module_error ());
		return FALSE;
	}

	if (!g_module_symbol (module, "camel_provider_module_init",
			      (gpointer *) &provider_module_init)) {
		g_set_error (
			error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			_("Could not load %s: No initialization "
			"code in module."), path);
		g_module_close (module);
		return FALSE;
	}

	provider_module_init ();

	return TRUE;
}

/**
 * camel_provider_register:
 * @provider: provider object
 *
 * Registers a provider.
 **/
void
camel_provider_register (CamelProvider *provider)
{
	g_once (&setup_once, provider_setup, NULL);

	provider_register_internal (provider);
}

static gint
provider_compare (gconstpointer a,
                  gconstpointer b)
{
	const CamelProvider *cpa = (const CamelProvider *) a;
	const CamelProvider *cpb = (const CamelProvider *) b;

	return strcmp (cpa->name, cpb->name);
}

static void
add_to_list (gpointer key,
             gpointer value,
             gpointer user_data)
{
	GList **list = user_data;

	*list = g_list_prepend(*list, value);
}

/**
 * camel_provider_list:
 * @load: whether or not to load in providers that are not already loaded
 *
 * This returns a list of available providers. If @load is %TRUE, it will
 * first load in all available providers that haven't yet been loaded.
 *
 * Free the returned list with g_list_free().  The #CamelProvider structs
 * in the list are owned by Camel and should not be modified or freed.
 *
 * Returns: (element-type CamelProvider) (transfer container): a #GList of #CamelProvider structs
 **/
GList *
camel_provider_list (gboolean load)
{
	GList *list = NULL;

	/* provider_table can be NULL, so initialize it */
	if (G_UNLIKELY (provider_table == NULL))
		camel_provider_init ();

	g_return_val_if_fail (provider_table != NULL, NULL);

	LOCK ();

	if (load) {
		GList *w;

		g_hash_table_foreach (module_table, add_to_list, &list);
		for (w = list; w; w = w->next) {
			CamelProviderModule *m = w->data;
			GError *error = NULL;

			if (!m->loaded) {
				camel_provider_load (m->path, &error);
				m->loaded = 1;
			}

			if (error != NULL) {
				g_critical (
					"%s: %s", G_STRFUNC,
					error->message);
				g_error_free (error);
			}
		}
		g_list_free (list);
		list = NULL;
	}

	g_hash_table_foreach (provider_table, add_to_list, &list);

	UNLOCK ();

	list = g_list_sort (list, provider_compare);

	return list;
}

/**
 * camel_provider_get:
 * @protocol: a #CamelProvider protocol name
 * @error: return location for a #GError, or %NULL
 *
 * Returns the registered #CamelProvider for @protocol, loading it
 * from disk if necessary.  If no #CamelProvider can be found for
 * @protocol, or the provider module fails to load, the function
 * sets @error and returns %NULL.
 *
 * The returned #CamelProvider is owned by Camel and should not be
 * modified or freed.
 *
 * Returns: a #CamelProvider for @protocol, or %NULL on error
 **/
CamelProvider *
camel_provider_get (const gchar *protocol,
                    GError **error)
{
	CamelProvider *provider = NULL;

	g_return_val_if_fail (protocol != NULL, NULL);
	g_return_val_if_fail (provider_table != NULL, NULL);

	LOCK ();

	provider = g_hash_table_lookup (provider_table, protocol);
	if (provider == NULL) {
		CamelProviderModule *module;

		module = g_hash_table_lookup (module_table, protocol);
		if (module != NULL && !module->loaded) {
			module->loaded = 1;
			if (!camel_provider_load (module->path, error))
				goto fail;
		}
		provider = g_hash_table_lookup (provider_table, protocol);
	}

	if (provider == NULL)
		g_set_error (
			error, CAMEL_SERVICE_ERROR,
			CAMEL_SERVICE_ERROR_URL_INVALID,
			_("No provider available for protocol “%s”"),
			protocol);
fail:
	UNLOCK ();

	return provider;
}
