Skip to content

Commit

Permalink
Add service string handling
Browse files Browse the repository at this point in the history
Not used yet but will be needed soon.
  • Loading branch information
zapek committed Oct 5, 2024
1 parent 5f20292 commit ee32bc9
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.app.xrs.service.identity;

import java.util.regex.Pattern;

public class IdentityServiceStorage
{
private static final Pattern SERVICE_STRING = Pattern.compile("^v2 \\{P:(.{1,1024}?)}\\{T:(.{1,1024}?)}\\{R:(.{1,1024}?)}$");

private Pgp pgp;
private Recognition recognition;
private Reputation reputation;

private boolean success;

public IdentityServiceStorage(long pgpIdentifier)
{
}

public IdentityServiceStorage(String storage)
{
success = in(storage);
}

private boolean in(String storage)
{
var matcher = SERVICE_STRING.matcher(storage);

if (!matcher.matches())
{
return false;
}

pgp = new Pgp(matcher.group(1));
if (!pgp.isSuccessful())
{
return false;
}

recognition = new Recognition(matcher.group(2));
if (!recognition.isSuccessful())
{
return false;
}

reputation = new Reputation(matcher.group(3));
if (!reputation.isSuccessful())
{
return false;
}
return true;
}

public String out()
{

return "v2 " + "{P:" +
pgp.out() +
"}" +
"{T:" +
recognition.out() +
"}" +
"{R:" +
reputation.out() +
"}";
}

public boolean isSuccess()
{
return success;
}

}
111 changes: 111 additions & 0 deletions app/src/main/java/io/xeres/app/xrs/service/identity/Pgp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.app.xrs.service.identity;

import io.xeres.common.id.Id;

import java.time.Instant;
import java.util.regex.Pattern;

class Pgp
{
private static final Pattern VALIDATED = Pattern.compile("^K:1 I:(\\p{XDigit}{16})$");
private static final Pattern UNVALIDATED_WITH_TIMESTAMP_ATTEMPTS_AND_ID = Pattern.compile("^K:0 T:(\\d{1,10}) C:(\\d{1,10}) I:(\\p{XDigit}{16})$");
private static final Pattern UNVALIDATED_WITH_TIMESTAMP_AND_ATTEMPTS = Pattern.compile("^K:0 T:(\\d{1,10}) C:(\\d{1,10})$");
private static final Pattern UNVALIDATED_WITH_TIMESTAMP = Pattern.compile("^K:0 T:(\\d{1,10})$");

private boolean validated;
private Instant lastCheck;
private int checkAttempt;
private long pgpIdentifier;

private boolean success;

public Pgp(long pgpIdentifier)
{
this.pgpIdentifier = pgpIdentifier;
validated = true;
}

public Pgp(String input)
{
success = in(input);
}

private boolean in(String input)
{
var matcher = VALIDATED.matcher(input);
if (matcher.matches())
{
validated = true;
pgpIdentifier = Long.parseUnsignedLong(matcher.group(1), 16);
return true;
}
matcher = UNVALIDATED_WITH_TIMESTAMP_ATTEMPTS_AND_ID.matcher(input);
if (matcher.matches())
{
lastCheck = Instant.ofEpochSecond(Long.parseLong(matcher.group(1)));
checkAttempt = Integer.parseUnsignedInt(matcher.group(2));
pgpIdentifier = Long.parseUnsignedLong(matcher.group(3), 16);
return true;
}
matcher = UNVALIDATED_WITH_TIMESTAMP_AND_ATTEMPTS.matcher(input);
if (matcher.matches())
{
lastCheck = Instant.ofEpochSecond(Long.parseLong(matcher.group(1)));
checkAttempt = Integer.parseUnsignedInt(matcher.group(2));
return true;
}
matcher = UNVALIDATED_WITH_TIMESTAMP.matcher(input);
if (matcher.matches())
{
lastCheck = Instant.ofEpochSecond(Long.parseLong(matcher.group(1)));
return true;
}
return false;
}

public String out()
{
if (validated)
{
return "K:1 I:" + Id.toString(pgpIdentifier);
}
else
{
var sb = new StringBuilder("K:0");
sb.append(" T:");
sb.append(lastCheck.getEpochSecond());
sb.append(" C:");
sb.append(checkAttempt);
if (pgpIdentifier != 0)
{
sb.append(" I:");
sb.append(Id.toString(pgpIdentifier));
}
return sb.toString();
}
}

public boolean isSuccessful()
{
return success;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.app.xrs.service.identity;

import java.time.Instant;
import java.util.regex.Pattern;

class Recognition
{
private static final Pattern RECOGNITION_PATTERN = Pattern.compile("^F:(\\d{1,10}) P:(\\d{1,10}) T:(\\d{1,10})$");

private int flags;
private Instant publish;
private Instant lastCheck;

private boolean success;

public Recognition(int flags, Instant publish, Instant lastCheck)
{
this.flags = flags;
this.publish = publish;
this.lastCheck = lastCheck;
}

public Recognition(String input)
{
success = in(input);
}

private boolean in(String input)
{
var matcher = RECOGNITION_PATTERN.matcher(input);
if (matcher.matches())
{
flags = Integer.parseUnsignedInt(matcher.group(1));
publish = Instant.ofEpochSecond(Long.parseLong(matcher.group(2)));
lastCheck = Instant.ofEpochSecond(Long.parseLong(matcher.group(3)));
return true;
}
return false;
}

public String out()
{
return "F:" +
flags +
" P:" +
publish.getEpochSecond() +
" T:" +
lastCheck.getEpochSecond();
}

public boolean isSuccessful()
{
return success;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.app.xrs.service.identity;

import java.util.regex.Pattern;

class Reputation
{
private static final Pattern REPUTATION_PATTERN = Pattern.compile("^(-?\\d{1,10}) (-?\\d{1,10}) (-?\\d{1,10}) (-?\\d{1,10})$");

private int overallScore;
private int idScore;
private int ownOpinion;
private int peerOpinion;

private boolean success;

public Reputation(int overallScore, int idScore, int ownOpinion, int peerOpinion)
{
this.overallScore = overallScore;
this.idScore = idScore;
this.ownOpinion = ownOpinion;
this.peerOpinion = peerOpinion;
}

public Reputation(String input)
{
success = in(input);
}

private boolean in(String input)
{
var matcher = REPUTATION_PATTERN.matcher(input);
if (matcher.matches())
{
overallScore = Integer.parseInt(matcher.group(1));
idScore = Integer.parseInt(matcher.group(2));
ownOpinion = Integer.parseInt(matcher.group(3));
peerOpinion = Integer.parseInt(matcher.group(4));
return true;
}
return false;
}

public String out()
{
return String.format("%d %d %d %d", overallScore, idScore, ownOpinion, peerOpinion);
}

public boolean isSuccessful()
{
return success;
}
}
Loading

0 comments on commit ee32bc9

Please sign in to comment.