pgsql/update_tables_0.5.x-0.6.pgsql
branchv0.6.x
changeset 518 5ec2068d02af
parent 515 09fa019bb330
child 521 75d1c0d6bb8f
equal deleted inserted replaced
517:88466a6ba3ae 518:5ec2068d02af
   457 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
   457 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
   458 --      varchar localpart
   458 --      varchar localpart
   459 --      varchar the_domain
   459 --      varchar the_domain
   460 -- Returns: recipient_destination records
   460 -- Returns: recipient_destination records
   461 -- ---
   461 -- ---
       
   462 CREATE OR REPLACE FUNCTION _interpolate_destination(
       
   463     IN destination varchar, localpart varchar, IN the_domain varchar)
       
   464     RETURNS varchar
       
   465 AS $$
       
   466     DECLARE
       
   467         result varchar(320);
       
   468     BEGIN
       
   469         IF position('%' in destination) = 0 THEN
       
   470             RETURN destination;
       
   471         END IF;
       
   472         result := replace(destination, '%n', localpart);
       
   473         result := replace(result, '%d', the_domain);
       
   474         result := replace(result, '%=', localpart || '=' || the_domain);
       
   475         RETURN result;
       
   476     END;
       
   477 $$ LANGUAGE plpgsql STABLE
       
   478 RETURNS NULL ON NULL INPUT
       
   479 EXTERNAL SECURITY INVOKER;
       
   480 
   462 CREATE OR REPLACE FUNCTION postfix_virtual_alias_map(
   481 CREATE OR REPLACE FUNCTION postfix_virtual_alias_map(
   463     IN localpart varchar, IN the_domain varchar)
   482     IN localpart varchar, IN the_domain varchar)
   464     RETURNS SETOF recipient_destination
   483     RETURNS SETOF recipient_destination
   465 AS $$
   484 AS $$
   466     DECLARE
   485     DECLARE
   469         catchall_cursor refcursor;
   488         catchall_cursor refcursor;
   470         recipient varchar(320) := localpart || '@' || the_domain;
   489         recipient varchar(320) := localpart || '@' || the_domain;
   471         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
   490         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
   472     BEGIN
   491     BEGIN
   473         FOR record IN
   492         FOR record IN
   474             SELECT recipient, destination
   493             SELECT recipient,
       
   494                 _interpolate_destination(destination, localpart, the_domain)
   475               FROM alias
   495               FROM alias
   476              WHERE gid = did
   496              WHERE gid = did
   477                AND address = localpart
   497                AND address = localpart
   478             LOOP
   498             LOOP
   479                 RETURN NEXT record;
   499                 RETURN NEXT record;
   487             -- over mailboxes/relocated, which is not what we want. Therefore,
   507             -- over mailboxes/relocated, which is not what we want. Therefore,
   488             -- we must first find out if the query is for an existing mailbox
   508             -- we must first find out if the query is for an existing mailbox
   489             -- or relocated entry and return the identity mapping if that is
   509             -- or relocated entry and return the identity mapping if that is
   490             -- the case
   510             -- the case
   491             OPEN catchall_cursor FOR
   511             OPEN catchall_cursor FOR
   492                 SELECT recipient, destination
   512                 SELECT recipient,
       
   513                     _interpolate_destination(destination, localpart, the_domain)
   493                   FROM catchall
   514                   FROM catchall
   494                  WHERE gid = did;
   515                  WHERE gid = did;
   495             FETCH NEXT FROM catchall_cursor INTO recordc;
   516             FETCH NEXT FROM catchall_cursor INTO recordc;
   496 
   517 
   497             IF recordc IS NOT NULL THEN
   518             IF recordc IS NOT NULL THEN