last-login-plugin.c revision 3008b30900f4de3e76f1a6d289444eb694f3d17c
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen/* Copyright (c) 2014 Dovecot authors, see the included COPYING file */
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#define LAST_LOGIN_DEFAULT_KEY_PREFIX "last-login/"
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(last_login_user_module,
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic void last_login_dict_deinit(struct mail_user *user)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic void last_login_user_deinit(struct mail_user *user)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic void last_login_dict_commit(int ret ATTR_UNUSED, void *context)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen /* don't deinit the dict immediately here, lib-dict will just crash */
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen luser->to = timeout_add(0, last_login_dict_deinit, user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic void last_login_mail_user_created(struct mail_user *user)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen /* we want to handle only logged in users,
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen not lda's raw user or accessed shared users */
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen dict_value = mail_user_plugin_getenv(user, "last_login_dict");
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen if (dict_init(dict_value, DICT_DATA_TYPE_STRING, user->username,
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen i_error("last_login_dict: dict_init(%s) failed: %s",
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen luser = p_new(user->pool, struct last_login_user, 1);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen MODULE_CONTEXT_SET(user, last_login_user_module, luser);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen key_name = mail_user_plugin_getenv(user, "last_login_key");
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen key_name = t_strdup_printf(LAST_LOGIN_DEFAULT_KEY_PREFIX"%s",
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen key_name = t_strconcat(DICT_PATH_SHARED, key_name, NULL);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen dict_set(trans, key_name, dec2str(ioloop_time));
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen dict_transaction_commit_async(&trans, last_login_dict_commit, user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic struct mail_storage_hooks last_login_mail_storage_hooks = {
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen .mail_user_created = last_login_mail_user_created
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenvoid last_login_plugin_init(struct module *module)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen mail_storage_hooks_add(module, &last_login_mail_storage_hooks);