/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.19
 * Generated at: 2012-12-23 21:10:24 UTC
 * Note: The last modified time of this file was set to
 *       the last modified time of the source file after
 *       generation to assist with modification tracking.
 */
package org.apache.jsp.addons.Live_0020Chat;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.io.*;
import java.util.*;
import protocol.*;
import org.apache.axis.encoding.Base64;
import java.io.*;

public final class chat_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;

  static {
    _jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1);
    _jspx_dependants.put("/addons/Live Chat/../../WEB-INF/.pwadminconf.jsp", Long.valueOf(1356261856000L));
  }

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\n");
      out.write("\n");
      out.write("\n");
      out.write("\n");
      out.write('\n');
      out.write('\n');

//-------------------------------------------------------------------------------------------------------------------------
//------------------------------- SETTINGS --------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------

	// pwadmin access password as md5 encrypted string
	// default password is root -> 63a9f0ea7bb98050796b649e85481845
	String iweb_password = "f67e209e9b8164dcb6812b9ac4357ed1";

	// connection settings to the mysql pw database
	String db_host = "localhost";
	String db_port = "3306";
	String db_user = "root";
	String db_password = "1011";
	String db_database = "pw";

	// Type of your items database required for mapping id's to names
	// Options are my or pwi
	String item_labels = "pwi";

	// Absolute path to your PW-Server main directory (startscript, stopscript, /gamed)
	// requires a tailing slash
	String pw_server_path = "/root/";

	// If you have hundreds of characters or heavy web acces through this site
	// It is recommend to turn the realtime character list feature off (false)
	// to prevent server from overload injected by character list generation
	boolean enable_character_list = true;

	String pw_server_name = "Your PW Servername";
	String pw_server_description = "+ Your server description here<br>+ 2 x 2.4 GHz, 2048 MB RAM, 100 MBit LAN<br>+ all dungeons open<br>+ tideborn nirvana weapons<br>+ World bosses";

//-------------------------------------------------------------------------------------------------------------------------
//----------------------------- END SETTINGS ------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------

	String pw_server_exp = "?";
	String pw_server_sp = "?";
	String pw_server_drop = "?";
	String pw_server_coins = "?";

	BufferedReader bfr = new BufferedReader(new FileReader(pw_server_path + "gamed/ptemplate.conf"));

	String row;
	while((row = bfr.readLine()) != null)
	{
		row = row.replaceAll("\\s", "");
		if(row.indexOf("exp_bonus=") != -1)
		{
			int pos = row.length();
			if(row.indexOf("#") != -1)
			{
				pos = row.indexOf("#");
			}
			pw_server_exp = row.substring(10, pos);
		}
		if(row.indexOf("sp_bonus=") != -1)
		{
			int pos = row.length();
			if(row.indexOf("#") != -1)
			{
				pos = row.indexOf("#");
			}
			pw_server_sp = row.substring(9, pos);
		}
		if(row.indexOf("drop_bonus=") != -1)
		{
			int pos = row.length();
			if(row.indexOf("#") != -1)
			{
				pos = row.indexOf("#");
			}
			pw_server_drop = row.substring(11, pos);
		}
		if(row.indexOf("money_bonus=") != -1)
		{
			int pos = row.length();
			if(row.indexOf("#") != -1)
			{
				pos = row.indexOf("#");
			}
			pw_server_coins = row.substring(12, pos);
		}
	}

	bfr.close();

	if(request.getSession().getAttribute("items") == null)
	{
		String[] items = new String[30001];

		try
		{
			bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(request.getRealPath("/include/items") + "/default.dat")), "UTF8"));
			if(item_labels.compareTo("my") == 0)
			{
				bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(request.getRealPath("/include/items") + "/my.dat")), "UTF8"));
			}
			if(item_labels.compareTo("pwi") == 0)
			{
				bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(request.getRealPath("/include/items") + "/pwi.dat")), "UTF8"));
			}
			int count = 0;
			while((row = bfr.readLine()) != null && count < 30001)
			{
				items[count] = row;
				count++;
			}
			bfr.close();
		}
		catch(Exception e)
		{
		}

		request.getSession().setAttribute("items", items);
	}

      out.write('\n');
      out.write('\n');
      out.write('\n');

	//------------------------------------
	//----------- SETTINGS ---------------
	//------------------------------------

	// number of last lines that are shown from the chat log file
	int show_lines = 20;
	// shows chat messages in the corresponding color
	boolean show_colors = true;
	// show a default smiley picture instead of the smiley code
	boolean show_smiley_picture = true;
	// set visibility filters
	boolean show_whisper = true;
	boolean show_faction = true;
	boolean show_rooms = true;
	boolean show_common = true;
	boolean show_world = true;
	boolean show_squad = true;
	boolean show_trade = true;

	//------------------------------------
	//--------- END SETTINGS -------------
	//------------------------------------

      out.write('\n');
      out.write('\n');

	boolean allowed = false;

	if(request.getSession().getAttribute("ssid") != null)
	{
		allowed = true;
	}

      out.write("\n");
      out.write("\n");
      out.write("<html>\n");
      out.write("\n");
      out.write("<head>\n");
      out.write("\t<meta http-equiv=\"refresh\" content=\"5\">\n");
      out.write("\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\">\n");
      out.write("\t<link rel=\"shortcut icon\" href=\"../../include/fav.ico\">\n");
      out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"../../include/style.css\">\n");
      out.write("</head>\n");
      out.write("\n");
      out.write("<body background=\"#aaaaaa\" style=\"margin: 0px;\">\n");
      out.write("\n");
      out.write("<table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\" border=\"0\">\n");
      out.write("\n");

