From 952660919678a541ddcb76dc95f4de212b170422 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Wed, 7 Apr 2021 17:19:57 +0200 Subject: [PATCH] Replaced Pastebin with Privatebin. --- src/org/april/hebdobot/bot/Hebdobot.java | 14 +- .../hebdobot/bot/hooks/FinishReviewHook.java | 9 +- .../april/hebdobot/bot/hooks/StatusHook.java | 2 +- src/org/april/hebdobot/cli/HebdobotCLI.java | 4 +- .../hebdobot/cli/HebdobotConfigFile.java | 35 +- .../hebdobot/privatebin/PrivatebinClient.java | 328 ++++++++---------- .../privatebin/PrivatebinSettings.java | 140 ++++++-- .../privatebin/PrivatebinClientTest.java | 37 +- 8 files changed, 324 insertions(+), 245 deletions(-) diff --git a/src/org/april/hebdobot/bot/Hebdobot.java b/src/org/april/hebdobot/bot/Hebdobot.java index ab5071f..17d1a42 100644 --- a/src/org/april/hebdobot/bot/Hebdobot.java +++ b/src/org/april/hebdobot/bot/Hebdobot.java @@ -54,7 +54,7 @@ import org.april.hebdobot.bot.review.Review; import org.april.hebdobot.cron.CronManager; import org.april.hebdobot.cron.CronSettings; import org.april.hebdobot.identica.IdenticaSettings; -import org.april.hebdobot.pastebin.PastebinSettings; +import org.april.hebdobot.privatebin.PrivatebinSettings; import org.april.hebdobot.twitter.TwitterClient; import org.april.hebdobot.twitter.TwitterSettings; import org.april.hebdobot.util.Timekeeper; @@ -86,7 +86,7 @@ public class Hebdobot extends PircBot private Review review; private Timekeeper timekeeper; private IdenticaSettings identicaSettings; - private PastebinSettings pastebinSettings; + private PrivatebinSettings privatebinSettings; private TwitterSettings twitterSettings; private CronSettings cronSettings; private UserAliases aliases; @@ -130,7 +130,7 @@ public class Hebdobot extends PircBot this.timekeeper = new Timekeeper(); this.identicaSettings = new IdenticaSettings(); - this.pastebinSettings = new PastebinSettings(); + this.privatebinSettings = new PrivatebinSettings(); this.twitterSettings = new TwitterSettings(); this.cronSettings = new CronSettings(); this.aliases = new UserAliases(); @@ -221,13 +221,13 @@ public class Hebdobot extends PircBot } /** - * Gets the pastebin settings. + * Gets the privatebin settings. * - * @return the pastebin settings + * @return the privatebin settings */ - public PastebinSettings getPastebinSettings() + public PrivatebinSettings getPrivatebinSettings() { - return this.pastebinSettings; + return this.privatebinSettings; } public Review getReview() diff --git a/src/org/april/hebdobot/bot/hooks/FinishReviewHook.java b/src/org/april/hebdobot/bot/hooks/FinishReviewHook.java index bb31d7b..9614e20 100644 --- a/src/org/april/hebdobot/bot/hooks/FinishReviewHook.java +++ b/src/org/april/hebdobot/bot/hooks/FinishReviewHook.java @@ -33,8 +33,7 @@ import org.april.hebdobot.bot.stats.ReviewData; import org.april.hebdobot.bot.stats.ReviewDatas; import org.april.hebdobot.bot.stats.ReviewDatasFile; import org.april.hebdobot.bot.stats.ReviewStatsReporter; -import org.april.hebdobot.pastebin.PastebinClient; -import org.april.hebdobot.pastebin.Private; +import org.april.hebdobot.privatebin.PrivatebinClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -115,14 +114,14 @@ public class FinishReviewHook extends Hook // Pastebin the review text. String pastebinUrl; - if (bot.getPastebinSettings().isValid()) + if (bot.getPrivatebinSettings().isValid()) { logger.info("Pastebin the review."); try { - PastebinClient pastebinClient = new PastebinClient(bot.getPastebinSettings().getApiKey()); + PrivatebinClient pastebinClient = new PrivatebinClient(bot.getPrivatebinSettings()); - pastebinUrl = pastebinClient.paste(reviewText, "Revue APRIL " + date, Private.UNLISTED); + pastebinUrl = pastebinClient.paste(reviewText); } catch (final Exception exception) { diff --git a/src/org/april/hebdobot/bot/hooks/StatusHook.java b/src/org/april/hebdobot/bot/hooks/StatusHook.java index bda9c5b..0ee008a 100644 --- a/src/org/april/hebdobot/bot/hooks/StatusHook.java +++ b/src/org/april/hebdobot/bot/hooks/StatusHook.java @@ -55,7 +55,7 @@ public class StatusHook extends Hook } bot.sendMessage(sender, " Alias settings : " + (bot.getAliases().size())); bot.sendMessage(sender, " Identica settings : " + (bot.getIdenticaSettings().isValid())); - bot.sendMessage(sender, " Pastebin settings : " + (bot.getPastebinSettings().isValid())); + bot.sendMessage(sender, " Pastebin settings : " + bot.getPrivatebinSettings().toString()); bot.sendMessage(sender, " Twitter settings : " + (bot.getTwitterSettings().isValid())); bot.sendMessage(sender, " Cron settings : " + (bot.getCronSettings().size())); bot.sendMessage(sender, " Review Wait Time : " + (bot.getReviewWaitTime())); diff --git a/src/org/april/hebdobot/cli/HebdobotCLI.java b/src/org/april/hebdobot/cli/HebdobotCLI.java index c2430c4..670dbb3 100644 --- a/src/org/april/hebdobot/cli/HebdobotCLI.java +++ b/src/org/april/hebdobot/cli/HebdobotCLI.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2017-2018 Christian Pierre MOMON + * Copyright (C) 2017-2021 Christian Pierre MOMON * Copyright (C) 2011-2013 Nicolas Vinot * * This file is part of (April) Hebdobot. @@ -209,7 +209,7 @@ public class HebdobotCLI // logger.info("Bot configuring…"); - bot.getPastebinSettings().setApiKey(config.getPastebinApiKey()); + bot.getPrivatebinSettings().setServerUrl(config.getPrivatebinUrl()); bot.getIdenticaSettings().setApiKey(config.getIdenticaApiKey()); bot.getIdenticaSettings().setApiSecret(config.getIdenticaApiSecret()); bot.getTwitterSettings().setConsumerKey(config.getTwitterConsumerKey()); diff --git a/src/org/april/hebdobot/cli/HebdobotConfigFile.java b/src/org/april/hebdobot/cli/HebdobotConfigFile.java index 9b10815..6a9a0c2 100644 --- a/src/org/april/hebdobot/cli/HebdobotConfigFile.java +++ b/src/org/april/hebdobot/cli/HebdobotConfigFile.java @@ -21,6 +21,8 @@ package org.april.hebdobot.cli; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.Properties; @@ -332,30 +334,23 @@ public class HebdobotConfigFile extends Properties } /** - * Gets the pastebin api key. + * Gets the private bin url. * - * @return the pastebin api key + * @return the private bin url */ - public String getPastebinApiKey() + public URL getPrivatebinUrl() { - String result; + URL result; - result = getProperty("pastebin.apiKey"); - - // - return result; - } - - /** - * Gets the name. - * - * @return the name - */ - public String getPrivateBinUrl() - { - String result; - - result = getProperty("privatebin.url"); + try + { + String value = getProperty("privatebin.url"); + result = new URL(value); + } + catch (MalformedURLException exception) + { + result = null; + } // return result; diff --git a/src/org/april/hebdobot/privatebin/PrivatebinClient.java b/src/org/april/hebdobot/privatebin/PrivatebinClient.java index 699804e..a733369 100644 --- a/src/org/april/hebdobot/privatebin/PrivatebinClient.java +++ b/src/org/april/hebdobot/privatebin/PrivatebinClient.java @@ -30,7 +30,6 @@ import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import java.util.Arrays; import java.util.Base64; -import java.util.List; import java.util.Random; import java.util.zip.Deflater; @@ -48,23 +47,21 @@ import javax.net.ssl.HttpsURLConnection; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; -import org.apache.http.message.BasicNameValuePair; -import org.april.hebdobot.HebdobotException; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import fr.devinsy.strings.StringList; /** * The Class PastebinClient. */ -public class PrivatebinClient +public class PrivatebinClient extends PrivatebinSettings { - private String serverUrl; - private Compression compression; + private static final Logger logger = LoggerFactory.getLogger(PrivatebinClient.class); /** * Instantiates a new privatebin client. @@ -72,203 +69,180 @@ public class PrivatebinClient * @param serverUrl * the server url */ - public PrivatebinClient(final String serverUrl) + public PrivatebinClient(final PrivatebinSettings settings) { - this.serverUrl = serverUrl; - this.compression = Compression.ZLIB; + super(settings); + } + + /** + * Instantiates a new privatebin client. + * + * @param url + * the url + */ + public PrivatebinClient(final URL url) + { + super(); + setServerUrl(url); } /** * Paste. * - * @param serverUrl - * the server url - * @param expirationDate - * the expiration - * @param burnAfterReading - * the burn after reading - * @param openDiscussion - * the open discussion + * @param text + * the text + * @param settings + * the settings * @return the string - * @throws HebdobotException - * the hebdobot exception * @throws PrivatebinException + * the privatebin exception */ - public String paste(final String text, final Expiration expiration, final Formatter formatter, final BurnAfterReading burnAfterReading, - final OpenDiscussion openDiscussion) throws PrivatebinException + public String paste(final String text) throws PrivatebinException { String result; result = null; try { - if (expiration == null) + // Note: the following code is based on: + // https://github.com/PrivateBin/PrivateBin/wiki/API + // https://github.com/kkingsley-BF/PrivateBin-Groovy/blob/master/Paste.groovy + + // Build message to encrypt. + JSONObject pasteDataJson = new JSONObject(); + pasteDataJson.put("paste", text); + String pasteData = pasteDataJson.toJSONString(); + logger.debug("pasteData={}", pasteData); + + // Compression. + byte[] pasteDataBytes; + if (this.compression == Compression.ZLIB) { - throw new IllegalArgumentException("Null parameter expiration."); - } - else if (formatter == null) - { - throw new IllegalArgumentException("Null parameter formatter."); - } - else if (burnAfterReading == null) - { - throw new IllegalArgumentException("Null parameter burn after reading."); - } - else if (openDiscussion == null) - { - throw new IllegalArgumentException("Null parameter open discussion."); + Deflater zipDeflater = new Deflater(); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + zipDeflater.setInput(pasteData.getBytes()); + zipDeflater.finish(); + byte[] buffer = new byte[1024]; + while (!zipDeflater.finished()) + { + int count = zipDeflater.deflate(buffer); + stream.write(buffer, 0, count); + } + byte[] output; + output = stream.toByteArray(); + stream.close(); + zipDeflater.end(); + // Need to remove the header + pasteDataBytes = Arrays.copyOfRange(output, 2, output.length - 4); } else { - // Note: the following code is based on: - // https://github.com/PrivateBin/PrivateBin/wiki/API - // https://github.com/kkingsley-BF/PrivateBin-Groovy/blob/master/Paste.groovy + pasteDataBytes = pasteData.getBytes(); + } - // Optional parameters - // =================== + // Generate password. + KeyGenerator keyGen = KeyGenerator.getInstance("AES"); + keyGen.init(192); + String randomPassword = Base64.getEncoder().encodeToString(keyGen.generateKey().getEncoded()); + String customPassword = randomPassword + this.password; - // Location of local file to attach, leave quotes empty for no - // file - String localAttachmentFilename = ""; - // Paste attachment name, leave quotes empty for no file - String pasteAttachmentFilename = ""; - // Read file contents into paste, leave quotes empty no no file - String plaintextFileLocation = ""; - // Set paste password, leave quotes empty for no password - String userPastePassword = ""; + // Generate IV. + byte[] cipherIVBytes = new byte[16]; + new Random().nextBytes(cipherIVBytes); + String cipherIVEncoded = Base64.getEncoder().encodeToString(cipherIVBytes); - // + // Generate salt. + byte[] kdfSaltBytes = new byte[8]; + new Random().nextBytes(kdfSaltBytes); + String kdfSaltEncoded = Base64.getEncoder().encodeToString(kdfSaltBytes); - // Generate password. - KeyGenerator keyGen = KeyGenerator.getInstance("AES"); - keyGen.init(192); - String randomPassword = Base64.getEncoder().encodeToString(keyGen.generateKey().getEncoded()); - String customPassword = randomPassword + userPastePassword; + // Generate secret key for cipher. + SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); + KeySpec passwordBasedEncryptionKeySpec = new PBEKeySpec(customPassword.toCharArray(), kdfSaltBytes, 100000, 256); + SecretKey secret = new SecretKeySpec(factory.generateSecret(passwordBasedEncryptionKeySpec).getEncoded(), "AES"); - // Generate IV. - byte[] cipherIVBytes = new byte[16]; - new Random().nextBytes(cipherIVBytes); - String cipherIVEncoded = Base64.getEncoder().encodeToString(cipherIVBytes); + // Cipher AAD. + StringList gcmTagString = new StringList(); + gcmTagString.append("["); + gcmTagString.append("["); + gcmTagString.append("\"").append(cipherIVEncoded).append("\"").append(","); + gcmTagString.append("\"").append(kdfSaltEncoded).append("\"").append(","); + gcmTagString.append("100000,256,128,"); + gcmTagString.append("\"").append("aes").append("\"").append(","); + gcmTagString.append("\"").append("gcm").append("\"").append(","); + gcmTagString.append("\"").append(this.compression.toString()).append("\""); + gcmTagString.append("]"); + gcmTagString.append(","); + gcmTagString.append("\"").append(this.formatter.toString()).append("\"").append(","); + gcmTagString.append(this.openDiscussion.toString()).append(","); + gcmTagString.append(this.burnAfterReading.toString()); + gcmTagString.append("]"); + byte[] gcmBytes = gcmTagString.toString().getBytes(); - // Generate salt. - byte[] kdfSaltBytes = new byte[8]; - new Random().nextBytes(kdfSaltBytes); - String kdfSaltEncoded = Base64.getEncoder().encodeToString(kdfSaltBytes); + System.out.println("gcm=" + gcmTagString.toString()); - // Build message to encrypt. - String pasteData = "{\"paste\":\"" + text + "\"}"; + // Generate cipher text. + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); + GCMParameterSpec spec = new GCMParameterSpec(128, cipherIVBytes); + cipher.init(Cipher.ENCRYPT_MODE, secret, spec); + cipher.updateAAD(gcmBytes); + byte[] cipherTextBytes = cipher.doFinal(pasteDataBytes); + String cipherText = Base64.getEncoder().encodeToString(cipherTextBytes); + logger.debug("cipherText={}", cipherText); - // Compression. - byte[] pasteDataBytes; - if (StringUtils.equals(this.compression.toString(), "zlib")) + // Create POST payload. + StringList payload = new StringList(); + payload.append("{"); + payload.append("\"v\":2,"); + payload.append("\"adata\":").append(gcmTagString.toString()).append(","); + payload.append("\"ct\":\"").append(cipherText).append("\","); + payload.append("\"meta\":{\"expire\":\"").append(this.expiration).append("\"}"); + payload.append("}"); + logger.debug("PAYLOAD={}", payload.toString()); + + // POST Request. + HttpsURLConnection pasteRequest = (HttpsURLConnection) this.serverUrl.openConnection(); + pasteRequest.setRequestMethod("POST"); + pasteRequest.setDoOutput(true); + pasteRequest.setRequestProperty("X-Requested-With", "JSONHttpRequest"); + pasteRequest.getOutputStream().write(payload.toString().getBytes()); + + // Server response. + int responseCode = pasteRequest.getResponseCode(); + logger.debug("Server response: {}", responseCode); + if (responseCode == 200) + { + String out = IOUtils.toString(pasteRequest.getInputStream(), "UTF-8"); + logger.debug("===> {}", out); + + JSONObject parser = (JSONObject) new JSONParser().parse(out); + String status = parser.get("status").toString(); + if (StringUtils.equals(status, "0")) { - Deflater zipDeflater = new Deflater(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - zipDeflater.setInput(pasteData.getBytes()); - zipDeflater.finish(); - byte[] buffer = new byte[1024]; - while (!zipDeflater.finished()) - { - int count = zipDeflater.deflate(buffer); - stream.write(buffer, 0, count); - } - byte[] output; - output = stream.toByteArray(); - stream.close(); - zipDeflater.end(); - // Need to remove the header - pasteDataBytes = Arrays.copyOfRange(output, 2, output.length - 4); + String id = parser.get("id").toString(); + String pasteURL = parser.get("url").toString(); + String deleteToken = parser.get("deletetoken").toString(); + String finalURL = this.serverUrl + pasteURL + "#" + Base58.encode(randomPassword.getBytes()); + String deleteURL = this.serverUrl + pasteURL + "&deletetoken=" + deleteToken; + logger.info("Pastebin SUCESS"); + logger.debug("Paste URL: {}", finalURL); + logger.debug("Delete URL: {}", deleteURL); + result = finalURL; } else { - pasteDataBytes = pasteData.getBytes(); - } - - // Generate secret key for cipher. - SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); - KeySpec passwordBasedEncryptionKeySpec = new PBEKeySpec(customPassword.toCharArray(), kdfSaltBytes, 100000, 256); - SecretKey secret = new SecretKeySpec(factory.generateSecret(passwordBasedEncryptionKeySpec).getEncoded(), "AES"); - - // Cipher AAD. - StringList gcmTagString = new StringList(); - gcmTagString.append("["); - gcmTagString.append("["); - gcmTagString.append("\"").append(cipherIVEncoded).append("\"").append(","); - gcmTagString.append("\"").append(kdfSaltEncoded).append("\"").append(","); - gcmTagString.append("100000,256,128,"); - gcmTagString.append("\"").append("aes").append("\"").append(","); - gcmTagString.append("\"").append("gcm").append("\"").append(","); - gcmTagString.append("\"").append(this.compression).append("\""); - gcmTagString.append("]"); - gcmTagString.append(","); - gcmTagString.append("\"").append(formatter).append("\"").append(","); - gcmTagString.append(openDiscussion).append(","); - gcmTagString.append(burnAfterReading); - gcmTagString.append("]"); - byte[] gcmBytes = gcmTagString.toString().getBytes(); - - System.out.println("gcm=" + gcmTagString.toString()); - - // Generate cipher text. - Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); - GCMParameterSpec spec = new GCMParameterSpec(128, cipherIVBytes); - cipher.init(Cipher.ENCRYPT_MODE, secret, spec); - cipher.updateAAD(gcmBytes); - byte[] cipherTextBytes = cipher.doFinal(pasteDataBytes); - String cipherText = Base64.getEncoder().encodeToString(cipherTextBytes); - - // Create POST payload. - StringList payload = new StringList(); - payload.append("{"); - payload.append("\"v\":2,"); - payload.append("\"adata\":").append(gcmTagString.toString()).append(","); - payload.append("\"ct\":\"").append(cipherText).append("\","); - payload.append("\"meta\":{\"expire\":\"").append(expiration).append("\"}"); - payload.append("}"); - - // POST Request. - HttpsURLConnection pasteRequest = (HttpsURLConnection) new URL(this.serverUrl).openConnection(); - pasteRequest.setRequestMethod("POST"); - pasteRequest.setDoOutput(true); - pasteRequest.setRequestProperty("X-Requested-With", "JSONHttpRequest"); - pasteRequest.getOutputStream().write(payload.toString().getBytes()); - System.out.println("PAYLOAD=" + payload.toString()); - - // Server response. - int responseCode = pasteRequest.getResponseCode(); - System.out.println("Server response: " + responseCode); - if (responseCode == 200) - { - - String out = IOUtils.toString(pasteRequest.getInputStream(), "UTF-8"); - System.out.println("===> " + out); - - JSONObject parser = (JSONObject) new JSONParser().parse(out); - String status = parser.get("status").toString(); - if (StringUtils.equals(status, "0")) - { - String id = parser.get("id").toString(); - String pasteURL = parser.get("url").toString(); - String deleteToken = parser.get("deletetoken").toString(); - String finalURL = this.serverUrl + pasteURL + "#" + Base58.encode(randomPassword.getBytes()); - String deleteURL = this.serverUrl + pasteURL + "&deletetoken=" + deleteToken; - System.out.println("SUCESS"); - System.out.println("Paste URL: " + finalURL); - System.out.println("Delete URL: " + deleteURL); - } - else - { - String output = parser.get("message").toString(); - System.out.println("message=" + output); - throw new PrivatebinException(out); - } + String output = parser.get("message").toString(); + logger.warn("message={}", output); + throw new PrivatebinException(out); } } } catch (BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException | InvalidKeyException | InvalidKeySpecException - | NoSuchAlgorithmException | NoSuchPaddingException | ParseException | UnsupportedEncodingException - | ClientProtocolException exception) + | NoSuchAlgorithmException | NoSuchPaddingException | ParseException | UnsupportedEncodingException | + + ClientProtocolException exception) { + exception.printStackTrace(); throw new PrivatebinException(exception); } catch (IOException exception) @@ -280,22 +254,4 @@ public class PrivatebinClient // return result; } - - /** - * Sets the parameter. - * - * @param params - * the params - * @param name - * the name - * @param value - * the value - */ - private static void setParameter(final List params, final String name, final String value) - { - if (value != null) - { - params.add(new BasicNameValuePair(name, value)); - } - } } diff --git a/src/org/april/hebdobot/privatebin/PrivatebinSettings.java b/src/org/april/hebdobot/privatebin/PrivatebinSettings.java index fa3c9b2..35e887b 100644 --- a/src/org/april/hebdobot/privatebin/PrivatebinSettings.java +++ b/src/org/april/hebdobot/privatebin/PrivatebinSettings.java @@ -18,17 +18,20 @@ */ package org.april.hebdobot.privatebin; -import org.apache.commons.lang3.StringUtils; +import java.net.URL; /** * The Class PrivatebinSettings */ public class PrivatebinSettings { - private String serverUrl; - private String expirationDate; - private String burnAfterReading; - private String openDiscussion; + protected URL serverUrl; + protected Compression compression; + protected Expiration expiration; + protected BurnAfterReading burnAfterReading; + protected OpenDiscussion openDiscussion; + protected Formatter formatter; + protected String password; /** * Instantiates a new Privatebin settings. @@ -36,27 +39,62 @@ public class PrivatebinSettings public PrivatebinSettings() { this.serverUrl = null; - this.expirationDate = null; - this.burnAfterReading = null; - this.openDiscussion = null; + this.compression = Compression.ZLIB; + this.expiration = Expiration.DAY_1; + this.burnAfterReading = BurnAfterReading.OFF; + this.openDiscussion = OpenDiscussion.OFF; + this.formatter = Formatter.PLAINTEXT; + this.password = ""; } - public String getBurnAfterReading() + /** + * Instantiates a new privatebin settings. + * + * @param settings + * the settings + */ + public PrivatebinSettings(final PrivatebinSettings settings) + { + this.serverUrl = settings.serverUrl; + this.compression = settings.compression; + this.expiration = settings.expiration; + this.burnAfterReading = settings.burnAfterReading; + this.openDiscussion = settings.openDiscussion; + this.formatter = settings.formatter; + this.password = settings.password; + } + + public BurnAfterReading getBurnAfterReading() { return this.burnAfterReading; } - public String getExpirationDate() + public Compression getCompression() { - return this.expirationDate; + return this.compression; } - public String getOpenDiscussion() + public Expiration getExpiration() + { + return this.expiration; + } + + public Formatter getFormatter() + { + return this.formatter; + } + + public OpenDiscussion getOpenDiscussion() { return this.openDiscussion; } - public String getServerUrl() + public String getPassword() + { + return this.password; + } + + public URL getServerUrl() { return this.serverUrl; } @@ -70,10 +108,15 @@ public class PrivatebinSettings { boolean result; - if ((StringUtils.isBlank(this.serverUrl)) || (StringUtils.containsOnly(this.serverUrl, 'X'))) + if (this.serverUrl == null) { result = false; } + else if ((this.burnAfterReading == BurnAfterReading.ON) && (this.openDiscussion == OpenDiscussion.ON)) + { + // Burn after reading cannot be active if opendiscussion is. + result = false; + } else { result = true; @@ -83,23 +126,78 @@ public class PrivatebinSettings return result; } - public void setBurnAfterReading(final String burnAfterReading) + public void setBurnAfterReading(final BurnAfterReading burnAfterReading) { - this.burnAfterReading = burnAfterReading; + if (burnAfterReading != null) + { + this.burnAfterReading = burnAfterReading; + } } - public void setExpirationDate(final String expirationDate) + public void setCompression(final Compression compression) { - this.expirationDate = expirationDate; + if (compression != null) + { + this.compression = compression; + } } - public void setOpenDiscussion(final String openDiscussion) + public void setExpiration(final Expiration expiration) { - this.openDiscussion = openDiscussion; + if (expiration != null) + { + this.expiration = expiration; + } } - public void setServerUrl(final String serverUrl) + public void setFormatter(final Formatter formatter) + { + if (formatter != null) + { + this.formatter = formatter; + } + } + + public void setOpenDiscussion(final OpenDiscussion openDiscussion) + { + if (openDiscussion != null) + { + this.openDiscussion = openDiscussion; + } + } + + public void setPassword(final String password) + { + if (password == null) + { + this.password = ""; + } + else + { + this.password = password; + } + } + + public void setServerUrl(final URL serverUrl) { this.serverUrl = serverUrl; } + + /** + * To string. + * + * @return the string + */ + @Override + public String toString() + { + String result; + + result = String.format("valid=%b, url=%s, formatter=%s, compression=%s, expiration=%s, burnafterreading=%s, opendiscussion=%s, password=%s", + isValid(), this.serverUrl, this.formatter, this.compression, this.expiration, this.burnAfterReading, this.openDiscussion, + this.password); + + // + return result; + } } diff --git a/test/org/april/hebdobot/privatebin/PrivatebinClientTest.java b/test/org/april/hebdobot/privatebin/PrivatebinClientTest.java index 5521000..2c8503b 100644 --- a/test/org/april/hebdobot/privatebin/PrivatebinClientTest.java +++ b/test/org/april/hebdobot/privatebin/PrivatebinClientTest.java @@ -18,6 +18,11 @@ */ package org.april.hebdobot.privatebin; +import java.io.File; +import java.net.URL; + +import org.apache.commons.io.FileUtils; +import org.apache.log4j.BasicConfigurator; import org.junit.Test; /** @@ -34,13 +39,39 @@ public class PrivatebinClientTest @Test public void testPaste00() throws Exception { - PrivatebinClient client = new PrivatebinClient("https://cpaste.org/"); + PrivatebinClient client = new PrivatebinClient(new URL("https://cpaste.org/")); // PrivatebinClient client = new // PrivatebinClient("https://paste.chapril.org/"); - String text = "This is a test."; + String text = "This is a test.\ntoto"; - String result = client.paste(text, Expiration.MINUTE_5, Formatter.PLAINTEXT, BurnAfterReading.OFF, OpenDiscussion.OFF); + client.setExpiration(Expiration.MINUTE_5); + String result = client.paste(text); + + System.out.println("result:" + result); + } + + /** + * Test paste 01. + * + * @throws Exception + * the exception + */ + @Test + public void testPaste01() throws Exception + { + BasicConfigurator.configure(); + + PrivatebinClient client = new PrivatebinClient(new URL("https://cpaste.org/")); + // PrivatebinClient client = new + // PrivatebinClient("https://paste.chapril.org/"); + + // String text = FileUtils.readFileToString(new + // File("./test/org/april/hebdobot/privatebin/20210305-log-irc-revue-hebdomadaire.txt")); + String text = FileUtils.readFileToString(new File("./test/org/april/hebdobot/privatebin/20210319-log-irc-revue-hebdomadaire.txt")); + + client.setExpiration(Expiration.MINUTE_5); + String result = client.paste(text); System.out.println("result:" + result); }