Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

whitelist adjustments #296

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 85 additions & 72 deletions src/commands/whitelist.cob
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ WORKING-STORAGE SECTION.

PROCEDURE DIVISION.
SET PTR TO ENTRY "Callback-Execute"
CALL "RegisterCommand" USING COMMAND-NAME COMMAND-HELP PTR NODE-ROOT
CALL "RegisterCommand" USING COMMAND-NAME COMMAND-HELP PTR NODE-ROOT

CALL "AddCommandLiteral" USING NODE-ROOT "reload" NODE-OPERATION
CALL "AddCommandLiteral" USING NODE-ROOT "reload" NODE-OPERATION
CALL "SetCommandExecutable" USING NODE-OPERATION

CALL "AddCommandLiteral" USING NODE-ROOT "on" NODE-OPERATION
CALL "AddCommandLiteral" USING NODE-ROOT "on" NODE-OPERATION
CALL "SetCommandExecutable" USING NODE-OPERATION

CALL "AddCommandLiteral" USING NODE-ROOT "off" NODE-OPERATION
CALL "AddCommandLiteral" USING NODE-ROOT "off" NODE-OPERATION
CALL "SetCommandExecutable" USING NODE-OPERATION

CALL "AddCommandLiteral" USING NODE-ROOT "list" NODE-OPERATION
CALL "AddCommandLiteral" USING NODE-ROOT "list" NODE-OPERATION
CALL "SetCommandExecutable" USING NODE-OPERATION

CALL "AddCommandLiteral" USING NODE-ROOT "add" NODE-OPERATION
CALL "AddCommandArgument" USING NODE-OPERATION "player" CMD-PARSER-GAME-PROFILE OMITTED NODE-ARGUMENT
CALL "AddCommandLiteral" USING NODE-ROOT "add" NODE-OPERATION
CALL "AddCommandArgument" USING NODE-OPERATION "player" CMD-PARSER-GAME-PROFILE OMITTED NODE-ARGUMENT
CALL "SetCommandExecutable" USING NODE-ARGUMENT

CALL "AddCommandLiteral" USING NODE-ROOT "remove" NODE-OPERATION
CALL "AddCommandArgument" USING NODE-OPERATION "player" CMD-PARSER-GAME-PROFILE OMITTED NODE-ARGUMENT
CALL "AddCommandLiteral" USING NODE-ROOT "remove" NODE-OPERATION
CALL "AddCommandArgument" USING NODE-OPERATION "player" CMD-PARSER-GAME-PROFILE OMITTED NODE-ARGUMENT
CALL "SetCommandExecutable" USING NODE-ARGUMENT

GOBACK.
Expand All @@ -46,9 +46,9 @@ PROCEDURE DIVISION.
WORKING-STORAGE SECTION.
COPY DD-WHITELIST.
COPY DD-SERVER-PROPERTIES.
01 BUFFER PIC X(255).
01 BUFFER PIC X(128).
01 BUFFER-POS BINARY-LONG UNSIGNED.
01 BYTE-COUNT BINARY-LONG UNSIGNED.
01 DYN-BUFFER-LEN BINARY-LONG UNSIGNED VALUE ZERO.
01 WHITELIST-INDEX BINARY-LONG UNSIGNED.
01 IO-FAILURE BINARY-CHAR UNSIGNED.
01 STATE-FAILURE BINARY-CHAR UNSIGNED.
Expand All @@ -58,98 +58,111 @@ PROCEDURE DIVISION.
LINKAGE SECTION.
COPY DD-CALLBACK-COMMAND-EXECUTE.

01 DYN-BUFFER.
03 FILLER PIC X OCCURS UNBOUNDED DEPENDING ON DYN-BUFFER-LEN.

PROCEDURE DIVISION USING LK-CLIENT-ID LK-PARTS LK-PRINT-USAGE.
IF LK-PART-COUNT = 2 AND LK-PART-VALUE(2) = "reload"

IF LK-PART-COUNT < 2
MOVE 1 TO LK-PRINT-USAGE
GOBACK
END-IF

EVALUATE LK-PART-COUNT ALSO LK-PART-VALUE(2)

WHEN 2 ALSO "reload"
CALL "Whitelist-Read" USING IO-FAILURE
PERFORM HandleDiskFailure
MOVE "Reloaded the whitelist" TO BUFFER
CALL "SendChatMessage" USING LK-CLIENT-ID BUFFER OMITTED
GOBACK
END-IF

IF LK-PART-COUNT = 2 AND LK-PART-VALUE(2) = "on"
WHEN 2 ALSO "on"
MOVE 1 TO SP-WHITELIST-ENABLE
CALL "ServerProperties-Write" USING IO-FAILURE
PERFORM HandleDiskFailure
MOVE "Whitelist enabled" TO BUFFER
CALL "SendChatMessage" USING LK-CLIENT-ID BUFFER OMITTED
GOBACK
END-IF

IF LK-PART-COUNT = 2 AND LK-PART-VALUE(2) = "off"
WHEN 2 ALSO "off"
MOVE 0 TO SP-WHITELIST-ENABLE
CALL "ServerProperties-Write" USING IO-FAILURE
PERFORM HandleDiskFailure
MOVE "Whitelist disabled" TO BUFFER
CALL "SendChatMessage" USING LK-CLIENT-ID BUFFER OMITTED
GOBACK
END-IF

IF LK-PART-COUNT = 2 AND LK-PART-VALUE(2) = "list"
IF WHITELIST-LENGTH = 0
WHEN 2 ALSO "list"
EVALUATE WHITELIST-LENGTH
WHEN 0
MOVE "There are no whitelisted players" TO BUFFER
CALL "SendChatMessage" USING LK-CLIENT-ID BUFFER OMITTED
GOBACK
END-IF
MOVE WHITELIST-LENGTH TO TEMP-INT64-PIC
INITIALIZE BUFFER
STRING "There are " FUNCTION TRIM(TEMP-INT64-PIC) " whitelisted player(s):" INTO BUFFER
COMPUTE BUFFER-POS = FUNCTION STORED-CHAR-LENGTH(BUFFER) + 2
PERFORM VARYING WHITELIST-INDEX FROM 1 BY 1 UNTIL WHITELIST-INDEX > WHITELIST-LENGTH
IF WHITELIST-INDEX > 1
MOVE ", " TO BUFFER(BUFFER-POS:2)
ADD 2 TO BUFFER-POS
WHEN 1
INITIALIZE BUFFER
STRING "There is 1 whitelisted player: " WHITELIST-NAME(1) INTO BUFFER
WHEN OTHER
MOVE WHITELIST-LENGTH TO TEMP-INT64-PIC
COMPUTE DYN-BUFFER-LEN = 32 + WHITELIST-LENGTH * (LENGTH OF WHITELIST-NAME (1) + 2)
*> overwrite the dynamic buffer's address with a dynamic buffer, if too big
IF DYN-BUFFER-LEN > LENGTH OF BUFFER
ALLOCATE DYN-BUFFER-LEN CHARACTERS RETURNING BUFFER-ADDRESS
SET ADDRESS OF DYN-BUFFER TO BUFFER-ADDRESS
ELSE
SET ADDRESS OF DYN-BUFFER TO ADDRESS OF BUFFER
END-IF
MOVE FUNCTION STORED-CHAR-LENGTH(WHITELIST-NAME(WHITELIST-INDEX)) TO BYTE-COUNT
MOVE WHITELIST-NAME(WHITELIST-INDEX)(1:BYTE-COUNT) TO BUFFER(BUFFER-POS:BYTE-COUNT)
ADD BYTE-COUNT TO BUFFER-POS
END-PERFORM
CALL "SendChatMessage" USING LK-CLIENT-ID BUFFER OMITTED
GOBACK
END-IF
MOVE 1 TO BUFFER-POS
*> no need to initialize the buffer as we only use it in the places we explicit set
STRING "There are " FUNCTION TRIM(TEMP-INT64-PIC, LEADING) " whitelisted players: "
FUNCTION TRIM (WHITELIST-NAME(1), TRAILING)
INTO DYN-BUFFER WITH POINTER BUFFER-POS
PERFORM WITH TEST AFTER
VARYING WHITELIST-INDEX FROM 2 BY 1 UNTIL WHITELIST-INDEX = WHITELIST-LENGTH
STRING ", " FUNCTION TRIM (WHITELIST-NAME(WHITELIST-INDEX), TRAILING)
INTO DYN-BUFFER WITH POINTER BUFFER-POS
*> note: we ensured that the buffer is always big enough, so no need for ON OVERFLOW here
END-PERFORM
SUBTRACT 1 FROM BUFFER-POS
CALL "SendChatMessage" USING LK-CLIENT-ID DYN-BUFFER OMITTED BUFFER-POS
IF DYN-BUFFER-LEN > LENGTH OF BUFFER
FREE BUFFER-ADDRESS
END-IF
GOBACK
END-EVALUATE

