{ PICS0A.INC - Pascal Integrated Communications System Overlays}
{ 5/25/87 VER. 1.6  Copyright 1987 by les archambault }

overlay function correct_fn(str: FileName): FileName;
{ Correct possible errors in file name }
  var
    i, j: integer;
  begin
    i := 1;                                 { Remove blanks and invalid characters }
    while i <= length(str) do
      if (str[i] in [' ', '*', ',', ':', ';', '=', '?', '_']) or (str[i]>'~')
        then delete(str, i, 1)
        else i := succ(i);
    while (str <> '') and (str[1] = '.') do { Remove leading '.' }
      delete(str, 1, 1);
    i := pos('.', str);                     { Remove redundant '.' }
    j := 1;
    while j <= length(str) do
      if (str[j] = '.') and (j > i)
        then delete(str, j, 1)
        else j := succ(j);
    i := pos('.', str);
    if i = 0                                { Ensure name has '.' }
      then
        begin
          str := copy(str, 1, 8);           { Ensure file name <= 8 characters }
          if length(str) > 0
            then str := str + '.'
        end
      else str := copy(str, 1, min(8, pred(i))) + '.' +
                  copy(str, succ(i), min(3, length(str) - i));
    correct_fn := str
  end;

overlay function compress_fn(name: FileName): FileName;
{ Strip hi bits and remove all blanks from file name }
  var
    i: integer;
  begin
    for i := 1 to length(name) do
      name[i] := chr($7F and ord(name[i]));
    i := pos(' ', name);
    while i > 0 do
      begin
        delete(name, i, 1);
        i := pos(' ', name)
      end;
    compress_fn := name
  end;

overlay procedure get_old_password(pr: StrPr; var valid: boolean);
{ Accept and validate old password.  Only 'Max_Tries' will be allowed. }
  var
    tries: integer;
  begin
    tries := 0;
    repeat
      valid := (user_rec.pw = prompt(pr, len_pw, 'S'));
      tries := succ(tries);
      if (not valid) and (tries < Max_Tries) then
         writeln(usr, 'Falsches Passwort! Bitte wiederholen.')
    until (not online) or valid or (tries > Max_Tries);
    if not valid
      then writeln(USR, 'Nur ', Max_Tries, ' Versuche erlaubt.')
  end;

overlay procedure get_new_password;
{ Accept and validate new password. }
  var
    i,x: integer;
    trial_pw: password;
  begin
    writeln(USR);
    writeln(USR, 'Bitte ein Passwort von 4-', len_pw, ' Zeichen eingeben, um eine');
    writeln(USR, 'unbefugte Benutzung Deines Namens in diesem System zu verhindern.');
    writeln(USR);
    repeat
      repeat
        trial_pw := prompt('Passwort (OHNE Echo) ', len_pw, 'SL');
        i := length(trial_pw);
        if (i < 4) or (i > len_pw)
          then writeln(USR, 'Laenge muss 4-', len_pw, ' Zeichen betragen.')
        else
          begin
            for x:=1 to length(trial_pw) do
              if (not(ord(trial_pw[x]) in [48..57])) and (not(ord(trial_pw[x]) in [65..90]))
                 then i:=0;
            if i=0 then writeln(usr,'Nur die Zeichen A-Z und Ziffern 0-9 benutzen.');
          end;
      until (not online) or ((4 <= i) and (i <= len_pw));
      user_rec.pw := prompt('Nochmal bitte        ', len_pw, 'SL');
      if user_rec.pw <> trial_pw
        then writeln(USR, 'Keine Uebereinstimmung.  Nochmal bitte.')
    until (not online) or (user_rec.pw = trial_pw);
    writeln(USR);
    writeln(USR, 'Dieses Passwort jetzt bitte nicht mehr vergessen.');
    writeln(USR, 'Es wird bei allen zukuenftigen Anrufen benoetigt.')
  end;

overlay procedure get_case;
{ Get case switch from user }
  begin
    user_rec.shift_lock := not ask('Beherrscht Dein Terminal Kleinschreibung')
  end;

