Skip to content

Instantly share code, notes, and snippets.

@azenla
Created June 20, 2013 23:18
Show Gist options
  • Select an option

  • Save azenla/5827627 to your computer and use it in GitHub Desktop.

Select an option

Save azenla/5827627 to your computer and use it in GitHub Desktop.
GCPHelper
package com.directmyfile.gcp.client.gcp;
import com.directmyfile.gcp.client.Main;
public class GCPHelper {
public static GCPLine getLineFromLine(String line) {
return new GCPLine(line);
}
public static void changeNickname(String newNickname) {
// TODO: Make this when GCP Specification is out
}
public static void register(String password) {
sendRaw(GCPConstants.REGISTER + GCPLine.colon + GCPConstants.PASSWORD_PROP + GCPLine.equals + password);
}
public static void ping() {
sendRaw(GCPConstants.PING + GCPLine.colon);
}
public static void ping(String user) {
sendRaw(GCPConstants.PING + GCPLine.colon + GCPConstants.USER_PROP + GCPLine.equals + user);
}
public static void sendRaw(String line) {
Main.connection.sendLine(line);
}
public static GCPError parseError(String line) {
GCPLine gcpLine = new GCPLine(line);
String errorString = gcpLine.getProperty(GCPConstants.ERROR_PROP);
String type = gcpLine.getProperty(GCPConstants.TYPE_PROP);
int error = Integer.parseInt(errorString);
if (gcpLine.getProperty(GCPConstants.CHANNEL_PROP)!=null) {
boolean channel = gcpLine.getProperty(GCPConstants.CHANNEL_PROP).equals("true");
String user = gcpLine.getProperty("user");
return new GCPError(error, type, channel, user, gcpLine.getProperty(GCPConstants.PARAMETERS_PROP));
} else {
return new GCPError(error, type, gcpLine.getProperty(GCPConstants.PARAMETERS_PROP));
}
}
public static GCPEventHandler getEventHandler() {
return Main.connection.getEventHandler().getGCPEventHandler();
}
public static void joinChannel(String channel) {
sendRaw(GCPConstants.JOIN + GCPLine.colon + GCPConstants.CHANNEL_PROP + GCPLine.equals + channel);
}
public static void leaveChannel(String channel) {
sendRaw(GCPConstants.LEAVE + GCPLine.colon + GCPConstants.CHANNEL_PROP + GCPLine.equals + channel);
}
public static void sendMessage(String target, String message) {
String isChannel;
if (target.startsWith("#")) {
isChannel = "true";
} else {
isChannel = "false";
}
sendRaw(GCPConstants.MSG + GCPLine.colon + GCPConstants.CHANNEL_PROP + GCPLine.equals + isChannel + GCPLine.colon + GCPConstants.TARGET_PROP + GCPLine.equals + target + GCPLine.colon + GCPConstants.MESSAGE_PROP + GCPLine.equals + message);
}
public static void quit() {
if (Main.connection.isConnected()) {
sendRaw("QUIT" + GCPLine.colon);
}
}
public static void safeExit(int exitCode) {
sendRaw("QUIT" + GCPLine.colon);
System.exit(exitCode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment