Created
December 21, 2012 21:39
-
-
Save anonymous/4356017 to your computer and use it in GitHub Desktop.
SASL implementation for PircBotX. Requires the following patch to the bot library: http://code.google.com/p/pircbotx/issues/detail?id=97
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SASL extends ListenerAdapter { | |
String user = "user"; // Change accordingly! | |
String password = "password"; // Change accordingly! | |
boolean connected = false; | |
@Override | |
public void onMotd(MotdEvent event) throws Exception { | |
connected = true; | |
} | |
/** | |
* Tokenize IRC raw input into it's components, keeping the | |
* 'sender' and 'message' fields intact. | |
* @param input A string in the format [:]item [item] ... [:item [item] ...] | |
* @return List of strings. | |
*/ | |
private static List<String> tokenize(String input) { | |
List<String> retn = new ArrayList<String>(); | |
if (input == null || input.length() == 0) | |
return retn; | |
String temp = input; | |
while (temp.contains(" ")) { | |
if (temp.startsWith(":") && retn.size() > 0) { | |
retn.add(temp.substring(1)); | |
return retn; | |
} | |
String[] split = temp.split(" ", 2); | |
retn.add(split[0]); | |
if (split.length > 1) | |
temp = split[1]; | |
} | |
return retn; | |
} | |
@Override | |
public void onSocketConnect(SocketConnectEvent event) throws Exception { | |
event.respond("CAP LS"); | |
connected = false; | |
} | |
@Override | |
public void onUnknown(UnknownEvent event) throws Exception { | |
if (connected) | |
return; | |
String line = event.getLine(); // :Sender CAP MyNick CMD [:More here] | |
List<String> args = tokenize(line); // [:Sender, CAP, MyNick, CMD, More here] | |
if (args.size() >= 1) { | |
if (args.get(0).startsWith(":")) | |
args.remove(0); // Sender | |
String response = args.remove(0); // "CAP" | |
if (response.equalsIgnoreCase("CAP")) { | |
if (args.size() >= 3) { | |
args.remove(0); // "MyNick" | |
String cmd = args.remove(0); // "CMD" | |
if (cmd.equalsIgnoreCase("NAK")) | |
event.respond("CAP END"); | |
else if (cmd.equalsIgnoreCase("LS")) { | |
List<String> caps = Arrays.asList(args.get(0).split(" ")); // [More, here] | |
if (caps.contains("sasl")) | |
event.respond("CAP REQ :sasl"); | |
else | |
event.respond("CAP END"); | |
} else if (cmd.equalsIgnoreCase("ACK")) { | |
List<String> caps = Arrays.asList(args.get(0).split(" ")); // [More, here] | |
if (caps.contains("sasl")) | |
event.respond("AUTHENTICATE PLAIN"); | |
} | |
} | |
} else if (response.equalsIgnoreCase("AUTHENTICATE")) { | |
Base64 test = new Base64(); | |
byte[] out = test.encode((user + '\0' + user + '\0' + password).getBytes("UTF-8")); | |
event.respond("AUTHENTICATE " + new String(out)); | |
} | |
} | |
} | |
@Override | |
public void onServerResponse(ServerResponseEvent event) throws Exception { | |
if (event.getCode() == 903) | |
event.respond("CAP END"); | |
else if (event.getCode() == 904 || event.getCode() == 905) { | |
// SASL auth failed. | |
// Respond accordingly here... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment