package main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.swing.JTextField;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.xml.sax.SAXException;
// Mod�le MVC: cette classe serait le controleur
public class Main {
private static String modsFilePath = new String("C:\\Program Files (x86)\\Allods Online\\Personal\\Logs\\mods.txt");
private static String xmlFilePath = new String("database.xml");
// private static String flagFilePath = new String("flag.txt");
private static final String configFilePath = new String("config.txt");
private static String ftpUrl = new String("ftp.domain.com");
private static String ftpLogin = new String("admin");
private static String ftpPass = new String("password");
private JTextField Error = null;
public Boolean debugMode = new Boolean(true);
public Boolean SavedFlag = false;
public Main() {
}
public void setErrorField(JTextField error) {
Error = error;
}
// error checking
public boolean loadConfigIsOk() {
Scanner scanner = null;
try {
FileInputStream ffile = new FileInputStream(configFilePath);
scanner = new Scanner(ffile);
while (scanner.hasNextLine()) {
String strLine = scanner.nextLine();
Scanner scanner2 = new Scanner(strLine);
scanner2.useDelimiter("=");
if (scanner2.hasNext()) {
String token = scanner2.next();
if (token.equals("modsFilePath") && scanner2.hasNext())
modsFilePath = new String(scanner2.next());
if (token.equals("ftpUrl") && scanner2.hasNext())
ftpUrl = new String(scanner2.next());
if (token.equals("ftpLogin") && scanner2.hasNext())
ftpLogin = new String(scanner2.next());
if (token.equals("ftpPass") && scanner2.hasNext())
ftpPass = new String(scanner2.next());
if (token.equals("debugMode") && scanner2.hasNextBoolean())
debugMode = scanner2.nextBoolean();
}
scanner2.close();
}
scanner.close();
// Testons si le fichier est accesible.
FileInputStream testFile = new FileInputStream(modsFilePath);
if (testFile != null)
testFile.close();
} catch (Exception e) {
if (scanner != null)
scanner.close();
e.printStackTrace();
return false;
}
return true;
}
public boolean getEntryToken() {
boolean res = false;
FileOutputStream fos = null;
FileInputStream fis = null;
FTPClient client = null;
try {
client = new FTPClient();
client.connect(ftpUrl);
if (!client.login(ftpLogin, ftpPass))
return false;
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
return false;
}
client.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
client.changeWorkingDirectory("Flibus");
fos = new FileOutputStream("entryToken.txt");
if (!client.retrieveFile("entryToken.txt", fos))
return false;
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
return false;
}
fos.close();
fis = new FileInputStream("entryToken.txt");
Scanner sc = new Scanner(fis);
if (sc.hasNextBoolean())
res = sc.nextBoolean();
sc.close();
client.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
client.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
return res;
}
public boolean setEntryToken(boolean val) {
boolean res = false;
FileOutputStream out = null;
FileInputStream in = null;
FTPClient client = new FTPClient();
try {
client.connect(ftpUrl);
if (!client.login(ftpLogin, ftpPass))
return false;
if (!FTPReply.isPositiveCompletion(client.getReplyCode()))
return false;
client.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
client.changeWorkingDirectory("Flibus");
out = new FileOutputStream("entryToken.txt");
out.write((new String(new Boolean(val).toString())).getBytes());
out.close();
in = new FileInputStream("entryToken.txt");
client.storeFile("entryToken.txt", in);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
return false;
}
res = true;
client.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
try
{
client.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
if (in != null) {
try {
in.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
return res;
}
public boolean uploadToFtp() {
boolean res = false;
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect(ftpUrl);
if (debugMode)
System.out.print(client.getReplyString());
client.login(ftpLogin, ftpPass);
if (debugMode)
System.out.print(client.getReplyString());
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
return false;
}
if (debugMode)
System.out.println("Le dossier courant est: " + client.printWorkingDirectory());
if (!client.changeWorkingDirectory("Flibus"))
return false;
if (debugMode)
System.out.print(client.getReplyString());
// client.enterLocalPassiveMode();
client.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
String filename = xmlFilePath;
fis = new FileInputStream(filename);
client.storeFile(filename, fis);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
return false;
}
if (debugMode)
System.out.print(client.getReplyString());
fis.close();
client.logout();
if (debugMode)
System.out.print(client.getReplyString());
res = true;
} catch (java.io.IOException e) {
res = false;
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
if (debugMode)
System.out.print(client.getReplyString());
} catch (java.io.IOException e) {
res = false;
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
}
}
if (res)
Error.setText(Error.getText().concat("\nUpload Successful!\n"));
else
Error.setText(Error.getText().concat("\n" + "Il y a eu une erreure:(\n"));
return res;
}
public boolean retrieveFromFTP() {
boolean res = false;
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect(ftpUrl);
client.login(ftpLogin, ftpPass);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
return false;
}
client.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
client.changeWorkingDirectory("Flibus");
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
return false;
}
String filename = xmlFilePath;
fos = new FileOutputStream(filename);
client.retrieveFile(filename, fos);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
return false;
}
res = true;
client.logout();
} catch (java.io.IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
client.disconnect();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
if (res)
Error.setText(Error.getText().concat("\nDownload from FTP successful!\n"));
else
Error.setText(Error.getText().concat("\nIl y a eu une erreure:(\n"));
return res;
}
public void clearModsFile() {
try {
FileOutputStream fichier = new FileOutputStream(modsFilePath);
fichier.write((new String()).getBytes());
fichier.close();
} catch (Exception e) {
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
}
}
public SortedSet<Person> readModsFileRaidPresence() {
Scanner scanner = null;
// BufferedWriter bw = null;
// File oldFile = null;
SortedSet<Person> res = new TreeSet<Person>();
try {
FileInputStream ffile = new FileInputStream(modsFilePath);
scanner = new Scanner(ffile);
// File tmp = File.createTempFile("allods_mod_file.txt", "");
// bw = new BufferedWriter(new FileWriter(tmp));
while (scanner.hasNextLine()) {
String strLine = scanner.nextLine();
if (strLine.contains("RaidPresence:")) {
Scanner scanner2 = new Scanner(strLine);
scanner2.useDelimiter(" ");
String nom = "";
while (scanner2.hasNext()) {
String token = scanner2.next();
if (token.equals("NOM")) {
if (scanner2.hasNext()) {
nom = scanner2.next();
if (!nom.equals("")) {
if (debugMode)
System.out.println("adding: " + nom);
res.add(new Person(nom, 1, null));
}
}
}
}
scanner2.close();
// re.addRaidPresence(new Person(nom, 1, null));
}
// else{
// bw.write(String.format("%s%n", strLine));
// }
}
scanner.close();
// bw.close();
// oldFile = new File(modsFilePath);
// if (oldFile.delete()){
// tmp.renameTo(oldFile);
// }
} catch (Exception e) {
if (scanner != null) {
scanner.close();
}
// if (bw!=null){
// try{
// bw.close();
// }
// catch (IOException ee)
// {
// if (Error != null){
// Error.setText(ee.getMessage());
// }
// ee.printStackTrace();
// }
// }
if (Error != null) {
Error.setText(Error.getText().concat(e.getMessage()));
}
e.printStackTrace();
}
return res;
}
/*
* Mods file mini Parser, lalr(2), no error check.
*/
public Persons readModsFile() {
Scanner scanner = null;
Persons res = new Persons();
try {
FileInputStream ffile = new FileInputStream(modsFilePath);
scanner = new Scanner(ffile);
while (scanner.hasNextLine()) {
String strLine = scanner.nextLine();
if (strLine.contains("TaxRecordAddon:")) {
Scanner scanner2 = new Scanner(strLine);
scanner2.useDelimiter(" ");
String nom = "";
Long val = null;
java.util.Date date = null;
while (scanner2.hasNext()) {
String token = scanner2.next();
if (token.equals("NOM")) {
if (scanner2.hasNext()) {
nom = scanner2.next();
}
} else if (token.equals("VAL")) {
if (scanner2.hasNextLong()) {
val = scanner2.nextLong();
}
} else if (token.equals("DATE")) {
if (scanner2.hasNext()) {
date = new java.util.Date(scanner2.next()); // a
// revoir
}
}
}
scanner2.close();
if (!nom.equals("") && val != null && date == null)
res.addPerson(new Person(nom, val));
else if (!nom.equals("") && val != null && date != null)
res.addPerson(new Person(nom, val, date));
}
}
scanner.close();
} catch (Exception e) {
if (scanner != null) {
scanner.close();
}
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
}
return res;
}
// Merges des 2 bases! <==> SQL update?
// Faut differentier le cas Merge impot avec fichier mods du merge de 2
// bases
// cette methode est utilis�e que pour rajouter les impots du mods.txt
public void saveDataBase(Persons ps) {
Persons my_set = loadDataBase();
// Merge de l'impot avec le fichier mods a priori, mais pas forcement!
// verifier le comportement en cas de merge de 2 bases.
for (Person p : ps.getPersonsVec()) {
my_set.addPerson(p);
}
// //merge les pr�sence raids.
// for (Person p : ps.getRaidPresenceSet()){
// my_set.mergeRaidPresence(p);
// }
try {
my_set.save_xml(xmlFilePath);
clearModsFile();
} catch (IOException e) {
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
} catch (XMLStreamException e) {
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
}
}
public void saveDataBaseLight(Persons ps) {
try {
ps.save_xml(xmlFilePath);
} catch (IOException e) {
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
} catch (XMLStreamException e) {
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
}
}
public Persons loadDataBase() {
Persons res = new Persons();
try {
res.load_xml(xmlFilePath, Error);
if (debugMode) {
System.out.println("Read from database: ");
for (Person p : res.getPersonsSet()) {
System.out.println("PersonSet: " + p);
}
for (Person p : res.getPersonsVec()) {
System.out.println("PersonVec: " + p);
}
for (Loot p : res.getWishList()) {
System.out.println("WishList: " + p);
}
for (Loot p : res.getDropedLoot()) {
System.out.println("DropedLoot: " + p);
}
System.out.println("End of Read from database: ");
}
} catch (SAXException e) {
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
} catch (IOException e) {
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
}
return res;
}
public boolean localBackup() {
boolean res = false;
FileInputStream from = null;
FileOutputStream to = null;
try {
from = new FileInputStream(xmlFilePath);
Date d = new Date();
String ddd = d.toString();
ddd = ddd.replace(" ", "_");
ddd = ddd.replace(":", "-");
String fpXmlBackup = "database_" + ddd + ".xml";
File f = new File("LocalDataBackups");
File ff = new File(f, fpXmlBackup);
to = new FileOutputStream(ff);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead);
res = true;
} catch (Exception e) {
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
res = false;
} finally {
if (from != null)
try {
from.close();
} catch (IOException e) {
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
res = false;
}
if (to != null)
try {
to.close();
} catch (IOException e) {
Error.setText(Error.getText().concat(e.getMessage()));
e.printStackTrace();
res = false;
}
}
return res;
}
}