2022-04-06 16:17:57 +02:00
|
|
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package pgp.vks.client.cli;
|
|
|
|
|
|
|
|
import pgp.vks.client.RequestVerify;
|
|
|
|
import pgp.vks.client.Status;
|
|
|
|
import pgp.vks.client.Upload;
|
|
|
|
import pgp.vks.client.VKS;
|
|
|
|
import pgp.vks.client.exception.CertCannotBePublishedException;
|
|
|
|
import picocli.CommandLine;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2022-08-04 00:49:08 +02:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.ResourceBundle;
|
2022-04-06 16:17:57 +02:00
|
|
|
|
2022-08-04 00:49:08 +02:00
|
|
|
@CommandLine.Command(
|
|
|
|
name = "upload",
|
|
|
|
resourceBundle = "msg_upload")
|
2022-04-06 16:17:57 +02:00
|
|
|
public class UploadCmd implements Runnable {
|
|
|
|
|
|
|
|
@CommandLine.Mixin
|
|
|
|
VKSCLI.KeyServerMixin keyServerMixin;
|
|
|
|
|
2022-08-04 00:49:08 +02:00
|
|
|
@CommandLine.Option(names = {"-r", "--request-verification"})
|
2022-04-06 16:17:57 +02:00
|
|
|
boolean requestVerification;
|
|
|
|
|
2022-08-04 00:49:08 +02:00
|
|
|
private final ResourceBundle msg;
|
|
|
|
|
|
|
|
public UploadCmd() {
|
|
|
|
msg = ResourceBundle.getBundle("msg_upload", Locale.getDefault());
|
|
|
|
}
|
|
|
|
|
2022-04-06 16:17:57 +02:00
|
|
|
public void run() {
|
|
|
|
VKS vks;
|
|
|
|
try {
|
|
|
|
vks = keyServerMixin.parent.getApi();
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
Upload upload = vks.upload();
|
|
|
|
try {
|
|
|
|
Upload.Response response = upload.cert(System.in);
|
|
|
|
|
|
|
|
// Unpublished mail addresses
|
|
|
|
List<String> unpublished = new ArrayList<>();
|
|
|
|
int maxMailLen = 0;
|
|
|
|
for (String address : response.getStatus().keySet()) {
|
|
|
|
Status status = response.getStatus().get(address);
|
|
|
|
if (address.length() > maxMailLen) {
|
|
|
|
maxMailLen = address.length();
|
|
|
|
}
|
|
|
|
if (status != Status.published && status != Status.revoked) {
|
|
|
|
unpublished.add(address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 00:49:08 +02:00
|
|
|
String msgUpload = String.format(msg.getString("output.uploaded_key"),
|
|
|
|
response.getKeyFingerprint(), response.getToken());
|
|
|
|
System.out.println(msgUpload);
|
2022-04-06 16:17:57 +02:00
|
|
|
|
2022-08-04 00:49:08 +02:00
|
|
|
String msgStatus = msg.getString("output.status");
|
2022-04-06 16:17:57 +02:00
|
|
|
if (!requestVerification || unpublished.isEmpty()) {
|
2022-08-04 00:49:08 +02:00
|
|
|
System.out.println(msgStatus);
|
2022-04-06 16:17:57 +02:00
|
|
|
for (String address : response.getStatus().keySet()) {
|
|
|
|
Status status = response.getStatus().get(address);
|
|
|
|
System.out.format("%-" + maxMailLen + "s %s\n", address, status);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestVerify.Response verifyResponse = vks.requestVerification().forEmailAddresses(unpublished.toArray(new String[0]))
|
|
|
|
.execute(response.getToken());
|
2022-08-04 00:49:08 +02:00
|
|
|
System.out.println(msgStatus);
|
2022-04-06 16:17:57 +02:00
|
|
|
for (String address : verifyResponse.getStatus().keySet()) {
|
|
|
|
Status status = response.getStatus().get(address);
|
|
|
|
System.out.format("%-" + maxMailLen + "s %s\n", address, status);
|
|
|
|
}
|
|
|
|
} catch (CertCannotBePublishedException e) {
|
|
|
|
throw new AssertionError(e.getMessage());
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|