-- Roundcube Webmail initial database structure

--
-- Table structure for table contacts and related
--

CREATE TABLE IF NOT EXISTS contacts (
  contact_id integer NOT NULL PRIMARY KEY,
  user_id integer NOT NULL,
  changed datetime NOT NULL default '0000-00-00 00:00:00',
  del tinyint NOT NULL default '0',
  name varchar(128) NOT NULL default '',
  email text NOT NULL default '',
  firstname varchar(128) NOT NULL default '',
  surname varchar(128) NOT NULL default '',
  vcard text NOT NULL default '',
  words text NOT NULL default ''
);

CREATE INDEX IF NOT EXISTS ix_contacts_user_id ON contacts(user_id, del);


CREATE TABLE IF NOT EXISTS contactgroups (
  contactgroup_id integer NOT NULL PRIMARY KEY,
  user_id integer NOT NULL default '0',
  changed datetime NOT NULL default '0000-00-00 00:00:00',
  del tinyint NOT NULL default '0',
  name varchar(128) NOT NULL default ''
);

CREATE INDEX IF NOT EXISTS ix_contactgroups_user_id ON contactgroups(user_id, del);


CREATE TABLE IF NOT EXISTS contactgroupmembers (
  contactgroup_id integer NOT NULL,
  contact_id integer NOT NULL default '0',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY (contactgroup_id, contact_id)
);

CREATE INDEX IF NOT EXISTS ix_contactgroupmembers_contact_id ON contactgroupmembers (contact_id);

--
-- Table structure for table identities
--

CREATE TABLE IF NOT EXISTS identities (
  identity_id integer NOT NULL PRIMARY KEY,
  user_id integer NOT NULL default '0',
  changed datetime NOT NULL default '0000-00-00 00:00:00',
  del tinyint NOT NULL default '0',
  standard tinyint NOT NULL default '0',
  name varchar(128) NOT NULL default '',
  organization varchar(128) default '',
  email varchar(128) NOT NULL default '',
  "reply-to" varchar(128) NOT NULL default '',
  bcc varchar(128) NOT NULL default '',
  signature text NOT NULL default '',
  html_signature tinyint NOT NULL default '0'
);

CREATE INDEX IF NOT EXISTS ix_identities_user_id ON identities(user_id, del);
CREATE INDEX IF NOT EXISTS ix_identities_email ON identities(email, del);

--
-- Table structure for table users
--

CREATE TABLE IF NOT EXISTS users (
  user_id integer NOT NULL PRIMARY KEY,
  username varchar(128) NOT NULL default '',
  mail_host varchar(128) NOT NULL default '',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  last_login datetime DEFAULT NULL,
  failed_login datetime DEFAULT NULL,
  failed_login_counter integer DEFAULT NULL,
  language varchar(5),
  preferences text NOT NULL default ''
);

CREATE UNIQUE INDEX IF NOT EXISTS ix_users_username ON users(username, mail_host);

--
-- Table structure for table session
--

CREATE TABLE IF NOT EXISTS session (
  sess_id varchar(128) NOT NULL PRIMARY KEY,
  changed datetime NOT NULL default '0000-00-00 00:00:00',
  ip varchar(40) NOT NULL default '',
  vars text NOT NULL
);

CREATE INDEX IF NOT EXISTS ix_session_changed ON session (changed);

--
-- Table structure for table dictionary
--

