-- sql/actions/people.sql
-- This file is part of Decimail; see http://decimail.org
-- (C) 2005-2007 Philip Endecott
 
-- 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
-- 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, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 

drop table people cascade;
drop table people_addresses cascade;
drop view people_mailboxes cascade;
drop trigger people_changed on people cascade;
delete from actions where mailbox_pattern='People/%';

begin;

create table people (
    username  text  not null,
    name      text  not null,
    primary key (username, name)
);

-- To make it possible to create new mailboxes under People/ there
-- must be at least one mailbox already present, since empty folders are
-- not found by Decimail's LIST command.  So you'll need to run something
-- like this:

-- insert into people (username, name) values ('your-username-here', 'Example');


create table people_addresses (
    username  text  not null,
    name      text  not null,
    email     text  not null,
    primary key (username, name, email),
    foreign key (username, name) references people
        on delete cascade on update cascade
);


create view people_mailboxes as select
    username,
    ('People/' || name) as mailbox_name,
    ('select m.msg_id from u_messages m '
      'join people_addresses pa on (m.from_addr = pa.email) '
      'where pa.username=\'' || username || '\''
      'and pa.name=\'' || name || '\''

      'union select m.msg_id from u_messages m '
      'join u_recipients r using(msg_id) '
      'join people_addresses pa on (r.addr = pa.email) '
      'where pa.username=\'' || username || '\''
      'and pa.name=\'' || name || '\'') as query
    from people;


create trigger people_changed
	after insert or update or delete on people
	for each statement
	execute procedure update_mailboxes();


insert into actions (mailbox_pattern, create_action, delete_action,
                     rename_action, copy_action)
values ('People/%',
        'insert into people (username,name) values (''$U'',replace(''$MB'',''People/'',''''))',
        'delete from people where username=''$U'' and name=replace(''$MB'',''People/'','''')',
        'update people set name=replace(''$NMB'',''People/'','''') where username=''$U'' and name=replace(''$OMB'',''People/'','''')',
        'insert into people_addresses select ''$U'' as username, replace(''$MB'',''People/'','''') as name, from_addr as email from messages where msg_id=$MSG_ID');

end;