IF LK-PART-COUNT = 3
WHEN 3 ALSO "add"
MOVE LK-PART-VALUE(3) TO TEMP-NAME
CALL "Players-NameToUUID" USING TEMP-NAME TEMP-UUID

IF LK-PART-VALUE(2) = "add"
CALL "Whitelist-Add" USING TEMP-UUID TEMP-NAME STATE-FAILURE IO-FAILURE
PERFORM HandleDiskFailure
IF STATE-FAILURE NOT = 0
MOVE "Player is already whitelisted" TO BUFFER
ELSE
INITIALIZE BUFFER
STRING "Added " FUNCTION TRIM(TEMP-NAME) " to the whitelist" INTO BUFFER
END-IF
CALL "SendChatMessage" USING LK-CLIENT-ID BUFFER OMITTED
GOBACK
INITIALIZE BUFFER
CALL "Whitelist-Add" USING TEMP-UUID TEMP-NAME STATE-FAILURE IO-FAILURE
PERFORM HandleDiskFailure
IF STATE-FAILURE NOT = 0
STRING "Player " FUNCTION TRIM(TEMP-NAME) " is already whitelisted" INTO BUFFER
ELSE
STRING "Added " FUNCTION TRIM(TEMP-NAME) " to the whitelist" INTO BUFFER
END-IF

IF LK-PART-VALUE(2) = "remove"
CALL "Whitelist-Remove" USING TEMP-UUID TEMP-NAME STATE-FAILURE IO-FAILURE
PERFORM HandleDiskFailure
IF STATE-FAILURE NOT = 0
MOVE "Player is not whitelisted" TO BUFFER
ELSE
INITIALIZE BUFFER
STRING "Removed " FUNCTION TRIM(TEMP-NAME) " from the whitelist" INTO BUFFER
END-IF
CALL "SendChatMessage" USING LK-CLIENT-ID BUFFER OMITTED
GOBACK
WHEN 3 ALSO "remove"
MOVE LK-PART-VALUE(3) TO TEMP-NAME
CALL "Players-NameToUUID" USING TEMP-NAME TEMP-UUID

INITIALIZE BUFFER
CALL "Whitelist-Remove" USING TEMP-UUID TEMP-NAME STATE-FAILURE IO-FAILURE
PERFORM HandleDiskFailure
IF STATE-FAILURE NOT = 0
STRING "Player " FUNCTION TRIM(TEMP-NAME) " is not whitelisted" INTO BUFFER
ELSE
STRING "Removed " FUNCTION TRIM(TEMP-NAME) " from the whitelist" INTO BUFFER
END-IF
END-IF

MOVE 1 TO LK-PRINT-USAGE
WHEN OTHER
MOVE 1 TO LK-PRINT-USAGE
GOBACK

END-EVALUATE.

CALL "SendChatMessage" USING LK-CLIENT-ID BUFFER
GOBACK.

HandleDiskFailure.
IF IO-FAILURE NOT = 0
MOVE "Input/output error" TO BUFFER
CALL "SendChatMessage" USING LK-CLIENT-ID BUFFER OMITTED
CALL "SendChatMessage" USING LK-CLIENT-ID "Input/output error"
GOBACK
END-IF
EXIT PARAGRAPH.
END-IF.

END PROGRAM Callback-Execute.

Expand Down