import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
//import java.util.Random;

import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
//import org.jivesoftware.smack.packet.Message.Body;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*******************************************************************************************************************/
class html2bb
{
	public static final Logger LOG=Logger.getLogger(html2bb.class);
	/**
	 * @param html
	 * @return
	 */
	public static String parse(String html)
	{
		//LOG.info("//Parsing START");
		try 
		{
			Document doc = Jsoup.parse(html);
			doc.select("label").remove();
			doc.select("input").remove();
			doc.select("script").remove();
			doc.select("textarea").remove();

			doc.select("*").removeAttr("alt");
			doc.select("*").removeAttr("title");
			doc.select("*").removeAttr("style");
			doc.select("*").removeAttr("id");
			doc.select("*").removeAttr("class");
			doc.select("*").removeAttr("width");
			doc.select("*").removeAttr("height");
			doc.select("*").removeAttr("target");
		
			html = doc.body().html();
			
		}catch(Exception e){LOG.error("ERROR Jsoup"+e);}
		//for(Element elm :doc.getAllElements()){if(elm.tagName()=="embed"){String src = elm.attr("src");}}
		html = html.replaceAll("&nbsp;|&#160;"," ");
		html = html.replaceAll("&iexcl;|&#161;","¡");
		html = html.replaceAll("&cent;|&#162;","¢");
		html = html.replaceAll("&pound;|&#163;","£");
		html = html.replaceAll("&curren;|&#164;","¤");
		html = html.replaceAll("&yen;|&#165;","¥");
		html = html.replaceAll("&brvbar;|&#166;","¦");
		html = html.replaceAll("&sect;|&#167;","§");
		html = html.replaceAll("&uml;|&#168;","¨");
		html = html.replaceAll("&copy;|&#169;","©");
		html = html.replaceAll("&ordf;|&#170;","ª");
		html = html.replaceAll("&laquo;|&#171;","«");
		html = html.replaceAll("&raquo;|&#187;","»");
		html = html.replaceAll("&not;|&#172;","¬");
		html = html.replaceAll("&reg;|&#174;","®");
		html = html.replaceAll("&macr;|&#175;","¯");
		html = html.replaceAll("&deg;|&#176;","°");
		html = html.replaceAll("&plusmn;|&#177;","±");
		html = html.replaceAll("&sup2;|&#178;","²");
		html = html.replaceAll("&sup3;|&#179;","³");
		html = html.replaceAll("&acute;|&#180;","´");
		html = html.replaceAll("&micro;|&#181;","µ");
		html = html.replaceAll("&para;|&#182;","¶");
		html = html.replaceAll("&middot;|&#183;","·");
		html = html.replaceAll("&cedil;|&#184;","¸");
		html = html.replaceAll("&sup1;|&#185;","¹	");
		html = html.replaceAll("&ordm;|&#186;","º");
		html = html.replaceAll("&frac14;|&#188;","¼");
		html = html.replaceAll("&frac12;|&#189;","½");
		html = html.replaceAll("&#8531;","⅓");
		html = html.replaceAll("&frac34;|&#190;","¾");
		html = html.replaceAll("&iquest;|&#191;","¿");
		html = html.replaceAll("&Agrave;|&#192;","À");
		html = html.replaceAll("&Aacute;|&#193;","Á");
		html = html.replaceAll("&Acirc;|&#194;","Â");
		html = html.replaceAll("&Atilde;|&#195;","Ã");
		html = html.replaceAll("&Auml;|&#196;","Ä");
		html = html.replaceAll("&Aring;|&#197;","Å");
		html = html.replaceAll("&AElig;|&#198;","Æ");
		html = html.replaceAll("&Ccedil;|&#199;","Ç");
		html = html.replaceAll("&Egrave;|&#200;","È");
		html = html.replaceAll("&Eacute;|&#201;","É");
		html = html.replaceAll("&Ecirc;|&#202;","Ê");
		html = html.replaceAll("&Euml;|&#203;","Ë");
		html = html.replaceAll("&Igrave;|&#204;","Ì");
		html = html.replaceAll("&Iacute;|&#205;","Í");
		html = html.replaceAll("&Icirc;|&#206;","Î");
		html = html.replaceAll("&Iuml;|&#207;","Ï");
		html = html.replaceAll("&ETH;|&#208;","Ð");
		html = html.replaceAll("&Ntilde;|&#209;","Ñ");
		html = html.replaceAll("&Ograve;|&#210;","Ò");
		html = html.replaceAll("&Oacute;|&#211;","Ó");
		html = html.replaceAll("&Ocirc;|&#212;","Ô");
		html = html.replaceAll("&Otilde;|&#213;","Õ");
		html = html.replaceAll("&Ouml;|&#214;","Ö");
		html = html.replaceAll("&times;|&#215;","×");
		html = html.replaceAll("&Oslash;|&#216;","Ø");
		html = html.replaceAll("&Ugrave;|&#217;","Ù");
		html = html.replaceAll("&Uacute;|&#218;","Ú");
		html = html.replaceAll("&Ucirc;|&#219;","Û");
		html = html.replaceAll("&Uuml;|&#220;","Ü");
		html = html.replaceAll("&Yacute;|&#221;","Ý");
		html = html.replaceAll("&THORN;|&#222;","Þ");
		html = html.replaceAll("&szlig;|&#223;","ß");
		html = html.replaceAll("&agrave;|&#224;","à");
		html = html.replaceAll("&aacute;|&#225;","á");
		html = html.replaceAll("&acirc;|&#226;","â");
		html = html.replaceAll("&atilde;|&#227;","ã");
		html = html.replaceAll("&auml;|&#228;","ä");
		html = html.replaceAll("&aring;|&#229;","å");
		html = html.replaceAll("&aelig;|&#230;","æ");
		html = html.replaceAll("&ccedil;|&#231;","ç");
		html = html.replaceAll("&egrave;|&#232;","è");
		html = html.replaceAll("&eacute;|&#233;","é");
		html = html.replaceAll("&ecirc;|&#234;","ê");
		html = html.replaceAll("&euml;|&#235;","ë");
		html = html.replaceAll("&igrave;|&#236;","ì");
		html = html.replaceAll("&iacute;|&#237;","í");
		html = html.replaceAll("&icirc;|&#238;","î");
		html = html.replaceAll("&iuml;|&#239;","ï");
		html = html.replaceAll("&eth;|&#240;","ð");
		html = html.replaceAll("&ntilde;|&#241;","ñ");
		html = html.replaceAll("&ograve;|&#242;","ò");
		html = html.replaceAll("&oacute;|&#243;","ó");
		html = html.replaceAll("&ocirc;|&#244;","ô");
		html = html.replaceAll("&otilde;|&#245;","õ");
		html = html.replaceAll("&ouml;|&#246;","ö");
		html = html.replaceAll("&divide;|&#247;","÷");
		html = html.replaceAll("&oslash;|&#248;","ø");
		html = html.replaceAll("&ugrave;|&#249;","ù");
		html = html.replaceAll("&uacute;|&#250;","ú");
		html = html.replaceAll("&ucirc;|&#251;","û");
		html = html.replaceAll("&uuml;|&#252;","ü");
		html = html.replaceAll("&yacute;|&#253;","ý");
		html = html.replaceAll("&thorn;|&#254;","þ");
		html = html.replaceAll("&yuml;|&#255;","ÿ");
		html = html.replaceAll("&fnof;|&#402;","ƒ");
		html = html.replaceAll("&Alpha;|&#913;","Α");
		html = html.replaceAll("&Beta;|&#914;","Β");
		html = html.replaceAll("&Gamma;|&#915;","Γ");
		html = html.replaceAll("&Delta;|&#916;","Δ");
		html = html.replaceAll("&Epsilon;|&#917;","Ε");
		html = html.replaceAll("&Zeta;|&#918;","Ζ");
		html = html.replaceAll("&Eta;|&#919;","Η");
		html = html.replaceAll("&Theta;|&#920;","Θ");
		html = html.replaceAll("&Iota;|&#921;","Ι");
		html = html.replaceAll("&Kappa;|&#922;","Κ");
		html = html.replaceAll("&Lambda;|&#923;","Λ");
		html = html.replaceAll("&Mu;|&#924;","Μ");
		html = html.replaceAll("&Nu;|&#925;","Ν");
		html = html.replaceAll("&Xi;|&#926;","Ξ");
		html = html.replaceAll("&Omicron;|&#927;","Ο");
		html = html.replaceAll("&Pi;|&#928;","Π");
		html = html.replaceAll("&Rho;|&#929;","Ρ");
		html = html.replaceAll("&Sigma;|&#931;","Σ");
		html = html.replaceAll("&Tau;|&#932;","Τ");
		html = html.replaceAll("&Upsilon;|&#933;","Υ");
		html = html.replaceAll("&Phi;|&#934;","Φ");
		html = html.replaceAll("&Chi;|&#935;","Χ");
		html = html.replaceAll("&Psi;|&#936;","Ψ");
		html = html.replaceAll("&Omega;|&#937;","Ω");
		html = html.replaceAll("&alpha;|&#945;","α");
		html = html.replaceAll("&beta;|&#946;","β");
		html = html.replaceAll("&gamma;|&#947;","γ");
		html = html.replaceAll("&delta;|&#948;","δ");
		html = html.replaceAll("&epsilon;|&#949;","ε");
		html = html.replaceAll("&zeta;|&#950;","ζ");
		html = html.replaceAll("&eta;|&#951;","η");
		html = html.replaceAll("&theta;|&#952;","θ");
		html = html.replaceAll("&iota;|&#953;","ι");
		html = html.replaceAll("&kappa;|&#954;","κ");
		html = html.replaceAll("&lambda;|&#955;","λ");
		html = html.replaceAll("&mu;|&#956;","μ");
		html = html.replaceAll("&nu;|&#957;","ν");
		html = html.replaceAll("&xi;|&#958;","ξ");
		html = html.replaceAll("&omicron;|&#959;","ο");
		html = html.replaceAll("&pi;|&#960;","π");
		html = html.replaceAll("&rho;|&#961;","ρ");
		html = html.replaceAll("&sigmaf;|&#962;","ς");
		html = html.replaceAll("&sigma;|&#963;","σ");
		html = html.replaceAll("&tau;|&#964;","τ");
		html = html.replaceAll("&upsilon;|&#965;","υ");
		html = html.replaceAll("&phi;|&#966;","φ");
		html = html.replaceAll("&chi;|&#967;","χ");
		html = html.replaceAll("&psi;|&#968;","ψ");
		html = html.replaceAll("&omega;|&#969;","ω");
		html = html.replaceAll("&thetasy;|&#977;","ϑ");
		html = html.replaceAll("&upsih;|&#978;","ϒ");
		html = html.replaceAll("&piv;|&#982;","ϖ");
		html = html.replaceAll("&bull;|&#8226;","•");
		html = html.replaceAll("&hellip;|&#8230;","…");
		html = html.replaceAll("&prime;|&#8242;","′");
		html = html.replaceAll("&Prime;|&#8243;","″");
		html = html.replaceAll("&oline;|&#8254;","‾");
		html = html.replaceAll("&frasl;|&#8260;","⁄");
		html = html.replaceAll("&weierp;|&#8472;","℘");
		html = html.replaceAll("&image;|&#8465;","ℑ");
		html = html.replaceAll("&real;|&#8476;","ℜ");
		html = html.replaceAll("&trade;|&#8482;","™");
		html = html.replaceAll("&alefsym;|&#8501;","ℵ");
		html = html.replaceAll("&larr;|&#8592;","←");
		html = html.replaceAll("&uarr;|&#8593;","↑");
		html = html.replaceAll("&rarr;|&#8594;","→");
		html = html.replaceAll("&darr;|&#8595;","↓");
		html = html.replaceAll("&#9668;","◄");
		html = html.replaceAll("&#9650;","▲");
		html = html.replaceAll("&#9658;","►");
		html = html.replaceAll("&#9660;","▼");
		html = html.replaceAll("&harr;|&#8596;","↔");
		html = html.replaceAll("&crarr;|&#8629;","↵");
		html = html.replaceAll("&lArr;|&#8656;","⇐");
		html = html.replaceAll("&uArr;|&#8657;","⇑");
		html = html.replaceAll("&rArr;|&#8658;","⇒");
		html = html.replaceAll("&dArr;|&#8659;","⇓");
		html = html.replaceAll("&hArr;|&#8660;","⇔");
		html = html.replaceAll("&forall;|&#8704;","∀");
		html = html.replaceAll("&part;|&#8706;","∂");
		html = html.replaceAll("&exist;|&#8707;","∃");
		html = html.replaceAll("&empty;|&#8709;","∅");
		html = html.replaceAll("&nabla;|&#8711;","∇");
		html = html.replaceAll("&isin;|&#8712;","∈");
		html = html.replaceAll("&notin;|&#8713;","∉");
		html = html.replaceAll("&ni;|&#8715;","∋");
		html = html.replaceAll("&prod;|&#8719;","∏");
		html = html.replaceAll("&sum;|&#8721;","∑");
		html = html.replaceAll("&minus;|&#8722;","−");
		html = html.replaceAll("&lowast;|&#8727;","∗");
		html = html.replaceAll("&radic;|&#8730;","√");
		html = html.replaceAll("&prop;|&#8733;","∝");
		html = html.replaceAll("&infin;|&#8734;","∞");
		html = html.replaceAll("&ang;|&#8736;","∠");
		html = html.replaceAll("&and;|&#8743;","∧");
		html = html.replaceAll("&or;|&#8744;","∨");
		html = html.replaceAll("&cap;|&#8745;","∩");
		html = html.replaceAll("&cup;|&#8746;","∪");
		html = html.replaceAll("&int;|&#8747;","∫");
		html = html.replaceAll("&there4;|&#8756;","∴");
		html = html.replaceAll("&sim;|&#8764;","∼");
		html = html.replaceAll("&cong;|&#8773;","≅");
		html = html.replaceAll("&asymp;|&#8776;","≈");
		html = html.replaceAll("&ne;|&#8800;","≠");
		html = html.replaceAll("&equiv;|&#8801;","≡");
		html = html.replaceAll("&le;|&#8804;","≤");
		html = html.replaceAll("&ge;|&#8805;","≥");
		html = html.replaceAll("&sub;|&#8834;","⊂");
		html = html.replaceAll("&sup;|&#8835;","⊃");
		html = html.replaceAll("&nsub;|&#8836;","⊄");
		html = html.replaceAll("&sube;|&#8838;","⊆");
		html = html.replaceAll("&supe;|&#8839;","⊇");
		html = html.replaceAll("&oplus;|&#8853;","⊕");
		html = html.replaceAll("&otimes;|&#8855;","⊗");
		html = html.replaceAll("&perp;|&#8869;","⊥");
		html = html.replaceAll("&sdot;|&#8901;","⋅");
		html = html.replaceAll("&loz;|&#9674;","◊");
		html = html.replaceAll("&spades;|&#9824;","♠");
		html = html.replaceAll("&clubs;|&#9827;","♣");
		html = html.replaceAll("&hearts;|&#9829;","♥");
		html = html.replaceAll("&diams;|&#9830;","♦");
		html = html.replaceAll("&apos;|&#39;","'");
		html = html.replaceAll("&quot;|&#34;","\"");
		html = html.replaceAll("&amp;|&#38;","&");
		html = html.replaceAll("&lt;|&#60;","<");
		html = html.replaceAll("&gt;|&#62;",">");
		html = html.replaceAll("&OElig;|&#338;","Œ");
		html = html.replaceAll("&oelig;|&#339;","œ");
		html = html.replaceAll("&Scaron;|&#352;","Š");
		html = html.replaceAll("&scaron;|&#353;","š");
		html = html.replaceAll("&Yuml;|&#376;","Ÿ");
		html = html.replaceAll("&circ;|&#710;","ˆ");
		html = html.replaceAll("&tilde;|&#732;","˜");
		html = html.replaceAll("&#8209;","‑");
		html = html.replaceAll("&ndash;|&#8211;","–");
		html = html.replaceAll("&mdash;|&#8212;","—");
		html = html.replaceAll("&lsquo;|&#8216;","‘");
		html = html.replaceAll("&rsquo;|&#8217;","’");
		html = html.replaceAll("&sbquo;|&#8218;","‚");
		html = html.replaceAll("&ldquo;|&#8220;","“");
		html = html.replaceAll("&rdquo;|&#8221;","”");
		html = html.replaceAll("&bdquo;|&#8222;","„");
		html = html.replaceAll("&dagger;|&#8224;","†");
		html = html.replaceAll("&Dagger;|&#8225;","‡");
		html = html.replaceAll("&permil;|&#8240;","‰");
		html = html.replaceAll("&lsaquo;|&#8249;","‹");
		html = html.replaceAll("&rsaquo;|&#8250;","›");
		html = html.replaceAll("&euro;|&#8364;","€");
		html = html.replaceAll("&#8470;","№");
		html = html.replaceAll("&#769;","́");

		html = html.replaceAll("&(.*?);","");//Удаляем все остальные мнемоники
		//****************************************************************
		html = html.replaceAll("< *hr( .*?)?>","[hr]");
		html = html.replaceAll("< *table( .*?)?>","[table]");
		html = html.replaceAll("< */ *table *>","[/table]");
		html = html.replaceAll("< *tr( .*?)?>","[tr]");
		html = html.replaceAll("< */ *tr *>","[/tr]");
		html = html.replaceAll("< *td( .*?)?>","[td]");
		html = html.replaceAll("< */ *td *>","[/td]");
		html = html.replaceAll("< *sub( .*?)?>","[sub]");
		html = html.replaceAll("< */ *sub *>","[/sub]");
		html = html.replaceAll("< *sup( .*?)?>","[sup]");
		html = html.replaceAll("< */ *sup *>","[/sup]");
		//****************************************************************		
		html = html.replaceAll("< *(b|strong)( .*?)?>","[b]");
		html = html.replaceAll("< */ *(b|strong) *>","[/b]");
		//****************************************************************		
		html = html.replaceAll("< *u( .*?)?>","[u]");
		html = html.replaceAll("< */ *u *>","[/u]");
		//****************************************************************
		html = html.replaceAll("< *(i|em)( .*?)?>","[i]");
		html = html.replaceAll("< */ *(i|em) *>","[/i]");
		//****************************************************************
		html = html.replaceAll("< *s( .*?)?>","[s]");
		html = html.replaceAll("< */ *s *>","[/s]");
		html = html.replaceAll("< *del( .*?)?>","[s]");
		html = html.replaceAll("< */ *del *>","[/s]");
		//****************************************************************
		html = html.replaceAll("< *blockquote( .*?)?>","[quote]");
		html = html.replaceAll("< */ *blockquote *>","[/quote]");
		//****************************************************************
		html = html.replaceAll("< *ul( .*?)?>","[list]");
		html = html.replaceAll("< */ *ul *>","[/list]");
		html = html.replaceAll("< *ol( .*?)?>","[list]");
		html = html.replaceAll("< */ *ol *>","[/list]");
		html = html.replaceAll("< *li( .*?)?>","[*]");
		html = html.replaceAll("< */ *li *>","");
		html = html.replaceAll("< *img( .*?)?src *= *('|\")(.*?)('|\")(.*?)? */? *>","[img]$3[/img]");
		//****************************************************************
		html = html.replaceAll("< *a( .*?)?href *= *('|\")(.*?)('|\")(.*?)? */? *>","[url=$3]");
		html = html.replaceAll("< */ *a *>","[/url]");
		//****************************************************************
		html = html.replaceAll("< *h1( .*?)?>","[size=\"20\"]");
		html = html.replaceAll("< *h2( .*?)?>","[size=\"18\"]");
		html = html.replaceAll("< *h3( .*?)?>","[size=\"16\"]");
		html = html.replaceAll("< *h4( .*?)?>","[size=\"14\"]");
		html = html.replaceAll("< *h5( .*?)?>","[size=\"12\"]");
		html = html.replaceAll("< *h6( .*?)?>","[size=\"10\"]");
		html = html.replaceAll("< */ *h[1-6] *>","[/size]");
		//****************************************************************
		html = html.replaceAll("< *iframe( .*?)?src *= *('|\")(.*?)('|\")(.*?)? */? *>","$3");
		html = html.replaceAll("< */ *iframe *>","");
		html = html.replaceAll("< *embed( .*?)?src *= *('|\")(.*?)('|\")(.*?)? */? *>","$3");
		html = html.replaceAll("< *object( .*?)?>","");
		html = html.replaceAll("< */ *object *>","");
		html = html.replaceAll("< *param( .*?)?>","");
		html = html.replaceAll("< */ *param *>","");
		//****************************************************************
		html = html.replaceAll("< *font( .*?)?>","[font]");
		html = html.replaceAll("< */ *font *>","[/font]");
		//****************************************************************
		html = html.replaceAll("\n( *)?","");
		html = html.replaceAll("< *br */? *>","\n");
		//****************************************************************
		html = html.replaceAll("<!--.*?-->","");
		html = html.replaceAll("<!-- *","");
		html = html.replaceAll(" *-->","");
		//html = html.replaceAll("< *script.*?>.*?< */script *>","");
		//html = html.replaceAll("< *tbody( .*?)?>","");
		//html = html.replaceAll("< */ *tbody *>","");
		//html = html.replaceAll("< *fieldset( .*?)?>","");
		//html = html.replaceAll("< */ *fieldset *>","");
		//html = html.replaceAll("< *span( .*?)?>","");
		//html = html.replaceAll("< */ *span *>","");
		//html = html.replaceAll("< *p( .*?)?>","");
		//html = html.replaceAll("< */ *p *>","");
		//html = html.replaceAll("< *div( .*?)?>","");
		//html = html.replaceAll("< */ *div *>","");
		//html = html.replaceAll("< *pre( .*?)?>","");
		//html = html.replaceAll("< */ *pre *>","");
		//html = html.replaceAll("< *nobr( .*?)?>","");
		//html = html.replaceAll("< */ *nobr *>","");
		//html = html.replaceAll("< *fieldset( .*?)?>","");
		//html = html.replaceAll("< */ *fieldset *>","");
		//html = html.replaceAll("< *noindex( .*?)?>","");
		//html = html.replaceAll("< */ *noindex *>","");
		//HTML5
		//html = html.replaceAll("< *article( .*?)?>","");
		//html = html.replaceAll("< */ *article *>","");
		//html = html.replaceAll("< *aside( .*?)?>","");
		//html = html.replaceAll("< */ *aside *>","");
		html = html.replaceAll("< *cite( .*?)?>","[i]");
		html = html.replaceAll("< */ *cite *>","[/i]");
		html = html.replaceAll("< *dfn( .*?)?>","[i]");
		html = html.replaceAll("< */ *dfn *>","[/i]");
		html = html.replaceAll("< *var( .*?)?>","[i]");
		html = html.replaceAll("< */ *var *>","[/i]");
		//****************************************************************
		html = html.replaceAll("((<code>(.*?)</code>)|(<.*?>))","$2");
		html = html.replaceAll("< *code( .*?)?>","[code]");
		html = html.replaceAll("< */ *code *>","[/code]");
			
		//LOG.info("//Parsing END");
		return html;
	}
	public static String parse_bboff(String html)
	{
		//LOG.info("//Parsing START");
		try 
		{
			Document doc = Jsoup.parse(html);
			doc.select("label").remove();
			doc.select("input").remove();
			doc.select("script").remove();
			doc.select("textarea").remove();

			doc.select("*").removeAttr("alt");
			doc.select("*").removeAttr("title");
			doc.select("*").removeAttr("style");
			doc.select("*").removeAttr("id");
			doc.select("*").removeAttr("class");
			doc.select("*").removeAttr("width");
			doc.select("*").removeAttr("height");
			doc.select("*").removeAttr("target");
		
			html = doc.body().html();
			
		}catch(Exception e){System.err.println(e);}
		
		//for(Element elm :doc.getAllElements()){if(elm.tagName()=="embed"){String src = elm.attr("src");}}
		html = html.replaceAll("&nbsp;|&#160;"," ");
		html = html.replaceAll("&iexcl;|&#161;","¡");
		html = html.replaceAll("&cent;|&#162;","¢");
		html = html.replaceAll("&pound;|&#163;","£");
		html = html.replaceAll("&curren;|&#164;","¤");
		html = html.replaceAll("&yen;|&#165;","¥");
		html = html.replaceAll("&brvbar;|&#166;","¦");
		html = html.replaceAll("&sect;|&#167;","§");
		html = html.replaceAll("&uml;|&#168;","¨");
		html = html.replaceAll("&copy;|&#169;","©");
		html = html.replaceAll("&ordf;|&#170;","ª");
		html = html.replaceAll("&laquo;|&#171;","«");
		html = html.replaceAll("&raquo;|&#187;","»");
		html = html.replaceAll("&not;|&#172;","¬");
		html = html.replaceAll("&reg;|&#174;","®");
		html = html.replaceAll("&macr;|&#175;","¯");
		html = html.replaceAll("&deg;|&#176;","°");
		html = html.replaceAll("&plusmn;|&#177;","±");
		html = html.replaceAll("&sup2;|&#178;","²");
		html = html.replaceAll("&sup3;|&#179;","³");
		html = html.replaceAll("&acute;|&#180;","´");
		html = html.replaceAll("&micro;|&#181;","µ");
		html = html.replaceAll("&para;|&#182;","¶");
		html = html.replaceAll("&middot;|&#183;","·");
		html = html.replaceAll("&cedil;|&#184;","¸");
		html = html.replaceAll("&sup1;|&#185;","¹	");
		html = html.replaceAll("&ordm;|&#186;","º");
		html = html.replaceAll("&frac14;|&#188;","¼");
		html = html.replaceAll("&frac12;|&#189;","½");
		html = html.replaceAll("&#8531;","⅓");
		html = html.replaceAll("&frac34;|&#190;","¾");
		html = html.replaceAll("&iquest;|&#191;","¿");
		html = html.replaceAll("&Agrave;|&#192;","À");
		html = html.replaceAll("&Aacute;|&#193;","Á");
		html = html.replaceAll("&Acirc;|&#194;","Â");
		html = html.replaceAll("&Atilde;|&#195;","Ã");
		html = html.replaceAll("&Auml;|&#196;","Ä");
		html = html.replaceAll("&Aring;|&#197;","Å");
		html = html.replaceAll("&AElig;|&#198;","Æ");
		html = html.replaceAll("&Ccedil;|&#199;","Ç");
		html = html.replaceAll("&Egrave;|&#200;","È");
		html = html.replaceAll("&Eacute;|&#201;","É");
		html = html.replaceAll("&Ecirc;|&#202;","Ê");
		html = html.replaceAll("&Euml;|&#203;","Ë");
		html = html.replaceAll("&Igrave;|&#204;","Ì");
		html = html.replaceAll("&Iacute;|&#205;","Í");
		html = html.replaceAll("&Icirc;|&#206;","Î");
		html = html.replaceAll("&Iuml;|&#207;","Ï");
		html = html.replaceAll("&ETH;|&#208;","Ð");
		html = html.replaceAll("&Ntilde;|&#209;","Ñ");
		html = html.replaceAll("&Ograve;|&#210;","Ò");
		html = html.replaceAll("&Oacute;|&#211;","Ó");
		html = html.replaceAll("&Ocirc;|&#212;","Ô");
		html = html.replaceAll("&Otilde;|&#213;","Õ");
		html = html.replaceAll("&Ouml;|&#214;","Ö");
		html = html.replaceAll("&times;|&#215;","×");
		html = html.replaceAll("&Oslash;|&#216;","Ø");
		html = html.replaceAll("&Ugrave;|&#217;","Ù");
		html = html.replaceAll("&Uacute;|&#218;","Ú");
		html = html.replaceAll("&Ucirc;|&#219;","Û");
		html = html.replaceAll("&Uuml;|&#220;","Ü");
		html = html.replaceAll("&Yacute;|&#221;","Ý");
		html = html.replaceAll("&THORN;|&#222;","Þ");
		html = html.replaceAll("&szlig;|&#223;","ß");
		html = html.replaceAll("&agrave;|&#224;","à");
		html = html.replaceAll("&aacute;|&#225;","á");
		html = html.replaceAll("&acirc;|&#226;","â");
		html = html.replaceAll("&atilde;|&#227;","ã");
		html = html.replaceAll("&auml;|&#228;","ä");
		html = html.replaceAll("&aring;|&#229;","å");
		html = html.replaceAll("&aelig;|&#230;","æ");
		html = html.replaceAll("&ccedil;|&#231;","ç");
		html = html.replaceAll("&egrave;|&#232;","è");
		html = html.replaceAll("&eacute;|&#233;","é");
		html = html.replaceAll("&ecirc;|&#234;","ê");
		html = html.replaceAll("&euml;|&#235;","ë");
		html = html.replaceAll("&igrave;|&#236;","ì");
		html = html.replaceAll("&iacute;|&#237;","í");
		html = html.replaceAll("&icirc;|&#238;","î");
		html = html.replaceAll("&iuml;|&#239;","ï");
		html = html.replaceAll("&eth;|&#240;","ð");
		html = html.replaceAll("&ntilde;|&#241;","ñ");
		html = html.replaceAll("&ograve;|&#242;","ò");
		html = html.replaceAll("&oacute;|&#243;","ó");
		html = html.replaceAll("&ocirc;|&#244;","ô");
		html = html.replaceAll("&otilde;|&#245;","õ");
		html = html.replaceAll("&ouml;|&#246;","ö");
		html = html.replaceAll("&divide;|&#247;","÷");
		html = html.replaceAll("&oslash;|&#248;","ø");
		html = html.replaceAll("&ugrave;|&#249;","ù");
		html = html.replaceAll("&uacute;|&#250;","ú");
		html = html.replaceAll("&ucirc;|&#251;","û");
		html = html.replaceAll("&uuml;|&#252;","ü");
		html = html.replaceAll("&yacute;|&#253;","ý");
		html = html.replaceAll("&thorn;|&#254;","þ");
		html = html.replaceAll("&yuml;|&#255;","ÿ");
		html = html.replaceAll("&fnof;|&#402;","ƒ");
		html = html.replaceAll("&Alpha;|&#913;","Α");
		html = html.replaceAll("&Beta;|&#914;","Β");
		html = html.replaceAll("&Gamma;|&#915;","Γ");
		html = html.replaceAll("&Delta;|&#916;","Δ");
		html = html.replaceAll("&Epsilon;|&#917;","Ε");
		html = html.replaceAll("&Zeta;|&#918;","Ζ");
		html = html.replaceAll("&Eta;|&#919;","Η");
		html = html.replaceAll("&Theta;|&#920;","Θ");
		html = html.replaceAll("&Iota;|&#921;","Ι");
		html = html.replaceAll("&Kappa;|&#922;","Κ");
		html = html.replaceAll("&Lambda;|&#923;","Λ");
		html = html.replaceAll("&Mu;|&#924;","Μ");
		html = html.replaceAll("&Nu;|&#925;","Ν");
		html = html.replaceAll("&Xi;|&#926;","Ξ");
		html = html.replaceAll("&Omicron;|&#927;","Ο");
		html = html.replaceAll("&Pi;|&#928;","Π");
		html = html.replaceAll("&Rho;|&#929;","Ρ");
		html = html.replaceAll("&Sigma;|&#931;","Σ");
		html = html.replaceAll("&Tau;|&#932;","Τ");
		html = html.replaceAll("&Upsilon;|&#933;","Υ");
		html = html.replaceAll("&Phi;|&#934;","Φ");
		html = html.replaceAll("&Chi;|&#935;","Χ");
		html = html.replaceAll("&Psi;|&#936;","Ψ");
		html = html.replaceAll("&Omega;|&#937;","Ω");
		html = html.replaceAll("&alpha;|&#945;","α");
		html = html.replaceAll("&beta;|&#946;","β");
		html = html.replaceAll("&gamma;|&#947;","γ");
		html = html.replaceAll("&delta;|&#948;","δ");
		html = html.replaceAll("&epsilon;|&#949;","ε");
		html = html.replaceAll("&zeta;|&#950;","ζ");
		html = html.replaceAll("&eta;|&#951;","η");
		html = html.replaceAll("&theta;|&#952;","θ");
		html = html.replaceAll("&iota;|&#953;","ι");
		html = html.replaceAll("&kappa;|&#954;","κ");
		html = html.replaceAll("&lambda;|&#955;","λ");
		html = html.replaceAll("&mu;|&#956;","μ");
		html = html.replaceAll("&nu;|&#957;","ν");
		html = html.replaceAll("&xi;|&#958;","ξ");
		html = html.replaceAll("&omicron;|&#959;","ο");
		html = html.replaceAll("&pi;|&#960;","π");
		html = html.replaceAll("&rho;|&#961;","ρ");
		html = html.replaceAll("&sigmaf;|&#962;","ς");
		html = html.replaceAll("&sigma;|&#963;","σ");
		html = html.replaceAll("&tau;|&#964;","τ");
		html = html.replaceAll("&upsilon;|&#965;","υ");
		html = html.replaceAll("&phi;|&#966;","φ");
		html = html.replaceAll("&chi;|&#967;","χ");
		html = html.replaceAll("&psi;|&#968;","ψ");
		html = html.replaceAll("&omega;|&#969;","ω");
		html = html.replaceAll("&thetasy;|&#977;","ϑ");
		html = html.replaceAll("&upsih;|&#978;","ϒ");
		html = html.replaceAll("&piv;|&#982;","ϖ");
		html = html.replaceAll("&bull;|&#8226;","•");
		html = html.replaceAll("&hellip;|&#8230;","…");
		html = html.replaceAll("&prime;|&#8242;","′");
		html = html.replaceAll("&Prime;|&#8243;","″");
		html = html.replaceAll("&oline;|&#8254;","‾");
		html = html.replaceAll("&frasl;|&#8260;","⁄");
		html = html.replaceAll("&weierp;|&#8472;","℘");
		html = html.replaceAll("&image;|&#8465;","ℑ");
		html = html.replaceAll("&real;|&#8476;","ℜ");
		html = html.replaceAll("&trade;|&#8482;","™");
		html = html.replaceAll("&alefsym;|&#8501;","ℵ");
		html = html.replaceAll("&larr;|&#8592;","←");
		html = html.replaceAll("&uarr;|&#8593;","↑");
		html = html.replaceAll("&rarr;|&#8594;","→");
		html = html.replaceAll("&darr;|&#8595;","↓");
		html = html.replaceAll("&#9668;","◄");
		html = html.replaceAll("&#9650;","▲");
		html = html.replaceAll("&#9658;","►");
		html = html.replaceAll("&#9660;","▼");
		html = html.replaceAll("&harr;|&#8596;","↔");
		html = html.replaceAll("&crarr;|&#8629;","↵");
		html = html.replaceAll("&lArr;|&#8656;","⇐");
		html = html.replaceAll("&uArr;|&#8657;","⇑");
		html = html.replaceAll("&rArr;|&#8658;","⇒");
		html = html.replaceAll("&dArr;|&#8659;","⇓");
		html = html.replaceAll("&hArr;|&#8660;","⇔");
		html = html.replaceAll("&forall;|&#8704;","∀");
		html = html.replaceAll("&part;|&#8706;","∂");
		html = html.replaceAll("&exist;|&#8707;","∃");
		html = html.replaceAll("&empty;|&#8709;","∅");
		html = html.replaceAll("&nabla;|&#8711;","∇");
		html = html.replaceAll("&isin;|&#8712;","∈");
		html = html.replaceAll("&notin;|&#8713;","∉");
		html = html.replaceAll("&ni;|&#8715;","∋");
		html = html.replaceAll("&prod;|&#8719;","∏");
		html = html.replaceAll("&sum;|&#8721;","∑");
		html = html.replaceAll("&minus;|&#8722;","−");
		html = html.replaceAll("&lowast;|&#8727;","∗");
		html = html.replaceAll("&radic;|&#8730;","√");
		html = html.replaceAll("&prop;|&#8733;","∝");
		html = html.replaceAll("&infin;|&#8734;","∞");
		html = html.replaceAll("&ang;|&#8736;","∠");
		html = html.replaceAll("&and;|&#8743;","∧");
		html = html.replaceAll("&or;|&#8744;","∨");
		html = html.replaceAll("&cap;|&#8745;","∩");
		html = html.replaceAll("&cup;|&#8746;","∪");
		html = html.replaceAll("&int;|&#8747;","∫");
		html = html.replaceAll("&there4;|&#8756;","∴");
		html = html.replaceAll("&sim;|&#8764;","∼");
		html = html.replaceAll("&cong;|&#8773;","≅");
		html = html.replaceAll("&asymp;|&#8776;","≈");
		html = html.replaceAll("&ne;|&#8800;","≠");
		html = html.replaceAll("&equiv;|&#8801;","≡");
		html = html.replaceAll("&le;|&#8804;","≤");
		html = html.replaceAll("&ge;|&#8805;","≥");
		html = html.replaceAll("&sub;|&#8834;","⊂");
		html = html.replaceAll("&sup;|&#8835;","⊃");
		html = html.replaceAll("&nsub;|&#8836;","⊄");
		html = html.replaceAll("&sube;|&#8838;","⊆");
		html = html.replaceAll("&supe;|&#8839;","⊇");
		html = html.replaceAll("&oplus;|&#8853;","⊕");
		html = html.replaceAll("&otimes;|&#8855;","⊗");
		html = html.replaceAll("&perp;|&#8869;","⊥");
		html = html.replaceAll("&sdot;|&#8901;","⋅");
		html = html.replaceAll("&loz;|&#9674;","◊");
		html = html.replaceAll("&spades;|&#9824;","♠");
		html = html.replaceAll("&clubs;|&#9827;","♣");
		html = html.replaceAll("&hearts;|&#9829;","♥");
		html = html.replaceAll("&diams;|&#9830;","♦");
		html = html.replaceAll("&apos;|&#39;","'");
		html = html.replaceAll("&quot;|&#34;","\"");
		html = html.replaceAll("&amp;|&#38;","&");
		html = html.replaceAll("&lt;|&#60;","<");
		html = html.replaceAll("&gt;|&#62;",">");
		html = html.replaceAll("&OElig;|&#338;","Œ");
		html = html.replaceAll("&oelig;|&#339;","œ");
		html = html.replaceAll("&Scaron;|&#352;","Š");
		html = html.replaceAll("&scaron;|&#353;","š");
		html = html.replaceAll("&Yuml;|&#376;","Ÿ");
		html = html.replaceAll("&circ;|&#710;","ˆ");
		html = html.replaceAll("&tilde;|&#732;","˜");
		html = html.replaceAll("&#8209;","‑");
		html = html.replaceAll("&ndash;|&#8211;","–");
		html = html.replaceAll("&mdash;|&#8212;","—");
		html = html.replaceAll("&lsquo;|&#8216;","‘");
		html = html.replaceAll("&rsquo;|&#8217;","’");
		html = html.replaceAll("&sbquo;|&#8218;","‚");
		html = html.replaceAll("&ldquo;|&#8220;","“");
		html = html.replaceAll("&rdquo;|&#8221;","”");
		html = html.replaceAll("&bdquo;|&#8222;","„");
		html = html.replaceAll("&dagger;|&#8224;","†");
		html = html.replaceAll("&Dagger;|&#8225;","‡");
		html = html.replaceAll("&permil;|&#8240;","‰");
		html = html.replaceAll("&lsaquo;|&#8249;","‹");
		html = html.replaceAll("&rsaquo;|&#8250;","›");
		html = html.replaceAll("&euro;|&#8364;","€");
		html = html.replaceAll("&#8470;","№");
		html = html.replaceAll("&#769;","́");

		html = html.replaceAll("&(.*?);","");//Удаляем все остальные мнемоники
			
		html = html.replaceAll("< *img( .*?)?src *= *('|\")(.*?)('|\")(.*?)? */? *>","$3");
		html = html.replaceAll("< *a( .*?)?href *= *('|\")(.*?)('|\")(.*?)? */? *>","$3 ");
		html = html.replaceAll("< *iframe( .*?)?src *= *('|\")(.*?)('|\")(.*?)? */? *>","$3");
		html = html.replaceAll("< *embed( .*?)?src *= *('|\")(.*?)('|\")(.*?)? */? *>","$3");
		html = html.replaceAll("\n( *)?","");
		html = html.replaceAll("< *br */? *>","\n");
		html = html.replaceAll("<!--.*?-->","");
		html = html.replaceAll("<!-- *","");
		html = html.replaceAll(" *-->","");
		html = html.replaceAll("((<code>(.*?)</code>)|(<.*?>))","$2");
		html = html.replaceAll("< *code( .*?)?>","[ ");
		html = html.replaceAll("< */ *code *>"," ]");
		
		html = html.replaceAll("\\<.*?>","");
		return html;
	}
	
	
}
/*******************************************************************************************************************/
class DataBase
{
	public static final Logger LOG=Logger.getLogger(JabberBot.class);
	Connection conn;
	Statement st = null;
	String str = "Test connect to MySQL\n\n";
	public DataBase() 
	{
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/RSSJabberBot", "user", "password");
			st = conn.createStatement();
			
		}catch(Exception e){LOG.error("ERROR_SQL:",e);}
	}
	//Получаем все RSS каналы из базы данных
	public List<String> getRSSFeeds()
	{
		//System.out.println("Select all RSS feeds.");
		List<String> rss_list = new ArrayList<String>();
		try 
		{
			ResultSet rs = st.executeQuery("SELECT * FROM RSS_feeds ORDER BY id ASC");
			while(rs.next()){rss_list.add(rs.getString("id")+"*"+rs.getString("rss"));}
			rs.close();
		}catch(Exception e){LOG.error("ERROR_SQL:",e);}
		return rss_list;
	}
	//Проверяем новые записи в ленте
	public List<String> getNew(String name_table, String RSS_feed)
	{
		LOG.info(name_table+"("+RSS_feed+")");
		String msg = "";//Переменная для формирования сообщения в bb кодах
		String msg_bboff = "";//Переменная для формирования сообщения без bb кодов
		//Получаем заголовоки ленты
		try 
		{
			try 
			{
				URL feedUrl = null; 
				feedUrl = new URL(RSS_feed);
				
				try
				{
					XmlReader reader = null; 
					reader =  new XmlReader(feedUrl);
					
					SyndFeedInput input = null; 
					input = new SyndFeedInput();
					
					try 
		            {
		            	SyndFeed feed = null; 
		            	feed = input.build(reader);
		            	
		            	//Проверка обновления ленты, если дата публикации ленты новая то тащим новости если нет то следующая лента
		                String pubDate = "NULL";
		                try 
		                {
		                	//Получаем дату публикации из XML
		                	//LOG.info("//Получаем дату публикации из XML");
		                	pubDate = feed.getPublishedDate().toString();
		                	            	
		    			}catch(Exception e)
		    			{
		    				//Даты публикации нету в XML файле
		    				//LOG.info("//Даты публикации нету в XML файле");
		    				if(e.getMessage() == null){pubDate = "NULL";}
		    			}
		                
		                ResultSet rsDate = st.executeQuery("SELECT * FROM RSS_feeds WHERE rss='"+RSS_feed+"'");
		                if(rsDate.next())
		                {
		                	//RSS канал найдет в списке
		                	//LOG.info("//RSS канал найдет в списке");
		                	String pubDateSQL = rsDate.getString("PublishedDate");//Получаем дату публикации из базы данных
		                	//Если даты равны не проверяем, если не равны проверяем, если NULL всегда проверяем
		                	if((pubDateSQL.equals(pubDate))&&(pubDate!="NULL")){LOG.info("Date null OR equal");}
		                	else
		                	{
		                		//LOG.info("Dates are not equal, update feed...");
		                		//Сохраняем новую дату публикации
		                		st.executeUpdate("UPDATE RSS_feeds SET PublishedDate='"+pubDate+"' WHERE rss='"+RSS_feed+"'");
		                		
		                		//Получаем новые ленты
		                		for (Object object : feed.getEntries())
		                        {
		                			//LOG.info("//Получаем новые ленты");
		                            SyndEntry entry = (SyndEntry) object;
		                            //----------------------------------------------------------//
		                            String title = "";
		                            title = entry.getTitle();
		                            if(title == null){title = entry.getLink();}
		                            
		                            //Если даты публикации новости нет, тогда ее заменяет ссылка на новость
		                            //В бд заноситься именно ссылка а отображется NULL
		                            String date = "NULL";
		                            String msg_date = "";
		                            try
		                            {
		                            	msg_date = date = entry.getPublishedDate().toString();
		                            }
		                            catch(Exception e)
		                            {
		                            	if(e.getMessage() == null){date = entry.getLink();msg_date = "empty";}
		                            }
		                            
		                            title = title.replaceAll("'", "\\\\'");//Экранируме символ, для записи в бд
		                            
		                            //Ищем тему в таблице RSS_n ленты совпадения по заголовку и дате
		                        	ResultSet rs = st.executeQuery("SELECT * FROM "+name_table+" WHERE title='"+title+"' AND PublishedDate='"+date+"'");
		                            if(!rs.next())//Темы в таблице нет, значит новая тема
		                            {
		                            	//LOG.info("//Темы в таблице нет, значит новая тема");
		                            	//Новая тема новости, формируем сообщение и сохраняем заголовок новости и дату либо ссылку на новость
		                            	st.executeUpdate("INSERT INTO "+name_table+" VALUES(NULL,'"+title+"','"+date+"')");
		                            	title = title.replaceAll("\\\\'","'");//Убираем экранирование чтобы вывести заголовок нормально
		                            	                            	
		                            	/*bb off*/msg_bboff +=title+"\n";
		                            	/*bb off*/msg_bboff +=entry.getLink()+"\n";
		                            	
		                            	msg +="[quote]";
		                            	msg +="[url=\""+entry.getLink()+"\"]"+title+"[/url]\n";
		                            	String Author = "NULL";
		                                try{Author = feed.getAuthor().toString();}catch(Exception e){if(e.getMessage() == null){Author = "NULL";}}
		                                
		                                /*bb off*/msg_bboff +="Автор: "+Author+" ("+msg_date+")\n";
		                                
		                            	msg +="[i][size=\"8\"]Автор: "+Author+" ("+msg_date+")[/size][/i]";
		                            	msg +="[hr]";
		                            	for(Object contobj : entry.getContents())
		                            	{
		                            		SyndContent cont = (SyndContent)contobj;
		                            		msg +=html2bb.parse(cont.getValue());
		                            		/*bb off*/msg_bboff +=html2bb.parse_bboff(cont.getValue());
		                            	}
		                            	//Добавляем перенос строки, так как может быть контент и в getContents и в getDescription
		                            	if(entry.getContents().size() != 0)
		                                {
		                                	if((entry.getDescription())!= null)
		                                	{
		                                		msg +="\n\n";
		                                		/*bb off*/msg_bboff +="\n---\n";
		                                	}
		                                }
		                            	
		                            	SyndContent content = entry.getDescription();
		                            	if(content != null)
		                            	{
		                            		msg +=html2bb.parse(content.getValue());
		                            		/*bb off*/msg_bboff +=html2bb.parse_bboff(content.getValue());
		                            	}
		                            	msg +="[/quote]\n";
		                            	/*bb off*/msg_bboff +="\n------------------------------------------------------------------\n";
		                            }
		                            rs.close();
		                        }
		            			//----------------------------------------------------------//
		                	}
		                	
		                }
		                rsDate.close();
		                reader.close();
		                
					}catch (Exception e){LOG.error("ERROR_FEED:",e);}
				}catch (Exception e){LOG.error("ERROR_XmlReader:",e);}
			}catch(Exception e){LOG.error("ERROR_URL:",e);}
        }catch(Exception e){LOG.error("ERROR_RSS:",e);}
		
		//Добавляем в List сообщения с бб кодами и без бб кодов
		List<String> message = new ArrayList<String>();
		message.add(msg);
		message.add(msg_bboff);
		
		return message;
	}
	
	//Получаем список пользователей подписаных на указаную ленту без паузы и с отображением ББ кодов 
	public List<String> getUsersRSS(String feed_id)
	{
		List<String> rss_users = new ArrayList<String>();
		try 
		{
			//Получаем тех пользователей у которых отключенна пауза и ббкоды отображаются при том новосоти все равно сохраняются
			ResultSet rs = st.executeQuery("SELECT jabber FROM USERS WHERE id_rss='"+feed_id+"' AND pause=0 AND bbcode=0");
			while(rs.next())
			{
				rss_users.add(rs.getString("jabber"));
			}
			rs.close();
			
		}catch(Exception e){LOG.error("ERROR_URL:",e);}
		return rss_users;
	}
	//Получаем список пользователей подписаных на указаную ленту без паузы и без отображением ББ кодов
	public List<String> getUsersRSS_bboff(String feed_id)
	{
		List<String> rss_users = new ArrayList<String>();
		try 
		{
			//Получаем тех пользователей у которых отключенна пауза и ббкоды НЕ отображаются  при том новосоти все равно сохраняются
			ResultSet rs = st.executeQuery("SELECT jabber FROM USERS WHERE id_rss='"+feed_id+"' AND pause=0 AND bbcode=1");
			while(rs.next())
			{
				rss_users.add(rs.getString("jabber"));
			}
			rs.close();
			
		}catch(Exception e){LOG.error("ERROR_URL:",e);}
		return rss_users;
	}
	
	//Устанавливаем/отключаем паузу для всех подписок 
	public String setPause(String jid,String pause)
	{
		String message = "";
		try 
		{
			//Проверяем есть ли подписки у пользователя для установки паузы
			ResultSet rs = st.executeQuery("SELECT jabber FROM USERS WHERE jabber='"+jid+"'");
			if(rs.next())
            {
				LOG.info("Общая пауза: "+jid);
				if(pause.equals("on")){st.executeUpdate("UPDATE USERS SET pause=1 WHERE jabber='"+jid+"'");message="Общая пауза для всех подписок включена.";}
				if(pause.equals("off")){st.executeUpdate("UPDATE USERS SET pause=0 WHERE jabber='"+jid+"'");message="Общая пауза для всех подписок выключена.";}
            }
			rs.close();
			
		}catch(Exception e){LOG.error("ERROR_PAUSE:",e);message="Ошибка включения/выключения паузы.";}
		return message;
	}
	//Отключение/включение бб кодов
	public String BBcode(String jid,String pause)
	{
		String message = "";
		try 
		{
			//Проверяем есть ли подписки у пользователя для установки паузы
			ResultSet rs = st.executeQuery("SELECT jabber FROM USERS WHERE jabber='"+jid+"'");
			if(rs.next())
            {
				LOG.info("Отключение/включение бб кодов: "+jid);
				if(pause.equals("on")){st.executeUpdate("UPDATE USERS SET bbcode=0 WHERE jabber='"+jid+"'");message="BB коды включены.";}
				if(pause.equals("off")){st.executeUpdate("UPDATE USERS SET bbcode=1 WHERE jabber='"+jid+"'");message="BB коды выключены.";}
            }
			rs.close();
			
		}catch(Exception e){LOG.error("ERROR_PAUSE:",e);message="Ошибка включения/выключения BB кодов.";}
		return message;
	}
	
	//Сохраняем RSS ленту, создаем необходимую таблицу и записи
	public int saveRSS(String feed_url,String jid)
	{
		LOG.info("---------------------------------------------");
		LOG.info("Subscribe: "+jid+" feed:[ "+feed_url+" ]");
		int i = 0;
		try 
		{
			new URL(feed_url);
			try 
			{
				ResultSet rs = st.executeQuery("SELECT * FROM RSS_feeds WHERE rss='"+feed_url+"'");
				if(!rs.next())//Лента новая, сохраняем, делаем подписку и создаем для нее таблицу
                {
					LOG.info("New feed");
					st.executeUpdate("INSERT INTO RSS_feeds VALUE(NULL,'"+feed_url+"','')",Statement.RETURN_GENERATED_KEYS);
					rs = st.getGeneratedKeys();
					if(rs.next())
					{
						int id = rs.getInt(1);
						st.executeUpdate("CREATE TABLE RSS_"+id+" (id INT NOT NULL AUTO_INCREMENT,title TEXT NOT NULL,PublishedDate TEXT NOT NULL, PRIMARY KEY (id),INDEX ( id ))");
						st.executeUpdate("INSERT INTO USERS VALUE(NULL,'"+jid+"','"+id+"',0,0)");
					}
					i = 1;
                }
				else//Лента уже есть в списке, делаем только подписку
				{
					LOG.info("New user for feed: "+feed_url);
					int id = rs.getInt(1);//Узанем id ленты
					//Проверить подписку
					rs = st.executeQuery("SELECT * FROM USERS WHERE jabber='"+jid+"' AND id_rss='"+id+"'");
					if(!rs.next())
	                {
						//Подписываем пользователя
						st.executeUpdate("INSERT INTO USERS VALUE(NULL,'"+jid+"','"+id+"',0,0)");
						i = 1;
	                }else{i = 2;/*Уже подписан, повторно посылает*/}
				}
				rs.close();
			}catch(SQLException e){i=-1;LOG.error("ERROR_SQL:",e);}
			
		}catch(MalformedURLException e){i=-1;LOG.error("ERROR_URL:",e);}
		
		LOG.info("---------------------------------------------");
		return i;
	}
	//Получаем ленты на которые подписан пользователь
	public String listUserRSS(String jid)
	{
		String msg = "";
		ResultSet rs;
		try 
		{
			List<String> rss_id = new ArrayList<String>();
			List<String> id_sub = new ArrayList<String>();
			List<String> pause_state = new ArrayList<String>();
			//Собираем id каналов на которые подписан пользователь
			rs = st.executeQuery("SELECT * FROM USERS WHERE jabber='"+jid+"'");
			while(rs.next())
			{
				rss_id.add(rs.getString("id_rss"));
				id_sub.add(rs.getString("id"));
				pause_state.add(rs.getString("pause"));
			}
			
			msg += "Ваши каналы (Your feeds):\n\n";
			rs.close();
			
			int rx = 0;//Переменная для получения id подписки пользователя из массива id_sub
			for(String str: rss_id) 
			{
				//Выводим имена подписок
				rs = st.executeQuery("SELECT * FROM RSS_feeds WHERE id='"+str+"'");
				if(rs.next())
				{
					String pause = "";if((pause_state.get(rx)).equals("0")){pause="Отключена";}else{pause="Включена";}
					
					msg += "Пауза:["+pause+"] ";
					msg += rs.getString("rss");
					msg += " - Вкл/Выкл паузу: P "+id_sub.get(rx)+" - Удалить подписку: D "+id_sub.get(rx)+"\n";
					rx++;
				}				
			}
			rs.close();
			
		}catch(SQLException e){LOG.error("ERROR_SQL:",e);}
		return msg;
	}
	
	//Устанавливаем паузу для конкретной ленты пользователя
	public String pauseFeed(String jid,String id)
	{
		String message = "";
		LOG.info("|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
		LOG.info("PAUSE a feed: "+jid+" id: "+id);
		ResultSet rs;
		try
		{
			try 
			{
				//Проверяем паузу ставит он именно свою подписку, для этого jid и id подписки должны быть равны
				rs = st.executeQuery("SELECT pause FROM USERS WHERE jabber='"+jid+"' AND id='"+id+"'");
				if(rs.next())
	            {
					if(rs.getString("pause").equals("0"))
					{
						st.executeUpdate("UPDATE USERS SET pause=1 WHERE jabber='"+jid+"' AND id='"+id+"'");
						message="Пауза для подписки включена.";
					}else
					{
						st.executeUpdate("UPDATE USERS SET pause=0 WHERE jabber='"+jid+"' AND id='"+id+"'");
						message="Пауза для подписки выключена.";
					}
									
	            }else{message="Ошибка, проверьте правильность вводимых данных.";}
				
				rs.close();
				
			}catch(Exception e) {LOG.error("ERROR_SQL2:",e);}
			
		}catch(Exception e){LOG.error("ERROR_SQL1:",e);}
		
		LOG.info("|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
		return message;
	}
	
	//Удаление подписки пользователя + проверка, на пустые каналы без пользователей
	public int deleteRSS(String jid,String id)
	{
		LOG.info("---------------------------------------------");
		LOG.info("Deleting a subscription: "+jid+" id: "+id);
		int i = 0;
		ResultSet rs;
		try
		{
			//Проверяем удаляет ли он именно свою подписку, для этого jid и id подписки должны быть равны
			rs = st.executeQuery("SELECT * FROM USERS WHERE jabber='"+jid+"' AND id='"+id+"'");
			if(rs.next())
            {
				i=2;
				st.executeUpdate("DELETE FROM USERS WHERE jabber='"+jid+"' AND id='"+id+"'");
				//Проверка на пустые подписки
				deleteEmpty();
				
            }else{i=1;}
			rs.close();
			
		}catch(Exception e){LOG.error("ERROR_SQL:",e);}
		
		if(i==1){LOG.info("Status: ERROR ID");}
		if(i==2){LOG.info("Status: OK");}
		LOG.info("---------------------------------------------");
		return i;
	}
	
	//Удаляем пустые подписки, каналы на которые ни кто не подписан
	public void deleteEmpty()
	{
		LOG.info("---------------------------------------------");
		LOG.info("Remove empty subscriptions START");
		ResultSet rs;
		for (String string : getRSSFeeds())//Получить id всех каналов из таблицы RSS_feeds
		{
			//Получаем одну RSS ленту
			int i = string.indexOf("*");
			String id = string.substring(0,i);
			String feed = string.substring(i+1,string.length());
			//Проверить есть ли id в таблице USERS если нету пустая подписка
			try 
			{
				rs = st.executeQuery("SELECT * FROM USERS WHERE id_rss='"+id+"'");
				if(!rs.next())
	            {
					LOG.info("Empty feed: "+feed);
					//Удалить запись из таблицы RSS_feeds и таблицу RSS_id
					st.executeUpdate("DELETE FROM RSS_feeds WHERE id='"+id+"'");
					st.executeUpdate("DROP TABLE RSS_"+id);
	            }
				rs.close();
			}catch(SQLException e){LOG.error("ERROR_SQL:",e);}
        }
		LOG.info("Remove empty subscriptions END");
		LOG.info("---------------------------------------------");
	}
	
}
/*******************************************************************************************************************/
class JabberBot implements Runnable
{
	public static final Logger LOG=Logger.getLogger(JabberBot.class);
	private ConnectionConfiguration connConfig;
    private XMPPConnection connection;
    private boolean status = true;
	 
	@Override
	public void run()
	{
		LOG.info("Run JabberBot thread...");
		connConfig = new ConnectionConfiguration("server",5222,"domen");
		SASLAuthentication.supportSASLMechanism("PLAIN");
		connConfig.setCompressionEnabled(false);
    	connConfig.setSASLAuthenticationEnabled(true);
    	connection = new XMPPConnection(connConfig);
        try
        {
        	connection.connect();
            connection.login("login","password","resource");
            Presence presence = new Presence(Presence.Type.available);
            presence.setStatus("RSS Jabber Bot");
            presence.setPriority(30);
            connection.sendPacket(presence);
            PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class));
            PacketListener myListener = new PacketListener() 
            {
                public void processPacket(Packet packet) 
                {
                	if(packet instanceof Message) 
                    {
                        Message message    = (Message) packet;
                        String messageBody = message.getBody();
                        String JID         = message.getFrom();
                        LOG.info("---------------------------------------------[MESSAGE START]");
                        if(messageBody == null)
                        {
                        	messageBody = "";
                        	Collection<Message.Body> bodies = message.getBodies();
                            for(Message.Body r:bodies){messageBody += r.getMessage();}
                        }
                        LOG.info(JID+": "+messageBody);
                        LOG.info("---------------------------------------------[MESSAGE END]");
                        
                        JID = JID.replaceAll("(.*?)[/].*","$1");
                        
                        DataBase db = new DataBase();
                        
                        
                        messageBody = messageBody.trim();
                        String command = messageBody.replaceAll("^(.*)[ ](.*)$","$1");
                        if(messageBody.indexOf(" ")!=-1){messageBody = messageBody.replaceAll("^(.*)[ ](.*)$"," $2");}else{messageBody="";}
                        messageBody = (command.toLowerCase().trim()+messageBody);
                       
                        //command only for admin
                        if(JID.equals("admin jid"))
                        {
                        	if(messageBody.equals("roster")){getRoster();}
                        	if(messageBody.equals("close")){connection.disconnect();status = false;LOG.info("Выход из программы!");System.exit(0);}
                        	if(messageBody.equals("empty")){db.deleteEmpty();}
                        }
                         //common commands for all
                        switch(messageBody)
                    	{
                        	case "ping":sendMessage(JID,"pong");break;
                        	case "list":sendMessage(JID,db.listUserRSS(JID));break;
                        	case "pause on":sendMessage(JID,db.setPause(JID,"on"));break;//Общая пауза включить
                        	case "pause off":sendMessage(JID,db.setPause(JID,"off"));break;//Общая пауза выключить
                        	case "bb on":sendMessage(JID,db.BBcode(JID,"on"));break;//Включить ВВ коды
                        	case "bb off":sendMessage(JID,db.BBcode(JID,"off"));break;//Отключить ВВ коды
                    		case "help":
                    		{
                    			String help = "rssbot@qip.ru - XMPP(jabber) бот, рассылающий новостные RSS ленты, оформленные в BB-коды.\n" +
                    					"--------------------------------------------------------\n" +
                    					"Подписка на ленту новостей:\n" +
                    					"s rss_url [где rss_url, URL адрес RSS ленты, на которую вы хотите подписаться]\n" +
                    					"\n" +
                    					"Вывод списка лент, на которые вы подписаны и состояние паузы:\n" +
                    					"list\n" +
                    					"\n" +
                    					"Удаление подписки на ленту новостей:\n" +
                    					"d 123 [где 123, идентификатор вашей подписки, узнать который можно при помощи  команды list]\n" +
                    					"\n" +
                    					"Включение/Отключение паузы на определенную ленту:\n" +
                    					"p 123 [где 123, идентификатор вашей подписки, узнать который можно при помощи  команды list]\n" +
                    					"\n" +
                    					"Включение общей паузы для всех подписок:\n" +
                    					"pause on\n" +
                    					"Отключение общей паузы для всех подписок:\n" +
                    					"pause off\n" +
                    					"\n" +
                    					"Включение BB кодов в сообщениях:\n" +
                    					"bb on\n" +
                    					"Отключение BB кодов в сообщениях:\n" +
                    					"bb off\n" +
                    					"\n" +
                    					"Проверка связи с ботом:\n" +
                    					"ping\n" +
                    					"\n" +
                    					"Вывод справки:\n" +
                    					"help\n" +
                    					"--------------------------------------------------------\n" +
                    					"Для того чтобы начать получать новостные ленты, достаточно добавить себе в контакт лист jid: rssbot@qip.ru и подписаться на RSS ленту и после этого вам будут приходить новости в виде сообщений оформленных в BB кодах.\n" +
                    					"\n" +
                    					"Предупреждение:\n" +
                    					"Версия бота находиться в стадии бета-тестирования, возможны проблемы с распознаванием html кода в теле RSS ленты, так же иные проблемы, просьба сообщить о замеченных недочетах по адресу jabber: lazy_elf@qip.ru\n" +
                    					"\n" +
                    					"Страница программы : http://blagin.ru/rssbot/ Конференция для обсуждения работы бота: rssbot@conference.qip.ru";
                    			sendMessage(JID,help);
                    			
                    		}break;
                    		default:
                    		{
                    			switch(messageBody.substring(0,2).toLowerCase())
                    			{
									case "s ":
									{
										String rss_url = messageBody.substring(2,messageBody.length()).trim();
										JID = JID.replaceAll("(.*?)[/].*","$1");
										int i = db.saveRSS(rss_url,JID);
										if(i == 1){sendMessage(JID,"Вы подписаны (You are subscribed).");}
										if(i == 2){sendMessage(JID,"Вы уже подписаны на данную ленту новостей (You are already subscribed to this feed).");}
										
									}break;
									
									case "d ":
									{
										String id = messageBody.substring(2,messageBody.length()).trim();
										JID = JID.replaceAll("(.*?)[/].*","$1");
										int i = db.deleteRSS(JID,id);
										if(i==1){sendMessage(JID,"Ошибка номера подписки, наберите команду [i]list[/i] (Error number of subscription, type in the command [i]list[/i]).");}
										if(i==2){sendMessage(JID,"Ваша подписка удалена (Your subscription is removed).");}
										
									}break;
									case "p ":
									{
										String id = messageBody.substring(2,messageBody.length()).trim();
										JID = JID.replaceAll("(.*?)[/].*","$1");
										sendMessage(JID,db.pauseFeed(JID,id));
										
									}break;

									default:/*sendMessage(JID,"Unknown command");*/break;
								}//end switch
                    			
                    		}break;
						}//end switch
                                                

                    }
                }
            };
            connection.addPacketListener(myListener, filter);

            Roster roster = connection.getRoster();
            roster.addRosterListener(new RosterListener() 
            {
            	public void entriesAdded(Collection<String> addresses) 
				{
            		LOG.info("//////////////////////////////////////////////////////////");
            		LOG.info("Added a new account: "+addresses);
            		LOG.info("//////////////////////////////////////////////////////////");
				}
				public void entriesDeleted(Collection<String> addresses){}
                public void entriesUpdated(Collection<String> addresses){}
                public void presenceChanged(Presence presence){/*System.out.println("Presence changed: " + presence.getFrom() + " " + presence);*/}
            });
            
            while(status)
    		{
            	//LOG.info("----------------[ While status true ]----------------");	
            	if(connection.isConnected())
            	{
            		//LOG.info("----------------[ Connection is connected ]----------------");
            		try 
        			{
        				//Получаем все RSS каналы из базы данных
        				DataBase db = new DataBase();
        				for (String string : db.getRSSFeeds())
        				{
        					//Получаем одну RSS ленту
        					int i = string.indexOf("*");
        					String id = string.substring(0,i);
        					String feed = string.substring(i+1,string.length());
        					
        					//Проверяем и получаем новые записи для этой ленты
        					List<String> data =  db.getNew("RSS_"+id, feed);
        					
        					//Отделяем сообщения с ббкодами и без них
        					String messages = data.get(0);
        					String messages_bboff = data.get(1);
        					
        					//--------------------------------------------------------------------------------------------------------------------        					
        					//Отправка сообщений с BB кодами
        					if(messages.length()!=0)
        					{
        						//Узнаеем кто подписан на текущую ленту и отсылаем ему сообщение с новыми новостями из ленты
        						//LOG.info("----------------[ NEW titles... ]----------------");
        						for (String jid : db.getUsersRSS(id))
        						{
        							sendMessage(jid,messages);
        						}
        						
        					}else{/*LOG.info("----------------[ No new titles... ]----------------");*/}
        					//--------------------------------------------------------------------------------------------------------------------
        					//Отправка сообщений без BB кодов
        					if(messages_bboff.length()!=0)
        					{
        						//Узнаеем кто подписан на текущую ленту и отсылаем ему сообщение с новыми новостями из ленты
        						//LOG.info("----------------[ NEW titles... ]----------------");
        						for (String jid : db.getUsersRSS_bboff(id))
        						{
        							sendMessage(jid,messages_bboff);
        						}
        						
        					}else{/*LOG.info("----------------[ No new titles... ]----------------");*/}
        					//--------------------------------------------------------------------------------------------------------------------
        			
        				}//end for
        				
        		    }catch(Exception e){LOG.error("ERROR_XMPP:",e);}
            		
            	}else{LOG.warn("XMPPbot is disconnecting!");}

            	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            	try{Thread.sleep(180000);}catch(Exception e){LOG.error("ERROR_THREAD:",e);}
            	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    			
    		}//end while
            
            
        }catch(Exception e){LOG.error("ERROR_XMPP:",e);}
	}
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void sendMessage(String to, String message)
	{
		if(!message.equals(""))
	    {
			ChatManager chatmanager = connection.getChatManager();
	        Chat newChat = chatmanager.createChat(to, null);
	        try
	        {
	        	newChat.sendMessage(message);
	        		        	
	        }catch(Exception e){LOG.error("ERROR_XMPP(sendMessage):",e);}
	    }
	}
	public void getRoster()
	{
		Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();
        String str = "Количество: "+entries.size() + "\n\n";
        for(RosterEntry r:entries){str += r.getUser()+"\n";}
        sendMessage("lazy_elf@qip.ru", str);
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class MainJabberBot 
{
	public static final Logger LOG=Logger.getLogger(MainJabberBot.class);
	public static void main(String[] args) 
	{
		LOG.info("Запуск программы...");
		//Запуск потока Jabber бота	
		try
        {
			Thread BotXmpp = new Thread(new JabberBot());
			BotXmpp.start();
        }catch(Exception e){LOG.error("Ошибка запуска потока:",e);}

	}
}