if(allowed)
{
	Vector<String> lines = new Vector<String>();
	String line;
	String time;
	String sender;
	String recipient;
	String message;
	String debug;
	String color;

	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(pw_server_path + "logs/world2.chat")));

	while((line = br.readLine()) != null)
	{
		lines.add(line);
	}

	br.close();

	int i = lines.size()-1;
	while(i>lines.size()-show_lines && i>=0)
	{
		line = lines.get(i);

		color = "<font color=\"#aaaaaa\">";
		time = line.substring(0, 19);
		line = line.substring(line.indexOf(": chat :")+9);
		sender = "*****";
		recipient = "*****";
		message = new String("*****");
		byte[] temp = Base64.decode(line.substring(line.indexOf("msg=")+4));

		// Whisper
		if(show_whisper && line.startsWith("Whisper"))
		{
			message = new String(temp, "UTF-16LE");
			sender = line.substring(line.indexOf("src=")+4);
			sender = "Player(" + sender.substring(0, sender.indexOf(" ")) + ")";
			recipient = line.substring(line.indexOf("dst=")+4);
			recipient = "Player(" + recipient.substring(0, recipient.indexOf(" ")) + ")";
			color = "<font color=\"#ff00dd\">";
		}
		// Faction
		if(show_faction && line.startsWith("Guild"))
		{
			message = new String(temp, "UTF-16LE");
			sender = line.substring(line.indexOf("src=")+4);
			sender = "Player(" + sender.substring(0, sender.indexOf(" ")) + ")";
			recipient = line.substring(line.indexOf("fid=")+4);
			recipient = "Faction(" + recipient.substring(0, recipient.indexOf(" ")) + ")";
			color = "<font color=\"#00fffc\">";
		}
		// Chatroom
		if(show_rooms && line.startsWith("Group"))
		{
			message = new String(temp, "UTF-16LE");
			sender = line.substring(line.indexOf("src=")+4);
			sender = "Player(" + sender.substring(0, sender.indexOf(" ")) + ")";
			recipient = line.substring(line.indexOf("room=")+5);
			recipient = "Room(" + recipient.substring(0, recipient.indexOf(" ")) + ")";
			color = "<font color=\"#000000\">";
		}
		if(line.startsWith("Chat"))
		{
			// Common
			if(show_common && line.indexOf("chl=0") != -1)
			{
				message = new String(temp, "UTF-16LE");
				sender = line.substring(line.indexOf("src=")+4);
				sender = "Player(" + sender.substring(0, sender.indexOf(" ")) + ")";
				recipient = "Common";
				color = "<font color=\"#000000\">";
			}
			// World
			if(show_world && line.indexOf("chl=1") != -1)
			{
				message = new String(temp, "UTF-16LE");
				sender = line.substring(line.indexOf("src=")+4);
				sender = "Player(" + sender.substring(0, sender.indexOf(" ")) + ")";
				recipient = "World";
				color = "<font color=\"#ffee00\">";
			}
			// Squad
			if(show_squad && line.indexOf("chl=2") != -1)
			{
				message = new String(temp, "UTF-16LE");
				sender = line.substring(line.indexOf("src=")+4);
				sender = "Player(" + sender.substring(0, sender.indexOf(" ")) + ")";
				recipient = "Squad";
				color = "<font color=\"#00ff00\">";
			}
			// Trade
			if(show_trade && line.indexOf("chl=7") != -1)
			{
				message = new String(temp, "UTF-16LE");
				sender = line.substring(line.indexOf("src=")+4);
				sender = "Player(" + sender.substring(0, sender.indexOf(" ")) + ")";
				recipient = "Trade";
				color = "<font color=\"#ff8800\">";
			}
			// Broadcast
			if(line.indexOf("chl=9") != -1)
			{
				message = new String(temp, "UTF-16LE");
				sender = line.substring(line.indexOf("src=")+4);
				sender = "Player(" + sender.substring(0, sender.indexOf(" ")) + ")";
				recipient = "System";
				color = "<font color=\"#ff2200\">";
			}
		}
/*
		// Smileys causing errors... removing entries == 0 from byte array
		for(int n=0; n<temp.length; n++)
		{
			if(temp[n] != 0)
			{
				message += (char)(temp[n]);
			}
		}
*/
		if(show_smiley_picture)
		{
			message = message.replaceAll(".<\\d+>...<\\d+.\\d+>", "<img src=\"smiley.gif\" align=\"absmiddle\"></img>");
		}

		if(!show_colors)
		{
			color = "<font color=\"#000000\">";
		}

		out.print("<tr>");
		out.print("<td align=\"center\" style=\"border-bottom: 1px solid #cccccc;\"> time: </td>");
		out.print("<td style=\"border-bottom: 1px solid #cccccc;\"><b>" + time + "</b></td>");
		out.print("<td align=\"center\" style=\"border-bottom: 1px solid #cccccc;\"> from: </td>");
		out.print("<td style=\"border-bottom: 1px solid #cccccc;\"><b>" + color + sender + "</font></b></td>");
		out.print("<td align=\"center\" style=\"border-bottom: 1px solid #cccccc;\"> to: </td>");
		out.print("<td style=\"border-bottom: 1px solid #cccccc;\"><b>" + color + recipient + "</font></b></td>");
		out.print("<td align=\"center\" style=\"border-bottom: 1px solid #cccccc;\"> msg: </td>");
		out.print("<td style=\"border-bottom: 1px solid #cccccc;\"><b>" + color + message + "</font></b></td>");
		out.println("</tr>");

		i--;
	}
}

      out.write("\n");
      out.write("</table>\n");
      out.write("\n");
      out.write("</body>\n");
      out.write("\n");
      out.write("</html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