overlay procedure get_nulls;
{ Get nulls from user }
  begin
    if online then
    user_rec.nulls := strint(prompt('Wieviele Nullen benoetigst Du [0-9]? ', 1, 'ES'))
  end;

overlay function mesg_start(pr: StrPr): integer;
{ Get starting message number from user }
  var
    i,last: integer;
  begin
    repeat
      writeln(usr); last:=user_rec.lasthi;
      i:= strint(prompt(pr +' (zuletzt gelesen #'+intstr(last,1)+') '+
      ' [' + intstr(msg_lo, 1) + '-' + intstr(msg_hi, 1) + ']? ',5,'E'));
      if (i <msg_lo) or (i >msg_hi)
        then Writeln(usr,'Ungueltige Brief-Nummer. Nochmal bitte.');
    until ((i>=msg_lo) and (i<=msg_hi)) or (not online);
   mesg_start := i
  end;

Overlay procedure mesg_header_list(loc:integer; var first_line,
  last_line:integer; var Fr_fn:firstname; var Fr_ln:lastname);
{ Display message header }
  var
    to_fn: firstname;
    to_ln: lastname;
    Tstr: StrTAD;
    temp_user_rec: user_list;
    this: areaptr;
  begin
    seek(summ_file, loc);
    read(summ_file, summ_rec);
    with summ_rec do
      begin
        if user_to = 0
          then
            begin
              to_fn := 'ALLE';
              to_ln := ''
            end
        else if user_to = user_loc
          then
            begin
              to_fn := user_rec.fn;
              to_ln := user_rec.ln
            end
          else
            begin
              if user_to<>-1 then
                begin
                  GetRec(DatF, user_to, temp_user_rec);
                  to_fn := temp_user_rec.fn;
                  to_ln := temp_user_rec.ln;
                end
              else
                begin
                  to_fn:='Benutzer';
                  to_ln:='Geloescht';
                end;
            end;
        if user_from = user_loc
          then
            begin
              fr_fn := user_rec.fn;
              fr_ln := user_rec.ln
            end
          else
            begin
              if user_from<>-1 then
                begin
                  GetRec(DatF, user_from, temp_user_rec);
                  fr_fn := temp_user_rec.fn;
                  fr_ln := temp_user_rec.ln;
                end
              else
                begin
                  fr_fn:='Benutzer';
                  fr_ln:='Geloescht';
                end;
            end;
        Tstr := FormTAD(date);
        this:=areabase;
        while (this<>nil) and (this^.area<>area) do this:=this^.next;
        writeln(USR);
        if num_prev=255 then write(usr,'<P>');
        case status of
          deleted:    write(USR, 'Geloeschter');
          read:       write(USR, 'Gelesener');
          private:    write(USR, 'Privater');
          public:     write(USR, 'Oeffentlicher');
          restricted: write(USR, 'Gesperrter');
        end;
        writeln(USR,' Brief #',num,' [',this^.areaname,'] vom: ',Tstr);
        writeln(USR, '  Von: ', fr_fn, ' ', fr_ln);
        writeln(USR, '   An: ', to_fn, ' ', to_ln);
        writeln(USR, 'Betr.: ', subject);
        if audit_on
          then
            begin
              setsect(AudDrv,AudUsr);
              writeln(AuditFile);
              if num_prev=255 then write(auditfile,'<P>');
              case status of
                deleted: write(AuditFile, 'Geloeschter');
                read:    write(AuditFile, 'Gelesener');
                private: write(AuditFile, 'Privater');
                public:  write(AuditFile, 'Oeffentlicher');
                restricted: write(Auditfile,'Gesperrter');
              end;
              writeln(AuditFile, ' Brief # ', num, ' [', this^.areaname, '] vom: ', Tstr);
              writeln(AuditFile, '  Von: ', fr_fn, ' ', fr_ln);
              writeln(AuditFile, '   An: ', to_fn, ' ', to_ln);
              writeln(AuditFile, 'Betr.: ', subject);
              setsect(homdrv,homusr);
            end;
        first_line := st_rec;
        last_line := size
      end
  end;   {message header list}

