CREATE TABLE nmail_user_login
(
  id bigserial NOT NULL,
  user_name text NOT NULL,
  "password" text NOT NULL,
  from_date timestamp,
  thru_date timestamp,
  CONSTRAINT nmail_user_login_pkey PRIMARY KEY (id)
) 
WITHOUT OIDS;
ALTER TABLE nmail_user_login OWNER TO nmail;

CREATE TABLE nmail_user
(
  user_id int8 UNIQUE NOT NULL PRIMARY KEY REFERENCES nmail_user_login(id),
  last_name text,
  first_name text
) 
WITHOUT OIDS;
ALTER TABLE nmail_user OWNER TO nmail;

CREATE TABLE email_address
(
  id bigserial NOT NULL PRIMARY KEY,
  email_address text NOT NULL,
  duplicated bool NOT NULL DEFAULT FALSE,
  wrong bool NOT NULL DEFAULT FALSE,
  user_id int8 NOT NULL REFERENCES nmail_user(user_id)
) 
WITHOUT OIDS;
ALTER TABLE email_address OWNER TO nmail;

CREATE TABLE mail_folder
(
  pop3_host text NOT NULL,
  pop3_password text NOT NULL,
  pop3_user_name text NOT NULL,
  port int4 NOT NULL DEFAULT 110,
  ssl bool NOT NULL DEFAULT FALSE,
  user_id int8 UNIQUE PRIMARY KEY NOT NULL REFERENCES nmail_user (user_id)
) 
WITHOUT OIDS;
ALTER TABLE mail_folder OWNER TO nmail;

CREATE TABLE smtp_settings
(
  host text NOT NULL,
  smtp_user_name text NOT NULL,
  debug bool NOT NULL DEFAULT FALSE,
  port int8 NOT NULL DEFAULT 25,
  tls bool NOT NULL DEFAULT FALSE,
  smtp_password text NOT NULL,
  protocol text NOT NULL DEFAULT 'smtp',
  auth bool NOT NULL DEFAULT TRUE,
  CONSTRAINT smtp_settings_pkey PRIMARY KEY (host, smtp_user_name)
) 
WITHOUT OIDS;
ALTER TABLE smtp_settings OWNER TO nmail;

CREATE TABLE user_proces
(
  id bigserial NOT NULL PRIMARY KEY,
  proces_type text NOT NULL,
  user_id int8 NOT NULL REFERENCES nmail_user (user_id)
) 
WITHOUT OIDS;
ALTER TABLE user_proces OWNER TO nmail;

CREATE TABLE email_sending_proces
(
  id int8 UNIQUE PRIMARY KEY NOT NULL REFERENCES user_proces (id),
  email_count int8 NOT NULL,
  sent_email int8 NOT NULL,
  failed_email int8 NOT NULL,
  end_proces timestamp,
  start_proces timestamp NOT NULL,
  email bytea
) 
WITHOUT OIDS;
ALTER TABLE email_sending_proces OWNER TO nmail;

CREATE TABLE user_restriction
(
  able_to_set_sender bool NOT NULL DEFAULT FALSE,
  can_import bool NOT NULL DEFAULT FALSE,
  user_id int8 UNIQUE PRIMARY KEY NOT NULL REFERENCES nmail_user (user_id),
  can_set_pop3 bool DEFAULT FALSE
) 
WITHOUT OIDS;
ALTER TABLE user_restriction OWNER TO nmail;

CREATE TABLE user_setting
(
  bounce_address text,
  bounced_delivered bool NOT NULL DEFAULT FALSE,
  default_sender_address text,
  default_sender_name text,
  user_id int8 UNIQUE PRIMARY KEY NOT NULL REFERENCES nmail_user (user_id),
  message_limit int8,
  time_limit int8
) 
WITHOUT OIDS;
ALTER TABLE user_setting OWNER TO nmail;

CREATE TABLE nmail_setting
(
    id serial NOT NULL PRIMARY KEY,
    mail_block_size int NOT NULL DEFAULT 10,
    block_waiting_time int8 NOT NULL DEFAULT 2000
)
WITHOUT OIDS;
ALTER TABLE nmail_setting OWNER TO nmail;