package components;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URLEncoder;
import main.Client;
public class ChatHandler {
private static boolean connected = false;
private static String PHPSESSID;
private static String[] lastList = {"",""};
private static String lastBan = "";
private static int numFails = 0;
public static boolean isConnected() {
return connected;
}
public static void confirmReloadUserList() {
lastList = new String[] {"",""};
}
private static String encodePOST(String key, String val) {
try {
return URLEncoder.encode(key,"UTF-8") + "=" + URLEncoder.encode(val,"UTF-8");
} catch (Exception ex) {
ex.printStackTrace(Client.theConsole);
return null;
}
}
public static void login(Window target, String userid, String username, String groups, String permissions, String rep, String check) {
try {
Socket s = new Socket("chat.hiveworkshop.com",80);
java.io.PrintWriter pw = new java.io.PrintWriter(s.getOutputStream());
String data = encodePOST("userid",userid)+"&"+encodePOST("username",username)+"&"+encodePOST("groups",groups)+"&"+encodePOST("permissions",permissions)+"&"+encodePOST("rep",rep)+"&"+encodePOST("check",check);
pw.print("POST / HTTP/1.1\r\n");
pw.print("Host: chat.hiveworkshop.com\r\n");
pw.print("Content-Length: "+data.length()+"\r\n");
pw.print("Content-Type: application/x-www-form-urlencoded\r\n");
pw.print("\r\n");
pw.print(data);
pw.flush();
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line;
String targ = "PHPSESSID=";
while ((line=br.readLine())!=null) {
if (line.contains(targ)) {
for (int i=0;i<line.length();i++) {
if (line.substring(i,i+targ.length()).equals(targ)) {
line = line.substring(i);
PHPSESSID=line.split(";")[0];
target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Successfully connected to server"));
break;
}
}
break;
}
}
s.close();
br.close();
pw.close();
isr.close();
connected = true;
} catch (IOException ex) {
ex.printStackTrace(Client.theConsole);
target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Failed to connect to server"));
} catch (Exception ex) {
ex.printStackTrace(Client.theConsole);
}
}
public static Message[] getMessages(Window target, int channel) {
if (!connected) {
return null;
}
try {
Socket s = new Socket("chat.hiveworkshop.com",80);
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.print("GET /chat_ajax.php?do=get&channel="+channel+" HTTP/1.1\r\n");
pw.print("Host: chat.hiveworkshop.com\r\n");
pw.print("Cookie: "+PHPSESSID+"\r\n");
pw.print("Referer: http://chat.hiveworkshop.com/\r\n");
pw.print("\r\n");
pw.flush();
InputStreamReader isr = new InputStreamReader(s.getInputStream());
while (!isr.ready()) {
Thread.sleep(10);
}
BufferedReader br = new BufferedReader(isr);
for (int i=0;i<10;i++) {
br.readLine();
}
int len = 1;
try {
len = Integer.parseInt(br.readLine(),16);
} catch (Exception ex) {
ex.printStackTrace();
}
char[] chars = new char[len];
for (int i=0;i<chars.length;i++) {
chars[i] = (char)br.read();
}
String line = new String(chars);
line = new String(line.getBytes(),"UTF-8");
if (line.contains("<banned>")) {
String message = "You are banned. Time until unban: ";
try {
message += line.split("<banned>")[1].split("</banned>")[0];
} catch (Exception ex) {
ex.printStackTrace(Client.theConsole);
}
if (!lastBan.equals(message)) {
lastBan = message;
target.writeToAll(new Message(target,Message.TYPE_SYSTEM,message));
}
return null;
}
lastBan = "";
String[] msgs = line.split("<m>");
Message[] messages = new Message[msgs.length-1];
for (int i=1;i<msgs.length;i++) {
String username = msgs[i].split("<username>")[1].split("</username>")[0];
String message = msgs[i].split("<message>")[1].split("</message>")[0];
String rank = msgs[i].split("<image>")[1].split("</image>")[0];
String type = msgs[i].split("<type>")[1].split("</type>")[0];
String ucolor = msgs[i].split("<color>")[1].split("</color>")[0];
username = username.substring(9,username.length()-3);
message = message.substring(9,message.length()-3).replace("http://hiveworkshop.com/resources_new/images/smileys/","file:///"+target.getClient().getImagesFolderPath()+"emotes"+System.getProperty("file.separator"));
rank = rank.substring(9,rank.length()-3);
if (rank.length()>4) {
rank = rank.substring(0,rank.length()-4);
}
type = type.substring(9,type.length()-3);
ucolor = ucolor.substring(9,ucolor.length()-3);
User u = new User(target,rank,username,ucolor);
int mType = 0;
if (type.equals("0")) {
if (Client.getBotOwner().equals(username)) {
mType = Message.TYPE_SELF;
} else {
mType = Message.TYPE_NORMAL;
}
} else if (type.equals("1")) {
mType = Message.TYPE_EMOTE;
} else if (type.equals("2") | type.equals("5")) {
mType = Message.TYPE_SYSTEM;
} else if (type.equals("3")) {
mType = Message.TYPE_WHISPER;
} else if (type.equals("4")) {
mType = Message.TYPE_ANNOUNCE;
} else if (type.equals("6")) {
mType = Message.TYPE_DING;
}
if (channel==1) {
messages[i-1] = new Message(target,u,target.getChannelObject("Lobby"),mType,message);
} else if (target.getChannelObject("Staff")!=null){
messages[i-1] = new Message(target,u,target.getChannelObject("Staff"),mType,message);
}
}
s.close();
s.close();
br.close();
pw.close();
isr.close();
return messages;
} catch (IOException ex) {
ex.printStackTrace(Client.theConsole);
target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Failed to receive messages"));
numFails++;
if (numFails>4) {
numFails=0;
connected = false;
target.disconnect();
}
} catch (Exception ex) {
ex.printStackTrace(Client.theConsole);
}
return null;
}
public static User[] getUserList(Window target, int channel) {
if (!connected) {
return null;
}
try {
Socket s = new Socket("chat.hiveworkshop.com",80);
java.io.PrintWriter pw = new java.io.PrintWriter(s.getOutputStream());
pw.print("GET /chat_ajax.php?do=listpeople&channel="+channel+" HTTP/1.1\r\n");
pw.print("Host: chat.hiveworkshop.com\r\n");
pw.print("Cookie: "+PHPSESSID+"\r\n");
pw.print("Referer: http://chat.hiveworkshop.com/\r\n");
pw.print("\r\n");
pw.flush();
java.io.InputStreamReader isr = new java.io.InputStreamReader(s.getInputStream());
while (!isr.ready()) {
Thread.sleep(10);
}
java.io.BufferedReader br = new java.io.BufferedReader(isr);
for (int i=0;i<10;i++) {
br.readLine();
}
int len = 1;
try {
len = Integer.parseInt(br.readLine(),16);
} catch (Exception ex) {
ex.printStackTrace();
}
char[] chars = new char[len];
for (int i=0;i<chars.length;i++) {
chars[i] = (char)br.read();
}
String line = new String(chars);
if (line==null || line.equals(lastList[channel-1])) {
return null;
} else {
lastList[channel-1] = line;
}
line = new String(line.getBytes(),"UTF-8");
String[] users = line.split("<user>");
User[] userList = new User[users.length-1];
for (int i=1;i<users.length;i++) {
String username = users[i].split("<username>")[1].split("</username>")[0];
String uid = users[i].split("<userid>")[1].split("</userid>")[0];
String at = users[i].split("<status>")[1].split("</status>")[0];
String ucolor = users[i].split("<color>")[1].split("</color>")[0];
String rank = users[i].split("<rank>")[1].split("</rank>")[0];
username = username.substring(9,username.length()-3);
uid = uid.substring(9,uid.length()-3);
at = at.substring(9,at.length()-3);
ucolor = ucolor.substring(9,ucolor.length()-3);
rank = rank.substring(9,rank.length()-7);
if (ucolor.equals(" ") | ucolor.isEmpty()) {
ucolor = "#B8A26E";
}
System.out.println(rank+"::"+username+"::"+ucolor+"::"+at);
userList[i-1] = new User(target,rank,username,ucolor,at);
}
s.close();
br.close();
isr.close();
pw.close();
return userList;
} catch (IOException ex) {
ex.printStackTrace(Client.theConsole);
target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Failed to receive members list"));
} catch (Exception ex) {
ex.printStackTrace();//Client.theConsole);
}
return null;
}
public static void sendMessage(String message, Window target, int channel) {
if (!connected) {
target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Not connected to server"));
return;
}
try {
Socket s = new Socket("chat.hiveworkshop.com",80);
message = java.net.URLEncoder.encode(message,"UTF-8");
java.io.PrintWriter pw = new java.io.PrintWriter(s.getOutputStream());
pw.print("GET /chat_ajax.php?do=write&channel="+channel+"&message="+message+" HTTP/1.1\r\n");
pw.print("Host: chat.hiveworkshop.com\r\n");
pw.print("Cookie: "+PHPSESSID+"\r\n");
pw.print("Referer: http://chat.hiveworkshop.com/\r\n");
pw.print("\r\n");
pw.flush();
s.close();
} catch (IOException ex) {
ex.printStackTrace(Client.theConsole);
target.writeToAll(new Message(target,Message.TYPE_SYSTEM,"Failed to send message"));
} catch (Exception ex) {
ex.printStackTrace(Client.theConsole);
}
}
}