%conststring(31) local authority mailbox = "LOCAL_AUTHORITY_MAILBOX"

%recordformat authority message fm(%record(message fm) system part,
                                   %integer code, result,
                                   %string(*)%name username,
                                   %string(*)%name password, old password,
                                   %record(fsys access fm)%name fsys access,
                                   %string(*)%name default directory,
                                   %string(*)%name textual description)

%constinteger authority enquire user       = 1
%constinteger authority validate user      = 2
%constinteger authority create user        = 3
%constinteger authority modify password    = 4
%constinteger authority modify user        = 5
%constinteger authority delete user        = 6
%constinteger authority print by username  = 7
%constinteger authority print by ID        = 8


%routine LA transact(%record(authority message fm)%name m)
   %ownrecord(mailbox fm)%name authority mailbox == nil
   %record(semaphore fm) s
   %record(mailbox fm) b
   %record(authority message fm)%name r
   %integer i
      %if authority mailbox == nil %start
         !! printstring("Lookup authority mailbox");  newline
         %unless FS lookup(local authority mailbox, i) %start
            !! printstring("No local authority mailbox??");  newline
            m_result = -1
            %return
         %finish
         authority mailbox == record(i)
      %finish
      !! printstring("Transact: authority mailbox at ")
      !! phex(addr(authority mailbox));  newline
      setup semaphore(s)
      setup mailbox(b, s)
      send message(m, authority mailbox, b)
      r == receive message(b)
      %signal 3,,, "Unexpected authority reply" %unless r == m
%end

%integerfn LA enquire user(%string(255) user,
                           %record(fsys access fm)%name fsys access,
                           %string(*)%name default directory,
                           %string(*)%name textual description)
   %record(authority message fm) m
      !! printstring("LA enquire user: ");  printstring(user);  newline
      setup message(m, size of(m))
      m_code = authority enquire user
      m_username == user
      m_fsys access == fsys access
      m_default directory == default directory
      m_textual description == textual description
      LA transact(m)
      %result = m_result
%end

%integerfn LA validate user(%string(255) user, pass,
                            %record(fsys access fm)%name fsys access,
                            %string(*)%name default directory)
   %record(authority message fm) m
      !! printstring("LA validate user: ");  printstring(user)
      !! space;  printstring(pass);  newline
      setup message(m, size of(m))
      m_code = authority validate user
      m_username == user
      m_password == pass
      m_fsys access == fsys access
      m_default directory == default directory
      LA transact(m)
      %result = m_result
%end

%integerfn LA create user(%string(255) user, pass,
                          %record(fsys access fm)%name fsys access,
                          %string(255) default directory, description)
   %record(authority message fm) m
      setup message(m, size of(m))
      m_code = authority create user
      m_username == user
      m_password == pass
      m_fsys access == fsys access
      m_default directory == default directory
      m_textual description == description
      LA transact(m)
      %result = m_result
%end

%integerfn LA modify password(%string(255) user,
                              %string(255) old password, new password)
   %record(authority message fm) m
      setup message(m, size of(m))
      m_code = authority modify password
      m_username == user
      m_password == new password
      m_old password == old password
      LA transact(m)
      %result = m_result
%end

%integerfn LA modify user(%string(255) user,
                          %record(fsys access fm)%name fsys access,
                          %string(255) default directory, description)
   %record(authority message fm) m
      setup message(m, size of(m))
      m_code = authority modify user
      m_username == user
      m_fsys access == fsys access
      m_default directory == default directory
      m_textual description == description
      LA transact(m)
      %result = m_result
%end

%integerfn LA delete user(%string(255) user,
                          %string(*)%name default directory)
   %record(authority message fm) m
      setup message(m, size of(m))
      m_code = authority delete user
      m_username == user
      m_default directory == default directory
      LA transact(m)
      %result = m_result
%end

%routine LA print database(%integer how)
   %record(authority message fm) m
      setup message(m, size of(m))
      %if how = 0 %then m_code = authority print by username %c
                  %else m_code = authority print by ID
      LA transact(m)
%end

%end %of %file
