-- Host: localhost

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";

--
-- Database: `vmail`
--

-- --------------------------------------------------------

--
-- Table structure for table `vm_aliases`
--

CREATE TABLE `vm_aliases` (
  `id` int(10) UNSIGNED NOT NULL,
  `mbox_id` int(10) UNSIGNED NOT NULL,
  `alias` varchar(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `vm_autoresponders`
--

CREATE TABLE `vm_autoresponders` (
  `id` int(10) UNSIGNED NOT NULL,
  `mbox_id` int(10) UNSIGNED NOT NULL,
  `subject` varchar(128) NOT NULL,
  `body` text NOT NULL,
  `mode` enum('Vacation','Autoresponder') NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT 1,
  `start` datetime DEFAULT NULL,
  `end` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `vm_domains`
--

CREATE TABLE `vm_domains` (
  `id` int(11) UNSIGNED NOT NULL,
  `domain` varchar(255) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '1 for enabled, 0 for disabled',
  `mbox_limit` smallint(6) UNSIGNED DEFAULT 10 COMMENT 'Maximum number of mailboxes for this domain',
  `mbox_quota_default` smallint(6) UNSIGNED DEFAULT 1 COMMENT 'Default mailbox quota in GB',
  `mbox_ratelimit_default` smallint(6) DEFAULT 100 COMMENT 'Default hourly rate limit for new mboxes'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `vm_filters`
--

CREATE TABLE `vm_filters` (
  `id` int(10) UNSIGNED NOT NULL,
  `mbox_id` int(11) UNSIGNED NOT NULL,
  `filter` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `vm_forwards`
--

CREATE TABLE `vm_forwards` (
  `id` int(10) UNSIGNED NOT NULL,
  `mbox_id` int(10) UNSIGNED NOT NULL,
  `forward_to` varchar(128) NOT NULL,
  `save_local` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `vm_greylisting`
--

CREATE TABLE `vm_greylisting` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `relay_hostname` varchar(255) DEFAULT NULL,
  `relay_ip` varchar(80) DEFAULT NULL,
  `sender` varchar(255) DEFAULT NULL,
  `recipient` varchar(255) DEFAULT NULL,
  `message_id` varchar(255) DEFAULT NULL,
  `block_expires` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
  `record_expires` datetime NOT NULL DEFAULT '9999-12-31 23:59:59',
  `create_time` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
  `type` enum('AUTO','MANUAL') NOT NULL DEFAULT 'MANUAL',
  `passcount` bigint(20) NOT NULL DEFAULT 0,
  `blockcount` bigint(20) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `vm_greylisting_resenders`
--

CREATE TABLE `vm_greylisting_resenders` (
  `id` bigint(20) NOT NULL,
  `hostname` varchar(255) NOT NULL,
  `count` bigint(20) UNSIGNED NOT NULL DEFAULT 1,
  `timestamp` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `vm_mboxes`
--

CREATE TABLE `vm_mboxes` (
  `id` int(10) UNSIGNED NOT NULL,
  `domain_id` int(10) UNSIGNED NOT NULL,
  `mbox` varchar(128) NOT NULL,
  `username` varchar(128) NOT NULL,
  `passwd` char(128) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT 1,
  `quota` int(10) UNSIGNED DEFAULT NULL,
  `ratelimit` smallint(6) DEFAULT NULL,
  `filter` tinyint(4) NOT NULL DEFAULT 2
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `vm_aliases`
--
ALTER TABLE `vm_aliases`
  ADD PRIMARY KEY (`id`),
  ADD KEY `mbox_delete_aliases` (`mbox_id`);

--
-- Indexes for table `vm_autoresponders`
--
ALTER TABLE `vm_autoresponders`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `autoresponder` (`mbox_id`);

--
-- Indexes for table `vm_domains`
--
ALTER TABLE `vm_domains`
  ADD PRIMARY KEY (`id`),
  ADD KEY `domain` (`domain`(191));

--
-- Indexes for table `vm_filters`
--
ALTER TABLE `vm_filters`
  ADD PRIMARY KEY (`id`),
  ADD KEY `mbox_delete_filters` (`mbox_id`);

--
-- Indexes for table `vm_forwards`
--
ALTER TABLE `vm_forwards`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `mbox_id` (`mbox_id`);

--
-- Indexes for table `vm_greylisting`
--
ALTER TABLE `vm_greylisting`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `vm_greylisting_resenders`
--
ALTER TABLE `vm_greylisting_resenders`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `vm_mboxes`
--
ALTER TABLE `vm_mboxes`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `username` (`username`),
  ADD KEY `email` (`domain_id`,`mbox`) USING BTREE;

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `vm_aliases`
--
ALTER TABLE `vm_aliases`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `vm_autoresponders`
--
ALTER TABLE `vm_autoresponders`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `vm_domains`
--
ALTER TABLE `vm_domains`
  MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `vm_filters`
--
ALTER TABLE `vm_filters`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `vm_forwards`
--
ALTER TABLE `vm_forwards`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `vm_greylisting`
--
ALTER TABLE `vm_greylisting`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `vm_greylisting_resenders`
--
ALTER TABLE `vm_greylisting_resenders`
  MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `vm_mboxes`
--
ALTER TABLE `vm_mboxes`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `vm_aliases`
--
ALTER TABLE `vm_aliases`
  ADD CONSTRAINT `mbox_delete_aliases` FOREIGN KEY (`mbox_id`) REFERENCES `vm_mboxes` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table `vm_autoresponders`
--
ALTER TABLE `vm_autoresponders`
  ADD CONSTRAINT `mbox_delete_autoresponders` FOREIGN KEY (`mbox_id`) REFERENCES `vm_mboxes` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table `vm_filters`
--
ALTER TABLE `vm_filters`
  ADD CONSTRAINT `mbox_delete_filters` FOREIGN KEY (`mbox_id`) REFERENCES `vm_mboxes` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table `vm_forwards`
--
ALTER TABLE `vm_forwards`
  ADD CONSTRAINT `mbox_delete_forwards` FOREIGN KEY (`mbox_id`) REFERENCES `vm_mboxes` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table `vm_mboxes`
--
ALTER TABLE `vm_mboxes`
  ADD CONSTRAINT `domain_delete_mboxes` FOREIGN KEY (`domain_id`) REFERENCES `vm_domains` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
COMMIT;
