/* Copyright (C) 2012 Damir Bikmuhametov */

/* Installation: see README file */
 
#include <stdlib.h>
#include <string.h>

#include "whoson.h"

#include "lib.h"
#include "network.h"
#include "mail-user.h"
#include "mail-storage-private.h"

#define IPADDR_LEN 16
#define USERNAME_LEN 100

static char ipaddr[IPADDR_LEN];
static char username[USERNAME_LEN];

const char *whoson_plugin_version = DOVECOT_VERSION;

void whoson_mail_user_created(struct mail_user *user)
{
	if (!IPADDR_IS_V4(user->remote_ip))
		i_error("WHOSON: Only IPv4 addresses are supported");
	else {
		(void)i_strocpy(ipaddr, net_ip2addr(user->remote_ip), IPADDR_LEN);
		(void)i_strocpy(username, user->username, USERNAME_LEN);

		if (wso_login(ipaddr, username, NULL, 0) == -1)
			i_error("wso_login() failed");
	}
}

static struct mail_storage_hooks whoson_mail_storage_hooks = {
    .mail_user_created = whoson_mail_user_created,
};

void whoson_plugin_init(struct module *module)
{
    mail_storage_hooks_add(module, &whoson_mail_storage_hooks);
    i_debug("%s called", __FUNCTION__);
}

void whoson_plugin_deinit(void)
{
    mail_storage_hooks_remove(&whoson_mail_storage_hooks);
    i_debug("%s called", __FUNCTION__);
}
