CREATE or REPLACE FUNCTION loginagent(integer,integer,integer) RETURNS integer AS '
   DECLARE
       res integer;
       qid integer;
       plid integer;
       dep_only varchar(3);
       cdid integer;
       qcid integer;
       uid ALIAS for $1;
       qline ALIAS for $2;
       fromext ALIAS for $3;
   BEGIN

-- see if we already logged in
       SELECT INTO res queue_contact.id
       from   queue_contact left join queue on queue.id = queue_contact.queue
                          left join phoneline on phoneline.id = queue.phoneline
       where  phoneline.extension = qline and queue_contact.contact = uid;

       IF res IS NOT NULL THEN
           RAISE NOTICE ''user % already logged into queue %, queue_contact.id = %'', uid, qline, res;
           return ''1''; -- success
       END IF;

-- not already logged in. Find the proper queue from the phoneline id.
       select into qid queue.id
       from   queue left join phoneline on phoneline.id = queue.phoneline
       where  extension = qline;

       IF qid IS NULL THEN
           RAISE NOTICE ''queue % not found'', qline;
           return ''0''; -- error: queue not found
       END IF;
-- okay, the queue is real, now log us in.
       if fromext = 1 THEN
         SELECT into plid id
         FROM   phoneline where function = ''contact'' and lookupid = uid
         limit  1;
       else
         SELECT into plid id
         FROM   phoneline where extension = fromext and function = ''contact'' and lookupid = uid
         limit  1;
       end if;

       IF plid IS NULL THEN
           RAISE NOTICE ''phoneline % not found for user %'', fromext, uid;
           return ''0''; -- our phoneline is not found
       END IF;

       SELECT into dep_only queue.department_only
         FROM queue
        WHERE id = qid and department_only = ''no'';

       IF dep_only IS NULL THEN
         RAISE NOTICE ''department_only set to yes'';
         SELECT into cdid contact_department.id
           FROM contact_department, queue
          WHERE queue.id = qid AND queue.department = contact_department.department
            AND contact_department.contact = uid;

         IF cdid IS NULL THEN
             RAISE NOTICE ''contact % not in same department as queue %'', uid, qid;
             return ''0'';
         END IF;

       END IF;

       insert into queue_contact(queue, contact, phoneline) values (qid, uid, plid);
       select into qcid queue_contact.id from queue_contact where (contact=uid and queue=qid);
       RAISE NOTICE ''user % logged in to queue %'', uid, qline;
       INSERT INTO activate(action, type, which, status, created ) 
              VALUES (''add'', ''queue_contact'', qcid, ''QUEUED'', NOW() );
       INSERT INTO activate(action, type, which, status, created )
              VALUES (''modify'', ''queue'', qid, ''QUEUED'', NOW() );
       return ''1'';
   END;
' LANGUAGE plpgsql;

CREATE or REPLACE FUNCTION logoutagent(integer,integer,integer) RETURNS integer AS '
   DECLARE
       res integer;
       qid integer;
       plid integer;
       qcid integer;
       queue_id integer;
       uid ALIAS for $1;
       qline ALIAS for $2;
       fromext ALIAS for $3;
   BEGIN

       select into qid queue.id
       from   queue left join phoneline on phoneline.id = queue.phoneline
       where  extension = qline;

       IF qid IS NULL THEN
           RAISE NOTICE ''queue % not found'', qline;
           return ''1''; -- error: queue not found, but still logged off
       END IF;

-- okay, the queue is real, now log us out
       if fromext = 1 THEN
         SELECT into plid id
         FROM   phoneline where function = ''contact'' and lookupid = uid
         limit  1;
       else
         SELECT into plid id
         FROM   phoneline where extension = fromext and function = ''contact'' and lookupid = uid
         limit  1;
       end if;

       SELECT into queue_id queue.id
         FROM queue,phoneline where queue.phoneline=phoneline.id and phoneline.extension=qline;

       select into qcid queue_contact.id from queue_contact where (contact = uid and phoneline = plid and queue = queue_id);
       IF qcid IS NULL THEN
           RAISE NOTICE ''user % alreaddy logged out of queue %'', uid, qline;
           return ''1'';
       END IF;

       delete from queue_contact where id = qcid;
       RAISE NOTICE ''user % logged out of queue %'', uid, qline;
       -- note: must be in this order: first queue_contact, then queue
       INSERT INTO activate(action, type, which, status, created )
              VALUES (''delete'', ''queue_contact'', qcid, ''QUEUED'', NOW() );
       INSERT INTO activate(action, type, which, status, created )
              VALUES (''modify'', ''queue'', qid, ''QUEUED'', NOW() );
       return ''1'';
   END;
' LANGUAGE plpgsql;
