(* ------------------------------------------------------------------------
  :Program.       ShowGroups
  :Contents.      shows all groups in UMS' messagebase
  :Author.        Kai Bolay [kai]
  :Address.       Snail Mail:              EMail:
  :Address.       Hoffmannstraße 168       UUCP: kai@amokle.stgt.sub.org
  :Address.       D-7250 (71229) Leonberg  FIDO: 2:2407/106.3
  :History.       v1.0 [kai] 25-Mar-93 (added Martin's suggestion)
  :History.       v1.1 [kai] 15-Apr-93 (added SERVER keyword, better Login() failure, changed flag)
  :Copyright.     Public Domain
  :Language.      Oberon
  :Translator.    AMIGA OBERON v3.01d
  :Imports.       ums
------------------------------------------------------------------------ *)
MODULE ShowGroups;

IMPORT
  ums,
  I: Intuition, d: Dos, e: Exec, u: Utility,
  NoGuru, Break,
  y: SYSTEM;
CONST
  Template = "USER/A,PASSWORD/A,SERVER/K,NEW/S\o$VER: ShowGroups 1.1 (15.4.93)\n\r";
VAR
  RD: d.RDArgsPtr;
  Args: STRUCT
    name: e.STRPTR;
    password: e.STRPTR;
    server: e.STRPTR;
    new: I.LONGBOOL;
  END;
  acc: LONGINT;
  res, last: LONGINT;
  Group: e.STRPTR;

(* $Debug- *)
PROCEDURE CheckErr;
VAR
  err: INTEGER;
  txt: ums.STRPTR;
BEGIN
  err := ums.ErrNum (acc);
  IF err # ums.ok THEN
    txt := ums.ErrTxt (acc);
    d.PrintF ("UMS-error: %ld, \"%s\"\n", err, txt);
    HALT (20);
  END;
END CheckErr;
(* $Debug= *)

BEGIN
  RD := d.ReadArgs (Template, Args, NIL);
  IF RD = NIL THEN
    d.PrintF ("Usage: %s\n", y.ADR (Template));
    HALT (20);
  END;
  (* $OddChk- $NilChk- *)
  acc := ums.UMSRLogin (Args.server^, Args.name^, Args.password^);
  (* $OddChk= $NilChk= *)
  IF acc <= 0 THEN
    d.PrintF ("Unable to login\n");
    HALT (20);
  END;

  (* clear local flag 0 on all messages *)
  res := ums.UMSSelectTags (acc, ums.tagSelWriteLocal, I.LTRUE,
                                 ums.tagSelSet,        LONGSET {},
                                 ums.tagSelUnset,      LONGSET {0},
                                 u.done);
  CheckErr;
  d.PrintF ("there are %ld msgs in your messagebase\n", res);

  (* set local flag 0 on all readable messages *)
  res := ums.UMSSelectTags (acc, ums.tagSelWriteLocal, I.LTRUE,
                                 ums.tagSelSet,        LONGSET {0},
                                 ums.tagSelUnset,      LONGSET {},
                                 ums.tagSelMask,       LONGSET {ums.ViewAccess, ums.ReadAccess},
                                 ums.tagSelMatch,      LONGSET {ums.ViewAccess, ums.ReadAccess},
                                 u.done);
  CheckErr;
  d.PrintF ("you have access to %ld of thereof\n", res);

  IF Args.new = I.LTRUE THEN
    (* clear local flag 0 on all read messages *)
    res := ums.UMSSelectTags (acc, ums.tagSelWriteLocal, I.LTRUE,
                                   ums.tagSelSet,        LONGSET {},
                                   ums.tagSelUnset,      LONGSET {0},
                                   ums.tagSelMask,       LONGSET {ums.Old},
                                   ums.tagSelMatch,      LONGSET {ums.Old},
                                   u.done);
    CheckErr;
    d.PrintF ("but %ld msgs are already read\n", res);
  END;

  d.PrintF ("\n");
  last := 0;
  LOOP
    (* find next message with flag 0 set *)
    last := ums.UMSSearchTags (acc, ums.tagSearchLocal, I.LTRUE,
                                    ums.tagSearchLast,  last,
                                    ums.tagSearchMask,  LONGSET {0},
                                    ums.tagSearchMatch, LONGSET {0},
                                    u.done);
    CheckErr;

    IF last = 0 THEN EXIT END; (* no more messages? *)


    (* read the group of the message *)
    IF ums.ReadUMSMsgTags (acc, ums.tagMsgNum,  last,
                                ums.tagRGroup,  y.ADR (Group),
                                u.done) THEN END;
    CheckErr;

    (* clear local flag 0 on all messages belonging to this group *)
    res := ums.UMSSelectTags (acc, ums.tagSelWriteLocal, I.LTRUE,
                                   ums.tagSelQuick,      I.LTRUE,
                                   ums.tagGroup,         Group,
                                   ums.tagSelSet,        LONGSET {},
                                   ums.tagSelUnset,      LONGSET {0},
                                   u.done);
    CheckErr;

    IF Group # NIL THEN
      d.PrintF ("%5ld msgs in %s\n", res, Group);
    ELSE
      d.PrintF ("%5ld private msgs\n", res);
    END;

    (* free the buffers belonging to the message *)
    ums.FreeUMSMsg (acc, last);
  END;
CLOSE
  IF acc # NIL THEN
    ums.Logout (acc); acc := 0;
  END;
  IF RD # NIL THEN
    d.FreeArgs (RD); RD := NIL;
  END;
END ShowGroups.
