     RETURN basedate;
   END IF;
 
-  temp_txt   := substring(repeatrule from ''UNTIL=([0-9TZ]+)(;|$)'');
+  temp_txt   := substring(repeatrule from 'UNTIL=([0-9TZ]+)(;|$)');
   IF temp_txt IS NOT NULL AND temp_txt::timestamp with time zone < earliest THEN
     RETURN NULL;
   END IF;
 
-  frequency  := substring(repeatrule from ''FREQ=([A-Z]+)(;|$)'');
+  frequency  := substring(repeatrule from 'FREQ=([A-Z]+)(;|$)');
   IF frequency IS NULL THEN
     RETURN NULL;
   END IF;
 
   past_repeats = 0;
   length = 1;
-  temp_txt   := substring(repeatrule from ''INTERVAL=([0-9]+)(;|$)'');
+  temp_txt   := substring(repeatrule from 'INTERVAL=([0-9]+)(;|$)');
   IF temp_txt IS NOT NULL THEN
     length     := temp_txt::int;
     basediff   := earliest - basedate;
 
-    -- RAISE NOTICE ''Frequency: %, Length: %(%), Basediff: %'', frequency, length, temp_txt, basediff;
+    -- RAISE NOTICE 'Frequency: %, Length: %(%), Basediff: %', frequency, length, temp_txt, basediff;
 
     -- Calculate the number of past periods between our base date and our earliest date
-    IF frequency = ''WEEKLY'' OR frequency = ''DAILY'' THEN
-      past_repeats := extract(''epoch'' from basediff)::INT8 / 86400;
-      -- RAISE NOTICE ''Days: %'', past_repeats;
-      IF frequency = ''WEEKLY'' THEN
+    IF frequency = 'WEEKLY' OR frequency = 'DAILY' THEN
+      past_repeats := extract('epoch' from basediff)::INT8 / 86400;
+      -- RAISE NOTICE 'Days: %', past_repeats;
+      IF frequency = 'WEEKLY' THEN
         past_repeats := past_repeats / 7;
       END IF;
     ELSE
-      past_repeats = extract( ''years'' from basediff );
-      IF frequency = ''MONTHLY'' THEN
-        past_repeats = (past_repeats *12) + extract( ''months'' from basediff );
+      past_repeats = extract( 'years' from basediff );
+      IF frequency = 'MONTHLY' THEN
+        past_repeats = (past_repeats *12) + extract( 'months' from basediff );
       END IF;
     END IF;
     IF length IS NOT NULL THEN

  RETURN our_answer;

END;
' LANGUAGE 'plpgsql' IMMUTABLE STRICT;


CREATE or REPLACE FUNCTION usr_is_role( INT, TEXT ) RETURNS BOOLEAN AS '
  SELECT EXISTS( SELECT 1 FROM role_member JOIN roles USING(role_no) WHERE role_member.user_no=$1 AND roles.role_name=$2 )
' LANGUAGE 'sql' IMMUTABLE STRICT;

CREATE or REPLACE FUNCTION get_permissions( INT, INT ) RETURNS TEXT AS '
DECLARE
  in_from ALIAS FOR $1;
  in_to   ALIAS FOR $2;

  tmp_confers1 TEXT;
  tmp_confers2 TEXT;
  tmp_txt TEXT;
  dbg TEXT DEFAULT '';
  r RECORD;
  counter INT;
BEGIN
  -- Self can always have full access
  IF in_from = in_to THEN
    RETURN 'A';
  END IF;

  -- dbg := 'S-';
  SELECT rt1.confers INTO out_confers FROM relationship r1 JOIN relationship_type rt1 USING ( rt_id )
                    WHERE r1.from_user = in_from AND r1.to_user = in_to AND NOT usr_is_role(r1.to_user,'Group');
  IF FOUND THEN
    RETURN dbg || out_confers;
  END IF;
  -- RAISE NOTICE 'No simple relationships between % and %', in_from, in_to;

  out_confers := '';
  FOR r IN SELECT rt1.confers AS r1, rt2.confers AS r2 FROM relationship r1 JOIN relationship_type rt1 USING(rt_id)
              JOIN relationship r2 ON r1.to_user=r2.from_user JOIN relationship_type rt2 ON r2.rt_id=rt2.rt_id
         WHERE r1.from_user=in_from AND r2.to_user=in_to
           AND EXISTS( SELECT 1 FROM role_member JOIN roles USING(role_no) WHERE role_member.user_no=r1.to_user AND roles.role_name='Group')
           AND NOT EXISTS( SELECT 1 FROM role_member JOIN roles USING(role_no) WHERE role_member.user_no=r2.to_user AND roles.role_name='Group')
           AND NOT EXISTS( SELECT 1 FROM role_member JOIN roles USING(role_no) WHERE role_member.user_no=r1.from_user AND roles.role_name='Group')
  LOOP
    -- RAISE NOTICE 'Permissions to group % from group %', r.r1, r.r2;
    -- FIXME: This is an oversimplification
    -- dbg := 'C-';
    tmp_confers1 := r.r1;
    tmp_confers2 := r.r2;
    IF tmp_confers1 != tmp_confers2 THEN
      IF tmp_confers1 ~* 'A' THEN
        -- Ensure that A is expanded to all supported privs before being used as a mask
        tmp_confers1 := 'AFBRWU';
      END IF;
      IF tmp_confers2 ~* 'A' THEN
        -- Ensure that A is expanded to all supported privs before being used as a mask
        tmp_confers2 := 'AFBRWU';
      END IF;
      -- RAISE NOTICE 'Expanded permissions to group % from group %', tmp_confers1, tmp_confers2;
      tmp_txt = '';
      FOR counter IN 1 .. length(tmp_confers2) LOOP
        IF tmp_confers1 ~* substring(tmp_confers2,counter,1) THEN
          tmp_txt := tmp_txt || substring(tmp_confers2,counter,1);