overlay procedure mesg_delete;
{ Delete the current message }
  var
    this: MesgPtr;
  begin
    summ_rec.status := deleted;
    seek(summ_file, pred(FilePos(summ_file)));
    write(summ_file, summ_rec);
    this := MesgCurr;
    if MesgCurr = MesgBase
      then
        begin
          MesgCurr := MesgBase^.next;
          MesgBase := MesgBase^.next;
          dispose(this)
        end
    else if MesgCurr <> nil
      then
        begin
          MesgCurr := MesgBase;             { Find previous record }
          while MesgCurr^.next <> this do
            MesgCurr := MesgCurr^.next;
          MesgCurr^.next := this^.next;     { Make it point to next record }
          if MesgLast = this
            then MesgLast := MesgCurr;
          MesgCurr := MesgCurr^.next;
          dispose(this)
        end;
    writeln(USR, 'Brief #', summ_rec.num, ' geloescht.')
  end;  {mesg_delete}

overlay procedure mesg_build_index(mesg_area: byte);
{ Scan summary file and build message index list.  Messages are tied
  to the current message area.
  All messages are accessible in mesg_area #0 (SYSTEM). }
  var
    this: MesgPtr;
  begin
    while MesgBase <> nil do                { Delete old messages }
      begin
        this := MesgBase;
        MesgBase := MesgBase^.next;         { Go to next on list }
        dispose(this)                       { Reclaim space }
      end;
    msg_all := 0;
    msg_ind := 0;
    msg_aut := 0;
    msg_sys := 0;
    msg_hi:=0;
    msg_lo:=30000;
    seek(summ_file, 1);
    while not EOF(summ_file) do
      with summ_rec do
        begin
          read(summ_file, summ_rec);
          if ((status<>deleted) and (status<>restricted) and (area=mesg_area))
          or (mesg_area=0) then
            begin
              if msg_lo>num then msg_lo:=num;
              if num>msg_hi then msg_hi:=num;
            end;
          if (status=public) and ((area=mesg_area) or (mesg_area=0))  {Public message}
            then
            If user_loc=user_to then
              begin
                msg_ind:=succ(msg_ind);
                msg_all:=succ(msg_all);    {add to public count too}
                mesg_insert(1);
              end
            else
            If user_loc=user_from then
              begin
              msg_aut:=succ(msg_aut);
              msg_all:=succ(msg_all);
              mesg_insert(2);
              end
            else
              begin
                msg_all := succ(msg_all);
                mesg_insert(0)
              end
          else if (status <> deleted) and (user_loc = user_to)
                  and ((area=mesg_area) or (mesg_area=0))
            then
              begin                         { Private message }
                msg_ind := succ(msg_ind);
                mesg_insert(1)
              end
          else if (status <> deleted) and (user_loc = user_from)
                  and ((area=mesg_area) or (mesg_area=0))
            then
              begin                         { Author of message }
                msg_aut := succ(msg_aut);
                mesg_insert(2)
              end
          else if mesg_area = 0
            then
              begin                         { Sysop can view all messages }
                msg_sys := succ(msg_sys);
                mesg_insert(3)
              end
        end;
    if msg_lo>=29999 then msg_lo:=0;
    summ_rec.user_from := 0
  end;

overlay procedure mesg_directory;
{ Display directory of messages }

  var
    col_width, col_count, col_limit,conf_num: integer;
    this:areaptr;
    temstr:string[160];
    found:boolean;

  begin  {msg_directory}
    col_width:=6;
    col_limit := max(1, user_rec.columns div col_width);
    writeln(USR, 'Briefe in diesem Briefkasten : ',msg_lo,'-',msg_hi);
    writeln(USR, 'Davon oeffentliche Briefe    : ', msg_all);
    writeln(USR);
    if msg_ind = 0
      then writeln(USR, 'In diesem Brett sind keine privaten Briefe fuer Dich.')
      else
        begin
          writeln(USR, 'Die folgenden Briefe sind an Dich adressiert:');
          col_count := 0;
          MesgCurr := MesgBase;
          while (not brk) and (MesgCurr <> nil) do
            begin
              if MesgCurr^.TypMsg = 1
                then
                  begin
                    write(USR, MesgCurr^.MesgNo:col_width);
                    col_count := succ(col_count);
                    if (0 = col_count mod col_limit)
                      then writeln(USR)
                  end;
              MesgCurr := MesgCurr^.next
            end;
          writeln(USR)
        end;
    if msg_aut > 0
      then
        begin
          writeln(USR, 'Die folgenden Briefe hast Du abgeschickt:');
          col_count := 0;
          MesgCurr := MesgBase;
          while (not brk) and (MesgCurr <> nil) do
            begin
              if MesgCurr^.TypMsg = 2
                then
                  begin
                    write(USR, MesgCurr^.MesgNo:col_width);
                    col_count := succ(col_count);
                    if (0 = col_count mod col_limit)
                      then writeln(USR)
                  end;
              MesgCurr := MesgCurr^.next
            end;
          writeln(USR);
        end;
    Seek(summ_file,1);                  {look for msgs in other areas}
    col_count:=0;  col_width:=12; temstr:=''; Writeln(usr);
    col_limit:=max(1,user_rec.columns div col_width);
    found:=false;
    While not EOF(summ_file) do
      with summ_rec do
        begin
        read(summ_file,summ_rec);
        if (status<>deleted) and (area<>areaset) and (user_loc=user_to) then
          begin
            this:=areabase;
            while (this<>nil) and (this^.area<>area) do this:=this^.next;
            conf_num:=this^.Areaconf;
            if (pos(this^.areaname,temstr)=0) and (this<>nil)
                and ((user_rec.access>=this^.areaaccs)
                or (test_bit(user_rec.conf_flags,conf_num))) then
              begin
                found:=true;
                Write(usr,this^.areaname:col_width);
                col_count:=succ(col_count); temstr:=temstr+this^.areaname;
                if (0=col_count mod col_limit) then writeln(usr);
              end;
          end;
        end;   {reading summary file}
    writeln(usr);
    if found then Writeln(usr,
       'In obigen Brettern sind weitere persoenliche Briefe fuer Dich.');
    writeln(usr);
  end;

overlay procedure scan_areas;
  var
    col_width, col_count, col_limit,conf_num: integer;
    this:areaptr;
    temstr:string[160];
    found:boolean;

  begin
    Seek(summ_file,1);   { neue oeffentliche Briefe in welchen Brettern? }
    col_count:=0;  col_width:=12; temstr:=''; Writeln(usr);
    col_limit:=max(1,user_rec.columns div col_width);
    found:=false;
    While not EOF(summ_file) do
      with summ_rec do
        begin
        read(summ_file,summ_rec);
        if (status=public) and (num>user_rec.lasthi) then
          begin
            this:=areabase;
            while (this<>nil) and (this^.area<>area) do this:=this^.next;
            conf_num:=this^.Areaconf;
            if (pos(this^.areaname,temstr)=0) and (this<>nil)
                and ((user_rec.access>=this^.areaaccs)
                or (test_bit(user_rec.conf_flags,conf_num))) then
              begin
                found:=true;
                Write(usr,this^.areaname:col_width);
                col_count:=succ(col_count); temstr:=temstr+this^.areaname;
                if (0=col_count mod col_limit) then writeln(usr);
              end;
          end;
        end;   {reading summary file}
    writeln(usr);
    if found then Writeln(usr,
      'In obigen Brettern sind neue oeffentliche Briefe seit letztem Anruf.')
     else
      writeln(usr,'Keine neuen oeffentlichen Briefe.');
    writeln(usr)
  end;


{END OF PICS0A.INC }

