"Fossies" - the Fresh Open Source Software archive

Member "evolution-brutus-1.2.35/camel/camel-brutus-provider.c" of archive evolution-brutus-1.2.35.tar.gz:


/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *    Authors: Jules Colding <colding@42tools.com>
 *
 *    Brutus Camel provider registration source code.
 *
 *    Copyright (C) 2005-2007 OMC Denmark ApS.
 *
 *    This program is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU General Public License as
 *    published by the Free Software Foundation; either version 2 of
 *    the License, or (at your option) any later version.
 *
 *    This program 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 General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; 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

#include <glib.h>
#include <glib/gstdio.h>
#include <string.h>

#include <camel/camel-provider.h>
#include <camel/camel-service.h>
#include <camel/camel-i18n.h>
#include <camel/camel-url.h>

#include <server/brutus.h>
#include <server/brutus_eds.h>
#include <server/brutus_util.h>
#include <session/brutusd.h>
#include "camel-brutus-store.h"
#include "camel-brutus-transport.h"

static int brutus_auto_detect_cb(CamelURL *url,
				 GHashTable **auto_detected,
				 CamelException *ex);

static guint brutus_url_hash(gconstpointer key);

static gint brutus_url_equal(gconstpointer a,
			     gconstpointer b);


CamelProviderConfEntry brutus_conf_entries[] = {
	/* override the labels/defaults of the standard settings */
	{ CAMEL_PROVIDER_CONF_LABEL, "hostname", NULL,
	  /* i18n: the '_' should appear before the same letter it
	     does in the evolution:mail-config.glade "_Server"
	     translation (or not at all) */
	  N_("Microsoft Exchange _Server:"), N_("<DNS resolvable name or IP address>") },

	/* override the labels/defaults of the standard settings */
	{ CAMEL_PROVIDER_CONF_LABEL, "username", NULL,
	  /* i18n: the '_' should appear before the same letter it
	     does in the evolution:mail-config.glade "User_name"
	     translation (or not at all) */
	  N_("Windows User_name:"), N_("<Your Windows domain user name>") },

	/* default settings */
	{ CAMEL_PROVIDER_CONF_SECTION_START, "mailcheck", NULL,
	  N_("Checking for new mail"), "1" },
	{ CAMEL_PROVIDER_CONF_CHECKBOX, "check_all", NULL,
	  N_("Check for new messages in all folders"), "0" },
	{ CAMEL_PROVIDER_CONF_SECTION_END },

	{ CAMEL_PROVIDER_CONF_SECTION_START, "general", NULL,
	  N_("Options") },
	{ CAMEL_PROVIDER_CONF_CHECKBOX, "filter", NULL,
	  N_("Apply filters to new messages in Inbox on this server"), "0" },
	{ CAMEL_PROVIDER_CONF_CHECKBOX, "filter_junk", NULL,
	  N_("Check new messages for Junk contents"), "0" },
	{ CAMEL_PROVIDER_CONF_CHECKBOX, "filter_junk_inbox", "filter_junk",
	  N_("Only check for Junk messages in the INBOX folder"), "0" },
	{ CAMEL_PROVIDER_CONF_CHECKBOX, "sync_offline", NULL,
	  N_("Automatically synchronize all email folders locally"), "0" },
	{ CAMEL_PROVIDER_CONF_SECTION_END },

	/* extra Brutus settings */
	{ CAMEL_PROVIDER_CONF_SECTION_START, "brutus_settings", NULL,
	  N_("Brutus Settings") },

	{ CAMEL_PROVIDER_CONF_ENTRY, "exchange_mailbox_email", NULL,
	  N_("Exchange Email Address:"), ""},

	/* { CAMEL_PROVIDER_CONF_ENTRY, "exchange_mailbox", NULL, */
	/*   N_("Exchange Mailbox (optional):"), ""}, */

	/* { CAMEL_PROVIDER_CONF_ENTRY, "exchange_mailbox_dn", NULL, */
	/*   N_("Mailbox Distinguished Name (optional):"), ""}, */

	{ CAMEL_PROVIDER_CONF_ENTRY, "windows_domain", NULL,
	  N_("Windows Domain:"), ""},

	{ CAMEL_PROVIDER_CONF_ENTRY, "brutus_server", NULL,
	  N_("Brutus Server:"), N_("<DNS resolvable name or IP address>") },

	{ CAMEL_PROVIDER_CONF_ENTRY, "iiop_port", NULL,
	  N_("Brutus Server Port:"), BRUTUS_DEFAULT_PORT_STR },

	{ CAMEL_PROVIDER_CONF_CHECKBOX, "expect_other_clients", NULL,
	  N_("Check if you use other client applications to access this mailbox"), "1" },

	{ CAMEL_PROVIDER_CONF_CHECKBOX, "public_folders", NULL,
	  N_("Support Exchange Public Folders"), "0" },

	{ CAMEL_PROVIDER_CONF_CHECKBOX, "create_calendar_account", NULL,
	  N_("Support the Exchange Calendar"), "0" },

	{ CAMEL_PROVIDER_CONF_CHECKBOX, "create_todo_account", NULL,
	  N_("Support Exchange Tasks"), "0" },

	{ CAMEL_PROVIDER_CONF_CHECKBOX, "create_note_account", NULL,
	  N_("Support Exchange Notes"), "0" },

	{ CAMEL_PROVIDER_CONF_CHECKBOX, "create_personal_address_book_account", NULL,
	  N_("Support Exchange Personal Address Book (EXPERIMENTAL)"), "0" },

	{ CAMEL_PROVIDER_CONF_CHECKBOX, "create_global_address_list_account", NULL,
	  N_("Support Exchange Global Address List (EXPERIMENTAL)"), "0" },

	{ CAMEL_PROVIDER_CONF_SECTION_END }, // Brutus settings
	{ CAMEL_PROVIDER_CONF_END }
};


