:macro Convers
echo
echo Convers server LinPac %V
echo Login %C at %T
echo
if %(exist STN_NICKNAME) = 1
  echo Hello %STN_NICKNAME, welcome to the conference !
else
  echo Hellp %N, welcome to the conference !
endif
echo Type /H for help
echo

;;*********************** INICIALIZACE ************************
set CONVERS "LP_CONVERS"  ;;For recognition channels in convers
set LOGTIME %T            ;;Login time
set CALL %C               ;;Callsign
if "%N" = "%U"            ;;Get user's name
  set NAME "???"
else
  set NAME "%N"
endif
set LAST "%<"
set OUT1 ""
set OUT2 ""
set OUT3 ""
set OUT4 ""
set OUT5 ""
set OUT6 ""
set OUT7 ""
set OUT8 ""
set MSG1 ""
set MSG2 ""
set MSG3 ""
set MSG4 ""
set MSG5 ""
set MSG6 ""
set MSG7 ""
set MSG8 ""

set LAST "*** Enters the conference at %T ***"
conv @SENDALL

label MAIN

  flush

  ;;**** CHECK MESSAGES FROM OTHER CHANNELS ****
  set i 1
  label TESTOUT
    if "%OUT%i" <> ""      ;;Comething to send ?
      echo <%C@%i>         ;; YES - print callsign 
      echo %OUT%i          ;;       print message
      set OUT%i ""         ;;       clear buffer
      flush
    endif
    if "%MSG%i" <> ""      ;;The same for personal messages
      echo *%C@%i*
      echo %MSG%i
      set MSG%i ""
      flush
    endif
    set i %[%i+1]
  if (%i <= 8) goto TESTOUT  ;;Repeat for all channels

  set LAST "%<"              ;;Some new entry ?

if ("%LAST" = "") goto MAIN

  ;;SOME USER ENTRY
  if ("%(strleft 1 "%LAST")" = "/") goto COMMAND ;;Begins with slash - it's command

  conv @SENDALL ;;Not a command - send to all as text
goto MAIN

;;**** SEND THE STRING TO ALL STATIONS ****
label SENDALL
  set I 1
  label SENDING
    if "%CONVERS@%I" = "LP_CONVERS"    ;;If the channel is in conference
      if %I <> %K                      ;; (and it's not our channel)
        set@%I OUT%K "%LAST"           ;;Send the text
      endif
    endif
    set I %[%I+1]
  if (%I <= 8) goto SENDING
return


;; **** COMMAND RECOGNITION ****
label COMMAND
set CMD "%(Upcase %LAST)"
if (%CMD = /Q) goto KONEC
if (%CMD = /H) goto HELP
if (%CMD = /W) goto WHO
if (%CMD = /U) goto WHO
if (%(strleft 4 "%CMD") = "/MSG") goto MSGS
echo *** Unknown command.
goto MAIN


;; ************************ COMMAND /H  **************************
label HELP
echo *** Available commands
echo   /H           -  this help
echo   /MSG <call>  -  send personal message
echo   /Q           -  logout
echo   /W nebo /U   -  list users
echo ***
goto MAIN


;; ************************ COMMAND /MSG *************************
label MSGS

;; REMOVE '/MSG'
set I %(strpos " " "%LAST")
if (%I = 0) goto MSGERROR
set I %[%(strlen %LAST) - %I - 1]
set LAST "%(strr %I "%LAST")"

;; SPLIT THE MESSAGE AND CALLSIGN
set I %(strpos " " "%LAST")
if (%I = 0) goto MSGERROR
set MSGC "%(strleft %I "%LAST")"
set MSGC "%(upcase "%MSGC")"
set I %[%(strlen "%LAST") - %I - 1]
set LAST "%(strr %I %LAST)"

;; FIND THE CALLSIGN
set FND 0
set I 1
label CMSGS
  if %CALL@%I = %MSGC
    set@%I MSG%K "%LAST"
    set FND 1
  endif
  set I %[%I+1]
if (%I < 9) goto CMSGS
if %FND = 0
  echo *** No such user in the conference
endif
unset MSGC FND
goto MAIN

;; Chyba pri MSGS
label MSGERROR
echo *** You must enter the callsign and the message
goto MAIN


; ************************ COMMAND /W **********************
label WHO
echo *** List of users:
set I 1
label CWHO
  if "%CONVERS@%I" = "LP_CONVERS"
    echo %CALL@%I - %NAME@%I - connected since %LOGTIME@%I
  endif
  set I %[%I+1]
if (%I < 9) goto CWHO
echo ***
goto MAIN


; ************************ LOGOUT ***********************
label KONEC
set LAST "*** Leaves conference at %T ***"
echo Logout at %T.
if %(exist STN_NICKNAME) = 1
  echo Come again, %STN_NICKNAME !
else
  echo Come again, %N !
endif
conv @SENDALL
unset CONVERS LOGTIME CALL NAME I CMD
unset OUT1 OUT2 OUT3 OUT4 OUT5 OUT6 OUT7 OUT8
unset MSG1 MSG2 MSG3 MSG4 MSG5 MSG6 MSG7 MSG8
