RRichard Russonmodule: tidy cleanup
06861aed创建于 4月22日历史提交
/**
 * @file
 * Definition of the Alias Module
 *
 * @authors
 * Copyright (C) 2025-2026 Richard Russon <rich@flatcap.org>
 *
 * @copyright
 * 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, see <http://www.gnu.org/licenses/>.
 */

/**
 * @page alias_module Definition of the Alias Module
 *
 * Definition of the Alias Module
 */

#include "config.h"
#include <stdbool.h>
#include <stddef.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "alias.h"
#include "module_data.h"
#include "reverse.h"

extern struct ConfigDef AliasVars[];

extern const struct Command AliasCommands[];

/**
 * alias_init - Initialise a Module - Implements Module::init()
 */
static bool alias_init(struct NeoMutt *n)
{
  struct AliasModuleData *mod_data = MUTT_MEM_CALLOC(1, struct AliasModuleData);
  neomutt_set_module_data(n, MODULE_ID_ALIAS, mod_data);

  mod_data->notify = notify_new();
  notify_set_parent(mod_data->notify, n->notify);

  STAILQ_INIT(&mod_data->alternates);
  STAILQ_INIT(&mod_data->unalternates);

  mod_data->alternates_notify = notify_new();
  notify_set_parent(mod_data->alternates_notify, n->notify);

  ARRAY_INIT(&mod_data->aliases);
  mod_data->reverse_aliases = alias_reverse_init();

  mod_data->groups = groups_new();

  mod_data->auto_subscribe_cache = NULL;
  STAILQ_INIT(&mod_data->mail);
  STAILQ_INIT(&mod_data->subscribed);
  STAILQ_INIT(&mod_data->unmail);
  STAILQ_INIT(&mod_data->unsubscribed);

  return true;
}

/**
 * alias_config_define_variables - Define the Config Variables - Implements Module::config_define_variables()
 */
static bool alias_config_define_variables(struct NeoMutt *n, struct ConfigSet *cs)
{
  return cs_register_variables(cs, AliasVars);
}

/**
 * alias_commands_register - Register NeoMutt Commands - Implements Module::commands_register()
 */
static bool alias_commands_register(struct NeoMutt *n, struct CommandArray *ca)
{
  return commands_register(ca, AliasCommands);
}

/**
 * alias_cleanup - Clean up a Module - Implements Module::cleanup()
 */
static bool alias_cleanup(struct NeoMutt *n, void *data)
{
  struct AliasModuleData *mod_data = data;

  notify_free(&mod_data->notify);
  notify_free(&mod_data->alternates_notify);

  mutt_regexlist_free(&mod_data->alternates);
  mutt_regexlist_free(&mod_data->unalternates);

  struct Alias **ap = NULL;
  ARRAY_FOREACH(ap, &mod_data->aliases)
  {
    alias_reverse_delete(mod_data->reverse_aliases, *ap);
  }
  aliaslist_clear(&mod_data->aliases);

  alias_reverse_cleanup(&mod_data->reverse_aliases);

  groups_free(&mod_data->groups);

  mutt_hash_free(&mod_data->auto_subscribe_cache);
  mutt_regexlist_free(&mod_data->mail);
  mutt_regexlist_free(&mod_data->subscribed);
  mutt_regexlist_free(&mod_data->unmail);
  mutt_regexlist_free(&mod_data->unsubscribed);

  FREE(&mod_data);
  return true;
}

/**
 * ModuleAlias - Module for the Alias library
 */
const struct Module ModuleAlias = {
  MODULE_ID_ALIAS,
  "alias",
  alias_init,
  NULL, // config_define_types
  alias_config_define_variables,
  alias_commands_register,
  NULL, // gui_init
  NULL, // gui_cleanup
  alias_cleanup,
};