static CamelProvider brutus_provider = {
	.protocol = "brutus",
	.name = N_("Microsoft Exchange (Courtesy of Brutus)"),

	.description = N_("Brutus enabled messaging and collaboration on Microsoft Exchange servers"),

	.domain = "mail",

	.flags = CAMEL_PROVIDER_IS_REMOTE
	| CAMEL_PROVIDER_IS_SOURCE
	| CAMEL_PROVIDER_IS_STORAGE
	| CAMEL_PROVIDER_DISABLE_SENT_FOLDER
#ifdef BRUTUS_HAVE_SSL /* enable SSLIOP when tested */
	| CAMEL_PROVIDER_SUPPORTS_SSL
#endif
	,

	.url_flags = CAMEL_URL_NEED_USER | CAMEL_URL_NEED_HOST | CAMEL_URL_NEED_PORT,

	.extra_conf = brutus_conf_entries,

	.translation_domain = GETTEXT_PACKAGE,

	.auto_detect = brutus_auto_detect_cb,

	.url_hash = brutus_url_hash,

	.url_equal = brutus_url_equal,

	.priv = NULL,

	/* ... */
};

CamelServiceAuthType camel_brutus_password_authtype = {
	N_("Password"),

	N_("This option will connect to the Exchange server using a "
	   "plaintext password."),

	"Plain",
	TRUE
};

#ifdef BRUTUS_HAVE_ENCRYPTED_AUTH
CamelServiceAuthType camel_brutus_encrypted_authtype = {
	N_("Secure Password"),

	N_("This option will connect to the Exchange server using an "
	   "encrypted password."),

	"Encrypted",
	TRUE
};
#endif

static inline gboolean
check_equal(const char *s1,
	    const char *s2)
{
	switch (!!s1 + !!s2) {
	case 0:
		return TRUE;
	case 1:
		return FALSE;
	}

	return (!strcmp(s1, s2));
}

static inline char *
mailbox(const CamelURL *url)
{
	char *retv = NULL;

	if (url)
		retv = brutus_strip_trailing_and_leading_whitespace(camel_url_get_param((CamelURL *)url, "effective_mailbox"));

	return retv;
}

static inline char *
exchange_server(const CamelURL *url)
{
	char *retv = NULL;

	if (url)
		retv = brutus_strip_trailing_and_leading_whitespace(camel_url_get_param((CamelURL *)url, "exchange_server"));

	return retv;
}

static int
brutus_auto_detect_cb(CamelURL *url,
		      GHashTable **auto_detected,
		      CamelException *ex)
{
	*auto_detected = g_hash_table_new(g_str_hash, g_str_equal);


	g_hash_table_insert(*auto_detected, g_strdup("user"), brutus_strip_trailing_and_leading_whitespace(url->user));
	g_hash_table_insert(*auto_detected, g_strdup("mailbox"), mailbox(url));
	g_hash_table_insert(*auto_detected, g_strdup("exchange_server"), exchange_server(url));

	return 0;
}

void
camel_provider_module_init(void)
{
	d("Initializing evolution-brutus runtime environment");
	if (!brutus_eds_initialize())
		d("WARNING: Possible initialization error for evolution-brutus runtime environment");

	brutus_provider.object_types[CAMEL_PROVIDER_STORE] =  camel_brutus_store_get_type() ;
	brutus_provider.object_types[CAMEL_PROVIDER_TRANSPORT] = camel_brutus_transport_get_type();
	brutus_provider.authtypes = g_list_prepend (brutus_provider.authtypes,
						    &camel_brutus_password_authtype
#ifdef BRUTUS_HAVE_ENCRYPTED_AUTH
						    , &camel_brutus_encrypted_authtype
#endif
		);

	bindtextdomain(GETTEXT_PACKAGE, BRUTUS_LOCALEDIR);
	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
	textdomain(GETTEXT_PACKAGE);

	camel_provider_register(&brutus_provider);
}

static void
add_hash(guint *hash, char *s)
{
	if (s)
		*hash ^= g_str_hash(s);
}

static guint
brutus_url_hash(gconstpointer key)
{
	const CamelURL *u = (CamelURL *)key;
	guint hash = 0;
	gchar *mb = mailbox(u);
	gchar *user = brutus_strip_trailing_and_leading_whitespace(u->user);
	gchar *host = brutus_strip_trailing_and_leading_whitespace(u->host);

	add_hash(&hash, user); /* Windows user */
	add_hash(&hash, host); /* Exchange server */
	add_hash(&hash, mb);   /* Exchange mailbox */

	g_free(mb);
	g_free(user);
	g_free(host);

	return hash;
}

static gint
brutus_url_equal(gconstpointer a, gconstpointer b)
{
	const CamelURL *u1 = a;
	const CamelURL *u2 = b;
	gchar *mb1 = mailbox(u1);
	gchar *mb2 = mailbox(u2);
	gchar *ex1 = exchange_server(u1);
	gchar *ex2 = exchange_server(u2);
	gchar *user1 = brutus_strip_trailing_and_leading_whitespace(u1->user);
	gchar *user2 = brutus_strip_trailing_and_leading_whitespace(u2->user);

	gboolean retv = (check_equal(user1, user2)
			 && check_equal(mb1, mb2)
			 && check_equal(ex1, ex2));

	g_free(user1);
	g_free(user2);
	g_free(mb1);
	g_free(mb2);
	g_free(ex1);
	g_free(ex2);

	return retv;
}