package net.AllGamer.AGBS;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLEncoder;
//import java.util.List;

//import org.bukkit.util.config.ConfigurationNode;

public class subscription extends Thread
{
	String key = AGBS.getAPIKEY();
	String[] x = AGBS.config.getString("subscriptions").split(",");
	String temp = AGBS.makesubs(x, 0);
	String subs = stripcrap(temp);

	public static String stripcrap(String s) 
	{
		String good = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		String result = "";
		for ( int i = 0; i < s.length(); i++ ) 
		{
			if ( good.indexOf(s.charAt(i)) >= 0 )
				result += s.charAt(i);
		}
		return result;
	}

	public void run()
	{
		while (true)
		{
			try
			{
				String data = URLEncoder.encode("subscribe", "UTF-8") + "=" + URLEncoder.encode(subs, "UTF-8");
				data += "&" + URLEncoder.encode("apikey", "UTF-8") + "=" + URLEncoder.encode(key, "UTF-8");

				// Send data
				URL url = new URL("http://209.236.124.35/api/fetch_subscriptions.json");
				java.net.URLConnection conn = url.openConnection();
				conn.setDoOutput(true);
				OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
				wr.write(data);
				wr.flush();

				// Get the response
				BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
				String line;
				while ((line = rd.readLine()) != null) 
				{
					if (line.contains("ok")) 
					{

					}
					// we need to figure out how we will handle this asap...
				}
				wr.close();
				rd.close();
				Thread.sleep(900000);
			}
			catch (Exception e) 
			{
				AGBS.log.severe(AGBS.logPrefix + " An error has occured while obtaining the subscriptions");
				AGBS.log.severe(AGBS.logPrefix + " " + e);
				try 
				{
					Thread.sleep(900000);
				} 
				catch (InterruptedException e1) 
				{
				}
			}
		}
	}
}
@@ -388,16 +388,279 @@
 t(%22,%22);%0A
+%09%7B%0A%09%09if ((Object)x == null)%0A%09%09%7B%0A%09%09%09AGBS.log.severe(AGBS.logPrefix + %22 An error has occured while obtaining the subscriptions%22);%0A%09%09%09AGBS.log.severe(AGBS.logPrefix + %22 You haven't defined any subscriptions! Please edit the log file.%22);%0A%09%09%09this.interrupt();%0A%09%09%7D%0A%09%7D%0A%0A
 %09String 
@@ -719,16 +719,17 @@
 (temp);%0A
+%09
 %0A%09public
@@ -1727,24 +1727,25 @@
  null) %0A%09%09%09%09
+%09
 %7B%0A%09%09%09%09%09if (l
@@ -1776,24 +1776,25 @@
 %09%09%7B%0A%0A%09%09%09%09%09%7D%0A
+%09
 %09%09%09%09%09// we n

fix if subscriptions are null