Allow posting to pastie.org or pastebin.com (pastie.org is the new default); closes #277

This commit is contained in:
Tim Ekl 2011-11-28 13:51:10 -05:00 committed by Eric Stokes
parent 8e74945b7e
commit df5bfdde80
7 changed files with 247 additions and 41 deletions

View File

@ -9,18 +9,15 @@ package com.onarandombox.MultiverseCore.commands;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.event.MVVersionRequestEvent;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteFailedException;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteService;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceFactory;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceType;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionDefault;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
@ -29,12 +26,12 @@ public class VersionCommand extends MultiverseCommand {
public VersionCommand(MultiverseCore plugin) {
super(plugin);
this.setName("Multiverse Version");
this.setCommandUsage("/mv version " + ChatColor.GOLD + "-p");
this.setCommandUsage("/mv version " + ChatColor.GOLD + "-[pb]");
this.setArgRange(0, 1);
this.addKey("mv version");
this.addKey("mvv");
this.addKey("mvversion");
this.setPermission("multiverse.core.version", "Dumps version info to the console, optionally to pastebin.com with a -p.", PermissionDefault.TRUE);
this.setPermission("multiverse.core.version", "Dumps version info to the console, optionally to pastie.org with -p or pastebin.com with a -b.", PermissionDefault.TRUE);
}
private String pasteBinBuffer = "";
@ -65,10 +62,18 @@ public class VersionCommand extends MultiverseCommand {
this.plugin.getServer().getPluginManager().callEvent(versionEvent);
pasteBinBuffer = versionEvent.getPasteBinBuffer();
if (args.size() == 1 && args.get(0).equalsIgnoreCase("-p")) {
String pasteBinUrl = postToPasteBin();
sender.sendMessage("Version info dumped here: " + ChatColor.GREEN + pasteBinUrl);
this.plugin.log(Level.INFO, "Version info dumped here: " + pasteBinUrl);
if (args.size() == 1) {
String pasteUrl = "";
if (args.get(0).equalsIgnoreCase("-p")) {
pasteUrl = this.postToService(PasteServiceType.PASTIE, true); // private post to pastie
} else if (args.get(0).equalsIgnoreCase("-b")) {
pasteUrl = this.postToService(PasteServiceType.PASTEBIN, true); // private post to pastie
} else {
return;
}
sender.sendMessage("Version info dumped here: " + ChatColor.GREEN + pasteUrl);
this.plugin.log(Level.INFO, "Version info dumped here: " + pasteUrl);
}
}
@ -77,36 +82,20 @@ public class VersionCommand extends MultiverseCommand {
this.plugin.log(Level.INFO, string);
}
private String postToPasteBin() {
/**
* Send the current contents of this.pasteBinBuffer to a web service.
*
* @param type Service type to send to
* @param isPrivate Should the paste be marked as private.
* @return URL of visible paste
*/
private String postToService(PasteServiceType type, boolean isPrivate) {
PasteService ps = PasteServiceFactory.getService(type, isPrivate);
try {
String data = URLEncoder.encode("api_dev_key", "UTF-8") + "=" + URLEncoder.encode("d61d68d31e8e0392b59b50b277411c71", "UTF-8");
data += "&" + URLEncoder.encode("api_option", "UTF-8") + "=" + URLEncoder.encode("paste", "UTF-8");
data += "&" + URLEncoder.encode("api_paste_code", "UTF-8") + "=" + URLEncoder.encode(this.pasteBinBuffer, "UTF-8");
data += "&" + URLEncoder.encode("api_paste_private", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");
data += "&" + URLEncoder.encode("api_paste_format", "UTF-8") + "=" + URLEncoder.encode("yaml", "UTF-8");
Date date = new Date();
// We're out of
//data += "&" + URLEncoder.encode("api_paste_name", "UTF-8") + "=" + URLEncoder.encode("Multiverse 2 Dump " + dateFormat.format(date), "UTF-8");
//data += "&" + URLEncoder.encode("api_user_key", "UTF-8") + "=" + URLEncoder.encode("c052ac52d2b0db88d36cc32ca462d151", "UTF-8");
URL url = new URL("http://pastebin.com/api/api_post.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String pasteBinUrl = "";
while ((line = rd.readLine()) != null) {
pasteBinUrl = line;
}
wr.close();
rd.close();
return pasteBinUrl;
} catch (Exception e) {
return ps.postData(ps.encodeData(this.pasteBinBuffer), ps.getPostURL());
} catch (PasteFailedException e) {
System.out.print(e);
return "Error Posting to pastebin.com";
return "Error posting to service";
}
}
}

View File

@ -0,0 +1,11 @@
package com.onarandombox.MultiverseCore.utils.webpaste;
public class PasteFailedException extends Exception {
public PasteFailedException() {
super();
}
public PasteFailedException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,33 @@
package com.onarandombox.MultiverseCore.utils.webpaste;
import java.net.URL;
public interface PasteService {
/**
* Encode the given String data into a format suitable for transmission in an HTTP request.
*
* @param data The raw data to encode.
* @param isPrivate Whether the paste is desired to be private.
* @return A URL-encoded string.
*/
public String encodeData(String data);
/**
* Get the URL to which this paste service sends new pastes.
*
* @param isPrivate Whether the paste is desired to be private.
* @return The URL that will be accessed to complete the paste.
*/
public URL getPostURL();
/**
* Post encoded data to the Web.
*
* @param encodedData A URL-encoded String containing the full request to post to
* the given URL. Can be the result of calling #encodeData().
* @param url The URL to which to paste. Can be the result of calling #getPostURL().
* @param isPrivate Whether the paste is desired to be private.
* @return The URL at which the new paste is visible.
*/
public String postData(String encodedData, URL url) throws PasteFailedException;
}

View File

@ -0,0 +1,11 @@
package com.onarandombox.MultiverseCore.utils.webpaste;
public class PasteServiceFactory {
public static PasteService getService(PasteServiceType type, boolean isPrivate) {
switch(type) {
case PASTEBIN: return new PastebinPasteService(isPrivate);
case PASTIE: return new PastiePasteService(isPrivate);
default: return null;
}
}
}

View File

@ -0,0 +1,6 @@
package com.onarandombox.MultiverseCore.utils.webpaste;
public enum PasteServiceType {
PASTEBIN,
PASTIE
}

View File

@ -0,0 +1,76 @@
package com.onarandombox.MultiverseCore.utils.webpaste;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PastebinPasteService implements PasteService {
protected boolean isPrivate;
public PastebinPasteService(boolean isPrivate) {
this.isPrivate = isPrivate;
}
public URL getPostURL() {
try {
return new URL("http://pastebin.com/api/api_post.php");
} catch (MalformedURLException e) {
return null; // should never hit here
}
}
public String encodeData(String data) {
try {
String encData = URLEncoder.encode("api_dev_key", "UTF-8") + "=" + URLEncoder.encode("d61d68d31e8e0392b59b50b277411c71", "UTF-8");
encData += "&" + URLEncoder.encode("api_option", "UTF-8") + "=" + URLEncoder.encode("paste", "UTF-8");
encData += "&" + URLEncoder.encode("api_paste_code", "UTF-8") + "=" + URLEncoder.encode(data, "UTF-8");
encData += "&" + URLEncoder.encode("api_paste_private", "UTF-8") + "=" + URLEncoder.encode(this.isPrivate ? "1" : "0", "UTF-8");
encData += "&" + URLEncoder.encode("api_paste_format", "UTF-8") + "=" + URLEncoder.encode("yaml", "UTF-8");
return encData;
} catch (UnsupportedEncodingException e) {
return ""; // should never hit here
}
}
public String postData(String encodedData, URL url) throws PasteFailedException {
try {
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(encodedData);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String pastebinUrl = "";
while ((line = rd.readLine()) != null) {
pastebinUrl = line;
}
wr.close();
rd.close();
return pastebinUrl;
} catch (Exception e) {
throw new PasteFailedException(e);
}
}
private Pattern getURLMatchingPattern() {
if(this.isPrivate) {
return Pattern.compile(".*http://pastie.org/.*key=([0-9a-z]+).*");
} else {
return Pattern.compile(".*http://pastie.org/([0-9]+).*");
}
}
private String formatURL(String pastieID) {
return "http://pastie.org/" + (this.isPrivate ? "private/" : "") + pastieID;
}
}

View File

@ -0,0 +1,80 @@
package com.onarandombox.MultiverseCore.utils.webpaste;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PastiePasteService implements PasteService {
protected boolean isPrivate;
public PastiePasteService(boolean isPrivate) {
this.isPrivate = isPrivate;
}
public URL getPostURL() {
try {
return new URL("http://pastie.org/pastes");
} catch (MalformedURLException e) {
return null; // should never hit here
}
}
public String encodeData(String data) {
try {
String encData = URLEncoder.encode("paste[authorization]", "UTF-8") + "=" + URLEncoder.encode("burger", "UTF-8"); // burger is magic
encData += "&" + URLEncoder.encode("paste[restricted]", "UTF-8") + "=" + URLEncoder.encode(this.isPrivate ? "1" : "0", "UTF-8");
encData += "&" + URLEncoder.encode("paste[parser_id]", "UTF-8") + "=" + URLEncoder.encode("6", "UTF-8"); // 6 is plain text
encData += "&" + URLEncoder.encode("paste[body]", "UTF-8") + "=" + URLEncoder.encode(data, "UTF-8");
return encData;
} catch (UnsupportedEncodingException e) {
return ""; // should never hit here
}
}
public String postData(String encodedData, URL url) throws PasteFailedException {
try {
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(encodedData);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String pastieUrl = "";
Pattern pastiePattern = this.getURLMatchingPattern();
while ((line = rd.readLine()) != null) {
Matcher m = pastiePattern.matcher(line);
if(m.matches()) {
String pastieID = m.group(1);
pastieUrl = this.formatURL(pastieID);
}
}
wr.close();
rd.close();
return pastieUrl;
} catch (Exception e) {
throw new PasteFailedException(e);
}
}
private Pattern getURLMatchingPattern() {
if(this.isPrivate) {
return Pattern.compile(".*http://pastie.org/.*key=([0-9a-z]+).*");
} else {
return Pattern.compile(".*http://pastie.org/([0-9]+).*");
}
}
private String formatURL(String pastieID) {
return "http://pastie.org/" + (this.isPrivate ? "private/" : "") + pastieID;
}
}