CREATE TABLE IF NOT EXISTS dictionary (
    user_id integer DEFAULT NULL,
   "language" varchar(5) NOT NULL,
    data text NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS ix_dictionary_user_language ON dictionary (user_id, "language");

--
-- Table structure for table searches
--

CREATE TABLE IF NOT EXISTS searches (
  search_id integer NOT NULL PRIMARY KEY,
  user_id integer NOT NULL DEFAULT '0',
  "type" smallint NOT NULL DEFAULT '0',
  name varchar(128) NOT NULL,
  data text NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS ix_searches_user_type_name ON searches (user_id, type, name);

--
-- Table structure for table cache
--

CREATE TABLE IF NOT EXISTS cache (
  user_id integer NOT NULL default 0,
  cache_key varchar(128) NOT NULL default '',
  expires datetime DEFAULT NULL,
  data text NOT NULL,
  PRIMARY KEY (user_id, cache_key)
);

CREATE INDEX IF NOT EXISTS ix_cache_expires ON cache(expires);

--
-- Table structure for table cache_shared
--

CREATE TABLE IF NOT EXISTS cache_shared (
  cache_key varchar(255) NOT NULL,
  expires datetime DEFAULT NULL,
  data text NOT NULL,
  PRIMARY KEY (cache_key)
);

CREATE INDEX IF NOT EXISTS ix_cache_shared_expires ON cache_shared(expires);

--
-- Table structure for table cache_index
--

CREATE TABLE IF NOT EXISTS cache_index (
    user_id integer NOT NULL,
    mailbox varchar(255) NOT NULL,
    expires datetime DEFAULT NULL,
    valid smallint NOT NULL DEFAULT '0',
    data text NOT NULL,
    PRIMARY KEY (user_id, mailbox)
);

CREATE INDEX IF NOT EXISTS ix_cache_index_expires ON cache_index (expires);

--
-- Table structure for table cache_thread
--

CREATE TABLE IF NOT EXISTS cache_thread (
    user_id integer NOT NULL,
    mailbox varchar(255) NOT NULL,
    expires datetime DEFAULT NULL,
    data text NOT NULL,
    PRIMARY KEY (user_id, mailbox)
);

CREATE INDEX IF NOT EXISTS ix_cache_thread_expires ON cache_thread (expires);

--
-- Table structure for table cache_messages
--

CREATE TABLE IF NOT EXISTS cache_messages (
    user_id integer NOT NULL,
    mailbox varchar(255) NOT NULL,
    uid integer NOT NULL,
    expires datetime DEFAULT NULL,
    data text NOT NULL,
    flags integer NOT NULL DEFAULT '0',
    PRIMARY KEY (user_id, mailbox, uid)
);

CREATE INDEX IF NOT EXISTS ix_cache_messages_expires ON cache_messages (expires);

--
-- Table structure for table filestore
--

CREATE TABLE IF NOT EXISTS filestore (
    file_id integer NOT NULL PRIMARY KEY,
    user_id integer NOT NULL,
    context varchar(32) NOT NULL,
    filename varchar(128) NOT NULL,
    mtime integer NOT NULL,
    data text NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS ix_filestore_user_id ON filestore(user_id, context, filename);

--
-- Table structure for table system
--

CREATE TABLE IF NOT EXISTS system (
  name varchar(64) NOT NULL PRIMARY KEY,
  value text NOT NULL
);

INSERT OR REPLACE INTO system (name, value) VALUES ('roundcube-version', '2019092900');

/**
 * Roundcube Calendar
 *
 * Plugin to add a calendar to Roundcube.
 *
 * @author Lazlo Westerhof
 * @author Thomas Bruederli
 * @author Albert Lee
 * @licence GNU AGPL
 * @copyright (c) 2010 Lazlo Westerhof - Netherlands
 * @copyright (c) 2014 Kolab Systems AG
 *
 **/

CREATE TABLE IF NOT EXISTS calendars (
  calendar_id integer NOT NULL PRIMARY KEY,
  user_id integer NOT NULL default '0',
  name varchar(255) NOT NULL default '',
  color varchar(255) NOT NULL default '',
  showalarms tinyint(1) NOT NULL default '1',
  CONSTRAINT fk_calendars_user_id FOREIGN KEY (user_id)
    REFERENCES users(user_id)
);

CREATE TABLE IF NOT EXISTS events (
  event_id integer NOT NULL PRIMARY KEY,
  calendar_id integer NOT NULL default '0',
  recurrence_id integer NOT NULL default '0',
  uid varchar(255) NOT NULL default '',
  instance varchar(16) NOT NULL default '',
  isexception tinyint(1) NOT NULL default '0',
  created datetime NOT NULL default '1000-01-01 00:00:00',
  changed datetime NOT NULL default '1000-01-01 00:00:00',
  sequence integer NOT NULL default '0',
  start datetime NOT NULL default '1000-01-01 00:00:00',
  end datetime NOT NULL default '1000-01-01 00:00:00',
  recurrence varchar(255) default NULL,
  title varchar(255) NOT NULL,
  description text NOT NULL,
  location varchar(255) NOT NULL default '',
  categories varchar(255) NOT NULL default '',
  url varchar(255) NOT NULL default '',
  all_day tinyint(1) NOT NULL default '0',
  free_busy tinyint(1) NOT NULL default '0',
  priority tinyint(1) NOT NULL default '0',
  sensitivity tinyint(1) NOT NULL default '0',
  status varchar(32) NOT NULL default '',
  alarms text default NULL,
  attendees text default NULL,
  notifyat datetime default NULL,
  CONSTRAINT fk_events_calendar_id FOREIGN KEY (calendar_id)
    REFERENCES calendars(calendar_id)
);

CREATE TABLE IF NOT EXISTS attachments (
  attachment_id integer NOT NULL PRIMARY KEY,
  event_id integer NOT NULL default '0',
  filename varchar(255) NOT NULL default '',
  mimetype varchar(255) NOT NULL default '',
  size integer NOT NULL default '0',
  data text NOT NULL default '',
  CONSTRAINT fk_attachment_event_id FOREIGN KEY (event_id)
    REFERENCES events(event_id)
);

CREATE TABLE IF NOT EXISTS itipinvitations (
  token varchar(64) NOT NULL PRIMARY KEY,
  event_uid varchar(255) NOT NULL,
  user_id integer NOT NULL default '0',
  event text NOT NULL,
  expires datetime NOT NULL default '1000-01-01 00:00:00',
  cancelled tinyint(1) NOT NULL default '0',
  CONSTRAINT fk_itipinvitations_user_id FOREIGN KEY (user_id)
    REFERENCES users(user_id)
);

CREATE INDEX IF NOT EXISTS ix_itipinvitations_uid ON itipinvitations(user_id, event_uid);

INSERT OR REPLACE INTO system (name, value) VALUES ('calendar-database-version', '2015022700');

/**
 * iCAL Client
 *
 * @version @package_version@
 * @author Daniel Morlock <daniel.morlock@awesome-it.de>
 *
 * Copyright (C) Awesome IT GbR <info@awesome-it.de>
 * Adapted for SQLite for cPanel, L.L.C. <cpanel.net> in 2019
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

CREATE TABLE IF NOT EXISTS ical_calendars (
  calendar_id integer NOT NULL PRIMARY KEY,
  user_id integer NOT NULL DEFAULT '0',
  name varchar(255) NOT NULL,
  color varchar(8) NOT NULL,
  showalarms tinyint(1) NOT NULL DEFAULT '1',

  ical_url varchar(255) NOT NULL,
  ical_user varchar(255) DEFAULT NULL,
  ical_pass varchar(1024) DEFAULT NULL,

  ical_last_change timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_ical_calendars_user_id FOREIGN KEY (user_id)
    REFERENCES users(user_id)
);

CREATE INDEX IF NOT EXISTS ical_user_name_idx ON ical_calendars(user_id, name);

CREATE TRIGGER IF NOT EXISTS UpdateLastTimeForiCalCalendar
  AFTER UPDATE
  ON ical_calendars
  FOR EACH ROW
BEGIN
  UPDATE ical_calendars SET ical_last_change = CURRENT_TIMESTAMP WHERE calendar_id = old.calendar_id; END;

CREATE TABLE IF NOT EXISTS ical_events (
  event_id integer NOT NULL PRIMARY KEY,
  calendar_id integer NOT NULL default '0',
  recurrence_id integer NOT NULL default '0',
  uid varchar(255) NOT NULL default '',
  instance varchar(16) NOT NULL default '',
  isexception tinyint(1) NOT NULL default '0',
  created datetime NOT NULL default '1000-01-01 00:00:00',
  changed datetime NOT NULL default '1000-01-01 00:00:00',
  sequence integer NOT NULL default '0',
  start datetime NOT NULL default '1000-01-01 00:00:00',
  end datetime NOT NULL default '1000-01-01 00:00:00',
  recurrence varchar(255) default NULL,
  title varchar(255) NOT NULL,
  description text NOT NULL,
  location varchar(255) NOT NULL default '',
  categories varchar(255) NOT NULL default '',
  url varchar(255) NOT NULL default '',
  all_day tinyint(1) NOT NULL default '0',
  free_busy tinyint(1) NOT NULL default '0',
  priority tinyint(1) NOT NULL default '0',
  sensitivity tinyint(1) NOT NULL default '0',
  status varchar(32) NOT NULL default '',
  alarms text default NULL,
  attendees text default NULL,
  notifyat datetime default NULL,

  ical_url varchar(255) NOT NULL,
  ical_last_change timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

  CONSTRAINT fk_ical_events_calendar_id FOREIGN KEY (calendar_id)
    REFERENCES ical_calendars(calendar_id)
);

CREATE INDEX IF NOT EXISTS ical_uid_idx ON ical_events(uid);
CREATE INDEX IF NOT EXISTS ical_recurrence_idx ON ical_events(recurrence_id);
CREATE INDEX IF NOT EXISTS ical_calendar_notify_idx ON ical_events(calendar_id,notifyat);

CREATE TRIGGER IF NOT EXISTS UpdateLastTimeForiCalEvents
  AFTER UPDATE
  ON ical_events
  FOR EACH ROW
BEGIN
  UPDATE ical_events SET ical_last_change = CURRENT_TIMESTAMP WHERE event_id = old.event_id; END;

CREATE TABLE IF NOT EXISTS ical_attachments (
  attachment_id integer NOT NULL PRIMARY KEY,
  event_id integer NOT NULL DEFAULT '0',
  filename varchar(255) NOT NULL DEFAULT '',
  mimetype varchar(255) NOT NULL DEFAULT '',
  size integer NOT NULL DEFAULT '0',
  data longtext NOT NULL,
  CONSTRAINT fk_ical_attachments_event_id FOREIGN KEY (event_id)
    REFERENCES ical_events(event_id)
);

REPLACE INTO system (name, value) VALUES ('calendar-ical-version', '2015022700');

/**
 * CalDAV Client
 *
 * @version @package_version@
 * @author Daniel Morlock <daniel.morlock@awesome-it.de>
 *
 * Copyright (C) Awesome IT GbR <info@awesome-it.de>
 * Adapted for SQLite for cPanel, L.L.C. <cpanel.net> in 2019
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

CREATE TABLE IF NOT EXISTS caldav_calendars (
  calendar_id integer NOT NULL PRIMARY KEY,
  user_id integer NOT NULL DEFAULT '0',
  name varchar(255) NOT NULL,
  color varchar(8) NOT NULL,
  showalarms tinyint(1) NOT NULL DEFAULT '1',
  readonly tinyint(1) NOT NULL DEFAULT '1',
  caldav_url varchar(255) NOT NULL,
  caldav_tag varchar(255) DEFAULT NULL,
  caldav_user varchar(255) DEFAULT NULL,
  caldav_pass varchar(1024) DEFAULT NULL,
  caldav_oauth_provider varchar(255) DEFAULT NULL,
  caldav_last_change timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_caldav_calendars_user_id FOREIGN KEY (user_id)
    REFERENCES users(user_id)
);

CREATE INDEX IF NOT EXISTS caldav_user_name_idx ON caldav_calendars(user_id, name);

CREATE TRIGGER IF NOT EXISTS UpdateLastTimeForCaldavCalendar
  AFTER UPDATE
  ON caldav_calendars
  FOR EACH ROW
BEGIN
  UPDATE caldav_calendars SET caldav_last_change = CURRENT_TIMESTAMP WHERE calendar_id = old.calendar_id; END;

CREATE TABLE IF NOT EXISTS caldav_events (
  event_id integer NOT NULL PRIMARY KEY,
  calendar_id integer NOT NULL DEFAULT '0',
  recurrence_id integer NOT NULL DEFAULT '0',
  uid varchar(255) NOT NULL DEFAULT '',
  instance varchar(16) NOT NULL DEFAULT '',
  isexception tinyint(1) NOT NULL DEFAULT '0',
  created datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
  changed datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
  sequence integer NOT NULL DEFAULT '0',
  start datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
  end datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
  recurrence varchar(255) DEFAULT NULL,
  title varchar(255) NOT NULL,
  description text NOT NULL,
  location varchar(255) NOT NULL DEFAULT '',
  categories varchar(255) NOT NULL DEFAULT '',
  url varchar(255) NOT NULL DEFAULT '',
  all_day tinyint(1) NOT NULL DEFAULT '0',
  free_busy tinyint(1) NOT NULL DEFAULT '0',
  priority tinyint(1) NOT NULL DEFAULT '0',
  sensitivity tinyint(1) NOT NULL DEFAULT '0',
  status varchar(32) NOT NULL DEFAULT '',
  alarms text NULL DEFAULT NULL,
  attendees text DEFAULT NULL,
  notifyat datetime DEFAULT NULL,
  caldav_url varchar(255) NOT NULL,
  caldav_tag varchar(255) DEFAULT NULL,
  caldav_last_change timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT uk_caldav_event UNIQUE (calendar_id,recurrence_id,uid,caldav_tag,instance),
  CONSTRAINT fk_caldav_events_calendar_id FOREIGN KEY (calendar_id)
    REFERENCES caldav_calendars(calendar_id)
);

CREATE INDEX IF NOT EXISTS caldav_uid_idx ON caldav_events(uid);
CREATE INDEX IF NOT EXISTS caldav_recurrence_idx ON caldav_events(recurrence_id);
CREATE INDEX IF NOT EXISTS caldav_calendar_notify_idx ON caldav_events(calendar_id,notifyat);

CREATE TRIGGER IF NOT EXISTS UpdateLastTimeForCalDAVEvents
  AFTER UPDATE
  ON caldav_events
  FOR EACH ROW
BEGIN
  UPDATE caldav_events SET caldav_last_change = CURRENT_TIMESTAMP WHERE event_id = old.event_id; END;

CREATE TABLE IF NOT EXISTS caldav_attachments (
  attachment_id integer NOT NULL PRIMARY KEY,
  event_id integer NOT NULL DEFAULT '0',
  filename varchar(255) NOT NULL DEFAULT '',
  mimetype varchar(255) NOT NULL DEFAULT '',
  size integer NOT NULL DEFAULT '0',
  data longtext NOT NULL,
  CONSTRAINT fk_caldav_attachments_event_id FOREIGN KEY (event_id)
    REFERENCES caldav_events(event_id)
);

REPLACE INTO system (name, value) VALUES ('calendar-caldav-version', '2016072000');
